You are on page 1of 219

LARA TECHNOLOGIES LARA TECHNOLOGIES

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

www.laratechnology.com 080-41310124 Page 0


LARA TECHNOLOGIES LARA TECHNOLOGIES

G. The JAR file is located at /foo/myLib.jar


and the Book class is
1. Program
compiled using javac -classpath
A developer is creating a class Book that
/foo/myLib.jar Book.java.
needs to access class Paper.
The Paper class is deployed in a JAR named
Answer: BDG
myLib.jar. Which three,
taken independently, will allow the
2. Program
developer to use the Paper class
Given:
while compiling the Book class? (Choose
1. package com.company.application;
three.)
2.
A. The JAR file is located at
3. public class MainClass {
$JAVA_HOME/jre/classes/myLib.jar.
4. public static void main(String[] args) { }
B. The JAR file is located at
5. }
$JAVA_HOME/jre/lib/ext/myLib.jar.
And MainClass exists in the
C. The JAR file is located at /foo/myLib.jar
/apps/com/company/application directory.
and a classpath
Assume the CLASSPATH environment
environment variable is set that includes
variable is set to “.“ (current
/foo/myLib.jar/Paper.class.
directory). Which two java commands
D. The JAR file is located at /foo/myLib.jar
entered at the command line
and a classpath
will run MainClass? (Choose two.)
environment variable is set that includes
A. java MainClass if run from the /apps
/foo/myLib.jar.
directory
E. The JAR file is located at /foo/myLib.jar
B. java com.company.application.MainClass
and the Book class is
if run from the /apps
compiled using javac -cp
directory
/foo/myLib.jar/Paper Book.java.
C. java -classpath /apps
F. The JAR file is located at /foo/myLib.jar
com.company.application.MainClass if run
and the Book class is
from any directory
compiled using javac -d /foo/myLib.jar
D. java -classpath . MainClass if run from
Book.java.
the

www.laratechnology.com 080-41310124 Page 1


LARA TECHNOLOGIES LARA TECHNOLOGIES

/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

www.laratechnology.com 080-41310124 Page 2


LARA TECHNOLOGIES LARA TECHNOLOGIES

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
{

www.laratechnology.com 080-41310124 Page 3


LARA TECHNOLOGIES LARA TECHNOLOGIES

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(); }
}
}

11. Program 13. Program


