Questions
Questions
Given:
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:
java Yippee2 a b c
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:
21. test("four");
22. test("tee");
23. test("to");
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.
Which two pairs of method declarations follow the JavaBeans standard for
accessing this field? (Choose two.)
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:
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:
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.}
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. }
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.}
14. Given:
1. public class B {
2. public void method2() throws TestException {
3. // more code here
4. }
5. }
25. try {
26. A a = new A();
27. a.method1();
28.} catch (Exception e) {
29. System.out.print("an error occurred");
30.}
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.}
18. Given:
A - null
B - zero
C - some
D - Compilation fails.
E - An exception is thrown at runtime.
19. Given:
A - null
B - finally
C - null finally
D - Compilation fails
E - finally exception
20. Given:
If methodA is invoked, which two are true at line 19? (Choose two.)
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:
A - int Long
B - Short Long
C - Compilation fails.
D - An exception is thrown at runtime.
23. Given:
A - Compilation fails.
B - Class C is displayed.
C - The code runs with no output.
D - An exception is thrown at runtime
24. Given:
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:
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:
A-0
B-1
C-2
D-a
E-b
F-c
G - The result is undefined.
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);
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:
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.}
A - Compilation fails.
B - Cannot add Toppings.
C - The code runs with no output.
D – A NullPointerException is thrown in line 4.
33. Given:
Under which three circumstances will the code on line 37 be executed? (Choose three).
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;
35. Given:
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:
and:
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:
31. test(null);
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:
And:
41. Given:
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:
java Yippee
java Yippee 1 2 3 4
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:
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:
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.}
A - short LONG
B - SHORT LONG
C - Compilation fails.
D - An exception is thrown at runtime.
47.Given:
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;
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) 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:
Which statement is true about the objects referenced by snoog, snooch and booch
immediately after line 23 executes?
logIt("log message1”);
logIt("log message2","log message3");
logIt(“log message4","log message5","log message6");
A - 4321
B - 0000
C - An exception is thrown at runtime.
D - Compilation fails because of an error in line 17
55. Given:
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.}
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:
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:
What is the result when the programmer attempts to compile the code and run it
with the command java Converter 12?
59. Which two code fragments correctly create and initialize a static array of int
elements? (Choose two.)
A) 6666
B) 66101
C) 101101
D) An exception is thrown at runtime
E) Compilation fails
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?
A) "1" is printed
B) "2" is printed
C) Compilation fails
D) An exception is thrown at runtime
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?
A) 11
B) 12
C) 4
D) 5
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();
}
}