Java is one of the most popular programming languages in the world. It is widely used for developing various types of applications, ranging from web and mobile applications to enterprise software. As a result, there is a high demand for Java developers in the job market. If you are preparing for a Java interview or want to test your knowledge of the language, it is essential to practice with multiple-choice questions (MCQs). In this article, we have compiled a list of Java MCQ questions that will help you assess your understanding of the language and its concepts.
See these Java MCQ questions
- What is the output of the following code snippet?
“`
public class Test {
public static void main(String[] args) {
int a = 5;
int b = 2;
int result = a / b;
System.out.println(result);
}
}
“`
a) 2
b) 2.5
c) 2.0
d) 2.5 (float) - Which of the following is not a primitive data type in Java?
a) int
b) float
c) string
d) char - What is the correct way to declare an array in Java?
a) int[] arr = new int[5];
b) int arr[] = new int[5];
c) int arr[5];
d) int arr = new int[5]; - What is the output of the following code?
“`
public class Test {
public static void main(String[] args) {
String str1 = “Hello”;
String str2 = “World”;
String result = str1 + str2;
System.out.println(result);
}
}
“`
a) HelloWorld
b) Hello World
c) Hello+World
d) Compilation error - Which keyword is used to explicitly throw an exception in Java?
a) catch
b) finally
c) throw
d) throws - What is the output of the following code?
“`
public class Test {
public static void main(String[] args) {
int x = 10;
int y = 5;
if (x > y)
System.out.println(“x is greater than y”);
else
System.out.println(“y is greater than x”);
}
}
“`
a) x is greater than y
b) y is greater than x
c) x=y
d) Compilation error - Which method is used to read input from the user in Java?
a) System.out.print()
b) System.in.read()
c) Scanner.nextLine()
d) BufferedReader.readLine() - What is the output of the following code?
“`
public class Test {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
}
}
“`
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3 4 5
d) 1 2 3 4 - What is the default value of an int variable in Java?
a) 0
b) 1
c) null
d) undefined - Which statement is used to exit a loop in Java?
a) break
b) return
c) exit
d) continue - What is the output of the following code?
“`
public class Test {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i : arr) {
if (i % 2 == 0)
continue;
System.out.println(i);
}
}
}
“`
a) 1 3 5
b) 2 4
c) 1 2 3 4 5
d) Compilation error - What is the purpose of the `finally` block in a try-catch-finally statement?
a) To handle exceptions
b) To execute code regardless of whether an exception occurs or not
c) To re-throw an exception
d) To catch multiple exceptions - Which method is used to compare two strings for equality in Java?
a) equals()
b) compareTo()
c) == operator
d) both a and b
These are just a few examples of Java MCQ questions. By practicing these questions, you can improve your understanding of Java and its concepts. Remember to not only focus on the correct answers but also understand the explanations behind them. This will help you in strengthening your knowledge and becoming a better Java programmer. Good luck with your Java MCQ practice!







