Monday, August 25, 2008

scjp faq2

101Q
Given:

1. public class Wait {
2. public static void main(String [] args) {
3. String liv = new String("I'm waiting");
4. int u = 91;
5. boolean hex = False;
6. System.out.println(liv + " " + u + " " + hex);
7. }
8. }
What is the result?





A
Compilation fails because of an error on line 3.
B
Compilation fails because of an error on line 5.
C
Compilation succeeds and the program prints "I'm waiting 91 False".
D
Compilation succeeds, but an exception occurs during program execution.

102Q
Given:

1. public class ReTank {
2. public static void main(String args[]) {
3. short blue = 110;
4. short white = 2;
5.
6. }
7. }
Which two can be inserted on line 5 to allow compilation to succeed? (Choose two.)





A
int red = (blue * white);
B
byte red = (blue * white);
C
long red = (blue * white);
D
short red = (blue * white);

103Q
Given:

1. public class TryThis {
2. public static void main(String [] args) {
3. int gab = 16;
4. int gar = 11;
5.
6. System.out.println(fab);
7. }
8. }
What should be inserted on line 5 to allow fab to equal 27?





A
int fab = gab ~ gar;
B
int fab = gab & gar;
C
int fab = gab ^ gar;
D
int fab = gab << gar;

104Q
Given:

1. public class MyClass {
2. public static void main(String [] args) {
3.
4. System.out.println(x);
5. }
6. }
Which two will produce the same results when inserted on line 3? (Choose two.)





A
int x = (~25);
B
int x = (10 | 3);
C
int x = (10 >> 2);
D
int x = (10 & 22);
E
int x = (-3 >>> 1);

105Q
Given:

1. public class juju {
2. public static void main(String [] args) {
3. double z = 0xFFFFFFFC;
4. int i = 0xFFFFFFDF;
5. double x = z * i;
6. System.out.println(x);
7. }
8. }
What is the result?





A
Compilation fails because of an error on line 3.
B
Compilation fails because of an error on line 5.
C
Compilation succeeds and the program prints "-37.0".
D
Compilation succeeds and the program prints "132.0".

106Q
public class Comp {
2. public static void main(String [] args) {
3. int a = 5, b = 6, c = 1, d = 15;
4. int e = a * b;
5.
6. System.out.println(f);
7. if (e == f) {
8. System.out.println("You did it");
9. }
10. else {
11. System.out.println("Sorry");
12. }
13. }
14. }
What should be entered on line 5 that will allow the program to print "You did it"?





A
int f = |d;
B
int f = d >> a;
C
int f = d << c;
D
int f = b && d;

108Q
Given:

1. public class Clip {
2. public static void main(String [] args) {
3. int zeb = 3;
4. int jeb = 50;
5. int zj = jeb >>> zeb;
6. System.out.println(zj);
7. }
8. }
Which group of statements can replace lines 3, 4, and 5 and produce the same results?





A
int zeb = 2;
int jeb = 27;
int zj = jeb & zeb;
B
int zeb = 2;
int jeb = 27;
int zj = jeb ^ zeb;
C
int zeb = 2;
int jeb = 27;
int zj = jeb | zeb;
D
int zeb = 2;
int jeb = 27;
int zj = jeb >> zeb;

108Q
public class Vact {
2. public static Vact getVact() {
3. Vact x = new Vact();
4. return x;
5. }
6. public static void main(String [] args) {
7. Object[] o = new Object[2];
8. o[0] = getVact();
9. boolean y = o[1] instanceof Vact;
10. System.out.println(y);
11. }
12. }
What is the result?





A
Compilation fails because of an error on line 9.
B
Compilation succeeds and the program prints "true".
C
Compilation succeeds and the program prints "false".
D
Compilation fails because of errors on lines 7 and 8.

109Q
Which statement about the instanceof operator is true?





A
It returns false if a left operand does not exist.
B
It returns false if a right operand does not exist.
C
It cannot be used with primitive types and values.
D
It cannot be used with array and object types and values.

110Q
public class Hur {
2. public static void main(String [] args) {
3. int x = 352;
4. int y = 439;
5. boolean z;
6. if (y != x) {
7. z = false;
8. }
9. else {
10. z = true;
11. }
12. System.out.println(z);
13. }
14. }
What is the output?

111Q
Given:

1. public class test {
2. public static void main(String [] args) {
3. int x = 323;
4. float y = (float)x;
5. System.out.println(y);
6. }
7. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints:
323.0
C
Compilation succeeds and the program prints nothing.
D
Compilation succeeds, but an exception occurs during program execution.

112Q
public class DetMan {
2. static Object nev = new Object();
3. public static void main(String [] args) {
4. Object lim = new Object();
5. lim = nev;
6. if(nev.equals.lim) {
7. System.out.println("One");
8. }
9. else {
10. System.out.println("Two");
11. }
12. }
13. }
What is the result?





A
Compilation fails because of an error on line 2.
B
Compilation fails because of an error on line 6.
C
Compilation succeeds and the program prints "Two".
D
Compilation succeeds and the program prints "One".


113Q
public class PuNch {
2. public static void main(String [] args) {
3. Object pop = new Object();
4. String bam = new String("Loud");
5. String pow = new String("Soft");
6. pop = pow;
7. bam = "Soft";
8. pop = bam;
9.
10. System.out.println("You got it.");
11. }
12. else {
13. System.out.println("Sorry.");
14. }
15. }
What should be inserted on line 9 to test whether the objects referred to by bam and pow contain the same data?





A
if(bam = pow) {
B
if(bam == pow) {
C
if(bam.equals.pow) {
D
if(bam.equals(pow)) {

114Q
public class Zurg {
2. public static void main(String [] args) {
3. Object bl = new Object();
4. Object sr = new String("Infinity");
5. Object wd = sr;
6. bl = sr;
7. if (wd.equals(bl)) {
8. System.out.println("Beyond");
9. }
10. else {
11. System.out.println("Ion");
12. }
13. System.out.println(sr);
14. }
15. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints:
Infinity
C
Compilation succeeds and the program prints:
Ion
Infinity
D
Compilation succeeds and the program prints:
Beyond
Infinity

115Q
public class Tree {
2. public static void main(String [] args) {
3. double a = 5.4;
4. double b = 6.7;
5. Double x = new Double(b);
6. Double y = new Double(a);
7.
8.
9. }
10. }
Which two can be inserted on lines 7 and 8 to allow compilation to succeed? (Choose two.)





A
if (a = b) { System.out.println("a = b"); }
B
if (a == y) { System.out.println("a == y"); }
C
if (a == b) { System.out.println("a == b"); }
D
if (b.equals(x)) { System.out.println("b.equals(x)"); }
E
if (x.equals(y)) { System.out.println("x.equals(y)"); }
F
if (x.equals.(a)) { System.out.println("x.equals.(a)"); }

116Q
public class TexMex {
2. public static void main(String [] args) {
3. String tex = new String("Hot");
4. String z = new String("Spicy");
5. String mex = new String(z);
6. String p = tex;
7. if (tex.equals(mex)) {
8. System.out.println(z);
9. }
10. else {
11. System.out.println(p + "n" + z);
12. }
13. }
14. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints "Spicy".
C
Compilation succeeds and the program prints "HotnSpicy".
D
Compilation succeeds, but an exception occurs during program execution.

117Q
public class Blue {
2. public static void main(String [] args) {
3. Object j = new Object();
4. Boolean b = new Boolean(false);
5. Boolean c = new Boolean(true);
6. j = c;
7. c = b;
8. if (j.equals(b)) {
9. System.out.println("true");
10. }
11. else {
12. System.out.println("false");
13. }
14. }
15. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints "true".
C
Compilation succeeds and the program prints nothing.
D
Compilation succeeds and the program prints "false".

118Q
public class TeSet {
2. public static void main(String args[]) {
3. int m = 2;
4. int p = 1;
5. int t = 0;
6. for(;p < 5;p++) {
7. if(t++ > m) {
8. m = p + t;
9. }
10. }
11. System.out.println("t equals " + t);
12. }
13. }
What is the resulting value of t?





A
2
B
4
C
6
D
7


119Q
public class Lept {
2. public static void main(String [] args) {
3. int a = 320;
4. int b = 4;
5. if ((a & b) == (a | b)) {
6. System.out.println(a & b);
7. }
8. else {
9. System.out.println(a | b);
10. }
11. }
12. }
What is the result?





A
Compilation fails because of an error on line 5.
B
Compilation succeeds and the program prints "0".
C
Compilation succeeds and the program prints "324".
D
Compilation succeeds, but an exception occurs during program execution.


120Q
Given:

1. public class Crunch {
2. public static void main(String [] args) {
3. boolean j = true;
4. boolean k = false;
5. System.out.println((j && k) + " " + (j || k));
6. }
7. }
What is the result?





A
Compilation succeeds and the program prints:
true true
B
Compilation succeeds and the program prints:
true false
C
Compilation succeeds and the program prints:
false true
D
Compilation succeeds and the program prints:
false false

121Q
Given:

1. public class Spot {
2. public static void main(String [] args) {
3. int zoo = 40; // = 00101000 binary
4. int koo = 49; // = 00110001 binary
5. System.out.println(____);
6. }
7. }
What should be inserted in the blank on line 5 to produce the output of 57?





A
zoo | koo
B
zoo & koo
C
zoo ^ koo
D
zoo >> koo

122Q
Given:

1. public class Shade {
2. public static void main(String [] args) {
3. Shade s = new Shade();
4. s.dark(40);
5. }
6. public void dark(int i) {
7. System.out.println(i & 52);
8. }
9. }
What is the result?





A
Compilation fails because of an error on line 4.
B
Compilation fails because of an error on line 7.
C
Compilation succeeds and the program prints "32".
D
Compilation succeeds and the program prints nothing.

123Q
Which statement about the || operator is true?





A
It performs a bitwise OR.
B
It performs a boolean OR.
C
It performs a bitwise AND.
D
It performs a boolean AND.

124Q
public class TrFor {
2. public static void main(String [] args) {
3. int star1 = 4;
4. int star2 = 10;
5. trMeth(star1, star2);
6. System.out.println(star1 + " " + star2);
7. }
8.
9. public static void trMeth(int hold1, int hold2) {
10. hold1 = hold2 * 20;
11. }
12. }
What is the result?





A
Compilation fails because of an error on line 5.
B
Compilation fails because of an error on line 9.
C
Compilation succeeds and the program prints "4 10".
D
Compilation succeeds and the program prints "80 200".

125Q
public class OldGame {
2. public static void main(String [] args) {
3. StringBuffer word1 = new StringBuffer("tic");
4. String word2 = new String("tac");
5. chgIt1(word1);
6. chgIt2(word2);
7. System.out.println(word1+word2);
8. }
9.
10. static void chgIt2(String hold2) {
11. hold2 += "toe";
12. }
13.
14. static void chgIt1(StringBuffer hold1) {
15. StringBuffer hold3 = new StringBuffer("toe");
16. hold1.append(hold3);
17. }
18. }
What will the program produce as output?


126Q
public class MyName2 {
2. public static void getName() {
3. String holdit = "My name is ";
4. }
5.
6. public static void main(String args[]) {
7. String fname = new String(args[0]);
8. String lname = new String(args[1]);
9. String whole = new String();
10. whole = whole.concat(getName()).concat(fname).concat(" ");
11. whole = whole.concat(lname).concat(".");
12. System.out.println(whole);
13. }
14. }
java MyName2 John Smith
What is the result?





A
Compilation fails because of an error on line 3.
B
Compilation fails because of an error on line 10.
C
Compilation succeeds and the program prints "My".
D
Compilation succeeds and the program prints "My is John Smith."
E
Compilation succeeds and the program prints "My name is John Smith."


127Q
public class Roses {
2. public float getPink() {
3. double j = 5.43;
4. return (j + 3.3);
5. }
6. public static void main(String [] args) {
7. Roses r = new Roses();
8. System.out.println(r.getPink());
9. }
10. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints ".73".
C
Compilation succeeds and the program prints "8.73".
D
Compilation succeeds and the program prints nothing.

128Q
public class Wht {
2. public String getWht(String a) {
3. a = a.concat("plus");
4. return a;
5. }
6.
7. public static void main(String [] args) {
8. Wht w = new Wht();
9. System.out.println(w.getWht("A "));
10. }
11. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints:
A
C
Compilation succeeds and the program prints:
A plus
D
Compilation succeeds, but an exception occurs during program execution.

129Q
public class Pan {
2. public static void main(String [] args) {
3. Pan p = new Pan();
4. StringBuffer z = new StringBuffer("bird");
5. p.getPan(z);
6. System.out.println(z);
7. }
8.
9. public void getPan(StringBuffer a) {
10. StringBuffer b = new StringBuffer("ie");
11. a.append(b);
12. }
13. }

What is the output?


130Q
public class Mt {
2. public String getMt(String a) {
3. a += " is here";
4. return a;
5. }
6.
7. public static void main(String [] args) {
8. Mt m = new Mt();
9. System.out.println(m.getMt(args[1]));
10. }
11. }
Using this command-line invocation:

java Mt Reid
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints:
Reid
C
Compilation succeeds and the program prints:
Reid is here
D
Compilation succeeds and the program prints nothing.
E
Compilation succeeds, but an exception occurs during program execution.


131Q
public class Green {
2. public static StringBuffer sb = new StringBuffer("yellow");
3. public static String s = new String();
4.
5. public static void main(String [] args) {
6. Green g = new Green();
7. g.shock();
8. System.out.println(g.sb);
9. System.out.println(g.s);
10. }
11.
12. public void shock() {
13. s = "blue";
14. sb.append("ish");
15. s = s.concat("ish");
16. }
17. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints nothing.
C
Compilation succeeds and the program prints:
yellowish
D
Compilation succeeds and the program prints:
yellowish
blueish

132Q
public class Letr {
2. public int getit(int x) {
3. x = x * 45;
4. return x;
5. }
6.
7. public static void main(String [] args) {
8. Letr l = new Letr();
9.
10. System.out.println(j);
11. }
12. }
What should be inserted on line 9 to allow compilation to succeed and produce the output "1620"?





A
getit(36);
B
l.getit(36);
C
int j = getit(36);
D
int j = l.getit(36);

133Q
public class Chld {
2. public static int x;
3. public void getChld(int x) {
4. x += 6;
5. }
6.
7. public static void main(String [] args) {
8. Chld ch = new Chld();
9. ch.getChld(59);
10. System.out.println(x);
11. }
12. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints "0".
C
Compilation succeeds and the program prints "59".
D
Compilation succeeds and the program prints "65".


134Q
public class pcdi {
2.
3. public static void main(String [] args) {
4. pcdi p = new pcdi();
5. char b = 'b';
6. p.pc(b);
7. }
8.
9. public void pc(char a) {
10. System.out.println(a);
11. }
12. }
What is the result?





A
Compilation fails because of an error on line 5.
B
Compilation fails because of an error on line 6.
C
Compilation fails because of an error on line 9.
D
Compilation succeeds and the program prints "b".

135Q
Which two demonstrate the "has a" relationship? (Choose two.)





A
public class Student {}
public class Freshman extends Student {}
B
public interface Movie {}
public class Theatre { private Movie movie }
C
public interface Building {}
public interface Skyscraper extends Building {}
D
public class Student {
private Course theCourse;
public Course getCourse() {
return theCourse;
}
}

136Q
Which statement about encapsulation is true?





A
It requires all methods to be overridden.
B
It allows other classes to use all fields in the class.
C
It provides access to all fields and methods in the class.
D
It keeps fields protected from accidental changes.

137Q
Which statement about the "has a" relationship is true?





A
It is represented by interfaces that extend interfaces.
B
It is represented by classes that implement interfaces.
C
It is represented by objects that extend other objects.
D
It is represented by objects that contain other objects.


138Q
When member data is declared with a private access modifier, what does this represent?





A
immutability
B
encapsulation
C
synchronization
D
the "is a" relationship


139Q
Which two demonstrate the "is a" relationship? (Choose two.)





A
public interface Green {}
public class Color {}
B
public interface Cup {}
public class Glass { private Cup cup; }
C
public interface Vehicle {}
public class Suv implements Vehicle {}
D
public interface Building {}
public interface House extends Building {}


140Q
Which statement about classes demonstrating the "is a" relationship is true?





A
They override no methods from the parent.
B
They override every method from the parent.
C
They do not inherit constructors from the parent.
D
They do not inherit methods and variables from the parent.


141Q
class IntType {
2. public String getType(String a, int b, char c) {
3. String holdit1 = new String();
4. holdit1 = a;
5. return holdit1;
6. }
7. }
8.
9. class OverType extends IntType {
10.
11. String holdit2 = new String();
12. holdit2 = holdit2.concat("This is ").concat(a);
13. return holdit2;
14. }
15.
16. public static void main(String args[]) {
17. OverType ot = new OverType();
18. String x = new String("x");
19. int y = 1;
20. char z = 'b';
21. System.out.println(ot.getType(x, y, z));
22. }
23. }
When inserted on line 10, which line will override the getType method and allow compilation to succeed?





A
public Char getType(String a, int b, char c) {
B
public Char getType(int b, String a, char c) {
C
public String getType(String a, int b, char c) {
D
public String getType(int b, String a, char c) {


142Q
class GrphSet {
2. public int getGrph(int x) {
3. return 432;
7. }
8. }
9.
10. class TreeSet extends GrphSet {
11. public long getGrph(int x) {
12. return 559;
13. }
14.
15. public static void main(String [] args) {
16. TreeSet tree = new TreeSet();
17. System.out.println(tree.getGrph(30));
18. }
19. }
What is the result?





A
Compilation fails because of an error on line 11.
B
Compilation fails because of an error on line 14.
C
Compilation succeeds and the program prints "559".
D
Compilation succeeds and the program prints "432".


143Q
public class AClass {
2. public static int aNum(int hold1, int hold2) {
3. hold1 = ((hold1 * 5) + hold2);
4. return hold1;
5. }
6.
7. public static int aNum(int hold1) {
8. hold1 = (hold1 * 5);
9. return hold1;
10. }
11.
12. public static void main(String [] args) {
13. int x1 = 1;
14. int x2 = 5;
15. System.out.println(aNum(x1, x2));
16. }
17. }
What is the result?





A
Compilation fails because of an error on line 2.
B
Compilation fails because of an error on line 15.
C
Compilation succeeds and the program prints "5".
D
Compilation succeeds and the program prints "10".


144Q
class AndBit {
2. public static String getBit(String a, int b, char c) {
3. String n = new String();
4. n = n.concat(a).concat(" got it");
5. return n;
6. }
7. }
8.
9. class OrBit extends AndBit {
10. public static int getBit(int b, String a, char c) {
11. int m = b * 3;
12. return m;
13. }
14.
15. public static void main(String [] args) {
16. String y = new String("You");
17. int x = 2;
18. char z = 'b';
19.
20. }
21. }
When inserted on line 19, which line will produce the output of "You got it"?





A
System.out.println(getBit(x));
B
System.out.println(getBit(y));
C
System.out.println(getBit(x, y, z));
D
System.out.println(getBit(y, x, z));


145Q
1. public class Home {
2. public void keepit(int x, int y) {
3. x = x * y;
4. System.out.println(x);
5. }
6.
7. public void keepit(int x, int y, int z) {
8. x = (x * y) + z;
9. System.out.println(x);
10. }
11.
12. public static void main(String [] args) {
13. Home h = new Home();
14. int a = 20;
15. int b = a + 4;
16. int c = b / 2;
17. if (c > 10) {
18. h.keepit(a, b, c);
19. }
20. else {
21. h.keepit(a, b);
22. }
23. }
24. }

What is the result?





A
Compilation fails.
B
Compilation succeeds and the program executes the method at line 2.
C
Compilation succeeds and the program executes the method at line 7.
D
Compilation succeeds, but an exception occurs during program execution.


146Q
class Tip {
2. public void getTip(String a) {
3. a += "Hello";
4. System.out.println(a);
5. }
6. }
7.
8. public class Top extends Tip {
9. public String getTip(String a) {
10. a = "Hi";
11. System.out.println(a);
12. }
13.
14. public static void main(String [] args) {
15. Top t = new Top();
16. String z = "Bye";
17. t.getTip(z);
18. }
19. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints "ByeHi".
C
Compilation succeeds and the program prints "ByeHello".
D
Compilation succeeds, but an exception occurs during program execution.



147Q
Given:

1. public class Snake {
2. public String tiger(String a) { return a; }
3. }
Which two might form legal overloads of the tiger method? (Choose two.)





A
public char tiger(String a) { return a; }
B
public String tiger(String b) { return b; }
C
public String tiger(String a, int b) { return a; }
D
public int tiger(String a, int b, int c) { return b; }


148Q
Given:

1. class Frog {
2. public Double toad(Double a) { return a; }
3. }
Which method overrides the toad method?





A
4. public class Frogger extends Frog {
5. public Float toad(Float a) { return a; }
6. }
B
4. public class Frogger extends Frog {
5. public Double toad(Double a) { return a; }
6. }
C
4. public class Frogger extends Frog {
5. public Float toad(Double a, Float b) { return a; }
6. }
D
4. public class Frogger extends Frog {
5. public Double toad(Double a, Double b) { return a; }
6. }


149Q
Which statement about overriding methods is true?





A
The argument list must match the original.
B
The return type can differ from the original.
C
The method name can differ from the original.
D
The argument list must differ from the original.


150Q
Which statement about overloading methods is true?





A
The return type must match the original.
B
The argument list must match the original.
C
The method name can differ from the original.
D
The argument list must differ from the original.

151Q
What is a proper method for declaring inner classes?





A
public class MyClass {}
static class YourClass {}
B
public class MyClass {
static class YourClass {}
}
C
public class MyClass {}
static class YourClass extends MyClass {}
D
public class MyClass {
static class YourClass { Object x = new Object(MyClass); }
}


152Q
public class Home {
2. public void keepit(int x, int y) {
3. x = x * y;
4. System.out.println(x);
5. }
6.
7. public void keepit(int x, int y, int z) {
8. x = (x * y) + z;
9. System.out.println(x);
10. }
11.
12. public static void main(String [] args) {
13. Home h = new Home();
14. int a = 20;
15. int b = a + 4;
16. int c = b / 2;
17. if (c > 10) {
18. h.keepit(a, b, c);
19. }
20. else {
21. h.keepit(a, b);
22. }
23. }
24. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program executes the method at line 2.
C
Compilation succeeds and the program executes the method at line 7.
D
Compilation succeeds, but an exception occurs during program execution.

153Q
class Tip {
2. public void getTip(String a) {
3. a += "Hello";
4. System.out.println(a);
5. }
6. }
7.
8. public class Top extends Tip {
9. public String getTip(String a) {
10. a = "Hi";
11. System.out.println(a);
12. }
13.
14. public static void main(String [] args) {
15. Top t = new Top();
16. String z = "Bye";
17. t.getTip(z);
18. }
19. }


A
Compilation fails.
B
Compilation succeeds and the program prints "ByeHi".
C
Compilation succeeds and the program prints "ByeHello".
D
Compilation succeeds, but an exception occurs during program execution.
154Q
What is a proper method for declaring inner classes?





A
public class MyClass {}
static class YourClass {}
B
public class MyClass {
static class YourClass {}
}
C
public class MyClass {}
static class YourClass extends MyClass {}
D
public class MyClass {
static class YourClass { Object x = new Object(MyClass); }
}

155Q
public class TheClass {
2. static int x = 3;
3. public static void main(String [] args){
4. x += OtrClass.x;
5. System.out.println(x);
6. }
7.
8. static class OtrClass {
9. static int x = TheClass.x + 4;
10. }
11. }
What is the result?





A
Compilation fails because of an error on line 4.
B
Compilation fails because of an error on line 9.
C
Compilation succeeds and the program prints "7".
D
Compilation succeeds and the program prints "10".

156Q
Given:

1. public class ThisClass {
2. private String mine = new String("mine");
3. public class ThatClass {
4. public void yourMeth() {
5. //method body
6. }
7. }
8. }
What should you use to instantiate ThatClass from outside its containing class?





A
ThatClass tc = new ThisClass.ThatClass();
B
ThatClass tc = ThatClass.new ThatClass();
C
ThisClass tc = new ThisClass();
ThisClass.ThatClass thc = tc.new ThatClass();
D
ThisClass tc = tc.new ThisClass();
tc.ThatClass thc = new tc.ThatClass();


157Q
Which statement about anonymous classes is true?





A
An interface can be defined anonymously.
B
An anonymous class can define static methods.
C
An anonymous class can declare static final constants.
D
A constructor for an anonymous class is defined using the same name as the containing class.

158Q
Given:

1. public class MyClass {
2. public static class YourClass {}
3. }
Which statement will construct an instance of the YourClass class from a separate class?





A
YourClass yc = new YourClass();
B
MyClass mc = new MyClass(YourClass());
C
MyClass.YourClass yc = new MyClass.YourClass();
D
MyClass mc = new MyClass();
mc.YourClass yc = new mc.YourClass();

159Q
Which statement about an anonymous inner class is true?





A
It cannot implement interfaces.
B
The constructor cannot take arguments.
C
It can be declared from within a method.
D
It can directly implement multiple interfaces.

160Q
Given:

1. class MyClass {
2. public class YourClass {}
3. }
What can you use to create an instance of YourClass from a separate class?





A
MyClass mc = new YourClass();
B
YourClass yc = new YourClass();
C
MyClass mc = new MyClass(YourClass());
D
MyClass mc = new MyClass();
MyClass.YourClass yc = mc.new YourClass();


161Q
public class Both implements Runnable {
2. public char [] c = new char[2];
3. public Both() {
4. c[0] = 'A';
5. c[1] = 'B';
6. }
7.
8. public void run() {
9. int i = 1;
10. for(int j = 0; j < 2; j++) {
11. if (i == 1) {
12. System.out.println(c[1] + " " + c[0]);
13. i = 0;
14. }
15. else {
16. System.out.println(c[0] + " " + c[1]);
17. i = 1;
18. }
19. }
20. }
21. }
The Both class should print: B A A B Which main() method will allow this output by the Both class?





A
public static void main(String [] args) {
new Thread(Both()).start();
}
B
public static void main(String [] args) {
new Thread(new Both()).start();
}
C
public static void main(String [] args) {
Thread bth = new Both();
bth.start();
}
D
Thread.start();public static void main(String [] args) {
Thread bth = new Both();
Thread.start();
}


162Q
These steps are used to define and execute a Thread.

1. Define a class that implements Runnable.
2.
3. Call the start() method of the Thread object.
Which process should be inserted on line 2 to complete these steps?





A
Override the run method.
B
Instantiate the Thread subclass.
C
Pass an instance of the Runnable object to the Thread constructor.
D
Pass an instance of the Thread object to the Runnable constructor and override the run method.


163Q
Why is the run() method implemented when using a thread?





A
The run() method is used to instantiate the Thread.
B
The run() method is used to start the execution of the Thread.
C
The run() method is used as the constructor for the class implementing the Thread.
D
The run() method is used to define the code that should be executed when the Thread is started.

164Q
Which method will start the execution of an instance myThread of a thread?





A
myThread.run()
B
myThread.join()
C
myThread.start()
D
myThread.isAlive()


165Q
public class Cookies implements Runnable {
2.
3. int pb = 3, z = 0;
4. for (;z < 15;z++) {
5. pb = pb + 3;
6. System.out.println(pb);
7. }
8. }
9.
10. public static void main(String [] args) {
11. Cookies ck = new Cookies();
12. new Thread(ck).start();
13. }
14. }
What should be inserted on line 2 to allow compilation to succeed?





A
public void set() {
B
public void run() {
C
public void start() {
D
public void execute() {


166Q
public class Jar extends Thread {
2. public void run() {
3. int gls = 4;
4. for (int t = 0; t < 3; t++) {
5. gls = (gls * 4)/2;
6. System.out.println(gls);
7. }
8. }
9.
10. public static void main(String [] args) {
11.
12.
13. }
14. }
To result in this output:

8
16
32
What should be inserted on lines 11 and 12?





A
Jar j = new Jar();
j.start();
B
Jar j = new Jar();
new Thread;
C
Thread t = new Thread();
t.start();
D
Thread t = new Thread(Jar);
t.start();


167Q
public class Sand implements Runnable {
2. public static void main(String [] args) {
3. Sand s = new Sand();
4. new Thread(s).start();
5. }
6.
7. public void run() {
8. double d = 1.2;
9. for (int x = 0; x < 2; x++) {
10. d = (d + 1.0);
11. System.out.println(d);
12. }
13. }
14. }
What is the result?





A
Compilation fails because of an error on line 3.
B
Compilation fails because of an error on line 4.
C
Compilation succeeds and the program prints:
2.2
3.2
D
Compilation succeeds and the program prints a blank line.
E
Compilation succeeds, but an exception occurs during program execution.

168Q
public class Beans implements Runnable {
2. public static void main(String [] args) {
3. Thread t = new Thread(new Beans);
4. t.start();
5. }
6.
7. public void run() {
8. for (int x = 0; x < 5; x++) {
9. System.out.println("This is iteration " + x);
10. }
11. }
12. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints a blank line.
C
Compilation succeeds, but an exception occurs during program execution.
D
Compilation succeeds and the program prints:
This is iteration 0
This is iteration 1


169Q
public class Bath implements Runnable {
2. public static void main(String [] args) {
3. Bath bth = new Bath();
4. new Thread(bth).start();
5. }
6.
7. public void run(bth) {
8. int h = 0, g = 0;
9. for (;g < 4;g++) {
10. h = h + 2;
11. System.out.println(h);
12. }
13. }
14. }
What is the result?





A
Compilation fails because of an error on line 4.
B
Compilation fails because of an error on line 7.
C
Compilation succeeds and the program prints:
2
4
6
8
D
Compilation succeeds and the program prints a blank line.


170Q
Which statement about a thread is true?





A
The notify() method cannot be used within a synchronized block.
B
A thread stops executing only when the stop() method is called.
C
The wait() method cannot be used within a synchronized block.
D
The wait() method is called on an object to stop execution of a thread until a notify() or notifyAll() method is issued on the same object.


171Q
Which statement about scheduling thread execution is true?





A
The wait() method is called to place a thread at the back of the runnable pool to allow threads of a lower priority to start.
B
The yield() method is called to place a thread at the back of the runnable pool to allow threads of a lower priority to start.
C
The wait() method is called to place a thread at the back of the runnable pool to allow threads of the same priority to start.
D
The yield() method is called to take a thread out of the running state and place it into the runnable pool to allow threads of the same priority to start.


172Q
public class News implements Runnable {
2. public void run() {
3. for (int x = 0; x < 5; x++) {
4. System.out.println(x);
5. }
6. }
7.
8. public static void main(String [] args) {
9. News n = new News();
10. Thread t1 = new Thread(n);
11. Thread t2 = new Thread(n);
12. t1.start();
13. t2.start();
14. t1.start();
15. }
16. }
What is the result?





A
Compilation fails.
B
Compilation succeeds, but an exception occurs during program execution.
C
Compilation succeeds and the program prints two sets of numbers beginning with 0 and ending with 5.
D
Compilation succeeds and the program prints three sets of numbers beginning with 0 and ending with 5.

173Q
public class Red extends Thread {
2. public void run() {
3. for (int y = 0; y < 3; y++) {
4. System.out.println(y);
5. if (y == 1) { System.exit(0); }
6. }
7. }
8.
9. public static void main(String [] args) {
10. Red r = new Red();
11. Thread t1 = new Thread(r);
12. Thread t2 = new Thread(r);
13. t1.start();
14. t2.start();
15. }
16. }
Which result is possible?





A
Compilation fails.
B
Compilation succeeds and the program prints:
0
1
C
Compilation succeeds and the program prints:
0
1
2
0
1
2
D
Compilation succeeds, but an exception occurs during program execution.

174Q
public class Snow {
2. public static void main(String [] args) {
3. Snow s = new Snow();
4. new Thread() {
5. public void run() {
6. for (int w = 0; w < 2; w++) {
7. System.out.print(w);
8. }
9. }
10. }.start();
11. new Thread() {
12. public void run() {
13. for (int z = 5; z < 7; z++) {
14. System.out.print(z);
15. }
16. }
17. }.start();
18. }
19. }
public class Snow {
2. public static void main(String [] args) {
3. Snow s = new Snow();
4. new Thread() {
5. public void run() {
6. for (int w = 0; w < 2; w++) {
7. System.out.print(w);
8. }
9. }
10. }.start();
11. new Thread() {
12. public void run() {
13. for (int z = 5; z < 7; z++) {
14. System.out.print(z);
15. }
16. }
17. }.start();
18. }
19. }

public class Snow {
2. public static void main(String [] args) {
3. Snow s = new Snow();
4. new Thread() {
5. public void run() {
6. for (int w = 0; w < 2; w++) {
7. System.out.print(w);
8. }
9. }
10. }.start();
11. new Thread() {
12. public void run() {
13. for (int z = 5; z < 7; z++) {
14. System.out.print(z);
15. }
16. }
17. }.start();
18. }
19. }

What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints a blank line.
C
Compilation succeeds, but an exception occurs during program execution.
D
Compilation succeeds and the program prints the numbers 0, 1, 5, and 6, in some order.


175Q
Which statement about threads in deadlock is true?





A
It occurs when Thread1 and Thread2 are waiting for a lock held by Thread3.
B
It occurs when Thread1 is waiting for a lock held by Thread2, and Thread2 is waiting for a lock held by Thread3.
C
It occurs when Thread1 is waiting for a lock held by Thread2, and Thread2 is waiting for a lock held by Thread1.
D
It occurs when Thread1 is waiting for a lock held by Thread2, and Thread3 is waiting for a lock held by Thread1.


176Q
public class MyThread implements Runnable {
2. private String holdA = "This is ";
3. private int[] holdB = {1,2,3,4,5,6,7,8,9,10};
4.
5. public static void main(String args[]) {
6. MyThread z = new MyThread();
7. (new Thread(z)).start();
8. (new Thread(z)).start();
9. }
10.
11. public synchronized void run() {
12. for(int w = 0;w < 10;w++) {
13. System.out.println(holdA + holdB[w] + ".");
14. }
15. }
16. }
What is the result?





A
Compilation fails because of an error on line 6.
B
Compilation fails because of an error on line 11.
C
Compilation fails because of errors on lines 7 and 8.
D
Compilation succeeds and the program prints each value in the holdB array at the end of the "This is " line. Each value is printed two times before the program ends, and the values are not printed in sequential order.
E
Compilation succeeds and the program prints each value in the holdB array at the end of the "This is " line. Each value is printed in order from 1 to 10 and, after the value 10 prints, it starts printing the values 1 to 10 in order again.


177Q
Which statement about the join() method of the Thread class is true?





A
It is used to combine two threads so that they execute simultaneously.
B
It is used to allow other threads of the same priority a chance to execute.
C
It is used to cause the current thread to pause for a specified period of time.
D
It is used to cause the current thread to pause until the argument thread terminates.


178Q
public class Sleep implements Runnable {
2. public void run() {
3. for (int z = 0; z < 15; z++) {
4. System.out.println(z);
5. }
6. }
7.
8. public static void main(String [] args) {
9. Sleep s = new Sleep();
10. Thread ta = new Thread(s);
11. Thread tb = new Thread(s);
12. try {
13.
14.
15.
16.
17. }
18. catch (InterruptedException e) {
19. System.out.println(e);
20. }
21. }
22. }


The first set of numbers, 0 to 14, should print in its entirety. The second set of numbers, 0 to 14, should print in its entirety after the first set. Which body should be inserted in the try block on line 13 to accomplish this?





A
ta.start();
ta.join();
tb.start();
B
ta.start();
tb.join();
tb.start();
C
ta.start();
ta.wait();
tb.join();
tb.start();
D
ta.start();
tb.wait();
tb.start();
tb.join();

179Q
public class Geo {
2. public static void main(String [] args) {
3. Geo g = new Geo();
4. final Object o1 = new Object();
5. final Object o2 = new Object();
6. Thread tx = new Thread(new Runnable() {
7. public void run() {
8. synchronized(o1) {
9. synchronized(o2) {
10. calc();
11. }
12. }
13. }
14. });
15.
16. Thread ty = new Thread(new Runnable() {
17. public void run() {
18. synchronized(o2) {
19. synchronized(o1) {
20. calc();
21. }
22. }
23. }
24. });
25. tx.start();
26. ty.start();
27. }
30. }
Which statement about the Geo.java program is true?





A
An exception can occur because of the two threads.
B
The program contains invalid synchronize statements.
C
A deadlock situation can occur with the two threads.
D
The program is constructed in a way that will avoid deadlock.


180Q
class Gotit2 {
2. int a, b;
3. public void geta(int g) {
4. a = a + g;
5. }
6. public void getb(int g) {
7. b = b + g;
8. }
9. public synchronized void getboth(int g) {
10. geta(g);
11. getb(g);
12. }
13. public synchronized boolean testit() {
14. return (a != b);
15. }
16. }

Which statement is true?





A
The testit() method will always return true.
B
The testit() method will always return false.
C
The testit() method can return true if geta() and getb() are called by multiple threads.
D
The testit() method can return true if the getboth() method is called by multiple threads.

181Q
public class Meet implements Runnable {
2. public void run() {
3. for (int y = 0; y < 20; y++) {
4. System.out.println(y);
5. }
6. }
7.
8. public static void main(String [] args) {
9. Meet m = new Meet();
10. Thread t1 = new Thread(m);
11. Thread t2 = new Thread(m);
12. try {
13. t1.start();
14. join(t1);
15. t2.start();
16. }
17. catch (Exception e) {
18. System.out.println(e);
19. }
20. }
21. }
What is the result?





A
Compilation fails.
B
Compilation succeeds, but an exception occurs during program execution.
C
Compilation succeeds and the program prints two sets of numbers from 0 to 19 in random order.
D
Compilation succeeds and the program prints one set of numbers from 0 to 19, and then repeats the set of numbers from 0 to 19.


182Q
What is used when defining threads to prevent more than one thread from executing critical parts of the same object concurrently?





A
join
B
wait
C
notify
D
synchronized


183Q
Which statement about the notify() method of an object is true?





A
It is used to place every thread into a wait mode.
B
It is used to wake up every thread that is waiting.
C
It is used to place a single thread into a wait mode.
D
It is used to wake up a single thread that is waiting.

184Q
Which statement about the wait() method of an object is true?





A
The parent class for the wait() method is java.lang.Thread.
B
The wait() method must be called from within a synchronized block.
C
The wait() method is only used in conjunction with the join() method.
D
The thread calling the wait() method does not release the object's lock flag.

185Q
public class Tack extends Thread {
2. public static void main(String [] args) {
3. Tack tk = new Tack();
4. new Thread(tk).start();
5. }
6.
7. public void run() {
8. int x = 0, z = 5;
9. for (;x<4;x++) {
10. z = z + 10;
11. System.out.print(z + " ");
12. }
13. }
14. }
What is the result?





A
Compilation fails because of an error on line 1.
B
Compilation fails because of an error on line 4.
C
Compilation succeeds and the program prints: 15 25 35 45
D
Compilation succeeds, but an exception occurs during program execution.
204Q

public class GetIt {
2. public static void main(String args[]) {
3. double x[] = {10.2, 9.1, 8.7};
4. int i[] = new int[3];
5. for(int a = 0;a < (x.length);a++) {
6.
7. System.out.println(i[a]);
8. }
9. }
10. }
The GetIt class should print the following:

11
10
9
Which line should you insert on line 6 to accomplish this?





A
i[a] = ((int)Math.min(x[a]));
B
i[a] = ((int)Math.max(x[a]));
C
i[a] = ((int)Math.ceil(x[a]));
D
i[a] = ((int)Math.floor(x[a]));


205Q
Given:

1. public class testit {
2. public static void main(String [] args) {
3. double x = 30.456;
4. System.out.println(Math.floor(x));
5. }
6. }
What is the output?

206Q
Given:

1. public class Plant {
2. public static void main(String [] args) {
3. float j = 1.34f;
4. float k = 0.64f;
5. int z = (int)Math.round(j * k);
6. System.out.println(z);
7. }
8. }
Given that the value of 1.34 * 0.64 is 0.8576, what is the output?


207Q
Given:

1. public class Gush {
2. public static void main(String [] args) {
3. // Insert method body here
4. }
5. }
Which main method body should be inserted at line 3 to result in the printing of the tangent of 76 degrees?





A
double p = Math.tan(Math.toDegrees(76)); System.out.println(p);
B
double p = Math.tangent(Math.toRadians(76)); System.out.println(p);
C
double p = Math.toRadians(76); double f = Math.tan(p); System.out.println(f);
D
double p = Math.toDegrees(76); double f = Math.tangent(p); System.out.println(f);


208Q
Given:

1. public class Shade {
2. public static void main(String [] args) {
3. float x, y;
4. x = 2.1f;
5. y = 1.3f;
6.
7. }
8. }
Which statement, inserted on line 6, would produce the output "2.1"?





A
System.out.println(Math.min(x, y));
B
System.out.println(Math.max(x, y));
C
System.out.println(Math.ceil(x, y));
D
System.out.pirntln(Math.floor(x, y));


209Q
Given:

1. public class GorLeg {
2. public static void main(String [] args) {
3. String gl = new String("Hello");
4. String lg = gl;
5. lg.concat(" you ");
6. lg += " there!";
7. System.out.println(lg);
8. }
9. }
What is the result?





A
Compilation fails.
B
Compilation succeeds and the program prints: Hello
C
Compilation succeeds and the program prints: Hello there!
D
Compilation succeeds and the program prints: Hello you there!


210Q
Given:

1. public class Shoe {
2. public static void main(String [] args) {
3. String tie = "white";
4. String knot = tie;
5. tie.concat("laces");
6. System.out.println(tie);
7. }
8. }
What is the result?





A
Compilation fails because of an error on line 4.
B
Compilation fails because of an error on line 5.
C
Compilation succeeds and the program prints "white".
D
Compilation succeeds and the program prints "whitelaces".

211Q
Given:

1. public class Lth {
2. public static void main(String [] args) {
3. String c = "foodshop";
4. String g = c;
5. g = c.substring(3);
6. c = g;
7. System.out.println(c);
8. }
9. }
What is the output?


212Q
Given:

1. public class Morg {
2. public static void main(String [] args) {
3. String lift = "heavy";
4. String tow = "tough";
5. lift.concat(tow);
6. System.out.println(lift);
7. }
8. }
What is the output?

213Q
Which statement about the Map interface is true?





A
Entries are placed in a Map using the values() method.
B
Entries are placed in a Map using the entrySet() method.
C
A key/value association is added to a Map using the put() method.
D
A key/value association is added to a Map using the putAll() method.


214Q
Which statement about the java.util.Set interface is true?





A
It allows duplicates.
B
It is an ordered collection.
C
It does not allow duplicates.
D
It is a collection of key and value pairs.


215Q
Which class contains ordered elements in a collection?





A
java.util.Set
B
java.util.ArrayList
C
java.util.Map
D
java.util.Observable


216Q
What are two interfaces implemented by the java.util.Hashtable class? (Choose two.)





A
java.util.Map
B
java.util.Set
C
java.util.List
D
java.lang.Cloneable
E
java.util.Collection
F
java.lang.Comparable


217Q
Which statement about the java.util.HashSet class is true?





A
The implementation is synchronized.
B
It does not permit the null element.
C
It does not guarantee the iteration order of the set.
D
It guarantees that the set is and will remain in a particular order.


218Q
Which statement about the java.util.SortedSet class is true?





A
A set can contain duplicate elements.
B
A set can contain any number of null elements.
C
It does not guarantee the set will be processed in a particular order.
D
All elements inserted into a sorted set must implement the Comparable interface.


219Q
Which interface is implemented to generate a series of elements, one at a time?





A
Map
B
SortedSet
C
Comparator
D
Enumeration


220Q
Which class will allow you to parse and print numbers in the format of the user's region?





A
BitSet
B
Locale
C
Hashtable
D
Observable
E
SimpleTimeZone

221Q
Which statement about the java.util.Vector class is true?





A
It represents a growable array of objects.
B
It represents a last-in-first-out stack of objects.
C
It represents a hashtable-based map with weak keys.
D
It represents a task that can be scheduled for one-time or repeated execution.

No comments: