0% found this document useful (0 votes)
21 views

Questions

The document contains examples of Java code snippets and questions about the output or behavior of the code. It covers topics like strings, exceptions, inheritance, interfaces, and encapsulation.

Uploaded by

hhuggh121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Questions

The document contains examples of Java code snippets and questions about the output or behavior of the code. It covers topics like strings, exceptions, inheritance, interfaces, and encapsulation.

Uploaded by

hhuggh121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 36

1.

Given:

1. public class TestString3 {


2. public static void main(String[] args) {
3. // insert code here
5. System.out.println(s);
6. }
7.}
Which two code fragments, inserted independently at line 3, generate the output
4247? (Choose two.)

A- String s = "123456789";
s = (s-"123").replace(l,3,"24") - "89";
B- StringBuffer s = new StringBuffer(“123456789”);
s.delete(0,3).replace(l ,3,"24"). delete(4,6);
C- StringBuffer s = new StringBuffer(“23456789”);
s.substring(3,6).delete(l ,3).insert(l,"24");
D- StringBuilder s = new StnngBuilder(“23456789”);
s.substring(3,6).delete(l ,2). insert(l,"24");
E- StringBuilder s = new StringBuilder(“123456789”);
s.delete(0,3).delete(l ,3).delete(2,5).insert(1, "24");

2. Given:

12. public class Yippee2 {


13. static public void main(String [] yahoo) {
14. for(int x = 1; x < yahoo.length; x++) {
15. System.out.print (yahoo[x] +" ");
16. }
17. }
18. }

and the command line invocation:

java Yippee2 a b c

What is the result?


A-ab
B-bc
C-abc
D - Compilation fails.
E - An exception is thrown at runtime.
3. Given classes defined in two different files:

1. package util;
2. public class BitUtils {
3. public static void process (byte[]) {/* more code here */}
4.}

1. package app;
2. public class SomeApp {
3. public static void main(String[] args) {
4. byte[] bytes = new byte[256];
5. // insert code here
6. }
7.}

What is required at line 5 in class SomeApp to use the process method of BitUtils?

A - process(bytes);
B - BitUtils.process(bytes);
C - util.BitUtils.process(bytes);
D - SomeApp cannot use methods in BitUtils.
E - import util.BitUtils.*; process(bytes);

4. Given:

11. public static void test(String str) {


12. int check = 4;
13. if (check = str.length()) {
14. System.out.print(str.charAt(check - = 1) +",");
15. }else{
16. System.out.print(str.charAt(0) +",");
17. }
18. }

and the invocation:

21. test("four");
22. test("tee");
23. test("to");

What is the result?


A - r, t, t,
B - r, e, o,
C - Compilation fails.
D - An exception is thrown at runtime.
5. Given classes defined in two different files:

1. package util;
2. public class BitUtils {
3. private static void process (byte[]) {}
4.}

1. package app;
2. public class SomeApp {
3. public static void main(String[] args) {
4. byte[] bytes = new byte[256];
5. // insert code here
6. }
7.}

What is required at line 5 in class SomeApp to use the process method of BitUtils?

A - process(bytes);
B - BitUtils.process(bytes);
C - app.BitUtils.process(bytes);
D - util.BitUtils.process(bytes);
E - import util.BitUtils.*; process(bytes);
F - SomeApp cannot use the process method in BitUtils.

6. A JavaBeans component has the following field:

11. private boolean enabled;

Which two pairs of method declarations follow the JavaBeans standard for
accessing this field? (Choose two.)

A- public void setEnabled( boolean enabled )


public boolean getEnabled()

B- public void setEnabled( boolean enabled )


public void isEnabled()

C- public void setEnabled( boolean enabled )


public boolean isEnabled()

D- public boolean setEnabled( boolean enabled )


public boolean getEnabled()
7. Given:

11. public static void main(String[] args) {


12. Object obj = new int[] { 1, 2, 3 };
13. int[] someArray = (int[]) obj;
14. for (int i : someArray) System.out.print(i +" ");
15. }

What is the result?

A-123
B - Compilation fails because of an error in line 12.
C - Compilation fails because of an error in line 13.
D - Compilation fails because of an error in line 14.
E - A ClassCastException is thrown at runtime.

8. Given:

20. public class CreditCard {


21. private String cardID;
22. private Integer limit;
23. public String ownerName;
24. public void setCardInformation (String cardID,String ownerName,
26. Integer limit) {
27. this.cardID = cardID;
28. this.ownerName = ownerName;
29. this.limit = limit;
30. }
31. }

Which statement is true?

A - The class is fully encapsulated.


B - The code demonstrates polymorphism.
C - The ownerName variable breaks encapsulation.
D - The cardID and limit variables break polymorphism.
E – The setCardInformation method breaks encapsulation.
9. Given:

11. static void test() throws RuntimeException {


12. try{
13. System.out.print("test ");
14. throw new RuntimeException();
15. }
16. catch (Exception ex) { System.out.print("exception ");}
17. }
18. public static void main(String[] args) {
19. try {test();}
20. catch (RuntimeException ex) { System.out.print("runtime ");}
21. System.out.print("end ");
22. }

What is the result?

A - test end
B - Compilation fails.
C - test runtime end
D - test exception end
E - A Throwable is thrown by main at runtime.

10. Given:

11. Float pi = new Float(3.14f);


12. if (pi > 3) {
13. System.out.print("pi is bigger than 3.");
14. }
15. else {
16. System.out.print("pi is not bigger than 3.");
17. }
18. finally {
19. System.out.println("Have a nice day.");
20. }

What is the result?

A - Compilation fails.
B - pi is bigger than 3.
C - An exception occurs at runtime.
D - pi is bigger than 3. Have a nice day.
E - pi is not bigger than 3. Have a nice day.
11. Given:

11. class A {
12. public void process() { System.out.print("A,");}
13.}
14. class B extends A {
15. public void process() throws IOException {
16. super.process();
17. System. out.print("B,");
18. throw new IOException();
19. }
20. public static void main(String[] args) {
21. try { new B().process();}
22. catch (IOException e) { System.out.println("Exception");}
23. }
24.}

What is the result?

A - Exception.
B - A, B, Exception.
C - Compilation fails because of an error in line 21.
D - Compilation fails because of an error in line 15.
E - A NullPointerException is thrown at runtime.

12. Given:

33. try {
34. // some code here
35. } catch (NullPointerException e1) {
36. System.out.print("a");
37. } catch (RuntimeException e2) {
38. System.out.print("b");
39. } finally {
40. System.out.print("c");
41. }

What is the result if a NullPointerException occurs on line 34?

A-c
B-a
C - ab
D - ac
E - bc
F - abc
13. Given:

11.interface DeclareStuff {
12. public static final int EASY = 3;
13. void doStuff(int t);
14.}
15.public class TestDeclare implements DeclareStuff {
16. public static void main (String [ ] args) {
17. int x = 5;
18. new TestDeclare().doStuff(++x);
19.}
20. void doStuff (int s) {
21. s += EASY+ ++s;
22. System.out.println("s " + s};
23. }
24.}

What is the result?


A –s 14
B - s 16
C - s 10
D - Compilation fails.
E - An exception is thrown at runtime.

14. Given:

10. interface A { void x();}


11. class B implements A { public void x() {} public void y() {}}
12. class C extends B { public void x() {}}
and:
20. java.util.List<A> list = new java.util.ArrayList<A>();
21. list.add(new B());
22. list.add(new C());
23. for (A a: list) {
24. a.x();
25. a.y();
26.}

What is the result?


A - The code runs with no output.
B - An exception is thrown at runtime.
C - Compilation fails because of an error in line 20.
D - Compilation fails because of an error in line 21.
E - Compilation fails because of an error in line 23.
F - Compilation fails because of an error in line 25.
15. Given:

31. public void method() {


32. A a = newA();
33. a.method1();
34. }

Which statement is true if a TestException is thrown on line 3 of class B?


1. public class A {
2. public void method1() {
3. try {
4. B b = new B();
5. b.method2();
6. // more code here
7. } catch (TestException te) {
8. throw new RuntimeException(te);
9. }
10. }
11.}

1. public class B {
2. public void method2() throws TestException {
3. // more code here
4. }
5. }

1. public class TestException extends Exception {


2. }

A - Line 33 must be called within a try block.


B - The exception thrown by method1 in class A is not required to be caught.
C - The method declared on line 31 must be declared to throw a
RuntimeException.
D - On line 5 of class A, the call to method2 of class B does not need to be placed
in a try/catch block.
16. Given:

25. try {
26. A a = new A();
27. a.method1();
28.} catch (Exception e) {
29. System.out.print("an error occurred");
30.}