Given:
class Manager4
{ 11. public class Ball {
enum E
{

www.laratechnology.com 080-41310124 Page 4


LARA TECHNOLOGIES LARA TECHNOLOGIES

12. public enum Color { RED, GREEN, }


}
BLUE };
13. public void foo() {
14. // insert code here 15. Program

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:

www.laratechnology.com 080-41310124 Page 5


LARA TECHNOLOGIES LARA TECHNOLOGIES

11. public class Test { 16. switch (myDog) {


12. public enum Dogs {collie, harrier, 17. case collie:
shepherd}; 18. System.out.print(”collie “);
13. public static void main(String [] args) { 19. case harrier:
14. Dogs myDog = Dogs.shepherd; 20. System.out.print(”harrier “);
15. switch (myDog) { 21. }
16. case collie: 22. }
17. System.out.print(”collie “); 23. }
18. case default: What is the result?
19. System.out.print(”retriever “); A. collie
20. case harrier: 21. B. harrier
System.out.print(”harrier “); C. Compilation fails.
22. } D. collie harrier
23. } E. An exception is thrown at runtime.
24. }
‘What is the result? Answer: D
A. harrier
18. Program
B. shepherd
C. retriever class Manager8
{
D. Compilation fails.
enum E
E. retriever harrier {
CON(90), TEST(30),
F. An exception is thrown at runtime.
JAVA(90);
E(int i)
Answer: D {
}
}
17. Program public static void main(String[] args)
{
Given: E e1 = E.TEST;
12. public class Test { System.out.println(e1);
}
13. public enum Dogs {collie, harrier}; }
14. public static void main(String [] args) {
19. Program
15. Dogs myDog = Dogs.collie;

www.laratechnology.com 080-41310124 Page 6


LARA TECHNOLOGIES LARA TECHNOLOGIES

Given: 11. public enum Direction { NORTH,


10. public class Fabric SOUTH, EAST, WEST }
11. public enum Color { 12. }
12. RED(0xff0000), GREEN(0x00ff00), 13. public class Sprite{
BLUE(0x0000ff); 14. // insert code here
13. private final int rgb; 15. }
14. Color( int rgb) { this.rgb = rgb; } Which code, inserted at line 14, allows the
15. public int getRGB() { return rgb; } Sprite class to compile?
16. }; A. Direction d = NORTH;
17. public static void main( String[] argv) { B. Nav.Direction d = NORTH;
18. // insert code here C. Direction d = Direction.NORTH;
19. } D. Nav.Direction d =
20. } Nav.Direction.NORTH;
Which two code fragments, inserted
independently at line 18, allow the Answer: D
Fabric class to compile? (Choose two.)
A. Color skyColor = BLUE; 21. Program
B. Color treeColor = Color.GREEN; class Manager9
{
C. Color purple = new Color( 0xff00ff); enum A
D. if( RED.getRGB() < BLUE.getRGB() ) {
CON, TEST(90),
{}
HELLO("ABC");
E. Color purple = Color.BLUE + A()
Color.RED; {
System.out.println("A()");
F. if( Color.RED.ordinal() < }
Color.BLUE.ordinal() ) {} A(int i)
{
System.out.println("A(int)");
Answer: BF }
A(String s1)
{
20. Program
Given:
System.out.println("A(String)");
10. class Nav{
}
}

www.laratechnology.com 080-41310124 Page 7


LARA TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String[] args) 12. MR(”Mr.”), MRS(”Mrs.”), MS(”Ms.”);


{
13. private final String title;
A a1 = A.HELLO;
System.out.println(a1); 14. private Title(String t) { title = t; }
}
15. public String format(String last, String
}
first) {
22. Program
16. return title + “ “ + first + “ “ + last;
class Manager10 17. }
{ 18. }
enum Month
{ 19. public static void main(String[] args) {
JAN(31), FEB(28), 20.
MAR(31); System.out.println(Title.MR.format(”Doe”,
int days;
Month(int days) “John”));
{ 21. }
this.days = days;
} What is the result?
int getDays() A. Mr. John Doe
{
return days; B. An exception is thrown at runtime.
} C. Compilation fails because of an error in
}
public static void main(String[] args) line 12.
{ D. Compilation fails because of an error in
Month m1 = Month.FEB;
System.out.println(m1); line 15.
System.out.println(m1.getDays()); E. Compilation fails because of an error in
System.out.println(m1.days);
System.out.println("--------"); line 20.
Month m2 = Month.JAN;
System.out.println(m2);
System.out.println(m2.getDays()); Answer: A
System.out.println(m2.days);
System.out.println("--------"); 24. Program
}
} class Manager11
{
enum A
23. Program {
CON1, CON2,
Given:
CON3
11. public enum Title { {
void test()

www.laratechnology.com 080-41310124 Page 8


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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;

www.laratechnology.com 080-41310124 Page 9


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ {
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)

www.laratechnology.com 080-41310124 Page 10


LARA TECHNOLOGIES LARA TECHNOLOGIES

public class TerraCarta { 1. package sun.scjp;


public double halfway() 2. public enum Color { RED, GREEN,
{ return Geodetics.DIAMETER/2.0; } } BLUE }
B. import static com.sun.scjp.Geodetics; 1. package sun.beta;
public class TerraCarta { 2. // insert code here
public double halfway() { return 3. public class Beta {
DIAMETER/2.0; } } 4. Color g = GREEN;
C. import static com.sun.scjp.Geodetics. *; 5. public static void main( String[] argv)
public class TerraCarta { 6. { System.out.println( GREEN); }
public double halfway() { return 7. }
DIAMETER/2.0; } } The class Beta and the enum Color are in
D. package com.sun.scjp; different packages.
public class TerraCarta { Which two code fragments, inserted
public double halfway() { return individually at line 2 of the Beta
DIAMETER/2.0; } } declaration, will allow this code to compile?
(Choose two.)
Answer: AC A. import sun.scjp.Color.*;
B. import static sun.scjp.Color.*;
34. Program C. import sun.scjp.Color; import static
sun.scjp.Color.*;
package pack2;
D. import sun.scjp.*; import static
class Manager5
{ sun.scjp.Color.*;
public static void main(String[] args)
E. import sun.scjp.Color; import static
{
System.out.println("1111"); sun.scjp.Color.GREEN;
System.exit(0);
System.out.println("2222");
} Answer: CE
}

36. Program
35. Program
package pack2;
import static java.lang.System.out;
Given: import static java.lang.System.exit;
class Manager6

www.laratechnology.com 080-41310124 Page 11


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ //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.*;

www.laratechnology.com 080-41310124 Page 12


LARA TECHNOLOGIES LARA TECHNOLOGIES

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[])

www.laratechnology.com 080-41310124 Page 13


LARA TECHNOLOGIES LARA TECHNOLOGIES

43. Program Employee emp = new Employee(x);


print(emp);
package pack2; System.out.println("----");
class Job print(emp);
{ System.out.println("----");
String title; print(emp);
double salary; System.out.println("----");
Job(String title, double salary) }
{ private static void print(Employee
this.title = title; emp)
this.salary = salary; {
} Job j1 = null;
} while(emp.hasNext())
class Employee {
{ j1 = emp.nextJob();
private int index; System.out.println(j1.title +
Job x[];
Employee(Job x[]) ":" + j1.salary);
{ }
this.x = x; }
} }
Job nextJob()
{
return x[index ++]; 44. Program
}
boolean hasNext() package pack3;
{ class Job
if(index == x.length) {
{ String title;
index = 0; double salary;
return false; Job(String title, double salary)
} {
return true; this.title = title;
} this.salary = salary;
} }
class Manager }
{ class Employee
public static void main(String[] args) {
{ Job x[];
Job j1 = new Job("software", Employee(Job x[])
50000.9); {
Job j2 = new Job("hardware", this.x = x;
}
10000.9); Iterator getIterator()
Job j3 = new Job("hr", 500.9); {
Job x[] = {j1, j2, j3}; Iterator it = new Iterator(x);

www.laratechnology.com 080-41310124 Page 14


LARA TECHNOLOGIES LARA TECHNOLOGIES

return it; j1 = it.nextJob();


}
}
System.out.println(j1.title + ":" +
class Iterator
{ j1.salary);
private int index; }
Job x[]; }
Iterator(Job x[]) }
{
this.x = x;
} 45. Program
Job nextJob()
{
package pack4;
return x[index ++];
class Job
}
{
boolean hasNext()
String title;
{
double salary;
return index < x.length;
Job(String title, double salary)
}
{
}
this.title = title;
class Manager
this.salary = salary;
{
}
public static void main(String[] args)
}
{
Job j1 = new Job("software",
class Employee
50000.9); {
Job j2 = new Job("hardware", Job x[];
Employee(Job x[])
10000.9);
{
Job j3 = new Job("hr", 500.9);
this.x = x;
ob x[] = {j1, j2, j3};
}
Employee emp = new Employee(x);
Iterator getIterator()
print(emp);
{
System.out.println("----");
Iterator it = new Iterator(x);
print(emp);
return it;
System.out.println("----");
}
print(emp);
}
System.out.println("----");
class Iterator
}
{
private static void print(Employee
private static int index;
emp) Job x[];
{ Iterator(Job x[])
Iterator it = emp.getIterator(); {
Job j1 = null; this.x = x;
while(it.hasNext()) }
{ Job nextJob()

www.laratechnology.com 080-41310124 Page 15


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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;

www.laratechnology.com 080-41310124 Page 16


LARA TECHNOLOGIES LARA TECHNOLOGIES

Job j1 = new Job("software", }


class Employee
50000.9);
{
Job j2 = new Job("hardware",
Job x[];
10000.9); Employee(Job x[])
Job j3 = new Job("hr", 500.9); {
Job x[] = {j1, j2, j3}; this.x = x;
Employee emp = new Employee(x); }
print(emp); Iterator getIterator()
System.out.println("----"); {
print(emp); Iterator it = new Iterator(x);
System.out.println("----"); return it;
print(emp); }
System.out.println("----"); static class Iterator
} {
private static void print(Employee private static int index;
Job x[];
emp)
Iterator(Job x[])
{
{
Employee.Iterator it
this.x = x;
=emp.getIterator(); }
Job j1 = null; Job nextJob()
while(it.hasNext()) {
{ return x[index ++];
j1 = it.nextJob(); }
boolean hasNext()
{
System.out.println(j1.title + ":" +
return index <
j1.salary);
x.length;
}
}
}
}
}
}//end of employee
class Manager
{
47. Program public static void main(String[] args)
{
package pack6; Job j1 = new Job("software",
class Job
50000.9);
{
Job j2 = new Job("hardware",
String title;
double salary; 10000.9);
Job(String title, double salary) Job j3 = new Job("hr", 500.9);
{ Job x[] = {j1, j2, j3};
this.title = title; Employee emp = new Employee(x);
this.salary = salary; print(emp);
} System.out.println("----");

www.laratechnology.com 080-41310124 Page 17


LARA TECHNOLOGIES LARA TECHNOLOGIES

print(emp); 49. Program


System.out.println("----");
print(emp); class B
System.out.println("----"); {
} class C{}
private static void print(Employee static class D{}
emp) void test1()
{ {
Employee.Iterator it C c1 = new C();
D d1 = new D();
=emp.getIterator(); }
Job j1 = null; static void test2()
while(it.hasNext()) {
{ //C c1 = new C();
j1 = it.nextJob(); D d1 = new D();
System.out.println(j1.title + ":" + }
j1.salary); }
}
} 50. Program
}
class C
{
48. Program int i;
static int j;
class A class D
{ {
int i; }
static int j; static class E
void test1() {
{ }
i = 20; void test1()
j = 30; {
test1(); i = 1;
test2(); j = 2;
} D d1 = new D();
static void test2() E e1 = new E();
{ test1();
//i = 20; test2();
j = 30; }
//test1(); static void test2()
test2(); {
} i = 1;
} j = 2;
D d1 = new D();
E e1 = new E();
test1();

www.laratechnology.com 080-41310124 Page 18


LARA TECHNOLOGIES LARA TECHNOLOGIES

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 {

www.laratechnology.com 080-41310124 Page 19


LARA TECHNOLOGIES LARA TECHNOLOGIES

//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 {

www.laratechnology.com 080-41310124 Page 20


LARA TECHNOLOGIES LARA TECHNOLOGIES

11. public class Point { public int x,y; } variable base?


12. public Point getPoint() { return new A. It can be any class.
Point(); } B. No class has access to base.
13. } C. The class must belong to the geometry
14. class Triangle { package.
15. public Triangle() { D. The class must be a subclass of the class
16. // insert code here Hypotenuse.
17. }18. }
Which code, inserted at line 16, correctly Answer: C
retrieves a local instance of a
Point object? 58. Program
A. Point p = Line.getPoint();
class H
B. Line.Point p = Line.getPoint();
{
C. Point p = (new Line()).getPoint(); class I
{
D. Line.Point p = (new Line()).getPoint();
}
static class J
{
Answer: D
}
}
class Manager
57. Program {
Given: public static void main(String[] args)
{
1. package geometry; H.I obj1 = null;
2. public class Hypotenuse { H.J obj2 = null;
obj1 = new H().new I();
3. public InnerTriangle it = new obj2 = new H.J();
InnerTriangle(); H h1 = new H();
obj1 = h1.new I();
4. class InnerTriangle { System.out.println("done");
5. public int base; }
}
6. public int height;
7. }
59. Program
8. }
Which is true about the class of an object class I
that can reference the {
public static void main(String[] args)

www.laratechnology.com 080-41310124 Page 21


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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("----"); }

www.laratechnology.com 080-41310124 Page 22


LARA TECHNOLOGIES LARA TECHNOLOGIES

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

www.laratechnology.com 080-41310124 Page 23


LARA TECHNOLOGIES LARA TECHNOLOGIES

public class B class A


{ {
public static int i=10; static int i;
public static void main(String[] args) }
{ System.out.println("done");
class C }
{ }
private C()
{
System.out.println("local"); 70. Program
}
}
class A
C c1=new C();
{
System.out.println("main");
void test1()
}
{
}
System.out.println("A-
test1");
68. Program }
void test2()
class O {
{ System.out.println("A-
public static void main(String[] args)
test2");
{
}
int i = 10;
}
final int j = 20;
class A
{
void test() 71. Program
{
//System.out.println(i) abstract class B
{
;
abstract void test1();
System.out.println(j);
void test2()
}
{
}
System.out.println("B-
System.out.println("done");
} test2");
} }
}

69. Program
72. Program
class P
{ interface C
public static void main(String[] args) {
{ void test1();

www.laratechnology.com 080-41310124 Page 24


LARA TECHNOLOGIES LARA TECHNOLOGIES

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()

www.laratechnology.com 080-41310124 Page 25


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ {
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.

www.laratechnology.com 080-41310124 Page 26


LARA TECHNOLOGIES LARA TECHNOLOGIES

10. interface Foo { C. If lines 16, 17 and 18 were removed,


11. int bar(); compilation would fail.
12. } D. If lines 24, 25 and 26 were removed,
13. compilation would fail.
14. public class Beta { E. If lines 16, 17 and 18 were removed, the
15. code would compile and
16. class A implements Foo { the output would be 2.
17. public int bar() { return 1; } F. If lines 24, 25 and 26 were removed, the
18. } code would compile and
19. the output would be 1.
20. public int fubar( Foo foo) { return
foo.bar(); } Answer: BEF
21.
81. Program
22. public void testFoo() {
23. class Manager8
{
24. class A implements Foo {
public static void main(String[] args)
25. public int bar() { return 2; } {
C c1 = new C();
26. }
c1.test1();
27. c1.test2();
System.out.println("done");
28. System.out.println( fubar( new A()));
}
29. } }
30.
82. Program
31. public static void main( String[] argv) {
32. new Beta().testFoo(); class Manager9
{
33. } public static void main(String[] args)
34. } {
C c1 = new C()
Which three statements are true? (Choose {
three.) public void test1()
{
A. Compilation fails. System.out.println("AIC-
B. The code compiles and the output is 2. test1");
}
public void test2()

www.laratechnology.com 080-41310124 Page 27


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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(); }

www.laratechnology.com 080-41310124 Page 28


LARA TECHNOLOGIES LARA TECHNOLOGIES

public void test2() {


{ System.out.println(3);
}
}
System.out.println("AIC-test2");
B b1 = new B()
}
{
};
void test()
method2(c1);
{
method1(new B()
System.out.println(2);
{
}
void test1()
};
{
b1.test();
System.out.println("AIC-
b1.main(args);
test1"); }
}
}); }
method2(new C() 87. Program
{
public void test1() class Manager13
{ {
System.out.println("AIC- public static void main(String[] args)
{
test1");
B b1 = method1();
}
C c1 = method2();
public void test2()
b1.test1();
{
b1.test2();
System.out.println("AIC-
c1.test1();
test2"); c1.test2();
} System.out.println("done");
}); }
System.out.println("done"); static B method1()
} {
} B b1 = new B()
{
void test1()
86. Program {
class A System.out.println("AIC-
test1");
{ }
public static void main(String[] args) };
{ return b1;
System.out.println(1); }
abstract class B static C method2()
{ {
abstract void test(); return new C()
public void main(String[] array) {

www.laratechnology.com 080-41310124 Page 29


LARA TECHNOLOGIES LARA TECHNOLOGIES

public void test1() class Manager14


{ {
System.out.println("AIC- public static void main(String[] args)
{
test1");
Fan f1 = new Fan();
}
Switch s1 = f1.getSwitch();
public void test2()
s1.on();
{
System.out.println("AIC-
System.out.println(f1.getRunningStatus());
test2");
s1.off();
}
};
} System.out.println(f1.getRunningStatus());
}
} }

88. Program 89. Program


Given:
interface Switch
{ 1. interface TestA { String toString(); }
void on(); 2. public class Test {
void off();
} 3. public static void main(String[] args) {
class Fan 4. System.out.println(new TestA() {
{
private boolean runningStatus; 5. public String toString() { return “test”; }
public Switch getSwitch() 6. });
{
return new Switch() 7. }
{ 8. }
public void on()
{ What is the result?
runningStatus = true; A. test
}
public void off() B. null
{ C. An exception is thrown at runtime.
runningStatus = false;
} D. Compilation fails because of an error in
}; line 1.
}
public boolean getRunningStatus() E. Compilation fails because of an error in
{ line 4.
return runningStatus;
} F. Compilation fails because of an error in
} line 5.

www.laratechnology.com 080-41310124 Page 30


LARA TECHNOLOGIES LARA TECHNOLOGIES

}
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()");

www.laratechnology.com 080-41310124 Page 31


LARA TECHNOLOGIES LARA TECHNOLOGIES

93. Program System.out.println("done");


}
class B extends A }
{
@Override
void test1() 96. Program
{
System.out.println("B- class Manager2
test1"); {
} public static void main(String[] args)
{
void test2() Thread t1 = new Thread();
{ t1.stop();
System.out.println("B- System.out.println("done");
}
test2"); }
}
}
97. Program

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();

www.laratechnology.com 080-41310124 Page 32


LARA TECHNOLOGIES LARA TECHNOLOGIES

list.add(90); <echo message="cleaning


list.add(90);
started"/>
System.out.println("done");
<delete dir="classes"/>
}
<mkdir dir="classes"/>
}
<delete dir="lib"/>
<mkdir dir="lib"/>
99. Program <delete dir="docs"/>
<mkdir dir="docs"/>
import java.util.ArrayList; </target>
class Manager5 <target name="compile"
{
depends="cleanup">
@SuppressWarnings({"unchecked",
<echo message="compilation
"deprecation"})
started"/>
public static void main(String[] args)
<javac srcdir="src"
{
ArrayList list = new destdir="classes"/>
</target>
ArrayList();
list.add(90);
<target name="jar"
list.add(90);
depends="compile">
Thread t1 = new Thread(); <echo message="making a
t1.stop();
jar started"/>
<jar basedir="classes"
C c1 = new C();
c1.test1();
System.out.println("Hello destfile="lib/mgr.jar"/>
</target>
World!");
<target name="doc-creation"
}
} depends="jar">
<javadoc destdir="docs"
sourcepath="src"
100. Build.xml packagenames="com.lara"/>
</target>
<project default="run"> <target name="run" depends="doc-
<target name="setup"> creation">
<echo message="setup is <java classpath="classes"
starting"/>
<mkdir dir="src"/> classname="com.lara.Manager"/>
<mkdir dir="classes"/> </target>
<mkdir dir="lib"/> </project>
<mkdir dir="docs"/>
</target>
<target name=s"cleanup"> 101. Program

www.laratechnology.com 080-41310124 Page 33


LARA TECHNOLOGIES LARA TECHNOLOGIES

package com.lara; double d=obj2.doubleValue();


public class M1 Long obj3=new Long(s1);
{ Long l1=obj3.longValue();
public static void main(String[] args)
System.out.println("done");
{
}
int i=0;
}
Integer obj=new Integer(i);
int k=obj.intValue();
105. Program
System.out.println("done");
package com.lara;
}
public class M5
}
{
102. Program
public static void main(String[] args)
package com.lara;
{
public class M2
String s1="true";
{
String s2="abc";
public static void main(String[] args)
Boolean b1=new Boolean(s1);
{
Boolean b2=new Boolean(s2);
Double d1=new Double(10.09);
System.out.println(b1);
double d2=d1.doubleValue();
System.out.println(b2);
System.out.println("done");
}
}
}
}
106. Program
103. Program
package com.lara;
package com.lara;
public class M6
public class M3
{
{
public static void main(String[] args)
public static void main(String[] args)
{
{
String s1="abc";
char c1='a';
Boolean obj=new Boolean(s1);
Character c2=new Character('a');
System.out.println(obj);
char c3=c2.charValue();
System.out.println();
}
}
}
}
107. Program
package com.lara;
104. Program
public class M7
package com.lara;
{
public class M4
public static void main(String[] args)
{
{
public static void main(String[] args)
String s1="abc";
{
Integer obj=new Integer(s1);
String s1="10";
System.out.println(obj);
Integer obj=new Integer(s1);
}
int i=obj.intValue();
}
Double obj2=new Double(s1);

www.laratechnology.com 080-41310124 Page 34


LARA TECHNOLOGIES LARA TECHNOLOGIES

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");

www.laratechnology.com 080-41310124 Page 35


LARA TECHNOLOGIES LARA TECHNOLOGIES

} public static void main(String[] args)


} {
Integer obj=new Integer(10);
test(obj);
114. Program }
package com.lara; static void test(int i)
public class M12 {
{ System.out.println("done");
public static void main(String[] args) }
{ }
Integer obj=10; 118. Program
int i=obj; //JDK1.6
System.out.println("done"); package com.lara;
} public class M16
} {
public static void main(String[] args)
115. Program {
//Jdk 1.4 Integer obj=new Integer(10);
package com.lara; test(obj.intValue());
public class M13 }
{ static void test(int i)
public static void main(String[] args) {
{ System.out.println("done");
Integer obj=10; }
int i=obj; }
System.out.println("done");
} 119. Program
} class Test
{
116. Program public static void main(String args[])
//Jdk 1.4 {
package com.lara; Integer i1=4678;
public class M14 Integer i2=4678;
{ if(i1==i2)
public static void main(String[] args) {
{ System.out.println(“same Objects”);
Integer obj=new Integer(10); }
int i=obj.intValue(); else
System.out.println("done"); {
} System.out.println(“Different Objects”);
} }
117. Program if(i1.equals(i2))
//Jdk 1.6 {
package com.lara; System.out.println(“Meaningfully equal”);
public class M15 }
{ else

www.laratechnology.com 080-41310124 Page 36


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 12. public Integer startingI;


System.out.println(“not equal”);
13. public void methodA() {
}
14. Integer i = new Integer(25);
}
} 15. startingI = i;
16. methodB(i);
120. Program 17. }
Given: 18. private void methodB(Integer i2) {
11. class Converter { 19. i2 = i2.intValue();
12. public static void main(String[] args) { 20.
13. Integer i = args[0]; 21. }
14. int j = 12; 22. }
15. System.out.println(”It is “ + (j==i) + If methodA is invoked, which two are true at
“that j==i.”); line 20? (Choose two.)
16. } A. i2 == startingI returns true.
17. } B. i2 == startingI returns false.
What is the result when the programmer C. i2.equals(startingI) returns true.
attempts to compile the code D. i2.equals(startingI) returns false.
and run it with the command java Converter
12? Answer: BC
A. It is true that j==i.
B. It is false that j==i. 122. Program
C. An exception is thrown at runtime. //JDK1.6
package com.lara;
D. Compilation fails because of an error in public class M17
line 13. {
public static void main(String[] args)
{
Answer: D int i=test();
}
static Integer test()
{
121. Program
return 20;
Given: }
}
10. public class MyClass {
11. 123. Program

www.laratechnology.com 080-41310124 Page 37


LARA TECHNOLOGIES LARA TECHNOLOGIES

//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;
}

www.laratechnology.com 080-41310124 Page 38


LARA TECHNOLOGIES LARA TECHNOLOGIES

public class M23 public class M26


{ {
public static void main(String[] args) static void test(int i)
{ {
char c1=new Character('a'); System.out.println("int");
Character c2=c1; }
System.out.println("done"); static void test(Double d)
{
}
}
System.out.println("Double");
129. Program
}
//JDK1.4
public static void main(String[] args)
package com.lara;
{
public class M24
Integer obj = new Integer(90);
{
test(obj);
public static void main(String[] args)
double d1 = 90.90;
{
test(d1);
char c1=new
}
Character('a').charValue(); }
Character c2=new Character(c1); 132. Program
System.out.println("done");
Given
}
}
130. Program
1. public class BuildStuff{
package com.lara;
public class M25 2. public static void main (String
{
args[]) {
static void test(int i)
{ 3. Boolean test=new Boolean(true);
System.out.println("int");
4. Integer x=343;
}
static void test(Integer i) 5.Integer y =new BildStuff().go(test,x);
{
6. System.out.println(y);
System.out.println("Integer");
} 7.}
public static void main(String[] args)
8. int go(Boolean b,int i){
{
int i = 10; 9. if(b) return (i/7);
test(i);
10. return (i/49);
Integer obj = new Integer(90);
test(obj); What is the result?
}
A. 7
}
131. Program B. 49
package com.lara;
C. 343

www.laratechnology.com 080-41310124 Page 39


LARA TECHNOLOGIES LARA TECHNOLOGIES

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"); }
}

static void test(double b)


{ 134. Program
System.out.println("double");
} Given:
static void test(Byte b) 11. public class Yikes {
{ 12.
System.out.println("Byte");
} 13. public static void go(Long n)
{System.out.println(”Long “);}
static void test(Integer b)

www.laratechnology.com 080-41310124 Page 40


LARA TECHNOLOGIES LARA TECHNOLOGIES

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?
17. short y= 6; A. short LONG
18. long z= 7; B. SHORT LONG
19. go(y); C. Compilation fails.
20. go(z); D. An exception is thrown at runtime.
21. }
22. } Answer: C
What is the result?
A. int Long 136. Program
B. Short Long package com.lara;
public class M28
C. Compilation fails. {
D. An exception is thrown at runtime. static void test()
{
System.out.println("test()");
Answer: A }

static void test(int ... x)


{
135. Program
System.out.println("int ... ");
Given: }
public static void main(String[] args)
12. public class Wow {
{
13. public static void go(short n) test();
test(10);
{System.out.println(”short”); }
test(20, 30);
14. public static void go(Short n) test(20, 30, 3, 50, 60);
}
{System.out.println(”SHORT”);}
}
15. public static void go(Long n)
{System.out.println(” LONG”); }
137. Program
16. public static void main(String [] args) { package com.lara;
public class M29
17. Short y= 6;
{
18.int z=7; static void test(int ... x)

www.laratechnology.com 080-41310124 Page 41


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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. }

www.laratechnology.com 080-41310124 Page 42


LARA TECHNOLOGIES LARA TECHNOLOGIES

A programmer needs to create a logging B. for(int z : x) System.out.println(z);


method that can accept an C. while( x.hasNext())
arbitrary number of arguments. For System.out.println( x.next());
example, it may be called in these D. for( int i=0; i< x.length; i++ )
ways: System.out.println(x[i]);
logIt(”log message 1 “);
logIt(”log message2”,”log message3”); Answer: BD
logIt(”log message4”, “log message5”, “log
message6); 143. Program
Which declaration satisfies this package com.lara;
public class M32
requirement? {
A. public void logIt(String * msgs) static void test1(String s1, int ... i)
{
B. public void logIt(String [] msgs)
C. public void logIt(String... msgs) }
static void test2(String ... s1, byte b)
D. public void logIt(String msg1, String {
msg2, String msg3)
}
}
Answer: C 144. Program

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

www.laratechnology.com 080-41310124 Page 43


LARA TECHNOLOGIES LARA TECHNOLOGIES

C. world world B.public static void main(String.*a)


D. Compilation fails C.public static void main(String… a)
E. An exception is thrown at runtime. D.public static void main(String[]…a)
Ans :D E.public static void main(String…[]a)

145. Program How many of the code fragments, inserted


package com.lara; independently at line 12, compile?
public class M33
{ A. 0
static void test(int ... x) B. 1
{
C. 2
} D. 3
static void test(int[] y)
{ E. 4
F. 5
}
} Ans: D

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)

www.laratechnology.com 080-41310124 Page 44


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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

www.laratechnology.com 080-41310124 Page 45


LARA TECHNOLOGIES LARA TECHNOLOGIES

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. }

www.laratechnology.com 080-41310124 Page 46


LARA TECHNOLOGIES LARA TECHNOLOGIES

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;

E. An exception is thrown at runtime.


public class A
{
public static void main(String[] args)
Answer: B
{
System.out.println(1);
int i = 10/0;
System.out.println(2);
158. Program }
Given: }

11. public class Commander {


12. public static void main(String[] args) { 160. Program

13. String myProp = /* insert code here */


package com.lara;
14. System.out.println(myProp); public class B
15. } {
public static void main(String[] args)
16. } {
and the command line: System.out.println(1);
test();
java -Dprop.custom=gobstopper System.out.println(2);
Commander }
static void test()
Which two, placed on line 13, will produce {
the output gobstopper? System.out.println(3);
int i = 10/0;
(Choose two.) System.out.println(4);
A. System.load(”prop.custom”); }
}
B. System.getenv(”prop.custom”);
C. System.property(”prop.custom”);
161. Program
D. System.getProperty(”prop.custom”);

www.laratechnology.com 080-41310124 Page 47


LARA TECHNOLOGIES LARA TECHNOLOGIES

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

162. Program package com.lara;


public class F
package com.lara; {
public class D public static void main(String[] args)
{ {
static void test1() System.out.println(100);
{ E.print();
System.out.println(1); System.out.println(200);
test2(); }
System.out.println(2); }
}
static void test2() 165. Program
{
System.out.println(3); package com.lara;
int i = public class G
Integer.parseInt("abc"); {
System.out.println(4); public static void main(String[] args)
} {
public static void main(String[] args) System.out.println(1);

www.laratechnology.com 080-41310124 Page 48


LARA TECHNOLOGIES LARA TECHNOLOGIES

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;
{

www.laratechnology.com 080-41310124 Page 49


LARA TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(3); 171. Program


test(); package com.lara;
System.out.println(4); public class K
} {
} public static void main(String[] args)
{
169. Program System.out.println(1);

package com.lara; try


public class I {
{ System.out.println(2);
public static void main(String[] args) int i = 10/0;
{ System.out.println(3);
System.out.println(1); }
int x[] = new int[999999999]; catch(ArithmeticException
System.out.println(2);
} ex)
} {
System.out.println(4);
System.out.println(ex.getMessage());
170. Program
System.out.println(5);
}
package com.lara; System.out.println(6);
public class J }
{ }
public static void main(String[] args)
{ Program
System.out.println(1); package com.lara;
try public class L
{ {
int i = 10/0; public static void main(String[] args)
} {
catch(ArithmeticException try
ex) {
{ System.out.println(1);
System.out.println(2); int i = 10/0;
System.out.println(2);
}
System.out.println(ex); catch(ArithmeticException
System.out.println(3);
} ex)
System.out.println(4); {
} System.out.println(3);
} ex.printStackTrace();
System.out.println(4);
}
System.out.println(5);

www.laratechnology.com 080-41310124 Page 50


LARA TECHNOLOGIES LARA TECHNOLOGIES

} }
} 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);

www.laratechnology.com 080-41310124 Page 51


LARA TECHNOLOGIES LARA TECHNOLOGIES

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;

www.laratechnology.com 080-41310124 Page 52


LARA TECHNOLOGIES LARA TECHNOLOGIES

import java.util.Scanner; public class T


public class S {
{ public static void main(String[] args)
public static void main(String[] args) {
{ Scanner sc = new
Scanner sc = new
Scanner(System.in);
Scanner(System.in); System.out.println("enter some
System.out.println("enter some
thing");
thing"); String s1 = sc.next();
String s1 = sc.next(); try
try {
{ System.out.println(1);
System.out.println(1); int i = Integer.parseInt(s1);
int i = Integer.parseInt(s1); System.out.println(2);
System.out.println(2); int k = i/ (i - 9);
int k = i / (i - 9); System.out.println(3);
System.out.println(3); }
}
catch(ArithmeticException
catch(NumberFormatException ex)
ex) {
{ System.out.println(4);
System.out.println(4);
System.out.println(ex);
System.out.println(ex); System.out.println(5);
System.out.println(5); }
} finally
{
System.out.println("finally");
catch(NumberFormatException ex)
}
{
System.out.println(6);
System.out.println(6);
}
}
System.out.println(ex);
System.out.println(7);
} 180. Program
System.out.println(8);
}
package com.lara;
}
import java.util.Scanner;
public class U
{
179. Program public static void main(String[] args)
{
package com.lara; Scanner sc = new
import java.util.Scanner;
Scanner(System.in);

www.laratechnology.com 080-41310124 Page 53


LARA TECHNOLOGIES LARA TECHNOLOGIES

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;

www.laratechnology.com 080-41310124 Page 54


LARA TECHNOLOGIES LARA TECHNOLOGIES

public class W System.out.println(4);


{ }
public static void main(String[] args) finally
{ {
if(true) System.out.println(5);
{ }
return; System.out.println(6);
} }
try }
{
System.out.println(1);
return; 185. Program
}
catch(ArithmeticException
package com.lara;
ex) public class Y
{ {
System.out.println(2); public static void main(String[] args)
} {
finally System.out.println(1);
{ try
System.out.println(3); {
} System.out.println(1);
System.out.println(4); int i = 10/0;
} System.out.println(2);
} }
catch(ArithmeticException
ex)
184. Program {
System.out.println(3);
package com.lara; int i = 20/0;
public class X System.out.println(4);
{ }
public static void main(String[] args) finally
{ {
System.out.println(1); System.out.println(5);
int i = }
System.out.println(6);
Integer.parseInt("abc");
System.out.println(2);
}
try
}
{
System.out.println(3);
}
catch(NullPointerException 186. Program

ex) Given:
{

www.laratechnology.com 080-41310124 Page 55


LARA TECHNOLOGIES LARA TECHNOLOGIES

11. public static void parse(String str) { System.out.println(3);


}
12. try {
catch(ArithmeticException
13. float f= Float.parseFloat(str);
ex)
14. } catch (NumberFormatException nfe) { {
System.out.println(4);
15. f= 0;
}
16. } finally { finally
{
17. System.out.println(f);
System.out.println(5);
18. } }
System.out.println(6);
19. }
}
20. public static void main(String[] args) { }
21. parse(”invalid”);
22. } 188. Program
What is the result? Given:
A. 0.0 33. try {
B. Compilation fails. 34. // some code here
C. A ParseException is thrown by the parse 35. } catch (NullPointerException e1) {
method at runtime. 36. System.out.print(”a”);
D. A NumberFormatException is thrown by 37. } catch (RuntimeException e2) {
the parse method at 38. System.out.print(”b”);
runtime. 39. } finally {
40. System.out.print(”c”);
Answer: B 41. }
What is the result if a NullPointerException
187. Program occurs on line 34?
A. c
package com.lara;
public class Z B. a
{ C. ab
public static void main(String[] args)
{ D. ac
System.out.println(1); E. bc
try
{ F. abc
System.out.println(2);
System.exit(0);

www.laratechnology.com 080-41310124 Page 56


LARA TECHNOLOGIES LARA TECHNOLOGIES

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)

www.laratechnology.com 080-41310124 Page 57


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ }
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;

www.laratechnology.com 080-41310124 Page 58


LARA TECHNOLOGIES LARA TECHNOLOGIES

// } }
// 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;
}

www.laratechnology.com 080-41310124 Page 59


LARA TECHNOLOGIES LARA TECHNOLOGIES

// //some stmts // return 40;


// }
// } // catch(ArithmeticException
// catch(NullPointerException
ex)
ex) // {
// { // return 50;
// // }
// } // return 70;
// catch(ArithmeticException // }
ex)
}
// {
// return 30;
// }
// } 192. Program
int test6()
{ package com.lara;
try public class D
{ {
//some stmts int test1()
{
} try
catch(NullPointerException {
//some stmts
ex)
}
{
catch(ArithmeticException
} ex)
catch(ArithmeticException {
ex)
}
{
finally
return 30;
{
}
return 1000;
}
}
return 10;
}
// int test7()
int test2()
// {
{
// try
try
// {
{
// //some stmts
//some stmts
// return 30;
}
// }
catch(ArithmeticException
// catch(NullPointerException
ex)
ex)
{
// {

www.laratechnology.com 080-41310124 Page 60


LARA TECHNOLOGIES LARA TECHNOLOGIES

} // 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

www.laratechnology.com 080-41310124 Page 61


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ {
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()

www.laratechnology.com 080-41310124 Page 62


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ // {
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)

www.laratechnology.com 080-41310124 Page 63


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ // }
// }
}
} void test21()
{
// void test18() try
// { {
// Class.forName("");
DriverManager.getConnection("");
// } DriverManager.getConnection("");
}
void test18() catch(SQLException ex)
{ {
try
{ }

DriverManager.getConnection(""); catch(ClassNotFoundException ex)


} {
catch(SQLException ex)
{ }
}
}
} // void test22()
// {
// void test19() // new FileWriter("");
// { // }
// try
// { void test23()
// {
// } try
// catch(SQLException ex) {
// { new FileWriter("");
// }
// } catch(IOException ex)
// } {

// void test20() }
// { }
// try
// { // void test24()
// Class.forName(""); // {
// } // try
// catch(SQLException ex) // {
// { //
// // }

www.laratechnology.com 080-41310124 Page 64


LARA TECHNOLOGIES LARA TECHNOLOGIES

// 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("");
// } }

www.laratechnology.com 080-41310124 Page 65


LARA TECHNOLOGIES LARA TECHNOLOGIES

catch(ParseException ex) System.out.println(3);


{ int i = 10/0;
ex.printStackTrace(); System.out.println(4);
} }
} }
// void test33()
// {
// try
// {
196. Program
//
package com.lara;
// }
// catch(ParseException ex)
public class H
// {
{
//
public static void main(String[] args)
// }
{
// }
System.out.println(1);
}
test();
System.out.println(2);
}
194. Program static void test()
{
package com.lara; System.out.println(3);
public class F try
{ {
public static void main(String[] args) int i = 10/0;
{ }
System.out.println(1); catch(ArithmeticException
int i = 10/0;
ex)
System.out.println(2);
{
}
System.out.println(4);
}
}
System.out.println(5);
}
195. Program }
package com.lara;
197. Progarm
public class G package com.lara;
{ public class I
public static void main(String[] args)
{
{
System.out.println(1);
public static void main(String[] args)
test();
{
System.out.println(2);
System.out.println(1);
}
try
static void test()
{
{

www.laratechnology.com 080-41310124 Page 66


LARA TECHNOLOGIES LARA TECHNOLOGIES

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);
} }

www.laratechnology.com 080-41310124 Page 67


LARA TECHNOLOGIES LARA TECHNOLOGIES

}
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);

