1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i = 0; i < 10; i++) {
14. int value = i * ((int) Math.random());
15. Integer intObj = new Integer(value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }
Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for garbage collection?
Given:
1. public class GC {
2. private Object o;
3. private void doSomethingElse(Object obj) { o = obj; }
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9. o = null;
10. }
11.}
When the doSomething method is called, after which line does the Object created in line 5 become
available for garbage collection?
public class Spock {
public static void main(String[] args) {
Long tail = 2000L;
Long distance = 1999L;
Long story = 1000L;
if ((tail > distance) ^ ((story * 2) == tail))
System.out.print(1);
if ((distance + 1 != tail) ^ ((story * 2) == distance))
System.out.print(2);
}
}
What is the result?
1. public class Pass2 {
2. public void main(String[] args) {
3. int x = 6;
4. Pass2 p = new Pass2();
5. p.doStuff(x);
6. System.out.print( main x = + x);
7. }
8.
9. void doStuff(int x) {
10. System.out.print( doStuff x = + x++);
11. }
12.}
And the command-line invocations:
javac Pass2.java
java Pass2 5
What is the result?
A class games.cards.Poker is correctly defined in the jar file Poker.jar. A user wants to execute the main method of Poker on a UNIX system using the command: java games.cards.Poker What allows the user to do this?
Given a correctly compiled class whose source code is:
1. package com.sun.sjcp;
2.
3. public class Commander {
4. public static void main(String[] args) {
5. // more code here
6. }
7. }
Assume that the class file is located in /foo/com/sun/sjcp/, the current directory is /foo/, and that the classpath contains . (current directory). Which command line correctly runs Commander?
Given:
interface DoStuff2 {
float getRange(int low, int high);
}
interface DoMore {
float getAvg(int a, int b, int c);
}
abstract class DoAbstract implements DoStuff2, DoMore {
}
class DoStuff implements DoStuff2 {
public float getRange(int x, int y) {
return 3.14f;
}
}
interface DoAll extends DoMore {
float getAvg(int a, int b, int c, int d);
}
What is the result?
public class Spock {
public static void main(String[] args) {
Long tail = 2000L;
Long distance = 1999L;
Long story = 1000L;
if ((tail > distance) ^ ((story * 2) == tail))
System.out.print(1);
if ((distance + 1 != tail) ^ ((story * 2) == distance))
System.out.print(2);
}
}
What is the result?
class Payload {
private int weight;
public Payload() {
}
public Payload(int w) {
weight = w;
}
public void setWeight(int w) {
weight = w;
}
public String toString() {
return Integer.toString(weight);
}
}
public class TestPayload {
static void changePayload(Payload p) {
/* insert code */
}
public static void main(String[] args) {
Payload p = new Payload(200);
p.setWeight(1024);
changePayload(p);
System.out.println(p is + p);
}
}
Which code fragment, inserted at the end of line 12, produces the output p is 420?