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.

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 declares 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) Both a & b
d) None of the mentioned
View Answer

Answer: a
Explanation: A abstract class is incomplete by itself and relies upon its subclasses to provide 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 is the output of this program?

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

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:
$ javac Abstract_demo.java
$ java Abstract_demo
2

8. What is the output of this program?

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

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 is the output of this program?

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

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 is the output of this program?

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

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.

Subscribe Newsletter & Posts

Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Follow Manish & Sanfoundry