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.
The True/False part expression should evaluate to a constant literal.
int p=5; System.out.print("Hello "); (p<6)?5:6;
Unresolved compilation problems:
The left-hand side of an assignment must be a variable
int num = false?10:20; System.out.println(num);
String name = "java"; int marks = name == "java"?10:20; System.out.println("Marks=" + marks);
String name = "cat"; int marks = name == "Cat"?10:20; System.out.println("Marks=" + marks);
String name1 = "pen"; String name2 = "pen"; int marks = name2.equals(name1)?50:80; System.out.println("Marks=" + marks);
void show() { int num = true ? getNumber() : 20; System.out.print("TOMATO=" + num); } void getNumber() { System.out.print(30); }
The "void" type is not allowed as an Operand of a Ternary or Conditional operator.
void show() { String name = true ? getName() : "FRANCE"; System.out.print(name); } void getName() { System.out.print("ENGLAND"); }
The void functions are not allowed inside a Ternay operator statement.
String forest = null; String output = forest != null ? "Goblin" : "Amazon"; System.out.println(output);
int a = 20, b=30; int total = a>10&&b<10?65:75; System.out.println(total);
int a = 20, b=30; boolean result = a&b?true:false; System.out.println(result);
int a = 4, b=7; int result = (true?a&b:a|b)>3?120:150; System.out.println(result);
final int a = 25, b=33; String name = !true?"Dino":"Tom"; System.out.println(name);
int a = 25, b=33; String name = true?"CAT":; System.out.println(name);
You can not skip any of the two expressions of a Ternary or Conditional operator (?:).