Escolar Documentos
Profissional Documentos
Cultura Documentos
2012
LARA
Lara technologies
TECHNOLOG
Y Core java part-2
CORE JAVA
PART-2
21-08-2012
102/12, 2nd Main, 5th Cross, Venkateswara College Road, Ramaiah Garden, Chikka
Adugodi, Thavarakere, Bangalore – 560029.
Phone No: 080 – 4131 0124
/apps/com/company/application directory
E. java -classpath Answer: B
/apps/com/company/application:. MainClass
4. Program
if run
from the /apps directory A UNIX user named Bob wants to replace
F. java com.company.application.MainClass his chess program with a
if run from the new one, but he is hot sure where the old
/apps/com/company/application directory one is installed. Bob is
currently able to run a Java chess program
Answer: BC starting from his home
directory /home/bob using the command:
3. Program
java -classpath /test:/home/bob/downloads/*
Given a correctly compiled class whose
.jar games.Chess
source code is:
Bob’s CLASSPATH is set (at login time) to:
1. package com.sun.sjcp;
/
2. public class Commander {
usr/lib:/home/bob/classes:/opt/java/lib:/opt/j
3. public static void main(String[] args) {
ava/lib/* .jar
4. // more code here
What is a possible location for the
5. }
Chess.class file?
6. }
A. /test/Chess.class
Assume that the class file is located in
B. /home/bob/Chess.class
/foo/com/sun/sjcp/, the current
C. /test/games/Chess.class
directory is /foo/, and that the classpath
D. /usr/lib/games/Chess.class
contains “.“ (current
E. /home/bob/games/Chess.class
directory).
F. inside jarfile /opt/java/lib/Games.jar (with
Which command line correctly runs
a correct manifest)
Commander?
G. inside jarfile
A. java Commander
/home/bob/downloads/Games.jar (with a
B. java com. sim. sjcp.Commander
correct
C. java com/sun/sjcp/Commander
manifest)
D. java -cp com.sun.sjcp Commander
E. java -cp com/sun/sjcp Commander
Answer: C
6. Program
5. Program enum Days
{
A class games.cards.Poker is correctly MON, TUE, WED, THRS, FRI,
defined in the jar file Poker.jar. SAT, SUN;
A user wants to execute the main method of }
Poker on a UNIX system 7. Program
using the command: class Manager
{
java games.cards.Poker public static void main(String[] args)
What allows the user to do this? {
Days d1 = Days.MON;
A. put Poker.jar in directory /stuff/java, and System.out.println(d1);
set the CLASSPATH to include Days d2 = Days.FRI;
System.out.println(d2);
/stuff/java Days d3 = Days.SUN;
B. put Poker.jar in directory /stuff/java, and System.out.println(d3);
}
set the CLASSPATH to include }
/stuff/java/*.jar
8. Program
C. Put Poker.jar in directory /stuff/java, and enum Month
set the CLASSPATH to include {
JAN, FEB, MAR, APR, MAY, JUN,
/stuff/java/Poker.jar
JUL, AUG, SEP, OCT, NOV, DEC;
D.putPoker.jarindirectory/stuff/java/games/c }
ards, and set theCLASSPATH to include class Manager1
{
/stuff/java public static void main(String[] args)
E.putPoker.jarindirectory/stuff/java/games/c {
Month m1 = Month.JAN;
ards, and set the CLASSPATH to include System.out.println(m1);
/stuffijava/*.jar m1 = Month.JUL;
System.out.println(m1);
F.putPoker.jarindirectory/stuff/java/games/c m1 = Month.MAR;
ards, andset the CLASSPATH to include System.out.println(m1);
}
/stuff/java/Poker.jar }
Answer: C
9. Program
class Manager2
{
enum A a, b, c, d;
{ }
CON1, CON2, CON3; public static void main(String[] args)
} {
public static void main(String[] args) E e1 = E.b;
{ System.out.println(e1);
A a1 = A.CON2; E e2 = E.d;
System.out.println(a1); System.out.println(e2);
A a2 = A.CON3;
System.out.println(a2);
}
System.out.println(e1.ordinal());
}
System.out.println(e2.ordinal());
}
10. Program
}
class A
{
enum B
{ 12. Program
C1, C2, C3;
} class Manager5
static void test() {
{ enum Test
B b1 = B.C2; {
System.out.println(b1); t1, t2, t3, t4, t5;
B b2 = B.C3; }
System.out.println(b2); public static void main(String[] args)
} {
} Test x[] = Test.values();
class Manager3 for(int i = 0; i < x.length; i+
{
+)
public static void main(String[] args)
{
{
A.B b1 = A.B.C2;
System.out.println(b1); System.out.println(x[i]);
A.B b2 = A.B.C3; }
System.out.println(b2); }
A.test(); }
}
}
15. { System.out.println(c); }
class Manager7
16. } {
enum X
17. }
{
Which code inserted at line 14 causes the A, B, C, D;
}
foo method to print RED,
public static void main(String[] args)
GREEN, and BLUE? {
X x1 = X.C;
A. for( Color c : Color.values())
switch(x1)
B. for( Color c = RED; c <= BLUE; c++) {
case A:
C. for( Color c; c.hasNext() ; c.next())
{
D. for( Color c = Color[0]; c <= Color[2]; System.out.println("in A");
break;
c++)
}
E. for( Color c = Color.RED; c <= case B:
{
Color.BLUE; c++)
System.out.println("in B");
break;
}
Answer: A
case C:
{
System.out.println("in C");
14. Program break;
}
class Manager6 case D:
{ {
enum C System.out.println("in D");
{ break;
X, Y, Z; }
} }
public static void main(String[] args) System.out.println("done");
{ }
C c1 = C.valueOf("X"); }
System.out.println(c1);
C c2 = C.valueOf("Z");
System.out.println(c2); 16. Program
C c3 = C.valueOf("A");
System.out.println(c3); Given:
{ public interface B
System.out.println("CSCB-test"); {
} int j = 20;
},CON4, CON5; String k = "abc";
}
void test()
{
System.out.println("test"); 27. Program
}
package pack1;
}
public enum C
public static void main(String[] args)
{
{
CON1, CON2, CON3;
A a1 = A.CON5;
}
A a2 = A.CON1;
A a3 = A.CON3;
A a4 = A.CON4;
System.out.println(a1); 28. Program
System.out.println(a2);
System.out.println(a3); package pack2;
System.out.println(a4); class Manager
a1.test(); {
a2.test(); public static void main(String[] args)
a3.test(); {
a4.test(); System.out.println(pack1.A.i);
} pack1.A.test();
} System.out.println(pack1.B.j);
System.out.println(pack1.B.k);
System.out.println(pack1.C.CON1);
25. Program System.out.println(pack1.C.CON2);
System.out.println(pack1.C.CON3);
}
package pack1;
}
public class A
{
public static int i = 20; 29. Program
public static void test()
{ package pack2;
System.out.println("done"); import static pack1.A.i;
} import static pack1.A.test;
} import static pack1.B.j;
import static pack1.B.k;
import static pack1.C.CON1;
26. Program import static pack1.C.CON2;
import static pack1.C.CON3;
class Manager1
package pack1;
{ {
public static void main(String[] args)
{
System.out.println(Integer.MAX_VALUE);
System.out.println(i);
test();
System.out.println(j);
System.out.println(k); System.out.println(Long.MAX_VALUE);
System.out.println(CON1); }
System.out.println(CON2); }
System.out.println(CON3);
}
} 32. Program
package pack2;
30. Program import static
java.lang.Integer.MAX_VALUE;
package pack2;
//import static
import static pack1.A.i;
import static pack1.A.test; java.lang.Long.MAX_VALUE;
import static pack1.B.j; import static java.lang.Long.*;
import static pack1.B.k; class Manager4
import static pack1.C.CON1; {
import static pack1.C.CON2; public static void main(String[] args)
import static pack1.C.CON3; {
import static java.lang.System.out; System.out.println(MAX_VALUE);
class Manager2 }
{ }
public static void main(String[] args)
{
out.println(i); 33. Program
test();
out.println(j); Given:
out.println(k); 10. package com.sun.scjp;
out.println(CON1);
out.println(CON2); 11. public class Geodetics {
out.println(CON3); 12. public static final double DIAMETER =
}
} 12756.32; // kilometers
13. }
Which two correctly access the DIAMETER
31. Program
member of the Geodetics
package pack2; class? (Choose two.)
class Manager3
{ A. import com.sun.scjp.Geodetics;
public static void main(String[] args)
36. Program
35. Program
package pack2;
import static java.lang.System.out;
Given: import static java.lang.System.exit;
class Manager6
{ //import pack1.B;
public static void main(String[] args) class Manager9
{ {
out.println("1111"); public static void main(String[] args)
exit(0); {
out.println("2222"); System.out.println(j);
} System.out.println(k);
} System.out.println(B.k);
System.out.println(B.j);
B b1 = null;
37. Program }
}
package pack2;
import static java.lang.System.*;
class Manager7 40. Program
{
public static void main(String[] args) Given a class Repetition:
{
out.println("1111"); 1. package utils;
exit(0); 2.
out.println("2222");
} 3. public class Repetition {
} 4. public static String twice(String s)
{ return s + s; }
38. Program 5. }
and given another class Demo:
package pack2;
import static pack1.A.*; 1. // insert code here
import pack1.A; 2.
class Manager8
{ 3. public class Demo {
public static void main(String[] args) 4. public static void main(String[] args) {
{
System.out.println(i); 5. System.out.println(twice(”pizza”));
test(); 6. }
A a1 = new A();
} 7. }
} Which code should be inserted at line 1 of
Demo.java to compile and
39. Program run Demo to print “pizzapizza”?
A. import utils.*;
package pack2;
import static pack1.B.*; B. static import utils.*;
C. import utils.Repetition.*; {
this.x = x;
D. static import utils.Repetition. *;
}
E. import utils.Repetition.twice(); Job nextJob()
{
F. import static utils.Repetition.twice;
return x[index ++];
G. static import utils.Repetition.twice; }
boolean hasNext()
Answer:F {
41. Program return index < x.length;
}
}
package pack2;
class Manager
import static pack1.C.*;
{
import pack1.C;
public static void main(String[] args)
class Manager10
{
{
Job j1 = new Job("software",
public static void main(String[] args)
{ 50000.9);
System.out.println(CON1); Job j2 = new Job("hardware",
System.out.println(CON2);
10000.9);
System.out.println(CON3);
Job j3 = new Job("hr", 500.9);
System.out.println(C.CON3);
Job x[] = {j1, j2, j3};
C c1 = C.CON1;
Employee emp = new Employee(x);
}
print(emp);
}
System.out.println("----");
print(emp);
System.out.println("----");
42. Program print(emp);
System.out.println("----");
package pack1; }
class Job private static void print(Employee
{
String title; emp)
double salary; {
Job(String title, double salary) Job j1 = null;
{ while(emp.hasNext())
this.title = title; {
this.salary = salary; j1 = emp.nextJob();
} System.out.println(j1.title +
} ":" + j1.salary);
}
class Employee }
{ }
private int index;
Job x[];
Employee(Job x[])
{ class Job
return x[index ++]; {
} String title;
boolean hasNext() double salary;
{ Job(String title, double salary)
return index < x.length; {
} this.title = title;
} this.salary = salary;
class Manager }
{ }
public static void main(String[] args) class Employee
{ {
Job j1 = new Job("software", Job x[];
Employee(Job x[])
50000.9);
{
Job j2 = new Job("hardware",
this.x = x;
10000.9); }
Job j3 = new Job("hr", 500.9); Iterator getIterator()
Job x[] = {j1, j2, j3}; {
Employee emp = new Employee(x); Iterator it = new Iterator(x);
print(emp); return it;
System.out.println("----"); }
print(emp); class Iterator
System.out.println("----"); {
print(emp); private int index;
System.out.println("----"); Job x[];
} Iterator(Job x[])
private static void print(Employee {
this.x = x;
emp)
}
{
Job nextJob()
Iterator it = emp.getIterator();
{
Job j1 = null;
return x[index ++];
while(it.hasNext())
}
{
boolean hasNext()
j1 = it.nextJob();
{
System.out.println(j1.title +
return index <
":" + j1.salary);
x.length;
}
}
}
}
}
}//end of employee
class Manager
{
46. Program public static void main(String[] args)
{
package pack5;
test2(); Given:
}
10. class Line {
}
11. public static class Point { }
12. }
51. Program
13.
class D 14. class Triangle {
{
15. // insert code here
class E
{ 16. }
int i;
Which code, inserted at line 15, creates an
void test1()
{ instance of the Point class
defined in Line?
}
//static int j; A. Point p = new Point();
B. Line.Point p = new Line.Point();
/*
static void test2() C. The Point class cannot be instatiated at
{
line 15.
}
*/ D. Line 1 = new Line() ; 1.Point p = new
}
1.Point();
}
Answer: B
52. Program
54. Program
class E
{
class F
static class F
{
{
int i;
int i;
static int j;
static int j;
void test1()
void test1()
{
{
i = 1;
}
j = 2;
static void test2()
test1();
{
test2();
}
G g1 = new G();
}
H h1 = new H();
}
}
static void test2()
53. Program {
//i = 1; p = 0;
j = 2; q = 9;
//test1(); //G g1 = new G();
test2(); H h1 = new H();
//G g1 = new G(); }
H h1 = new H(); static void test6()
} {
class G //i = 10;
{ j = 20;
int m; //test1();
//static int n; test2();
void test3() //p = 9;
{ q = 10;
i = 10; //G g1 = new G();
j = 20; H h1 = new H();
test1(); }
test2(); }
G g1 = new G(); }
H h1 = new H();
m = 20;
//n = 20; 55. Program
}
/*
class G
static void test4()
{
{
class H
i = 10;
{
j = 20;
}
test1();
static class I
test2();
{
G g1 = new G();
H h1 = new H();
}
m = 20;
public static void main(String[] args)
n = 20;
{
}
H h1 = null;
*/
I i1 = null;
}
//h1 = new H();
static class H
i1 = new I();
{
System.out.println("done");
int p;
}
static int q;
}
void test5()
{
//i = 1;
j = 2; 56. Program
//test1(); Given:
test2();
10. class Line {
{ System.out.println("1111");
class A System.out.println("1111");
{ System.out.println("1111");
int i = 10; System.out.println("----");
} System.out.println("1111");
A a1 = new A(); System.out.println("1111");
System.out.println(a1.i); System.out.println("1111");
a1.i = 20; }
System.out.println(a1.i); }
}
} 62. Program
class L
60. Program {
static void test()
class J {
{ System.out.println("1111");
static void test() System.out.println("1111");
{ System.out.println("1111");
class B }
{ public static void main(String[] args)
int i = 20; {
} test();
B b1 = new B(); System.out.println("----");
System.out.println("b1.i:" + b1.i); test();
b1.i = 30; System.out.println("----");
System.out.println("b1.i:" + b1.i); test();
} }
public static void main(String[] args) }
{
test();
System.out.println("Hello World!"); 63. Program
}
}
class M
{
public static void main(String[] args)
61. Program {
class A
class K {
{ void test()
public static void main(String[] args) {
{ System.out.println("1111");
System.out.println("1111"); System.out.println("1111");
System.out.println("1111"); System.out.println("1111");
System.out.println("1111"); }
System.out.println("----"); }
A a1 = new A();
a1.test();
System.out.println("----"); 65. Program
a1.test();
System.out.println("----");
class A
a1.test();
{
}
A()
}
{
System.out.println(10);
64. Program }
public static void main(String[] args)
class N {
{ class B
{ {
class A B()
{ {
} super();
} System.out.println("B()");
}
N() }
{ B b1=new B();
class A }
{ }
}
}
66. Program
static
{ public class B
class A {
{ public static int i=10;
} public static void main(String[] args)
} {
void test() private class C
{ {
class A private C()
{ {
} System.out.println("local");
} }
public static void main(String[] args) }
{ C c1=new C();
class A System.out.println("main");
{ }
} }
System.out.println("Hello World!");
}
} 67. Program
69. Program
72. Program
class P
{ interface C
public static void main(String[] args) {
{ void test1();
void test2(); {
} A a1 = new A();
A a2 = new A()
{
73. Program void test2()
{
System.out.println("AIC-
class Manager1
{ test2");
public static void main(String[] args) }
{ };
A a1 = new A(); a1.test1();
a1.test1(); a1.test2();
a1.test2(); a2.test1();
System.out.println("done"); a2.test2();
} System.out.println("done");
} }
}
74. Program
76. Program
class Manager2
{ class Manager4
public static void main(String[] args) {
{ public static void main(String[] args)
A a1 = new A() {
{ A a1 = new A()
void test1() {
{ };
System.out.println("AIC- a1.test1();
a1.test2();
test1");
System.out.println("------");
}
A a2 = new A()
};
{
a1.test1();
void test1()
a1.test2();
{
System.out.println("Hello World!");
System.out.println("AIC-
}
} test1");
}
};
a2.test1();
75. Program a2.test2();
System.out.println("------");
class Manager3 A a3 = new A()
{ {
public static void main(String[] args) void test2()
{ {
System.out.println("AIC- B b1 = new B()
{
test2");
void test1()
}
{
};
System.out.println("AIC-
a3.test1();
a3.test2(); test1");
System.out.println("------"); }
A a4 = new A() };
{ b1.test1();
void test1() b1.test2();
{ System.out.println("done");
System.out.println("AIC- }
}
test1");
}
void test2()
{ 79. Program
System.out.println("AIC-
class Manager7
test2");
{
}
public static void main(String[] args)
};
{
a4.test1();
B b1 = new B()
a4.test2();
{
}
void test1()
}
{
System.out.println("AIC-
77. Program
test1");
}
class Manager5
void test2()
{
{
public static void main(String[] args)
System.out.println("AIC-
{
B b1 = new B(); test2");
b1.test1(); }
b1.test2(); };
System.out.println("done"); b1.test1();
} b1.test2();
} System.out.println("done");
}
78. Program }
class Manager6
{ 80. Program
public static void main(String[] args) Click the Exhibit button.
{ c1.test2();
System.out.println("AIC- }
public static void main(String[] args)
test2");
{
}
B b1 = null;
};
method1(b1);
c1.test1();
C c1 = null;
c1.test2();
method2(c1);
System.out.println("done");
System.out.println("done");
}
}
}
}
83. Program
85. Program
class Manager10
class Manager12
{
{
static void method1(B b1)
static void method1(B b1)
{
{
b1.test1();
b1.test1();
b1.test2();
b1.test2();
}
}
static void method2(C c1)
static void method2(C c1)
{
{
c1.test1();
c1.test1();
c1.test2();
c1.test2();
}
}
public static void main(String[] args)
public static void main(String[] args)
{
{
System.out.println("done");
B b1 = new B()
}
{
}
void test1()
{
System.out.println("AIC-
84. Program
test1");
}
class Manager11
};
{
method1(b1);
static void method1(B b1)
C c1 = new C()
{
{
b1.test1();
public void test1()
b1.test2();
{
}
System.out.println("AIC-
static void method2(C c1)
{ test1");
c1.test1(); }
}
D(int i)
Answer: A
{
System.out.println("D(int)");
}
90. Program
{
Given: System.out.println("D-IIB");
}
10. interface Foo { int bar(); } }
11. public class Sprite { class Manager15
{
12. public int fubar( Foo foo) { return public static void main(String[] args)
foo.bar(); } {
D d1 = new D()
13. public void testFoo() { {
14. fubar( {
System.out.println("AIC-
15. // insert code here
IIB");
16.); }
17. } };
System.out.println("-----");
18. } D d2 = new D(10)
Which code, inserted at line 15, allows the {
{
class Sprite to compile? System.out.println("AIC-
A. Foo { public int bar() { return 1; } } IIB");
B. new Foo { public int bar() { return 1; } } }
};
C. newFoo() { public int bar(){return 1; } } }
D. new class Foo { public int bar() { return }
1; } }
92. Program
Answer: C
class A
{
void test1()
91. Program
{
System.out.println("A-
class D
{ test1");
D() }
{ }
System.out.println("D()");
94. Program
@SuppressWarnings(value="deprecation")
class Manager3
class C {
{ public static void main(String[] args)
@Deprecated {
void test1() Thread t1 = new Thread();
{ t1.stop();
//some content C c1 = new C();
} c1.test1();
void test2() System.out.println("Hello World!");
{ }
//again some content }
}
}
98. Program
import java.util.ArrayList;
95. Program
class Manager4
{
class Manager1
{
public static void main(String[] args) @SuppressWarnings(value="unchecked")
{ public static void main(String[] args)
C c1 = new C(); {
c1.test1(); ArrayList list = new
c1.test2(); ArrayList();
108. Program }
package com.lara; }
public class M8
{ 111. Program
public static void main(String[] args) class W8
{ {
String s1="10"; static void test(Byte b,Byte b)
int i=Integer.parseInt(s1); {
double d=Double.parseDouble(s1); System.out.println(“Byte”);
float f1=Float.parseFloat(s1); }
byte b1=Byte.parseByte(s1); static void test(byte … b)
System.out.println("done"); {
} System.out.println(“byte”);
} }
public static void main (String args[])
109. Progrm {
package com.lara; byte b=10;
public class M9 test(b,b);
{ }
public static void main(String[] args) }
{
int i=10; 112. Program
double d=10.09; package com.lara;
byte b=10; public class M10
String s1=Integer.toString(i); {
String s2=Double.toString(d); public static void main(String[] args)
String s3=Byte.toString(b); {
System.out.println("done"); int i=10;
} String s1="10";
} Integer obj1=Integer.valueOf(s1);
Integer obj2=Integer.valueOf(i);
110. Program System.out.println("done");
class W7 }
{ }
static void test(Byte b,Byte i)
{ 113. Program
System.out.println(“Byte”); package com.lara;
} public class M11
static void test(byte … b) {
{ public static void main(String[] args)
System.out.println(“byte”); {
} double d1=10.09;
public static void main (String args[]) String s1="20.90";
{ Double d2=Double.valueOf(d1);
byte b=10; Double d3=Double.valueOf(s1);
test(b,b); System.out.println("done");
//Jdk1.4 }
package com.lara; }
public class M18 126. Program
{ //JDK1.6
public static void main(String[] args) package com.lara;
{ public class M21
int i=test(); {
} public static void main(String[] args)
static Integer test() {
{ Boolean flag=true;
return 20; if(flag)
} {
} System.out.println(flag);
124. Program }
//JDK1.4 else
package com.lara; {
public class M19 System.out.println(flag);
{ }
public static void main(String[] args) }
{ }
int i=test().intvalue();
} 127. Program
static Integer test() //JDK1.6
{ package com.lara;
return new Integer(20); public class M21
} {
} public static void main(String[] args)
{
125. Program Boolean flag=true;
//jdk1.6 if(flag)
package com.lara; {
public class M20
{
System.out.println(flag);
public static void main(String[] args)
}
{
else
Boolean flag=true;
{
if(flag)
{
System.out.println(flag);
}
System.out.println(flag);
}
}
}
else
128. Program
{
//Jdk1.4
System.out.println(flag);
package com.lara;
}
D. Compilation fails {
System.out.println("Integer");
E. An exception is thrown at runtime.
}
Ans : B
static void test(Double b)
{
133. Program
package com.lara; System.out.println("Double");
public class M27 }
{
static void test(byte b) static void test(Number b)
{ {
System.out.println("byte");
}
System.out.println("Number");
static void test(short b) }
{
System.out.println("short"); static void test(Object b)
} {
System.out.println("Object");
static void test(int b) }
{
System.out.println("int"); static void test(byte ... b)
} {
System.out.println("byte ...");
static void test(long b) }
{
System.out.println("long"); public static void main(String[] args)
} {
byte b = 10;
static void test(float b) test(b);
{ }
System.out.println("float"); }
}
{ 5.
System.out.println(x.length);
6. public String doit(int... vals) {
for(int i : x)
{ 7. return “b”;
System.out.println(i);
8. }
}
System.out.println("-----"); 9. }
}
Given:
public static void main(String[] args)
{ 25. A a=new A();
test();
26. System.out.println(a.doit(4, 5));
test(20, 30);
test(90, 9, 20, 40); What is the result?
test(80, 2, 34, 450, 1000);
A. Line 26 prints “a” to System.out.
}
} B. Line 26 prints ‘b” to System.out.
138. Program
C. An exception is thrown at line 26 at
package com.lara;
public class M30 runtime.
{
D. Compilation of class A will fail due to an
static void test(int ... x)
{ error in line 6.
System.out.println(x.length);
Answer: A
for(int i : x)
{
System.out.println(i); 140. Program
} package com.lara;
System.out.println("-----"); public class M31
} {
public static void main(String[] args) static void test(String ... strings)
{ {
test(); for(String str : strings)
test(20, 30); {
test(90, 9, 20, 40); System.out.print(str + ",");
test(80, 2, 34, 450, 1000); }
} System.out.println();
} }
139. Program public static void main(String[] args)
{
test("msg1");
Click the Exhibit button. test("msg1", "msg2");
1. public class A { test("msg1", "msg2", "msg3");
test("msg1", "msg2", "msg3", "msg4");
2. public String doit(int x, int y) { }
3. return “a”; }
141. Program
4. }
Given
12. public class Barn{
142. Program
13. public static void main (String args[]){
Given: 14. new Barn().go(“hi”,1);
10. public class Bar { 15. new Barn().go(“hi”,”world”,2);
11.static void foo(int...x) { 16.}
12. // insert code here 17. public void go(String…y,int x){
13. } 18.System.out.println(y[y.length-1]+””);
14. } 19.}
Which two code fragments, inserted 20.}
independently at line 12, will allow What is the result?
the class to compile? (Choose two.) A. hi hi
A. foreach(x) System.out.println(z); B. hi world
148. Program
146. Program
class A
package com.lara;
{
public class M34
public static void main(String[] args)
{
{
public static void main(String ... x)
System.out.println("Hello
{
System.out.println("Hello to all"); World!");
} }
} }
149. Program
147. Program class B
{
Given
public static void main(String[] args)
11. class Mud{ {
System.out.println("Hello " +
12. //insert code here
args[0]);
13. System.out.println(“hi”);
}
14.} }
150. Program
15.}
class C
And the following five fragments {
public static void main(String[] args)
A.public static void main(String…a)
{ System.out.println("enter
System.out.println(args.length);
string value");
}
String s1 = sc.next();
}
System.out.println("enter int
value");
151. Program int i = sc.nextInt();
class D System.out.println("enter
{
double value");
public static void main(String[] args)
double j = sc.nextDouble();
{
System.out.println("enter boolean
for(String s1 : args)
{ value");
System.out.println(s1); boolean b = sc.nextBoolean();
} System.out.println("you have
}
entered");
}
System.out.println(s1);
152. Program
System.out.println(i);
package com.lara;
System.out.println(j);
import java.util.Scanner;
System.out.println(b);
public class Manager
System.out.println("done");
{
}
public static void main(String[] args)
}
{
Scanner sc = new
154. Program
Scanner(System.in);
Given:
System.out.println("enter
12. String csv = “Sue,5,true,3”;
some thing");
String s1 = sc.next(); 13. Scanner scanner = new Scanner( csv);
System.out.println("you have
14. scanner.useDelimiter(”,”);
enterd:" + s1);
15. int age = scanner.nextInt();
}
} What is the result?
153. Program
A. Compilation fails.
package com.lara;
import java.util.Scanner; B. After line 15, the value of age is 5.
public class Manager1
C. After line 15, the value of age is 3.
{
public static void main(String[] args) D. An exception is thrown at runtime.
{
Scanner sc =
new Answer: D
Scanner(System.in);
155. Program
21. }
Given the command line java Pass2 and: and two separate command line
15. public class Pass2 { invocations:
16. public void main(String [] args) { java Yippee
17.int x=6; java Yippee 1 2 3 4
18. Pass2 p = new Pass2(); What is the result?
19. p.doStuff(x); A. No output is produced.
20. System.out.print(” main x = “+ x); 123
21. } B. No output is produced.
22. 234
23. void doStuff(int x) { C. No output is produced.
24. System.out.print(” doStuffx = “+ x++); 1234
25. } D. An exception is thrown at runtime.
26. } 123
What is the result? E. An exception is thrown at runtime.
A. Compilation fails. 234
B. An exception is thrown at runtime. F. An exception is thrown at rijntime.
C. doStuffx = 6 main x = 6 1234
D. doStuffx = 6 main x = 7
E. doStuffx = 7 main x = 6 Answer: B
F. doStuffx = 7 main x = 7
157. Program
Answer: B Given:
12. public class Yippee2 {
156. Program
13.
Given:
14. static public void main(String [] yahoo)
15. public class Yippee {
{
16. public static void main(String [] args) {
15. for(int x= 1; x<yahoo.length; x++) {
17. for(int x = 1; x < args.length; x++) {
16. System.out.print(yahoo[x] + “ “);
18. System.out.print(args[x] +“ “);
17. }
19. }
18. }
20. }
19. } E.
and the command line invocation: System.getProperties().getProperty(”prop.c
java Yippee2 a b c ustom”);
What is the result?
A.a b Answer: DE
B.b c
C.a b c 159. Program
D. Compilation fails. package com.lara;
package com.lara; {
public class C System.out.println(5);
{ test1();
public static void main(String[] args) System.out.println(6);
{ }
System.out.println(1); }
test1();
System.out.println(2);
} 163. Program
static void test1()
{
package com.lara;
System.out.println(3);
public class E
test2();
{
System.out.println(4);
static void print()
}
{
static void test2()
System.out.println(1);
{
int i = 10/0;
System.out.println(5);
System.out.println(2);
String s1 = null;
}
System.out.println(s1.length()); }
System.out.println(6);
}
}
164. Program
main(args); 6. System.out.println(x);
System.out.println(2);
7. }
}
} 8. public static void main(String[] args) {
9. new Boxer1(new Integer(4));
166. Program 10. }
Given: 11. }
10. public class ClassA { What is the result?
11. public void count(int i) { A. The value “4” is printed at the command
12. count(++i); line.
13. } B. Compilation fails because of an error in
14. } line 5.
And: C. Compilation fails because of an error in
20. ClassA a = new ClassA(); line 9.
21. a.count(3); D. A NullPointerException occurs at
Which exception or error should be thrown runtime.
by the virtual machine? E. A NumberFormatException occurs at
A. StackOverflowError runtime.
B. NullPointerException F. An IllegalStateException occurs at
C. NumberFormatException runtime.
D. IllegalArgumentException Answer: D
E. ExceptionlnlnitializerError
Question 118
Answer: A
168. Program
package com.lara;
167. Program
public class H
Given: {
public static void main(String[] args)
1. public class Boxer1 {
{
2. Integer i; System.out.println(1);
test();
3. int x;
System.out.println(2);
4. public Boxer1(int y) { }
static void test()
5. x=i+y;
{
} }
} catch(ArithmeticException
ex)
{
172. Program System.out.println(i);
}
package com.lara; System.out.println(i);
public class M }
{ }
public static void main(String[] args)
{
try 174. Program
{
int i = 10;
package com.lara;
System.out.println(i);
public class O
}
{
catch(ArithmeticException
public static void main(String[] args)
ex) {
{ System.out.println(1);
int i = 10/0;
try
System.out.println(ex);
{
//System.out.println(i)
System.out.println(2);
; }
int j = 20; catch(ArithmeticException
System.out.println(j);
ex)
}
{
//System.out.println(i);
System.out.println(3);
//System.out.println(j);
}
}
System.out.println(4);
}
}
}
173. Program
175. Program
package com.lara;
package com.lara;
public class N
public class P
{
{
public static void main(String[] args)
public static void main(String[] args)
{
{
int i = 0;
System.out.println(1);
try
try
{
{
System.out.println(i);
System.out.println(2);
int i = 10/0; }
System.out.println(3); System.out.println(9);
} }
catch(ArithmeticException }
ex)
{
System.out.println(4); 177. Program
int j = 20/0;
System.out.println(5); package com.lara;
} import java.util.Scanner;
System.out.println(6); public class R
} {
} public static void main(String[] args)
{
Scanner sc = new
176. Program Scanner(System.in);
System.out.println("enter some
package com.lara;
thing");
public class Q
String s1 = sc.next();
{
try
public static void main(String[] args)
{
{
System.out.println(1);
System.out.println(1);
int i = Integer.parseInt(s1);
try
System.out.println(2);
{
int k = i / (i - 9);
System.out.println(2);
System.out.println(3);
int i = 10/0;
}
System.out.println(3);
}
catch(ArithmeticException catch(NumberFormatException ex)
{
ex)
System.out.println(4);
{
System.out.println(4);
try System.out.println(ex);
{ System.out.println(5);
System.out.println(5); }
int i = 20/0; System.out.println(6);
System.out.println(6); }
} }
catch(ArithmeticException ex1)
{
System.out.println(7); 178. Program
}
System.out.println(8); package com.lara;
System.out.println("enter some }
finally
thing");
{
String s1 = sc.next();
System.out.println(3);
int i = test(s1);
}
System.out.println(i);
System.out.println(4);
}
}
static int test(String s1)
}
{
int i = 0;
try
{ 182. Program
i= Given:
Integer.parseInt(s1); 31. // some code here
return i;
} 32. try {
33. // some code here
catch(NumberFormatException ex) 34. } catch (SomeException se) {
{
return 0; 35. // some code here
} 36. } finally {
finally
{ 37. // some code here
return 1000; 38. }
}
} Under which three circumstances will the
} code on line 37 be executed?
(Choose three.)
181. Program A. The instance gets garbage collected.
B. The code on line 33 throws an exception.
package com.lara;
public class V C. The code on line 35 throws an exception.
{ D. The code on line 31 throws an exception.
public static void main(String[] args)
{ E. The code on line 33 executes
try successfully.
{
System.out.println(1);
return; Answer: BCE
}
catch(ArithmeticException
ex) 183. Program
{
System.out.println(2); package com.lara;
ex) Given:
{
Answer: D // {
// return 20;
// }
189. Program // }
int test6(boolean flag)
{
package com.lara;
if(flag)
public class A
{
{
int test(boolean flag)
}
{
else
return 1000;
{
}
return 20;
int test1(boolean flag)
}
{
return 0;
if(flag)
}
{
return 10;
// int test7(boolean flag)
}
// {
else
// if(flag)
{
// {
return 20;
// return 200;
}
// }
}
// else
// int test3(boolean flag)
// {
// {
// return 20;
// if(flag)
// }
// {
// return 0;
// return 10;
// }
// }
// }
}
int test4(boolean flag)
{
if(flag)
{ 190. Program
return 10;
} package com.lara;
return 20; public class B
} {
int test(String s1)
// int test5(boolean flag) {
// { try
// if(flag) {
// { //some stmts
// }
// }
// else
catch(NumberFormatException ex)
{ }
return 500;
} }
return 20; // int test4(String s1)
} // {
// try
int test1(String s1) // {
{ // //some stmts
try // return 20;
{ // }
//some stmts //
return 1;
catch(NumberFormatException ex)
}
// {
//
catch(NumberFormatException ex) // }
{ // }
return 0;
} int test5(String s1)
} {
try
// int test2(String s1) {
// { //some stmts
// try return 20;
// { }
// //some stmts
catch(NumberFormatException ex)
// }
{
//
catch(NumberFormatException ex) }
// { return 30;
// return 0; }
// }
// } // int test6(String s1)
// {
int test3(String s1) // try
{ // {
try // //some stmts
{ // return 20;
//some stmts // }
//
}
catch(NumberFormatException ex)
// {
catch(NumberFormatException ex) // return 0;
{ // }
return 0; // return 30;
// } }
// int test3()
} // {
// try
191. Program // {
// //some stmts
package com.lara;
// }
public class C
// catch(NullPointerException
{
int test1() ex)
{ // {
try // return 20;
{ // }
//some stmts // catch(ArithmeticException
}
ex)
catch(NullPointerException
// {
ex) // return 30;
{ // }
// }
} int test4()
catch(ArithmeticException {
try
ex)
{
{
//some stmts
}
}
return 10;
catch(NullPointerException
}
int test2() ex)
{ {
try return 20;
{ }
//some stmts catch(ArithmeticException
return 10;
ex)
}
{
catch(NullPointerException
return 30;
ex) }
{ return 300;
return 20; }
}
catch(ArithmeticException // int test5()
// {
ex)
// try
{
// {
return 30;
}
} // return 20;
finally // }
{ // catch(ArithmeticException
return 30;
ex)
}
// {
}
// return 30;
// int test3()
// }
// {
// finally
// try
// {
// {
//
// //some stmts
// }
// }
// return 40;
// catch(ArithmeticException
// }
ex) // int test6()
// { // {
// // try
// } // {
// finally // //some stmts
// { // return 20;
// return 30; // }
// } // catch(ArithmeticException
// return 40;
ex)
// }
// {
int test4()
// return 30;
{
// }
try
// finally
{
// {
//some stmts
// return 40;
}
// }
catch(ArithmeticException
// return 50;
ex) // }
{ }
return 40;
}
finally 193. Program
{
package com.lara;
}
import java.io.FileWriter;
return 40;
import java.io.IOException;
}
import java.lang.instrument.ClassDefinition;
// int test5()
import java.sql.DriverManager;
// {
import java.sql.SQLException;
// try
import java.text.DateFormat;
// {
import java.text.ParseException;
// //some stmts
public class E
{ {
void test1() System.out.println(1);
{ String s1 = null;
System.out.println(1); s1.length();
int i = 10/0; System.out.println(2);
System.out.println(2); }
} void test6()
void test2() {
{ System.out.println(1);
System.out.println(1); try
try {
{
}
} catch(NullPointerException
catch(ArithmeticException
ex)
ex) {
{
}
} System.out.println(2);
System.out.println(2); }
} void test7()
void test3() {
{ Object obj = new Object();
System.out.println(1); E e1 = (E) obj;
int i = }
Integer.parseInt("abc");
void test8()
System.out.println(2);
{
}
test8();
void test4()
}
{
System.out.println(1);
void test9()
try
{
{
try
{
}
}
catch(NumberFormatException ex) catch(StackOverflowError
{
ex)
{
System.out.println(ex);
} }
System.out.println(2); }
}
void test5() void test10()
{ // {
int x[] = new int[999999999]; // ex.printStackTrace();
} // }
// }
void test11()
{
try // void test15()
{ // {
// try
} // {
catch(NoClassDefFoundError //
// }
ex)
//
{
catch(ClassNotFoundException ex)
// {
System.out.println(ex);
//
}
// }
}
// }
// void test12()
// void test16()
// {
// {
// Class.forName("");
// try
// }
// {
//
void test13()
{ System.out.println("done");
try // }
{ //
Class.forName("");
catch(ClassNotFoundException ex)
}
// {
//
catch(ClassNotFoundException ex) // }
{ // }
ex.printStackTrace();
} void test17()
} {
try
// void test14() {
// { Class.forName("");
// try Class.forName("");
// { Class.forName("");
// Class.forName(""); Class.forName("");
// } }
// catch(NullPointerException
ex) catch(ClassNotFoundException ex)
{ // }
// }
}
} void test21()
{
// void test18() try
// { {
// Class.forName("");
DriverManager.getConnection("");
// } DriverManager.getConnection("");
}
void test18() catch(SQLException ex)
{ {
try
{ }
// void test20() }
// { }
// try
// { // void test24()
// Class.forName(""); // {
// } // try
// catch(SQLException ex) // {
// { //
// // }
// catch(IOException ex)
// { void test29()
// {
// } try
// } {
Thread.sleep(1000);
// void test25() }
// { catch(InterruptedException
// clone();
ex)
// }
{
//
}
void test26()
}
{
try
// void test30()
{
// {
clone();
// try
}
// {
//
catch(CloneNotSupportedException // }
// catch(InterruptedException
ex)
{ ex)
// {
} //
} // }
// }
// void test27
// { // void test31()
// try // {
// { // DateFormat df =
// //
// }
DateFormat.getDateInstance();
//
// df.parse("");
catch(CloneNotSupportedException // }
ex)
void test32()
// {
{
// ex.printStackTrace();
DateFormat df =
// }
// }
DateFormat.getDateInstance();
// void test28 try
// { {
// Thread.sleep(1000); df.parse("");
// } }
test();
}
catch(ArithmeticException
ex)
199. Program
{
package com.lara;
public class K
System.out.println("exception"); {
} public static void main(String[] args)
System.out.println(2); {
} System.out.println(1);
static void test() try
{ {
System.out.println(3); Class.forName("");
int i = 10/0; }
System.out.println(4);
}
catch(ClassNotFoundException ex)
}
{
198. Program
package com.lara; System.out.println(ex);
public class I }
{ System.out.println(2);
public static void main(String[] args) }
{ }
System.out.println(1);
try 200. Program
{ package com.lara;
test(); public class K
} {
catch(ArithmeticException public static void main(String[] args)
{
ex) System.out.println(1);
{ try
{
System.out.println("exception"); Class.forName("");
} }
System.out.println(2);
} catch(ClassNotFoundException ex)
static void test() {
{
System.out.println(3);
int i = 10/0; System.out.println(ex);
System.out.println(4); }
} System.out.println(2);
} }
}
System.out.println("ex");
201. Program
}
package com.lara;
System.out.println(2);
public class L
}
{
static void test() throws
public static void main(String[] args)
{ ClassNotFoundException
System.out.println(1); {
test(); System.out.println(3);
System.out.println(2); Class.forName("");
} System.out.println(4);
static void test() }
{ }
System.out.println(3);
try
{
Class.forName(""); 203. Program
} package com.lara;
class W
{
catch(ClassNotFoundException ex)
void test() throws
{
ClassNotFoundException
{
System.out.println(ex);
System.out.println(1);
}
Class.forName("");
System.out.println(4);
System.out.println(2);
}
}
}
}
public class N
202. Program
{
package com.lara;
public static void main(String[] args)
public class M
{
{
System.out.println(3);
public static void main(String[] args)
W w1 = new W();
{
try
System.out.println(1);
{
try
System.out.println(4);
{
w1.test();
test();
System.out.println(5);
}
}
catch(ClassNotFoundException ex)
catch(ClassNotFoundException ex)
{
{
System.out.println(6);
}
System.out.println(7);
catch(ClassNotFoundException ex)
}
{
}
}
204. Program
}
package com.lara;
}
public class O
{
206. Program
public static void main(String[] args)
package com.lara;
{
import java.io.IOException;
// try
import java.sql.SQLException;
// {
public class Q
//
{
// }
static void test1() throws
//
SQLException
catch(ClassNotFoundException ex)
{
// {
//
}
// }
static void test2() throws
}
void test() throws IOException
{
ClassNotFoundException
{
}
}
static void test3() throws
}
NullPointerException
205. Program {
package com.lara;
public class P }
{
static void test() throws static void test4()
{
ClassNotFoundException
try
{
{
test1();
}
}
catch(SQLException ex)
public static void main(String[] args)
{
{
try
}
{
try
test();
{
}
test2();
} catch(Exception ex)
catch(IOException ex) {
{
}
} }
test3(); }
}
static void test5() 207. Program
{
Given:
try
{ 11.classA {
test1();
12. public void process()
test2();
} { System.out.print(”A,”); } }
catch(IOException ex)
13. class B extends A {
{
14. public void process() throws
}
IOException {
catch(SQLException ex)
{ 15. super.process();
16. System.out.print(”B,”);
}
} 17. throw new IOException();
static void test6()
18. } }
throws SQLException, IOException
{ 19. public static void main(String[] args) {
test1();
20. try { new B().process(); }
test2();
} 21. catch (IOException e)
static void test7() throws Exception
{ System.out.println(”Exception”); } }
{
test1(); What is the result?
test2();
A. Exception
}
void test8() throws Throwable B. A,B,Exception
{
C. Compilation fails because of an error in
test2();
test1(); line 20.
}
D. Compilation fails because of an error in
void test9()
{ line 14.
try
E. A NullPointerException is thrown at
{
test2(); runtime.
test1();
}
Answer: D Given:
11. static classA {
208. Program 12. void process() throws Exception { throw
Given: new Exception(); }
11.classA { 13. }
12. public void process() 14. static class B extends A {
{ System.out.print(”A “); } } 15. void process() { System.out.println(”B
13. class B extends A { “); }
14. public void process() throws 16. }
RuntimeException { 17. public static void main(String[] args) {
15. super.process(); 18.A a=new B();
16. if (true) throw new RuntimeException(); 19. a.process();
17. System.out.print(“B”); }} 20.}
18. public static void main(String[] args) { What is the result?
19. try { ((A)new B()).process(); } A. B
20. catch (Exception e) B. The code runs with no output.
{ System.out.print(”Exception “); } C. An exception is thrown at runtime.
21. } D. Compilation fails because of an error in
What is the result? line 15.
A. Exception E. Compilation fails because of an error in
B. A Exception line 18.
C. A Exception B F. Compilation fails because of an error in
D. A B Exception line 19.
E. Compilation fails because of an error in
line 14. Answer: F
F. Compilation fails because of an error in
line 19. 210. Program
Given:
Answer: B 11. static class A {
12. void process() throws Exception { throw
209. Program new Exception(); }
13. } 91. }
14. static class B extends A { 92. return info;
15. void process() Which is true if a ResourceException is
{ System.out.println(”B”); } thrown on line 86?
16. } A. Line 92 will not execute.
17. public static void main(String[] args) { B. The connection will not be retrieved in
18. new B().process(); line 85.
19. } C. The resource connection will not be
What is the result? closed on line 88.
A. B D. The enclosing method will throw an
B. The code runs with no output. exception to its caller.
C. Compilation fails because of an error in
line 12. Answer: C
D. Compilation fails because of an error in
line 15. 212. Program
E. Compilation fails because of an error in Click the Exhibit button.
line 18. 1. public class A {
2. public void method1() {
Answer: A 3. B b=new B();
4. b.method2();
211. Program 5. // more code here
Given: 6. }
84. try { 7. }
85. ResourceConnection con = 1. public class B {
resourceFactory.getConnection(); 2. public void method2() {
86. Results r = con.query(”GET INFO 3.C c=new C();
FROM CUSTOMER”); 4. c.method3();
87. info = r.getData(); 5. // more code here
88. con.close(); 6. }
89. } catch (ResourceException re) { 7. }
90. errorLog.write(re.getMessage()); 1. public class C {
}
Answer: B S(int i) throws SQLException
{
this();
215. Program }
Given:
void test1()
11. public static void main(String[] args) { {
S s1 = null;
12. try {
try
13. args=null; {
s1 = new S();
14. args[0] = “test”;
s1 = new S(20);
15. System.out.println(args[0]); }
catch(SQLException ex) }
{
ex.printStackTrace(); 217. Program
} package com.lara;
} class V
{
void test2() throws SQLException V() throws
{
CloneNotSupportedException
S s1 = new S();
{
s1 = new S(90);
}
}
}
void test3() throws Exception
public class T extends V
{
{
S s1 = new S();
T() throws
S s2 = new S(89);
} CloneNotSupportedException
{
void test4() throws Throwable
{ }
S s1 = new S(9); }
s1 = new S();
}
void test5()
{
S s1 = null; 218. Program
try
Click the Exhibit button.
{
s1 = new S(); SomeException:
s1 = new S(20);
1. public class SomeException {
}
catch(Exception ex) 2. }
{
Class A:
} 1. public class A {
try
2. public void doSomething() { }
{
s1 = new S(9); 3. }
s1 = new S();
Class B:
}
catch(Throwable t) 1. public class B extends A {
{
2. public void doSomething() throws
} SomeException { }
}
3. }
2. Given:
3. public String sayHello(String name) 11. static void test() {
throws TestException { 12. try {
4. 13. String x=null;
5. if(name == null) { 14. System.out.print(x.toString() +“ “);
6. throw new TestException(); 15. }
7. } 16. finally { System.out.print(“finally “); }
8. 17. }
9. return “Hello “+ name; 18. public static void main(String[] args) {
10. } 19. try { test(); }
11. 20. catch (Exception ex)
12. } { System.out.print(”exception “); }
A programmer wants to use this code in an 21. }
application: What is the result?
{ //
super(msg); // }
}
} // void test1() throws Exception
// {
224. Program //
package com.lara; // }
import java.io.FileNotFoundException;
import java.io.IOException; // void test1() throws Throwable
import java.sql.SQLException; // {
//
class A // }
{
void test1() throws SQLException // void test1() throws
{
ClassNotFoundException
// {
}
//
void test2() throws
// }
FileNotFoundException
{ // void test1() throws
NumberFormatException
}
// {
void test3() throws IOException
//
{
// }
}
// void test2()
void test4() throws
// {
NullPointerException //
{ // }
package com.lara; // }
import java.io.FileNotFoundException;
import java.io.IOException; // void test1() throws Throwable
import java.sql.SQLException; // {
//
class A // }
{
void test1() throws SQLException // void test1() throws
{
ClassNotFoundException
// {
}
//
void test2() throws
// }
FileNotFoundException
{ // void test1() throws
NumberFormatException
}
// {
void test3() throws IOException
//
{
// }
}
// void test2()
void test4() throws
// {
NullPointerException //
{ // }
{ 240. Program
System.out.println(1); package pack1;
assert false : 1000; class J
System.out.println(2); {
} public static void main(String[] args)
} {
237. Program System.out.println("Hello
package pack1;
World!");
class G
int assert = 10;
{
System.out.println(assert);
public static void main(String[] args)
}
{
}
System.out.println(1);
241. Program
assert false : false;
package pack1;
System.out.println(2);
class D
}
{
}
void test1()
238. Program
{
package pack1;
System.out.println("test1-
class H
{ begin");
public static void main(String[] args) assert false;
{ System.out.println("test1-
System.out.println(1);
end");
assert false : test();
}
System.out.println(2);
}
}
class Manager
static int test()
{
{
public static void main(String[] args)
return 100;
{
} }
System.out.println("main
239. Program
package pack1; begin");
class I assert false;
{ D d1 = new D();
public static void main(String[] args) d1.test1();
{ System.out.println("main
System.out.println(1);
end");
assert test();
}
System.out.println(2);
}
}
242. Program
static boolean test()
{ Given:
return false;
8. public class test {
}
} 9. public static void main(String [] a) {
package com.lara; }
class B extends Thread
{ }
public void run() }
{
for(int i = 0; i < 1000; i++)
{ 255. Program
System.out.println(i);
}
package com.lara;
}
class D extends Thread
}
{
public class Manager4
public void run()
{
{
public static void main(String[] args)
for(int i = 0; i < 1000; i++)
{
{
B b1 = new B();
System.out.println(i);
b1.start();
}
System.out.println("done");
}
}
}
}
public class Manager6
{
254. Program public static void main(String[] args)
{
package com.lara; D d1 = new D();
class C implements Runnable d1.start();
{
public void run() D d2 = new D();
{ d2.start();
for(int i = 0; i < 1000; i++)
{ for(int i = 1000; i < 2000; i+
System.out.println(i);
+)
}
{
}
System.out.println(i);
}
}
public class Manager5
}
{
}
public static void main(String[] args)
{
C c1 = new C();
Thread t1 = new Thread(c1); 256. Program
t1.start();
package com.lara;
for(int i = 1000; i < 2000; i+ class D extends Thread
{
+)
public void run()
{
{
System.out.println(i);
263. Program
package com.lara;
public class Manager14
package com.lara;
{
public class Manager13
public static void main(String[] args)
{
{
public static void main(String[] args)
Thread t1 = new Thread()
{
{
} {
} System.out.println(i);
} try
public class Manager1 {
{ Thread.sleep(1000);
public static void main(String[] args) }
{ catch(InterruptedException
A a1 = new A();
ex)
a1.start();
{
System.out.println("done");
}
} System.out.println(ex);
}
}
267. Program }
}
package com.lara;
class B implements Runnable
{ 268. Program
public void run()
{ package com.lara;
for(int i = 0; i < 10; i++) class Util
{ {
System.out.println(i); static void sleep(long millis)
try {
{ try
Thread.sleep(1000); {
} Thread.sleep(millis);
catch(InterruptedException }
catch(InterruptedException
ex)
{ ex)
{
System.out.println(ex);
} System.out.println(ex);
} }
} }
} }
public class Manager2 class C extends Thread
{ {
public static void main(String[] args) public void run()
{ {
B b1 = new B(); for(int i = 0; i < 20; i++)
Thread t1 = new Thread(b1); {
t1.start(); System.out.println(i);
for(int i = 10; i < 20; i++) Util.sleep(1000);
} }
}
}
class D implements Runnable 270. Program
{
public void run()
package com.lara;
{
class E extends Thread
for(int i = 20; i < 40; i++ )
{
{
System.out.println(i);
}
Util.sleep(1000);
public class Manager5
}
{
}
public static void main(String[] args)
}
{
public class Manager3
E e1 = new E();
{
e1.setName("first thread");
public static void main(String[] args)
e1.start();
{
E e2 = new E();
C c1 = new C();
e2.setName("second thread");
c1.start();
e2.start();
D d1 = new D();
System.out.println(e1.getId());
Thread t1 = new Thread(d1);
System.out.println(e1.getName());
t1.start();
System.out.println(e1.getPriority());
for(int i = 40; i < 60; i++)
System.out.println(e1.isDaemon());
{
System.out.println("-----");
System.out.println(i);
System.out.println(e2.getId());
Util.sleep(1000);
System.out.println(e2.getName());
}
System.out.println(e2.getPriority());
}
System.out.println(e2.isDaemon());
}
}
}
269. Program
271. Program
package com.lara;
public class Manager4
package com.lara;
{
class F extends Thread
public static void main(String[] args)
{
{
F(String s1)
Thread t1 = Thread.currentThread();
{
t1.setName("initiator");
super(s1);
System.out.println(t1.getId());
}
System.out.println(t1.getName());
}
System.out.println(t1.getPriority());
class G implements Runnable
System.out.println(t1.isDaemon());
{
}
public void run()
{ }
A a1 = new A();
} System.out.println(a1.getPriority());
} Thread t1 = Thread.currentThread();
public class Manager6
{
t1.setPriority(Thread.MAX_PRIORITY);
public static void main(String[] args)
A a2 = new A();
{
System.out.println(a2.getPriority());
F f1 = new F("first");
}
f1.start();
}
G g1 = new G();
Thread t1 = new Thread(g1,
"second"); 274. Program
t1.start();
System.out.println(f1.getName());
package com.lara;
System.out.println(t1.getName());
class H extends Thread
}
{
}
public void run()
{
for(int i = 0; i < 3000; i++)
272. Program {
System.out.println(i);
package com.lara; }
public class Manager7 }
{ }
public static void main(String[] args) public class Manager9
{ {
Thread t1 = Thread.currentThread(); public static void main(String[] args)
System.out.println(t1.getPriority()); {
t1.setPriority(10); H obj = new H();
System.out.println(t1.getPriority()); //obj.setDaemon(true);
} obj.start();
} System.out.println("done");
}
}
273. Program
{ try
System.out.println(i); {
} sleep(1000);
System.out.println("child }
catch(InterruptedException
end");
} ex)
} {
public class Manager10
{
System.out.println(ex);
public static void main(String[] args)
break;
{
}
I obj = new I();
}
obj.start();
}
try
}
{
public class Manager11
obj.join();
{
}
public static void main(String[] args)
catch(InterruptedException
{
ex) J obj = new J();
{ obj.start();
ex.printStackTrace(); Scanner sc = new
}
Scanner(System.in);
for(int i = 1000; i < 2000; i+
String decider;
+) do
{ {
System.out.println(i); try
} {
} Thread.sleep(20000);
} }
catch(InterruptedException
ex)
{
276. Program ex.printStackTrace();
}
package com.lara; System.out.println("continue?(y/n)");
import java.util.Scanner; decider = sc.next();
class J extends Thread }while("y".equals(decider));
{ obj.interrupt();
public void run() }
{ }
int counter = 0;
while(! isInterrupted())
{ 277. Program
counter ++;
System.out.println(counter);
package com.lara.pack1; {
class A A a1 = new A();
{ B b1 = new B(a1);
synchronized void test1() C c1 = new C(a1);
{ b1.start();
for(int i = 0; i < 1000; i++) c1.start();
{
System.out.println(i); }
} }
}
synchronized void test2()
{ 278. Program
for(int i = 1000; i < 2000; i+
Which two classes correctly implement
+)
{ both the java.lang.Runnable
System.out.println(i); and the java.lang.Clonable interfaces?
}
} (Choose two.)
} A. public class Session
class B extends Thread
{ implements Runnable, Clonable {
A a1; public void run();
B(A a1)
{ public Object clone();
this.a1 = a1; }
}
public void run() B. public class Session
{ extends Runnable, Clonable {
a1.test1();
} public void run() { / do something */ }
} public Object clone() { / make a copy */ }
class C extends Thread
{ }
A a1; C. public class Session
C(A a1)
{ implements Runnable, Clonable {
this.a1 = a1; public void run() { / do something */ }
}
public void run() public Object clone() { /* make a copy */ }
{ }
a1.test2();
} D. public abstract class Session
} implements Runnable, Clonable {
public class Manager1
{ public void run() { / do something */ }
public static void main(String[] args) public Object clone() { /*make a copy */ }
}
280. Program
E. public class Session
Given
implements Runnable, implements Clonable
11. public class PingPong2{
{
12. synchronized void hit(long n){
public void run() { / do something */ }
13. for(int i=1;i<3;i++)
public Object clone() { / make a copy */ }
14. System.out.println(n+”-”+i+””);
}
15.}}
16. public class Tester implements
Answer: CD
Runnable{
17. static PingPong2 pp2=new PingPong2();
279. Program
18. public static void main(String args[]){
11. public class PingPong implements
19. new Thread(new Tester()).start();
Runnable{
20. new Thread(new Tester()).start();
12. synchronized void hit(long n){
21.}
13. for(int i=1;i<3;i++)
22. public void run(){
14. System.out.println(n+”-”+i+””);
23. pp2.hit(Thread.currentThread().getId());
15.}
24.}
16. public static void main(String args[]){
25.}
17. new Thread(new PingPong()).start();
Which two statements are true?(choose two)
18. new Thread(new PingPong()).start();
19.}
A. The output could be 5-1 6-1 6-2 5-2
20. public void run(){
B. The output could be 6-1 6-2 5-1 5-2
21. hit(Thread.currentThread().getId());
C. The output could be 6-1 5-2 6-2 5-1
22.}
D. The output could be 6-1 6-2 5-1 7-1
23.}
Which two statements are true?(choose two)
Ans: B
A. The output could be 8-1 7-2 8-2 7-1
B. The output could be 7-1 7-2 8-1 6-1
281. Program
C. The output could be 8-1 7-1 7-2 8-2
D. The output could be 8-1 8-2 7-1 7-2
Which tw o code fragments will execute the
Ans : C, D
method doStuff() in a
14. }
285. Program
15. }
Given:
What is the result?
11. Runnable r = new Runnable() {
A. Compilation fails.
12. public void run() {
B. An exception is thrown at runtime.
13. System.out.print(”Cat”);
C. The code executes normally and prints
14. }
‘foo”.
15. };
D. The code executes normally, but nothing
16. Threadt=new Thread(r) {
is printed.
17. public void run() {
18. System.out.print(”Dog”);
Answer: B
19. }
20. };
284. Program
21. t.start();
Given:
What is the result?
1. public class Threads5 {
A. Cat
2. public static void main (String[] args) {
B. Dog
3. new Thread(new Runnable() {
C. Compilation fails.
4. public void run() {
D. The code runs with no output.
5. System.out.print(”bar”);
E. An exception is thrown at runtime.
6. }}).start();
7. }
Answer: B
8. }
What is the result?
286. Program
A. Compilation fails.
Click the Exhibit button.
B. An exception is thrown at runtime.
Given:
C. The code executes normally and prints
10. public class Starter extends Thread {
“bar”.
11. private int x= 2;
D. The code executes normally, but nothing
12. public static void main(String[] args)
prints.
throws Exception {
13. new Starter().makeItSo();
Answer: C
Given: Given:
1. public class Threads2 implements 1. public class TestOne {
Runnable { 2. public static void main (String[] args)
2. throws Exception {
3. public void nun() { 3. Thread.sleep(3000);
4. System.out.println(”run.”); 4. System.out.println(”sleep”);
289. Program
package com.lara.pack2;
Given: class Shared
1. public class TestOne implements {
void test1()
Runnable { {
2. public static void main (String[] args) Thread t1 = Thread.currentThread();
for(int i = 0; i < 1000; i++)
throws Exception { {
3. Thread t = new Thread(new TestOne()); System.out.println("test1:" +
} System.out.println("test8:" +
}
t1.getName() + ":" + i);
synchronized void test4()
}
{
}
Thread t1 = Thread.currentThread();
}
for(int i = 0; i < 1000; i++)
class Thread1 extends Thread
{
{
System.out.println("test4:" +
Shared s1;
t1.getName() + ":" + i); Thread1(Shared s1)
} {
} this.s1 = s1;
static void test5() }
{ public void run()
Thread t1 = Thread.currentThread(); {
for(int i = 0; i < 1000; i++) s1.test3(); //(A)
{ }
System.out.println("test5:" + }
class Thread2 extends Thread
t1.getName() + ":" + i);
{
}
Shared s1;
}
Thread2(Shared s1)
static void test6()
{
{
this.s1 = s1;
Thread t1 = Thread.currentThread();
}
for(int i = 0; i < 1000; i++)
public void run()
{
{
System.out.println("test6:" +
s1.test3(); //(B)
t1.getName() + ":" + i); }
} }
} public class Manager
synchronized static void test7() {
{ public static void main(String[] args)
Thread t1 = Thread.currentThread(); {
for(int i = 0; i < 1000; i++) Shared s1 = new Shared();
{ Shared s2 = new Shared();
Thread1 t1 = new Thread1(s1); //(C)
Thread2 t2 = new Thread2(s1); //(D)
System.out.println("test7:" +
t1.start();
t1.getName() + ":" + i); t2.start();
} }
} }
synchronized static void test8()
{
Case 1:
Thread t1 = Thread.currentThread();
for(int i = 0; i < 1000; i++)
{ A-test1
B-test1 C-s1
C-s1 D-s1
D-s1 Case 6:
Case 2: A-test3
A-test2 B-test4
B-test2 C-s1
C-s1 D-s1
D-s1 Case 7:
Case 3: A-test4
A-test1 B-test4
B-test2 C-s1
C-s1 D-s1
D-s1 Case 8:
Case 4: A-test4
A-test2 B-test5
B-test3 C-s1
C-s1 D-s1
D-s1 Case 9:
Case 5: A-test5
A-test3 B-test5
B-test3 C-s1
A-test5 B-test8
B-test6 C-s1
C-s1 D-s2
A-test5 B-test8
B-test6 C-s1
C-s1 D-s2
A-test6 B-test5
B-test7 C-s1
C-s1 D-s2
A-test7 B-test8
B-test7 C-s1
C-s1 D-s1
D-s2 Case18 :
A-test8 }
}
B-test8 class B extends Thread
{
Shared s1, s2;
C-s1
B(Shared s1, Shared s2)
{
D-s2 this.s1 = s1;
this.s2 = s2;
291. Program }
public void run()
package com.lara.pack3; {
import s2.test2(s1);
}
java.lang.management.ManagementFactory; }
import java.lang.management.ThreadInfo; class Util
import {
java.lang.management.ThreadMXBean; static void sleep(long millis)
class Shared {
{ try
synchronized void test1(Shared s) {
{ Thread.sleep(millis);
System.out.println("test1 begin"); }
Util.sleep(1000); catch(InterruptedException
s.test2(this); ex)
System.out.println("test1 end"); {
} ex.printStackTrace();
synchronized void test2(Shared s) }
{ }
System.out.println("test2 begin"); }
Util.sleep(1000); public class Manager
s.test1(this); {
System.out.println("test2 end"); public static void main(String[] args)
} {
} Shared s1 = new Shared();
class A extends Thread Shared s2 = new Shared();
{ A a1 = new A(s1, s2);
Shared s1, s2; a1.start();
A(Shared s1, Shared s2) B b1 = new B(s1, s2);
{ b1.start();
this.s1 = s1; Util.sleep(2000);
this.s2 = s2; ThreadMXBean tx =
} ManagementFactory.getThreadMXBean();
public void run() long ids[] =
{
s1.test1(s2); tx.findDeadlockedThreads();
6. public synchronized void printAll() { F. The code may ruin with output “A A A B
7. for (int i = 0; i <names.size(); i++) { C A B C C “, then exit.
8. System.out.print(names.get(i) +“ “); G. The code may ruin with output “A B C A
9. } A B C A B C “, then exit.
10. }
11. public static void main(String[] args) { Answer: EG
12. final NameList sl = new NameList();
13.for(int i=0;i<2;i++) { 297. Program
14. new Thread() { Given:
15. public void ruin() { 1. public class TestFive {
16. sl.add(”A”); 2. private int x;
17. sl.add(”B”); 3. public void foo() {
18. sl.add(”C”); 4 int current = x;
19. sl.printAll(); 5. x = current + 1;
20. } 6. }
21. }.start(); 7. public void go() {
22. } 8. for(int i=0;i<5;i++) {
23. } 9. new Thread() {
24. } 10. public void run() {
Which two statements are true if this class is 11. foo();
compiled and run? 12. System.out.print(x + “, “);
(Choose two.) 13. } }.start();
A. An exception may be thrown at runtime. 14. }}}
B. The code may run with no output, without Which two changes, taken together, would
exiting. guarantee the output: 1, 2,
C. The code may run with no output, exiting 3, 4, 5, ? (Choose two.)
normally. A. Move the line 12 print statement into the
D. The code may rum with output “A B A B foo() method.
C C “, then exit. B. Change line 7 to public synchronized
E. The code may rum with output “A B C A void go() {.
B C A B C “, then exit.
} {
public void run() void test1()
{ {
System.out.println("D-run begin"); //some stmts
obj.test1(); synchronized (this) //mutex
System.out.println("D-run end"); {
} try
} {
public class Manager wait();
{ }
public static void main(String[] args) catch(InterruptedException
{
ex)
A a1 = new A();
{
A a2 = new A();
ex.printStackTrace();
B b1 = new B(a1);
}
C c1 = new C(a1);
}
D d1 = new D(a1);
//some more stmts
b1.start();
}
c1.start();
void test2()
d1.start();
{
//some stmts
try
synchronized (this)
{
{
Thread.sleep(30000);
notifyAll();
}
}
catch(InterruptedException
//some other stmts
ex) }
{ }
ex.printStackTrace(); class B extends Thread
} {
System.out.println("about to A obj;
B(A obj)
release");
{
this.obj = obj;
//a1.test2();
}
a2.test2();
public void run()
}
{
}
System.out.println("run begin");
obj.test1();
System.out.println("run end");
}
}
306. Program public class Manager
{
package com.lara.pack6; public static void main(String[] args)
class A {
A a = new A(); }
B b = new B(a); catch(InterruptedException
b.start();
ex)
try
{
{
ex.printStackTrace();
Thread.sleep(10000);
}
}
synchronized (t1)
catch(InterruptedException
{
ex) t1.notify();
{ }
ex.printStackTrace(); }
} }
a.test2();
} 308. Program
}
package com.lara.pack8;
307. Program public class Manager1
{
package com.lara.pack7; public static void main(String[] args)
class Thread1 extends Thread {
{ Thread.State states[] =
public synchronized void run() Thread.State.values();
{ for(Thread.State state : states)
System.out.println("begin"); {
try
{
System.out.println(state);
wait();
}
}
}
catch(InterruptedException
}
ex)
{
ex.printStackTrace();
}
System.out.println("end"); 309. Program
}
}
package com.lara.pack8;
public class Manager
class A extends Thread
{
{
public static void main(String[] args)
public void run()
{
{
Thread1 t1 = new Thread1();
for(int i = 0; i < 3000; i++)
t1.start();
{
try
System.out.println(i);
{
}
Thread.sleep(20000);
}
System.out.println(ex);
312. Program }
System.out.println(main.getState());
} {
} static void sleep(long millis)
public class Manager5 {
{ try
public static void main(String[] args) {
{ Thread.sleep(millis);
Thread main = }
catch(InterruptedException
Thread.currentThread();
D d1 = new D(main); ex)
d1.start(); {
try ex.printStackTrace();
{ }
d1.join(); }
} }
catch(InterruptedException class E extends Thread
{
ex)
Shared s1, s2;
{
E(Shared s1, Shared s2)
ex.printStackTrace();
{
}
this.s1 = s1;
System.out.println("end");
this.s2 = s2;
}
}
}
public void run()
{
s1.test1(s2);
314. Program }
}
package com.lara.pack8; class F extends Thread
class Shared {
{ Shared s1, s2;
synchronized void test1(Shared s1) F(Shared s1, Shared s2)
{ {
System.out.println(1); this.s1 = s1;
Util.sleep(1000); this.s2 = s2;
s1.test2(this); }
System.out.println(2); public void run()
} {
synchronized void test2(Shared s1) s2.test2(s1);
{ }
System.out.println(3); }
Util.sleep(1000); public class Manager6
s1.test1(this); {
System.out.println(4); public static void main(String[] args)
} {
} Shared s1 = new Shared();
class Util Shared s2 = new Shared();
315. Program
Answer:C,D
Given that Traingle implement
Runnable,and;
316. Program
31.void go()throws Exception{
32.Thread t=new Thread(new Triangle()); package com.lara.pack8;
class G extends Thread
33.t.start(); {
34.for(int x=1;x<100000;x++){ public void run()
{
35.//insert code here System.out.println("begin");
36.if(x%100==0)System.out.println(“g”); try
{
37.}} Thread.sleep(10000);
38.public void run(){ }
catch(InterruptedException
39.try{
ex)
40.for(int x=1;x<100000;x++) {
41.//insert the same code here ex.printStackTrace();
}
42.if(x%100==0)System.out.println(“t”); System.out.println("end");
43.} }
}
44.}catch(Exception e){} public class Manager7
45.} {
public static void main(String[] args)
Which two statements,insert independently {
at both lines G g1 = new G();
g1.start();
35 and 41,tend allow both threas to try
temporarily pause and allow the other {
Thread.sleep(1000);
threads to execute” }
catch(InterruptedException {
Test t;
ex)
A(Test t)
{
{
ex.printStackTrace();
this.t = t;
}
}
g1.stop();
public void run()
try
{
{
System.out.println("1:" + t.i);
Thread.sleep(1000);
t.i = 10;
}
Util.sleep(500);
catch(InterruptedException
System.out.println("2:" + t.i);
ex) t.i = 20;
{ Util.sleep(500);
ex.printStackTrace(); System.out.println("3:" + t.i);
} t.i = 30;
Util.sleep(500);
System.out.println("4:" + t.i);
System.out.println(g1.getState());
t.i = 40;
}
}
}
}
class B extends Thread
{
317. Program Test t;
B(Test t)
package com.lara.pack9; {
class Util this.t = t;
{ }
static void sleep(long millis) public void run()
{ {
try System.out.println("5:" + t.i);
{ t.i = 50;
Thread.sleep(millis); Util.sleep(500);
} System.out.println("6:" + t.i);
catch(InterruptedException t.i = 60;
Util.sleep(500);
ex)
System.out.println("7:" + t.i);
{
t.i = 70;
ex.printStackTrace();
Util.sleep(500);
}
System.out.println("8:" + t.i);
}
t.i = 80;
}
}
class Test
}
{
public class Manager
int i;
{
}
public static void main(String[] args)
class A extends Thread
{ System.out.println("2:" + t.get());
Test t1 = new Test(); t.set(20);
t1.i = 90; Util.sleep(500);
A a1 = new A(t1); System.out.println("3:" + t.get());
a1.start(); t.set(30);
Util.sleep(250); Util.sleep(500);
B b1 = new B(t1); System.out.println("4:" + t.get());
b1.start(); t.set(40);
Util.sleep(40000); }
System.out.println("9:" + }
class B extends Thread
t1.i);
{
}
ThreadLocal t;
}
B(ThreadLocal t)
{
this.t = t;
318. Program }
public void run()
package com.lara.pack10; {
class Util System.out.println("5:" + t.get());
{ t.set(50);
static void sleep(long millis) Util.sleep(500);
{ System.out.println("6:" + t.get());
try t.set(60);
{ Util.sleep(500);
Thread.sleep(millis); System.out.println("7:" + t.get());
} t.set(70);
catch(InterruptedException Util.sleep(500);
System.out.println("8:" + t.get());
ex)
t.set(80);
{
}
ex.printStackTrace();
}
}
public class Manager
}
{
}
public static void main(String[] args)
class A extends Thread
{
{
ThreadLocal t1 = new
ThreadLocal t;
A(ThreadLocal t) ThreadLocal();
{ t1.set(90);
this.t = t; A a1 = new A(t1);
} a1.start();
public void run() Util.sleep(250);
{ B b1 = new B(t1);
System.out.println("1:" + t.get()); b1.start();
t.set(10); Util.sleep(40000);
Util.sleep(500); System.out.println("9:" + t1.get());
{ th.release();
th = new th.goToWait();
pm.setThread(th);
ModelThread();
Util.sleep(1000);
th.start();
}
}
}
return th;
}
}
class User2 extends Thread
public void setThread(ModelThread
{
th) ThreadPoolManager pm = null;
{ User2(ThreadPoolManager pm)
if(pool.size() < 10) {
{ this.pm = pm;
pool.add(th); }
} public void run()
else {
{ while(true)
th.stop(); {
th = null; ModelThread th =
}
pm.getThread();
}
th.release();
public void release()
th.goToWait();
{
pm.setThread(th);
ModelThread th = null;
Util.sleep(1000);
for(int i = 0; i < pool.size(); )
}
{
}
th =
}
(ModelThread)pool.remove(0); class User3 extends Thread
th.stop(); {
} ThreadPoolManager pm = null;
} User3(ThreadPoolManager pm)
} {
class User1 extends Thread this.pm = pm;
{ }
ThreadPoolManager pm = null; public void run()
User1(ThreadPoolManager pm) {
{ while(true)
this.pm = pm; {
} ModelThread th =
public void run()
pm.getThread();
{
th.release();
while(true)
th.goToWait();
{
pm.setThread(th);
ModelThread th =
Util.sleep(1000);
pm.getThread(); }
} package com.lara;
} class B
public class Manager {
{ int i;
public static void main(String[] args) B(int i)
{
{
this.i = i;
ThreadPoolManager pm =
}
new ThreadPoolManager();
}
pm.init();
public class Manager2
User1 u1 = new User1(pm);
{
User2 u2 = new User2(pm);
public static void main(String[] args)
User3 u3 = new User3(pm);
{
u1.start();
B b1 = new B(10);
u2.start();
String s1 = b1.toString();
u3.start();
System.out.println(s1);
Util.sleep(500000);
System.out.println(b1.toString());
u1.stop();
System.out.println(b1);
u2.stop();
}
u3.stop();
}
pm.release();
System.out.println("End of
the Game"); 324. Program
}
}
package com.lara;
class C
{
322. Program int i;
C(int i)
package com.lara; {
class A this.i = i;
{ }
int i; public String toString()
} {
public class Manager1 return "i = " + i;
{ }
public static void main(String[] args) }
{ public class Manager3
A a1 = new A(); {
a1.i = 20; public static void main(String[] args)
System.out.println(a1); {
} C c1 = new C(90);
} System.out.println(c1);
C c2 = new C(20);
System.out.println(c2);
323. Program }
} }
}
public class Manager5
325. Program {
public static void main(String[] args)
{
package com.lara;
E e1 = new E(1, 2);
class D
E e2 = new E(11, 25);
{
System.out.println(e1);
int i;
System.out.println(e2);
D(int i)
}
{
}
this.i = i;
}
}
public class Manager4
{
public static void main(String[] args) 327. Program
{
D d1 = new D(90); package com.lara;
D d2 = new D(90); class F
D d3 = d2; {
D d4 = d1; String s1;
System.out.println(d1); int i;
System.out.println(d2); F(String s1, int i)
System.out.println(d3); {
System.out.println(d4); this.s1 = s1;
} this.i = i;
} }
class I
{
328. Program int x;
I(int x)
{
package com.lara;
this.x = x;
class G
}
{
public String toString()
int i;
{
G(int i)
return "x = " + x;
{
}
this.i = i;
}
}
public class Manager8
public String toString()
{
{
public static void main(String[] args)
return "i = " + i;
{
}
I obj = new I(10);
}
String s1 = "hello " + obj;
class H
System.out.println(s1);
{
}
G g1;
}
int j;
H(G g1, int j)
{ 330. Program
this.g1 = g1;
this.j = j; package com.lara;
} class K
public String toString() {
{ int i;
return g1 + ", j = " + j; K(int i)
} {
} this.i = i;
public class Manager7 }
{ public String toString()
public static void main(String[] args) {
{ String s1 = super.toString();
G g1 = new G(90); String s2 = "i = " + i;
H h1 = new H(g1, 20); return s1 + " & " + s2;
System.out.println(g1); }
System.out.println(h1); }
} public class Manager9
} {
public static void main(String[] args)
{
329. Program K k1 = new K(10);
System.out.println(k1);
package com.lara;
K k2 = new K(20);
System.out.println(k2);
332. Program
}
}
package com.lara;
class N
{
331. Program }
public class Manager11
package com.lara; {
class L public static void main(String[] args)
{ {
int i; N n1 = null;
L(int i) System.out.println(n1);
{ String s1 = n1 + "abc";
this.i = i; System.out.println(s1);
} }
public String toString() }
{
return "i = " + i;
} 333. Program
}
class M extends L
package com.rst;
{
class A
int j;
{
M(int i, int j)
int i;
{
}
super(i);
public class Manager
this.j = j;
{
}
public static void main(String[] args)
public String toString()
{
{
A a1 = new A();
return super.toString() + ", j =
a1.i = 10;
" + j; A a2 = new A();
} a2.i = 10;
} A a3 = a1;
public class Manager10 System.out.println(a1 == a2);
{ System.out.println(a2 == a3);
public static void main(String[] args) System.out.println(a1 == a3);
{ }
L obj1 = new L(90); }
M obj2 = new M(2, 40);
System.out.println(obj1);
System.out.println(obj2); 334. Program
}
}
package com.rst; }
class B }
{
int i;
} 336. Program
public class Manager1
{
package com.rst;
public static void main(String[] args)
class D
{
{
B b1 = new B();
int i;
B b2 = new B();
D(int i)
B b3 = b1;
{
b1.i = b2.i = 20;
this.i = i;
System.out.println(b1 == b2);
}
System.out.println(b2 == b3);
}
System.out.println(b3 == b1);
public class Manager3
System.out.println(b1.i ==
{
b2.i); public static void main(String[] args)
int i = 9; {
int j = 9; D d1 = new D(90);
System.out.println(i == j); D d2 = d1;
} System.out.println(d1 == d2);
}
System.out.println(d1.equals(d2));
}
335. Program }
package com.rst;
class C 337. Program
{
int i;
package com.rst;
C(int i)
class E
{
{
this.i = i;
int i;
}
E(int i)
}
{
public class Manager2
this.i = i;
{
}
public static void main(String[] args)
public boolean equals(Object obj)
{
{
C c1 = new C(10);
return this == obj;
C c2 = new C(10);
}
System.out.println(c1 == c2);
}
public class Manager4
System.out.println(c1.equals(c2)); {
&& d ==
myObj.d); System.out.println(obj1.equals(obj2));
return flag; System.out.println("-------");
}
}
System.out.println(obj3.equals(obj4));
public class Manager7
System.out.println("-------");
{
public static void main(String[] args)
{ System.out.println(obj1.equals(obj3));
H h1 = new H(9, 20.9); System.out.println("-------");
H h2 = new H(9, 20.9);
System.out.println(h1.equals(h2));
System.out.println(obj3.equals(obj1));
}
}
}
}
341. Program
342. Program
package com.rst;
package com.rst;
class L
class P
{
{
int i;
int i;
}
}
class M
class Q
{
{
int i;
int i;
public boolean equals(Object obj)
public boolean equals(Object obj)
{
{
M myObj = (M) obj;
if(!(obj instanceof Q))
return i == myObj.i;
{
}
return false;
}
}
public class Manager8
Q myObj = (Q) obj;
{
return i == myObj.i;
public static void main(String[] args)
}
{
}
L obj1 = new L();
public class Manager9
L obj2 = new L();
{
M obj3 = new M();
M obj4 = new M();
obj1.i = 10; 343. public static void main(String[] args)
obj2.i = 10;
obj3.i = 10; {
obj4.i = 10; P p1 = new P();
P p2 = new P();
Q q1 = new Q();
} 348. Program
Given:
347. Program 42. public class ClassA {
43. public int getValue() {
package com.rst;
44.int value=0;
class U
{ 45. boolean setting = true;
int i;
46. String title=”Hello”;
U(int i)
{ 47. if (value || (setting && title ==
this.i = i;
“Hello”)) { return 1; }
}
public String toString() 48. if (value == 1 & title.equals(”Hello”)) {
{
return 2; }
return "i = " + i;
} 49. }
public boolean equals(Object obj)
50. }
{
return (obj instanceof U && i And:
== ((U)obj).i); 70. ClassA a = new ClassA();
}
71. a.getValue();
public int hashCode()
{ What is the result?
return i;
A. 1
}
} B. 2
public class Manager13
C. Compilation fails.
{
public static void main(String[] args) D. The code runs with no output.
{
E. An exception is thrown at runtime.
U u1 = new U(90);
U u2 = new U(90);
System.out.println(u1);
Answer: C
System.out.println(u2);
Given:
System.out.println(u1.hashCode());
11. public class Person {
System.out.println(u2.hashCode()); 12. private String name, comment;
} 13. private int age;
}
14. public Person(String n, int a, String c) {
351. Program
package com.rst;
class V
package com.rst;
{
class W
int i, j;
{
V(int i, int j)
int i;
{
String s1;
this.i = i;
W(int i, String s1)
this.j = j;
{
}
this.i = i;
public String toString()
this.s1 = s1;
{
}
return "i = " + i + ", j = " + j;
public int hashCode()
}
{ String s1 =
String str =
Integer.toString(i);
Integer.toString(i); String s2 =
int hash = str.hashCode();
Integer.toString(j);
hash += s1.hashCode();
int hash = s1.hashCode();
return hash;
hash += s2.hashCode();
}
return hash;
}
}
public class Manager15
}
{
public class Manager16
public static void main(String[] args)
{
{
public static void main(String[] args)
W w1 = new W(10, "abc");
{
W w2 = new W(10, "abc");
X x1 = new X(9, 20);
System.out.println(w1.hashCode());
X x2 = new X(20, 9);
System.out.println(w2.hashCode());
System.out.println(x1);
}
System.out.println(x2);
}
System.out.println(x1.equals(x2));
352. Program
System.out.println(x1.hashCode());
package com.rst;
class X
System.out.println(x2.hashCode());
{
}
int i, j;
}
X(int i, int j)
{
this.i = i;
this.j = j; 353. Program
}
public String toString() package com.lara;
{ public class A implements Cloneable
return "i = " + i + ", j = " + j; {
} int i;
public boolean equals(Object obj)
{ public static void main(String[] args)
return (obj instanceof X && throws
i == ((X)obj).i
CloneNotSupportedException
&& {
j == A a1 = new A();
a1.i = 10;
((X)obj).j);
A a2 = (A)a1.clone();
}
System.out.println(a2.i);
public int hashCode()
}
{
{ package com.lara;
class C
} {
public class Manager
{ }
public static void main(String[] args) public class Manager2
{ {
A a1 = new A(); public static void main(String[] args)
A a2 = new A(); throws Exception
System.out.println(a1 == a2); {
Class c1 = a1.getClass(); Class c1 =
Class c2 = a2.getClass();
Class.forName("com.lara.C");
System.out.println(c1 == c2);
C obj1 = new C();
}
Class c2 = obj1.getClass();
}
System.out.println(c1 == c2);
}
}
360. Program
System.out.println(c3 == c1); {
} System.out.println("test2");
} }
}
363. Program public class Manager5
{
package com.lara; public static void main(String[] args)
import java.lang.reflect.Method; throws Exception
class E {
{ Class c1 =
public void test()
{ Class.forName("com.lara.F");
System.out.println("test"); F f1 = (F) c1.newInstance();
} f1.test1();
} f1.test2();
public class Manager4 System.out.println("------");
{ Object obj =
public static void main(String[] args)
throws Exception c1.newInstance();
{ Method m1 =
Class c1 = c1.getDeclaredMethod("test1");
Class.forName("com.lara.E"); Method m2 =
Object obj = c1.getDeclaredMethod("test2");
c1.newInstance(); m1.invoke(obj);
Method m1 = m2.invoke(obj);
c1.getDeclaredMethod("test"); }
m1.invoke(obj); }
System.out.println("done");
}
}
365. Program
package com.lara;
import java.lang.reflect.Method;
364. Program
class G
{
package com.lara; public void test(int i, String s1)
import java.lang.reflect.Method; {
class F System.out.println("test:" +
{
public void test1() i);
{ System.out.println("test:" +
System.out.println("test1"); s1);
} }
public void test2() }
367. Program
366. Program
package com.lara;
package com.lara; public class Manager8
import java.lang.reflect.Method; {
import java.util.Scanner; public static void main(String[] args)
class H throws Throwable
{ {
public void test1() Manager8 m1 = new
{
Manager8();
System.out.println("from H.test1");
m1.finalize();
}
m1.finalize();
}
m1.finalize();
class I
m1.finalize();
{
}
public void test2()
}
{
System.out.println("from I.test2");
}
} 368. program
public class Manager7 Given:
{
public static void main(String[] args) 11. rbo = new ReallyBigObject();
throws Exception
the order in which the two objects became B. Only the object referenced by booch is
finalizable. eligible for garbage
collection.
Answer: BD C. Only the object referenced by snoog is
eligible for garbage
372. Program collection.
Given: D. Only the object referenced by snooch is
11. class Snoochy { eligible for garbage
12. Boochybooch; collection.
13. public Snoochy() { booch = new E. The objects referenced by snooch and
Boochy(this); } booch are eligible for garbage
14. } collection.
15.
16. class Boochy { Answer: E
17. Snoochy snooch;
18. public Boochy(Snoochy s) { snooch = 373. Program
s; } Given:
19. } 1. public class GC {
And the statements: 2. private Object o;
21. public static void main(String[] args) { 3. private void doSomethingElse(Object obj)
22. Snoochy snoog = new Snoochy(); { o = obj; }
23. snoog = null; 4. public void doSomething() {
24. // more code here 5. Object o = new Object();
25. } 6. doSomethingElse(o);
Which statement is true about the objects 7. o = new Object();
referenced by snoog, 8. doSomethingElse(null);
snooch, and booch immediately after line 23 9.o=null;
executes? 10. }
A. None of these objects are eligible for 11. }
garbage collection. When the doSomething method is called,
after which line does the
} System.out.println(s1.length());
391. Program }
package com.lara; }
public class U
{ 395. Program
public static void main(String[] args) package com.lara;
{ public class Y
String s1="ja".concat("va"); {
String s2="java"; public static void main(String[] args)
System.out.println(s1==s2); {
} String s1=" abc 123 ";
} s1.trim();
System.out.println(s1.length());
392. Program }
package com.lara; }
public class V
{
396. Program
public static void main(String[] args)
package com.lara;
{
public class Z
String s1="abc";
{
System.out.println(s1.length());
public static void main(String[] args)
{
} String s1=" abc 123 ";
} s1=s1.trim();
System.out.println(s1.length());
393. Program }
package com.lara; }
public class W
{ 397. Program
public static void main(String[] args) package com.lara;
{ public class M1
String s1=null; {
System.out.println(s1.length()); public static void main(String[] args)
} {
} String s1="abc123xyba"
System.out.println(s1.charAt(4));
package com.lara; {
public class M5 public static void main(String[] args)
{ {
public static void main(String[] args) String s1="lara tech";
{ System.out.println(s1.startsWith("lara"));
String s1="abc;123;xyz;hello"; System.out.println(s1.endsWith("tech"));
String x[]=s1.split(";"); System.out.println(s1.startsWith("abc"));
for(String obj:x) System.out.println(s1.endsWith("abc"));
{ }
}
System.out.println(obj);
}
406. Program
}
}
package com.lara;
403. Program public class A
package com.lara; {
public class M6 public static void main(String[] args)
{ {
public static void main(String[] args) StringBuffer sb=new StringBuffer();
{ sb.append("abc\n");
String s1="lara tech"; sb.append("xyz\n");
String s2=s1.substring(0,4); sb.append("hello");
String s3=s1.substring(5,9); System.out.println(sb);
String s4=s1.substring(5); }
System.out.println(s2); }
System.out.println(s3);
System.out.println(s4); 407. Program
}
} package com.lara;
public class B
404. Program {
package com.lara; public static void main(String[] args)
public class M7 {
{ StringBuffer sb=new StringBuffer();
public static void main(String[] args) sb.append("abc\n");
{ sb.append("abc\n");
String s1="Abcxyz123"; sb.append("abc\n");
System.out.println(s1.toUpperCase()); sb.append("abc");
System.out.println(s1.toLowerCase()); System.out.println(sb);
System.out.println(s1); }
} }
}
405. Program
408. Program
package com.lara;
public class M8
3. public void log(String message, String How many String objects will be created
user) { when this method is invoked?
4. logger.append(message); A. 1
5. logger.append(user); B. 2
6. } C. 3
7. } D. 4
The programmer must guarantee that a E. 5
single MyLogger object works F. 6
properly for a multi-threaded system. How
must this code be changed Answer: C
to be thread-safe?
A. synchronize the log method 421. Program
B. replace StringBuilder with StringBuffer Given:
C. No change is necessary, the current 1. public class TestString3 {
MyLogger code is already 2. public static void main(String[] args) {
thread-safe. 3. // insert code here
D. replace StringBuilder with just a String 5. System.out.println(s);
object and use the string 6. }
concatenation (+=) within the log method 7. }
Which two code fragments, inserted
Answer: A independently at line 3, generate
the output 4247? (Choose two.)
420. Program A. String s = “123456789”;
Given: s = (s-”123”).replace(1,3,”24”) - “89”;
11. public String makinStrings() { B. StringBuffer s = new
12. String s = “Fred”; StringBuffer(”123456789”);
13. s = s + “47”; s.delete(0,3).replace( 1,3, “24”).delete(4,6);
14. s = s.substring(2, 5); C. StringBuffer s = new
15. s = s.toUpperCase(); StringBuffer(”123456789”);
16. return s.toString(); s.substring(3,6).delete( 1 ,3).insert( 1, “24”);
17. }
D. StringBuilder s = new {
String s1="abcabaabc";
StringBuilder(”123456789”);
Pattern p1=Pattern.compile("[ab]");
s.substring(3,6).delete( 1 ,2).insert( 1, “24”); Matcher m1=p1.matcher(s1);
while(m1.find())
E. StringBuilder s = new
{
StringBuilder(”123456789”); System.out.println(m1.start()
s.delete(0,3).delete( 1 , +":"+m1.group());
}
3).delete(2,5).insert( 1, “24”);
}
}
Answer: BE
424. Program
package com.lara;
422. Program import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class C
package com.lara;
{
import java.util.regex.Matcher;
public static void main(String[] args)
import java.util.regex.Pattern;
{
public class A
String s1="abcxyz1pqr2km9rt";
{
Pattern
public static void main(String[] args)
{ p1=Pattern.compile("[abkpq]");
String s1="abab89abc1abc1s2"; Matcher m1=p1.matcher(s1);
Pattern p1=Pattern.compile("abc"); while(m1.find())
Matcher m1=p1.matcher(s1); {
while(m1.find()) System.out.println(m1.start()
{
+":"+m1.group());
System.out.println(m1.start()
}
+":"+m1.group()); }
} }
}
} 425. Program
package com.lara;
423. Program import java.util.regex.Matcher;
import java.util.regex.Pattern;
package com.lara; public class D
import java.util.regex.Matcher; {
import java.util.regex.Pattern; public static void main(String[] args)
public class B {
{ String s1="abcxyz1pqr2km9rt";
public static void main(String[] args) Pattern p1=Pattern.compile("[a-p]");
package com.lara; }
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class E
{ 428. Program
public static void main(String[] args)
{
package com.lara;
String s1="abcxyz1pqr2km9rt";
import java.util.regex.Matcher;
Pattern p1=
import java.util.regex.Pattern;
Pattern.compile("[123456]"); public class G
Matcher m1=p1.matcher(s1); {
while(m1.find()) public static void main(String[] args)
{ {
String
System.out.println(m1.start() s1="abcxyz1pqr2km9rt";
Pattern p1=
+":"+m1.group());
} Pattern.compile("[a-ep-z1-35-9]");
} Matcher m1=p1.matcher(s1);
} while(m1.find())
{
427. Program System.out.println(m1.start()
+":"+m1.group());
package com.lara; }
import java.util.regex.Matcher;
import java.util.regex.Pattern; }
public class F
{ }
public static void main(String[] args)
{
String 429. Program
s1="abcxyz1pqr2km9rt";
package com.lara;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class G 431. Program
{
public static void main(String[] args)
package com.lara;
{
import java.util.regex.Matcher;
String
import java.util.regex.Pattern;
s1="abcxyz1pqr2km9rt"; public class J
Pattern p1= {
public static void main(String[] args)
Pattern.compile("[a-ep-z1-35-9]");
{
Matcher m1=p1.matcher(s1);
String
while(m1.find())
{ s1="abcxyz1pqr2km9rt";
System.out.println(m1.start() Pattern p1=
+":"+m1.group()); Pattern.compile("\\d+");
} Matcher m1=p1.matcher(s1);
while(m1.find())
} {
System.out.println(m1.start()
}
+":"+m1.group());
}
}
430. Program }
package com.lara;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class I 432. Program
{
public static void main(String[] args)
package com.lara;
{
import java.util.regex.Matcher;
String
import java.util.regex.Pattern;
s1="abcxyz1pqr2km9rt"; public class K
Pattern p1= {
public static void main(String[] args)
Pattern.compile("\\d");
{
Matcher m1=p1.matcher(s1);
String s1="a-b$6AB%1jbc";
while(m1.find())
Pattern p1=
{
System.out.println(m1.start() Pattern.compile("\\w");
Matcher m1=p1.matcher(s1);
+":"+m1.group());
while(m1.find())
}
{
}
}
System.out.println(m1.start() }
+":"+m1.group());
} 435. Program
}
} package com.lara;
public class N
433. Program {
public static void main(String[] args)
{
package com.lara;
String s1="a1b2c3d4";
import java.util.regex.Matcher;
String x[]=s1.split("[0-9]");
import java.util.regex.Pattern;
for(String str:x)
public class L
{
{
public static void main(String[] args)
{ System.out.println(str);
String s1="abc xyz 123"; }
Pattern p1= }
}
Pattern.compile("\\s");
Matcher m1=p1.matcher(s1);
while(m1.find()) 436. Program
{
System.out.println(m1.start() package com.lara;
public class O
+":"+m1.group());
{
}
public static void main(String[] args)
}
{
}
String s1="a1b2c3d4";
String x[]=s1.split("2");
434. Program for(String str:x)
{
package com.lara;
import java.util.regex.Matcher;
System.out.println(str);
import java.util.regex.Pattern;
}
public class M
}
{
}
public static void main(String[] args)
{
String s1="a1b2c3d4"; 437. Program
String x[]=s1.split("\\d"); Given:
for(String str:x)
{ 11. String test = “This is a test”;
12. String[] tokens = test.split(”\s”);
System.out.println(str); 13. System.out.println(tokens.length);
}
} What is the result?
Answer: A
441. Program
439. Program
package com.lara;
Given: import java.util.StringTokenizer;
public class Q
11. String test = “Test A. Test B. Test C.”;
{
12. // insert code here public static void main(String[] args)
{
13. String[] result = test.split(regex);
StringTokenizer st=new
Which regular expression inserted at line 12
StringTokenizer("a1b2c3d4","\\d");
will correctly split test into while (st.hasMoreElements())
{
System.out.println(st.nextElement()); 445. Program
}
} package com.lara;
} public class I
{
442. Program public static void main(String[] args)
{
package com.lara; System.out.printf("%s to only
public class I %S","hello","ladies");
{ }
public static void main(String[] args) }
{
System.out.printf("%s","Hello");
}
446. Program
}
package com.lara;
public class I
443. Program
{
package com.lara; public static void main(String[] args)
public class I {
{ System.out.printf("%d + %d= %d",1,2,3);
public static void main(String[] args) }
{ }
System.out.printf("(%s)","Hello");
}
447. Program
}
package com.lara;
public class I
444. Program
{
public static void main(String[] args)
package com.lara; {
public class I System.out.printf("%2$d + %1$d =
{
public static void main(String[] args) %3$d",1,2,3);
{ }
}
System.out.printf("(%s)","Hello");
}
448. Program
}
package com.lara;
public class I
{
public static void main(String[] args)
{ package com.lara;
System.out.printf("%2$d + public class I
{
%1$d = %3$d",1,2,3);
public static void main(String[] args)
}
{
}
System.out.printf("<%0-
10d>",23);
449. Program }
}
package com.lara;
public class I
{ 453. Program
public static void main(String[] args)
{ package com.lara;
System.out.printf("(%10d)",100); public class I
} {
} public static void main(String[] args)
{
450. Program
System.out.printf("%0,10d)",100000);
package com.lara; }
}
public class I
{
public static void main(String[] args)
{ 454. Program
System.out.printf("(%-
Given:
10d)",23);
} 12. System.out.format(”Pi is approximately
} %d.”, Math.PI);
What is the result?
451. Program
A. Compilation fails.
package com.lara; B. Pi is approximately 3.
public class I
{ C. Pi is approximately 3.141593.
public static void main(String[] args) D. An exception is thrown at runtime.
{
System.out.printf("<%010d>",23);
} Answer: D
}
+,10d>",1000);
package com.lara;
}
public class I
}
{
public static void main(String[] args)
{
456. Program System.out.printf("PI value
is: %f",Math.PI);
package com.lara;
}
public class I
}
{
public static void main(String[] args)
{
System.out.printf("%-(,10d",-100); 459. Program
}
} package com.lara;
import java.util.Date;
public class A
457. Program {
public static void main(String[] args)
Given {
1. public class LineUp{ Date d1=new Date();
System.out.println(d1);
2. public static void main(String args[]){ }
3. double d=12.345; }
6.}
package com.lara;
Which code fragment, inserted at line 4 import java.util.Date;
produces the output |12.345|? public class B
{
A. System.out.printf(“|%6d|\n”,d); public static void main(String[] args)
B. System.out.printf(“|%6f|\n”,d); {
Date d1=new Date(0);
C. System.out.printf(“|%3.7d|\n”,d); System.out.println(d1);
D. System.out.printf(“|%3.7f|\n”,d); }
}
E. System.out.printf(“|%6.3d|\n”,d);
461. Program }
}
package com.lara;
import java.util.Date;
464. Program
public class B
{
public static void main(String[] args) package com.lara;
{ import java.util.Date;
Date d1=new Date(1000*60*60); public class C
System.out.println(d1); {
} public static void main(String[] args)
} {
Date d1=new Date(-
24*1000*60*60);
462. Program
System.out.println(d1);
long millis=d1.getTime();
package com.lara; System.out.println(millis);
import java.util.Date; }
}
public class B
{
public static void main(String[] args)
{
Date d1=new
465. Program
Date(24*1000*60*60);
System.out.println(d1); package com.lara;
} import java.util.Date;
} public class C
{
package com.lara;
import java.util.Calendar;
import java.util.Date; 469. Program
public class D
{
package com.lara;
public static void main(String[] args)
import java.util.Calendar;
{
import java.util.Date;
Calendar c1=Calendar.getInstance();
public class D
Date d1=c1.getTime();
{
System.out.println(d1);
public static void main(String[] args)
}
{
}
Calendar c1=Calendar.getInstance();
c1.add(Calendar.MONTH, 2);
Date d1=c1.getTime();
467. Program System.out.println(d1);
}
package com.lara; }
import java.util.Calendar;
import java.util.Date;
public class D 470. Program
{
public static void main(String[] args)
package com.lara;
{
import java.util.Calendar;
Calendar c1=Calendar.getInstance();
import java.util.Date;
c1.add(Calendar.DATE, 2);
public class D
Date d1=c1.getTime();
{
System.out.println(d1);
public static void main(String[] args)
}
{
}
Calendar c1=Calendar.getInstance();
c1.add(Calendar.MONTH, -2);
Date d1=c1.getTime();
468. Program System.out.println(d1);
}
package com.lara; }
import java.util.Calendar;
import java.util.Date;
public class D 471. Program
{
public static void main(String[] args)
package com.lara;
{
import java.util.Calendar;
Calendar c1=Calendar.getInstance();
import java.util.Date;
c1.add(Calendar.DATE, -2);
public class D
Date d1=c1.getTime();
{
System.out.println(d1);
public static void main(String[] args)
}
{
}
package com.lara;
473. Program
import java.text.DateFormat;
import java.util.Date;
package com.lara; public class E
import java.text.DateFormat; {
import java.util.Date; public static void main(String[] args)
public class E {
{ Date d1=new Date();
public static void main(String[] args) System.out.println(d1);
{ DateFormat
Date d1=new Date();
System.out.println(d1); df=DateFormat.getDateInstance(DateFormat
DateFormat .
df=DateFormat.getDateInstance(DateFormat FULL);
String s1=df.format(d1);
.SHORT); System.out.println(s1);
String s1=df.format(d1); }
System.out.println(s1); }
}
}
476. Program
} 479. Program
}
package com.lara;
import java.text.NumberFormat;
477. Program import java.util.Locale;
public class G
{
package com.lara;
public static void main(String[] args)
import java.text.NumberFormat;
{
import java.util.Locale;
double d1=102929.8789789;
public class G
System.out.println(d1);
{
NumberFormat
public static void main(String[] args)
{ nf1=NumberFormat.getCurrencyInstance();
double d1=102929.8789789; String s1=nf1.format(d1);
System.out.println(d1); System.out.println(s1);
NumberFormat
}
nf1=NumberFormat.getInstance(Locale.ITA
}
LY);
String s1=nf1.format(d1); 480. Program
System.out.println(s1);
} Given:
} • d is a valid, non-null Date object
• df is a valid, non-null DateFormat
object set to the current locale .
What outputs the current locales country
478. Program name and the appropriate
Given:
Answer: B
33. Date d = new Date(0);
34. String ds = “December 15, 2004”; 486. Program
Answer: C
Answer: B
492. Program
491. Program
A class games.cards.Poker is correctly
A UNIX user named Bob wants to replace defined in the jar file Poker.jar.
his chess program with a A user wants to execute the main method of
new one, but he is hot sure where the old Poker on a UNIX system
one is installed. Bob is using the command:
currently able to run a Java chess program java games.cards.Poker
starting from his home What allows the user to do this?
directory /home/bob using the command: A. put Poker.jar in directory /stuff/java, and
java -classpath /test:/home/bob/downloads/* set the CLASSPATH to include
.jar games.Chess /stuff/java
Bob’s CLASSPATH is set (at login time) to: B. put Poker.jar in directory /stuff/java, and
/ set the CLASSPATH to include
usr/lib:/home/bob/classes:/opt/java/lib:/opt/j /stuff/java/*.jar
ava/lib/* .jar C. Put Poker.jar in directory /stuff/java, and
What is a possible location for the set the CLASSPATH to include
Chess.class file? /stuff/java/Poker.jar
A. /test/Chess.class D.putPoker.jarindirectory/stuff/java/games/c
B. /home/bob/Chess.class ards, and set theCLASSPATH to include
C. /test/games/Chess.class /stuff/java
D. /usr/lib/games/Chess.class E.putPoker.jarindirectory/stuff/java/games/c
E. /home/bob/games/Chess.class ards, and set the CLASSPATH to include
F. inside jarfile /opt/java/lib/Games.jar (with /stuffijava/*.jar
a correct manifest) F.putPoker.jarindirectory/stuff/java/games/c
G. inside jarfile ards, andset the CLASSPATH to include
/home/bob/downloads/Games.jar (with a /stuff/java/Poker.jar
correct Answer: C
manifest)
493. Program
Answer: D
12. MR(”Mr.”), MRS(”Mrs.”), MS(”Ms.”); Which two correctly access the DIAMETER
13. private final String title; member of the Geodetics
14. private Title(String t) { title = t; } class? (Choose two.)
15. public String format(String last, String A. import com.sun.scjp.Geodetics;
first) { public class TerraCarta {
16. return title + “ “ + first + “ “ + last; public double halfway()
17. } { return Geodetics.DIAMETER/2.0; } }
18. } B. import static com.sun.scjp.Geodetics;
19. public static void main(String[] args) { public class TerraCarta {
20. public double halfway() { return
System.out.println(Title.MR.format(”Doe”, DIAMETER/2.0; } }
“John”)); C. import static com.sun.scjp.Geodetics. *;
21. } public class TerraCarta {
What is the result? public double halfway() { return
A. Mr. John Doe DIAMETER/2.0; } }
B. An exception is thrown at runtime. D. package com.sun.scjp;
C. Compilation fails because of an error in public class TerraCarta {
line 12. public double halfway() { return
D. Compilation fails because of an error in DIAMETER/2.0; } }
line 15.
E. Compilation fails because of an error in Answer: AC
line 20.
500. Program
Answer: A
Given:
499. Program 1. package sun.scjp;
Given: 2. public enum Color { RED, GREEN,
10. package com.sun.scjp; BLUE }
11. public class Geodetics { 1. package sun.beta;
12. public static final double DIAMETER = 2. // insert code here
12756.32; // kilometers 3. public class Beta {
13. } 4. Color g = GREEN;
Given:
10. class Line {
Answer: CE
11. public static class Point { }
501. Program 12. }
13.
Given a class Repetition:
14. class Triangle {
1. package utils;
15. // insert code here
2.
16. }
3. public class Repetition {
Which code, inserted at line 15, creates an
4. public static String twice(String s)
instance of the Point class
{ return s + s; }
defined in Line?
5. }
A. Point p = new Point();
and given another class Demo:
B. Line.Point p = new Line.Point();
1. // insert code here
2.
17. short y= 6;
1. public class BuildStuff{ 18. long z= 7;
2. public static void main (String 19. go(y);
args[]) { 20. go(z);
3. Boolean test=new Boolean(true); 21. }
4. Integer x=343; 22. }
5.Integer y =new BildStuff().go(test,x); What is the result?
6. System.out.println(y); A. int Long
7.} B. Short Long
8. int go(Boolean b,int i){ C. Compilation fails.
9. if(b) return (i/7); D. An exception is thrown at runtime.
10. return (i/49);
What is the result? Answer: A
F. 7
G. 49 512. Program
H. 343 Given:
I. Compilation fails 12. public class Wow {
J. An exception is thrown at runtime. 13. public static void go(short n)
Ans : B {System.out.println(”short”); }
14. public static void go(Short n)
511. Program
{System.out.println(”SHORT”);}
Given: 15. public static void go(Long n)
11. public class Yikes { {System.out.println(” LONG”); }
12. 16. public static void main(String [] args) {
13. public static void go(Long n) 17. Short y= 6;
{System.out.println(”Long “);} 18.int z=7;
14. public static void go(Short n) 19. go(y);
{System.out.println(”Short “);} 20. go(z);
15. public static void go(int n) 21. }
{System.out.println(”int “);} 22. }
16. public static void main(String [] args) { What is the result?
B. SHORT LONG
C. Compilation fails.
A programmer needs to create a logging
D. An exception is thrown at runtime.
method that can accept an
arbitrary number of arguments. For
Answer: C
example, it may be called in these
513. Program ways:
logIt(”log message 1 “);
Click the Exhibit button. logIt(”log message2”,”log message3”);
1. public class A { logIt(”log message4”, “log message5”, “log
2. public String doit(int x, int y) { message6);
3. return “a”; Which declaration satisfies this
4. } requirement?
5. A. public void logIt(String * msgs)
6. public String doit(int... vals) { B. public void logIt(String [] msgs)
7. return “b”; C. public void logIt(String... msgs)
8. } D. public void logIt(String msg1, String
9. } msg2, String msg3)
Given:
25. A a=new A(); Answer: C
26. System.out.println(a.doit(4, 5));
What is the result?
515. Program
A. Line 26 prints “a” to System.out.
B. Line 26 prints ‘b” to System.out. Given:
C. An exception is thrown at line 26 at 10. public class Bar {
runtime. 11.static void foo(int...x) {
D. Compilation of class A will fail due to an 12. // insert code here
error in line 6. 13. }
14. }
Answer: A
Which two code fragments, inserted
independently at line 12, will allow
Given
How many of the code fragments, inserted
12. public class Barn{
independently at line 12, compile?
13. public static void main (String args[]){
G. 0
14. new Barn().go(“hi”,1);
H. 1
15. new Barn().go(“hi”,”world”,2);
I. 2
16.}
J. 3
17. public void go(String…y,int x){
K. 4
18.System.out.println(y[y.length-1]+””);
L. 5
19.}
Ans: D
20.}
What is the result? 518. Program
F. hi hi Given:
G. hi world 12. String csv = “Sue,5,true,3”;
H. world world 13. Scanner scanner = new Scanner( csv);
I. Compilation fails 14. scanner.useDelimiter(”,”);
J. An exception is thrown at runtime. 15. int age = scanner.nextInt();
Ans :D What is the result?
A. Compilation fails.
517. Program
B. After line 15, the value of age is 5.
Given
C. After line 15, the value of age is 3.
11. class Mud{
D. An exception is thrown at runtime.
521. Program
Answer: B
Given:
520. Program 12. public class Yippee2 {
Given: 13.
15. public class Yippee { 14. static public void main(String [] yahoo)
16. public static void main(String [] args) { {
Given:
Given:
Answer: D
11.classA {
528. Program
Given:
Answer: B 11. static class A {
12. void process() throws Exception { throw
530. Program new Exception(); }
Given: 13. }
11. static classA { 14. static class B extends A {
12. void process() throws Exception { throw 15. void process()
new Exception(); } { System.out.println(”B”); }
13. } 16. }
17. public static void main(String[] args) { B. The connection will not be retrieved in
18. new B().process(); line 85.
19. } C. The resource connection will not be
What is the result? closed on line 88.
A. B D. The enclosing method will throw an
B. The code runs with no output. exception to its caller.
C. Compilation fails because of an error in
line 12. Answer: C
D. Compilation fails because of an error in
line 15. 533. Program
E. Compilation fails because of an error in Click the Exhibit button.
line 18. 1. public class A {
2. public void method1() {
Answer: A 3. B b=new B();
4. b.method2();
532. Program 5. // more code here
Given: 6. }
84. try { 7. }
85. ResourceConnection con = 1. public class B {
resourceFactory.getConnection(); 2. public void method2() {
86. Results r = con.query(”GET INFO 3.C c=new C();
FROM CUSTOMER”); 4. c.method3();
87. info = r.getData(); 5. // more code here
88. con.close(); 6. }
89. } catch (ResourceException re) { 7. }
90. errorLog.write(re.getMessage()); 1. public class C {
91. } 2. public void method3() {
92. return info; 3. // more code here
Which is true if a ResourceException is 4. }
thrown on line 86? 5. }
A. Line 92 will not execute. Given:
25. try { 7. }
26. A a=new A(); 1. public class B {
27. a.method1(); 2. public void method2() throws
28. } catch (Exception e) { TestException {
29. System.out.print(”an error occurred”); 3. // more code here
30. } 4. }
Which two are true if a 5. }
NullPointerException is thrown on line 3 of 1. public class TestException extends
class C? (Choose two.) Exception {
A. The application will crash. 2. }
B. The code on line 29 will be executed. Given:
C. The code on line 5 of class A will 31. public void method() {
execute. 32. A a=new A();
D. The code on line 5 of class B will 33. a.method1();
execute. 34. }
E. The exception will be propagated back to Which is true if a TestException is thrown
line 27. on line 3 of class B?
A. Line 33 must be called within a try block.
Answer: BE B. The exception thrown by method1 in
class A is not required to be
534. Program
caught.
Click the Exhibit button.
C. The method declared on line 31 must be
1. public class A {
declared to throw a
2. public void method1() {
RuntimeException.
3. try {
D. On line 5 of class A, the call to method2
4. B b=new B();
of class B does not need to
5. b.method2();
be placed in a try/catch block.
6. // more code here
7. } catch (TestException te) {
Answer: B
8. throw new RuntimeException(te);
9. }
535. Program
6. }
Given: Class B:
11. public static void main(String[] args) { 1. public class B extends A {
12. try { 2. public void doSomething() throws
13. args=null; SomeException { }
14. args[0] = “test”; 3. }
15. System.out.println(args[0]); Which is true about the two classes?
16. } catch (Exception ex) { A. Compilation of both classes will fail.
17. System.out.println(”Exception”); B. Compilation of both classes will succeed.
18. } catch (NullPointerException npe) { C. Compilation of class A will fail.
19. Compilation of class B will succeed.
System.out.println(”NullPointerException”) D. Compilation of class B will fail.
; Compilation of class A will succeed.
20. }
21. } Answer: D
What is the result?
A. test 537. Program
B. Exception Class TestException
C. Compilation fails. 1. public class TestException extends
D. NullPointerException Exception {
2. }
Answer: C Class A:
1. public class A {
536. Program 2.
Click the Exhibit button. 3. public String sayHello(String name)
SomeException: throws TestException {
1. public class SomeException { 4.
2. } 5. if(name == null) {
Class A: 6. throw new TestException();
1. public class A { 7. }
2. public void doSomething() { } 8.
3. } 9. return “Hello “+ name;
7.}
540. Program
8. class SubB2 extends A{
Given:
9.void foo()
11. static void test() throws
{System.out.primtln(“B”);}
RuntimeException {
10.}
12. try {
11. class Tester{
13. System.out.print(”test “);
12. public static void main(String
14. throw new RuntimeException();
args[]){
15. }
13. A a=new SubB2();
16. catch (Exception ex)
14. a.foo();
{ System.out.print(”exception “); }
15.}
17. }
16.}
18. public static void main(String[] args) {
What is the result?
19. try { test(); }
F. B
20. catch (RuntimeException ex)
G. B, followed by Exception
{ System.out.print(”runtime “); }
H. Compilation fails due to an error on line
21. System.out.print(”end “);
9
22. }
I. Compilation fails due to an error on line
What is the result?
14
A. test end
J. An exception is thrown with no other
B. Compilation fails.
output.
C. test runtime end
D. test exception end
Ans : D
E. A Throwable is thrown by main at
runtime. 542. Program
Given:
Which two will produce an AssertionError? An AssertionError is thrown with the word
(Choose two.) “stuff” added to the stack
A. java test trace.
B. java -ea test D. passed
C. java test file1 An AssertionError is thrown without the
D. java -ea test file1 word “stuff” added to the
E. java -ea test file1 file2 stack trace.
F. java -ea:test test file1 E. passed
An AssertionException is thrown with the
Answer: BE word “stuff” added to the
stack trace.
543. Program F. passed
Given: An AssertionException is thrown without
12. public class AssertStuff { the word “stuff” added to the
13. stack trace.
14. public static void main(String [] args) {
16. int y= 7; Answer: C
17. int x=5;
18. assert (x> y): “stuff”; 544. Program
Given: Given:
1. public class Threads2 implements 1. public class TestOne {
Runnable { 2. public static void main (String[] args)
2. throws Exception {
3. public void nun() { 3. Thread.sleep(3000);
4. System.out.println(”run.”); 4. System.out.println(”sleep”);
5. throw new 5. }
RuntimeException(”Problem”); 6. }
6. } What is the result?
7. public static void main(String[] args) { A. Compilation fails.
8. Thread t = new Thread(new Threads2()); B. An exception is thrown at runtime.
9. t.start(); C. The code executes normally and prints
10. System.out.println(”End of method.”); “sleep”.
2 31.
3. private static Object resource = new 32. static class Thread2 extends Thread {
Object(); 33. public void run() {
4. 34. synchronized (resource) {
5. private static void delay(long n) { 35. System.out.print(”Start2 “);
6. try { Thread.sleep(n); } 36. delay(2000);
7. catch (Exception e) 37. System.out.print(”End2 “);
{ System.out.print(”Error “); } 38. }
8. } 39. }
9 40. }
10. public static void main(String[] args) { 41. }
11. System.out.print(”StartMain “); Assume that sleep(n) executes in exactly m
12. new Thread1().start(); milliseconds, and all other
13. delay(1000); code executes in an insignificant amount of
14. Thread t2 = new Thread2(); time. What is the output if
15. t2.start(); the main() method is run?
16. delay(1000); A. Compilation fails.
17. t2.interrupt B. Deadlock occurs.
18. delay(1000); C. StartMain Start1 Error EndMain End1
19. System.out.print(”EndMain “); D. StartMain Start1 EndMain End1 Start2
20. } End2
21. E. StartMain Start1 Error Start2 EndMain
22. static class Thread 1 extends Thread { End2 End1
23. public void run() { F. StartMain Start1 Start2 Error End2
24. synchronized (resource) { EndMain End1
25. System.out.print(”Startl “); G. StartMain Start1 EndMain End1 Start2
26. delay(6000); Error End2
27. System.out.print(”End1 “);
28. } Answer: G
29. }
30. } 562. Program
C. Synchronizing the run() method would 21. final Record a, final Record b, final int
make the class thread-safe. amount) {
D. The data in variable “x” are protected 22. Thread t = new Thread() {
from concurrent access 23. public void run() {
problems. 24. new Clerk().transfer(a, b, amount);
E. Declaring the doThings() method as 25. }
static would make the class 26. };
thread-safe. 27. t.start();
F. Wrapping the statements within 28. }
doThings() in a synchronized(new 29. }
Object()) { } block would make the class 30. class Clerk {
thread-safe. 31. public synchronized void
transfer(Record a, Record b, int amount){
Answer: E 32. synchronized (a) {
33. synchronized (b) {
568. Program 34. a.add(-amount);
Click the Exhibit button. 35. b.add(amount);
10. public class Transfers { 36. }
11. public static void main(String[] args) 37. }
throws Exception { 38. }
12. Record r1 = new Record(); 39. }
13. Record r2 = new Record(); 40. class Record {
14. doTransfer(r1, r2, 5); 41.int num=10;
15. doTransfer(r2, r1, 2); 42. public int get() { return num; }
16. doTransfer(r1, r2, 1); 43. public void add(int n) { num = num + n;
17. // print the result }
18. System.out.println(”rl = “ + r1.get() +“, 44. }
r2=” + r2.get()); If Transfers.main() is run, which three are
19. } true? (Choose three.)
20. private static void doTransfer( A. The output may be “r1 = 6, r2 = 14”.
B. The output may be “r1 = 5, r2 = 15”.
C. The finalize() method for a given Object 13. public Snoochy() { booch = new
is called no more than once by the garbage Boochy(this); }
collector. 14. }
D. The order in which finalize() is called on 15.
two objects is based on the order in which 16. class Boochy {
two objects became finalizable. 17. Snoochy snooch;
Ans :C 18. public Boochy(Snoochy s) { snooch =
s; }
579. Program 19. }
Which two are true? (Choose two.) And the statements:
A. A finalizer may NOT be invoked 21. public static void main(String[] args) {
explicitly. 22. Snoochy snoog = new Snoochy();
B. The finalize method declared in class 23. snoog = null;
Object takes no action. 24. // more code here
C. super.finalize() is called implicitly by any 25. }
overriding finalize method. Which statement is true about the objects
D. The finalize method for a given object referenced by snoog,
will be called no more than snooch, and booch immediately after line 23
once by the garbage collector. executes?
E. The order in which finalize will be called A. None of these objects are eligible for
on two objects is based on garbage collection.
the order in which the two objects became B. Only the object referenced by booch is
finalizable. eligible for garbage
collection.
Answer: BD C. Only the object referenced by snoog is
eligible for garbage
collection.
580. Program
D. Only the object referenced by snooch is
Given:
eligible for garbage
11. class Snoochy {
collection.
12. Boochybooch;
Given:
Answer: E 11. public static void test(String str) {
12. int check = 4;
581. Program 13. if (check = str.length()) {
Given: 14. System.out.print(str.charAt(check -= 1)
1. public class GC { +“, “);
2. private Object o; 15. } else {
3. private void doSomethingElse(Object obj) 16. System.out.print(str.charAt(0) + “, “);
{ o = obj; } 17. }
4. public void doSomething() { 18. }
5. Object o = new Object(); and the invocation:
6. doSomethingElse(o); 21. test(”four”);
7. o = new Object(); 22. test(”tee”);
8. doSomethingElse(null); 23. test(”to”);
9.o=null; What is the result?
10. } A. r, t, t,
11. } B. r, e, o,
When the doSomething method is called, C. Compilation fails.
after which line does the D. An exception is thrown at runtime.
Object created in line 5 become available for Answer: C
garbage collection?
A. Line 5 583. Program
B. Line 6 Given:
C. Line 7 11. public static void test(String str) {
D. Line 8 12. if(str == null | str.lellgth() == 0) {
E. Line 9 13. System.out.println(”String is empty”);
F. Line 10 14. } else {
Answer: BE Answer: A
Given: Given:
11. String test = “This is a test”; 11. String test = “Test A. Test B. Test C.”;
12. String[] tokens = test.split(”\s”); 12. // insert code here
13. System.out.println(tokens.length); 13. String[] result = test.split(regex);
What is the result? Which regular expression inserted at line 12
A. 0 will correctly split test into
B. 1 “Test A,” “Test B,” and “Test C”?
C. 4 A. String regex = “;
D. Compilation fails. B. String regex = “ “;
E. An exception is thrown at runtime. C. String regex = “.*“.
D. String regex = “\\s”
Answer: D E. String regex = “\\.\\s*”;
F. String regex = “\\w[ \.] +“;
593. Program
Given: Answer: E
11. String test= “a1b2c3”;
595. Program
12. String[] tokens = test.split(”\\d”);
Given: