Java Questions and Answers Part-30

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

Answer: c
Explanation: Runtime

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

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

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 = b.getClass();
System.out.print(obj.getSuperclass());
}
}
a) X
b) Y
c) class X
d) class Y

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:
class 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(b.equals(a));
}
}
a) 0
b) 1
c) true
d) false

Answer: d
Explanation:
Output:
false

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.isInstance(a));
}
}
a) 0
b) 1
c) true
d) false

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

8. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
String str = "TRUE";
boolean x = Boolean.valueOf(str);
System.out.print(x);
}
}
a) True
b) False
c) Compilation Error
d) Runtime Error

Answer: a
Explanation: valueOf() returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE. If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean), as this method is likely to yield significantly better space and time.
Output:
true

9. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
String str = "true false";
boolean x = Boolean.parseBoolean(str);
System.out.print(x);
}
}
a) True
b) False
c) System Dependent
d) Compilation Error

Answer: b
Explanation: parseBoolean() Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”.
Example: Boolean.parseBoolean(“True”) returns true.
Example: Boolean.parseBoolean(“yes”) returns false.
Output:
false

10. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
String x = Boolean.toString(false);
}
}
a) true
b) false
c) System Dependent
d) Compilation Error

Answer: b
Explanation: toString() Returns a String object representing the specified boolean. If the specified boolean is true, then the string “true” will be returned, otherwise the string “false” will be returned.
Output:
false