Java MCQs Class of java.lang library of Java Programming Language.
1. Which of these classes encapsulate runtime state of an object? Answer: a 2. Which of these methods returns the class of an object? Answer: a 3. Which of these methods return a class object given its name? Answer: d 4. Which of these class defines how the classes are loaded? 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) X 7. What will be the output of the following Java program? a) 0
a) Class
b) System
c) Runtime
d) Cache
Clarification: None.
a) getClass()
b) Class()
c) WhoseClass()
d) WhoseObject()
Clarification: None.
a) getClass()
b) findClass()
c) getSystemClass()
d) findSystemClass()
Clarification: findSystemClass() returns a class object given its name.
a) Class
b) System
c) Runtime
d) ClassLoader
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 = a.getClass();
System.out.print(obj.getName());
}
}
b) Y
c) a
d) b
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
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(obj.isLocalClass());
}
}
b) 1
c) true
d) false
Clarification: None.
Output:
$ javac Output.java
$ java Output
false