Java Questions & Answers – Java.lang – Class

This section of our 1000+ Java MCQs focuses 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
View Answer

Answer: a
Explanation: None.

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

Answer: a
Explanation: None.

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

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

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

Answer: d
Explanation: None.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  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
View Answer

Answer: a
Explanation: 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:

advertisement
$ javac Output.java
$ java Output
X

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

advertisement
  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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: None.
Output:

$ javac Output.java
$ java Output
false

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.