Learn JAVA MCQs

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

Which of the following is the correct syntax to create an instance of a class?

1) ClassName obj = new ClassName();

2) ClassName obj = ClassName();

3) ClassName obj = new ClassName;

4) None of the above

Answer : ClassName obj = new ClassName();

Which of the following can be used to handle multiple exceptions in a single catch block?

1) catch(Throwable e)

2) catch(Exception e)

3) catch(Error e)

4) None of the above

Answer : catch(Throwable e)

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

1) 25

2) 20

3) 30

4) None of the above

Answer : 25

Which of the following is true about a synchronized method in Java?

1) It can be accessed by multiple threads simultaneously.

2) It can only be accessed by one thread at a time.

3) It is faster than non-synchronized methods.

4) None of the above

Answer : It can only be accessed by one thread at a time.

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

1) Math.random()

2) Random()

3) Both 1 and 2

4) None of the above

Answer : Both 1 and 2

What is the output of System.out.println("Java".charAt(2));?

1) J

2) v

3) a

4) Error

Answer : v

What is the use of the `final` keyword in Java?

1) To define a constant

2) To prevent inheritance

3) To prevent method overriding

4) All of the above

Answer : All of the above

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

1) 1

2) 2

3) 3

4) Error

Answer : 1

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

1) int

2) char

3) float

4) String

Answer : String

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

1) true

2) false

3) Error

4) None of the above

Answer : true