Learn JAVA MCQs

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

Which of the following is a correct way to convert a string to an integer in Java?

1) Integer.parseInt()

2) Integer.valueOf()

3) Both 1 and 2

4) None of the above

Answer : Both 1 and 2

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

1) A static method belongs to the class rather than the object.

2) A static method can be called using an object.

3) A static method can access instance variables.

4) None of the above

Answer : A static method belongs to the class rather than the object.

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

1) for loop

2) while loop

3) do-while loop

4) None of the above

Answer : None of the above

Which of the following is used to create a new string from a character array in Java?

1) String.copyValueOf()

2) String.valueOf()

3) new String()

4) None of the above

Answer : String.copyValueOf()

Which of the following is true about constructors in Java?

1) A constructor has the same name as the class.

2) A constructor has no return type.

3) Both 1 and 2

4) None of the above

Answer : Both 1 and 2

What is the output of System.out.println(10 + 20 + "30");?

1) 30

2) 3030

3) 2030

4) None of the above

Answer : 3030

Which of the following is a valid syntax for an if-else statement in Java?

1) if (condition) { statements } else { statements }

2) if condition { statements } else { statements }

3) if(condition) { statements } else statements;

4) None of the above

Answer : if (condition) { statements } else { statements }

Which of the following statements is true about packages in Java?

1) A package is a collection of classes.

2) A package is used to prevent name conflicts.

3) Packages are used for code reusability.

4) All of the above

Answer : All of the above

Which of the following is not a valid type of constructor in Java?

1) Default constructor

2) Parameterized constructor

3) Copy constructor

4) None of the above

Answer : Copy constructor

Which keyword is used to inherit a class in Java?

1) extends

2) implements

3) inherits

4) None of the above

Answer : extends