www.laratechnology.com 080-41310124 Page 68


LARA TECHNOLOGIES LARA TECHNOLOGIES

}
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();

www.laratechnology.com 080-41310124 Page 69


LARA TECHNOLOGIES LARA TECHNOLOGIES

} 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();
}

www.laratechnology.com 080-41310124 Page 70


LARA TECHNOLOGIES LARA TECHNOLOGIES

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(); }

www.laratechnology.com 080-41310124 Page 71


LARA TECHNOLOGIES LARA TECHNOLOGIES

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 {

www.laratechnology.com 080-41310124 Page 72


LARA TECHNOLOGIES LARA TECHNOLOGIES

2. public void method3() { {


try
3. // more code here
{
4. } R r1 = new R();
}
5. }
Given:
catch(ClassNotFoundException ex)
25. try { {
26. A a=new A();
}
27. a.method1(); }
static void test() throws
28. } catch (Exception e) {
ClassNotFoundException
29. System.out.print(”an error occurred”);
{
30. } R r1 = new R();
}
Which two are true if a
}
NullPointerException is thrown on line 3 of
214. Program
class C? (Choose two.)
Click the Exhibit button.
A. The application will crash.
1. public class A {
B. The code on line 29 will be executed.
2. public void method1() {
C. The code on line 5 of class A will
3. try {
execute.
4. B b=new B();
D. The code on line 5 of class B will
5. b.method2();
execute.
6. // more code here
E. The exception will be propagated back to
7. } catch (TestException te) {
line 27.
8. throw new RuntimeException(te);
9. }
Answer: BE
6. }
7. }
213. Program
package com.lara; 1. public class B {
public class R 2. public void method2() throws
{
R() throws ClassNotFoundException TestException {
{ 3. // more code here
} 4. }
public static void main(String[] args)

www.laratechnology.com 080-41310124 Page 73


LARA TECHNOLOGIES LARA TECHNOLOGIES

5. } 16. } catch (Exception ex) {


1. public class TestException extends 17. System.out.println(”Exception”);
Exception { 18. } catch (NullPointerException npe) {
2. } 19.
Given: System.out.println(”NullPointerException”)
31. public void method() { ;
32. A a=new A(); 20. }
33. a.method1(); 21. }
34. } What is the result?
Which is true if a TestException is thrown A. test
on line 3 of class B? B. Exception
A. Line 33 must be called within a try block. C. Compilation fails.
B. The exception thrown by method1 in D. NullPointerException
class A is not required to be
caught. Answer: C
C. The method declared on line 31 must be
declared to throw a 216. Program
RuntimeException. package com.lara;
import java.sql.SQLException;
D. On line 5 of class A, the call to method2 public class S
of class B does not need to {
S() throws SQLException
be placed in a try/catch block. {

}
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]); }

www.laratechnology.com 080-41310124 Page 74


LARA TECHNOLOGIES LARA TECHNOLOGIES

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. }

www.laratechnology.com 080-41310124 Page 75


LARA TECHNOLOGIES LARA TECHNOLOGIES

Which is true about the two classes? 45. A a=new A();


A. Compilation of both classes will fail. 46. System.out.println(a.sayHello(”John”));
B. Compilation of both classes will succeed. Which two are true? (Choose two.)
C. Compilation of class A will fail. A. Class A will not compile.
Compilation of class B will succeed. B. Line 46 can throw the unchecked
D. Compilation of class B will fail. exception TestException.
Compilation of class A will succeed. C. Line 45 can throw the unchecked
exception TestException.
Answer: D D. Line 46 will compile if the enclosing
method throws a TestException.
219. Program E. Line 46 will compile if enclosed in a try
Class TestException block, where TestException
1. public class TestException extends is caught.
Exception {
2. } Answer: DE
Class A:
1. public class A { 220. Program

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?

www.laratechnology.com 080-41310124 Page 76


LARA TECHNOLOGIES LARA TECHNOLOGIES

A. null 12. try {


B. finally 13. System.out.print(”test “);
C. null finally 14. throw new RuntimeException();
D. Compilation fails. 15. }
E. finally exception 16. catch (Exception ex)
{ System.out.print(”exception “); }
Answer: E 17. }
18. public static void main(String[] args) {
221. Program 19. try { test(); }
Given: 20. catch (RuntimeException ex)
10. public class Foo { { System.out.print(”runtime “); }
11. static int[] a; 21. System.out.print(”end “);
12. static { a[0]=2; } 22. }
13. public static void main( String[] args) {} What is the result?
14. } A. test end
Which exception or error will be thrown B. Compilation fails.
when a programmer attempts C. test runtime end
to run this code? D. test exception end
A. java.lang. StackOverflowError E. A Throwable is thrown by main at
B. java.lang.IllegalStateException runtime.
C. java.lang.ExceptionlnlnitializerError
D. Answer: D
java.lang.ArraylndexOutOfBoundsExceptio
n 223. Program
package com.lara;
public class AgeIsNegativeException
Answer: C
extends ArithmeticException
{
public AgeIsNegativeException()
222. Program {
Given:
}
11. static void test() throws public
RuntimeException { AgeIsNegativeException(String msg)

www.laratechnology.com 080-41310124 Page 77


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ //
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 //
{ // }

} // void test2() throws


void test5()
FileNotFoundException
{
// {
//
}
// }
}
public class B extends A
// void test2() throws IOException
{
// {
// void test1()
//
// {
// }
//
// }
// void test2() throws SQLException
// {
// void test1() throws SQLException
//
// {
// }

www.laratechnology.com 080-41310124 Page 78


LARA TECHNOLOGIES LARA TECHNOLOGIES

// void test2() throws Exception // void test4()


// { // {
// //
// } // }
//
// void test2() throws
// void test4() throws
ArithmeticException
// { NullPointerException
// // {
// } //
// }
// void test3()
// { // void test4() throws
//
ArithmeticException
// }
// {
//
// void test3() throws IOException
// }
// {
//
// void test4() throws
// }
ClassNotFoundException
// void test3() throws // {
//
FileNotFoundException
// }
// {
//
// }
// void test5()
// {
// void test3() throws
//
ClassNotFoundException // }
// {
// // void test5() throws
// }
NullPointerException
// {
// void test3() throws Exception
//
// {
// }
//
// void test5() throws
// }
ClassNotFoundException
// void test3() throws // {
//
ClassCastException
// }
// {
}
//
// }
225. Program

www.laratechnology.com 080-41310124 Page 79


LARA TECHNOLOGIES LARA TECHNOLOGIES

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 //
{ // }

} // void test2() throws


void test5()
FileNotFoundException
{
// {
//
}
// }
}
public class B extends A
// void test2() throws IOException
{
// {
// void test1()
//
// {
// }
//
// }
// void test2() throws SQLException
// {
// void test1() throws SQLException
//
// {
// }
//
// }
// void test2() throws Exception
// {
// void test1() throws Exception
//
// {
// }
//

www.laratechnology.com 080-41310124 Page 80


LARA TECHNOLOGIES LARA TECHNOLOGIES

// void test2() throws


// void test4() throws
ArithmeticException
// { NullPointerException
// // {
// } //
// }
// void test3()
// { // void test4() throws
//
ArithmeticException
// }
// {
//
// void test3() throws IOException
// }
// {
//
// void test4() throws
// }
ClassNotFoundException
// void test3() throws // {
//
FileNotFoundException
// }
// {
//
// }
// void test5()
// {
// void test3() throws
//
ClassNotFoundException // }
// {
// // void test5() throws
// }
NullPointerException
// {
// void test3() throws Exception
//
// {
// }
//
// void test5() throws
// }
ClassNotFoundException
// void test3() throws // {
//
ClassCastException
// }
// {
}
//
// }
226. Program

// void test4() 5. class A{


// {
6. void foo() throws
//
// } Exception{throw new Exception();}
//
7.}

www.laratechnology.com 080-41310124 Page 81


LARA TECHNOLOGIES LARA TECHNOLOGIES

8. class SubB2 extends A{ }


System.out.println(2);
9.void foo()
}
{System.out.primtln(“B”);} }
10.}
228. Program
11. class Tester{ package com.lara;
public class E
12. public static void main(String
{
args[]){ public static void main(String[] args)
{
13. A a=new SubB2();
System.out.println(1);
14. a.foo(); if(true)
{
15.}
throw new
16.}
OutOfMemoryError("some message");
What is the result? }
System.out.println(2);
A. B
}
B. B, followed by Exception }
C. Compilation fails due to an error on line
229. Program
9 package com.lara;
public class F
D. Compilation fails due to an error on line
{
14 public static void main(String[] args)
throws ClassNotFoundException
E. An exception is thrown with no other
{
output. System.out.println(1);
if(true)
{
Ans : D throw new
ClassNotFoundException();
}
227. Program System.out.println(2);
package com.lara; }
public class D }
{
public static void main(String[] args) 230. Program
{ package com.lara;
System.out.println(1); import java.util.Scanner;
if(true) public class G
{ {
throw new public static void main(String[] args)
NumberFormatException("some msg"); {

www.laratechnology.com 080-41310124 Page 82


LARA TECHNOLOGIES LARA TECHNOLOGIES

Scanner sc = new System.out.println(1);


assert false;
Scanner(System.in);
System.out.println(2);
System.out.println(1);
}
System.out.println("Enter age");
}
int age = sc.nextInt();
if(age <= 0) 233. Program
{ package pack1;
throw new class B
{
ArithmeticException();
public static void main(String[] args)
}
{
System.out.println(2);
System.out.println(1);
//continue....
int i = 0;
}
assert i > 0;
}
System.out.println(2);
}
231. Program
}
package com.lara;
import java.util.Scanner;
234. Program
public class H
package pack1;
{
class C
public static void main(String[] args)
{
{
public static void main(String[] args)
Scanner sc =
{
new Scanner(System.in);
System.out.println(1);
System.out.println(1);
assert true;
System.out.println("Enter
System.out.println(2);
age"); }
int age = sc.nextInt(); }
if(age <= 0) 235. Program
{ package pack1;
throw new class E
{
ArithmeticException("Age should be +ve");
public static void main(String[] args)
}
{
System.out.println(2);
System.out.println(1);
//continue....
assert false : "error occured";
}
System.out.println(2);
}
}
}
232. Program
236. Program
package pack1;
package pack1;
public class A
class F
{
{
public static void main(String[] args)
public static void main(String[] args)
{

www.laratechnology.com 080-41310124 Page 83


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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) {

www.laratechnology.com 080-41310124 Page 84


LARA TECHNOLOGIES LARA TECHNOLOGIES

10. assert a.length == 1; B. stuff


11. } passed
12. } C. passed
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.
243. 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”; 244. Program

19. System.out.println(”passed”); Click the Exhibit button.


20. } 1. public class Test {
21. } 2.
And these command line invocations: 3. public static void main(String [] args) {
java AssertStuff 4. boolean assert = true;
java -ea AssertStuff 5. if(assert) {
What is the result? 6. System.out.println(”assert is true”);
A. passed 7. }
stuff 8. }

www.laratechnology.com 080-41310124 Page 85


LARA TECHNOLOGIES LARA TECHNOLOGIES

9. A. All of the assert statements are used


10. } appropriately.
Given: B. Only the assert statement on line 31 is
javac -source 1.3 Test.java used appropriately.
What is the result? C. The assert statements on lines 29 and 31
A. Compilation fails. are used appropriately.
B. Compilation succeeds with errors. D. The assert statements on lines 26 and 29
C. Compilation succeeds with warnings. are used appropriately.
D. Compilation succeeds without warnings E. The assert statements on lines 29 and 33
or errors. are used appropriately.
F. The assert statements on lines 29, 31, and
Answer: C 33 are used
appropriately.
245. Program G. The assert statements on lines 26, 29, and
Given: 31 are used
23.int z=5; appropriately.
24.
25. public void stuff1(int x) { Answer: C
26. assert (x> 0);
246. Program
27. switch(x) {
Given:
28. case 2: x= 3;
11. static void test() throws Error {
29. default: assert false; } }
12. if (true) throw new AssertionError();
30.
13. System.out.print(”test “);
31. private void stuff2(int y) { assert (y < 0);
14. }
}
15. public static void main(String[] args) {
32.
16. try { test(); }
33. private void stuff3() { assert (stuff4O); }
17. catch (Exception ex)
34.
{ System.out.print(”exception “); }
35. private boolean stuff4() { z = 6; return
18. System.out.print(”end “);
false; }
19. }
Which is true?
What is the result?

www.laratechnology.com 080-41310124 Page 86


LARA TECHNOLOGIES LARA TECHNOLOGIES

A. end 248. Program


package com.lara;
B. Compilation fails.
import java.util.Scanner;
C. exception end public class I
{
D. exception test end
public static void main(String[] args)
E. A Throwable is thrown by main. {
Scanner sc =
F. An Exception is thrown by main.
new Scanner(System.in);
System.out.println(1);
System.out.println("Enter
Answer: E
age");
int age = sc.nextInt();
247. Program if(age <= 0)
{
Given a method that must ensue that its throw new
parameter is not null: AgeIsNegativeException("age should not e
11. public void someMethod(Object value) { -ve");
12. // check for null value }
System.out.println(2);
.... //continue....
20. System.out.println(value.getClass()); }
}
21. }
What, inserted at line 12, is the appropriate 249. Program

way to handle a null


package com.lara;
value? public class Manager
{
A. assert value == null;
public static void main(String[] args)
B. assert value !null, “value is null”; {
System.out.println(1);
C. if (value == null) {
test();
throw new AssertionException(”value is System.out.println(2);
}
null”);
static void test()
D. if (value == null) { {
System.out.println(3);
throw new
System.out.println(4);
IllegalArgumentException(”value is null”); }
}

Answer: D 250. Program

www.laratechnology.com 080-41310124 Page 87


LARA TECHNOLOGIES LARA TECHNOLOGIES

package com.lara; 252. Program


public class Manager1
{ package com.lara;
public static void main(String[] args) class ThreadA extends Thread
{ {
for(int i = 0; i < 1000; i++) public void run()
{ {
System.out.println(i); for(int i = 0; i < 1000; i++)
} {
System.out.println(i);
for(int i = 1000; i < 2000; i+ }
+) }
{ }
System.out.println(i); class ThreadB extends Thread
} {
} public void run()
} {
for(int i = 1000; i < 2000; i+
251. Program +)
{
package com.lara; System.out.println(i);
class Thread1 extends Thread }
{ }
public void run() }
{ public class Manager3
for(int i = 0; i < 1000; i ++) {
{ public static void main(String[] args)
System.out.println(i); {
} ThreadA t1 = new ThreadA();
} t1.start();
}
public class Manager2 ThreadB t2 = new ThreadB();
{ t2.start();
public static void main(String[] args) //t2.setDaemon(on)
{
Thread1 t1 = new Thread1(); for(int i = 2000; i < 3000; i+
t1.start(); +)
for(int i = 1000; i < 2000; i+ {
+) System.out.println(i);
{ }
System.out.println(i); }
} }
}
} 253. Program

www.laratechnology.com 080-41310124 Page 88


LARA TECHNOLOGIES LARA TECHNOLOGIES

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);

www.laratechnology.com 080-41310124 Page 89


LARA TECHNOLOGIES LARA TECHNOLOGIES

for(int i = 0; i < 1000; i++) for(int i = 1000; i < 2000; i+


{
+)
System.out.println(i);
{
}
System.out.println(i);
}
}
}
}
public class Manager6
}
{
public static void main(String[] args)
{
D d1 = new D(); 258. Program
d1.start();
D d2 = new D(); package com.lara;
d2.start(); class F extends Thread
for(int i = 1000; i < 2000; i+ {
public void run()
+)
{
{
for(int i = 0; i < 1000; i++)
System.out.println(i);
{
}
System.out.println(i);
}
start();
}
}
}
}
257. Program public class Manager8
{
package com.lara; public static void main(String[] args)
class E implements Runnable {
{ F f1 = new F();
public void run() f1.start();
{ for(int i = 1000; i < 2000; i+
for(int i = 0; i < 1000; i ++)
+)
{
{
System.out.println(i);
System.out.println(i);
}
}
}
}
}
}
public class Manager7
{
public static void main(String[] args)
{ 259. Program
E e1 = new E();
Thread t1 = new Thread(e1); package com.lara;
t1.start(); class G extends Thread
Thread t2 = new Thread(e1); {
t2.start(); public void run()
{

www.laratechnology.com 080-41310124 Page 90


LARA TECHNOLOGIES LARA TECHNOLOGIES

for(int i = 0; i < 1000; i++) H h1 = new H();


{ //h1.start();
System.out.println(i); for(int i = 1000; i < 2000; i +
}
+)
}
{
void startThread()
System.out.println(i);
{
}
start();
}
}
}
}
public class Manager9
{
public static void main(String[] args)
{ 261. Program
G g1 = new G();
g1.startThread(); package com.lara;
for(int i = 1000; i < 2000; i+ class I extends Thread
{
+)
public void run()
{
{
System.out.println(i);
for(int i = 0; i < 1000; i++)
}
{
}
System.out.println(i);
}
}
}
}
260. Program public class Manager11
{
package com.lara; public static void main(String[] args)
class H extends Thread {
{ I obj = new I();
H() obj.run();
{ obj.start();
start(); for(int i = 1000; i < 2000; i+
}
+)
public void run()
{
{
System.out.println(i);
for(int i = 0; i < 1000; i++)
}
{
}
System.out.println(i);
}
}
}
}
public class Manager10 262. Program
{
public static void main(String[] args) package com.lara;
{ public class Manager12

www.laratechnology.com 080-41310124 Page 91


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ class A extends Thread


static class A extends Thread {
{ public void run()
public void run() {
{ for(int i = 0; i < 1000; i++)
for(int i = 0; i < 1000; i ++) {
{
System.out.println(i);
System.out.println(i);
}
}
}
}
}
}
static class B implements Runnable
{
class B implements Runnable
public void run()
{
{
public void run()
for(int i = 1000; i < 2000; i+
{
+) for(int i = 1000; i < 2000; i+
{
+)
System.out.println(i);
{
}
System.out.println(i);
}
}
}
}
public static void main(String[] args)
}
{
A a1 = new A();
A a1 = new A();
a1.start();
a1.start();
B b1 = new B();
B b1 = new B();
Thread t1 = new Thread(b1);
Thread t1 = new Thread(b1);
t1.start();
t1.start();
for(int i = 2000; i< 3000; i+
for(int i = 2000; i < 3000; i+
+)
+)
{
{
System.out.println(i);
System.out.println(i);
}
}
}
}
}
}
264. Program

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()
{
{

www.laratechnology.com 080-41310124 Page 92


LARA TECHNOLOGIES LARA TECHNOLOGIES

public void run() {


{ System.out.println(i);
for(int i = 0; i < 1000; i++) }
{ }
System.out.println(i); }.start();
} new Thread(new Runnable()
} {
}; public void run()
t1.start(); {
for(int i = 1000; i < 2000; i+
Runnable r1 = new
+)
Runnable() {
{ System.out.println(i);
public void run() }
{ }
for(int i = 1000; i < 2000; i+ }).start();
+)
for(int i = 2000; i < 3000; i+
{
System.out.println(i); +)
} {
} System.out.println(i);
}; }
Thread t2 = new Thread(r1); }
t2.start(); }
for(int i = 2000; i < 3000; i+
+)
{ 266. Program
System.out.println(i);
} package com.lara;
} class A extends Thread
} {
public void run()
{
265. Program for(int i = 0; i < 10; i++)
{
System.out.println(i);
package com.lara;
try
public class Manager15
{
{
sleep(1000);
public static void main(String[] args)
}
{
catch(InterruptedException
new Thread()
{ ex)
public void run() {
{ ex.printStackTrace();
for(int i = 0; i < 1000; i++) }

www.laratechnology.com 080-41310124 Page 93


LARA TECHNOLOGIES LARA TECHNOLOGIES

} {
} 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);

www.laratechnology.com 080-41310124 Page 94


LARA TECHNOLOGIES LARA TECHNOLOGIES

} }
}
}
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()

www.laratechnology.com 080-41310124 Page 95


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ }
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