Which two statements are true if a NullPointerException is thrown on line 3 of


class C? (Choose two.)

l.public class A {
2. public void method1 ( ) {
3. B b = new B( );
4. b.method2( );
5. // more code here
6. }
7.}

1.public class B {
2. public void method2( ) {
3. C c = new C( );
4. c.method3( );
5. // more code here
6. }
7.}

1. public class C {
2. public void method3 ( ) {
3. // more code here
4. }
5.}

A - The application will crash.


B - The code on line 29 will be executed.
C - The code on line 5 of class A will execute.
D - The code on line 5 of class B will execute.
E - The exception will be propagated back to line 27.
17. Given:

1. public class Boxer1 {


2. Integer i;
3. int x;
4. public Boxer1(int y) {
5. x = i+y;
6. System.out.println(x);
7. }
8. public static void main(String[] args) {
9. new Boxer1(new Integer(4));
10. }
11.}

What is the result?

A - The value "4" is printed at the command line.


B - Compilation fails because of an error in line 5
C - Compilation fails because of an error in line 9
D - A NullPointerException occurs at runtime.
E - A NumberFormatException occurs at runtime.
F - An IllegalStateException occurs at runtime.

18. Given:

11. public static void main(String[] args) {


12. String str = "null";
13. if (str == null) {
14. System .out.println("null");
15. } else (str.length() == 0) {
16. System.out.println("zero");
17. } else {
18. System.out.println("some");
19. }
20. }

What is the result?

A - null
B - zero
C - some
D - Compilation fails.
E - An exception is thrown at runtime.
19. Given:

11. static void test() {


12. try{
13. String x = null;
14. System.out.print(x.toString() +" ");
15. }
16. finally { System.out.print("finally "); }
17.}
18. public static void main(String[] args) {
19. try { test(); }
20. catch (Exception ex) { System.out.print("exception ");}
21.}

What is the result?

A - null
B - finally
C - null finally
D - Compilation fails
E - finally exception

20. Given:

10. public class MyClass {


11. public Integer startingl;
12. public void methodA( ) {
13. Integer i = new Integer(25);
14. startingl = i;
15. methodB(i);
16. }
17. private void methodB (Integer i2) {
18. i2 = i2.intValue( );
19.
20. }
21.}

If methodA is invoked, which two are true at line 19? (Choose two.)

A - i2 == startingl returns true.


B - i2 == startingl returns false.
C - i2.equals(startingl) returns true.
D - i2.equals(startingl) returns false.
21. Given:

11. public void genNumbers() {


12. ArrayList numbers = new ArrayList();
13. for (int i=0; i<10; i++) {
14. int value = i * ((int) Math.random());
15. Integer intObj = new Integer(value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }

Which line of code marks the earliest point that an object referenced by intObj
becomes a candidate for garbage collection?

A - Line 16
B - Line 17
C - Line 18
D - Line 19
E - The object is NOT a candidate for garbage collection.

22. Given:

11. public class Yikes {


12. public static void go(Long n) {System.out.println("Long ");}
13. public static void go(Short n) {System.out.println("Short");}
14. public static void go(int n) {System.out.println("int");}
15. public static void main(String [] args) {
16. short y = 6;
17. long z = 7;
18. go(y);
19. go(z);
20. }
21.}

What is the result?

A - int Long
B - Short Long
C - Compilation fails.
D - An exception is thrown at runtime.
23. Given:

ClassA a = new ClassA();


a.methodA();

What is the result?


10.public class ClassA {
11. public void methodA() {
12. ClassB classB = new ClassB();
13. classB.getValue();
14. }
15.}
and:
20.class ClassB {
21. public ClassC classC;
22. public String getValue() {
23. return classC.getValue();
24. }
25.}
and:
30.class ClassC {
31. public String value;
32. public String getValue() {
33. value = "ClassB";
34. return value;
35. }
36.}

A - Compilation fails.
B - Class C is displayed.
C - The code runs with no output.
D - An exception is thrown at runtime

24. Given:

int[] myArray = new int[] {1,2,3,4,5};

What allows you to create a list from this array?

A - List myList = myArray.asList();


B - List myList = Arrays.asList(myArray);
C - List myList = new ArrayList(myArray);
D - List myList = Collections.fromArray(myArray);
25.Given:

11. static class A {


12. void process() throws Exception { throw new Exception();}
13. }
14. static class B extends A {
15. void process() { System.out.println("B");}
16. }
17. public static void main(String[] args) {
18. new B().process();
19. }

What is the result?

A-B
B - The code runs with no output.
C - Compilation fails because of an error in line 12.
D - Compilation fails because of an error in line 15.
E - Compilation fails because of an error in line 18.

26. Given:

10. public class Bar {


11. static void foo( int... x) {
12. // insert code here
13. }
14. }

Which two code fragments, inserted independently at line 12, will allow the class
to compile? (Choose two.)

A - foreach( x) System.out.println(z);
B - for( int z : x) System.out.println(z);
C - while( x.hasNext() ) System.out.println( x.next() );
D - for( int i=0; i< x.length; i++) System.out.println(x[i]);
27.Given:

13.public static void search(List<String> list) {


14. list.clear();
15. list.add("b");
16. list.add("a");
17. list.add("c");
18. System.out.println(Collections.binarySearch(list, "a"));
19.}

What is the result of calling search with a valid List implementation?

A-0
B-1
C-2
D-a
E-b
F-c
G - The result is undefined.

28. What is true about the following code?

1. class Foo {
2. void g(int i) {
3. System.out.println("a");
4. }
5. }
6. class Bar extends Foo {
7. void g(Integer i) {
8. System.out.println("b");
9. }
10.}
11. ...
12 . Bar b = new Bar() ;
13. b.g(10);

A) It will print "a".


B) It will print "b".
C) It will print "ab".
D) It won't compile because the g() in Bar can't override the g() in Foo.
29.Given the code. What is the output?

