Create New Post

Java MCQs - 13

  1. Consider the following code:
class A {
    int x = 5;
    void display() {
        System.out.println("A: " + x);
    }
}

class B extends A {
    int x = 10;
    void display() {
        System.out.println("B: " + x);
    }
}

public class Main {
    public static void main(String[] args) {
        A obj = new B();
        obj.display();
    }
}
 

What is the output of the above code?

A) A: 5
B) B: 10
C) A: 10
D) Compilation Error

Answer: B) B: 10

Explanation: Despite the fact that the reference obj is of type A, polymorphism ensures that the method display() of class B is called since obj actually refers to an object of type B, where x is 10.

  1. What is the difference between the == operator and the equals() method in Java?

A) They are the same and can be used interchangeably.
B) == is used to compare reference equality, while equals() is used to compare object content equality.
C) == is used to compare object content equality, while equals() is used to compare reference equality.
D) There is no difference; both are used to compare reference equality.

Answer: B) == is used to compare reference equality, while equals() is used to compare object content equality.

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

A) It is used to define a class that cannot be subclassed.
B) It is used to declare a method that cannot be overridden.
C) It is used to indicate that a field should not be serialized.
D) It is used to synchronize access to a shared resource.

Answer: C) It is used to indicate that a field should not be serialized.

  1. Which of the following statements is true about the static keyword in Java?

A) It is used to declare an instance variable.
B) It is used to define a method that belongs to the class rather than to any particular object of the class.
C) It is used to prevent a method or variable from being accessed outside its own package.
D) It is used to specify that a class cannot be instantiated.

Answer: B) It is used to define a method that belongs to the class rather than to any particular object of the class.

  1. Consider the following code:
 public class Main {
    public static void main(String[] args) {
        try {
            System.out.println("Hello");
            return;
        } finally {
            System.out.println("Finally");
        }
    }
}

What is the output of the above code?

A) Hello
B) Finally
C) Hello Finally
D) Finally Hello

Answer: C) Hello Finally

Explanation: The finally block always executes, even if there is a return statement in the try block.

  1. Which of the following statements about the HashMap class in Java is true?

A) It allows duplicate keys.
B) It maintains the insertion order of its elements.
C) It is synchronized.
D) It allows null keys and values.

Answer: D) It allows null keys and values.

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

A) It is used to define a class that cannot be subclassed.
B) It is used to declare a method that cannot be overridden.
C) It is used to prevent a method or variable from being accessed outside its own package.
D) It is used to ensure visibility of changes to a shared variable across threads.

Answer: D) It is used to ensure visibility of changes to a shared variable across threads.

  1. Consider the following code:
 public class Main {
    public static void main(String[] args) {
        String str = "Hello";
        str.concat(" World");
        System.out.println(str);
    }
}

What is the output of the above code?

A) Hello
B) World
C) Hello World
D) Compilation Error

Answer: A) Hello

Explanation: Strings in Java are immutable. The concat() method returns a new string but does not modify the original string.

  1. Which of the following statements about Java threads is true?

A) Threads are always executed sequentially.
B) Java does not support multithreading.
C) Threads share the same memory space.
D) Threads cannot communicate with each other.

Answer: C) Threads share the same memory space.

  1. What is the purpose of the super keyword in Java?

A) It is used to access superclass methods and constructors.
B) It is used to prevent a method or variable from being accessed outside its own package.
C) It is used to define a class that cannot be subclassed.
D) It is used to specify that a class cannot be instantiated.

Answer: A) It is used to access superclass methods and constructors.

Comments

Leave a Reply

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

33774