Learn JAVA MCQs

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

Which of the following is true about constructors in Java?

1) Constructors have no return type.

2) Constructors can be overloaded.

3) Constructors have the same name as the class.

4) All of the above

Answer : All of the above

Which of the following is used to compare two strings in Java?

1) equals()

2) ==

3) compare()

4) None of the above

Answer : equals()

Which of the following is a valid declaration of an array in Java?

1) int[] arr = new int[5];

2) int arr[] = new int(5);

3) int arr() = new int[5];

4) None of the above

Answer : int[] arr = new int[5];

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

1) interface

2) implements

3) extends

4) None of the above

Answer : interface

Which of the following is true about static variables in Java?

1) They are shared among all instances of a class.

2) They are instance-specific.

3) They can only be used within a method.

4) None of the above

Answer : They are shared among all instances of a class.

What is the output of System.out.println(10 / 0);?

1) 0

2) undefined

3) ArithmeticException

4) None of the above

Answer : ArithmeticException

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

1) static

2) final

3) const

4) None of the above

Answer : const

Which of the following is true about garbage collection in Java?

1) It frees up memory.

2) It is manually invoked by the programmer.

3) It runs at regular intervals.

4) None of the above

Answer : It frees up memory.

Which of the following is used to generate a random number in Java?

1) Math.random()

2) Random()

3) rand()

4) None of the above

Answer : Math.random()

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

1) 10

2) 5

3) null

4) None of the above

Answer : 10