MCQ on Java: Welcome to our blog post on “50 Java Multiple-Choice Questions to Test Your Skills”! This 50 MCQ on Java will assess your understanding of Java. It will help both beginner and experienced programmers aiming to test their knowledge. These MCQs will help you to pass BCA, BTech, or any competitive examination.
To get benefits from these 50 Java Multiple-Choice Questions, you must try the MCQ options first. And see the answer below after 50 MCQ as well as “View Answer”.
Java Multiple-Choice Questions: Java MCQ
1. What is the output of the following Java code snippet?
int x = 5; int y = x++; System.out.println(y);
a)4
b) 5
c) 6
d) Compile error
View Answer2. Which keyword is used to define a constant variable in Java?
a) final
b) const
c) static
d) constant
3. What is the default value of an uninitialized variable of type int in Java?
a) 0
b) 1
c) -1
d) null
4. What is the purpose of the `break` statement in Java?
a) To exit a loop or switch statement
b) To skip the current iteration of a loop
c) To continue to the next iteration of a loop
d) To throw an exception
5. Which of the following is not a valid Java identifier?
a) myVariable
b) 123abc
c) _underscore
d) $dollar
6. What is the output of the following Java code snippet?
String str1 = “Hello”;
String str2 = “Hello”;
System.out.println(str1 == str2);
a) true
b) false
c) Compile error
d) Runtime error
7. Which data type should be used to store a single character in Java?
a) char
b) int
c) String
d) byte
8. What is the output of the following Java code snippet?
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers.length);
a) 1
b) 2
c) 3
d) 5
9. What is the result of the expression `5 + 2 * 3` in Java?
a) 7
b) 11
c) 15
d) 21
10. Which keyword is used to create an object in Java?
a) new
b) create
c) instance
d) object
11. What is the output of the following Java code snippet?
int x = 10;
if (x > 5) {
System.out.println(“Hello”);
}
a) Hello
b) Nothing
c) Compile error
d) Runtime error
12. Which of the following is not a valid modifier in Java?
a) public
b) private
c) protected
d) global
13. What is the output of the following Java code snippet?
int x = 10;
int y = 5;
System.out.println(x > y ? “x” : “y”);
a) x
b) y
c) 10
d) 5
14. Which of the following is a valid declaration of a two-dimensional array in Java?
a) int[] arr;
b) int[][] arr;
c) int[2] arr;
d) int[2][2] arr;
15. What is the output of the following Java code snippet?
for (int i = 0; i < 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
a) 0 1 2
b) 0 1 2 3
c) 0 1 2 3 4
d) Compile error
16. Which method is used to read input from the user in Java?
a) System.input()
b) Scanner.read()
c) Console.readLine()
d) System.in.read()
17. What is the output of the following Java code snippet?
int x = 10;
System.out.println(x++);
a) 10
b) 11
c) 12
d) Compile error
18. Which of the following is not a valid Java array declaration?
a) int[] numbers;
b) int numbers[];
c) int numbers[5];
d) int[] numbers = new int[5];
19. What is the output of the following Java code snippet?
String str = “Java”;
str.concat(” Programming”);
System.out.println(str);
a) Java
b) Java Programming
c) Programming
d) Compile error
Best 55 Sports Quiz with Answers: MCQs on Sports General Knowledge, MCQ on Sports
20. Which keyword is used to inherit a class in Java?
a) super
b) this
c) extends
d) implements
21. What is the output of the following Java code snippet?
int x = 0;
do {
System.out.println(x);
x++;
} while (x < 5);
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3 4 5
d) Compile error
22. Which of the following is not a valid access modifier in Java?
a) private
b) protected
c) package-private
d) public
23. What is the output of the following Java code snippet?
int x = 10;
int y = 5;
System.out.println(x < y ? x : y);
a) x
b) y
c) 10
d) 5
24. Which of the following is a valid way to create a thread in Java?
a) Thread.run()
b) Thread.start()
c) Thread.sleep()
d) Thread.wait()
25. What is the output of the following Java code snippet?
int x = 5;
int y = 2;
System.out.println(x / y);
a) 2
b) 2.5
c) 2.0
d) Compile error
26. Which operator is used for concatenating two strings in Java?
a) +
b) –
c) *
d) /
27. What is the output of the following Java code snippet?
int x = 10;
System.out.println(++x);
a) 10
b) 11
c) 12
d) Compile error
28. Which of the following is not a valid Java loop statement?
a) for
b) while
c) until
d) do-while
29. What is the output of the following Java code snippet?
int[] numbers = new int[3];
System.out.println(numbers[1]);
a) 0
b) 1
c) 2
d) Compile error
30. Which of the following is not a valid Java primitive data type?
a) int
b) double
c) boolean
d) string
31. What is the output of the following Java code snippet?
int x = 5;
int y = 2;
System.out.println(x % y);
a) 1
b) 2
c) 2.5
d) Compile error
32. Which keyword is used to prevent a class from being inherited in Java?
a) static
b) final
c) abstract
d) private
33. What is the output of the following Java code snippet?
int x = 5;
int y = 2;
System.out.println(x > y && x < 10);
a) true
b) false
c) Compile error
d) Runtime error
34. Which of the following is used to handle exceptions in Java?
a) try-catch
b) if-else
c) switch-case
d) for-each
35. What is the output of the following Java code snippet?
String str = “Java”;
str = str.toUpperCase();
System.out.println(str);
a) java
b) JAVA
c) Compile error
d) Runtime error
Introduction To Data Communication And Networking Fundamentals 7 Layer Of OSI
36. Which of the following is not a valid way to declare a method in Java?
a) void myMethod() {}
b) int myMethod() {}
c) public void myMethod() {}
d) static void myMethod() {}
37. What is the output of the following Java code snippet?
int x = 5;
System.out.println(x–);
a) 5
b) 4
c) 6
d) Compile error
38. Which operator is used to access the instance variables and methods of a class in Java?
a) .
b) ::
c) ->
d) :
39. What is the output of the following Java code snippet?
String str = “Java”;
System.out.println(str.length());
a) 0
b) 1
c) 3
d) 4
40. Which of the following is not a valid type of loop in Java?
a) for loop
b) while loop
c) do-while loop
d) if-else loop
41. What is the output of the following Java code snippet?
int x = 5;
int y = 2;
System.out.println(x > y || x < 10);
a) true
b) false
c) Compile error
d) Runtime error
42. Which of the following is not a valid way to create an instance of a class in Java?
a) ClassName obj = new ClassName();
b) ClassName obj = new ClassName;
c) ClassName obj = new ClassName(param1, param2);
d) ClassName obj = ClassName.getInstance();
43. What is the output of the following Java code snippet?
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[3]);
a) 1
b) 2
c) 3
d) 4
44. Which keyword is used to implement method overriding in Java?
a) override
b) extends
c) implements
d) super
45. What is the output of the following Java code snippet?
int x = 5;
System.out.println(x += 2);
a) 5
b) 7
c) 10
d) Compile error
46. Which of the following is not a valid way to define a constant in Java?
a) final int CONSTANT = 10;
b) const int CONSTANT = 10;
c) static final int CONSTANT = 10;
d) public static final int CONSTANT = 10;
47. What is the output of the following Java code snippet?
int x = 10;
System.out.println(x);
a) 10
b) 11
c) 12
d) Compile error
48. Which operator is used to compare two values for equality in Java?
a) ==
b) =
c) !=
d) >
49. What is the output of the following Java code snippet?
String str = “Java”;
System.out.println(str.charAt(2));
a) J
b) a
c) v
d) Compile error
50. Which of the following is not a valid way to declare a variable in Java?
a) int x;
b) int x = 5;
c) int x, y;
d) int x = 5, y = 10;
Conclusion
In the above 50 Java Multiple-Choice Questions (MCQ), we have provided answers. Enjoy, enrich, and test your Java knowledge. The 50 MCQ answers are also here.
50 Java Multiple-Choice Questions – Answers (MCQ)
1. Answer: b) 5
2. Answer: a) final
3. Answer: a) 0
4. Answer: a) To exit a loop or switch statement
5. Answer: b) 123abc
6. Answer: a) true
7. Answer: a) char
8. Answer: d) 5
9. Answer: b) 11
10. Answer: a) new
11. Answer: a) Hello
12. Answer: d) global
13. Answer: c) 10
14. Answer: b) int[][] arr;
15. Answer: a) 0 1 2
16. Answer: c) Console.readLine()
17. Answer: a) 10
18. Answer: c) int numbers[5];
19. Answer: a) Java
20. Answer: c) extends
21. Answer: a) 0 1 2 3 4
22. Answer: d) public
23. Answer: b) y
24. Answer: b) Thread.start()
25. Answer: a) 2
26. Answer: a) +
27. Answer: b) 11
28. Answer: c) until
29. Answer: a) 0
30. Answer: d) string
31. Answer: a) 1
32. Answer: b) final
33. Answer: a) true
34. Answer: a) try-catch
35. Answer: b) JAVA
36. Answer: b) int myMethod() {}
37. Answer: b) 5
38. Answer: a) .
39. Answer: d) 4
40. Answer: d) if-else loop
41. Answer: a) true
42. Answer: b) ClassName obj = new ClassName;
43. Answer: d) 4
44. Answer: a) override
45. Answer: b) 7
46. Answer: b) const int CONSTANT = 10;
47. Answer: a) 10
48. Answer: a) ==
49. Answer: c) v
50. Answer: b) int 5 = x;