Core Java MCQ

1. Which of the following is a marker interface?

  1. Serializable
  2. Cloneable
  3. Remote
  4. All of the above
 

Answer: d)

Explanation: Marker interfaces in Java are empty interfaces used to signal to the JVM or other code that objects of the implementing class should be treated differently. Examples include Serializable, Cloneable, and Remote.


2. What will be the output of the following code?

  1. 10 20
  2. 20 10
  3. 10 10
  4. 20 20
 

Answer: b)

Explanation: The code uses the XOR swap algorithm, which swaps the values of x and y without using a temporary variable. After execution, x becomes 20 and y becomes 10.


3. Which of the following is true about the final keyword?

  1. It can be used with classes, methods, and variables.
  2. It can be used with constructors.
  3. It cannot be used with methods.
  4. It is the same as the finally keyword.
 

Answer: a)

Explanation: The final keyword in Java can be used to declare constants (variables), prevent method overriding (methods), and prevent inheritance (classes). It cannot be used with constructors and is different from the finally keyword used in exception handling.


4. What is the purpose of the transient keyword in Java?

  1. To indicate that a variable is not part of the serialization process.
  2. To indicate that a variable is volatile.
  3. To make a variable synchronized.
  4. To make a variable final.
 

Answer: a)

Explanation: The transient keyword in Java is used to mark member variables that should not be serialized. When an object is serialized, transient variables are ignored, and their values are not saved.


5. Which of the following methods is used to make a thread wait for another thread to finish?

  1. wait()
  2. sleep()
  3. join()
  4. notify()
 

Answer: c)

Explanation: The join() method in Java allows a thread to wait until another thread finishes its execution.


6. Which of the following statements is true regarding the Java memory model?

  1. The Stack memory is used to store objects.
  2. The Heap memory is used to store primitive data types and method frames.
  3. The Stack memory is used to store method frames and local variables.
  4. The Heap memory is used to store method frames and local variables.

Answer: C)

Explanation: In Java, Stack memory stores method frames and local variables, whereas Heap memory is used to store objects and their instance variables.


7. What will be the output of the following code?

  1. 30
  2. 31
  3. 32
  4. Compilation Error

Answer: B)

Explanation: The expression evaluates as follows: x++ uses the current value of x (10) and then increments x to 11, while ++y increments y to 21 and then uses the value. Therefore, 10 + 21 = 31.


8. Which of the following allows a thread to communicate that it is ready but must wait until the processor is available?

  1. yield()
  2. sleep()
  3. wait()
  4. notify()

Answer: A)

Explanation: The yield() method causes the currently executing thread to pause and allows other threads of the same priority to execute. It signals the scheduler that the thread is ready to run but must wait for processor time.


9. In Java, which of the following types of exceptions must be caught or declared to be thrown in the method signature?

  1. RuntimeException
  2. Error
  3. CheckedException
  4. All of the above

Answer: C)

Explanation: Checked exceptions must be caught or declared in the method signature. Examples include IOException and SQLException. Runtime exceptions and errors do not need to be caught or declared.


10. What will be the output of the following code?

  1. false true
  2. true false
  3. true true
  4. false false

Answer: A)

Explanation: s1 == s2 returns false because they are different objects in memory. s1.equals(s2) returns true because the equals method compares the contents of the strings.