Learn JAVA MCQs

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

Which of the following is not a Java access modifier?

1) public

2) private

3) protected

4) package

Answer : package

Which of the following is true about a final variable in Java?

1) It cannot be changed after initialization.

2) It can be changed anytime.

3) It cannot be initialized.

4) None of the above

Answer : It cannot be changed after initialization.

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

1) 2

2) 3

3) 5

4) None of the above

Answer : 2

Which of the following is used to create an immutable class in Java?

1) Using the `final` keyword

2) Using the `static` keyword

3) Using the `private` keyword

4) None of the above

Answer : Using the `final` keyword

What is the output of System.out.println("abc".toUpperCase());?

1) ABC

2) abc

3) Error

4) None of the above

Answer : ABC

Which of the following is used to initialize instance variables in Java?

1) Constructors

2) Methods

3) Both 1 and 2

4) None of the above

Answer : Constructors

What is the output of System.out.println(15 | 3);?

1) 3

2) 7

3) 15

4) None of the above

Answer : 15

Which of the following is used to create a thread-safe class in Java?

1) Using synchronized methods

2) Using volatile keyword

3) Using atomic variables

4) All of the above

Answer : All of the above

What is the output of System.out.println("Hello World".indexOf("W"));?

1) 6

2) 7

3) 5

4) None of the above

Answer : 6

Which of the following is used to read data from a file in Java?

1) FileReader

2) BufferedReader

3) Scanner

4) All of the above

Answer : All of the above