Java MCQs Runtime & ClassLoader classes of Java Programming Language.
1. Which of these classes encapsulate runtime environment? Answer: c 2. Which of the following exceptions is thrown by every method of Runtime class? Answer: c 3. Which of these methods returns the total number of bytes of memory available to the program? Answer: b 4. Which of these Exceptions is thrown by loadClass() method of ClassLoader class? Answer: d 5. What will be the output of the following Java program? a) X 6. What will be the output of the following Java program? a) 0 7. What will be the output of the following Java program? a) 0
a) Class
b) System
c) Runtime
d) ClassLoader
Clarification: None.
a) IOException
b) SystemException
c) SecurityException
d) RuntimeException
Clarification: Every method of Runtime class throws SecurityException.
a) getMemory()
b) TotalMemory()
c) SystemMemory()
d) getProcessMemory()
Clarification: TotalMemory() returns the total number of bytes available to the program.
a) IOException
b) SystemException
c) ClassFormatError
d) ClassNotFoundException
Clarification: None.
class X
{
int a;
double b;
}
class Y extends X
{
int c;
}
class Output
{
public static void main(String args[])
{
X a = new X();
Y b = new Y();
Class obj;
obj = b.getClass();
System.out.print(obj.getSuperclass());
}
}
b) Y
c) class X
d) class Y
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
class X
{
int a;
double b;
}
class Y extends X
{
int c;
}
class Output
{
public static void main(String args[])
{
X a = new X();
Y b = new Y();
Class obj;
obj = b.getClass();
System.out.print(b.equals(a));
}
}
b) 1
c) true
d) false
Clarification: None.
Output:
$ javac Output.java
$ java Output
false
class X
{
int a;
double b;
}
class Y extends X
{
int c;
}
class Output
{
public static void main(String args[])
{
X a = new X();
Y b = new Y();
Class obj;
obj = b.getClass();
System.out.print(obj.isInstance(a));
}
}
b) 1
c) true
d) false
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