ExamTray App is now Available on Google Play EXAMTRAY android app link

 

Java Ternary Operator (Conditional) Interview MCQ Questions and Answers

Study and learn Interview MCQ Questions and Answers on Java Ternary Operator or Conditional Operator. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.

Go through Java Theory Notes on Ternary Operator before reading these objective questions.



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) State TRUE or FALSE. True expression part comes first after ? (question mark) symbol and before : (colon) symbol.
A) FALSE
B) TRUE
C) -
D) -
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
C) -
D) -
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 Java 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 can not skip any of the two expressions of a Ternary or Conditional operator (?:).