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
Explanation: None.
2. Which of these methods returns the class of an object?
a) getClass()
b) Class()
c) WhoseClass()
d) WhoseObject()
View Answer
Explanation: None.
3. Which of these methods return a class object given its name?
a) getClass()
b) findClass()
c) getSystemClass()
d) findSystemClass()
View Answer
Explanation: 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
View Answer
Explanation: None.
5. What will be the output of the following Java program?
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());
}
}
a) X
b) Y
c) a
d) b
View Answer
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:
$ javac Output.java
$ java Output
X
6. What will be the output of the following Java program?
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());
}
}
a) X
b) Y
c) class X
d) class Y
View Answer
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?
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());
}
}
a) 0
b) 1
c) true
d) false
View Answer
Explanation: None.
Output:
$ javac Output.java $ java Output false
Sanfoundry Global Education & Learning Series Java Programming Language.
- Apply for Computer Science Internship
- Practice Information Technology MCQs
- Apply for Java Internship
- Practice Programming MCQs
- Check Java Books