package com.lara; 275. Program


public class Manager8
{ package com.lara;
public static void main(String[] args) class I extends Thread
{ {
class A extends Thread public void run()
{ {
for(int i = 0; i < 1000; i++)

www.laratechnology.com 080-41310124 Page 96


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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);

www.laratechnology.com 080-41310124 Page 97


LARA TECHNOLOGIES LARA TECHNOLOGIES

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 */ }

www.laratechnology.com 080-41310124 Page 98


LARA TECHNOLOGIES LARA TECHNOLOGIES

}
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

www.laratechnology.com 080-41310124 Page 99


LARA TECHNOLOGIES LARA TECHNOLOGIES

separate thread? (Choose two.) 7. t.run();


A. new Thread() { 8. t.run();
public void run() { doStuff(); } 9. t.start();
} 10. }
B. new Thread() { 11. }
public void start() { doStuff(); } What is the result?
} A. Compilation fails.
C. new Thread() { B. An exception is thrown at runtime.
public void start() { doStuff(); } C. The code executes and prints “running”.
} .run(); D. The code executes and prints
D. new Thread() { “runningrunning”.
public void run() { doStuff(); } E. The code executes and prints
} .start(); “runningrunningrunning”.
E. new Thread(new Runnable() {
public void run() { doStuff(); } Answer: E
} ).run();
F. new Thread(new Runnable() { 283. Program
public void run() { doStuff(); } Given:
}).start(); 1. public class Threads4 {
2. public static void main (String[] args) {
Answer: DF 3. new Threads4().go();
4. }
282. Program 5. public void go() {
Given: 6. Runnable r = new Runnable() {
1. public class Threads3 implements 7. public void run() {
Runnable { 8. System.out.print(”foo”);
2. public void run() { 9. }
3. System.out.print(”running”); 10. };
4. } 11. Thread t = new Thread(r);
5. public static void main(String[] args) { 12. t.start();
6. Thread t = new Thread(new Threads3()); 13. t.start();

www.laratechnology.com 080-41310124 Page 100


LARA TECHNOLOGIES LARA TECHNOLOGIES

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

www.laratechnology.com 080-41310124 Page 101


LARA TECHNOLOGIES LARA TECHNOLOGIES

14. } 5. throw new


15. public Starter() { RuntimeException(”Problem”);
16. x=5; 6. }
17. start(); 7. public static void main(String[] args) {
18. } 8. Thread t = new Thread(new Threads2());
19. public void makeItSo() throws 9. t.start();
Exception { 10. System.out.println(”End of method.”);
20. join(); 11. }
21. x=x- 1; 12. }
22. System.out.println(x); Which two can be results? (Choose two.)
23. } A. java.lang.RuntimeException: Problem
24. public void run() { x *= 2; } B. run.
25. } java.lang.RuntimeException: Problem
What is the output if the main() method is C. End of method.
rum? java.lang.RuntimeException: Problem
A. 4 D. End of method.
B. 5 run.
C. 8 java.lang.RuntimeException: Problem
D. 9 E. run.
E. Compilation fails. java.lang.RuntimeException: Problem
F. An exception is thrown at runtime. End of method.
G. It is impossible to determine for certain.
Answer: D Answer: DE

287. Program 288. 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”);

www.laratechnology.com 080-41310124 Page 102


LARA TECHNOLOGIES LARA TECHNOLOGIES

5. } B. An exception is thrown at runtime.


6. } C. The code executes and prints
What is the result? “StartedComplete”.
A. Compilation fails. D. The code executes and prints
B. An exception is thrown at runtime. “StartedComplete0123”.
C. The code executes normally and prints E. The code executes and prints
“sleep”. “Started0l23Complete”.
D. The code executes normally, but nothing
is printed. Answer: E
Answer: C
290. Program

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:" +

4. t.start(); t1.getName() + ":" + i);


}
5. System.out.print(”Started”); }
6. t.join(); void test2()
{
7. System.out.print(”Complete”); Thread t1 = Thread.currentThread();
8. } for(int i = 0; i < 1000; i++)
{
9. public void run() { System.out.println("test2:" +
10. for (int i= 0; i< 4; i++) { t1.getName() + ":" + i);
11. System.out.print(i); }
}
12. } synchronized void test3()
13. } {
Thread t1 = Thread.currentThread();
14. } for(int i = 0; i < 1000; i++)
What can be a result? {
System.out.println("test3:" +
A. Compilation fails.
t1.getName() + ":" + i);

www.laratechnology.com 080-41310124 Page 103


LARA TECHNOLOGIES LARA TECHNOLOGIES

} 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

www.laratechnology.com 080-41310124 Page 104


LARA TECHNOLOGIES LARA TECHNOLOGIES

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

www.laratechnology.com 080-41310124 Page 105


LARA TECHNOLOGIES LARA TECHNOLOGIES

D-s1 Case 14:

Case 10: A-test7

A-test5 B-test8

B-test6 C-s1

C-s1 D-s2

D-s1 Case 15:

Case 11: A-test4

A-test5 B-test8

B-test6 C-s1

C-s1 D-s2

D-s2 Case 16:

Case 12: A-test1

A-test6 B-test5

B-test7 C-s1

C-s1 D-s2

D-s2 Case 17:

Case 13: A-test8

A-test7 B-test8

B-test7 C-s1

C-s1 D-s1

D-s2 Case18 :

www.laratechnology.com 080-41310124 Page 106


LARA TECHNOLOGIES LARA TECHNOLOGIES

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();

www.laratechnology.com 080-41310124 Page 107


LARA TECHNOLOGIES LARA TECHNOLOGIES

if(ids != null) C. Deadlocked threads release once their


{
sleep() methods sleep duration has expired.
System.out.println("dead
D. Deadlocking can occur only when the
locked threads are");
ThreadInfo ti[] = wait(),notify(),and notifyAll() methods are
tx.getThreadInfo(ids); used incorrectly.
ThreadInfo thInfo = null;
E. It is possible for a single-threaded
for(int i = 0; i < ti.length; i+
application to deadlock if synchronized
+)
{ blocks are used incorrectly.
thInfo = ti[i];
F. If a piece of code is capable of
deadlocking, you cannot eliminate the
System.out.println(thInfo.getThreadName())
possibility of deadlocking by inserting
;
} invocation of
}
Thread.yield().
else
{
Ans: A, F
System.out.println("no threads are
under locked");
} 293. Program
} Click the Exhibit button.
}
Given:
1. public class TwoThreads {
2
292. Program 3. private static Object resource = new
Which two statements are true? (Choose Object();
two) 4.
5. private static void delay(long n) {
A. It is possible for more than two threads to 6. try { Thread.sleep(n); }
deadlock at once. 7. catch (Exception e)
B. the JVM implementation guarantees that { System.out.print(”Error “); }
multiple threads can not enter into a 8. }
deadlock state. 9
10. public static void main(String[] args) {

www.laratechnology.com 080-41310124 Page 108


LARA TECHNOLOGIES LARA TECHNOLOGIES

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. } 294. Program
31. Given:
32. static class Thread2 extends Thread { public class NamedCounter {
33. public void run() { private final String name;
34. synchronized (resource) { private int count;
35. System.out.print(”Start2 “); public NamedCounter(String name)
36. delay(2000); { this.name = name; }
37. System.out.print(”End2 “); public String getName() { return name; }
38. } public void increment() { coount++; }
39. } public int getCount() { return count; }
40. } public void reset() { count = 0; }
41. } }

www.laratechnology.com 080-41310124 Page 109


LARA TECHNOLOGIES LARA TECHNOLOGIES

Which three changes should be made to 13.


adapt this class to be used 14. public static void main(String[] args) {
safely by multiple threads? (Choose three.) 15. new Threads1().go();
A. declare reset() using the synchronized 16. }
keyword 17.
B. declare getName() using the 18. public void go() {
synchronized keyword 19. Runnable r1 = new Runner();
C. declare getCount() using the 20. new Thread(r1).start();
synchronized keyword 21. new Thread(r1 ).start();
D. declare the constructor using the 22. }
synchronized keyword 23. }
E. declare increment() using the Which two are possible results? (Choose
synchronized keyword two.)
A. 0, 2, 4, 4, 6, 8, 10, 12,
Answer: ACE B. 0, 2, 4, 6, 8, 10, 2, 4,
C. 0, 2, 4, 6, 8, 10, 12, 14,
295. Program D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12,
Click the Exhibit button: 14, 14,
1. public class Threads 1 { E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10,
2. intx=0; 12, 14,
3. public class Runner implements Runnable
{ Answer: AC
4. public void run() {
5. int current = 0; 296. Program

6. for(int=i=0;i<4;i++){ Click the Exhibit button.


7. current = x; 1. import java.util.*;
8. System.out.print(current + “, “); 2.
9. x = current + 2; 3. public class NameList {
10. } 4. private List names = new ArrayList();
11. } 5. public synchronized void add(String
12. } name) { names.add(name); }

www.laratechnology.com 080-41310124 Page 110


LARA TECHNOLOGIES LARA TECHNOLOGIES

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.

www.laratechnology.com 080-41310124 Page 111


LARA TECHNOLOGIES LARA TECHNOLOGIES

C. Change the variable declaration on line 3 Answer: CEF


to private volatile int x;.
D. Wrap the code inside the foo() method 299. Program
with a synchronized( this ) Given:
block. 1. public class TestSeven extends Thread {
E. Wrap the for loop code inside the go() 2. private static int x;
method with a synchronized 3. public synchronized void doThings() {
block synchronized(this) { // for loop code 4. int current = x;
here }. 5. current++;
6. x = current;
Answer: AD 7. }
8. public void run() {
298. Program 9. doThings();
Which three will compile and rim without 10. }
exception? (Choose three.) 1 1.}
A. private synchronized Object o; Which is true?
B. void go() { A. Compilation fails.
synchronized() { /* code here */ } B. An exception is thrown at runtime.
} C. Synchronizing the run() method would
C. public synchronized void go() { /* code make the class thread-safe.
here */ } D. The data in variable “x” are protected
D. private synchronized(this) void go() { /* from concurrent access
code here */ } problems.
E. void go() { E. Declaring the doThings() method as
synchronized(Object.class) { /* code here static would make the class
*/ } thread-safe.
} F. Wrapping the statements within
F. void go() { doThings() in a synchronized(new
Object o = new Object(); Object()) { } block would make the class
synchronized(o) { /* code here */ } thread-safe.
}

www.laratechnology.com 080-41310124 Page 112


LARA TECHNOLOGIES LARA TECHNOLOGIES

Answer: E 33. synchronized (b) {


34. a.add(-amount);
300. Program 35. b.add(amount);
Click the Exhibit button. 36. }
10. public class Transfers { 37. }
11. public static void main(String[] args) 38. }
throws Exception { 39. }
12. Record r1 = new Record(); 40. class Record {
13. Record r2 = new Record(); 41.int num=10;
14. doTransfer(r1, r2, 5); 42. public int get() { return num; }
15. doTransfer(r2, r1, 2); 43. public void add(int n) { num = num + n;
16. doTransfer(r1, r2, 1); }
17. // print the result 44. }
18. System.out.println(”rl = “ + r1.get() +“, If Transfers.main() is run, which three are
r2=” + r2.get()); true? (Choose three.)
19. } A. The output may be “r1 = 6, r2 = 14”.
20. private static void doTransfer( B. The output may be “r1 = 5, r2 = 15”.
21. final Record a, final Record b, final int C. The output may be “r1 = 8, r2 = 12”.
amount) { D. The code may run (and complete) with
22. Thread t = new Thread() { no output.
23. public void run() { E. The code may deadlock (without
24. new Clerk().transfer(a, b, amount); completing) with no output.
25. } F. M IllegalStateException or
26. }; InterruptedException may be thrown at
27. t.start(); runtime.
28. }
29. } Answer: ABE
30. class Clerk {
31. public synchronized void 301. Program

transfer(Record a, Record b, int amount){ Click the Exhibit button.


32. synchronized (a) { 1. class Computation extends Thread {

www.laratechnology.com 080-41310124 Page 113


LARA TECHNOLOGIES LARA TECHNOLOGIES

2. 30. for (Computation c : computations)


3. private int num; 31. System.out.print(c.getResult() +“ “);
4. private boolean isComplete; 32. }
5. private int result; 33. }
6. What is the result?
7. public Computation(int num) { this.num A. The code will deadlock.
= num; } B. The code may run with no output.
8. C. An exception is thrown at runtime.
9. public synchronized void run() { D. The code may run with output “0 6”.
10. result = num * 2; E. The code may run with output “2 0 6 4’.
11. isComplete = true; F. The code may ruin with output “0 2 4 6”.
12. notify();
13. } Answer: F
14.
15. public synchronized int getResult() { 302. Program
16. while (!isComplete) { Given:
17. try { 7. void waitForSignal() {
18. wait(); 8. Object obj = new Object();
19. } catch (InterruptedException e) { } 9. synchronized (Thread.currentThread()) {
20. } 10. obj.wait();
21. return result; 11. obj.notify();;
22. } 12. }
23. 13. }
24. public static void main(String[] args) { Which is true?
25. Computation[] computations = new A. This code may throw an
Computation [4]; InterruptedException.
26. for (int i = 0; i < computations.length; B. This code may throw an
i++) { IllegalStateException.
27. computations[i] = new Computation(i); C. This code may throw a TimeoutException
28. computations[i] .start(); after ten minutes.
29. }

www.laratechnology.com 080-41310124 Page 114


LARA TECHNOLOGIES LARA TECHNOLOGIES

D. This code will not compile unless 304. Program

“obj.wait()” is replaced with


package com.lara.pack4;
“((Thread) obj).wait()”. class A
E. Reversing the order of obj.wait() and {
synchronized void test1()
obj.notify() may cause this {
method to complete normally. System.out.println("test1

F. A call to notify() or notifyAll() from begin");


try
another thread may cause this {
method to complete normally. wait();
}
catch(InterruptedException
Answer: B ex)
{
ex.printStackTrace();
303. Program }
System.out.println("test1
Given:
end");
foo and bar are public references available
}
to many other threads. foo synchronized void test2()
{
refers to a Thread and bar is an Object. The
notifyAll();
thread foo is currently }
}
executing bar.wait(). From another thread,
class Thread1 extends Thread
which statement is the {
A a1;
most reliable way to ensue that foo will stop
Thread1(A a1)
executing wait()? {
this.a1 = a1;
A. foo.notify();
}
B. bar.notify(); public void run()
{
C. foo.notifyAll();
a1.test1();
D. Thread.notify(); }
}
E. bar.notiFYAll();
class Thread2 extends Thread
F. Object.notify(); {
A a1;
Thread2(A a1)
Answer: E {
this.a1 = a1;
}

www.laratechnology.com 080-41310124 Page 115


LARA TECHNOLOGIES LARA TECHNOLOGIES

public void run() catch(InterruptedException


{
ex)
a1.test1();
{
}
ex.printStackTrace();
}
}
public class Manager
}
{
synchronized void test2()
public static void main(String[] args)
{
{
//notify();
A a1 = new A();
notifyAll();
Thread1 t1 = new
}
Thread1(a1); }
Thread2 t2 = new class B extends Thread
{
Thread2(a1);
A obj;
t1.start();
B(A obj)
t2.start();
{
try
this.obj = obj;
{
}
Thread.sleep(20000);
public void run()
}
{
catch(InterruptedException
System.out.println("B-run begin");
ex) obj.test1();
{ System.out.println("B-run end");
ex.printStackTrace(); }
} }
System.out.println("about to class C extends Thread
{
release");
A obj;
new A().test2();
C(A obj)
}
{
}
this.obj = obj;
}
public void run()
305. Program {
System.out.println("C-run begin");
package com.lara.pack5; obj.test1();
class A System.out.println("C-run end");
{ }
synchronized void test1() }
{ class D extends Thread
try {
{ A obj;
wait(); D(A obj)
} {
this.obj = obj;

www.laratechnology.com 080-41310124 Page 116


LARA TECHNOLOGIES LARA TECHNOLOGIES

} {
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 {

www.laratechnology.com 080-41310124 Page 117


LARA TECHNOLOGIES LARA TECHNOLOGIES

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);
}

www.laratechnology.com 080-41310124 Page 118


LARA TECHNOLOGIES LARA TECHNOLOGIES

} class B extends Thread


public class Manager2 {
{ public void run()
public static void main(String[] args) {
{ System.out.println("begin");
A a1 = new A(); try
System.out.println("a:" + {
Thread.sleep(10000);
a1.getState());
}
a1.start();
catch(InterruptedException
System.out.println("b:" +
ex)
a1.getState());
{
try
ex.printStackTrace();
{
}
Thread.sleep(1);
System.out.println("end");
}
}
catch(InterruptedException
}
ex) public class Manager3
{ {
public static void main(String[] args)
{
System.out.println(ex);
B b1 = new B();
}
b1.start();
System.out.println("c:" +
try
a1.getState()); {
try Thread.sleep(5000);
{ }
Thread.sleep(10000); catch(InterruptedException
}
ex)
catch(InterruptedException
{
ex)
{
System.out.println(ex);
}
System.out.println(ex);
}
System.out.println(b1.getState());
System.out.println("d:" +
}
a1.getState()); }
}
}

310. Program 311. Program

package com.lara.pack8; package com.lara.pack8;

www.laratechnology.com 080-41310124 Page 119


LARA TECHNOLOGIES LARA TECHNOLOGIES

class C extends Thread Given that t1 is a reference to a live


{
thread, which is true?
public synchronized void run()
{ A. The Thread.sleep() method can take t1 as
System.out.println("begin");
an argument.
try
{ B. The Object.notify() method can take t1 as
wait();
an argument.
}
catch(InterruptedException C. The Thread.yield() method can take t1 as
ex) an argument.
{
D. The Thread.setPriority() method can take
ex.printStackTrace();
} t1 as an argument.
System.out.println("end");
E. The Object.notify() method arbitrary
}
} chooses which thread to notify.
public class Manager4
Ans: E
{
public static void main(String[] args)
{
C c1 = new C(); 313. Program
c1.start();
try package com.lara.pack8;
{ class D extends Thread
Thread.sleep(5000); {
} Thread main;
catch(InterruptedException D(Thread main)
{
ex)
this.main = main;
{
}
ex.printStackTrace();
public void run()
}
{
try
System.out.println(c1.getState()); {
synchronized (c1) Thread.sleep(2000);
{ }
c1.notify(); catch(InterruptedException
}
ex)
}
{
}

System.out.println(ex);
312. Program }

System.out.println(main.getState());

www.laratechnology.com 080-41310124 Page 120


LARA TECHNOLOGIES LARA TECHNOLOGIES

} {
} 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();

www.laratechnology.com 080-41310124 Page 121


LARA TECHNOLOGIES LARA TECHNOLOGIES

E e1 = new E(s1, s2); (Choose two)


F f1 = new F(s1, s2);
e1.start();
f1.start(); A.Thread.wait()
Util.sleep(10000);
B.Thread.join()
System.out.println(e1.getState());
System.out.println(f1.getState()); C.Thread.yield()
}
D.Thread.sleep(1)
}
E.Thread.notify()

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” }

www.laratechnology.com 080-41310124 Page 122


LARA TECHNOLOGIES LARA TECHNOLOGIES

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

www.laratechnology.com 080-41310124 Page 123


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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());

www.laratechnology.com 080-41310124 Page 124


LARA TECHNOLOGIES LARA TECHNOLOGIES

} Thread t1 = new Thread(tg,


}
b1, "3rd thread");
Thread t2 = new Thread(tg,
319. Program b1, "4th thread");
a1.start();
a2.start();
package com.lara.pack12;
//t1.start();
class A extends Thread
t2.start();
{
tg.stop();
A(ThreadGroup tg, String name)
}
{
}
super(tg, name);
}
public void run()
{
for(int i = 0; i < 1000; i++)
{
System.out.println(i); 320. Program
}
} package com.lara.pack13;
} class A
class B implements Runnable {
{ //some members
public void run() }
{ class B extends Thread
for(int i = 1000; i < 2000; i+ {
//several attributues
+)
public void run()
{
{
System.out.println(i);
//some stmts
}
}
}
}
}
class C extends A implements Runnable
public class Manager
{
{
//several attributes
public static void main(String[] args)
public void run()
{
{
ThreadGroup tg =
//some stmts
new
}
ThreadGroup("first group"); }
A a1 = new A(tg, "first public class Manager
{
thread");
public static void main(String[] args)
A a2 = new A(tg, "2nd
{
thread"); B b1 = new B();
B b1 = new B(); b1.start();

www.laratechnology.com 080-41310124 Page 125


LARA TECHNOLOGIES LARA TECHNOLOGIES

B b2 = new B(); notify();


b2.start(); }
C c1 = new C(); public void run()
Thread t1 = new Thread(c1); {
Thread t2 = new Thread(c1); while(true)
t1.start(); {
t2.start(); goToWait();
} //main task starting
} for(int i = 0; i < 10; i+
+)
321. Program {
System.out.println(getName() + ":" +
package com.lara;
i);
import java.util.ArrayList;
Util.sleep(1000);
class Util
}
{
//main task end
static void sleep(long millis)
release();
{
}
try
}
{
}
Thread.sleep(millis);
class ThreadPoolManager
}
{
catch(InterruptedException
private ArrayList pool = new
ex)
ArrayList();
{
public void init()
ex.printStackTrace();
{
}
ModelThread th = null;
}
for(int i = 0; i < 10; i++)
}
{
class ModelThread extends Thread
th = new
{
public synchronized void goToWait() ModelThread();
{ th.start();
try pool.add(th);
{ }
wait(); }
} public ModelThread getThread()
catch(InterruptedException {
ModelThread th = null;
ex)
if(pool.size() > 0)
{
{
ex.printStackTrace();
th = (ModelThread)
}
} pool.remove(0);
public synchronized void release() }
{ else

www.laratechnology.com 080-41310124 Page 126


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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(); }

www.laratechnology.com 080-41310124 Page 127


LARA TECHNOLOGIES LARA TECHNOLOGIES

} 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 }

www.laratechnology.com 080-41310124 Page 128


LARA TECHNOLOGIES LARA TECHNOLOGIES

} }
}
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;
} }

