Learn JAVA MCQs

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

Which of the following is a valid syntax for a switch statement in Java?

1) switch (expression) { case value: break; }

2) switch (expression) { case value => break; }

3) switch (expression) : { case value: break; }

4) None of the above

Answer : switch (expression) { case value: break; }

Which of the following is a valid syntax for an if-else statement in Java?

1) if (condition) { } else { }

2) if condition { } else { }

3) if (condition): { } else: { }

4) None of the above

Answer : if (condition) { } else { }

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

1) 20

2) 18

3) 22

4) None of the above

Answer : 18

Which of the following statements is true about the static keyword in Java?

1) Static methods belong to the class, not to objects.

2) Static variables are shared among all instances of a class.

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) 21

2) 11

3) 7

4) None of the above

Answer : 11

Which of the following methods is used to stop a thread in Java?

1) stop()

2) terminate()

3) destroy()

4) None of the above

Answer : stop()

Which of the following is used to store multiple values of the same type in Java?

1) Array

2) List

3) Set

4) All of the above

Answer : Array

Which of the following is not a valid way to instantiate a string in Java?

1) String str = new String("Hello");

2) String str = "Hello";

3) String str = String("Hello");

4) None of the above

Answer : String str = String("Hello");

Which of the following is used to declare an abstract class in Java?

1) abstract class

2) interface class

3) final class

4) None of the above

Answer : abstract class

What is the return type of the compareTo() method in Java?

1) int

2) boolean

3) String

4) None of the above

Answer : int