Saturday, August 3, 2024

Object Class MCQs

 Object Class MCQs

Which class is the root class of all classes in Java?


A. Base

B. Super

C. Object

D. Root

Which of the following methods is defined in the Object class?


A. toString()

B. print()

C. getName()

D. printString()

What does the equals() method in the Object class do?


A. Compares the memory addresses of two objects

B. Compares the contents of two objects for equality

C. Compares the types of two objects

D. Retrieves the string representation of an object

What is the default implementation of the hashCode() method in the Object class?


A. Returns a unique integer for each object

B. Returns a hash code based on the object's memory address

C. Returns the length of the object

D. Returns a constant value

Which method of the Object class is used to obtain a string representation of an object?


A. toString()

B. getString()

C. getRepresentation()

D. printString()

What is the purpose of the clone() method in the Object class?


A. To create a new instance of the object with the same state

B. To create a copy of the object's class definition

C. To return a new class with modified attributes

D. To generate a unique identifier for the object

Which of the following is true about the finalize() method in the Object class?


A. It is called by the garbage collector before an object is destroyed

B. It is used to finalize the state of an object before serialization

C. It is invoked when an object is created

D. It is used to check if an object is ready for garbage collection

Which method is called when an object is printed using System.out.println()?


A. toString()

B. print()

C. getDescription()

D. getString()

What will be the output of the following code?


java

Copy code

class Test {

    @Override

    public String toString() {

        return "Test Object";

    }

}


public class Main {

    public static void main(String[] args) {

        Test t = new Test();

        System.out.println(t);

    }

}

A. Test Object

B. Test@<hashcode>

C. Test

D. Compilation error

Which of the following methods can be overridden in a subclass?


A. hashCode()

B. toString()

C. clone()

D. All of the above

What does the getClass() method in the Object class return?


A. The name of the class

B. The Class object representing the runtime class of the object

C. The superclass of the object

D. The class loader for the class

What will be the output of the following code?


java

Copy code

class Test {

    @Override

    public boolean equals(Object obj) {

        return this == obj;

    }

}


public class Main {

    public static void main(String[] args) {

        Test t1 = new Test();

        Test t2 = new Test();

        System.out.println(t1.equals(t2));

    }

}

A. true

B. false

C. Compilation error

D. Runtime exception

Which method of the Object class must be implemented when a class implements the Cloneable interface?


A. clone()

B. toString()

C. hashCode()

D. equals()

Which method is used to get a hash code value for an object?


A. getHashCode()

B. getCode()

C. hashCode()

D. getValue()

What will be the result of the following code?


java

Copy code

class Test {

    @Override

    public String toString() {

        return "Test Object";

    }

    

    @Override

    public int hashCode() {

        return 42;

    }

}


public class Main {

    public static void main(String[] args) {

        Test t = new Test();

        System.out.println(t.hashCode());

    }

}

A. 42

B. Test Object

C. A unique hash code value

D. Compilation error

What is the result of calling obj.getClass().getName() where obj is an instance of a class?


A. The name of the class as a String

B. The class loader for the class

C. The Class object for the class

D. The superclass of the class

Which method in the Object class is used to check whether two objects are the same?


A. compareTo()

B. equals()

C. isEqual()

D. compare()

What will be the result of the following code?


java

Copy code

class Test {

    @Override

    public void finalize() {

        System.out.println("Finalizing");

    }

}


public class Main {

    public static void main(String[] args) {

        Test t = new Test();

        t = null;

        System.gc();

    }

}

A. Finalizing (if the garbage collector runs)

B. Compilation error

C. No output

D. Runtime exception

Which of the following methods is not present in the Object class?


A. wait()

B. notify()

C. notifyAll()

D. sleep()

What will be the output of the following code?


java

Copy code

class Test {

    public int x;

    

    public Test(int x) {

        this.x = x;

    }

    

    @Override

    public String toString() {

        return "x = " + x;

    }

}


public class Main {

    public static void main(String[] args) {

        Test t = new Test(10);

        System.out.println(t);

    }

}

A. x = 10

B. Test@<hashcode>

C. Test Object

D. Compilation error

Answers:

C. Object

A. toString()

B. Compares the contents of two objects for equality

B. Returns a hash code based on the object's memory address

A. toString()

A. To create a new instance of the object with the same state

A. It is called by the garbage collector before an object is destroyed

A. toString()

A. Test Object

D. All of the above

B. The Class object representing the runtime class of the object

B. false

A. clone()

C. hashCode()

A. 42

A. The name of the class as a String

B. equals()

A. Finalizing (if the garbage collector runs)

D. sleep()

A. x = 10


No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...