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) An IF-ELSE statement is also called _____.
a) Branching statement
b) Control statement
c) Block statements
d) All
View Answer
Answer:- d) AllQ2) Which of these are selection statements in Java?
a) if()
b) for()
c) continue
d) break
View Answer
Answer:- a) if()Here if is a selection statement. Ans for() is a looping statement. Continue and break are jump statements.
Q3) What is the maximum number of ELSE-IF statements that can be present in between starting IF and ending ELSE statements?
a) 32
b) 64
c) 128
d) None
View Answer
Answer:- d) NoneWe can write any number of ELSE-IF statements in a Java program. There is no restriction.
Q4) Which of these selection statements test only for equality?
a) if
b) switch
c) if & switch
d) none of the mentioned
View Answer
Answer:- b) switchQ5) An ELSE or ELSE-IF statement in Java can not exist alone without an IF statement.
a) True
b) False
View Answer
Answer:- a) TrueWe canβt write ELSE or ELSE-IF statements in Java without an IF statement.
Q6) 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.print("Hi ");
if(a < 20)
System.out.print("Hello ");
else
System.out.print("Know Program ");
}
}
a) Hi
b) Hello
c) Know Program
d) Hi Know Program
e) Hello Know Program
View Answer
Answer:- d) Hi Know ProgramHere 25>5 => true; therefore the first if-statement will be executed. In the second if-statement 25 < 20 => false; therefore else block will be executed.
Q7) What is the output of the given below program?
public class Main
{
public static void main(String[] args) {
float a = 7.3f;
if(a == 7.3)
System.out.print("Hi");
else
System.out.print("Know Program");
}
}
a) Hi
b) Know Program
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- b) Know ProgramThe 7.3 is a double type value but the βaβ variable is float type. Both are not the same, hence the condition becomes false, and the ELSE block will be executed.
Also see:- Difference between float and double in Java
Q8) Find the output of the given Java program?
public class Main
{
public static void main(String[] args) {
double a = 7.3;
if(a = 7.3)
System.out.print("Hi");
else
System.out.print("Know Program");
}
}
a) Hi
b) Know Program
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- d) Compile-time errorIn conditions, we must use boolean type value. Whenever we use comparisons then it generates a boolean value, but here we are assigning value to the variable. Hence it gives compile-time error.
Q9) What is the output of the given below program?
public class Main {
public static void main(String[] args) {
boolean x = true;
boolean y = false;
boolean z = true;
if(x && y || y && z)
System.out.print("Hi");
else
System.out.print("Know Program");
}
}
a) Hi
b) Know Program
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- b) Know ProgramQ10) Find the output of the given Java program?
public class Main {
public static void main(String[] args) {
boolean x = true;
boolean y = false;
boolean z = true;
if(x & y | y & z | z)
System.out.print("Hi");
else
System.out.print("Know Program");
}
}
a) Hi
b) Know Program
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- a) HiIf 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!