Learn JAVA MCQs

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

Which of the following is not a valid method in the String class?

1) charAt()

2) substring()

3) toLower()

4) None of the above

Answer : toLower()

What is the default value of a reference variable in Java?

1) 0

2) null

3) undefined

4) None of the above

Answer : null

Which of the following is true about the default constructor in Java?

1) It is provided automatically if no constructor is defined.

2) It must be explicitly defined by the programmer.

3) It can have parameters.

4) None of the above

Answer : It is provided automatically if no constructor is defined.

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

1) 9

2) 10

3) 10.5

4) None of the above

Answer : 10

Which of the following is true about the Object class in Java?

1) It is the parent class of all classes.

2) It defines the equals() method.

3) It defines the hashCode() method.

4) All of the above

Answer : All of the above

Which of the following is not a valid return type for a method in Java?

1) void

2) int

3) String

4) class

Answer : class

What is the output of System.out.println(10 % 3);?

1) 1

2) 3

3) 0

4) None of the above

Answer : 1

Which of the following is true about encapsulation in Java?

1) It is a way to restrict access to certain components.

2) It can be achieved using access modifiers.

3) Both 1 and 2

4) None of the above

Answer : Both 1 and 2

Which of the following is used to define constants in Java?

1) const

2) final

3) static

4) None of the above

Answer : final

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

1) for

2) while

3) do-while

4) foreach

Answer : foreach