Java Object Class MCQ

Java Object Class MCQ | Also see:- Java Hashcode Value QuizJava Equals() Method Quiz, Java toString() Method Quiz, Clone() Method Quiz

Q1) Which of these classes is the superclass of every class in Java?

a) Integer
b) Math
c) Object
d) Exception

View Answer Answer:- c) Object

Either directly or indirectly, every class in Java is the child class of java.lang.Object class.

Q2) Which of the following statement is invalid?

class Animal{}
class Tiger extends Animal{}

a) Tiger is a subclass of Animal class.
b) Tiger is a subclass of the Object class.
c) Animal is a subclass of Object class.
d) None of these.

View Answer Answer:- d) None of these.

The Animal is a subclass of java.lang.Object class and Tiger is the subclass of Animal class. Indirectly Tiger is also the subclass of java.lang.Object class.

Q3) Which of the following option is the same as the below statement?

class A{}

a) class A extends Object {}
b) class A extends java.lang.Object {}
c) Both (a) and (b)
d) None of these.

View Answer Answer:- c) Both (a) and (b)

Q4) If we want to define a method that should receive all types of values then the parameter type must be?

a) Object
b) Number
c) String
d) Something else

View Answer Answer:- a) Object

If we want to define a method receives any type of object, then the parameter must be of java.lang.Object type.

Q5) Which of the following method of java.lang.Object class, we can’t override in the subclass?

a) hashCode()
b) getClass()
c) finalize()
d) clone()

View Answer Answer:- b) getClass()

The getClass() method is a final method and it can’t be overridden in the subclasses.

Q6) Which of the following method is internally not called by the Object.toString() method?

a) getClass()
b) hashCode()
c) getName()
d) finalize()

View Answer Answer:- d) finalize()

The toString() method of java.lang.Object is implemented as, return getClass().getName() + “@” + Integer.toHexString(hashCode());

Q7) Whenever we are overriding the equals() method then it is recommended to override which method of the java.lang.Object class?

a) toString()
b) hashCode()
c) clone()
d) getClass()

View Answer Answer:- a) hashCode()

Two equivalent objects should have the same hashcode. We should follows this relation between hashCode() and equals() method. Hence it is recommended to override the hashCode() method. Based on which parameter we override equals() method, it is highly recommended to use the same parameter while overriding hashCode() also.

Q8) Generally, which of the following method is overridden in the subclass just to change the accessibility modifier, not to change the implementation logic.

a) getClass()
b) clone()
c) equals()
d) hashCode()

View Answer Answer:- b) clone()

The clone() method of the Object class is protected and because of this, we can access it only in the subclass, not in the user class (like Test class). To overcome this problem we must override the clone() method of java.lang.Object class. Since we are overriding the clone() method just to change the accessibility modifier not to change the implementation logic so while overriding we must call Object class clone() as super.clone().

Q9) Find the output of the below Java program?

class Student {}
public class Test {
  public static void main(String[] args) {
     Student s1 = new Student();
     System.out.println(s1.getClass().getName());
  }
}

a) Test
b) Student
c) Object
d) Class

View Answer Answer:- b) Student

Q10) Whenever we call the getClass() method then which of the following statement is valid.

a) Test class is loaded, stored in JVM & java.lang.Class object reference is returned. The static variable, static block, the main method of the Test class are not executed.
b) Test class is loaded, stored in JVM & java.lang.Class object reference is returned. The static variables and static blocks are executed. But the main method is not executed.
c) Already created Class object reference with the Test class is returned.
d) None of these

View Answer Answer:- c) Already created Class object reference with Test class is returned.

The getClass() method is a non-static method and it means to call getClass() method object must be there. The object can be created after class loading, and static variable/block execution.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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