Java Questions & Answers – The Object Class

This section of our 1000+ Java MCQs focuses on Object class of Java Programming Language.

1. Which of these class is superclass of every class in Java?
a) String class
b) Object class
c) Abstract class
d) ArrayList class
View Answer

Answer: b
Explanation: Object class is superclass of every class in Java.

2. Which of these method of Object class can clone an object?
a) Objectcopy()
b) copy()
c) Object clone()
d) clone()
View Answer

Answer: c
Explanation: None.

3. Which of these method of Object class is used to obtain class of an object at run time?
a) get()
b) void getclass()
c) Class getclass()
d) None of the mentioned
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

4. Which of these keywords can be used to prevent inheritance of a class?
a) super
b) constant
c) class
d) final
View Answer

Answer: d
Explanation: Declaring a class final implicitly declared all of its methods final, and makes the class inheritable.

5. Which of these keywords cannot be used for a class which has been declared final?
a) abstract
b) extends
c) abstract and extends
d) none of the mentioned
View Answer

Answer: a
Explanation: A abstract class is incomplete by itself and relies upon its subclasses to provide a complete implementation. If we declare a class final then no class can inherit that class, an abstract class needs its subclasses hence both final and abstract cannot be used for a same class.

6. Which of these class relies upon its subclasses for complete implementation of its methods?
a) Object class
b) abstract class
c) ArrayList class
d) None of the mentioned
View Answer

Answer: b
Explanation: None.

7. What will be the output of the following Java program?

advertisement
  1.     abstract class A 
  2.     {
  3.         int i;
  4.         abstract void display();
  5.     }    
  6.     class B extends A 
  7.     {
  8.         int j;
  9.         void display() 
  10.         {
  11.             System.out.println(j);
  12.         }
  13.     }    
  14.     class Abstract_demo 
  15.     {
  16.         public static void main(String args[])
  17.         {
  18.             B obj = new B();
  19.             obj.j=2;
  20.             obj.display();    
  21.         }
  22.     }

a) 0
b) 2
c) Runtime Error
d) Compilation Error
View Answer

Answer: b
Explanation: class A is an abstract class, it contains a abstract function display(), the full implementation of display() method is given in its subclass B, Both the display functions are the same. Prototype of display() is defined in class A and its implementation is given in class B.
output:

advertisement
$ javac Abstract_demo.java
$ java Abstract_demo 
2

8. What will be the output of the following Java program?

  1.    class A 
  2.    {
  3. 	int i;
  4. 	int j;
  5.         A() 
  6.         {
  7.             i = 1;
  8.             j = 2;
  9.         }
  10.    }
  11.    class Output 
  12.    {
  13.         public static void main(String args[])
  14.         {
  15.              A obj1 = new A();
  16.              A obj2 = new A();
  17. 	     System.out.print(obj1.equals(obj2));
  18.         }
  19.    }

a) false
b) true
c) 1
d) Compilation Error
View Answer

Answer: a
Explanation: obj1 and obj2 are two different objects. equals() is a method of Object class, Since Object class is superclass of every class it is available to every object.
output:

$ javac Output.java
$ java Output
false

9. What will be the output of the following Java code?

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.              Object obj = new Object();
  6. 	     System.out.print(obj.getclass());
  7.         }
  8.     }

a) Object
b) class Object
c) class java.lang.Object
d) Compilation Error
View Answer

Answer: c
Explanation: None.
output:

$ javac Output.java
$ java Output
class java.lang.Object

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

  1.    class A 
  2.    {
  3.         int i;
  4. 	int j;
  5.         A() 
  6.         {
  7.             i = 1;
  8.             j = 2;
  9.         }
  10.    }
  11.    class Output 
  12.    {
  13.         public static void main(String args[])
  14.         {
  15.              A obj1 = new A();
  16. 	     System.out.print(obj1.toString());
  17.         }
  18.    }

a) true
b) false
c) String associated with obj1
d) Compilation Error
View Answer

Answer: c
Explanation: toString() is method of class Object, since it is superclass of every class, every object has this method. toString() returns the string associated with the calling object.
output:

$ javac Output.java
$ java Output 
A@1cd2e5f

Sanfoundry Global Education & Learning Series – Java Programming Language.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.