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 [=]
Answer[=]
2) Java Ternary operator is sometimes called ____.
A) Relational Operator
B) Conditional Operator
C) Logical Operator
D) None
Answer [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
6) A java Ternary operator has priority less than ___.
A) Relational operators
B) Arithmetic operators
C) Logical and bitwise operators
D) All
Answer [=]
Answer[=]
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 [=]
Answer[=]


 

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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
11) You can nest one Java Ternary operator inside another Ternary operator. State TRUE or FALSE.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]


 

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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]


 

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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]


Like or Share

Show some care. Like or Subscribe. [FB]...[Youtube]

C MCQ App by ExamTray 

Android APP

Java MCQ App by ExamTray 

Android APP
ALL MCQ App by ExamTray Android APP

Ad

 

Try Some Java Books

Book Price
1. Java - The Complete Reference  Check Price
2. Core Java: An Integrated Approach, Black Book Check Price
3. Java All-in-One for Dummies Check Price
4. OCP Java SE 8: Programmer II Check Price
5. Programming with Java Check Price
6. Head First Java Check Price

We may get some affiliate commission for the above purchases.