Академический Документы
Профессиональный Документы
Культура Документы
Sample questions
Given:
String s = "";
A. 5
if(011 == 9) s += 4;
B. 45
if(0x11 == 17) s += 5;
C. 46
Integer I = 12345;
D. 56
if(I.intValue() ==
Integer.valueOf("12345"))
s += 6;
E. 456
System.out.println(s);
G. An exception is thrown
at runtime
F. Compilation fails.
Given:
String s = "";
A. 5
if(011 == 9) s += 4;
B. 45
if(0x11 == 17) s += 5;
C. 46
Integer I = 12345;
D. 56
if(I.intValue() ==
Integer.valueOf("12345"))
s += 6;
E. 456
System.out.println(s);
G. An exception is thrown
at runtime
F. Compilation fails.
Answer
8. }
Given:
Answer
Given:
2. abstract class Tool {
3. int SKU;
4. abstract void getSKU();
5. }
6. public class Hammer {
8. }
Which line(s), inserted independently at line 7, will compile? (Choose all that apply.)
A. void getSKU() { ; }
B. private void getSKU() { ; }
C. protected void getSKU() { ; }
D. public void getSKU() { ; }
Q
2. public class Wacky {
3. public static void main(String[]
args) {
4. int x = 5;
5. int y = (x < 6) ? 7 : 8;
6. System.out.print(y + " ");
7. boolean b = (x < 6) ? false : true;
8. System.out.println(b + " ");
9. assert(x < 6) : "bob";
10. assert( ((x < 6) ? false : true) ) :
"fred";
11. } }
Given:
Answer
2. public class Wacky {
3. public static void main(String[]
args) {
4. int x = 5;
5. int y = (x < 6) ? 7 : 8;
6. System.out.print(y + " ");
7. boolean b = (x < 6) ? false : true;
8. System.out.println(b + " ");
9. assert(x < 6) : "bob";
10. assert( ((x < 6) ? false : true) ) :
"fred";
11. } }
Given:
Q
Given:
A. 12
4. String a = "12b";
B. 12b
C. Compilation fails.
6. System.out.println(new
Chopper().chop(args[0]));
D. A NullPointerException is thrown.
8. int chop(String a) {
9. if(a == null) throw new
IllegalArgumentException();
10. return Integer.parseInt(a);
11. } }
And, if the code compiles, the invocation:
java Chopper
E. A NumberFormatException is thrown.
F. An IllegalArgumentException is thrown.
G. An ArrayIndexOutOfBoundsException is
thrown.
7. }
Answer
Given:
A. 12
4. String a = "12b";
B. 12b
C. Compilation fails.
6. System.out.println(new
Chopper().chop(args[0]));
D. A NullPointerException is thrown.
8. int chop(String a) {
9. if(a == null) throw new
IllegalArgumentException();
10. return Integer.parseInt(a);
11. } }
And, if the code compiles, the invocation:
java Chopper
E. A NumberFormatException is thrown.
F. An IllegalArgumentException is thrown.
G. An ArrayIndexOutOfBoundsException is
thrown.
7. }
4. Console c = System.console();
5. char[] pw;
6. if(c == null) return;
7. pw = c.readPassword("%s",
"pw: ");
8.
System.out.println(c.readLine("
%s", "input: "));
10
4. Console c = System.console();
5. char[] pw;
6. if(c == null) return;
7. pw = c.readPassword("%s",
"pw: ");
8.
System.out.println(c.readLine("
%s", "input: "));
Answer
11
Q
2. public class Kant extends Philosopher {
3. // insert code here
5. public static void main(String[] args) {
6. new Kant("Homer");
7. new Kant();
8. }
9. }
10. class Philosopher {
Kant(String s) { this(); }
12. }
E. Kant() { super("Bart"); }
Kant(String s) { this("Homer"); }
Given:
12
Answer
2. public class Kant extends Philosopher {
3. // insert code here
5. public static void main(String[] args) {
6. new Kant("Homer");
7. new Kant();
8. }
9. }
10. class Philosopher {
Kant(String s) { this(); }
12. }
E. Kant() { super("Bart"); }
Kant(String s) { this("Homer"); }
Given:
13
Q
Given:
A. 2 4
B. 0 2 4
6. for(int x = 0; x < 5;
bump(x))
7. System.out.print(x + "
");
8. } }
D. 0 2 4 6
E. Compilation fails.
F. Some other result
occurs.
C. 2 4 6
14
Answer
Given:
A. 2 4
B. 0 2 4
6. for(int x = 0; x < 5;
bump(x))
7. System.out.print(x + "
");
8. } }
D. 0 2 4 6
E. Compilation fails.
F. Some other result
occurs.
C. 2 4 6
15
Q
Given that Calendar.MONTH starts
with January == 0, and given:
3. import java.util.*;
A. March 4, 1999
B. April 4, 1999
C. March 4, 2000
7. c.set(1999,11,25);
8. c.roll(Calendar.MONTH, 3);
9. c.add(Calendar.DATE, 10);
10. System.out.println(c.getTime());
11. } }
D. April 4, 2000
E. Compilation fails.
F. An exception is thrown at runtime.
6. Calendar c =
Calendar.getInstance();
16
Answer
Given that Calendar.MONTH starts
with January == 0, and given:
3. import java.util.*;
A. March 4, 1999
B. April 4, 1999
C. March 4, 2000
7. c.set(1999,11,25);
8. c.roll(Calendar.MONTH, 3);
9. c.add(Calendar.DATE, 10);
10. System.out.println(c.getTime());
11. } }
D. April 4, 2000
E. Compilation fails.
F. An exception is thrown at runtime.
6. Calendar c =
Calendar.getInstance();
17
Q
42. String s1 = " ";
43. StringBuffer s2 = new StringBuffer("
");
44. StringBuilder s3 = new
StringBuilder(" ");
45. for(int i = 0; i < 1000; i++) // Loop
#1
46. s1 = s1.concat("a");
47. for(int i = 0; i < 1000; i++) // Loop
#2
48. s2.append("a");
49. for(int i = 0; i < 1000; i++) // Loop
#3
50. s3.append("a");
Given:
18
Answer
42. String s1 = " ";
43. StringBuffer s2 = new StringBuffer("
");
44. StringBuilder s3 = new
StringBuilder(" ");
45. for(int i = 0; i < 1000; i++) // Loop
#1
46. s1 = s1.concat("a");
47. for(int i = 0; i < 1000; i++) // Loop
#2
48. s2.append("a");
49. for(int i = 0; i < 1000; i++) // Loop
#3
50. s3.append("a");
Given:
19
Given:
java Errrrr
java Errrrr x
6. try {
7. a = args[0];
8. System.out.print(a);
9. s = "t1 ";
10. }
G. Compilation fails.
14. } }
20
Given:
java Errrrr
java Errrrr x
6. try {
7. a = args[0];
8. System.out.print(a);
9. s = "t1 ";
10. }
G. Compilation fails.
Answer
14. } }
21
Q
Given:
3. class MotorVehicle {
A. 22 42 42
B. 22 63 63
5. }
C. 33 42 42
6. class Bicycle {
D. 33 63 42
7. void go(MotorVehicle m) {
E. Compilation fails.
9. } }
22
Answer
Given:
3. class MotorVehicle {
A. 22 42 42
B. 22 63 63
5. }
C. 33 42 42
6. class Bicycle {
D. 33 63 42
7. void go(MotorVehicle m) {
E. Compilation fails.
9. } }
23
Q
2. public class Pregnant extends Thread {
3. int x = 0;
4. public static void main(String[] args) {
5. Runnable r1 = new Pregnant();
6. new Thread(r1).start();
7. new Thread(r1).start();
8. }
9. public void run() {
10. for(int j = 0; j < 3; j++) {
11. x = x + 1;
12. x = x + 10;
13. System.out.println(x + " ");
14. x = x + 100;
Given:
15. } } }
24
Answer
2. public class Pregnant extends Thread {
3. int x = 0;
4. public static void main(String[] args) {
5. Runnable r1 = new Pregnant();
6. new Thread(r1).start();
7. new Thread(r1).start();
8. }
9. public void run() {
10. for(int j = 0; j < 3; j++) {
11. x = x + 1;
12. x = x + 10;
13. System.out.println(x + " ");
14. x = x + 100;
Given:
15. } } }
25
Q
Given:
5. class OOthing { void doStuff() { System.out.print("oo "); } }
6. class GuiThing extends OOthing {
7. void doStuff() { System.out.print("gui "); }
8. }
9. public class Button extends GuiThing {
18. // super.super.doStuff();
19. } }
26
Answer
Given:
5. class OOthing { void doStuff() { System.out.print("oo "); } }
6. class GuiThing extends OOthing {
7. void doStuff() { System.out.print("gui "); }
8. }
9. public class Button extends GuiThing {
18. // super.super.doStuff();
19. } }
27
Q
Given:
A. Compilation fails.
2. static int x;
4. static {
5. int[] ia = {1,2,3};
28
Answer
Given:
A. Compilation fails.
2. static int x;
4. static {
5. int[] ia = {1,2,3};
29
Q
Which are true? (Choose all that apply.)
A. If a classs members values can be retrieved, but not
changed, without using the classs API, the class is not
cohesive.
30
Answer
Which are true? (Choose all that apply.)
A. If a classs members values can be retrieved, but not
changed, without using the classs API, the class is not
cohesive.
31