250+ TOP MCQs on Java.lang – Class and Answers

Java MCQs Class of java.lang library of Java Programming Language.

1. Which of these classes encapsulate runtime state of an object?
a) Class
b) System
c) Runtime
d) Cache

Answer: a
Clarification: None.

2. Which of these methods returns the class of an object?
a) getClass()
b) Class()
c) WhoseClass()
d) WhoseObject()

Answer: a
Clarification: None.

3. Which of these methods return a class object given its name?
a) getClass()
b) findClass()
c) getSystemClass()
d) findSystemClass()

Answer: d
Clarification: findSystemClass() returns a class object given its name.

4. Which of these class defines how the classes are loaded?
a) Class
b) System
c) Runtime
d) ClassLoader

Answer: d
Clarification: None.

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

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. 	int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = a.getClass();
  18.             System.out.print(obj.getName());
  19.         }
  20.     }

a) X
b) Y
c) a
d) b

Answer: a
Clarification: getClass() is used to obtain the class of an object, here ‘a’ is an object of class ‘X’. hence a.getClass() returns ‘X’ which is stored in class Class object obj.
Output:

$ javac Output.java
$ java Output
X

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

  1.     class X
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. 	int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(obj.getSuperclass());
  19.         }
  20.     }

a) X
b) Y
c) class X
d) class Y

Answer: c
Clarification: getSuperClass() returns the super class of an object. b is an object of class Y which extends class X, Hence Super class of b is X. therefore class X is printed.
Output:

$ javac Output.java
$ java Output
class X

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

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. 	int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(obj.isLocalClass());
  19.         }
  20.     }

a) 0
b) 1
c) true
d) false

Answer: d
Clarification: None.
Output:

$ javac Output.java
$ java Output
false

Leave a Reply

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