public class Hotel {


private int roomNr;
public Hotel(int roomNr) {
this.roomNr = roomNr;
}
public int getRoomNr() {
return this.roomNr;
}
static Hotel doStuff(Hotel hotel) {
hotel = new Hotel(1);
return hotel;
}
public static void main(String args[]) {
Hotel h1 = new Hotel(100);
System.out.print(h1.getRoomNr() + " ");
Hotel h2 = doStuff(h1);
System.out.print(h1.getRoomNr() + " ");
System.out.print(h2.getRoomNr() + " ");
h1 = doStuff(h2);
System.out.print(h1.getRoomNr() + " ");
System.out.print(h2.getRoomNr() + " ");
}
}
A) 100 1 1 1 1
B) 100 100 1 1 1
C) 100 100 100 1 1
D) 100 100 100 100 1
E) 100 100 100 100 100
30. Given:

10.package com.sun.scjp;
11.public class Geodetics {
12. public static final double DIAMETER = 12756.32; // kilometers
13.}

Which two correctly access the DIAMETER member of the Geodetics class? (Choose
two.)

A. - import com.sun.scjp.Geodetics;
public class TerraCarta {
public double halfway() { return Geodetics.DIAMETER/2.0; }
B. - import static com.sun.scjp.Geodetics;
public class TerraCarta{
public double halfway() {return DIAMETER/2.0;} }
C. - import static com.sun.scjp.Geodetics.*;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0;} }
D. - package com.sun.scjp;
public class TerraCarta {
public double halfwayQ { return DIAMETER/2.0;} }

31. Given:

11. public static void parse(String str) {


12. try {
13. float f = Float.parseFloat(str);
14. } catch (NumberFormatException nfe) {
15. f = 0;
16. } finally {
17. System.out.println(f);
18. }
19.}
20. public static void main(String[ ] args) {
21. parse("invalid");
22.}

What is the result?

A - 0.0
B - Compilation fails.
C - A ParseException is thrown by the parse method at runtime.
D - A NumberFormatException is thrown by the parse method at runtime
32. Given:

1. class Pizza {
2. java.util.Array List toppings ;
3. public final void addTopping(String topping) {
4. toppings.add(topping);
5. }
6.}
7. public class PepperoniPizza extends Pizza {
8. public void addTopping(String topping) {
9. System.out.println("Cannot add Toppings");
10. }
11. public static void main(String[] args) {
12. Pizza pizza = new PepperoniPizza();
13. pizza.addTopping("Mushrooms");
14. }
15.}

What is the result?

A - Compilation fails.
B - Cannot add Toppings.
C - The code runs with no output.
D – A NullPointerException is thrown in line 4.

33. Given:

31. //some code here