public String toString()


326. Program {
return "s1 = " + s1 + ", i = " +
package com.lara; i;
class E }
{ }
int i, j; public class Manager6
E(int i, int j) {
{ public static void main(String[] args)
this.i = i; {
this.j = j; F f1 = new F("abc", 22);
} F f2 = new F("abc", 22);
public String toString() System.out.println(f1);
{ System.out.println(f2);
return "i = " + i + " & j = " + }
}
j;

www.laratechnology.com 080-41310124 Page 129


LARA TECHNOLOGIES LARA TECHNOLOGIES

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;

www.laratechnology.com 080-41310124 Page 130


LARA TECHNOLOGIES LARA TECHNOLOGIES

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
}
}

www.laratechnology.com 080-41310124 Page 131


LARA TECHNOLOGIES LARA TECHNOLOGIES

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)); {

www.laratechnology.com 080-41310124 Page 132


LARA TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String[] args) int i, j;


{ G(int i, int j)
E e1 = new E(90); {
E e2 = new E(90); this.i = i;
E e3 = e1; this.j = j;
System.out.println(e1.equals(e2)); }
System.out.println(e2.equals(e3)); public boolean equals(Object obj)
System.out.println(e3.equals(e1)); {
} G myObj = (G) obj;
} return i == myObj.i && j ==
myObj.j;
}
338. Program }
public class Manager6
package com.rst; {
class F public static void main(String[] args)
{ {
int i; G g1 = new G(1, 2);
F(int i) G g2 = new G(1, 2);
{ System.out.println(g1.equals(g2));
this.i = i; }
} }
public boolean equals(Object obj)
{
F myObj = (F) obj; 340. Program
return this.i == myObj.i;
}
package com.rst;
}
class H
public class Manager5
{
{
int i;
public static void main(String[] args)
double d;
{
H(int i, double d)
F f1 = new F(10);
{
F f2 = new F(10);
this.i = i;
this.d = d;
System.out.println(f1.equals(f2)); }
}
} public String toString()
{
return "i = " + i + ", d = " + d;
339. Program }

public boolean equals(Object obj)


package com.rst;
{
class G
H myObj = (H) obj;
{
boolean flag = (myObj.i == i

www.laratechnology.com 080-41310124 Page 133


LARA TECHNOLOGIES LARA TECHNOLOGIES

&& 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();

www.laratechnology.com 080-41310124 Page 134


LARA TECHNOLOGIES LARA TECHNOLOGIES

Q q2 = new Q(); 345. Program


p1.i = 10;
p2.i = 10; package com.rst;
q1.i = 10; class S
q2.i = 10; {
System.out.println(p1.equals(p2)); int i;
System.out.println(q1.equals(q2)); }
System.out.println(p1.equals(q2)); public class Manager11
System.out.println(q1.equals(p1)); {
} public static void main(String[] args)
} {
S s1 = new S();
S s2 = new S();
344. Program s1.i = s2.i = 10;
System.out.println(s1);
package com.rst; System.out.println(s2);
class R System.out.println(s1.equals(s2));
{ System.out.println(s1.hashCode());
int i, j; System.out.println(s2.hashCode());
double weight; }
R(int i, int j, double weight) }
{
this.i = i;
this.j = j; 346. Program
this.weight = weight;
} package com.rst;
class T
public boolean equals(Object obj) {
{ int i;
return (obj instanceof R && T(int i)
i == ((R)obj).i && {
j == ((R)obj).j && this.i = i;
weight == ((R)obj).weight); }
} }
} public class Manager12
public class Manager10 {
{ public static void main(String[] args)
public static void main(String[] args) {
{ T t1 = new T(90);
R r1 = new R(10, 20, 9.98); T t2 = t1;
R r2 = new R(10, 20, 9.98); System.out.println(t1);
System.out.println(r1.equals(r2)); System.out.println(t2);
} System.out.println(t1.equals(t2));
} System.out.println(t1.hashCode());
System.out.println(t2.hashCode());
}

www.laratechnology.com 080-41310124 Page 135


LARA TECHNOLOGIES LARA TECHNOLOGIES

} 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);

System.out.println(u1.equals(u2)); 349. Program

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) {

www.laratechnology.com 080-41310124 Page 136


LARA TECHNOLOGIES LARA TECHNOLOGIES

15. name = n; age = a; comment = c; public boolean equals(Object obj)


{
16. }
return (obj instanceof V &&
17. public boolean equals(Object o) { i == ((V)obj).i &&
j == ((V)obj).j);
18. if(! (o instanceof Person)) return false;
}
19, Person p = (Person)o; public int hashCode()
{
20. return age == p.age &&
String s1 =
name.equals(p.name);
Integer.toString(i);
21. } String s2 =
22. } Integer.toString(j);
int hash = s1.hashCode();
What is the appropriate definition of the
hash += s2.hashCode();
hashCode method in class return hash;
}
Person?
}
A. return super.hashCode(); public class Manager14
{
B. return name.hashCode() + age * 7;
public static void main(String[] args)
C. return name.hashCode() + {
V v1 = new V(10, 40);
comment.hashCode() /2;
V v2 = new V(10, 40);
D. return name.hashCode() + System.out.println(v1);
System.out.println(v2);
comment.hashCode() / 2 - age * 3;
System.out.println(v1.equals(v2));
System.out.println(v1.hashCode());
System.out.println(v2.hashCode());
Answer: B
}
}
350. Program

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()
}

www.laratechnology.com 080-41310124 Page 137


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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()
}
{

www.laratechnology.com 080-41310124 Page 138


LARA TECHNOLOGIES LARA TECHNOLOGIES

} public String toString()


{
return "i = " + i + ", d = " + d
354. Program + ", s1 = " + s1 + ", obj = " + obj;
}
package com.lara; public static void main(String[] args)
public class B implements Cloneable throws
{
CloneNotSupportedException
int i;
{
C c1 = new C(20, 2.8, "abc",
public static void main(String[] args)
throws 45);
System.out.println(c1);
CloneNotSupportedException
C c2 = (C) c1.clone();
{
System.out.println(c2);
B b1 = new B();
c2.i = 40;
b1.i = 10;
c2.d = 200.909;
B b2 = (B)b1.clone();
c2.s1 = "cba";
System.out.println(b2.i);
c2.obj = 400;
b2.i = 20; System.out.println("---");
System.out.println(b1.i); System.out.println(c1);
b1.i = 30; System.out.println(c2);
System.out.println(b2.i); }
System.out.println(b1.i); }
}
}
356. Program

355. Program package com.lara;


class D
package com.lara; {
public class C implements Cloneable int i;
{ }
int i; public class E implements Cloneable
double d; {
String s1; D d1;
Integer obj; int j;
C(int i, double d, String s1, Integer
public static void main(String[] args)
obj)
throws
{
this.i = i; CloneNotSupportedException
this.d = d; {
this.s1 = s1; E e1 = new E();
this.obj = obj; e1.d1 = new D();
} e1.d1.i = 10;

www.laratechnology.com 080-41310124 Page 139


LARA TECHNOLOGIES LARA TECHNOLOGIES

e1.j = 20; g2.f1.i = 40;


g2.j = 50;
E e2 = (E) e1.clone(); System.out.println(g1.f1.i);
System.out.println(e2.d1.i); System.out.println(g1.j);
System.out.println(e2.j); }
}
e2.d1.i = 100;
e2.j = 200;
System.out.println(e1.d1.i);
System.out.println(e1.j); 358. Program
} package com.lara;
} public class M
{
@Override
357. Program protected void finalize() throws
Throwable
package com.lara; {
class F System.out.println("from
{
int i; finalize");
} }
public class G implements Cloneable public static void main(String[] args)
{ {
F f1; M m1 = new M();
int j; m1 = null;
System.out.println("obj
protected Object clone() became abandoned");
throws Runtime.getRuntime().gc();
CloneNotSupportedException try
{ {
Object obj = super.clone();
G g1 = (G) obj; Thread.sleep(60*1000);
g1.f1 = new F(); }
g1.f1.i = f1.i; catch(InterruptedException
return g1;
} ex)
public static void main(String[] args) {
throws ex.printStackTrace();
}
CloneNotSupportedException System.out.println("end");
{ }
G g1 = new G(); }
g1.f1 = new F(); 359. Program
g1.f1.i = 10;
g1.j = 20;
package com.lara;
G g2 = (G) g1.clone();
class A

www.laratechnology.com 080-41310124 Page 140


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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

package com.lara; 362. Program


class B
{ package com.lara;
class D
} {
public class Manager1
{ }
public static void main(String[] args) public class Manager3
{ {
B b1 = new B(); public static void main(String[] args)
B b2 = new B(); {
B b3 = new B(); D d1 = new D();
B b4 = new B(); Class c1 = d1.getClass();
Class c1 = b1.getClass(); Class c2 = null;
Class c2 = b2.getClass(); try
Class c3 = b3.getClass(); {
Class c4 = b4.getClass(); c2 =
System.out.println(c1 == c2);
System.out.println(c2 == c3); Class.forName("com.lara.D");
System.out.println(c3 == c4); }
System.out.println(c4 == c1); catch(ClassNotFoundException ex)
} {
} ex.printStackTrace();
}
Class c3 = D.class;
361. Program System.out.println(c1 == c2);
System.out.println(c2 == c3);

www.laratechnology.com 080-41310124 Page 141


LARA TECHNOLOGIES LARA TECHNOLOGIES

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() }

www.laratechnology.com 080-41310124 Page 142


LARA TECHNOLOGIES LARA TECHNOLOGIES

public class Manager6 {


{ Scanner sc = new
public static void main(String[] args)
Scanner(System.in);
throws Exception
System.out.println("enter class
{
Class c1 = name");
String className = sc.next();
System.out.println("enter method
Class.forName("com.lara.G");
Object obj = name");
String methodName = sc.next();
c1.newInstance();
Class c1 =
((G)obj).test(10, "abc");
Class.forName(className);
Object obj = c1.newInstance();
System.out.println("=========");
Method m1 =
Method m1 =
c1.getDeclaredMethod(methodName);
c1.getDeclaredMethod("test", int.class,
m1.invoke(obj);
String.class); System.out.println("done");
m1.invoke(obj, 20, "xyz"); }
} }
}

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

www.laratechnology.com 080-41310124 Page 143


LARA TECHNOLOGIES LARA TECHNOLOGIES

12. // more code here


13. rbo = null; Answer: BD
14. /* insert code here */
Which statement should be placed at line 14 370. Program
to suggest that the virtual Which statements are true?
machine expend effort toward recycling the A. A class finalize() method cannot be
memory used by the invoked explicitly.
object rbo? B. super.finalize() is called explicitly by any
A. System.gc(); overriding finalize() method.
B. Runtime.gc(); C. The finalize() method for a given Object
C. System.freeMemory(); is called no more than once by the garbage
D. Runtime.getRuntime().growHeap(); collector.
E. Runtime.getRuntime().freeMemory(); D. The order in which finalize() is called on
two objects is based on the order in which
Answer: A two objects became finalizable.
Ans :C
369. Program

Which two are true? (Choose two.) 371. Program


A. A finalizer may NOT be invoked Which two are true? (Choose two.)
explicitly. A. A finalizer may NOT be invoked
B. The finalize method declared in class explicitly.
Object takes no action. B. The finalize method declared in class
C. super.finalize() is called implicitly by any Object takes no action.
overriding finalize method. C. super.finalize() is called implicitly by any
D. The finalize method for a given object overriding finalize method.
will be called no more than D. The finalize method for a given object
once by the garbage collector. will be called no more than
E. The order in which finalize will be called once by the garbage collector.
on two objects is based on E. The order in which finalize will be called
the order in which the two objects became on two objects is based on
finalizable.

www.laratechnology.com 080-41310124 Page 144


LARA TECHNOLOGIES LARA TECHNOLOGIES

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

www.laratechnology.com 080-41310124 Page 145


LARA TECHNOLOGIES LARA TECHNOLOGIES

Object created in line 5 become available for {


String s1="abc";
garbage collection?
s1="xyz";
A. Line 5 System.out.println(s1);
}
B. Line 6
}
C. Line 7
377. Program
D. Line 8
package com.lara;
E. Line 9 public class E
{
F. Line 10
public static void main(String[] args)
{
String s1="Lara";
Answer: D
String s2="rst";
System.out.println(s1);
System.out.println(s2);
374. program System.out.println(s1.equals(s2));
package com.lara; System.out.println(s1.toString());
public class A System.out.println(s2.toString());
{ System.out.println(s1.hashCode());
public static void main(String[] args) System.out.println(s2.hashCode());
{ }
String s1="abc"; }
System.out.println(s1);
} 378. Program
} package com.lara;
public class G
375. Program {
package com.lara; public static void main(String[] args)
public class B {
{ String s1=new String("Lara");
public static void main(String[] args) String s2=new String("Lara");
{ System.out.println(s1);
String s1="abc"; System.out.println(s2);
String s2="abc"; System.out.println(s1.equals(s2));
System.out.println(s1); System.out.println(s2.equals(s1));
}
System.out.println(s2); }
}
} 379. Program
package com.lara;
376. Program public class I
package com.lara; {
public class C public static void main(String[] args)
{ {
public static void main(String[] args)

www.laratechnology.com 080-41310124 Page 146


LARA TECHNOLOGIES LARA TECHNOLOGIES

String s1="Lara"; public static void main(String[] args)


String s2="Lara"; {
String s3=new String("Lara");
String s4=new String("Lara"); String s1="java";
System.out.println(s1); String s2="ja"+"va";
System.out.println(s2); System.out.println(s1==s2);
System.out.println(s3); }
System.out.println(s4); }
System.out.println("___________");
System.out.println(s1==s2);
382. Program
System.out.println(s1==s3);
package com.lara;
public class L
System.out.println(s1==s4); {
System.out.println(s3==s4); public static void main(String[] args)
System.out.println("___________");
System.out.println(s1.equals(s2)); {
System.out.println(s1.equals(s3));
System.out.println(s1.equals(s4)); String s1="java";
System.out.println("___________"); String s2="ja";
System.out.println(s1.hashCode()== String s3=s2+"va";
s2.hashCode()); System.out.println(s1==s3);
System.out.println(s1.hashCode()== }
s3.hashCode()); }
System.out.println(s1.hashCode()==
s4.hashCode()); 383. Program
System.out.println("___Done___"); package com.lara;
} public class M
} {
public static void main(String[] args)
{
380. Program String s1="java";
package com.lara; String s2="ja";
public class J String s3="va";
{ String s4=s2+s3;
public static void main(String[] args)
System.out.println(s1==s4);
{
}
String s1="Lara";
}
System.out.println(s1);
s1=s1+"Rst";
384. Program
System.out.println(s1);
package com.lara;
}
public class N
}
{
381. Program
public static void main(String[] args)
package com.lara;
{
public class K
String s1=null;
{
System.out.println(s1);

www.laratechnology.com 080-41310124 Page 147


LARA TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(s1); public static void main(String[] args)


s1=s1+s1+null; {
System.out.println(s1); String
s1="abc";System.out.println(s1);s1.concat("
}
} Rst");
System.out.println(s1);
}
385. Program
}
package com.lara;
public class O
388. Program
{
package com.lara;
public static void main(String[] args)
public class R
{
{
System.out.println(2+4);
public static void main(String[] args)
System.out.println(2+4+"lara");
{
System.out.println(2+"lara"+4);
String s1="Lara";
System.out.println("lara"+2+4);
s1=s1.concat("Rst");
System.out.println("lara"+2+null);
System.out.println(s1);
System.out.println(null+"abc");
}
} }
}
389. Program
386. Program package com.lara;
package com.lara; public class S
public class O {
{ public static void main(String[] args)
public static void main(String[] args) {
{ String s1="ja";
String s2="va";
System.out.println(2+4); String s3=s1.concat(s2);
System.out.println(2+4+"lara"); String s4="java";
System.out.println(2+"lara"+4); System.out.println(s3==s4);
System.out.println("lara"+2+4); }
System.out.println("lara"+2+null); }
System.out.println(null+"abc");
} 390. Program
} package com.lara;
public class T
{
public static void main(String[] args)
{
387. Program
String s1="ja"+"va";
package com.lara; String s2="java";
public class Q System.out.println(s1==s2);
{ }

www.laratechnology.com 080-41310124 Page 148


LARA TECHNOLOGIES LARA TECHNOLOGIES

} 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));

394. Program System.out.println(s1.charAt(6));


package com.lara;
public class X
System.out.println(s1.charAt(2));
{
}
public static void main(String[] args)
}
{
String s1="null";

www.laratechnology.com 080-41310124 Page 149


LARA TECHNOLOGIES LARA TECHNOLOGIES

398. Program 17. }


Given: And the invocation:
11. public static void test(String str) { 31. test(null);
12. int check = 4; What is the result?
13. if (check = str.length()) { A. Au exception is thrown at runtime.
14. System.out.print(str.charAt(check -= 1) B. “String is empty” is printed to output.
+“, “); C. Compilation fails because of au error in
15. } else { line 12.
16. System.out.print(str.charAt(0) + “, “); D. “String is not empty” is printed to
17. } output.
18. } Answer: A
and the invocation:
21. test(”four”);
22. test(”tee”); 400. Program
package com.lara;
23. test(”to”); public class M2
What is the result? {
public static void main(String[] args)
A. r, t, t, {
B. r, e, o, String s1="a1b2c3a1b4";
System.out.println(s1.indexOf('a'));
C. Compilation fails. System.out.println(s1.indexOf('a',4));
D. An exception is thrown at runtime. System.out.println(s1.lastIndexOf('b'));
System.out.println(s1.lastIndexOf('b',4);
Answer: C }
}

399. Program 401. Program


package com.lara;
Given:
public class M3
11. public static void test(String str) { {
public static void main(String[] args)
12. if(str == null | str.lellgth() == 0) {
{
13. System.out.println(”String is empty”); String s1="a1b2c3a1b4";
System.out.println(s1.indexOf('z'));
14. } else {
System.out.println(s1.lastIndexOf('p'));
15. System.out.println(”String is not }
}
empty”);
16. } 402. Program

www.laratechnology.com 080-41310124 Page 150


LARA TECHNOLOGIES LARA TECHNOLOGIES

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

www.laratechnology.com 080-41310124 Page 151


LARA TECHNOLOGIES LARA TECHNOLOGIES

package com.lara; System.out.println(sb.length());


public class C System.out.println(sb.capacity());
{ System.out.println("------------");
public static void main(String[] args) sb.append("abcabcabcabcabcabcabc");
{ System.out.println(sb.length());
StringBuffer sb=new StringBuffer("abc"); System.out.println(sb.capacity());
StringBuffer sb1=new StringBuffer("abc"); System.out.println("------------");
System.out.println(sb.toString()); sb.trimToSize();
System.out.println(sb1.toString()); System.out.println(sb.capacity());
System.out.println(sb==sb1); System.out.println(sb.length());
, System.out.println(sb.hashCode()); }
System.out.println(sb1.hashCode()); }
System.out.println(sb.equals(sb1));
} 411. Program
}
package com.lara;
409. Program public class F
{
package com.lara; public static void main(String[] args)
public class D {
{ StringBuffer sb=new StringBuffer();
public static void main(String[] args) sb.append("abc");
{ sb.append("xyz");
StringBuffer sb=new StringBuffer(); sb.append("rst");
sb.append("abcabcabcabcabc"); System.out.println(sb);
System.out.println(sb); sb.reverse();
System.out.println(sb.capacity()); System.out.println(sb);
System.out.println(sb.length()); }
sb.trimToSize(); }
System.out.println(sb.capacity());
System.out.println(sb.length()); 412. Program
}
} package com.lara;
public class G
{
410. Program public static void main(String[] args)
package com.lara; {
StringBuffer sb=new StringBuffer();
sb.append("abc");
public class E
sb.append("xyz");
{
sb.append("rst");
public static void main(String[] args)
System.out.println(sb);
{
sb.delete(3, 6);
StringBuffer sb=new StringBuffer();
System.out.println(sb);
System.out.println(sb.capacity());
}
sb.append("abcabcabcabcabcabcabc");
}

www.laratechnology.com 080-41310124 Page 152


LARA TECHNOLOGIES LARA TECHNOLOGIES

23. String s1=”123”;


413. Program
24. //insert code here
package com.lara; 25. System.out.println(sb1+””+s1);
public class H Which code fragment, inserted at line
{
public static void main(String[] args) 24,output “123abc 123abc”?
{ A. sb1.append(“abc”);s1.append(“abc”);
StringBuilder sb=new StringBuilder();
sb.append("abc\n"); B. sb1.append(“abc”);s1.concat(“abc”);
sb.append("xyz\n"); C. sb1.concat(“abc”);s1.append(“abc”);
sb.append("rst");
System.out.println(sb); D. sb1.concat(“abc”);s1.concat(“abc”);
} E. sb1.appemd(“abc”);s1=s1.concat(“abc”);
}
F. sb1.concat(“abc”);s1=s1.concat(“abc”);
G. sb1.appemd(“abc”);s1=s1+s1.concat(“ab
414. Program
c”);
Which two scenario’s are not safe to
H. sb1.concat(“abc”);s1=s1+s1.concat(“abc
replace a StringBuffer object with a
”);
StringBuilder object? (Choose two)
A. When using versions of java technology
Ans : E
earlier than 5.0.
B. When sharing a StringBuffer among
416. Program
multiple threads.
Given
C.When using the java.io.class
1.public class KungFu{
StringBufferInputStream.
2. public static void main (String args[]){
D. When u plan to reuse the StringBuffer to
3. Integer x=400;
build morethan one Strong.
4. Integer y=x;
Ans: A, B
5. x++;
6. StringBuilder sb1=new
415. Program
StringBuilder(“123”);
Given
7. StringBuilder sb2=sb1;
8. sb1.append(“5”);
22.StringBuilder sb1=new
9. System.out.println((x==y)+””+
StringBuilder(“123”);
(sb1==sb2));

www.laratechnology.com 080-41310124 Page 153


LARA TECHNOLOGIES LARA TECHNOLOGIES

10.} 418. Program

11.} Given this method in a class:


What is the result? 21. public String toString() {
A. true true 22. StringBuffer buffer = new
B. false true StringBuffer();
C. true false 23. buffer.append(’<’);
D. false false 24. buffer.append(this.name);
E. Compilation fails 25. buffer.append(’>’);
F. An exception is thrown at runtime 26. return buffer.toString();
Ans : B 27. }
Which is true?
417. Program A. This code is NOT thread-safe.
Given: B. The programmer can replace
1. public class TestString 1 { StringBuffer with StringBuilder with no
2. public static void main(String[] args) other changes.
{ C. This code will perform well and
3. String str = “420”; converting the code to use
4. str += 42; StringBuilder will not enhance the
5. System.out.print(str); performance.
6. } D. This code will perform poorly. For better
7. } performance, the code
What is the output? should be rewritten: return “<“+
A. 42 this.name + “>”;
B. 420
C. 462 Answer: B
D. 42042
E. Compilation fails. 419. Program

F. An exception is thrown at runtime. Given:


1. public class MyLogger {
Answer: D 2. private StringBuilder logger = new
StringBuuilder();

www.laratechnology.com 080-41310124 Page 154


LARA TECHNOLOGIES LARA TECHNOLOGIES

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. }

www.laratechnology.com 080-41310124 Page 155


LARA TECHNOLOGIES LARA TECHNOLOGIES

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]");

www.laratechnology.com 080-41310124 Page 156


LARA TECHNOLOGIES LARA TECHNOLOGIES

Matcher m1=p1.matcher(s1); Pattern p1=


while(m1.find())
Pattern.compile("[1-9]");
{
Matcher m1=p1.matcher(s1);
System.out.println(m1.start()
while(m1.find())
+":"+m1.group()); {
} System.out.println(m1.start()
}
+":"+m1.group());
}
}
426. Program }

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;

www.laratechnology.com 080-41310124 Page 157


LARA TECHNOLOGIES LARA TECHNOLOGIES

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())
}
{
}
}

