Learn JAVA MCQs

Prepare JAVA MCQs (Multiple choice Questions) for exam and job interviews.

Which of the following is a non-primitive data type in Java?

1) int

2) boolean

3) String

4) char

Answer : String

What is the output of System.out.println("Hello".startsWith("He"));?

1) true

2) false

3) Error

4) None of the above

Answer : true

Which of the following is true about Java enums?

1) They can be compared using ==.

2) They can have methods.

3) Both 1 and 2

4) None of the above

Answer : Both 1 and 2

What is the output of System.out.println(5 * 2 + 3 - 1);?

1) 12

2) 10

3) 9

4) None of the above

Answer : 12

Which of the following is the correct way to instantiate a HashMap in Java?

1) Map<String, String> map = new HashMap<>();

2) HashMap<String, String> map = new HashMap<>();

3) Both 1 and 2

4) None of the above

Answer : Both 1 and 2

What is the output of System.out.println("A".toLowerCase());?

1) a

2) A

3) Error

4) None of the above

Answer : a

Which of the following is not a valid statement in Java?

1) int x = 5;

2) x = 10;

3) int x = 10.5;

4) None of the above

Answer : int x = 10.5;

Which of the following is used to get the class of an object in Java?

1) getClass()

2) classOf()

3) getType()

4) None of the above

Answer : getClass()

What is the output of System.out.println(10 >> 1);?

1) 5

2) 10

3) 20

4) None of the above

Answer : 5

Which of the following is a valid way to declare a method in an interface?

1) public void method();

2) void method();

3) protected void method();

4) None of the above

Answer : public void method();