32. try {
33. //some code here
34. } catch (SomeException se) {
35. //some code here
36. } finally {
37. //some code here
38. }

Under which three circumstances will the code on line 37 be executed? (Choose three).

A - The instance gets garbage collected.


B - The code on line 33 throws an exception.
C - The code on line 35 throws an exception.
D - The code on line 31 throws an exception.
E - The code on line 33 executes successfully.
34. Given:

84. try {
85. ResourceConnection con = resourceFactory.getConnection();
86. Results r = con.query("GET INFO FROM CUSTOMER");
87. info = r.getData();
88. con.close();
89. } catch (ResourceException re) {
90. errorLog .write(re.getMessage());
91. }
92. return info;

Which statement is true if a ResourceException is thrown on line 86?

A - Line 92 will not execute.


B - The connection will not be retrieved in line 85.
C - The resource connection will not be closed on line 88.
D - The enclosing method will throw an exception to its caller.

35. Given:

11. static void test() throws Error {


12. if (true) throw new AssertionError();
13. System.out.print("test");
14. }
15. public static void main(String[ ] args) {
16. try { test(); }
17. catch (Exception ex) { System.out.print("exception "); }
18. System.out.print("end ");
19. }

What is the result?

A - end
B - Compilation fails.
C - exception end
D - exception test end
E - A Throwable is thrown by main.
F - An Exception is thrown by main.
36. Given:

10: public class Hello {


11: String title;
12: int value;
13: public Hello() {
14: title+=" World";
15: }
16: public Hello(int value) {
17: this.value = value;
18: title = "Hello";
19: Hello();
20: }
21: }

and:

30: Hello c = new Hello(5);


31: System.out.println(c.title);

What is the result?

A - Hello
B - Hello World
C - Compilation fails.
D - Hello World 5
E - The code runs with no output.
F - An exception is thrown at runtime.

37.Given:

55. int []x = {1,2, 3, 4, 5};


56. int y[] = x;
57. System.out.println(y[2]);

Which statement is true?

A - Line 57 will print the value 2.


B - Line 57 will print the value 3.
C - Compilation will fail because of an error in line 55.
D - Compilation will fail because of an error in line 56.
38. Given:

11. public static void test(String str) {


12. if (str == null | str.length() == 0) {
13. System.out.println("String is empty");
14. } else {
15. System.out.println("String is not empty");
16. }
17.}

And the invocation:

31. test(null);

What is the result?

A - An exception is thrown at runtime.


B - "String is empty" is printed to output.
C - Compilation fails because of an error in line 12.
D - "String is not empty" is printed to output.

39. Given:

1. public class GC {
2. private Object o;
3. private void doSomethingElse(Object obj) { o = obj;}
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9. o = null;
10. }
11.}

When the doSomething method is called, after which line does the Object created in
line 5 become available for garbage collection?
A - Line 5
B - Line 6
C - Line 7
D - Line 8
E - Line 9
F - Line 10
40. Given:

1. public class Target {


2. private int i = 0;
3. public int addOne(){
4. return ++i;
5. }
6. }

And:

1. public class Client {


2. public static void main(String[] argsX
3. System.out.println(new Target().addOne()):
4. }
5. }

Which change can you make to Target without affecting Client?

A - Line 4 of class Target can be changed to return i++;