www.laratechnology.com 080-41310124 Page 158


LARA TECHNOLOGIES LARA TECHNOLOGIES

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?

www.laratechnology.com 080-41310124 Page 159


LARA TECHNOLOGIES LARA TECHNOLOGIES

A. 0 “Test A,” “Test B,” and “Test C”?


B. 1 A. String regex = “;
C. 4 B. String regex = “ “;
D. Compilation fails. C. String regex = “.*“.
E. An exception is thrown at runtime. D. String regex = “\\s”
E. String regex = “\\.\\s*”;
Answer: D F. String regex = “\\w[ \.] +“;

438. Program Answer: E


Given:
11. String test= “a1b2c3”; 440. Program

12. String[] tokens = test.split(”\\d”);


package com.lara;
13. for(String s: tokens) System.out.print(s import java.util.StringTokenizer;
+“ “); public class P
{
What is the result? public static void main(String[] args)
A. a b c {
StringTokenizer st=new
B. 1 2 3
StringTokenizer("abc:xyz:hello",":");
C. a1b2c3 while (st.hasMoreElements())
D. a1 b2 c3 {
System.out.println(st.nextElement());
E. Compilation fails. }
F. The code runs with no output. }
}
G. An exception is thrown at runtime.

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())

www.laratechnology.com 080-41310124 Page 160


LARA TECHNOLOGIES LARA TECHNOLOGIES

{
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)

www.laratechnology.com 080-41310124 Page 161


LARA TECHNOLOGIES LARA TECHNOLOGIES

{ 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
}

452. Program 455. Program

www.laratechnology.com 080-41310124 Page 162


LARA TECHNOLOGIES LARA TECHNOLOGIES

package com.lara; F. System.out.printf(“|%6.3f|\n”,d);


public class I
Ans:F
{
public static void main(String[] args)
{
System.out.printf("<%- 458. Program

+,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; }

4. //insert code here


5.} 460. Program

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);

www.laratechnology.com 080-41310124 Page 163


LARA TECHNOLOGIES LARA TECHNOLOGIES

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
{

463. Program public static void main(String[] args)


{
package com.lara; Date d1=new Date();
import java.util.Date; System.out.println(d1);
public class C long millis=d1.getTime();
{ System.out.println(millis);
public static void main(String[] args) }
{
Date d1=new }
Date(24*1000*60*60);
System.out.println(d1);
long millis=d1.getTime(); 466. Program
System.out.println(millis);

www.laratechnology.com 080-41310124 Page 164


LARA TECHNOLOGIES LARA TECHNOLOGIES

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)
}
{
}

www.laratechnology.com 080-41310124 Page 165


LARA TECHNOLOGIES LARA TECHNOLOGIES

Calendar c1=Calendar.getInstance(); 474. Program


c1.add(Calendar.YEAR, 2);
Date d1=c1.getTime(); package com.lara;
System.out.println(d1); import java.text.DateFormat;
} import java.util.Date;
} public class E
{
public static void main(String[] args)
472. Program {
Date d1=new Date();
package com.lara; System.out.println(d1);
import java.util.Calendar;
import java.util.Date; DateFormat
public class D df=DateFormat.getDateInstance(DateFormat
{
public static void main(String[] args) .MEDIUM);
{ String s1=df.format(d1);
Calendar c1=Calendar.getInstance(); System.out.println(s1);
c1.add(Calendar.YEAR, -2); }
Date d1=c1.getTime(); }
System.out.println(d1);
}
}
475. Program

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

www.laratechnology.com 080-41310124 Page 166


LARA TECHNOLOGIES LARA TECHNOLOGIES

package com.lara; package com.lara;


import java.text.DateFormat; import java.text.NumberFormat;
import java.util.Date; import java.util.Locale;
import java.util.Locale; public class G
public class F {
{ public static void main(String[] args)
public static void main(String[] args) {
{ double d1=102929.8789789;
Date d1=new Date(); System.out.println(d1);
System.out.println(d1); NumberFormat
DateFormat
nf1=NumberFormat.getInstance();
df=DateFormat.getDateInstance(DateFormat String s1=nf1.format(d1);
System.out.println(s1);
.SHORT, new Locale("it"));
}
String s1=df.format(d1);
}
System.out.println(s1);

} 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

www.laratechnology.com 080-41310124 Page 167


LARA TECHNOLOGIES LARA TECHNOLOGIES

version of d’s date? E. The value of a is 3.1415.


A. Locale loc = Locale.getLocale(); F. The value of a is 3.1416.
System.out.println(loc.getDisplayCountry() G. The value of b is 2.0000.
+ “ “+ df.format(d));
B. Locale loc = Locale.getDefault(); Answer: CF
System.out.println(loc.getDisplayCountry()
+ “ “ + df.format(d)); 482. Program
C. Locale bc = Locale.getLocale(); Given:
System.out.println(loc.getDisplayCountry() 11. double input = 314159.26;
+ “ “+ df.setDateFormat(d)); 12. NumberFormat nf=
D. Locale loc = Locale.getDefault(); NumberFormat.getInstance(Locale.ITALIA
System.out.println(loc.getDispbayCountry() N);
+ “ “+ df.setDateFormat(d)); 13. String b;
14. //insert code here
Answer: B Which code, inserted at line 14, sets the
value of b to 3 14.159,26?
481. Program A. b = nf.parse( input);
Given: B. b = nf.format( input);
12. NumberFormat nf= C. b = nf.equals( input);
NumberFormat.getInstance(); D. b = nf.parseObject( input);
13. nf.setMaximumFractionDigits(4);
14. nf.setMinimumFractionDigits(2); Answer: B
15. String a = nf.format(3.1415926);
483. Program
16. String b = nf.format(2);
Given:
Which two are true about the result if the
14. DateFormat df;
default locale is Locale.US?
15. Date date = new Date();
(Choose two.)
16. //insert code here
A. The value of b is 2.
17. String s = df.format( date);
B. The value of a is 3.14.
Which two, inserted independently at line
C. The value of b is 2.00.
16, allow the code to
D. The value of a is 3.141.
compile? (Choose two.)

www.laratechnology.com 080-41310124 Page 168


LARA TECHNOLOGIES LARA TECHNOLOGIES

A. df= new DateFormat(); 35. // insert code here


B. df= Date.getFormatter(); 36. try {
C. df= date.getFormatter(); 37. d = df.parse(ds);
D. df= date.getDateFormatter(); 38. }
E. df= Date.getDateFormatter(); 39. catch(ParseException e) {
F. df= DateFormat.getInstance(); 40. System.out.println(”Unable to parse “+
G. df = DateFormat.getDateInstance(); ds);
41. }
Answer: FG 42. // insert code here too
Which will create the appropriate
484. Program
DateFormat object and add a day to
Given:
the Date object?
12. Date date = new Date();
A. 35. DateFormat df=
13. df.setLocale(Locale.ITALY);
DateFormat.getDateFormat();
14. String s = df.format(date);
42. d.setTime( (60 * 60 * 24) +
The variable df is an object of type
d.getTime());
DateFormat that has been
B. 35. DateFormat df=
initialized in line 11. What is the result if
DateFormat.getDateJnstance(); 42.
this code is run on December
d.setTime( (1000 * 60 * 60 * 24) +
14, 2000?
d.getTime());
A. The value of s is 14-dic-2004.
C. 35. DateFormat df=
B. The value of s is Dec 14, 2000.
DateFormat.getDateFormat();
C. An exception is thrown at runtime.
42. d.setLocalTime( (1000*60*60*24) +
D. Compilation fails because of an error in
d.getLocalTime());
line 13.
D. 35. DateFormat df=
DateFormat.getDateJnstance();
Answer: D
42. d.setLocalTime( (60 * 60 * 24) +
d.getLocalTime());
485. Program

Given:
Answer: B
33. Date d = new Date(0);
34. String ds = “December 15, 2004”; 486. Program

www.laratechnology.com 080-41310124 Page 169


LARA TECHNOLOGIES LARA TECHNOLOGIES

Given a valid DateFormat object named df,


and
488. Program
16. Date d = new Date(0L);
A developer is creating a class Book that
17. String ds = “December 15, 2004”;
needs to access class Paper.
18. // insert code here
The Paper class is deployed in a JAR named
What updates d’s value with the date
myLib.jar. Which three,
represented by ds?
taken independently, will allow the
A. 18. d = df.parse(ds);
developer to use the Paper class
B. 18. d = df.getDate(ds);
while compiling the Book class? (Choose
C. 18. try {
three.)
19. d = df.parse(ds);
A. The JAR file is located at
20. } catch(ParseException e) { };
$JAVA_HOME/jre/classes/myLib.jar.
D. 18. try {
B. The JAR file is located at
19. d = df.getDate(ds);
$JAVA_HOME/jre/lib/ext/myLib.jar.
20. } catch(ParseException e) { };
C. The JAR file is located at /foo/myLib.jar
and a classpath
Answer: C
environment variable is set that includes
/foo/myLib.jar/Paper.class.
487. Program D. The JAR file is located at /foo/myLib.jar
and a classpath
package com.lara;
import java.text.NumberFormat; environment variable is set that includes
import java.util.Locale; /foo/myLib.jar.
public class G
{ E. The JAR file is located at /foo/myLib.jar
public static void main(String[] args) and the Book class is
{
double d1=102929.8789789; compiled using javac -cp
System.out.println(d1); /foo/myLib.jar/Paper Book.java.
NumberFormat
F. The JAR file is located at /foo/myLib.jar
nf1=NumberFormat.getCurrencyInstance(L
and the Book class is
ocale.UK);
String s1=nf1.format(d1); compiled using javac -d /foo/myLib.jar
System.out.println(s1); Book.java.
}
}

www.laratechnology.com 080-41310124 Page 170


LARA TECHNOLOGIES LARA TECHNOLOGIES

G. The JAR file is located at /foo/myLib.jar /apps/com/company/application directory


and the Book class is E. java -classpath
compiled using javac -classpath /apps/com/company/application:. MainClass
/foo/myLib.jar Book.java. if run
from the /apps directory
Answer: BDG F. java com.company.application.MainClass
if run from the
489. Program /apps/com/company/application directory
Given:
1. package com.company.application; Answer: BC
2.
490. Program
3. public class MainClass {
Given a correctly compiled class whose
4. public static void main(String[] args) { }
source code is:
5. }
1. package com.sun.sjcp;
And MainClass exists in the
2. public class Commander {
/apps/com/company/application directory.
3. public static void main(String[] args) {
Assume the CLASSPATH environment
4. // more code here
variable is set to “.“ (current
5. }
directory). Which two java commands
6. }
entered at the command line
Assume that the class file is located in
will run MainClass? (Choose two.)
/foo/com/sun/sjcp/, the current
A. java MainClass if run from the /apps
directory is /foo/, and that the classpath
directory
contains “.“ (current
B. java com.company.application.MainClass
directory).
if run from the /apps
Which command line correctly runs
directory
Commander?
C. java -classpath /apps
A. java Commander
com.company.application.MainClass if run
B. java com. sim. sjcp.Commander
from any directory
C. java com/sun/sjcp/Commander
D. java -classpath . MainClass if run from
D. java -cp com.sun.sjcp Commander
the
E. java -cp com/sun/sjcp Commander

www.laratechnology.com 080-41310124 Page 171


LARA TECHNOLOGIES LARA TECHNOLOGIES

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

www.laratechnology.com 080-41310124 Page 172


LARA TECHNOLOGIES LARA TECHNOLOGIES

Given: 16. case collie:


11. public class Ball { 17. System.out.print(”collie “);
12. public enum Color { RED, GREEN, 18. case default:
BLUE }; 19. System.out.print(”retriever “);
13. public void foo() { 20. case harrier: 21.
14. // insert code here System.out.print(”harrier “);
15. { System.out.println(c); } 22. }
16. } 23. }
17. } 24. }
Which code inserted at line 14 causes the ‘What is the result?
foo method to print RED, A. harrier
GREEN, and BLUE? B. shepherd
A. for( Color c : Color.values()) C. retriever
B. for( Color c = RED; c <= BLUE; c++) D. Compilation fails.
C. for( Color c; c.hasNext() ; c.next()) E. retriever harrier
D. for( Color c = Color[0]; c <= Color[2]; F. An exception is thrown at runtime.
c++)
E. for( Color c = Color.RED; c <= Answer: D
Color.BLUE; c++)
495. Program
Answer: A Given:
12. public class Test {
13. public enum Dogs {collie, harrier};
494. Program 14. public static void main(String [] args) {
Given: 15. Dogs myDog = Dogs.collie;
11. public class Test { 16. switch (myDog) {
12. public enum Dogs {collie, harrier, 17. case collie:
shepherd}; 18. System.out.print(”collie “);
13. public static void main(String [] args) { 19. case harrier:
14. Dogs myDog = Dogs.shepherd; 20. System.out.print(”harrier “);
15. switch (myDog) { 21. }

www.laratechnology.com 080-41310124 Page 173


LARA TECHNOLOGIES LARA TECHNOLOGIES

22. } D. if( RED.getRGB() < BLUE.getRGB() )


23. } {}
What is the result? E. Color purple = Color.BLUE +
A. collie Color.RED;
B. harrier F. if( Color.RED.ordinal() <
C. Compilation fails. Color.BLUE.ordinal() ) {}
D. collie harrier
E. An exception is thrown at runtime. Answer: BF

Answer: D

496. Program 497. Program


Given:
Given:
10. class Nav{
10. public class Fabric
11. public enum Direction { NORTH,
11. public enum Color {
SOUTH, EAST, WEST }
12. RED(0xff0000), GREEN(0x00ff00),
12. }
BLUE(0x0000ff);
13. public class Sprite{
13. private final int rgb;
14. // insert code here
14. Color( int rgb) { this.rgb = rgb; }
15. }
15. public int getRGB() { return rgb; }
Which code, inserted at line 14, allows the
16. };
Sprite class to compile?
17. public static void main( String[] argv) {
A. Direction d = NORTH;
18. // insert code here
B. Nav.Direction d = NORTH;
19. }
C. Direction d = Direction.NORTH;
20. }
D. Nav.Direction d =
Which two code fragments, inserted
Nav.Direction.NORTH;
independently at line 18, allow the
Fabric class to compile? (Choose two.)
Answer: D
A. Color skyColor = BLUE;
B. Color treeColor = Color.GREEN; 498. Program
C. Color purple = new Color( 0xff00ff); Given:
11. public enum Title {

www.laratechnology.com 080-41310124 Page 174


LARA TECHNOLOGIES LARA TECHNOLOGIES

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;

www.laratechnology.com 080-41310124 Page 175


LARA TECHNOLOGIES LARA TECHNOLOGIES

5. public static void main( String[] argv) 3. public class Demo {


6. { System.out.println( GREEN); } 4. public static void main(String[] args) {
7. } 5. System.out.println(twice(”pizza”));
The class Beta and the enum Color are in 6. }
different packages. 7. }
Which two code fragments, inserted Which code should be inserted at line 1 of
individually at line 2 of the Beta Demo.java to compile and
declaration, will allow this code to compile? run Demo to print “pizzapizza”?
(Choose two.) A. import utils.*;
A. import sun.scjp.Color.*; B. static import utils.*;
B. import static sun.scjp.Color.*; C. import utils.Repetition.*;
C. import sun.scjp.Color; import static D. static import utils.Repetition. *;
sun.scjp.Color.*; E. import utils.Repetition.twice();
D. import sun.scjp.*; import static F. import static utils.Repetition.twice;
sun.scjp.Color.*; G. static import utils.Repetition.twice;
E. import sun.scjp.Color; import static
Answer:F
sun.scjp.Color.GREEN; 502. Program

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.

www.laratechnology.com 080-41310124 Page 176


LARA TECHNOLOGIES LARA TECHNOLOGIES

C. The Point class cannot be instatiated at 3. public InnerTriangle it = new


line 15. InnerTriangle();
D. Line 1 = new Line() ; 1.Point p = new 4. class InnerTriangle {
1.Point(); 5. public int base;
6. public int height;
Answer: B 7. }
8. }
503. Program
Which is true about the class of an object
Given:
that can reference the
10. class Line {
variable base?
11. public class Point { public int x,y; }
A. It can be any class.
12. public Point getPoint() { return new
B. No class has access to base.
Point(); }
C. The class must belong to the geometry
13. }
package.
14. class Triangle {
D. The class must be a subclass of the class
15. public Triangle() {
Hypotenuse.
16. // insert code here
17. }18. }
Answer: C
Which code, inserted at line 16, correctly
retrieves a local instance of a 505. Program

Point object? Click the Exhibit button.


A. Point p = Line.getPoint(); 10. interface Foo {
B. Line.Point p = Line.getPoint(); 11. int bar();
C. Point p = (new Line()).getPoint(); 12. }
D. Line.Point p = (new Line()).getPoint(); 13.
14. public class Beta {
Answer: D 15.
16. class A implements Foo {
504. Program 17. public int bar() { return 1; }
Given: 18. }
1. package geometry; 19.
2. public class Hypotenuse {

www.laratechnology.com 080-41310124 Page 177


LARA TECHNOLOGIES LARA TECHNOLOGIES

20. public int fubar( Foo foo) { return Answer: BEF


foo.bar(); }
506. Program
21.
Given:
22. public void testFoo() {
1. interface TestA { String toString(); }
23.
2. public class Test {
24. class A implements Foo {
3. public static void main(String[] args) {
25. public int bar() { return 2; }
4. System.out.println(new TestA() {
26. }
5. public String toString() { return “test”; }
27.
6. });
28. System.out.println( fubar( new A()));
7. }
29. }
8. }
30.
What is the result?
31. public static void main( String[] argv) {
A. test
32. new Beta().testFoo();
B. null
33. }
C. An exception is thrown at runtime.
34. }
D. Compilation fails because of an error in
Which three statements are true? (Choose
line 1.
three.)
E. Compilation fails because of an error in
A. Compilation fails.
line 4.
B. The code compiles and the output is 2.
F. Compilation fails because of an error in
C. If lines 16, 17 and 18 were removed,
line 5.
compilation would fail.
D. If lines 24, 25 and 26 were removed,
Answer: A
compilation would fail.
E. If lines 16, 17 and 18 were removed, the
507. Program
code would compile and
the output would be 2.
Given:
F. If lines 24, 25 and 26 were removed, the
10. interface Foo { int bar(); }
code would compile and
11. public class Sprite {
the output would be 1.
12. public int fubar( Foo foo) { return
foo.bar(); }

www.laratechnology.com 080-41310124 Page 178


LARA TECHNOLOGIES LARA TECHNOLOGIES

13. public void testFoo() { C. An exception is thrown at runtime.


14. fubar( D. Compilation fails because of an error in
15. // insert code here line 13.
16.);
17. } Answer: D
18. }
Which code, inserted at line 15, allows the 509. Program
class Sprite to compile? Given:
A. Foo { public int bar() { return 1; } } 10. public class MyClass {
B. new Foo { public int bar() { return 1; } } 11.
C. newFoo() { public int bar(){return 1; } } 12. public Integer startingI;
D. new class Foo { public int bar() { return 13. public void methodA() {
1; } } 14. Integer i = new Integer(25);
15. startingI = i;
Answer: C 16. methodB(i);
17. }
508. Program
18. private void methodB(Integer i2) {
Given:
19. i2 = i2.intValue();
11. class Converter {
20.
12. public static void main(String[] args) {
21. }
13. Integer i = args[0];
22. }
14. int j = 12;
If methodA is invoked, which two are true at
15. System.out.println(”It is “ + (j==i) +
line 20? (Choose two.)
“that j==i.”);
A. i2 == startingI returns true.
16. }
B. i2 == startingI returns false.
17. }
C. i2.equals(startingI) returns true.
What is the result when the programmer
D. i2.equals(startingI) returns false.
attempts to compile the code
and run it with the command java Converter
Answer: BC
12?
A. It is true that j==i. 510. Program
B. It is false that j==i. Given

www.laratechnology.com 080-41310124 Page 179


LARA TECHNOLOGIES LARA TECHNOLOGIES

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?

www.laratechnology.com 080-41310124 Page 180


LARA TECHNOLOGIES LARA TECHNOLOGIES

A. short LONG 514. Program

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

www.laratechnology.com 080-41310124 Page 181


LARA TECHNOLOGIES LARA TECHNOLOGIES

the class to compile? (Choose two.) 12. //insert code here


A. foreach(x) System.out.println(z); 13. System.out.println(“hi”);
B. for(int z : x) System.out.println(z); 14.}
C. while( x.hasNext()) 15.}
System.out.println( x.next()); And the following five fragments
D. for( int i=0; i< x.length; i++ ) A.public static void main(String…a)
System.out.println(x[i]); B.public static void main(String.*a)
C.public static void main(String… a)
Answer: BD D.public static void main(String[]…a)
E.public static void main(String…[]a)
516. Program

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.

