15. Consider the class hierarchy shown below:
FourWheeler
(implements DrivingUtilities)
/ / \ \
/ / \ \
/ / \ \
/ / \ \
/ / \ \
Car Truck Bus Crane
Consider the following code below:
1.DrivingUtilities du;
2.FourWheeler fw;
3.Truck myTruck = new Truck();
4.du = (DrivingUtilities)myTruck;
5.fw = new Crane();
6.fw = du;
Which of the statements below are true?
A.Line 4 will not compile because an interface cannot refer to an object.
B.The code will compile and run.
C.The code will not compile without an explicit cast at line 6, because going down the hierarchy without casting is not allowed.
D.The code at line 4 will compile even without the explicit cast.
E.The code will compile if we put an explicit cast at line 6 but will throw an exception at runtime.
C and D are correct. A and B are obviously wrong because there is nothing wrong in an interface referring to an object. C is correct because an explicit cast is needed to go down the hierarchy. D is correct because no explicit cast is needed at line 4, because we are going up the hierarchy. E is incorrect because if we put an explicit cast at line 6, the code will compile and run perfectly fine, no exception will be thrown because the runtime class of du (that is Truck) can be converted to type FourWheeler without any problem.
16. What results from the following code?
1.class MyClass
2.{
3.void myMethod(int i) {System.out.println("int version");}
4.void myMethod(String s) {System.out.println("String version");}
5.public static void main(String args[])
6.{
7.MyClass obj = new MyClass();
8.char ch = 'c';
9.obj.myMethod(ch);
10.}
11.}
A.Line 4 will not compile as void method can’t e overridden.
B.An exception at line 9.
C.Line 9 will not compile as there is no version of myMethod which takes a char as argument.
D.The code compiles and produces output: int version
E.The code compiles and produces output: String version
D is correct. A is incorrect as void methods can be overridden without any problem. B is incorrect as char ch declaration is valid. C is incorrect as char type in java is internally stored as integer and there is a method which takes int as an input. D is correct, on line 9 char ch is widened to an int and passed to int version of the myMethod(). E is incorrect as int version of myMethod() is called.
17. What is the result when you compile and run the following code?
public class ThrowsDemo {
static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
A.compile error
B.runtime error
C.compile successfully, nothing is printed.
D.inside throwMethod followed by caught: java.lang.IllegalAccessException: demo
A is correct. Exception :java.lang.IllegalAccessExcption must be caught or placed in the throws clause of the throwMethod(), i.e. the declaration of throwMethod() be changed to "static void throwMethod() throws IllegalAccessExcption". Thus compilation error will occur.
18. What will be printed when you execute the following code?
class X {
Y b = new Y();
X() {
System.out.print("X");
}
}
class Y {
Y() {
System.out.print("Y");
}
}
public class Z extends X {
Y y = new Y();
Z() {
System.out.print("Z");
}
public static void main(String[] args) {
new Z();
}
}
A.Z
B.YZ
C.XYZ
D.YXYZ
D is correct. A difficult but a fundamental question, please observe carefully. Before any object is constructed the object of the parent class is constructed(as there is a default call to the parent's constructor from the constructor of the child class via the super() statement). Also note that when an object is constructed the variables are initialized first and then the constructor is executed. So when new Z() is executed , the object of class X will be constructed, which means Y b = new Y() will be executed and "Y" will be printed as a result. After that constructor of X will be called which implies "X" will be printed. Now the object of Z will be constructed and thus Y y = new Y() will be executed and Y will be printed and finally the constructor Z() will be called and thus "Z" will be printed. Thus YXYZ will be printed.
19. What will happen when you attempt to compile and run the following code snippet?
Boolean b = new Boolean("TRUE"); //不区分大小写
if(b.booleanValue()){
System.out.println("Yes : " + b);
}else{
System.out.println("No : " + b);
}
A.The code will not compile.
B.It will print – Yes: true
C.It will print – Yes: TRUE
D.It will print – No: false
E.It will print – No: FALSE
B is the correct choice. The wrapper class Boolean has the following constructor -public Boolean(String s) It allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true". Otherwise, allocate a Boolean object representing the value false.E.g.
new Boolean("TRUE") produces a Boolean object that represents true.
new Boolean("anything") produces a Boolean object that represents false.
The internal toString() representation of this object produces the boolean value string in lower case, hence it prints "Yes : true" instead of "Yes : TRUE".