Ternary Operator in Java (Conditional) Interview MCQ
Table of Contents
Study and learn Interview MCQ Questions and Answers on Ternary Operator in Java or Conditional Operator. Attend job interviews easily with these Multiple Choice Questions
Java language has a special operator called Ternary Operator that works like a Shortcut IF ELSE control statement. It is also called a Conditional Operator.
Ternary Operator in Java (?:) or Conditional Operator Explained
Ternary Operator in Java (?:) or Question Mark Colon Operator tests a condition and moves the control to two branching points, one for true and one for false.
Syntax:
(condition)? (true part expression): (false part expression)
The condition can be a direct relational operation or a boolean logical expression or a simple true/false. True Part and False Part must return a value. So you can not write a Ternary operator statement that does not return anything. You can not even put Functions returning void.
You can use Ternary Operator with any control statement that expects a boolean type value. So, you can use Ternary Operator with IF, ELSE IF, WHILE, DO WHILE, FOR and more.
Ternary operator has got the least priority among all other operators like Arithmetic, Relational, Bitwise and Logical. It has more priority than Assignment operators and Lambda operator.
Example Error 1: Assignments and Return Value in Ternary Operator
You can not use Ternary Operator alone as a standalone statement. So, do not write a Ternary operator without an assignment operation. Also return value is a must. You can not put void returning method calls inside branching expressions of a Ternary Operator. You get the following errors.
- Syntax error, insert “AssignmentOperator Expression” to complete Expression
- The left-hand side of an assignment must be a variable
- void is an invalid type for the variable
- Type mismatch: cannot convert from void to int
class TernaryOperator1 { public static void main(String args[]) { int a =5; (a<6)?5:6; //Error int b = (a<6)?5:6; //WORKS int c = (a>6)?5:6; //WORKS int d = true?show():show(); //ERROR. No Return Value } static void show() { System.out.println("SHOW METHOD"); } } //ERROR: //Left Hand side of an assignment must be a variable //Solution: Use a variable and assign //Now b holds 5 as a<6 is true //Now c holds 6 as a>6 is false //show method returns void. It is not valid in Java.
Example 2: Ternary Operator in Java with boolean values
You can put an expression resulting in any data type say byte, short, char, int, long, float, double, String and Object as part of TRUE PART or FALSE PART of a Ternary Operator.
class TernaryOperator2 { public static void main(String args[]) { boolean b; b = false?2<4: 5>7; //false?false:false } } //Now b = 5>7 ==> holds false
Example 3: Ternary Operator in Java with null object Checking
An object occupies memory. If it is set to NULL (null), the memory occupied by it will be released by the Garbage Collector. If an object is null, you can not use or reference any of its member variables or methods.
class TernaryOperator3 { public static void main(String args[]) { String a = "Hello Java", b=null; b = (b==null)?a: b; //b =a System.out.println(b); b = (b==null)?a: "2 "+ b; // b = "2 " + b System.out.println(b); } } //Hello Java //2 Hello Java
Example 4: String Assignments Inside in java
You can do assignment operations inside TRUE Part or FALSE Part of a Ternary Operator. Make sure, the return type is compatible with the assigning constants. We have done String assignments in this example.
class TernaryOperator4 { public static void main(String args[]) { String a = "Hello Java"; String b = a!=null? a="HELLO":(a="JAVA"); System.out.println(b); } } //OUTPUT //HELLO
Example 5: Nesting of Ternary Operator with Multiple Conditions
You can nest one Ternary operator inside another Ternary operator. You should carefully select the output data type of one Ternary operator sitting inside another ternary operator. Here, the first condition is NULL comparison and the second condition is EQUALS comparison.
class TernaryOperator5 { public static void main(String args[]) { String a = "Java"; String b = a!=null? a.equals("Java")?"YES JAVA":"NO JAVA":"JAVA ISLAND"; //a!=null? (a.equals("Java")?"YES JAVA":"NO JAVA"):"JAVA ISLAND"; String a = null; String b = a!=null? a.equals("Java")?"YES JAVA":"NO JAVA":"JAVA ISLAND"; System.out.println(b); } } //OUTPUT //YES JAVA //JAVA ISLAND
Example 6: Using IF ELSE statements in java
You can use Ternary Operator inside an IF or ELSE IF statements if it is returning a boolean data type constant.
class TernaryOperator6 { public static void main(String args[]) { if(2>0?true:5<10) { System.out.println("Using Ternary Inside IF"); } } } //OUTPUT //Using Ternary Inside IF
Practice these examples to get more knowledge
[WpProQuiz 86]
Ternary Operator in Java Interview MCQ
1) What is the other name for a Question Mark – Colon (?:) operator in Java?
A) Special Relational operator
B) Special Logical Operator
C) Ternary Operator
D) None
Answer [=] C
2) Java Ternary operator is sometimes called ____.
A) Relational Operator
B) Conditional Operator
C) Logical Operator
D) None
Answer [=] B
3) The condition of a Java Ternary operator should evaluate to ___.
A) 1 or 0
B) true or false
C) TRUE or FALSE
D) None
Answer [=] B
4) True expression part comes first after ? (question mark) symbol and before : (colon) symbol ?
A) FALSE
B) TRUE
Answer [=] B
5) Java Ternary operator can be used with ___.
A) if-else statements
B) while, do while loops
C) for loop, enhanced for loop
D) All
Answer [=] D
6) A java Ternary operator has priority less than ___.
A) Relational operators
B) Arithmetic operators
C) Logical and bitwise operators
D) All
Answer [=] D
7) Java assignment operator has priority more than ___.
A) Assignment and Lambda operator
B) Logical and bitwise operator
C) Arithmetic operators
D) Logical operators
Answer [=] A
8) The True Part Expression of a Java conditional operator or Ternary operator ____ return a value.
A) may
B) can
C) must
D) None
Answer [=] C
Explanation:
The True/False part expression should evaluate to a constant literal.
9) The False Part Expression of a Java conditional operator or Ternary operator ____ return a value.
A) may
B) can
C) must
D) None
Answer [=] C
10) Choose a possible error with a Ternary operator while compiling a Java program.
A) The left-hand side of an assignment must be a variable.
B) void is an invalid type for the variable
C) Type mismatch: cannot convert from void to int
D) All
Answer [=] D
11) You can nest one Java Ternary operator inside another Ternary operator. State TRUE or FALSE.
A) FALSE
B) TRUE
Answer [=] B
12) What is the output of the code snippet with the ternary operator?
int p=5; System.out.print("Hello "); (p<6)?5:6;
A) Hello 5
B) Hello 6
C) Hello
D) Compiler error
Answer [=] D
Explanation:
Unresolved compilation problems:
The left-hand side of an assignment must be a variable
13) What is the output of the Java code snippet with Ternary operator?
int num = false?10:20; System.out.println(num);
A) 10
B) 20
C) 0
D) Compiler error
Answer [=] B
14) What is the output of the Java code snippet with Ternary operator?
String name = "java"; int marks = name == "java"?10:20; System.out.println("Marks=" + marks);
A) marks=0
B) marks=10
C) marks=20
D) Compiler error
Answer [=] B
15) What is the output of the Java code snippet with a Ternary operator?
String name = "cat"; int marks = name == "Cat"?10:20; System.out.println("Marks=" + marks);
A) Marks=0
B) Marks=10
C) Marks=20
D) Compiler error
Answer [=] C
16) What is the output of the Java code snippet with a Ternary operator?
String name1 = "pen"; String name2 = "pen"; int marks = name2.equals(name1)?50:80; System.out.println("Marks=" + marks);
A) Marks=0
B) Marks=50
C) Marks=80
D) Compiler error
Answer [=] B
17) What is the output of the Java code snippet with a Ternary operator?
void show() { int num = true ? getNumber() : 20; System.out.print("TOMATO=" + num); } void getNumber() { System.out.print(30); }
A) TOMATO=0
B) TOMATO=20
C) TOMATO=30
D) Compiler error
Answer [=] D
Explanation:
The “void” type is not allowed as an Operand of a Ternary or Conditional operator.
18) What is the output of Java code snippet with a Ternary operator or Conditional operator?
void show() { String name = true ? getName() : "FRANCE"; System.out.print(name); } void getName() { System.out.print("ENGLAND"); }
A) Empty string
B) FRANCE
C) ENGLAND
D) Compiler error
Answer [=] D
Explanation:
The void functions are not allowed inside a Ternay operator statement.
19) What is the output of code snippet with a Ternary operator?
String forest = null; String output = forest != null ? "Goblin" : "Amazon"; System.out.println(output);
A) null
B) Goblin
C) Amazon
D) Compiler error
Answer [=] C
20) What is the output of the Java code snippet below?
int a = 20, b=30; int total = a>10&&b<10?65:75; System.out.println(total);
A) 0
B) 65
C) 75
D) Error: The left-hand side of an assignment must be a variable
Answer [=] C
21) What is the output of the Java code snippet below?
int a = 20, b=30; boolean result = a&b?true:false; System.out.println(result);
A) false
B) true
C) 0
D) Compiler error
Answer [=] D
22) What is the output of the Java code snippet below?
int a = 4, b=7; int result = (true?a&b:a|b)>3?120:150; System.out.println(result);
A) 4
B) 120
C) 150
D) Compiler error
Answer [=] B
23) What is the output of Java code snippet below?
final int a = 25, b=33; String name = !true?"Dino":"Tom"; System.out.println(name);
A) Empty
B) Dino
C) Tom
D) Compiler error
Answer [=] C
24) What is the output of Java code snippet below?
int a = 25, b=33; String name = true?"CAT":; System.out.println(name);
A) Empty
B) CAT
C) null
D) Compiler error
Answer [=] D
Explanation:
You cannot skip any of the two expressions of a Ternary or Conditional operator (?:).