Create New Post

Java MCQs - 5

  1. What is the output of the following code?
String str = "Hello, World!";
System.out.println(str.substring(7));

A) World!
B) Hello
C) World
D) Compiler error

Answer: A) World!

  1. In Java, what is the purpose of the 'synchronized' keyword?

A) It is used to make a method static.
B) It is used to indicate that a method cannot be overridden.
C) It is used to synchronize the execution of multiple threads on a shared resource.
D) It is used to declare variables that cannot be modified.

Answer: C) It is used to synchronize the execution of multiple threads on a shared resource.

  1. Which of the following is NOT a valid access modifier in Java?

A) private
B) public
C) protected
D) internal

Answer: D) internal

  1. What is the purpose of the 'volatile' keyword in Java?

A) It is used to declare constants.
B) It is used to make a variable unmodifiable.
C) It is used to indicate that a variable's value may be changed unexpectedly by other parts of the program.
D) It is used to specify that a method cannot be overridden.

Answer: C) It is used to indicate that a variable's value may be changed unexpectedly by other parts of the program.

  1. What is the output of the following code?
int x = 5;
int y = x++;
System.out.println(y);

A) 4
B) 5
C) 6
D) Compiler error

Answer: B) 5

  1. Which of the following is NOT a valid type of exception handling in Java?

A) try-catch-finally
B) throw
C) throws
D) try-finally

Answer: D) try-finally

  1. What is the correct way to declare a constant in Java?

A) constant int MAX_VALUE = 100;
B) final int MAX_VALUE = 100;
C) static final int MAX_VALUE = 100;
D) const MAX_VALUE = 100;

Answer: C) static final int MAX_VALUE = 100;

  1. What is the output of the following code?
String str1 = "hello";
String str2 = "HELLO";
System.out.println(str1.equalsIgnoreCase(str2));

A) true
B) false
C) Compiler error
D) Runtime error

Answer: A) true

  1. What is the purpose of the 'break' statement in Java?

A) It terminates the loop and transfers control to the next iteration.
B) It terminates the program.
C) It is used to exit a switch statement.
D) It skips the remaining code in a loop and continues to the next iteration.

Answer: C) It is used to exit a switch statement.

  1. Which of the following is true about the 'StringBuilder' class in Java?

A) It is immutable.
B) It is synchronized.
C) It is a subclass of the 'String' class.
D) It is mutable.

Answer: D) It is mutable.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

69535