www.laratechnology.com 080-41310124 Page 182


LARA TECHNOLOGIES LARA TECHNOLOGIES

17. for(int x = 1; x < args.length; x++) {


Answer: D 18. System.out.print(args[x] +“ “);
19. }
519. Program 20. }
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

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) { {

www.laratechnology.com 080-41310124 Page 183


LARA TECHNOLOGIES LARA TECHNOLOGIES

15. for(int x= 1; x<yahoo.length; x++) { A. System.load(”prop.custom”);


16. System.out.print(yahoo[x] + “ “); B. System.getenv(”prop.custom”);
17. } C. System.property(”prop.custom”);
18. } D. System.getProperty(”prop.custom”);
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
523. Program
C.a b c
Given:
D. Compilation fails.
10. public class ClassA {
E. An exception is thrown at runtime.
11. public void count(int i) {
12. count(++i);
Answer: B
13. }
14. }
And:
522. Program
20. ClassA a = new ClassA();
Given:
21. a.count(3);
11. public class Commander {
Which exception or error should be thrown
12. public static void main(String[] args) {
by the virtual machine?
13. String myProp = /* insert code here */
A. StackOverflowError
14. System.out.println(myProp);
B. NullPointerException
15. }
C. NumberFormatException
16. }
D. IllegalArgumentException
and the command line:
E. ExceptionlnlnitializerError
java -Dprop.custom=gobstopper
Commander
Answer: A
Which two, placed on line 13, will produce
the output gobstopper?
(Choose two.) 524. Program

Given:

www.laratechnology.com 080-41310124 Page 184


LARA TECHNOLOGIES LARA TECHNOLOGIES

1. public class Boxer1 { 35. // some code here


2. Integer i; 36. } finally {
3. int x; 37. // some code here
4. public Boxer1(int y) { 38. }
5. x=i+y; Under which three circumstances will the
6. System.out.println(x); code on line 37 be executed?
7. } (Choose three.)
8. public static void main(String[] args) { A. The instance gets garbage collected.
9. new Boxer1(new Integer(4)); B. The code on line 33 throws an exception.
10. } C. The code on line 35 throws an exception.
11. } D. The code on line 31 throws an exception.
What is the result? E. The code on line 33 executes
A. The value “4” is printed at the command successfully.
line.
B. Compilation fails because of an error in Answer: BCE
line 5.
526. Program
C. Compilation fails because of an error in
Given:
line 9.
11. public static void parse(String str) {
D. A NullPointerException occurs at
12. try {
runtime.
13. float f= Float.parseFloat(str);
E. A NumberFormatException occurs at
14. } catch (NumberFormatException nfe) {
runtime.
15. f= 0;
F. An IllegalStateException occurs at
16. } finally {
runtime.
17. System.out.println(f);
Answer: D
18. }
525. Program 19. }
Given: 20. public static void main(String[] args) {
31. // some code here 21. parse(”invalid”);
32. try { 22. }
33. // some code here What is the result?
34. } catch (SomeException se) { A. 0.0

www.laratechnology.com 080-41310124 Page 185


LARA TECHNOLOGIES LARA TECHNOLOGIES

B. Compilation fails. Given:


C. A ParseException is thrown by the parse 11.classA {
method at runtime. 12. public void process()
D. A NumberFormatException is thrown by { System.out.print(”A,”); } }
the parse method at 13. class B extends A {
runtime. 14. public void process() throws
IOException {
Answer: B 15. super.process();
16. System.out.print(”B,”);
527. Program
17. throw new IOException();
Given:
18. } }
33. try {
19. public static void main(String[] args) {
34. // some code here
20. try { new B().process(); }
35. } catch (NullPointerException e1) {
21. catch (IOException e)
36. System.out.print(”a”);
{ System.out.println(”Exception”); } }
37. } catch (RuntimeException e2) {
What is the result?
38. System.out.print(”b”);
A. Exception
39. } finally {
B. A,B,Exception
40. System.out.print(”c”);
C. Compilation fails because of an error in
41. }
line 20.
What is the result if a NullPointerException
D. Compilation fails because of an error in
occurs on line 34?
line 14.
A. c
E. A NullPointerException is thrown at
B. a
runtime.
C. ab
D. ac
Answer: D
E. bc
F. abc
529. Program

Given:
Answer: D
11.classA {
528. Program

www.laratechnology.com 080-41310124 Page 186


LARA TECHNOLOGIES LARA TECHNOLOGIES

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. 531. 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. }

www.laratechnology.com 080-41310124 Page 187


LARA TECHNOLOGIES LARA TECHNOLOGIES

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:

www.laratechnology.com 080-41310124 Page 188


LARA TECHNOLOGIES LARA TECHNOLOGIES

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. }

www.laratechnology.com 080-41310124 Page 189


LARA TECHNOLOGIES LARA TECHNOLOGIES

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;

www.laratechnology.com 080-41310124 Page 190


LARA TECHNOLOGIES LARA TECHNOLOGIES

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?
45. A a=new A(); A. null
46. System.out.println(a.sayHello(”John”)); B. finally
Which two are true? (Choose two.) C. null finally
A. Class A will not compile. D. Compilation fails.
B. Line 46 can throw the unchecked E. finally exception
exception TestException.
C. Line 45 can throw the unchecked Answer: E
exception TestException.
D. Line 46 will compile if the enclosing 539. Program
method throws a TestException. Given:
E. Line 46 will compile if enclosed in a try 10. public class Foo {
block, where TestException 11. static int[] a;
is caught. 12. static { a[0]=2; }
13. public static void main( String[] args) {}
Answer: DE 14. }
Which exception or error will be thrown
538. Program when a programmer attempts
Given: to run this code?
11. static void test() { A. java.lang. StackOverflowError
12. try { B. java.lang.IllegalStateException
13. String x=null; C. java.lang.ExceptionlnlnitializerError
14. System.out.print(x.toString() +“ “); D.
15. } java.lang.ArraylndexOutOfBoundsExceptio
16. finally { System.out.print(“finally “); } n
17. }
18. public static void main(String[] args) { Answer: C

www.laratechnology.com 080-41310124 Page 191


LARA TECHNOLOGIES LARA TECHNOLOGIES

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:

Answer: D 8. public class test {


9. public static void main(String [] a) {
541. Program
10. assert a.length == 1;
5. class A{
11. }
6. void foo() throws
12. }
Exception{throw new Exception();}

www.laratechnology.com 080-41310124 Page 192


LARA TECHNOLOGIES LARA TECHNOLOGIES

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

19. System.out.println(”passed”); Click the Exhibit button.


20. } 1. public class Test {
21. } 2.
And these command line invocations: 3. public static void main(String [] args) {
java AssertStuff 4. boolean assert = true;
java -ea AssertStuff 5. if(assert) {
What is the result? 6. System.out.println(”assert is true”);
A. passed 7. }
stuff 8. }
B. stuff 9.
passed 10. }
C. passed Given:

www.laratechnology.com 080-41310124 Page 193


LARA TECHNOLOGIES LARA TECHNOLOGIES

javac -source 1.3 Test.java B. Only the assert statement on line 31 is


What is the result? used appropriately.
A. Compilation fails. C. The assert statements on lines 29 and 31
B. Compilation succeeds with errors. are used appropriately.
C. Compilation succeeds with warnings. D. The assert statements on lines 26 and 29
D. Compilation succeeds without warnings are used appropriately.
or errors. E. The assert statements on lines 29 and 33
are used appropriately.
Answer: C F. The assert statements on lines 29, 31, and
33 are used
545. Program appropriately.
Given: G. The assert statements on lines 26, 29, and
23.int z=5; 31 are used
24. appropriately.
25. public void stuff1(int x) {
26. assert (x> 0); Answer: C
27. switch(x) {
546. Program
28. case 2: x= 3;
Given:
29. default: assert false; } }
11. static void test() throws Error {
30.
12. if (true) throw new AssertionError();
31. private void stuff2(int y) { assert (y < 0);
13. System.out.print(”test “);
}
14. }
32.
15. public static void main(String[] args) {
33. private void stuff3() { assert (stuff4O); }
16. try { test(); }
34.
17. catch (Exception ex)
35. private boolean stuff4() { z = 6; return
{ System.out.print(”exception “); }
false; }
18. System.out.print(”end “);
Which is true?
19. }
A. All of the assert statements are used
What is the result?
appropriately.
A. end
B. Compilation fails.

www.laratechnology.com 080-41310124 Page 194


LARA TECHNOLOGIES LARA TECHNOLOGIES

C. exception end Which two classes correctly implement


D. exception test end both the java.lang.Runnable
E. A Throwable is thrown by main. and the java.lang.Clonable interfaces?
F. An Exception is thrown by main. (Choose two.)
A. public class Session
Answer: E implements Runnable, Clonable {
public void run();
547. Program public Object clone();
Given a method that must ensue that its }
parameter is not null: B. public class Session
11. public void someMethod(Object value) { extends Runnable, Clonable {
12. // check for null value public void run() { / do something */ }
.... public Object clone() { / make a copy */ }
20. System.out.println(value.getClass()); }
21. } C. public class Session
What, inserted at line 12, is the appropriate implements Runnable, Clonable {
way to handle a null public void run() { / do something */ }
value? public Object clone() { /* make a copy */ }
A. assert value == null; }
B. assert value !null, “value is null”; D. public abstract class Session
C. if (value == null) { implements Runnable, Clonable {
throw new AssertionException(”value is public void run() { / do something */ }
null”); public Object clone() { /*make a copy */ }
D. if (value == null) { }
throw new E. public class Session
IllegalArgumentException(”value is null”); implements Runnable, implements Clonable
{
public void run() { / do something */ }
Answer: D
public Object clone() { / make a copy */ }
548. Program }

www.laratechnology.com 080-41310124 Page 195


LARA TECHNOLOGIES LARA TECHNOLOGIES

Answer: CD 16. public class Tester implements


Runnable{
549. Program 17. static PingPong2 pp2=new PingPong2();
11. public class PingPong implements 18. public static void main(String args[]){
Runnable{ 19. new Thread(new Tester()).start();
12. synchronized void hit(long n){ 20. new Thread(new Tester()).start();
13. for(int i=1;i<3;i++) 21.}
14. System.out.println(n+”-”+i+””); 22. public void run(){
15.} 23. pp2.hit(Thread.currentThread().getId());
16. public static void main(String args[]){ 24.}
17. new Thread(new PingPong()).start(); 25.}
18. new Thread(new PingPong()).start(); Which two statements are true?(choose two)
19.}
20. public void run(){ E. The output could be 5-1 6-1 6-2 5-2
21. hit(Thread.currentThread().getId()); F. The output could be 6-1 6-2 5-1 5-2
22.} G. The output could be 6-1 5-2 6-2 5-1
23.} H. The output could be 6-1 6-2 5-1 7-1
Which two statements are true?(choose two)
E. The output could be 8-1 7-2 8-2 7-1 Ans: B
F. The output could be 7-1 7-2 8-1 6-1
G. The output could be 8-1 7-1 7-2 8-2 551. Program

H. 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
separate thread? (Choose two.)
550. Program
A. new Thread() {
Given
public void run() { doStuff(); }
11. public class PingPong2{
}
12. synchronized void hit(long n){
B. new Thread() {
13. for(int i=1;i<3;i++)
public void start() { doStuff(); }
14. System.out.println(n+”-”+i+””);
}
15.}}

www.laratechnology.com 080-41310124 Page 196


LARA TECHNOLOGIES LARA TECHNOLOGIES

C. new Thread() { B. An exception is thrown at runtime.


public void start() { doStuff(); } C. The code executes and prints “running”.
} .run(); D. The code executes and prints
D. new Thread() { “runningrunning”.
public void run() { doStuff(); } E. The code executes and prints
} .start(); “runningrunningrunning”.
E. new Thread(new Runnable() {
public void run() { doStuff(); } Answer: E
} ).run();
F. new Thread(new Runnable() { 553. Program
public void run() { doStuff(); } Given:
}).start(); 1. public class Threads4 {
2. public static void main (String[] args) {
Answer: DF 3. new Threads4().go();
4. }
552. Program 5. public void go() {
Given: 6. Runnable r = new Runnable() {
1. public class Threads3 implements 7. public void run() {
Runnable { 8. System.out.print(”foo”);
2. public void run() { 9. }
3. System.out.print(”running”); 10. };
4. } 11. Thread t = new Thread(r);
5. public static void main(String[] args) { 12. t.start();
6. Thread t = new Thread(new Threads3()); 13. t.start();
7. t.run(); 14. }
8. t.run(); 15. }
9. t.start(); What is the result?
10. } A. Compilation fails.
11. } B. An exception is thrown at runtime.
What is the result? C. The code executes normally and prints
A. Compilation fails. ‘foo”.

www.laratechnology.com 080-41310124 Page 197


LARA TECHNOLOGIES LARA TECHNOLOGIES

D. The code executes normally, but nothing 15. };


is printed. 16. Threadt=new Thread(r) {
17. public void run() {
Answer: B 18. System.out.print(”Dog”);
19. }
554. Program 20. };
Given: 21. t.start();
1. public class Threads5 { What is the result?
2. public static void main (String[] args) { A. Cat
3. new Thread(new Runnable() { B. Dog
4. public void run() { C. Compilation fails.
5. System.out.print(”bar”); D. The code runs with no output.
6. }}).start(); E. An exception is thrown at runtime.
7. }
8. } Answer: B
What is the result?
A. Compilation fails. 556. Program

B. An exception is thrown at runtime. Click the Exhibit button.


C. The code executes normally and prints Given:
“bar”. 10. public class Starter extends Thread {
D. The code executes normally, but nothing 11. private int x= 2;
prints. 12. public static void main(String[] args)
throws Exception {
Answer: C 13. new Starter().makeItSo();
14. }
555. Program 15. public Starter() {
Given: 16. x=5;
11. Runnable r = new Runnable() { 17. start();
12. public void run() { 18. }
13. System.out.print(”Cat”); 19. public void makeItSo() throws
14. } Exception {

www.laratechnology.com 080-41310124 Page 198


LARA TECHNOLOGIES LARA TECHNOLOGIES

20. join(); 11. }


21. x=x- 1; 12. }
22. System.out.println(x); Which two can be results? (Choose two.)
23. } A. java.lang.RuntimeException: Problem
24. public void run() { x *= 2; } B. run.
25. } java.lang.RuntimeException: Problem
What is the output if the main() method is C. End of method.
rum? java.lang.RuntimeException: Problem
A. 4 D. End of method.
B. 5 run.
C. 8 java.lang.RuntimeException: Problem
D. 9 E. run.
E. Compilation fails. java.lang.RuntimeException: Problem
F. An exception is thrown at runtime. End of method.
G. It is impossible to determine for certain.
Answer: D Answer: DE

557. Program 558. 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”.

www.laratechnology.com 080-41310124 Page 199


LARA TECHNOLOGIES LARA TECHNOLOGIES

D. The code executes normally, but nothing


is printed. Answer: E
Answer: C
560. Program

