Java MCQs on Object class of Java Programming Language.
1. Which of these class is superclass of every class in Java? Answer: b 2. Which of these method of Object class can clone an object? Answer: c 3. Which of these method of Object class is used to obtain class of an object at run time? Answer: c 4. Which of these keywords can be used to prevent inheritance of a class? Answer: d 5. Which of these keywords cannot be used for a class which has been declared final? Answer: a 6. Which of these class relies upon its subclasses for complete implementation of its methods? Answer: b 7. What will be the output of the following Java program? a) 0 8. What will be the output of the following Java program? a) false 9. What will be the output of the following Java code? a) Object 10. What will be the output of the following Java code? a) true
a) String class
b) Object class
c) Abstract class
d) ArrayList class
Clarification: Object class is superclass of every class in Java.
a) Objectcopy()
b) copy()
c) Object clone()
d) clone()
Clarification: None.
a) get()
b) void getclass()
c) Class getclass()
d) None of the mentioned
Clarification: None.
a) super
b) constant
c) class
d) final
Clarification: Declaring a class final implicitly declared all of its methods final, and makes the class inheritable.
a) abstract
b) extends
c) abstract and extends
d) none of the mentioned
Clarification: 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.
a) Object class
b) abstract class
c) ArrayList class
d) None of the mentioned
Clarification: None.
abstract class A
{
int i;
abstract void display();
}
class B extends A
{
int j;
void display()
{
System.out.println(j);
}
}
class Abstract_demo
{
public static void main(String args[])
{
B obj = new B();
obj.j=2;
obj.display();
}
}
b) 2
c) Runtime Error
d) Compilation Error
Clarification: 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
class A
{
int i;
int j;
A()
{
i = 1;
j = 2;
}
}
class Output
{
public static void main(String args[])
{
A obj1 = new A();
A obj2 = new A();
System.out.print(obj1.equals(obj2));
}
}
b) true
c) 1
d) Compilation Error
Clarification: 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
class Output
{
public static void main(String args[])
{
Object obj = new Object();
System.out.print(obj.getclass());
}
}
b) class Object
c) class java.lang.Object
d) Compilation Error
Clarification: None.
output:
$ javac Output.java
$ java Output
class java.lang.Object
class A
{
int i;
int j;
A()
{
i = 1;
j = 2;
}
}
class Output
{
public static void main(String args[])
{
A obj1 = new A();
System.out.print(obj1.toString());
}
}
b) false
c) String associated with obj1
d) Compilation Error
Clarification: 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: