Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
Q1) Java-style IF-ELSE statements are similar to ____.
a) C style
b) C++ Style
c) Both C and C++ style
d) None
View Answer
Answer:- c) Both C and C++ styleQ2) An ELSE statement must be preceded by ____ statement in Java.
a) IF
b) ELSE IF
c) IF or ELSE IF
d) None
View Answer
Answer:- c) IF or ELSE IFQ3) Choose the correct syntax of the Java IF statement below.
a)
if(condition)
//statement
b)
if(condition)
{
//statement
}
c)
if(condition)
{
//statement1
//statement2
}
d) All
View Answer
Answer:- d) AllQ4) An IF statement in Java is also a ____ statement.
a) boolean
b) conditional
c) iterative
d) optional
View Answer
Answer:- b) conditionalQ5) Every IF statement must be followed by an ELSE of ELSE-IF statement.
a) True
b) False
View Answer
Answer:- b) FALSEQ6) Find the output of the given Java program?
public class Main
{
public static void main(String[] args) {
if(1>3)
System.out.println("Know Program");
}
}
a) Know Program
b) Other Output
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- c) Compiled Successfully, No Output.Here 1>3 hence condition becomes false because 1 is not greater than 3. Therefore if block will not be executed.
Q7) Find the output of the given Java program?
public class Main
{
public static void main(String[] args) {
if(5<4)
System.out.println("Hi");
else
System.out.println("Hello");
}
}
a) Hi
b) Hello
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- b) HelloHere 5<4 hence the condition becomes false because 5 is not lesser than 4. Therefore else block will be executed.
Q8) What is the output of the given below program?
public class Main
{
public static void main(String[] args) {
if(a<b)
System.out.println("Hi");
else
System.out.println("Hello");
}
}
a) Hi
b) Hello
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- d) Compile-time errorIn Java, all variables must be declared before using it. Here “a”, and “b” variables are not declared.
Q9) Find the output of the given Java program?
public class Main
{
public static void main(String[] args) {
boolean x, y, z;
x = y = z = true;
if(!x || (!y && !z))
System.out.println("Hi");
else
System.out.println("Hello");
}
}
a) Hi
b) Hello
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- b) HelloThe condition (!y && !z) => (!true && !true) => false && false => false. Then, (!x || false) => (!true || false) => false || false => false. Therefore overall condition becomes false, and else block will be executed.
Q10) What is the output of the below program?
public class Main
{
public static void main(String[] args) {
int a = 25;
if(a < 5)
System.out.println("Hi");
if(a < 20)
System.out.println("Hello");
else
System.out.println("Know Program");
}
}
a) Hi
b) Hello
c) Know Program
d) Compiled Successfully, No Output.
e) Compile-time error
View Answer
Answer:- c) Know ProgramIn this program, 25 < 5 => condition becomes false so first if-statement will not be executed. Again 25 < 20 => condition becomes false so second if-statement also will not be executed. But the else block of second if-statement will be executed.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!