Question: Part A: class I { private String name; public void finalize() { System.out.print(name); } public I(String s) { name = s; } } class J
Part A:
class I {
private String name;
public void finalize() {
System.out.print(name);
}
public I(String s) {
name = s;
}
}
class J {
private static void m1(I[] a1) {
a1[0] = a1[1] = a1[2] = null;
}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);
System.gc();
}
}
After method m1 returns, the object created on which line is not eligible for garbage collection?
a. 1
b. 2
c. 3
d. 4
e. None of the above
f. Compile-time error
g. Run-time error
Part B:
interface I {
String s1 = "I";
}
class A implements I {
String s1 = "A";
}
class B extends A {
String s1 = "B";
}
class C extends B {
String s1 = "C";
void printIt() {
System.out.print(((A)this).s1
+ ((B)this).s1
+ ((C)this).s1
+ ((I)this).s1);
}
public static void main (String[] args) {
new C().printIt();
}
}
What is the result of attempting to compile and run the program?
a. Prints: ABCI
b. Run-time error
c. Compile-time error
d. None of the above
Part C:
Given:
10. interface Foo {}
11. class Alpha implements Foo { }
12. class Beta extends Alpha {}
13. class Delta extends Beta {
14. public static void main( String[] args) {
15. Beta x = new Beta();
16. // insert code here
17. }
18. }
Which code, inserted at line 16, will cause a java.lang.ClassCastException?
A. Alpha a = x;
B. Foo f= (Delta)x;
C. Foo f= (Alpha)x;
D. Beta b = (Beta)(Alpha)x;
Part D:
Given.
10. class Foo {
11. private int x;
12.publicFoo(intx) {this.x=x; }
13. public void setX( int x) { this.x = x; }
14. public int getX() { return x; }
15. }
16.
17. public class Gamma {
18.
19. static Foo fooBar( Foo foo) {
20. foo = new Foo( 100);
21. return foo;
22. }
23.
24. public static void main( String[] args) {
25. Foo foo = new Foo( 300);
26. System.out.print( foo.getX() + -);
27.
28. Foo fooFoo = fooBar( foo);
29. System.out.print( foo.getX() + -);
30. System.out.print( fooFoo.getX() + -);
31.
32. foo = fooBar( fooFoo);
33. System.out.print( foo.getX() + -);
34. System.out.prmt( fooFoo.getX());
35. }
36. }
What is the output of this program?
A. 300-100-100-100-100
B. 300-300-100-100-100
C. 300-300-300-100-100
D. 300-300-300-300-100
Part E:
Which implementation of the List interface provides for the fastest insertion of a new element into the middle of the list?
a. Vector
b. ArrayList
c. LinkedList
d. None of the above
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