Which two statements are true? (Choose


559. Program
two)
Given:
1. public class TestOne implements
A. It is possible for more than two threads to
Runnable {
deadlock at once.
2. public static void main (String[] args)
B. the JVM implementation guarantees that
throws Exception {
multiple threads can not enter into a
3. Thread t = new Thread(new TestOne());
deadlock state.
4. t.start();
C. Deadlocked threads release once their
5. System.out.print(”Started”);
sleep() methods sleep duration has expired.
6. t.join();
D. Deadlocking can occur only when the
7. System.out.print(”Complete”);
wait(),notify(),and notifyAll() methods are
8. }
used incorrectly.
9. public void run() {
E. It is possible for a single-threaded
10. for (int i= 0; i< 4; i++) {
application to deadlock if synchronized
11. System.out.print(i);
blocks are used incorrectly.
12. }
F. If a piece of code is capable of
13. }
deadlocking, you cannot eliminate the
14. }
possibility of deadlocking by inserting
What can be a result?
invocation of
A. Compilation fails.
Thread.yield().
B. An exception is thrown at runtime.
C. The code executes and prints
Ans: A, F
“StartedComplete”.
D. The code executes and prints
561. Program
“StartedComplete0123”.
Click the Exhibit button.
E. The code executes and prints
Given:
“Started0l23Complete”.
1. public class TwoThreads {

www.laratechnology.com 080-41310124 Page 200


LARA TECHNOLOGIES LARA TECHNOLOGIES

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

www.laratechnology.com 080-41310124 Page 201


LARA TECHNOLOGIES LARA TECHNOLOGIES

Given: 3. public class Runner implements Runnable


public class NamedCounter { {
private final String name; 4. public void run() {
private int count; 5. int current = 0;
public NamedCounter(String name) 6. for(int=i=0;i<4;i++){
{ this.name = name; } 7. current = x;
public String getName() { return name; } 8. System.out.print(current + “, “);
public void increment() { coount++; } 9. x = current + 2;
public int getCount() { return count; } 10. }
public void reset() { count = 0; } 11. }
} 12. }
Which three changes should be made to 13.
adapt this class to be used 14. public static void main(String[] args) {
safely by multiple threads? (Choose three.) 15. new Threads1().go();
A. declare reset() using the synchronized 16. }
keyword 17.
B. declare getName() using the 18. public void go() {
synchronized keyword 19. Runnable r1 = new Runner();
C. declare getCount() using the 20. new Thread(r1).start();
synchronized keyword 21. new Thread(r1 ).start();
D. declare the constructor using the 22. }
synchronized keyword 23. }
E. declare increment() using the Which two are possible results? (Choose
synchronized keyword two.)
A. 0, 2, 4, 4, 6, 8, 10, 12,
Answer: ACE B. 0, 2, 4, 6, 8, 10, 2, 4,
C. 0, 2, 4, 6, 8, 10, 12, 14,
563. Program D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12,
Click the Exhibit button: 14, 14,
1. public class Threads 1 { E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10,
2. intx=0; 12, 14,

www.laratechnology.com 080-41310124 Page 202


LARA TECHNOLOGIES LARA TECHNOLOGIES

Which two statements are true if this class is


Answer: AC compiled and run?
(Choose two.)
564. Program A. An exception may be thrown at runtime.
Click the Exhibit button. B. The code may run with no output, without
1. import java.util.*; exiting.
2. C. The code may run with no output, exiting
3. public class NameList { normally.
4. private List names = new ArrayList(); D. The code may rum with output “A B A B
5. public synchronized void add(String C C “, then exit.
name) { names.add(name); } E. The code may rum with output “A B C A
6. public synchronized void printAll() { B C A B C “, then exit.
7. for (int i = 0; i <names.size(); i++) { F. The code may ruin with output “A A A B
8. System.out.print(names.get(i) +“ “); C A B C C “, then exit.
9. } G. The code may ruin with output “A B C A
10. } A B C A B C “, then exit.
11. public static void main(String[] args) {
12. final NameList sl = new NameList(); Answer: EG
13.for(int i=0;i<2;i++) {
14. new Thread() { 565. Program

15. public void ruin() { Given:


16. sl.add(”A”); 1. public class TestFive {
17. sl.add(”B”); 2. private int x;
18. sl.add(”C”); 3. public void foo() {
19. sl.printAll(); 4 int current = x;
20. } 5. x = current + 1;
21. }.start(); 6. }
22. } 7. public void go() {
23. } 8. for(int i=0;i<5;i++) {
24. } 9. new Thread() {
10. public void run() {

www.laratechnology.com 080-41310124 Page 203


LARA TECHNOLOGIES LARA TECHNOLOGIES

11. foo(); C. public synchronized void go() { /* code


12. System.out.print(x + “, “); here */ }
13. } }.start(); D. private synchronized(this) void go() { /*
14. }}} code here */ }
Which two changes, taken together, would E. void go() {
guarantee the output: 1, 2, synchronized(Object.class) { /* code here
3, 4, 5, ? (Choose two.) */ }
A. Move the line 12 print statement into the }
foo() method. F. void go() {
B. Change line 7 to public synchronized Object o = new Object();
void go() {. synchronized(o) { /* code here */ }
C. Change the variable declaration on line 3 }
to private volatile int x;. Answer: CEF
D. Wrap the code inside the foo() method
with a synchronized( this ) 567. Program
block. Given:
E. Wrap the for loop code inside the go() 1. public class TestSeven extends Thread {
method with a synchronized 2. private static int x;
block synchronized(this) { // for loop code 3. public synchronized void doThings() {
here }. 4. int current = x;
5. current++;
Answer: AD 6. x = current;
7. }
566. Program 8. public void run() {
Which three will compile and rim without 9. doThings();
exception? (Choose three.) 10. }
A. private synchronized Object o; 1 1.}
B. void go() { Which is true?
synchronized() { /* code here */ } A. Compilation fails.
} B. An exception is thrown at runtime.

www.laratechnology.com 080-41310124 Page 204


LARA TECHNOLOGIES LARA TECHNOLOGIES

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”.

www.laratechnology.com 080-41310124 Page 205


LARA TECHNOLOGIES LARA TECHNOLOGIES

C. The output may be “r1 = 8, r2 = 12”. 18. wait();


D. The code may run (and complete) with 19. } catch (InterruptedException e) { }
no output. 20. }
E. The code may deadlock (without 21. return result;
completing) with no output. 22. }
F. M IllegalStateException or 23.
InterruptedException may be thrown at 24. public static void main(String[] args) {
runtime. 25. Computation[] computations = new
Computation [4];
Answer: ABE 26. for (int i = 0; i < computations.length;
i++) {
569. Program 27. computations[i] = new Computation(i);
Click the Exhibit button. 28. computations[i] .start();
1. class Computation extends Thread { 29. }
2. 30. for (Computation c : computations)
3. private int num; 31. System.out.print(c.getResult() +“ “);
4. private boolean isComplete; 32. }
5. private int result; 33. }
6. What is the result?
7. public Computation(int num) { this.num A. The code will deadlock.
= num; } B. The code may run with no output.
8. C. An exception is thrown at runtime.
9. public synchronized void run() { D. The code may run with output “0 6”.
10. result = num * 2; E. The code may run with output “2 0 6 4’.
11. isComplete = true; F. The code may ruin with output “0 2 4 6”.
12. notify();
13. } Answer: F
14.
15. public synchronized int getResult() { 570. Program

16. while (!isComplete) { Given:


17. try { 7. void waitForSignal() {

www.laratechnology.com 080-41310124 Page 206


LARA TECHNOLOGIES LARA TECHNOLOGIES

8. Object obj = new Object(); executing bar.wait(). From another thread,


9. synchronized (Thread.currentThread()) { which statement is the
10. obj.wait(); most reliable way to ensue that foo will stop
11. obj.notify();; executing wait()?
12. } A. foo.notify();
13. } B. bar.notify();
Which is true? C. foo.notifyAll();
A. This code may throw an D. Thread.notify();
InterruptedException. E. bar.notiFYAll();
B. This code may throw an F. Object.notify();
IllegalStateException.
C. This code may throw a TimeoutException Answer: E
after ten minutes.
572. Program
D. This code will not compile unless
Given that t1 is a reference to a live
“obj.wait()” is replaced with
thread, which is true?
“((Thread) obj).wait()”.
A. The Thread.sleep() method can take t1 as
E. Reversing the order of obj.wait() and
an argument.
obj.notify() may cause this
B. The Object.notify() method can take t1 as
method to complete normally.
an argument.
F. A call to notify() or notifyAll() from
C. The Thread.yield() method can take t1 as
another thread may cause this
an argument.
method to complete normally.
D. The Thread.setPriority() method can take
t1 as an argument.
Answer: B
E. The Object.notify() method arbitrary
chooses which thread to notify.
571. Program
Ans: E
Given:
foo and bar are public references available 573. Program

to many other threads. foo Given that Traingle implement

refers to a Thread and bar is an Object. The Runnable,and;

thread foo is currently

www.laratechnology.com 080-41310124 Page 207


LARA TECHNOLOGIES LARA TECHNOLOGIES

31.void go()throws Exception{ 42. public class ClassA {


32.Thread t=new Thread(new Triangle()); 43. public int getValue() {
33.t.start(); 44.int value=0;
34.for(int x=1;x<100000;x++){ 45. boolean setting = true;
35.//insert code here 46. String title=”Hello”;
36.if(x%100==0)System.out.println(“g”); 47. if (value || (setting && title ==
37.}} “Hello”)) { return 1; }
38.public void run(){ 48. if (value == 1 & title.equals(”Hello”)) {
39.try{ return 2; }
40.for(int x=1;x<100000;x++) 49. }
41.//insert the same code here 50. }
42.if(x%100==0)System.out.println(“t”); And:
43.} 70. ClassA a = new ClassA();
44.}catch(Exception e){} 71. a.getValue();
45.} What is the result?
Which two statements,insert independently A. 1
at both lines B. 2
35 and 41,tend allow both threas to C. Compilation fails.
temporarily pause and allow the other D. The code runs with no output.
threads to execute” E. An exception is thrown at runtime.
(Choose two)
Answer: C
A.Thread.wait()
B.Thread.join() 575. Program
C.Thread.yield() Given:
D.Thread.sleep(1) 11. public class Person {
E.Thread.notify() 12. private String name, comment;
13. private int age;
Answer:C,D 14. public Person(String n, int a, String c) {
15. name = n; age = a; comment = c;
574. Program
16. }
Given:

www.laratechnology.com 080-41310124 Page 208


LARA TECHNOLOGIES LARA TECHNOLOGIES

17. public boolean equals(Object o) { C. System.freeMemory();


18. if(! (o instanceof Person)) return false; D. Runtime.getRuntime().growHeap();
19, Person p = (Person)o; E. Runtime.getRuntime().freeMemory();
20. return age == p.age &&
name.equals(p.name); Answer: A
21. }
22. }
What is the appropriate definition of the
577. Program
hashCode method in class
Which two are true? (Choose two.)
Person?
A. A finalizer may NOT be invoked
A. return super.hashCode();
explicitly.
B. return name.hashCode() + age * 7;
B. The finalize method declared in class
C. return name.hashCode() +
Object takes no action.
comment.hashCode() /2;
C. super.finalize() is called implicitly by any
D. return name.hashCode() +
overriding finalize method.
comment.hashCode() / 2 - age * 3;
D. The finalize method for a given object
will be called no more than
Answer: B
once by the garbage collector.
576. program E. The order in which finalize will be called
Given: on two objects is based on
11. rbo = new ReallyBigObject(); the order in which the two objects became
12. // more code here finalizable.
13. rbo = null;
14. /* insert code here */ Answer: BD
Which statement should be placed at line 14
to suggest that the virtual 578. Program
machine expend effort toward recycling the Which statements are true?
memory used by the A. A class finalize() method cannot be
object rbo? invoked explicitly.
A. System.gc(); B. super.finalize() is called explicitly by any
B. Runtime.gc(); overriding finalize() method.

www.laratechnology.com 080-41310124 Page 209


LARA TECHNOLOGIES LARA TECHNOLOGIES

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;

www.laratechnology.com 080-41310124 Page 210


LARA TECHNOLOGIES LARA TECHNOLOGIES

E. The objects referenced by snooch and Answer: D


booch are eligible for garbage
collection. 582. Program

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 {

www.laratechnology.com 080-41310124 Page 211


LARA TECHNOLOGIES LARA TECHNOLOGIES

15. System.out.println(”String is not Given


empty”);
16. } 22.StringBuilder sb1=new
17. } StringBuilder(“123”);
And the invocation: 23. String s1=”123”;
31. test(null); 24. //insert code here
What is the result? 25. System.out.println(sb1+””+s1);
A. Au exception is thrown at runtime. Which code fragment, inserted at line
B. “String is empty” is printed to output. 24,output “123abc 123abc”?
C. Compilation fails because of au error in I. sb1.append(“abc”);s1.append(“abc”);
line 12. J. sb1.append(“abc”);s1.concat(“abc”);
D. “String is not empty” is printed to K. sb1.concat(“abc”);s1.append(“abc”);
output. L. sb1.concat(“abc”);s1.concat(“abc”);
Answer: A M. sb1.appemd(“abc”);s1=s1.concat(“abc”);
N. sb1.concat(“abc”);s1=s1.concat(“abc”);
O. sb1.appemd(“abc”);s1=s1+s1.concat(“ab
584. Program c”);
Which two scenario’s are not safe to P. sb1.concat(“abc”);s1=s1+s1.concat(“abc
replace a StringBuffer object with a ”);
StringBuilder object? (Choose two)
A. When using versions of java technology Ans : E
earlier than 5.0.
B. When sharing a StringBuffer among 586. Program
multiple threads. Given
C.When using the java.io.class 1.public class KungFu{
StringBufferInputStream. 2. public static void main (String args[]){
D. When u plan to reuse the StringBuffer to 3. Integer x=400;
build morethan one Strong. 4. Integer y=x;
Ans: A, B 5. x++;
6. StringBuilder sb1=new
585. Program StringBuilder(“123”);

www.laratechnology.com 080-41310124 Page 212


LARA TECHNOLOGIES LARA TECHNOLOGIES

7. StringBuilder sb2=sb1; F. An exception is thrown at runtime.


8. sb1.append(“5”);
9. System.out.println((x==y)+””+ Answer: D
(sb1==sb2));
10.} 588. Program
11.} Given this method in a class:
What is the result? 21. public String toString() {
G. true true 22. StringBuffer buffer = new
H. false true StringBuffer();
I. true false 23. buffer.append(’<’);
J. false false 24. buffer.append(this.name);
K. Compilation fails 25. buffer.append(’>’);
L. An exception is thrown at runtime 26. return buffer.toString();
Ans : B 27. }
Which is true?
587. Program A. This code is NOT thread-safe.
Given: B. The programmer can replace
1. public class TestString 1 { StringBuffer with StringBuilder with no
2. public static void main(String[] args) other changes.
{ C. This code will perform well and
3. String str = “420”; converting the code to use
4. str += 42; StringBuilder will not enhance the
5. System.out.print(str); performance.
6. } D. This code will perform poorly. For better
7. } performance, the code
What is the output? should be rewritten: return “<“+
A. 42 this.name + “>”;
B. 420
C. 462 Answer: B
D. 42042
E. Compilation fails. 589. Program

www.laratechnology.com 080-41310124 Page 213


LARA TECHNOLOGIES LARA TECHNOLOGIES

Given: 14. s = s.substring(2, 5);


1. public class MyLogger { 15. s = s.toUpperCase();
2. private StringBuilder logger = new 16. return s.toString();
StringBuuilder(); 17. }
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 591. 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.)
590. 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);

www.laratechnology.com 080-41310124 Page 214


LARA TECHNOLOGIES LARA TECHNOLOGIES

C. StringBuffer s = new 13. for(String s: tokens) System.out.print(s


StringBuffer(”123456789”); +“ “);
s.substring(3,6).delete( 1 ,3).insert( 1, “24”); What is the result?
D. StringBuilder s = new A. a b c
StringBuilder(”123456789”); B. 1 2 3
s.substring(3,6).delete( 1 ,2).insert( 1, “24”); C. a1b2c3
E. StringBuilder s = new D. a1 b2 c3
StringBuilder(”123456789”); E. Compilation fails.
s.delete(0,3).delete( 1 , F. The code runs with no output.
3).delete(2,5).insert( 1, “24”); G. An exception is thrown at runtime.

Answer: BE Answer: A

592. Program 594. Program

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:

www.laratechnology.com 080-41310124 Page 215


LARA TECHNOLOGIES LARA TECHNOLOGIES

12. System.out.format(”Pi is approximately • df is a valid, non-null DateFormat


%d.”, Math.PI); object set to the current locale .
What is the result? What outputs the current locales country
A. Compilation fails. name and the appropriate
B. Pi is approximately 3. version of d’s date?
C. Pi is approximately 3.141593. A. Locale loc = Locale.getLocale();
D. An exception is thrown at runtime. System.out.println(loc.getDisplayCountry()
+ “ “+ df.format(d));
Answer: D B. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
596. Program
+ “ “ + df.format(d));
Given
C. Locale bc = Locale.getLocale();
1. public class LineUp{
System.out.println(loc.getDisplayCountry()
2. public static void main(String args[]){
+ “ “+ df.setDateFormat(d));
3. double d=12.345;
D. Locale loc = Locale.getDefault();
4. //insert code here
System.out.println(loc.getDispbayCountry()
5.}
+ “ “+ df.setDateFormat(d));
6.}
Which code fragment, inserted at line 4
Answer: B
produces the output |12.345|?
G. System.out.printf(“|%6d|\n”,d);
598. Program
H. System.out.printf(“|%6f|\n”,d);
Given:
I. System.out.printf(“|%3.7d|\n”,d);
12. NumberFormat nf=
J. System.out.printf(“|%3.7f|\n”,d);
NumberFormat.getInstance();
K. System.out.printf(“|%6.3d|\n”,d);
13. nf.setMaximumFractionDigits(4);
L. System.out.printf(“|%6.3f|\n”,d);
14. nf.setMinimumFractionDigits(2);
Ans:F
15. String a = nf.format(3.1415926);
597. Program 16. String b = nf.format(2);
Given: Which two are true about the result if the
• d is a valid, non-null Date object default locale is Locale.US?
(Choose two.)

www.laratechnology.com 080-41310124 Page 216


LARA TECHNOLOGIES LARA TECHNOLOGIES

A. The value of b is 2. 17. String s = df.format( date);


B. The value of a is 3.14. Which two, inserted independently at line
C. The value of b is 2.00. 16, allow the code to
D. The value of a is 3.141. compile? (Choose two.)
E. The value of a is 3.1415. A. df= new DateFormat();
F. The value of a is 3.1416. B. df= Date.getFormatter();
G. The value of b is 2.0000. C. df= date.getFormatter();
D. df= date.getDateFormatter();
Answer: CF E. df= Date.getDateFormatter();
F. df= DateFormat.getInstance();
599. Program G. df = DateFormat.getDateInstance();
Given:
11. double input = 314159.26; Answer: FG
12. NumberFormat nf=
601. Program
NumberFormat.getInstance(Locale.ITALIA
Given:
N);
12. Date date = new Date();
13. String b;
13. df.setLocale(Locale.ITALY);
14. //insert code here
14. String s = df.format(date);
Which code, inserted at line 14, sets the
The variable df is an object of type
value of b to 3 14.159,26?
DateFormat that has been
A. b = nf.parse( input);
initialized in line 11. What is the result if
B. b = nf.format( input);
this code is run on December
C. b = nf.equals( input);
14, 2000?
D. b = nf.parseObject( input);
A. The value of s is 14-dic-2004.
B. The value of s is Dec 14, 2000.
Answer: B
C. An exception is thrown at runtime.
600. Program D. Compilation fails because of an error in
Given: line 13.
14. DateFormat df;
15. Date date = new Date(); Answer: D
16. //insert code here

www.laratechnology.com 080-41310124 Page 217


LARA TECHNOLOGIES LARA TECHNOLOGIES

42. d.setLocalTime( (60 * 60 * 24) +


602. Program
d.getLocalTime());
Given:
33. Date d = new Date(0);
Answer: B
34. String ds = “December 15, 2004”;
35. // insert code here 603. Program

36. try { Given a valid DateFormat object named df,

37. d = df.parse(ds); and

38. } 16. Date d = new Date(0L);

39. catch(ParseException e) { 17. String ds = “December 15, 2004”;

40. System.out.println(”Unable to parse “+ 18. // insert code here

ds); What updates d’s value with the date

41. } represented by ds?

42. // insert code here too A. 18. d = df.parse(ds);

Which will create the appropriate B. 18. d = df.getDate(ds);

DateFormat object and add a day to C. 18. try {

the Date object? 19. d = df.parse(ds);

A. 35. DateFormat df= 20. } catch(ParseException e) { };

DateFormat.getDateFormat(); D. 18. try {

42. d.setTime( (60 * 60 * 24) + 19. d = df.getDate(ds);

d.getTime()); 20. } catch(ParseException e) { };

B. 35. DateFormat df=


DateFormat.getDateJnstance(); 42. Answer: C

d.setTime( (1000 * 60 * 60 * 24) +


d.getTime());
C. 35. DateFormat df=
DateFormat.getDateFormat();
42. d.setLocalTime( (1000*60*60*24) +
d.getLocalTime());
D. 35. DateFormat df=
DateFormat.getDateJnstance();

www.laratechnology.com 080-41310124 Page 218

You might also like