选择题
1: Consider the class hierarchy shown below:
——————————————————————–
class FourWheeler implements DrivingUtilities
class Car extends FourWheeler
class Truck extends FourWheeler
class Bus extends FourWheeler
class Crane extends FourWheeler
———————————————————————-
Consider the following code below:
ingUtilities du;
Wheeler fw;
k myTruck = new Truck();
= (DrivingUtilities)myTruck;
= new Crane();
= du;
Which of the statements below are true?
Choices:
4 will not compile because an interface cannot refer to an object.
code will compile and run.
code will not compile without an explicit cast at line 6, because going down the hierarchy without casting is not allowed.
code will compile if we put an explicit cast at line 6 but will throw an exception at runtime.
2:A class design requires that a member variable should be accessible only by same package, which modifer word should be used?
ected
ic
modifer
ate
3:
What is the result when you compile and run the following code?
public class ThrowsDemo
{
static void throwMethod()
{
tln(“Inside throwMethod.”);
throw new IllegalAccessException(“demo”);
}
public static void main(String args[])
{
try
{
throwMethod();
}
catch (IllegalAccessException e)
{
tln(“Caught ” + e);
}
}
}
Choices:
What is the result when you compile and run the following code?
public class ThrowsDemo
{
static void throwMethod()
{
tln(“Inside throwMethod.”);
throw new IllegalAccessException(“demo”);
}
public static void main(String args[])
{
try
{
throwMethod();
}
catch (IllegalAccessException e)
{
tln(“Caught ” + e);
}
}
}
Choices:
ilation error
ime error
ile successfully, nothing is printed.
de throwMethod. followed by caught:galAccessExcption: demo
4:
In the following code, which is the earliest statement, where the object originally held in e, may be garbage collected:
ic class Test {
2. public static void main (String args []) {
3. Employee e = new Employee(“Bob”, 48);
4. ulatePay();
5. tln(tDetails());
6. e = null;
7. e = new Employee(“Denise”, 36);
8. ulatePay();
9. tln(tDetails());
10. }
11.}
Only One:
In the following code, which is the earliest statement, where the object originally held in e, may be garbage collected:
ic class Test {
2. public static void main (String args []) {
3. Employee e = new Employee(“Bob”, 48);
4. ulatePay();
5. tln(tDetails());
6. e = null;
7. e = new Employee(“Denise”, 36);
8. ulatePay();
9. tln(tDetails());
10. }
11.}
Only One:
10
11
7
8
5:
What will be the result of executing the following code?
public static void main(String args[])
{
char digit = ‘a’;
for (int i = 0; i < 10; i++)
{
switch (digit)
{
case ‘x’ :
{
int j = 0;
tln(j);
}
default :
{
int j = 100;
tln(j);
}
}
}
int i = j;
tln(i);
}
Choices:
What will be the result of executing the following code?
public static void main(String args[])
{
char digit = ‘a’;
for (int i = 0; i < 10; i++)
{
switch (digit)
{
case ‘x’ :
{
int j = 0;
tln(j);
}
default :
{
int j = 100;
tln(j);
}
}
}
int i = j;
tln(i);
}
Choices:
A.100 will be printed 11 times.
code will not compile because the variable i cannot be declared twice within the main() method.
code will not compile because the variable j cannot be declared twice within the switch statement.
of these.