Core Java MCQ1. Which of the following is a marker interface?
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?
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?
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?
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?
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?
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?
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?
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?
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?
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. Next TopicSwap two numbers in java using function |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India