Learn JAVA MCQs

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

Which of the following is a valid declaration of a boolean variable in Java?

1) boolean b = 1;

2) boolean b = true;

3) boolean b = "true";

4) None of the above

Answer : boolean b = true;

What is the purpose of the `this` keyword in Java?

1) To refer to the current object

2) To refer to the superclass

3) To refer to the subclass

4) None of the above

Answer : To refer to the current object

What is the output of System.out.println(5 % 2);?

1) 1

2) 2

3) 5

4) None of the above

Answer : 1

Which of the following is not a part of the Java Virtual Machine?

1) Class Loader

2) Execution Engine

3) Java Compiler

4) Garbage Collector

Answer : Java Compiler

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

1) interface

2) Interface

3) class

4) None of the above

Answer : interface

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

1) 15

2) 10

3) 5

4) None of the above

Answer : Option 1

Which of the following can be used to create a package in Java?

1) package

2) import

3) namespace

4) None of the above

Answer : package

What is the output of System.out.println("Hello".charAt(0));?

1) H

2) e

3) Hello

4) None of the above

Answer : H

Which of the following is the correct syntax to declare an array in Java?

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

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

3) Both 1 and 2

4) None of the above

Answer : Both 1 and 2

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

1) 532

2) 10

3) 8

4) None of the above

Answer : 532