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) Find the output of the given Java program?
public class Main {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
a) Hello World
b) HelloWorld
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- a) Hello WorldQ2) Find the output of the given Java program?
public class Main {
public static void main(String args[]) {
System.out.println("Hello World ");
System.out.println("Hello Know Program");
}
}
a) Hello World
b) Know Program
c) Hello World Hello Know Program
d)
Hello World
Hello Know Program
View Answer
Ans:- d)Hello World
Hello Know Program
The println() method places a new line. Therefore the second String “Hello Know Program” will be printed in a new line.
Q3) What is the output of the given below program?
public class Main {
public static void main(String args[]) {
System.out.print("Hello World ");
System.out.print("Hello Know Program");
}
}
a) Hello World
b) Know Program
c) Hello World Hello Know Program
d)
Hello World
Hello Know Program
View Answer
Answer:- c) Hello World Hello Know ProgramThe print() method doesn’t add any new line, therefore the second String “Hello Know Program” will be printed in the same line.
Q4) What is the output of the given below program?
public class Main {
public static void main(String args[]) {
System.out.print("Hello World ");
System.out.println("Hello Know Program");
}
}
a) Hello World
b) Know Program
c) Hello World Hello Know Program
d)
Hello World
Hello Know Program
View Answer
Answer:- c) Hello World Hello Know ProgramQ5) What is the output of the given below program?
public class Main {
public static void main(String args[]) {
System.out.println("Hello "" World");
}
}
a) Hello World
b) HelloWorld
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- d) Compile-time errorTo concatenate or add two strings we should use the + operator. Here we will get the following compile time error,
error: ')' expected System.out.println("Hello "" World"); ^ 1 error
Q6) Find the output of the given Java program?
public class Main {
public static void main(String args[]) {
System.out.println("Hello "+" World");
}
}
a) Hello World
b) HelloWorld
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- a) Hello WorldIn this program, to concatenate the two strings + operator is used. Hence “Hello “+” World” becomes “Hello World”.
Q7) Find the output of the given Java program?
public class Main {
public static void main(String args[]) {
System.out.println("Hello "*" World");
}
}
a) Hello World
b) HelloWorld
c) Compiled Successfully, No Output.
d) Compile-time error
View Answer
Answer:- d) Compile-time errorWe can’t use * to concatenate the String.
Q8) What is the output of the given below program?
public class Main {
public static void main(String args[]) {
try {
System.out.print("Hello");
}
catch(ArithmeticException e) {
System.out.println("World!");
}
}
}
a) Hello World!
b) Hello
c) World!
d) Compiled Successfully, No Output.
e) Compile-time error
View Answer
Answer:- b) HelloException is not raised is the try block, therefore, the try block will be executed successfully, and the catch block will not be executed. The catch block will be executed only when an exception is raised in the try block.
Q9) What is the output of the given below program?
public class Main {
public static void main(String args[]) {
try {
System.out.print("Hello" + " " + 1 / 0);
}
catch(ArithmeticException e) {
System.out.println("World!");
}
}
}
a) Hello World!
b) Hello
c) World!
d) Compiled Successfully, No Output.
e) Compile-time error
View Answer
Answer:- c) World!Whenever we divide a number with 0 then it raises ArithmeticException. In try block exception is raised, therefore execution of try block will be terminated and catch block 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!