B - Line 2 of class Target can be changed to private int i = 1;
C - Line 3 of class Target can be changed to private int addOne(){
D - Line 2 of class Target can be changed to private Integer i = 0;

41. Given:

11. public void testIfA() {


12. if (testIfB("True")) {
13. System.out.println("True");
14. } else {
15. System.out.println("Not true");
16. }
17. }
18. public Boolean testIfB(String str) {
19. return Boolean.valueOf(str);
20. }

What is the result when method testIfA is invoked?

A - True
B - Not true
C- An exception is thrown at runtime.
D - Compilation fails because of an error at line 12
E - Compilation fails because of an error at line 19
42. Given:

15. public class Yippee {


16. public static void main(String [ ] args) {
17. for(int x= 1; x< args.length; x++) {
18. System.out.print(args[x] +"");
19. }
20. }
21. }

and two separate command line invocations:

java Yippee
java Yippee 1 2 3 4

What is the result?

A - No output is produced.
123
B - No output is produced.
234
C - No output is produced.
1234
D - An exception is thrown at runtime.
123
E - An exception is thrown at runtime.
234
F - An exception is thrown at runtime.
1234

43. Given:

10. public class Foo {


11. static int[ ] a;
12. static { a[0]=2;}
13. public static void main( String[ ] args ) { }
14.}

Which exception or error will be thrown when a programmer attempts to run this
code?

A - java.lang.StackOverflowError
B - java.lang.IllegalStateException
C - java.lang.ExceptionInInitializerError
D - java.lang.ArrayIndexOutOfBoundsException
44. Given:

11. public static void main(String[] args) {


12. Integer i = new Integer(l) + new Integer(2):
13. switch(i) {
14. case 3: System.out.println(“three"); break;
15. default: System.out.println("other"); break;
16. }
17.}

What is the result?


A - three
B - other
C - An exception is thrown at runtime.
D - Compilation fails because of an error on line 12.
E - Compilation fails because of an error on line 13.
F - Compilation fails because of an error on line 15.

45. Given:

1. class ClassA {
2. public int numberOfInstances;
3. protected ClassA(int numberOfInstances) {
4. this.numberOfInstances = numberOfInstances;
5. }
6.}
7. public class ExtendedA extends ClassA {
8. private ExtendedA(int numberOfInstances) {
9. super(numberOflnstances);
10. }
11. public static void main(String[ ] args) {
12. ExtendedA ext = new ExtendedA(420);
13. System. out.print(ext.numberOfInstances);
14. }
15.}

Which statement is true? )

A - 420 is the output.


B - An exception is thrown at runtime.
C - All constructors must be declared public.
D - Constructors CANNOT use the private modifier.
E - Constructors CANNOT use the protected modifier.
46. Given:

12. public class Wow {


13. public static void go(short n) {System.out.println("short");}
14. public static void go(Short n) {System.out.println("SHORT");}
15. public static void go(Long n) {System.out.println(" LONG");}
16. public static void main(String [ ] args) {
17. Short y = 6;
18. int z = 7;
19. go(y);
20. go(z);
21. }
22.}

What is the result?

A - short LONG
B - SHORT LONG
C - Compilation fails.
D - An exception is thrown at runtime.

47.Given:

11. public static void main(String[ ] args) {


12. try{
13. args = null;
14. args[0] = "test";
15. System.out.println(args[0]);
16. } catch (Exception ex) {
17. System.out.println("Exception");
18. } catch (NullPointerException npe) {
19. System.out.println("NullPointerException");
20. }
21. }

What is the result?


A - test
B - Exception
C - Compilation fails.
D - NullPointerException
48.Given a class Repetition:

1. package utils;
2. public class Repetition {
3. public static String twice(String s) {return s + s;}
4.}
and given another class Demo:
1. // insert code here
2. public class Demo {
3. public static void main(String[ ] args) {
4. System .out.println(twice("pizza"));
5. }
6. }

Which code should be inserted at line 1 of Demo.java to compile and run Demo to
print "pizzapizza"?

A - import utils.*;
B - static import utils.*;
C - import utils.Repetition.*;
D - static import utils.Repetition.*;
E - import utils.Repetition.twice();
F - import static utils.Repetition.twice;
G - static import utils.Repetition.twice;

49. Which statement is true about the two classes?

SomeException:
1. public class SomeException {
2. }

Class A:
1. public class A {
2. public void doSomething() { }
3. }

Class B:
1. public class B extends A {
2. public void doSomething() throws SomeException { }
3. }

A - Compilation of both classes will fail.


B - Compilation of both classes will succeed
C - Compilation of class A will fail. Compilation of class B will succeed.
D - Compilation of class B will fail. Compilation of class A will succeed.
50. Given:

1. public class Score implements Comparable<Score> {


2. private int wins, losses;
3. public Score(int w, int i) { wins = w: losses = i;}
4. public int getWins() {return wins;}
5. public int getLosses() {return losses;}
6. public String toString() {
7. return "<" + wins +”,”+ losses + ">";
8. }
9. // insert code here
10.}

Which method will complete this class?

A - public int compareTo (Object o){/*more code here*/}


B - public int compareTo (Score other){/*more code here*/}
C - public int compare (Score s1, Score s2){/*rnore code here"/}
D - public int compare ( Object o1, Object o2){/*more code here"/}

51. Fill in the blank to import the DEFAULT_VALUE:

17. package com.foo;


18.
19. public class Foo {
20. public static final int DEFAULT_VALUE = 10;
21. }
22.
23. package com.bar;
24.
25. __________________;
26.
27. public class Bar {
28. }

A) import com.foo.Foo.DEFAULT_VALUE
B) import static com.foo.Foo.DEFAULT_VALUE
C) static import com.foo.Foo.DEFAULT_VALUE
D) static import com.foo.Foo
52. Given:

11. class Snoochy {


12. Boochy booch;
13. public Snoochy() { booch = new Boochy(this); }
14.}
15. class Boochy {
16. Snoochy snooch;
17. public Boochy(Snoochy s) { snooch = s;}
18.}
And the statements:
21. public static void main(String[] args) {
22. Snoochy snoog = new Snoochy();
23. snoog = null;
24. // more code here
25. }

Which statement is true about the objects referenced by snoog, snooch and booch
immediately after line 23 executes?

A - None of these objects are eligible for garbage collection.


B - Only the object referenced by booch is eligible for garbage collection.
C - Only the object referenced by snoog is eligible for garbage collection.
D - Only the object referenced by snooch is eligible for garbage collection.
E - The objects referenced by snooch and booch are eligible for garbage collection.
53. A programmer needs to create a logging method that can accept an arbitrary
number of arguments. For example, it may be called in these ways:

logIt("log message1”);
logIt("log message2","log message3");
logIt(“log message4","log message5","log message6");

Which declaration satisfies this requirement?

A - public void logIt(String * msgs)


B - public void logIt(String [ ] msgs)
C - public void logIt(String…msgs)
D - public void logIt(String msgl, String msg2, String msg3)

54. What is the result?

11. class Person {


12. String name = "No name";
13. public Person(String nm) { name = nm; }
14.}
15. class Employee extends Person {
16. String empID = "0000";
17. public Employee(String id) { empID =id; }
18.}
19. public class EmployeeTest {
20. public static void main(String[ ] args){
21. Employee e = new Employee("4321");
22. System.out.println(e.empID);
23. }
24.}

A - 4321
B - 0000
C - An exception is thrown at runtime.
D - Compilation fails because of an error in line 17
55. Given:

1. public class TestString1 {


2. public static void main(String[ ] args} {
3. String str = "420";
4. str += 42;
5. System.out.print(str);
6. }
7.}

What is the output?

A - 42
B - 420
C - 462
D - 42042
E - Compilation fails.
F - An exception is thrown at runtime.

56. Given:
1. import java.util.*;
2. public class LetterASort {
3. public static void main(String[ ] args) {
4. ArrayList<String> strings = new ArrayList<String>();
5. strings.add("aAaA");
6. strings.add("AaA");
7. strings.add("aAa");
8. strings.add("AAaa");
9. Collections.sort(strings);
10. for(String s : strings){ System.out.print(s + “ ”);}
11. }
12.}

What is the result?

A - Compilation falls.
B - aAaA aAa AAaa AaA
C - AAaa AaA aAa aAaA
D - AaA AAaa aAaA aAa
E - aAa AaA aAaA AAaa
F - An exception is thrown at runtime.
57. Given:

11. String[ ] elements = {"for", "tea", "too"};


12. String first = elements.length > 0 ? elements[0]: null;

What is the result?

A - Compilation fails.
B - An exception is thrown at runtime.
C - The variable first is set to null.
D - The variable first is set to elements[0].

58. Given:

11. class Converter {


12. public static void main(String[ ] args) {
13. Integer i = args[0];
14. int j =12;
15. System.out.println("It is" + (j = = i) +" that j = = i.");
16. }
17. }

What is the result when the programmer attempts to compile the code and run it
with the command java Converter 12?

A - It is true that j==i.


B - It is false that j==i.
C - An exception is thrown at runtime.
D - Compilation fails because of an error in line 13.

59. Which two code fragments correctly create and initialize a static array of int
elements? (Choose two.)

A - static final int[ ] a = {100,200 };


B - static final int[ ] a;
static { a=new int[2]; a[0]=100; a[1]=200; }
C - static final int[ ] a = new int[2]{ 100,200 };
D - static final int[ ] a;
static void init() { a = new int[3]; a[0]=l00; a[l]=200; }
60. Given the following code in a file called MySplurt.java:
public class Splurt {
int it = 66;
public int getIt() {
return it;
}
}
public class MySplurt extends Splurt {
int it = 101;
public static void main(String...args) {
System.out.print(new Splurt().getIt());
System.out.print(new MySplurt().getIt());
}
}
What is the result of attempting to compile and run the file in a typical Java
environment?

A) 6666
B) 66101
C) 101101
D) An exception is thrown at runtime
E) Compilation fails

61. Which of the following are legal statements?

A) int [ ] [ ] array = new int [ ] [3];


B) int [ ] [ ] array = new int [3] [ ]; +
C) int [ ] [] array = new int [3] [3]; +
D) int [ ] array = {‘a’, ’b’, ‘c’}; +
E) int [ ] array = new int [3]; +
F) int [3] array = {3, 4, 5}; -
G) int [ ] array = new int [ ] {1, 2, 3}; +
H) int array = new int [3] {1, 2, 3}; -
I) int [ ] array = new short [3]; -
J) Object [ ] array = new Integer [3]; +
K) int [ ] array = {1, 2, 3}; +
62. What is the result of attempting to compile and run this class?
public class Zobb {
public static void main(String [] args) {
StringBuilder sb = new StringBuilder("Foo");
sb.append("bar").delete(2, 4).reverse().insert(2, "tt");
String s = sb.toString().substring(1, 5);
s.toUpperCase();
System.out.println(s);
}
}
A) OTTF
B) TTAO
C) ROTTF
D) atto
E) ttao
F) ottf
G) rottF
H) rattoF
I) Compile time error
J) Compiles and outputs a String not listed above
K) An exception is thrown at run time

63. Which of these are valid JavaBeans method signatures?

A) public boolean isActive ( )


B) int getFooNumber ( )
C) public long size ( )
D) public void addElement (Element e)
E) public void removeWibbleListener (BurbleListener w)
F) public boolean getActive ( )

64. After the statements:

String s1 = "hello world";


String s2 = "hello world";
StringBuffer sb1 = new StringBuffer("hello world");
StringBuffer sb2 = new StringBuffer("hello world");

Which of the following expressions would evaluate as true?


A) s1.equals (s2)
B) s1 = = s2
C) sb1 = = sb2
D) sb1.equals (sb2)
65. Given:

String str1 = new String("charith");


String str2 = new String("charith");
String str3 = "charith";
String str4 = "charith";
System.out.print((str1==str2) + " ");
System.out.print(str3==str4);
What is result?
A) false false
B) true true
C) true false
D) false true
E) Compilation fails

66. Given the code. What is the result?

class Hotel {
public int bookings;
public void book() {
bookings++;
}
}
public class SuperHotel extends Hotel {
public void book() {
bookings--;
}
public void book(int size) {
book();
super.book();
bookings += size;
}
public static void main(String args[]) {
Hotel hotel = new SuperHotel();
hotel.book(2);
System.out.print(hotel.bookings);
}
}
A) Compilation fails
B) An exception is thrown at runtime
C) 0
D) 1
E) 2
F) -1
67. Given the code. What is the result?

public class SomeClass {


private int value = 1;
public int getValue() {
return value;
}
public void changeVal(int value) {
value = value;
}
public static void main(String args[]) {
int a = 2;
SomeClass c = new SomeClass();
c.changeVal(a);
System.out.print(c.getValue());
}
}

A) "1" is printed
B) "2" is printed
C) Compilation fails
D) An exception is thrown at runtime

68. Given the code. What is the result?

public static void main(String args[]) {


try {
String arr[] = new String[10];
arr = null;
arr[0] = "one";
System.out.print(arr[0]);
} catch(Exception ex) {
System.out.print("exception");
} catch(NullPointerException nex) {
System.out.print("null pointer exception");
}
}

A) "one" is printed
B) " exception " is printed
C) "null pointer exception" is printed
D) Compilation fails
69. Given the code. What is the output?

public class Test {


int a = 10;
public void doStuff(int a) {
a += 1;
System.out.println(a++);
}
public static void main(String args[]) {
Test t = new Test();
t.doStuff(3);
}
}

A) 11
B) 12
C) 4
D) 5

70.Given the code. What is the result?

class Hotel {
public void book() throws Exception {
throw new Exception();
}
}
public class SuperHotel extends Hotel {
public void book() {
System.out.print("booked");
}
public static void main(String args[]) {
Hotel h = new SuperHotel();
h.book();
}
}

A) An exception is thrown at runtime


B) Compilation fails
C) The code runs without any output
D) "booked" is printed

You might also like