250+ TOP MCQs on Java.lang – Runtime & ClassLoader Classes and Answers

Java MCQs Runtime & ClassLoader classes of Java Programming Language.

1. Which of these classes encapsulate runtime environment?
a) Class
b) System
c) Runtime
d) ClassLoader

Answer: c
Clarification: None.

2. Which of the following exceptions is thrown by every method of Runtime class?
a) IOException
b) SystemException
c) SecurityException
d) RuntimeException

Answer: c
Clarification: Every method of Runtime class throws SecurityException.

3. Which of these methods returns the total number of bytes of memory available to the program?
a) getMemory()
b) TotalMemory()
c) SystemMemory()
d) getProcessMemory()

Answer: b
Clarification: TotalMemory() returns the total number of bytes available to the program.

4. Which of these Exceptions is thrown by loadClass() method of ClassLoader class?
a) IOException
b) SystemException
c) ClassFormatError
d) ClassNotFoundException

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 = 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

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(b.equals(a));
  19.         }
  20.     }

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

Answer: d
Clarification: None.
Output:

$ javac Output.java
$ java Output
false

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.isInstance(a));
  19.         }
  20.     }

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

Answer: d
Clarification: Although class Y extends class X but still a is not considered related to Y. hence isInstance() returns false.
Output:

$ javac Output.java
$ java Output
false

Leave a Reply

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