Поможем написать учебную работу
Если у вас возникли сложности с курсовой, контрольной, дипломной, рефератом, отчетом по практике, научно-исследовательской и любой другой работой - мы готовы помочь.
Если у вас возникли сложности с курсовой, контрольной, дипломной, рефератом, отчетом по практике, научно-исследовательской и любой другой работой - мы готовы помочь.
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. Date date = new Date();
13. df.setLocale(Locale.ITALY);
14. String s = df.format(date);
The variable df is an object of type DateFormat that has been initialized in line 11. What is the result if this code is run on December 14, 2000?
A - The value of s is 14-dic-2004.
B - The value of s is Dec 14, 2000.
C - An exception is thrown at runtime.
D - Compilation fails because of an error in line 13.
3. When comparing java.io.BufferedWriter to Java.io.FileWriter, which capability exists as a method in only one of the two?
A - closing the stream
B - flushing the stream
C - writing to the stream
D - marking a location in the stream
E - writing a line separator to the stream
4. Which code, inserted at line 11, will allow this class to correctly serialize and deserialize?
1. import java.io.*;
2. public class Foo implements Serializable {
3. public int x, y;
4. public Foo( int x, int y ) {
5. this.x =x; this.y = y; }
6. private void writeObject (ObjectOutputStream s ) throws IOException {
7. s.writeInt(x); s.writeInt(y) ;
8. }
9. private void readObject (ObjectInputStream s )
10. throws IOException,C1assNotFoundException {
11. // insert code here
12. }
13. }
A - s.defaultReadObject();
B - this = s.defaultReadObject();
C - y = s.readInt(); x = s.readInt();
D - x = s.readInt(); y = s.readInt();
5. Which three statements concerning the use of the java.io.Serializable interface are true? (Choose three.)
A - Objects from classes that use aggregation cannot be serialized.
B - An object serialized on one JVM can be successfully deserialized on a different
JVM.
C - The values in fields with the volatile modifier will NOT survive serialization
and deserialization.
D - The values in fields with the transient modifier will NOT survive serialization
and deserialization.
E - It is legal to serialize an object of a type that has a supertype that does NOT
implement java.io.Serializable.
6. 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 locale's country name and the appropriate version of d's date?
A - Locale loc = Locale.getLocale();
System .out.println(loc.getDisplayCountry() + " " + df.format(d));
B - Locale loc = Locale.getDefault();
System .out.println(loc.getDisplayCountry() + " " + df.format(d));
C - Locale loc = Locale.getLocale();
System .out.println(loc.getDisplayCountry() + " " + df .setDateFormat(d));
D - Locale loc = Locale.getDefault();
System .out.println(loc.getDisplayCountry() + " " + df.setDateFormat(d));
7. 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 - a b
B - b c
C - a b c
D - Compilation fails.
E - An exception is thrown at runtime.
8. 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);
9. Given:
enum Example { ONE, TWO, THREE }
Which statement is true?
A - The expressions (ONE == ONE) and ONE.equals(ONE) are both
guaranteed to be true.
B - The expression (ONE < TWO) is guaranteed to be true and
ONE.compareTo(TWO) is guaranteed to be less than one.
C - The Example values cannot be used in a raw java.util.HashMap; instead,
the programmer must use a java.util.EnumMap.
D The Example values can be used in a java.util.SortedSet, but the set
will NOT be sorted because enumerated types do NOT implement
java.lang.Comparable.
10. A developer is creating a class Book, that needs to access class Paper. The Paper class is deployed in a JAR named myLib.jar. Which three, taken independently, will allow the developer to use the Paper class while compiling the Book class? (Choose three.)
A - The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.
B - The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar..
C - The JAR file is located at /foo/myLib.jar and a classpath environment
variable is set that includes /foo/myLib.jar/Paper.class
D - The JAR file is located at /foo/myLib.jar and a classpath environment
variable is set that includes /foo/myLib.jar.
E - The JAR file is located at /foo/myLib.jar and the Book class is compiled
using javac cp /foo/myLib.jar/Paper Book.java.
F - The JAR file is located at /foo/myLib.jar and the Book class is compiled
using javac d /foo/myLib.jar Book.java
G - The JAR file is located at /foo/myLib.jar and the Book class is compiled
using javac - classpath /foo/myLib.jar Book.java
11. Given:
1. import java.util.*;
2. public class PQ {
3. public static void main(String[] args) {
4. PriorityQueue<String> pq = new PriorityQueue<String>();
5. pq.add("carrot");
6. pq.add("apple");
7. pq.add("banana");
8. System.out.println(pq.poll() +":" + pq.peek());
9. }
10.}
What is the result?
A - apple:apple
B - carrot:apple
C - apple:banana
D - banana:apple
E - carrot:carrot
F - carrot:banana
12. 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.
13. Given:
23. Object [] myObjects = {
24. new Integer(12),
25. new String("foo"),
26. new Integer(5),
27. new Boolean(true)
28. };
29. Arrays.sort(myObjects);
30. for(int i=0; i<myObjects.length; i++) {
31. System.out.print(myObjects[i].toString());
32. System.out.print(" ");
33. }
What is the result?
A - Compilation fails due to an error in line 23.
B - Compilation fails due to an error in line 29.
C - A ClassCastException occurs in line 29.
D - A ClassCastException occurs in line 31.
E - The value of all four objects prints in natural order.
14. Given:
1. import java.util.*;
2. public class WrappedString {
3. private String s;
4. public WrappedString(String s) {this.s = s;}
5. public static void main(String[] args) {
6. HashSet<Object> hs = new HashSet<Object>();
7. WrappedString wsl = new WrappedString("aardvark");
8. WrappedString ws2 = new WrappedString("aardvark");
9. String s1 = new String("aardvark");
10. String s2 = new String("aardvark");
11. hs.add(wsl); hs.add(ws2); hs.add(sl); hs.add(s2);
12. System.out.println(hs.size());}}
What is the result?
A - 0
B - 1
C - 2
D - 3
E - 4
F - Compilation fails.
G - An exception is thrown at runtime.
15. Given:
11. public class Key {
12. private long id1;
13. private long id2;
14. // class Key methods
15. }
A programmer is developing a class Key, that will be used as a key in a standard java.util.HashMap. Which two methods should be overridden to assure that Key works correctly as a key? (Choose two.)
A - public int hashCode()
B - public boolean equals(Key k)
C - public int compareTo(Object o)
D - public boolean equals(Object o)
E - public boolean compareTo(Key k)
16. 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.
17. Given a pre-generics implementation of a method:
11. public static int sum(List list) {
12. int sum = 0;
13. for (Iterator iter = list.iterator(); iter.hasNext();) {
14. int i = ((lnteger)iter.next()).intValue();
15. sum+=i;
16. }
17. return sum;
18.}
Which three changes must be made to the method sum to use generics? (Choose three.)
A - remove line 14
B - replace line 14 with "int i = iter.next();"
C - replace line 13 with "for (int i : IntList) {"
D - replace line 13 with "for (Iterator iter : intList) {"
E - replace the method declaration with "sum(List<int> IntList)"
F - replace the method declaration with "sum(List<Integer> IntList)"
18. Given:
11. // insert code here
12. private N min, max;
13. public N getMin() { return min; }
14. public N getMax() { return max; }
15. public void add( N added ) {
16. if (min == null || added.doubleValue() < min.doubleValue())
17. min = added;
18. if (max == null || added.doubleValue() > max.doubleValue())
19. max = added;
20. }
21. }
Which two, inserted at line 11, will allow the code to compile? (Choose two.)
A - public class MinMax<?> {
B - public class MinMax<? extends Number> {
C - public class MinMax<N extends Object> {
D - public class MinMax<N extends Number> {
E - public class MinMax<? extends Object> {
F - public class MinMax<N extends Integer> {
19. 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()
20. Which two are possible results? (Choose two.)
1. public class Threads1 {
2. int x = 0;
3. public class Runner implements Runnable {
4. public void run() {
5. int current = 0;
6. for(int i = 0; i < 4; i++) {
7. current = x;
8. System.out.print(current + ", ");
9. x = current + 2;
10. }
11. }
12. }
13. public static void main(String[] args) {
14. new Threadsl().go();
15. }
16. public void go() {
17. Runnable r1 = new Runner();
18. new Thread(r1).start();
19. new Thread(r1).start();
20. }
21.}
A - 0, 2, 4, 4, 6, 8, 10, 6,
В - 0, 2, 4, 6, 8, 10, 2, 4,
С - 0, 2, 4, 6, 8, 10, 12, 14,
D - 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
E - 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
21. Given:
11. String test = "This is a test";
12. String[] tokens = test.split("\s");
13. System.out.println(tokens.length);
What is the result?
A- 0
B- 1
C- 4
D- Compilation fails.
E- An exception is thrown at runtime.
22. Given:
1. public class Threads3 implements Runnable {
2. public void run() {
3. System .out.print("running");
4. }
5. public static void main(String[] args) {
6. Thread t = new Thread(new Threads3());
7. t.run();
8. t.run();
9. t.start();
10. }
11. }
What is the result?
A - Compilation fails.
B - An exception is thrown at runtime.
C - The code executes and prints "running".
D - The code executes and prints "runningrunning".
E - The code executes and prints "runningrunningrunning".
23. Which three will compile and run without exception? (Choose three.)
A - private synchronized Object o;
B - void go(){
synchronized() { /* code here */}
C - public synchronized void go() { /* code here */ }
D - private synchronized(this) void go() {/* code here */}
E - void go(){
synchronized(Object.class) {/* code here */}
F - void go(){
Object o = new Object();
synchronized(o) {/* code here */}
24. Given:
10. class Nav{
11. public enum Direction { NORTH, SOUTH, EAST, WEST }
12.}
13. public class Sprite{
14. // insert code here
15. }
Which code, inserted at line 14, allows the Sprite class to compile?
A - Direction d = NORTH;
B Nav.Direction d = NORTH;
C - Direction d = Direction.NORTH;
D - Nav.Direction d = Nav.Direction.NORTH;
25. Given:
7. void waitForSignal() {
8. Object obj = new Object();
9. synchronized (Thread.currentThread()) {
10. obj.wait();
11. obj.notify();
12. }
13. }
Which statement is true?
A - This code may throw an InterruptedException.
B - This code may throw an IllegalStateException.
C - This code may throw a TimeoutException after ten minutes.
D - This code will not compile unless "obj.wait()" is replaced with
"((Thread) obj).wait()".
E - Reversing the order of obj.wait() and obj.notify() may cause this method
to complete normally.
F - A call to notify() or notifyAll() from another thread may cause this method
to complete normally.
26. What is the result?
1. class Computation extends Thread {
2. private int num;
3. private boolean isComplete;
4. private int result;
5. public Computation(int num) { this.num= num; }
6. public synchronized void run() {
7. result = num * 2;
8. isComplete = true;
9. notify();
10. }
11. public synchronized int getResult() {
12. while (!isComplete) {
13. try {
14. wait();
15. } catch (InterruptedException e) {}
16. }
17. return result;
18. }
19. public static void main(String[] args){
20. Computation[] computations = new Computation[4];
21. for (int i = 0; i < computations.length; i++) {
22. computations[i] = new Computation(i);
23. computations[i].start();
24. }
25. for (Computation c : computations)
26. System.out.print (c.getResult() + " ");
27. }
28. }
A - The code will deadlock.
B - The code may run with no output.
C - An exception is thrown at runtime.
D - The code may run with output "0 6".
E - The code may run with output "2 0 6 4".
F - The code may run with output "0 2 4 6".
27. Which two statements are true about has-a and is-a relationships? (Choose two.)
A - Inheritance represents an is-a relationship.
B - Inheritance represents a has-a relationship.
C - Interfaces must be used when creating a has-a relationship.
D - Instance variables can be used when creating a has-a relationship.
28. 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 - 1 2 3
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.
29. Given:
1. public class TestOne implements Runnable {
2. public static void main (String[] args) throws Exception {
3. Thread t = new Thread (new TestOne());
4. t.start();
5. System. out.print ("Started");
6. t.join();
7. System .out.print ("Complete");
8. }
9. public void run() {
10. for (int i = 0; i <4; i++) {
11. System.out.print (i);
12. }
13. }
14.}
What can be a result?
A - Compilation fails.
B - An exception is thrown at runtime.
C - The code executes and prints "StartedComplete".
D - The code executes and prints "StartedComplete0123".
E - The code executes and prints "Started0123Complete".
30. Given:
1. public class TestOne {
2. public static void main (String[] args) throws Exception {
3. Thread.sleep(3000);
4. System .out.println("sleep");
5. }
6. }
What is the result?
A - Compilation fails.
B - An exception is thrown at runtime.
C - The code executes normally and prints "sleep".
D - The code executes normally, but nothing is printed.
31. Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.)
A - new Thread() {
public void run() { doStuff();}
};
B - new Thread() {
public void start() { doStuff();}
};
C - new Thread() {
public void start() { doStuff();}
}.run();
D - new Thread() {
public void run() { doStuff();}
}.start();
E - new Thread (new Runnable() {
public void run() { doStuff();}
}).run();
F - new Thread(new Runnable() {
public void run() { doStuff();}
}).start();
32. 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.
33. Given:
11.Runnable r = new Runnable() {
12. public void run() {
13. System. out.print("Cat");
14. }
15.};
16.Thread t = new Thread(r) {
17. public void run() {
18. System.out.print("Dog");
19. }
20.};
21.t.start();
What is the result?
A - Cat.
B - Dog.
C - Compilation fails.
D - The code runs with no output.
E - An exception is thrown at runtime.
34. Given:
1. public class TestFive {
2. private int x;
3. public void foo() {
4. int current = x;
5. x = current + 1;
6. }
7. public void go() {
8. for(int i = 0; i < 5; i++) {
9. new Thread() {
10. public void run() {
11. foo();
12. System.out.print(x +", ");
13. }
14. }.start();
15. }
16. }
17.}
Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ? (Choose two.)
A - move the line 12 print statement into the foo() method
B - change line 7 to public synchronized void go() {
C - change the variable declaration on line 2 to private volatile intx;
D - wrap the code inside the foo() method with a synchronized( this ) block
E - wrap the for loop code inside the go() method with a synchronized block
synchronized(this) {//for loop code here }
35. Given:
foo and bar are public references available to many other threads, foo refers to a Thread and bar is an Object. The thread foo is currently executing bar.wait(). From another thread, what provides the most reliable way to ensure that foo will stop executing wait()?
A - foo.notify();
B - bar.notify();
C - foo.notifyAll();
D - Thread.notify();
E - bar.notifyAll();
F - Object.notify();
36. 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.
37. 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.
38. Given:
11. public static Iterator reverse(List list) {
12. Collections.reverse(list);
13. return list.iterator();
14. }
15. public static void main(String[] args) {
16. List list = new ArrayList();
17. list.add("1"); list.add("2"); list.add("3");
18. for (Object obj: reverse(list))
19. System.out.print(obj +", ");
20. }
What is the result?
A - 3, 2, 1,
B - 1, 2, 3,
C - Compilation fails.
D - The code runs with no output.
E - An exception is thrown at runtime.
39. 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.
40. 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
41. Given:
11.public static Collection get() {
12. Collection sorted = new LinkedList();
13. sorted.add("B"); sorted.add("C"); sorted.add("A");
14. return sorted;
15.}
16.public static void main(String[] args) {
17. for (Object obj: get()) {
18. System.out.print (obj +", ");
19. }
20.}
What is the result?
What is the result?
A - A, B, C,
B - B, C, A,
C - Compilation fails.
D - The code runs with no output.
E - An exception is thrown at runtime.
42. What is the output if the main() method is run?
10. public class Starter extends Thread {
11. private int x = 2;
12. public static void main(String[] args) throws Exception {
13. new Starter().makeItSo();
14. }
15. public Starter() {
16. x = 5;
17. start();
18. }
19. public void makeItSo() throws Exception {
20. join();
21. x = x - 1;
22. System.out.println(x);
23. }
24. public void run() { x *= 2; }
25. }
A - 4
B - 5
C - 8
D - 9
E - Compilation fails.
F - An exception is thrown at runtime.
G - It is impossible to determine for certain.
43. Given a method that must ensure that its parameter is not null:
11.public void someMethod(Object value) {
12. // check for null value
…
20. System .out.println(value.getClass());
21.}
What, inserted at line 12, is the appropriate way to handle a null value?
A - assert value == null;
B - assert value != null, "value is null";
C - if (value == null) {
throw new AssertionException("value is null"); }
D - if (value == null) {
throw new IllegalArgumentException("value is null");}
44. Given:
23. int z = 5;
24. public void stuff 1 (int x) {
25. assert (x>0);
26. switch(x){
27. case 2: x = 3;
28. default: assert false; }}
29. private void stuff2(int y) { assert (y < 0);}
30. private void stuff3() { assert (stuff4());}
31. private boolean stuff4() {z = 6; return false;}
Which statement is true?
A - All of the assert statements are used appropriately.
B - Only the assert statement on line 29 is used appropriately.
C - The assert statements on lines 28 and 29 are used appropriately.
D - The assert statements on lines 25 and 28 are used appropriately.
E - The assert statements on lines 28 and 30 are used appropriately.
F - The assert statements on lines 28, 29, and 30 are used appropriately.
G - The assert statements on lines 25, 28, and 29 are used appropriately.
45. Given:
1. package geometry;
2. public class Hypotenuse {
3. public InnerTriangle it = new lnnerTriangle();
4. class InnerTriangle {
5. public int base;
6. public int height;
7. }
8.}
Which statement is true about the class of an object that can reference the variable base?
A - It can be any class.
B - No class has access to base.
C - The class must belong to the geometry package.
D - The class must be a subclass of the class Hypotenuse.
46. Which two statements are true if this class is compiled and run? (Choose two.)
1. import java.util.*;
2. public class NameList {
3. private List names = new ArrayList();
4. public synchronized void add(String name) { names.add(name); }
5. public synchronized void printAll() {
6. for (int i = 0; i < names.size();i++) {
7. System.out.print (names.get(i) + " ");
8. }
9. }
10. public static void main(String[] args) {
11. final NameList sl = new NameList();
12. for (int i = 0; i < 2; i++) {
13. new Thread() {
14. public void run() {
15. s1.add("A");
16. s1.add("B");
17. s1.add("C");
18. s1.printAll();
19. }
20. }.start();
21. }
22. }
23.}
A - An exception may be thrown at runtime.
B - The code may run with no output, without exiting.
C - The code may run with no output, exiting normally.
D - The code may run with output "A B A B C C ", then exit.
E - The code may run with output "A B C A B C A B C ", then exit.
F - The code may run with output "A A A B C A B C C ", then exit.
G - The code may run with output "A B C A A B C A B C ", then exit.
47. Which four statements are true? (Choose four.)
A - Has-a relationships should never be encapsulated.
B - Has-a relationships should be implemented using inheritance.
C - Has-a relationships can be implemented using instance variables.
D - Is-a relationships can be implemented using the extends keyword.
E - Is-a relationships can be implemented using the implements keyword.
F - The relationship between Movie and Actress is an example of an is-a
relationship.
G - An array or a collection can be used to implement a one-to-many has-a
relationship.
48. 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.
49. Given:
1.public class MyLogger {
2. private StringBuilder logger = new StringBuilder();
3. public void log(String message, String user) {
4. logger.append(message);
5. logger.append(user);
6. }
7.}
The programmer must guarantee that a single MyLogger object works properly for a multi-threaded system.
How must this code be changed to be thread-safe?
A - synchronize the log method
B - replace StringBuilder with StringBuffer
C - replace StringBuilder with just a String object and use the string concatenation
(+=) within the log method
D - No change is necessary, the current MyLogger code is already thread-safe.
50. Given a valid DateFormat object named df, and
16. Date d = new Date(0L);
17. String ds = "December 15, 2004";
18. // insert code here
What updates d's value with the date represented by ds?
A - 18. d = df.parse(ds);
B - 18. d = df.getDate(ds);
C - 18.try {
19. d= df.parse(ds);
20. } catch(ParseException e) { };
D - 18.try {
19. d = df.getDate(ds);
20. } catch(ParseException e) { };
51. Given:
12. import java.io.*;
13. public class Forest implements Serializable {
14. private Tree tree = new Tree();
15. public static void main(String [] args) {
16. Forest f = new Forest();
17. try{
18. FileOutputStream fs = new FileOutputStream("Forest.ser");
19. ObjectOutputStream os = new ObjectOutputStream(fs);
20. os.writeObject(f); os.close();
21. } catch (Exception ex) { ex.printStackTrace();}
22. }
23.}
24.class Tree {}
What is the result?
A - Compilation fails.
B - An exception is thrown at runtime.
C - An instance of Forest is serialized.
D - An instance of Forest and an instance of Tree are both serialized.
52. 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.
53. Which Man class properly represents the relationship "Man has a best friend who is a Dog"?
A - class Man extends Dog { }
B - class Man implements Dog { }
C - class Man {private BestFriend dog;}
D - class Man {private Dog bestFriend;}
E - class Man {private Dog<bestFriend>;}
F - class Man {private BestFriend<dog>;}
54. 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(newB());
22. list.add(newC());
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.
55. Given:
12. public class Test {
13. public enum Dogs {collie, harrier};
14. public static void main(String [] args) {
15. Dogs myDog = Dogs.collie;
16. switch (myDog) {
17. case collie:
18. System.out.print("collie");
19. case harrier:
20. System.out.print("harrier");
21. }
22. }
23.}
What is the result?
A. collie
B. harrier
C. Compilation fails.
D. collie harrier
E. An exception is thrown at runtime.
56. 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.
57. 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.
58. 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.
59. 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. A a = new B();
19. a.process();
20.}
What is the result?
A - B
B - The code runs with no output.
C - An exception is thrown at runtime.
D - Compilation fails because of an error in line 15.
E - Compilation fails because of an error in line 18.
F - Compilation fails because of an error in line 19.
60. 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