PRO192

Ace your homework & exams now with Quizwiz!

A, B, and C do not ensure that multithreaded code does not deadlock.

18. How can you ensure that multithreaded code does not deadlock? A. Synchronize access to all shared variables. B. Make sure all threads yield from time to time. C. Vary the priorities of your threads. D. A, B, and C do not ensure that multithreaded code does not deadlock.

D

18. How can you ensure that multithreaded code does not deadlock? A. Synchronize access to all shared variables. B. Make sure all threads yield from time to time. C. Vary the priorities of your threads. D. A, B, and C do not ensure that multithreaded code does not deadlock.

A, B, E

19. Which of the following operators can perform promotion on their operands? (Choose all that apply.) A. + B. - C. ++ D. -- E. ~ F. !

A final class may not be extended.

19. Which of the following statements are true? A. A final class must be instantiated. B. A final class may only contain final methods. C. A final class may not contain non-final data fields. D. A final class may not be extended. E. None of the above.

D

19. Which of the following statements are true? A. A final class must be instantiated. B. A final class may only contain final methods. C. A final class may not contain non-final data fields. D. A final class may not be extended. E. None of the above.

B

2. Which of the following statements is true? A. An abstract class may not have any final methods. B. A final class may not have any abstract methods.

D

22. Select the list of primitives ordered in smallest to largest bit size representation. A. boolean, char, byte, double B. byte, int, float, char C. char, short, long, float D. char, int, float, long E. None of the above

d

22. Suppose you are writing a class that will provide custom deserialization. The class implements java.io.Serializable (not java.io.Externalizable). What access mode should the readObject() method have? A. public B. protected C. default D. private

D

1. Which of the statements below are true? (Choose all that apply.) A. UTF characters are all 8 bits. B. UTF characters are all 16 bits. C. UTF characters are all 24 bits. D. Unicode characters are all 16 bits. E. Bytecode characters are all 16 bits. F. None of the above.

Unicode characters are all 16 bits.

1. Which of the statements below are true? (Choose all that apply.) A. UTF characters are all 8 bits. B. UTF characters are all 16 bits. C. UTF characters are all 24 bits. D. Unicode characters are all 16 bits. E. Bytecode characters are all 16 bits. F. None of the above.

D

10. What is the range of values that can be assigned to a variable of type byte? A. Depends on the underlying hardware B. 0 through 28 − 1 C. 0 through 216 − 1 D. −27 through 27 − 1 E. −215 through 215 − 1

A

10. What is the result of attempting to compile and execute the following code fragment? Assume that the code fragment is part of an application that has write permission in the current working directory. Also assume that before execution, the current working directory does not contain a file called datafile. 1. try { 2. RandomAccessFile raf = new 3. RandomAccessFile("datafile" ,"rw"); 4. BufferedOutputStream bos = new 5. BufferedOutputStream(raf); 6. DataOutputStream dos = new 7. DataOutputStream(bos); 8. dos.writeDouble(Math.PI); 9. dos.close(); 10. bos.close(); 11. raf.close(); 12. } 13. catch (IOException e) { } A. The code fails to compile. B. The code compiles but throws an exception at line 4. C. The code compiles and executes but has no effect on the local file system. D. The code compiles and executes; afterward, the current working directory contains a file called datafile.

B

11. Suppose a source file contains a large number of import statements. How do the imports affect the time required to compile the source file? A. Compilation takes no additional time. B. Compilation takes slightly more time. C. Compilation takes significantly more time.

A, B

11. Suppose class Supe, in package packagea, has a method called doSomething(). Suppose class Subby, in package packageb, overrides doSomething(). What access modes may Subby's version of the method have? (Choose 2) A. public B. protected C. Default D. private

public protected

11. Suppose class Supe, in package packagea, has a method called doSomething(). Suppose class Subby, in package packageb, overrides doSomething(). What access modes may Subby's version of the method have? (Choose all that apply.) A. public B. protected C. Default D. private

B

11. Suppose ob1 and ob2 are references to instances of java.lang.Object. If (ob1 == ob2) is false, can ob1.equals(ob2) ever be true? A. Yes B. No

D

11. Suppose you are writing a class that will provide custom serialization. The class implements java.io.Serializable (not java.io.Externalizable). What access mode should the writeObject() method have? A. public B. protected C. default D. private

private

11. Suppose you are writing a class that will provide custom serialization. The class implements java.io.Serializable (not java.io.Externalizable). What access mode should the writeObject() method have? A. public B. protected C. default D. private

A, C

11. Which lines check that x is equal to four? Assume assertions are enabled at compile time and runtime. A. assert x == 4; B. assert x != 4; C. assert x == 4 : "x is not 4"; D. assert x != 4 : "x is not 4";

assert x == 4; assert x == 4 : "x is not 4";

11. Which lines check that x is equal to four? Assume assertions are enabled at compile time and runtime. A. assert x == 4; B. assert x != 4; C. assert x == 4 : "x is not 4"; D. assert x != 4 : "x is not 4";

B, D

11. Which of the following are legal? (Choose all that apply.) A. List<String> theList = new Vector<String>; B. List<String> theList = new Vector<String>(); C. Vector <String> theVec = new Vector<String>; D. Vector <String> theVec = new Vector<String>();

List<String> theList = new Vector<String>(); Vector <String> theVec = new Vector<String>();

11. Which of the following are legal? (Choose all that apply.) A. List<String> theList = new Vector<String>; B. List<String> theList = new Vector<String>(); C. Vector <String> theVec = new Vector<String>; D. Vector <String> theVec = new Vector<String>();

B

11. Which of the following is the most appropriate way to handle invalid arguments in a public method? A. Throw java.lang.InvalidArgumentException. B. Throw java.lang.IllegalArgumentException. C. Check for argument validity in an assert statement, which throws AssertionError when the arguments are invalid. D. Use non-assert code to check for argument validity. If invalid arguments are detected, explicitly throw AssertionError.

All of the above

11. Which of the following may legally appear as the new type (between the parentheses) in a cast operation? A. Classes B. Interfaces C. Arrays of classes D. Arrays of interfaces E. All of the above

D

11. Which of the following may legally appear as the new type (between the parentheses) in a cast operation? A. Classes B. Interfaces C. Arrays of classes D. Arrays of interfaces E. All of the above

D

11. Which of the following may not be synchronized? A. Blocks within methods B. Static methods C. Blocks within static methods D. Classes

A, B

11. Which of the following may override a method whose signature is void xyz(float f)? A. void xyz(float f) B. public void xyz(float f) C. private void xyz(float f) D. public int xyz(float f) E. private int xyz(float f)

void xyz(float f) public void xyz(float f)

11. Which of the following may override a method whose signature is void xyz(float f)? A. void xyz(float f) B. public void xyz(float f) C. private void xyz(float f) D. public int xyz(float f) E. private int xyz(float f)

A

14. Which statement is true about the following method? int selfXor(int i) { return i ^ i; } A. It always returns 0. B. It always returns 1. C. It always an int where every bit is 1. D. The returned value varies depending on the argument.

D

16. Which of the following are true? A. The JVM runs until there is only one daemon thread. B. The JVM runs until there are no daemon threads. C. The JVM runs until there is only one non-daemon thread. D. The JVM runs until there are no non-daemon threads.

D

16. Which of the following operations might throw an ArithmeticException? A. + B. - C. * D. / E. None of these

Access the variables only via synchronized methods.

17. How do you prevent shared data from being corrupted in a multithreaded environment? A. Mark all variables as synchronized. B. Mark all variables as volatile. C. Use only static variables. D. Access the variables only via synchronized methods.

D

17. How do you prevent shared data from being corrupted in a multithreaded environment? A. Mark all variables as synchronized. B. Mark all variables as volatile. C. Use only static variables. D. Access the variables only via synchronized methods.

A, B, C, D

17. Suppose shorty is a short and wrapped is a Short. Which of the following are legal Java state- ments? (Choose all correct options.) A. shorty = wrapped; B. wrapped = shorty; C. shorty = new Short((short)9); D. shorty = 9;

B

17. True or false: If class Y extends class X, the two classes are in different packages, and class X has a protected method called abby(), then any instance of Y may call the abby() method of any other instance of Y. A. True B. False

False

17. True or false: If class Y extends class X, the two classes are in different packages, and class X has a protected method called abby(), then any instance of Y may call the abby() method of any other instance of Y. A. True B. False

D

17. What is the return type of the instanceof operator? A. A reference B. A class C. An int D. A boolean

All of the above

17. When a negative long is cast to a byte, what are the possible values of the result? A. Positive B. Zero C. Negative D. All of the above

D

17. When a negative long is cast to a byte, what are the possible values of the result? A. Positive B. Zero C. Negative D. All of the above

B

17. When is it appropriate to pass a cause to an exception's constructor? A. Always B. When the exception is being thrown in response to catching of a different exception type C. When the exception is being thrown from a public method D. When the exception is being thrown from a private method

When the exception is being thrown in response to catching of a different exception type

17. When is it appropriate to pass a cause to an exception's constructor? A. Always B. When the exception is being thrown in response to catching of a different exception type C. When the exception is being thrown from a public method D. When the exception is being thrown from a private method

C

17. Which line of code tells a scanner called sc to use a single digit as a delimiter? A. sc.useDelimiter("d"); B. sc.useDelimiter("\d"); C. sc.useDelimiter("\\d"); D. sc.useDelimiter("d+"); E. sc.useDelimiter("\d+"); F. sc.useDelimiter("\\d+");

sc.useDelimiter("\\d");

17. Which line of code tells a scanner called sc to use a single digit as a delimiter? A. sc.useDelimiter("d"); B. sc.useDelimiter("\d"); C. sc.useDelimiter("\\d"); D. sc.useDelimiter("d+"); E. sc.useDelimiter("\d+"); F. sc.useDelimiter("\\d+");

A, B

17. Which of the following are legal? (Choose 2) A. double d = 1.2d; B. double d = 1.2D; C. double d = 1.2d5; D. double d = 1.2D5;

A, C

17. Which of the following are true? A. An anonymous inner class may implement at most one interface. B. An anonymous inner class may implement arbitrarily many interfaces. C. An anonymous inner class may extend a parent class other than Object. D. An anonymous inner class that implements one interface may extend a parent class other than Object. E. An anonymous inner class that implements several interfaces may extend a parent class other than Object.

A, B, C, D

17. Which of the following are true? (Choose all that apply.) A. System.out has a println() method. B. System.out has a format() method. C. System.err has a println() method. D. System.err has a format () method.

public void logIt(String... msgs)

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 message 1 "); logIt("log message2","log message3"); logIt("log message4", "log message5", "log message6"); Which declaration satisfies this requirement? (Choose one.) a. public void logIt(String * msgs) b. public void logIt(String [] msgs) c. public void logIt(String... msgs) d. public void logIt(String msg1, String msg2, String msg3)

A

An abstract class can contain methods with declared bodies. A ⦁ True B ⦁ False

C

Assertions are used to enforce all but which of the following? A ⦁ Preconditions B ⦁ Postconditions C ⦁ Exceptions D ⦁ Class invariants

101

Consider the following application: 1. class Q6 { 2. public static void main(String args[]) { 3. Holder h = new Holder(); 4. h.held = 100; 5. h.bump(h); 6. System.out.println(h.held); 7. } 8. } 9. 10. class Holder { 11. public int held; 12. public void bump(Holder theHolder) { 13. theHolder.held++; 14 } 15. } 15. } What value is printed out at line 6? a. 0 b. 1 c. 100 d. 101

Comparable interface and its compareTo method.

In order for objects in a List to be sorted, those objects must implement which interface and method? (Choose one.) a. Comparable interface and its compareTo method. b. Comparable interface and its compare method c. Compare interface and its compareTo method d. Comparable interface and its equals method

Class loading takes no additional time.

Suppose a source file contains a large number of import statements and one class definition. How do the imports affect the time required to load the class? (Choose one.) a. Class loading takes no additional time. b. Class loading takes slightly more time. c. Class loading takes significantly more time.

private

Suppose class A has a method called doSomething(), with default access. Suppose class B extends A and overrides doSomething(). Which access modes may not apply to B's version of doSomething()? (Choose one) a. public b. private c. protected d. Default

All the above

Suppose prim is an int and wrapped is an Integer. Which of the following are legal Java statements? (Choose one.) a. prim = wrapped; b. wrapped = prim; c. prim = new Integer(9); d. wrapped = 9; e. All the above

When the type of x is Object

Suppose the declared type of x is a class, and the declared type of y is an interface. When is the assignment x = y; legal? (Choose one.) a. When the type of x is Object b. When the type of x is an array c. Always d. Never

Sometimes

Suppose the type of xarr is an array of XXX, and the type of yarr is an array of YYY. When is the assignment xarr = yarr; legal? (Choose one.) a. Sometimes b. Always c. Never d. None of the others choices

private

Suppose you are writing a class that will provide custom deserialization. The class implements java.io.Serializable (not java.io.Externalizable). What access mode should the readObject() method have? (Choose one.) a. public b. protected c. default d. private

private

Suppose you are writing a class that will provide custom serialization. The class implements java.io.Serializable (not java.io.Externalizable). What access mode should the writeObject() method have? (Choose one.) a. public b. protected c. default d. private

Override run().

Suppose you want to create a custom thread class by extending java.lang.Thread in order to provide some special functionality. Which of the following must you do? (Choose one.) a. Declare that your class implements java.lang.Runnable. b. Override run(). c. Override start(). d. Make sure that all access to all data is via synchronized methods.

A

QN=104 (4925) public class Test{ public static void main(String[] args){ String s1 = "xyz"; String s2 = "xyz"; if (s1 == s2) System.out.println("Line 4"); if (s1.equals(s2)) System.out.println("Line 6"); } } What is the output? a. Line 4 Line 6 b. Line 4 c. Line 6 d. No output, compile error e. No output

C

QN=105 (219) public class Test{ public static void main(String[] args){ String s1 = "xyz"; String s2 = new String("xyz"); if (s1 == s2) System.out.println("Line 4"); if (s1.equals(s2)) System.out.println("Line 6"); } } What is the output? a. Line 4 Line 6 b. Line 4 c. Line 6 d. No output, compile error e. No output

E

QN=129 (222) Suppose prim is an int and wrapped is an Integer. Which of the following are legal Java statements? (Choose one.) a. prim = wrapped; b. wrapped = prim; c. prim = new Integer(9); d. wrapped = 9; e. All the above

B

QN=140 The class is used to implement a pull-down menu that provides a number of items to select from. a. MenuBar b. Menu c. MenuItem d. PopUp

B

QN=141 (1876) The element method alters the contents of a Queue. a. True b. False

A

QN=143 There are two classes in Java to enable communication using datagrams (310) namely. a. DataPacket and DataSocket b. DatagramPacket and DatagramSocket c. DatagramPack and DatagramSock

B

QN=149 (75) What does the following code do? Integer i = null; if (i != null & i.intValue() == 5) System.out.println("Value is 5"); a. Prints "Value is 5". b. Throws an exception. c. Compile error

A, C

QN=15 (239) A file is created with the following code: 1. FileOutputStream fos = new FileOutputStream("datafile"); 2. DataOutputStream dos = new DataOutputStream(fos); 3. for (int i=0; i<500; i++) 4. dos.writeInt(i); You would like to write code to read back the data from this file. Which solutions will work? (Choose two.) a. Construct a FileInputStream, passing the name of the file. Onto the FileInputStream, chain a DataInputStream, and call its readInt() method. b. Construct a FileReader, passing the name of the file. Call the file reader's readInt() method. c. Construct a RandomAccessFile, passing the name of the file. Call the random access file's readInt() method. d. Construct a FileReader, passing the name of the file. Onto the FileReader, chain a DataInputStream, and call its readInt() method.

DriverManager

The class is the primary class that has the driver information. a. DriverManager b. Driver c. ODBCDriver d. None of the others

False

The element method alters the contents of a Queue. a. True b. False

A

QN=179 (5694) When you compile a program written in the Java programming language, the compiler converts the human-readable source file into platform- independent code that a Java Virtual Machine can understand. What is this platform-independent code called? a. bytecode b. binary code c. machine code d. cpu instruction

C

QN=18 (1426) 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 message 1 "); logIt("log message2","log message3"); logIt("log message4", "log message5", "log message6"); Which declaration satisfies this requirement? (Choose one.) a. public void logIt(String * msgs) b. public void logIt(String [] msgs) c. public void logIt(String... msgs) d. public void logIt(String msg1, String msg2, String msg3)

A

QN=180 (313) Whenever a method does not want to handle exceptions using the try block, the is used. a. throws b. throw c. throwable d. nothrows

throws

QN=180 (313) Whenever a method does not want to handle exceptions using the try block, the is used. a. throws b. throw c. throwable d. nothrows

B, C

QN=225 (85) Which of the following may appear on the right-hand side of an instanceof operator? (Choose two.) a. A reference b. A class c. An interface d. A variable of primitive type e. The name of a primitive type

A, C

QN=226 (120) Which of the following may be declared final? (Choose two.) a. Classes b. Interfaces c. Methods

B, C

QN=227 (58) Which of the following may be statically imported? (Choose two.) a. Package names b. Static method names c. Static field names d. Method-local variable names

B, C, D

QN=228 (121) Which of the following may follow the static keyword? (Choose three.) a. Class definitions b. Data c. Methods d. Code blocks enclosed in curly brackets e. Variables

E

QN=229 (137) Which of the following may legally appear as the new type (between the parentheses) in a cast operation? (Choose one.) a. Classes b. Interfaces c. Arrays of classes d. Arrays of interfaces e. All of the others

C

QN=23 (7263) A(n) object is uses to obtain a Connection to a Database a. ConnectionManager b. DatabaseManager c. DriverManager d. JDBCManager

D

QN=230 (138) Which of the following may legally appear as the new type (between the parentheses) in a cast operation? (Choose one.) a. Abstract classes b. Final classes c. Primitives d. All of the above

D

QN=231 (202) Which of the following may not be synchronized? (Choose one.) a. Blocks within methods b. Static methods c. Blocks within static methods d. Classes

A, B

QN=232 (186) Which of the following may override a method whose signature is void xyz(float f)? (Choose two.) a. void xyz(float f) b. public void xyz(float f) c. private void xyz(float f) d. public int xyz(float f) e. private int xyz(float f)

A

QN=233 (199) Which of the following methods in the Thread class are deprecated? (Choose one.) a. suspend() and resume() b. wait() and notify() c. start() and stop() d. sleep() and yield()

D

QN=234 (81) Which of the following operations might throw an ArithmeticException? (Choose one.) a. >> b. >>> c. << d. None of these

D

QN=235 (82) Which of the following operations might throw an ArithmeticException? (Choose one.) a. + b. - c. * d. / e. None of the others

A, B, E

QN=236 (145) Which of the following operators can perform promotion on their operands? (Choose three.) a. + b. - c. ++ d. - - e. ~ f. !

A, D

QN=245 (232) Which of the following statements are true? (Choose two.) a. StringBuilder is generally faster than StringBuffer. b. StringBuffer is generally faster than StringBuilder. c. StringBuilder is threadsafe; StringBuffer is not. d. StringBuffer is threadsafe; StringBuilder is not.

B

QN=246 (108) Which of the following statements are true? 1)An abstract class may not have any final methods. 2)A final class may not have any abstract methods. a. Only statement 1 b. Only statement 2 c. Both statement 1 and 2

F

QN=253 (128) Which one line in the following code will not compile? 1. byte b = 5; 2. char c = '5'; 3. short s = 55; 4. int i = 555; 5. float f = 555.5f; 6. b = s; 7. i = c; 8. if (f > b) 9. f = i; a. Line 5 b. Line 2 c. Line 3 d. Line 7 e. Line 9 f. Line 6

D

QN=254 (193) Which one statement is always true about the following application? 1. class HiPri extends Thread { 2. HiPri() { 3. setPriority(10); 4. } 5. 6. public void run() { 7. System.out.println( 8. "Another thread starting up."); 9. while (true) { } 10. } 11. 12. public static void main(String args[]) { 13. HiPri hp1 = new HiPri(); 14. HiPri hp2 = new HiPri(); 15. HiPri hp3 = new HiPri(); 16. hp1.start(); 17. hp2.start(); 18. hp3.start(); 19. } 20. } a. When the application is run, thread hp1 will execute; threads hp2 and hp3 will never get the CPU. b. When the application is run, thread hp1 will execute to completion, thread hp2 will execute to completion, then thread hp3 will execute to completion. c. When the application is run, all three threads (hp1, hp2, and hp3) will execute concurrently, taking time-sliced turns in the CPU. d. None of the above scenarios can be guaranteed to happen in all cases.

D

QN=66 (1532) Given: 11. String test = "This is a test"; 12. String[] tokens = test.split("\s"); 13. System.out.println(tokens.length); What is the result? (Choose one.) a. 0 b. 1 c. 4 d. Compilation fails. e. An exception is thrown at runtime.

A

QN=93 (218) In the following code fragment, after execution of line 1, sbuf references an instance of the StringBuffer class. After execution of line 2, sbuf still references the same instance. 1. StringBuffer sbuf = new StringBuffer("FPT"); 2. sbuf.append("-University"); a. True b. False

D

QN=96 (7266) Interface helps manage the connection between a Java program and a database. a. ResultSet b. DriverManager c. Statement d. Connection

A

QN=97 (76) Is it possible to define a class called Thing so that the following method can return true under certain circumstances? boolean weird(Thing s) { Integer x = new Integer(5); return s.equals(x); } a. Yes b. No

Yes

QN=97 (76) Is it possible to define a class called Thing so that the following method can return true under certain circumstances? boolean weird(Thing s) { Integer x = new Integer(5); return s.equals(x); } a. Yes b. No

A

QN=98 (205) Is it possible to write code that can execute only if the current thread owns multiple locks? a. Yes b. No

Yes

QN=98 (205) Is it possible to write code that can execute only if the current thread owns multiple locks? a. Yes b. No

B

QN=99 (301) JDBC supports and models. a. Single-tier and two-tier b. Two-tier and three-tier c. Three-tier and four-tier d. None of the others

Throws an exception.

What does the following code do? Integer i = null; if (i != null & i.intValue() == 5) System.out.println("Value is 5"); a. Prints "Value is 5". b. Throws an exception. c. Compile error

−2^7 through 2^7 − 1

What is the range of values that can be assigned to a variable of type byte? (Choose one.) a. Depends on the underlying hardware b. 0 through 2^8 − 1 c. 0 through 2^16 − 1 d. −2^7 through 2^7 − 1 e. −2^15 through 2^15 − 1

−2^15 through 2^15 − 1

What is the range of values that can be assigned to a variable of type short? (Choose one.) a. Depends on the underlying hardware b. 0 through 2^16 − 1 c. 0 through 2^32 − 1 d. −2^15 through 2^15 − 1 e. −2^31 through 2^31 − 1

A

What is the value of x after the following line is executed? x = 32 * (31 - 10 * 3); A. 32 B. 31 C. 3 D. 704 E. None of the above

D

What is the value of x after the following operation is performed? x = 23 % 4; A. 23 B. 4 C. 5.3 D. 3 E. 5

D

What keyword is used to prevent an object from being serialized? A ⦁ private B ⦁ volatile C ⦁ protected D ⦁ transient E ⦁ None of the above

B

What method call is used to tell a thread that it has the opportunity to run? A ⦁ wait() B ⦁ notify() C ⦁ start() D ⦁ run()

"is a"

What relationship does the extends keyword represent? A. "is a" B. "has a" C. Polymorphism D. Multivariance E. Overloading

int

When a byte is added to a char, what is the type of the result? a. byte b. char c. int d. short e. You can't add a byte to a char.

A, B, D

Which of the following are valid declarations? Assume java.util.* is imported. (choose 3) A. Vector<Map> v; B. Set<String> s; C. Map<String> m; D. Map<String, String> m;

All the above

Which of the following are valid mode strings for the RandomAccessFile constructor? (Choose one.) a. "r" b. "rw" c. "rws" d. "rwd" e. All the above

A

You can determine all the keys in a Map in which of the following ways? A⦁ By getting a Set object from the Map and iterating through it. B⦁ By iterating through the Iterator of the Map. C⦁ By enumerating through the Enumeration of the Map. D⦁ By getting a List from the Map and enumerating through the List. E⦁ You cannot determine the keys in a Map

1. Which of these is not a bitwise operator? a) & b) &= c) |= d) <= [expand title="View Answer"] Answer:d Explanation: <= is a relational operator. [/expand] 2. Which operator is used to invert all the digits in binary representation of a number? a) ~ b) <<< c) >>> d) ^

a

10. What is the output of this program? class Output { public static void main(String args[]) { int a = 1; int b = 2; int c = 3; a |= 4; b >>= 1; c <<= 1; a ^= c; System.out.println(a + " " + b + " " + c); } } a) 3 1 6 b) 2 2 3 c) 2 3 4 d) 3 3 6

a

10. What is the output of this program? class area { public static void main(String args[]) { double r, pi, a; r = 9.8; pi = 3.14; a = pi * r * r; System.out.println(a); } } a) 301.5656 b) 301 c) 301.56 d) 301.56560000

a

10. What is the output of this program? class dynamic_initialization { public static void main(String args[]) { double a, b; a = 3.0; b = 4.0; double c = Math.sqrt(a * a + b * b); System.out.println(c); } } a) 5.0 b) 25.0 c) 7.0 d) Compilation Error

a

18. Which of the following may appear on the left-hand side of an instanceof operator? A. A reference B. A class C. An interface D. A variable of primitive type

a

2. What is the range of data type byte in Java? a) -128 to 127 b) -32768 to 32767 c) -2147483648 to 2147483647 d) None of the mentioned

a

2. Which of these are selection statements in Java? a) if() b) for() c) continue d) break

a

3. Which of the following loops will execute the body of loop even when condition controlling the loop is initially false? a) do-while b) while c) for d) None of the mentioned

a

3. Which of these values can a boolean variable contain? a) True & False b) 0 & 1 c) Any integer value d) true

a

A

The keyword extends refers to what type of relationship? A ⦁ "is a" B ⦁ "has a" C ⦁ "was a" D ⦁ "will be a" E ⦁ None of the above

4. An expression involving byte, int, and literal numbers is promoted to which of these? a) int b) long c) byte d) float

a

4. Decrement operator, -, decreases value of variable by what number? a) 1 b) 2 c) 3 d) 4

a

4. What is the order of precedence (highest to lowest) of following operators? 1. & 2. ^ 3. ?: a) 1 -> 2 -> 3 b) 2 -> 1 -> 3 c) 3 -> 2 -> 1 d) 2 -> 3 -> 1

a

5. What is Truncation is Java? a) Floating-point value assigned to an integer type. b) Integer value assigned to floating type. c) Floating-point value assigned to an Floating type. d) Integer value assigned to floating type.

a

6. What is the output of this program? class array_output { public static void main(String args[]) { char array_variable [] = new char[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = 'i'; System.out.print(array_variable[i] + "" ); i++; } } } a) i i i i i b) 0 1 2 3 4 c) i j k l m d) None of the mentioned

a

6. What is the output of this program? class char_increment { public static void main(String args[]) { char c1 = 'D'; char c2 = 84; c2++; c1++; System.out.println(c1 + " " + c2); } } a) E U b) U E c) V E d) U F

a

7. What is the output of this program? class bitwise_operator { public static void main(String args[]) { int a = 3; int b = 6; int c = a | b; int d = a & b; System.out.println(c + " " + d); } } a) 7 2 b) 7 7 c) 7 5 d) 5 2

a

7. What is the output of this program? class mainclass { public static void main(String args[]) { char a = 'A'; a++; System.out.print((int)a); } } a) 66 b) 67 c) 65 d) 64

a

1. What is the output of relational operators? a) Integer b) Boolean c) Characters d) Double

b

1. What is the range of data type short in Java? a) -128 to 127 b) -32768 to 32767 c) -2147483648 to 2147483647 d) None of the mentioned

b

1. Which of these is data type long literal? a) 0x99fffL b) ABCDEFG c) 0x99fffa d) 99671246

b

1. Which of these is necessary condition for automatic type conversion in Java? a) The destination type is smaller than source type. b) The destination type is larger than source type. c) The destination type can be larger or smaller than source type. d) None of the mentioned

b

1. Which of these selection statements test only for equality? a) if b) switch c) if & switch d) None of the mentioned

b

10. What is the output of this program? class Output { public static void main(String args[]) { int x , y = 1; x = 10; if (x != 10 && x / 0 == 0) System.out.println(y); else System.out.println(++y); } } a) 1 b) 2 c) Runtime error owing to division by zero in if condition. d) Unpredictable behavior of program.

b

10. What is the output of this program? class asciicodes { public static void main(String args[]) { char var1 = 'A'; char var2 = 'a'; System.out.println((int)var1 + " " + (int)var2); } } a) 162 b) 65 97 c) 67 95 d) 66 98

b

11. Suppose ob1 and ob2 are references to instances of java.lang.Object. If (ob1 == ob2) is false, can ob1.equals(ob2) ever be true? A. Yes B. No

b

3. What is the error in this code? byte b = 50; b = b * 50; a) b can not contain value 100, limited by its range. b) * operator has converted b * 50 into int, which can not be converted to byte without casting. c) b can not contain value 50. d) No error in this code

b

What is the output of this program? class rightshift_operator { public static void main(String args[]) { int x; x = 10; x = x >> 1; System.out.println(x); } } a) 10 b) 5 c) 2 d) 20

b

10. What is the output of this program? class Output { public static void main(String args[]) { boolean a = true; boolean b = false; boolean c = a ^ b; System.out.println(!c); } } a) 0 b) 1 c) false d) true

c

18. Which of the following are legal? A. char c = 0x1234; B. char c = \u1234; C. char c = '\u1234';

c

19. Consider the following code: 1. StringBuffer sbuf = new StringBuffer(); 2. sbuf = null; 3. System.gc(); Choose all true statements: A. After line 2 executes, the StringBuffer object is garbage collected. B. After line 3 executes, the StringBuffer object is garbage collected. C. After line 2 executes, the StringBuffer object is eligible for garbage collection. D. After line 3 executes, the StringBuffer object is eligible for garbage collection.

c

2. Modulus operator, %, can be applied to which of these? a) Integers b) Floating - point numbers c) Both Integers and floating - point numbers. d) None of the mentioned

c

2. What should be expression1 evaluate to in using ternary operator as in this line? expression1 ? expression2 : expression3 a) Integer b) Floating - point numbers c) Boolean d) None of the mentioned

c

2. Which of these coding types is used for data type characters in Java? a) ASCII b) ISO-LATIN-1 c) UNICODE d) None of the mentioned

c

3. Which of the following expressions results in a positive value in x? A. int x = -1; x = x >>> 5; B. int x = -1; x = x >>> 32; C. byte x = -1; x = x >>> 5; D. int x = -1; x = x >> 5;

c

3. With x = 0, which of the following are legal lines of Java code for changing the value of x to 1? 1. x++; 2. x = x + 1; 3. x += 1; 4. x =+ 1; a) 1, 2 & 3 b) 1 & 4 c) 1, 2, 3 & 4 d) 3 & 2

c

4. If an expression contains double, int, float, long, then whole expression will promoted into which of these data types? a) long b) int c) double d) float

c

What is the output of this program? class mainclass { public static void main(String args[]) { boolean var1 = true; boolean var2 = false; if (var1) System.out.println(var1); else System.out.println(var2); } } a) 0 b) 1 c) true d) false

c

accept()

method is used to wait for a client to initiate communications. a. wait() b. accept() c. listen()

4. Which of the following statements is true? A. Transient methods may not be overridden. B. Transient methods must be overridden. C. Transient classes may not be serialized. D. Transient variables must be static. E. Transient variables are not serialized.

e

B

1. A signed data type has an equal number of non-zero positive and negative values available. A. True B. False

C

1. After execution of the following code fragment, what are the values of the variables x, a, and b? 1. int x, a = 6, b = 7; 2. x = a++ + b++; A. x = 15, a = 7, b = 8 B. x = 15, a = 6, b = 7 C. x = 13, a = 7, b = 8 D. x = 13, a = 6, b = 7

B, C, F, D

1. Consider the following code: 1. for (int i = 0; i < 2; i++) { 2. for (int j = 0; j < 3; j++) { 3. if (i == j) { 4. continue; 5. } 6. System.out.println("i = " + i + " j = " + j); 7. } 8. } Which lines would be part of the output? (Choose all that apply.) A. i = 0 j = 0 B. i = 0 j = 1 C. i = 0 j = 2 D. i = 1 j = 0 E. i = 1 j = 1 F. i = 1 j = 2

A, C, E

1. Consider this class: 1. public class Test1 { 2. public float aMethod(float a, float b) { 3. } 4. 5. } Which of the following methods would be legal if added (individually) at line 4? (Choose all that apply.) A. public int aMethod(int a, int b) { } B. public float aMethod(float a, float b) { } C. public float aMethod(float a, float b, int c) throws Exception { } D. public float aMethod(float c, float d) { } E. private float aMethod(int a, int b, int c) { }

F

1. Given a string constructed by calling s = new String("xyzzy"), which of the calls modifies the string? A. s.append("aaa"); B. s.trim(); C. s.substring(3); D. s.replace('z', 'a'); E. s.concat(s); F. None of the above

B, D, E

1. Given the following: public enum Wallpaper { BROWN, BLUE, YELLOW; } Which of the following are legal? A. enum PatternedWallpaper extends Wallpaper { STRIPES, DOTS, PLAIN; } B. Wallpaper wp = Wallpaper.BLUE; C. Wallpaper wp = new Wallpaper(Wallpaper.BLUE); D. void aMethod(Wallpaper wp) { System.out.println(wp); } E. int hcode = Wallpaper.BLUE.hashCode();

A, D, E

1. Which of the following declarations are illegal? (Choose 3) A. default String s; B. transient int i = 41; C. public final static native int w(); D. abstract double d; E. abstract final double hyperbolicCosine();

Both primitives and object references can be both converted and cast.

1. Which of the following statements is correct? (Choose one.) A. Only primitives are converted automatically; to change the type of an object reference, you have to do a cast. B. Only object references are converted automatically; to change the type of a primitive, you have to do a cast. C. Arithmetic promotion of object references requires explicit casting. D. Both primitives and object references can be both converted and cast. E. Casting of numeric types may require a runtime check.

D

1. Which of the following statements is correct? (Choose one.) A. Only primitives are converted automatically; to change the type of an object reference, you have to do a cast. B. Only object references are converted automatically; to change the type of a primitive, you have to do a cast. C. Arithmetic promotion of object references requires explicit casting. D. Both primitives and object references can be both converted and cast. E. Casting of numeric types may require a runtime check.

A

1. Which one statement is true concerning the following code? 1. class Greebo extends java.util.Vector 2. implements Runnable { 3. public void run(String message) { 4. System.out.println("in run() method: " + 5. message); 6. } 7. } 8. 9. class GreeboTest { 10. public static void main(String args[]) { 12. Greebo g = new Greebo(); 13. Thread t = new Thread(g); 14. t.start(); 15. } 16. } A. There will be a compiler error, because class Greebo does not correctly implement the Runnable interface. B. There will be a compiler error at line 13, because you cannot pass a parameter to the constructor of a Thread. C. The code will compile correctly but will crash with an exception at line 13. D. The code will compile correctly but will crash with an exception at line 14. E. The code will compile correctly and will execute without throwing any exceptions.

There will be a compiler error, because class Greebo does not correctly implement the Runnable interface.

1. Which one statement is true concerning the following code? 1. class Greebo extends java.util.Vector 2. implements Runnable { 3. public void run(String message) { 4. System.out.println("in run() method: " + 5. message); 6. } 7. } 8. 9. class GreeboTest { 10. public static void main(String args[]) { 12. Greebo g = new Greebo(); 13. Thread t = new Thread(g); 14. t.start(); 15. } 16. } A. There will be a compiler error, because class Greebo does not correctly implement the Runnable interface. B. There will be a compiler error at line 13, because you cannot pass a parameter to the constructor of a Thread. C. The code will compile correctly but will crash with an exception at line 13. D. The code will compile correctly but will crash with an exception at line 14. E. The code will compile correctly and will execute without throwing any exceptions.

Line 26 prints "a" to System.out.

1. public class A { 2. public String doit(int x, int y) { 3. return "a"; 4. } 5. 6. public String doit(int... vals) { 7. return "b"; 8. } 9. } Given: 25. A a=new A(); 26. System.out.println(a.doit(4, 5)); What is the result? (Choose one.) a. Line 26 prints "a" to System.out. b. Line 26 prints "b" to System.out. c. An exception is thrown at line 26 at runtime. d. Compilation of class A will fail due to an error in line 6.

The code on line 29 will be executed. The exception will be propagated back to line 27.

1. 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.} 25. try { 26. A a=new A(); 27. a.method1(); 28. }catch (Exception e) { 29. System.out.print("an error occurred"); 30. } Which two are true if a NullPointerException is thrown on line 3 of class C? (Choose two.) 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.

B

10. Consider the following code: 1. Raccoon rocky; 2. SwampThing pogo; 3. Washer w; 4. 5. rocky = new Raccoon(); 6. w = rocky; 7. pogo = w; Which of the following statements is true? (Choose one.) A. Line 6 will not compile; an explicit cast is required to convert a Raccoon to a Washer. B. Line 7 will not compile; an explicit cast is required to convert a Washer to a SwampThing. C. The code will compile and run. D. The code will compile but will throw an exception at line 7, because runtime conversion from an interface to a class is not permitted. E. The code will compile but will throw an exception at line 7, because the runtime class of w cannot be converted to type SwampThing.

Line 7 will not compile; an explicit cast is required to convert a Washer to a SwampThing.

10. Consider the following code: 1. Raccoon rocky; 2. SwampThing pogo; 3. Washer w; 4. 5. rocky = new Raccoon(); 6. w = rocky; 7. pogo = w; Which of the following statements is true? (Choose one.) A. Line 6 will not compile; an explicit cast is required to convert a Raccoon to a Washer. B. Line 7 will not compile; an explicit cast is required to convert a Washer to a SwampThing. C. The code will compile and run. D. The code will compile but will throw an exception at line 7, because runtime conversion from an interface to a class is not permitted. E. The code will compile but will throw an exception at line 7, because the runtime class of w cannot be converted to type SwampThing.

A, B, D

10. Consider the following code: 1. public class Assertification { 2. public static void main(String[] args) { 3. assert args.length == 0; 4 } 5. } Which of the following conditions must be true in order for the code to throw an AssertionError? Assume you are using release 5.0. (Choose all that apply.) A. The source code must be compiled with the -source 1.5 flag. B. The application must be run with the -enableassertions flag or another assertion- enabling flag. C. The args array must have exactly zero elements. D. The args array must have one or more elements.

A, C

10. Given the following: int[] ages = { 9, 41, 49 }; int sum = 0; Which of the following are legal ways to add the elements of the array? A. for (int i=0; i<ages.length; i++) sum += ages[i]; B. for (int i=0; i<=ages.length; i++) sum += ages[i]; C. for (int i:ages) sum += i; D. sum += ages[int i:ages];

for (int i=0; i<ages.length; i++) sum += ages[i]; for (int i:ages) sum += i;

10. Given the following: int[] ages = { 9, 41, 49 }; int sum = 0; Which of the following are legal ways to add the elements of the array? A. for (int i=0; i<ages.length; i++) sum += ages[i]; B. for (int i=0; i<=ages.length; i++) sum += ages[i]; C. for (int i:ages) sum += i; D. sum += ages[int i:ages];

A

10. Is it possible to define a class called Thing so that the following method can return true under certain circumstances? boolean weird(Thing s) { Integer x = new Integer(5); return s.equals(x); } A. Yes B. No

D

10. Suppose prim is an int and wrapped is an Integer. Which of the following are legal Java statements? (Choose all that apply.) A. prim = wrapped; B. wrapped = prim; C. prim = new Integer(9); D. wrapped = 9;

The program will compile and execute. The output will be Before: 0 After: 2.

10. This question concerns the following class definition: 1. package abcde; 2. 3. public class Bird { 4. protected static int referenceCount = 0; 5. public Bird() { referenceCount++; } 6. protected void fly() { /* Flap wings, etc. */ } 7. static int getRefCount() { return referenceCount; } 8. } Which statement is true about class Bird and the following class Nightingale? 1. package singers; 2. 3. class Nightingale extends abcde.Bird { 4. Nightingale() { referenceCount++; } 5. 6. public static void main(String args[]) { 7. System.out.print("Before: " + referenceCount); 8. Nightingale florence = new Nightingale(); 9. System.out.println(" After: " + referenceCount); 10. florence.fly(); 11. } 12. } A. The program will compile and execute. The output will be Before: 0 After: 2. B. The program will compile and execute. The output will be Before: 0 After: 1. C. Compilation of Nightingale will fail at line 4 because static members cannot be overridden. D. Compilation of Nightingale will fail at line 10 because method fly() is protected in the superclass. E. Compilation of Nightingale will succeed, but an exception will be thrown at line 10, because method fly() is protected in the superclass.

Classes Data Methods

14. Which of the following may be declared final? (Choose all that apply.) A. Classes B. Data C. Methods

The code fails to compile.

10. What is the result of attempting to compile and execute the following code fragment? Assume that the code fragment is part of an application that has write permission in the current working directory. Also assume that before execution, the current working directory does not contain a file called datafile. 1. try { 2. RandomAccessFile raf = new 3. RandomAccessFile("datafile" ,"rw"); 4. BufferedOutputStream bos = new 5. BufferedOutputStream(raf); 6. DataOutputStream dos = new 7. DataOutputStream(bos); 8. dos.writeDouble(Math.PI); 9. dos.close(); 10. bos.close(); 11. raf.close(); 12. } 13. catch (IOException e) { } A. The code fails to compile. B. The code compiles but throws an exception at line 4. C. The code compiles and executes but has no effect on the local file system. D. The code compiles and executes; afterward, the current working directory contains a file called datafile.

C, E

10. Which of the following are legal enums? A. enum Animals { LION, TIGER, BEAR } B. enum Animals { int age; LION, TIGER, BEAR; } C. enum Animals { LION, TIGER, BEAR; int weight; } D. enum Animals { LION(450), TIGER(450), BEAR; int weight; Animals(int w) { weight = w; } } E. enum Animals { LION(450), TIGER(450), BEAR; int weight; Animals() { } Animals(int w) { weight = w; } }

C

10. Which of the following statements about the wait() and notify() methods is true? A. The wait() and notify() methods can be called outside synchronized code. B. The programmer can specify which thread should be notified in a notify() method call. C. The thread that calls wait() goes into the monitor's pool of waiting threads. D. The thread that calls notify() gives up the lock.

The thread that calls wait() goes into the monitor's pool of waiting threads.

10. Which of the following statements about the wait() and notify() methods is true? A. The wait() and notify() methods can be called outside synchronized code. B. The programmer can specify which thread should be notified in a notify() method call. C. The thread that calls wait() goes into the monitor's pool of waiting threads. D. The thread that calls notify() gives up the lock.

An exception is thrown at runtime.

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. 23. public String getValue() { 24. return classC.getValue(); 25. } 26.} And: 30. class ClassC { 31. public String value; 32. 33. public String getValue() { 34. value = "ClassB"; 35. return value; 36. } 37.} ClassA a = new ClassA(); a.methodA(); What is the result? (Choose one.) a. Compilation fails. b. ClassC is displayed. c. The code runs with no output. d. An exception is thrown at runtime.

third second first snootchy 420

11. public class Bootchy { 12. int bootch; 13. String snootch; 14. 15. public Bootchy() { 16. this("snootchy"); 17. System.out.print("first "); 18. } 19. 20. public Bootchy(String snootch) { 21. this(420, "snootchy"); 22. System.out.print("second "); 23. } 24. 25. public Bootchy(int bootch, String snootch) { 26. this.bootch = bootch; 27. this.snootch = snootch; 28. System.out.print("third "); 29. } 30. 31. public static void main(String[] args) { 32. Bootchy b = new Bootchy(); 33. System.out.print(b.snootch +" " + b.bootch); 34. } 35. } What is the result? (Choose one.) a. snootchy 420 third second first b. snootchy 420 first second third c. first second third snootchy 420 d. third second first snootchy 420 e. third first second snootchy 420 f. first second first third snootchy 420

Compilation fails.

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? (Choose one.) 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.

A, B

12. Given the following, Map<String> names = new HashMap<String>(); which of the following are legal? (Choose all that apply.) A. Iterator<String> iter = names.iterator(); B. for (String s:names) C. while (String s:names)

A

12. Suppose a source file contains a large number of import statements and one class definition. How do the imports affect the time required to load the class? A. Class loading takes no additional time. B. Class loading takes slightly more time. C. Class loading takes significantly more time.

A

12. Suppose salaries is an array containing floats. Which of the following are valid loop control statements for processing each element of salaries? A. for (float f:salaries) B. for (int i:salaries) C. for (float f::salaries) D. for (int i::salaries)

D

12. Suppose you are writing a class that will provide custom deserialization. The class implements java.io.Serializable (not java.io.Externalizable). What access mode should the readObject() method have? A. public B. protected C. default D. private

private

12. Suppose you are writing a class that will provide custom deserialization. The class implements java.io.Serializable (not java.io.Externalizable). What access mode should the readObject() method have? A. public B. protected C. default D. private

C

12. When a byte is added to a char, what is the type of the result? A. byte B. char C. int D. short E. You can't add a byte to a char.

A, D

12. Which are appropriate uses of assertions? A. Checking preconditions in a private method B. Checking postconditions in a private method C. Checking preconditions in a public method D. Checking postconditions in a public method

C, D

12. Which of the following are true? (Choose all that apply.) A. An enum definition should declare that it extends java.lang.Enum. B. An enum may be subclassed. C. An enum may contain public method definitions. D. An enum may contain private data.

A, B, C, D

12. Which of the following calls may be made from a non-static synchronized method? A. A call to the same method of the current object. B. A call to the same method of a different instance of the current class. C. A call to a different synchronized method of the current object. D. A call to a static synchronized method of the current class.

All of the above

12. Which of the following may legally appear as the new type (between the parentheses) in a cast operation? A. Abstract classes B. Final classes C. Primitives D. All of the above

D

12. Which of the following may legally appear as the new type (between the parentheses) in a cast operation? A. Abstract classes B. Final classes C. Primitives D. All of the above

F

12. Which of the following statements are true? A. An abstract class may be instantiated. B. An abstract class must contain at least one abstract method. C. An abstract class must contain at least one abstract data field. D. An abstract class must be overridden. E. An abstract class must declare that it implements an interface. F. None of the above.

None of the above.

12. Which of the following statements are true? A. An abstract class may be instantiated. B. An abstract class must contain at least one abstract method. C. An abstract class must contain at least one abstract data field. D. An abstract class must be overridden. E. An abstract class must declare that it implements an interface. F. None of the above.

A, C

13. EOFException and ObjectStreamException both extend IOException. NotSerializable- Exception extends ObjectStreamException. AWTException does not extend any of these. All are checked exceptions. Suppose class AClass has a method callMe() whose declaration is void callMe() throws ObjectStreamException Which of the following may appear in a subclass of AClass? A. void callMe() B. void callMe() throws IOException C. void callMe() throws NotSerializableException D. void callMe() throws ObjectStreamException, AWTException

A

13. How many locks does an object have? A. One B. One for each method C. One for each synchronized method D. One for each non-static synchronized method

One

13. How many locks does an object have? A. One B. One for each method C. One for each synchronized method D. One for each non-static synchronized method

B, C

14. Which of the following may be statically imported? (Choose 2) A. Package names B. Static method names C. Static field names D. Method-local variable names

B

13. Suppose class A extends Object; class B extends A; and class C extends B. Of these, only class C implements java.io.Serializable. Which of the following must be true in order to avoid an exception during deserialization of an instance of C? A. A must have a no-args constructor. B. B must have a no-args constructor. C. C must have a no-args constructor. D. There are no restrictions regarding no-args constructors.

B must have a no-args constructor.

13. Suppose class A extends Object; class B extends A; and class C extends B. Of these, only class C implements java.io.Serializable. Which of the following must be true in order to avoid an exception during deserialization of an instance of C? A. A must have a no-args constructor. B. B must have a no-args constructor. C. C must have a no-args constructor. D. There are no restrictions regarding no-args constructors.

The class will compile if it is declared abstract. The class may not be instantiated.

13. Suppose interface Inty defines five methods. Suppose class Classy declares that it implements Inty but does not provide implementations for any of the five interface methods. Which is/are true? A. The class will not compile. B. The class will compile if it is declared public. C. The class will compile if it is declared abstract. D. The class may not be instantiated.

C, D

13. Suppose interface Inty defines five methods. Suppose class Classy declares that it implements Inty but does not provide implementations for any of the five interface methods. Which is/are true? (Choose 2) A. The class will not compile. B. The class will compile if it is declared public. C. The class will compile if it is declared abstract. D. The class may not be instantiated.

A

13. Suppose the declared type of x is a class, and the declared type of y is an interface. When is the assignment x = y; legal? A. When the type of x is Object B. When the type of x is an array C. Always D. Never

C

13. When a short is added to a float, what is the type of the result? A. short B. int C. float D. You can't add a short to a float.

B, D

13. Which of the following are legal clone() methods in a class called Q13 that extends Object? A. public Object clone() { return super.clone(); } B. public Object clone() throws CloneNotSupportedException { return super.clone(); } C. public Q13 clone() { return (Q13)super.clone(); } D. public Q13 clone() throws CloneNotSupportedException { return (Q13)super.clone(); }

A, C

13. Which of the following are legal import statements? (choose 2) A. import java.util.Vector; B. static import java.util.Vector.*; C. import static java.util.Vector.*; D. import java.util.Vector static;

A, B, D

13. Which of the following are legal? (Choose all that apply.) A. for (int i=0, j=1; i<10; i++, j++) B. for (int i=0, j=1;; i++, j++) C. for (int i=0, float j=1; ; i++, j++) D. for (String s = ""; s.length()<10; s += '!')

A, B, C, D

13. Which of the following are true? (Choose all that apply.) A. An enum definition may contain the main() method of an application. B. You can call an enum's toString() method. C. You can call an enum's wait() method. D. You can call an enum's notify() method.

A

14. Is it possible to write code that can execute only if the current thread owns multiple locks? A. Yes. B. No.

Yes.

14. Is it possible to write code that can execute only if the current thread owns multiple locks? A. Yes. B. No.

B, E

14. ObjectStreamException extends IOException. NotSerializableException extends ObjectStreamException. AWTException does not extend any of these. All are checked exceptions. The callMe() method throws NotSerializableException.What does the following code print out? Choose all lines that are printed. try { callMe(); System.out.println("I threw"); } catch (ObjectStreamException x) { System.out.println("Object stream"); } catch (IOException x) { System.out.println("IO"); } catch (Exception x) { System.out.println("Exception"); } finally { System.out.println("Finally"); } A. I threw B. Object Stream C. IO D. Exception E. Finally

Object Stream Finally

14. ObjectStreamException extends IOException. NotSerializableException extends ObjectStreamException. AWTException does not extend any of these. All are checked exceptions. The callMe() method throws NotSerializableException.What does the following code print out? Choose all lines that are printed. try { callMe(); System.out.println("I threw"); } catch (ObjectStreamException x) { System.out.println("Object stream"); } catch (IOException x) { System.out.println("IO"); } catch (Exception x) { System.out.println("Exception"); } finally { System.out.println("Finally"); } A. I threw B. Object Stream C. IO D. Exception E. Finally

D

14. Suppose a method called finallyTest() consists of a try block, followed by a catch block, followed by a finally block. Assuming the JVM doesn't crash and the code does not execute a System.exit() call, under what circumstances will the finally block not begin to execute? A. The try block throws an exception, and the catch block also throws an exception. B. The try block throws an exception that is not handled by the catch block. C. The try block throws an exception, and the catch block calls finallyTest() in a way that causes another exception to be thrown. D. If the JVM doesn't crash and the code does not execute a System.exit() call, the finally block will always execute.

C

14. Suppose class A extends Object; Class B extends A; and class C extends B. Of these, only class C implements java.io.Externalizable. Which of the following must be true in order to avoid an exception during deserialization of an instance of C? A. A must have a no-args constructor. B. B must have a no-args constructor. C. C must have a no-args constructor. D. There are no restrictions regarding no-args constructors.

C must have a no-args constructor.

14. Suppose class A extends Object; Class B extends A; and class C extends B. Of these, only class C implements java.io.Externalizable. Which of the following must be true in order to avoid an exception during deserialization of an instance of C? A. A must have a no-args constructor. B. B must have a no-args constructor. C. C must have a no-args constructor. D. There are no restrictions regarding no-args constructors.

A

14. Suppose the type of xarr is an array of XXX, and the type of yarr is an array of YYY. When is the assignment xarr = yarr; legal? A. Sometimes B. Always C. Never

Sometimes

14. Suppose the type of xarr is an array of XXX, and the type of yarr is an array of YYY. When is the assignment xarr = yarr; legal? A. Sometimes B. Always C. Never

A

14. Suppose x and y are of type TrafficLightState, which is an enum. What is the best way to test whether x and y refer to the same constant? A. if (x == y) B. if (x.equals(y)) C. if (x.toString().equals(y.toString())) D. if (x.hashCode() == y.hashCode())

if (x == y)

14. Suppose x and y are of type TrafficLightState, which is an enum. What is the best way to test whether x and y refer to the same constant? A. if (x == y) B. if (x.equals(y)) C. if (x.toString().equals(y.toString())) D. if (x.hashCode() == y.hashCode())

A, D

14. Which of the following classes implement java.util.List? A. java.util.ArrayList B. java.util.HashList C. java.util.StackList D. java.util.Stack

java.util.ArrayList java.util.Stack

14. Which of the following classes implement java.util.List? A. java.util.ArrayList B. java.util.HashList C. java.util.StackList D. java.util.Stack

A, B, C

14. Which of the following may be declared final? (Choose all that apply.) A. Classes B. Data C. Methods

A, B, E

15. Given the following class: public class Xyz implements java.io.Serializable { public int iAmPublic; private int iAmPrivate; static int iAmStatic; transient int iAmTransient; volatile int iAmVolatile; . . . } Assuming the class does not perform custom serialization, which fields are written when an instance of Xyz is serialized? (Choose all that apply.) A. iAmPublic B. iAmPrivate C. iAmStatic D. iAmTransient E. iAmVolatile

C

15. What happens when you try to compile and run the following code? public class Q15 { static String s; public static void main(String[] args) { System.out.println(">>" + s + "<<"); } } A. The code does not compile B. The code compiles, and prints out >><< C. The code compiles, and prints out >>null<<

B

15. When is x & y an int? (Choose one). A. Always B. Sometimes C. When neither x nor y is a float, a long, or a double

Sometimes

15. When is x & y an int? (Choose one). A. Always B. Sometimes C. When neither x nor y is a float, a long, or a double

D

15. Which of the following are legal loop definitions? (Choose all that apply.) A. while (int a = 0) { /* whatever */ } B. while (int a == 0) { /* whatever */ } C. do { /* whatever */ } while (int a = 0) D. do { /* whatever */ } while (int a == 0) E. for (int a==0; a<100; a++) { /* whatever */ } F. None of them are legal.

A, B, C, D, E

15. Which of the following are methods of the java.util.SortedSet interface? A. first B. last C. headSet D. tailSet E. subSet

B, C, D

15. Which of the following are true? (Choose all that apply.) A. When an application begins running, there is one daemon thread, whose job is to execute main(). B. When an application begins running, there is one non-daemon thread, whose job is to execute main(). C. A thread created by a daemon thread is initially also a daemon thread. D. A thread created by a non-daemon thread is initially also a non-daemon thread.

B, C, D

15. Which of the following may follow the static keyword? (Choose all that apply.) A. Class definitions B. Data C. Methods D. Code blocks enclosed in curly brackets

Data Methods Code blocks enclosed in curly brackets

15. Which of the following may follow the static keyword? (Choose all that apply.) A. Class definitions B. Data C. Methods D. Code blocks enclosed in curly brackets

D

15. Which of the following operations might throw an ArithmeticException? A. >> B. >>> C. << D. None of these

A

15. Which of the following restrictions apply to anonymous inner classes? A. They must be defined inside a code block. B. They may only read and write final variables of the enclosing class. C. They may only call final methods of the enclosing class. D. They may not call the enclosing class' synchronized methods.

They must be defined inside a code block.

15. Which of the following restrictions apply to anonymous inner classes? A. They must be defined inside a code block. B. They may only read and write final variables of the enclosing class. C. They may only call final methods of the enclosing class. D. They may not call the enclosing class' synchronized methods.

D

15. While testing some code that you are developing, you notice that an ArrayIndexOutOf- BoundsException is thrown. What is the appropriate reaction? A. Enclose the offending code in a try block, with a catch block for ArrayIndexOutOfBoundsException that does nothing. B. Enclose the offending code in a try block, with a catch block for ArrayIndexOutOfBoundsException that prints out a descriptive message. C. Declare that the method that contains the offending code throws ArrayIndexOutOfBoundsException. D. None of the above.

None of the above.

15. While testing some code that you are developing, you notice that an ArrayIndexOutOf- BoundsException is thrown. What is the appropriate reaction? A. Enclose the offending code in a try block, with a catch block for ArrayIndexOutOfBoundsException that does nothing. B. Enclose the offending code in a try block, with a catch block for ArrayIndexOutOfBoundsException that prints out a descriptive message. C. Declare that the method that contains the offending code throws ArrayIndexOutOfBoundsException. D. None of the above.

A, B, D

16. Given the following code, which of the following will compile? enum Spice { NUTMEG, CINNAMON, CORIANDER, ROSEMARY; } A. Spice sp = Spice.NUTMEG; Object ob = sp; B. Spice sp = Spice.NUTMEG; Object ob = (Object)sp; C. Object ob = new Object(); Spice sp = object; D. Object ob = new Object(); Spice sp = (Spice)object;

C, D

16. How is IllegalArgumentException used? (Choose all correct options.) A. It is thrown by the JVM when a method is called with incompatible argument types. B. It is thrown by the JVM to indicate arithmetic overflow. C. It is thrown by certain methods of certain core Java classes to indicate that preconditions have been violated. D. It should be used by programmers to indicate that preconditions of public methods have been violated. E. It should be used by programmers to indicate that preconditions of nonpublic methods have been violated.

A, C, D

16. Suppose class A has a method called doSomething(), with default access. Suppose class B extends A and overrides doSomething(). Which access modes may apply to B's version of doSomething()? (Choose all that apply.) A. public B. private C. protected D. Default

public protected Default

16. Suppose class A has a method called doSomething(), with default access. Suppose class B extends A and overrides doSomething(). Which access modes may apply to B's version of doSomething()? (Choose all that apply.) A. public B. private C. protected D. Default

C

16. What are the legal types for whatsMyType? short s = 10; whatsMyType = !s; A. short B. int C. There are no possible legal types.

There are no possible legal types.

16. What are the legal types for whatsMyType? short s = 10; whatsMyType = !s; A. short B. int C. There are no possible legal types.

E

16. What method of the java.io.File class can create a file on the hard drive? A. newFile() B. makeFile() C. makeNewFile() D. createFile() E. createNewFile()

createNewFile()

16. What method of the java.io.File class can create a file on the hard drive? A. newFile() B. makeFile() C. makeNewFile() D. createFile() E. createNewFile()

A, B, E

16. Which of the following are legal argument types for a switch statement? A. byte B. int C. long D. float E. char F. String

byte int char

16. Which of the following are legal argument types for a switch statement? A. byte B. int C. long D. float E. char F. String

C, D

16. Which of the following are legal? (Choose 2) A. int a = abcd; B. int b = ABCD; C. int c = 0xabcd; D. int d = 0XABCD; E. int e = 0abcd; F. int f = 0ABCD;

C, D, E

16. Which of the following are methods of the java.util.SortedMap interface? A. first B. last C. headMap D. tailMap E. subMap

A, B

19. Which of the following may appear on the right-hand side of an instanceof operator? (Choose 2) A. A reference B. A class C. An interface D. A variable of primitive type E. The name of a primitive type

An exception is thrown at line 9.

18. What happens when you try to compile and run the following application? 1. import java.io.*; 2. 3. public class Xxx { 4. public static void main(String[] args) { 5. try { 6. File f = new File("xxx.ser"); 7. FileOutputStream fos = new FileOutputStream(f); 8. ObjectOutputStream oos = new ObjectOutputStream(fos); 9. oos.writeObject(new Object()); 10. oos.close(); 11. fos.close(); 12. } 13. catch (Exception x) { } 14. } 15. } A. Compiler error at line 9. B. An exception is thrown at line 9. C. An exception is thrown at line 10. D. No compiler error and no exception.

B

18. What happens when you try to compile and run the following application? 1. import java.io.*; 2. 3. public class Xxx { 4. public static void main(String[] args) { 5. try { 6. File f = new File("xxx.ser"); 7. FileOutputStream fos = new FileOutputStream(f); 8. ObjectOutputStream oos = new ObjectOutputStream(fos); 9. oos.writeObject(new Object()); 10. oos.close(); 11. fos.close(); 12. } 13. catch (Exception x) { } 14. } 15. } A. Compiler error at line 9. B. An exception is thrown at line 9. C. An exception is thrown at line 10. D. No compiler error and no exception.

C

18. What happens when you try to compile and run this application? 1. import java.util.*; 2. 3. public class Apple { 4. public static void main(String[] a) { 5. Set<Apple> set = new TreeSet<Apple>(); 6. set.add(new Apple()); 7. set.add(new Apple()); 8. set.add(new Apple()); 9. } 10. } A. Compiler error. B. An exception is thrown at line 6. C. An exception is thrown at line 7. D. An exception is thrown at line 8. E. No exception is thrown.

C

18. When a negative byte is cast to a long, what are the possible values of the result? A. Positive B. Zero C. Negative

B, C

18. Which methods return an enum constant's name? A. getName() B. name() C. toString() D. nameString() E. getNameString()

name() toString()

18. Which methods return an enum constant's name? A. getName() B. name() C. toString() D. nameString() E. getNameString()

C

18. Which of the following are legal? A. char c = 0x1234; B. char c = \u1234; C. char c = '\u1234';

A

18. Which of the following may appear on the left-hand side of an instanceof operator? A. A reference B. A class C. An interface D. A variable of primitive type

B

18. Which of the following should always be caught? A. Runtime exceptions B. Checked exceptions C. Assertion errors D. Errors other than assertion errors

Checked exceptions

18. Which of the following should always be caught? A. Runtime exceptions B. Checked exceptions C. Assertion errors D. Errors other than assertion errors

D

18. Which of the following statements are true? A. A final class must be instantiated. B. A final class must contain at least one final method. C. A final class must contain at least one final data field. D. A final class may not be extended. E. None of the above.

A, C

18. Which of the following statements are true? (Choose all correct options.) A. StringBuilder encapsulates a mutable string. B. StringBuilder is threadsafe. C. StringBuffer is threadsafe. D. StringBuffer is generally faster than StringBuilder.

A class An interface

19. Which of the following may appear on the right-hand side of an instanceof operator? (Choose all that apply.) A. A reference B. A class C. An interface D. A variable of primitive type E. The name of a primitive type

C

19. Consider the following code: 1. StringBuffer sbuf = new StringBuffer(); 2. sbuf = null; 3. System.gc(); Choose all true statements: A. After line 2 executes, the StringBuffer object is garbage collected. B. After line 3 executes, the StringBuffer object is garbage collected. C. After line 2 executes, the StringBuffer object is eligible for garbage collection. D. After line 3 executes, the StringBuffer object is eligible for garbage collection

A

19. Given arrays a1 and a2, which call returns true if a1 and a2 have the same length, and a1[i].equals(a2[i]) for every legal index i? A. java.util.Arrays.equals(a1, a2); B. java.util.Arrays.compare(a1, a2); C. java.util.List.compare(a1, a2); D. java.util.List.compare(a1, a2);

A

19. Suppose class X contains the following method: void doSomething(int a, float b) { ... } Which of the following methods may appear in class Y, which extends X? A. public void doSomething(int a, float b) { ... } B. private void doSomething(int a, float b) { ... } C. public void doSomething(int a, float b) throws java.io.IOException { ... } D. private void doSomething(int a, float b) throws java.io.IOException { ... }

public void doSomething(int a, float b) { ... }

19. Suppose class X contains the following method: void doSomething(int a, float b) { ... } Which of the following methods may appear in class Y, which extends X? A. public void doSomething(int a, float b) { ... } B. private void doSomething(int a, float b) { ... } C. public void doSomething(int a, float b) throws java.io.IOException { ... } D. private void doSomething(int a, float b) throws java.io.IOException { ... }

A, B, D

19. Suppose you know that a file named aaa was created by a Java program that used a DataOutputStream. The file contains 10 doubles, followed by a UTF string. Which of the following code snippets read the string correctly? Assume all code exists in an environment that legally handles IOException. (Choose all correct options.) A. RandomAccessFile raf = new RandomAccessFile("aaa", "r"); for (int i=0; i<10; i++) raf.readDouble(); String s = raf.readUTF(); B. RandomAccessFile raf = new RandomAccessFile("aaa", "r"); raf.seek(10*8); String s = raf.readUTF(); C. FileReader fr = new FileReader(fr); for (int i=0; i<10*8; i++) fr.read(); String s = fr.readUTF(); D. FileInputStream fis = new FileInputStream("aaa"); DataInputStream dis = new DataInputStream(fis); for (int i=0; i<10; i++) dis.readDouble(); String s = dis.readUTF(); E. FileInputStream fis = new FileInputStream("aaa"); DataInputStream dis = new DataInputStream(fis); dis.seek(10*8); String s = dis.readUTF();

A

19. When does an exception's stack trace get recorded in the exception object? A. When the exception is constructed B. When the exception is thrown C. When the exception is caught D. When the exception's printStackTrace() method is called

When the exception is constructed

19. When does an exception's stack trace get recorded in the exception object? A. When the exception is constructed B. When the exception is thrown C. When the exception is caught D. When the exception's printStackTrace() method is called

A, D

19. Which of the following are true? (Choose all that apply.) A. When you declare a method to be synchronized, the method always synchronizes on the lock of the current object. B. When you declare a method to be synchronized, you can specify the object on whose lock the method should synchronize. C. When you declare a block of code inside a method to be synchronized, the block always synchronizes on the lock of the current object. D. When you declare a block of code inside a method to be synchronized, you can specify the object on whose lock the block should synchronize.

A, C, D, E

19. Which of the following are valid mode strings for the RandomAccessFile constructor? (Choose all that apply.) A. "r" B. "ro" C. "rw" D. "rws" E. "rwd"

F

2. Choose the valid identifiers from those listed here. (Choose all that apply.) A. BigOlLongStringWithMeaninglessName B. $int C. bytes D. $1 E. finalist F. All of the other choice

i = 1 j = 0

2. Consider the following code: 1. outer: for (int i = 0; i < 2; i++) { 2. for (int j = 0; j < 3; j++) { 3. if (i == j) { 4. continue outer; 5. } 6. System.out.println("i = " + i + " j = " + j); 7. } 8. } Which lines would be part of the output? (Choose all that apply.) A. i = 0 j = 0 B. i = 0 j = 1 C. i = 0 j = 2 D. i = 1 j = 0 E. i = 1 j = 1

D

2. Consider the following code: 1. outer: for (int i = 0; i < 2; i++) { 2. for (int j = 0; j < 3; j++) { 3. if (i == j) { 4. continue outer; 5. } 6. System.out.println("i = " + i + " j = " + j); 7. } 8. } Which lines would be part of the output? (Choose all that apply.) A. i = 0 j = 0 B. i = 0 j = 1 C. i = 0 j = 2 D. i = 1 j = 0 E. i = 1 j = 1 F. i = 1 j = 2

B, D

2. Consider these classes, defined in separate source files: 1. public class Test1 { 2. public float aMethod(float a, float b) 3. throws IOException {... 4. } 5. } 1. public class Test2 extends Test1 { 2. 3. } Which of the following methods would be legal (individually) at line 2 in class Test2? (Choose all that apply.) A. float aMethod(float a, float b) {...} B. public int aMethod(int a, int b) throws Exception {...} C. public float aMethod(float a, float b) throws Exception {...} D. public float aMethod(float p, float q) {...}

B, D, F

2. Given the following: package pack; class Sploo { public int a; public static int b; int c; static int d; public void eee() { } public static void fff() { } } Which of the following features of class Sploo may be accessed by a class, in package pack, as a result of the following import? import static pack.Sploo.*; A. a B. b C. c D. d E. eee() F. fff()

B, C

2. Which of the following expressions are legal? (Choose 2) A. int x = 6; x = !x; B. int x = 6; if (!(x > 3)) {} C. int x = 6; x = ~x;

D

2. Which of the statements below are true? (Choose all that apply.) A. When you construct an instance of File, if you do not use the file-naming semantics of the local machine, the constructor will throw an IOException. B. When you construct an instance of File, if the corresponding file does not exist on the local file system, one will be created. C. When an instance of File is garbage collected, the corresponding file on the local file system is deleted. D. None of the above.

None of the above.

2. Which of the statements below are true? (Choose all that apply.) A. When you construct an instance of File, if you do not use the file-naming semantics of the local machine, the constructor will throw an IOException. B. When you construct an instance of File, if the corresponding file does not exist on the local file system, one will be created. C. When an instance of File is garbage collected, the corresponding file on the local file system is deleted. D. None of the above.

C

2. Which one statement is always true about the following application? 1. class HiPri extends Thread { 2. HiPri() { 3. setPriority(10); 4. } 5. 6. public void run() { 7. System.out.println( 8. "Another thread starting up."); 9. while (true) { } 10. } 11. 12. public static void main(String args[]) { 13. HiPri hp1 = new HiPri(); 14. HiPri hp2 = new HiPri(); 15. HiPri hp3 = new HiPri(); 16. hp1.start(); 17. hp2.start(); 18. hp3.start(); 19. } 20. } A. When the application is run, thread hp1 will execute; threads hp2 and hp3 will never get the CPU. B. When the application is run, thread hp1 will execute to completion, thread hp2 will execute to completion, then thread hp3 will execute to completion. C. When the application is run, all three threads (hp1, hp2, and hp3) will execute concurrently, taking time-sliced turns in the CPU. D. None of the above scenarios can be guaranteed to happen in all cases.

When the application is run, all three threads (hp1, hp2, and hp3) will execute concurrently, taking time-sliced turns in the CPU.

2. Which one statement is always true about the following application? 1. class HiPri extends Thread { 2. HiPri() { 3. setPriority(10); 4. } 5. 6. public void run() { 7. System.out.println( 8. "Another thread starting up."); 9. while (true) { } 10. } 11. 12. public static void main(String args[]) { 13. HiPri hp1 = new HiPri(); 14. HiPri hp2 = new HiPri(); 15. HiPri hp3 = new HiPri(); 16. hp1.start(); 17. hp2.start(); 18. hp3.start(); 19. } 20. } A. When the application is run, thread hp1 will execute; threads hp2 and hp3 will never get the CPU. B. When the application is run, thread hp1 will execute to completion, thread hp2 will execute to completion, then thread hp3 will execute to completion. C. When the application is run, all three threads (hp1, hp2, and hp3) will execute concurrently, taking time-sliced turns in the CPU. D. None of the above scenarios can be guaranteed to happen in all cases.

C

2. Which one statement is true about the following code? 1. String s1 = "abc" + "def"; 2. String s2 = new String(s1); 3. if (s1 == s2) 4. System.out.println("== succeeded"); 5. if (s1.equals(s2)) 6. System.out.println(".equals() succeeded"); A. Lines 4 and 6 both execute. B. Line 4 executes and line 6 does not. C. Line 6 executes and line 4 does not. D. Neither line 4 nor line 6 executes.

D

20. What is -50 >> 1? A. A negative number with very large magnitude. B. A positive number with very large magnitude. C. -100 D. -25 E. 100 F. 25

B

20. Suppose you want to create a custom thread class by extending java.lang.Thread in order to provide some special functionality. Which of the following must you do? A. Declare that your class implements java.lang.Runnable. B. Override run(). C. Override start(). D. Make sure that all access to all data is via synchronized methods.

C

20. Suppose you want to read a file that was not created by a Java program. The file contains lines of 8-bit text, and the 8-bit encoding represents the local character set, as represented by the cur- rent default locale. The lines are separated by newline characters. Which strategy reads the file and produces Java strings? A. Create a RandomAccessFile instance and use its readText() method. B. Create a RandomAccessFile instance and use its readUTF() method. C. Create a FileReader instance. Pass it into the constructor of LineNumberReader. Use LineNumberReader's readLine() method. D. Create a FileInputStream instance. Pass it into the constructor of LineNumberReader. Use LineNumberReader's readLine() method. E. Create a FileInputStream instance. Pass it into the constructor of DataInputStream. Use DataInputStream's readLine() method.

A, C, D

20. This question involves IOException, AWTException, and EOFException. They are all checked exception types. IOException and AWTException extend Exception, and EOFException extends IOException. Suppose class X contains the following method: void doSomething() throws IOException{ ... } Which of the following methods may appear in class Y, which extends X? A. void doSomething() { ... } B. void doSomething() throws AWTException { ... } C. void doSomething() throws EOFException { ... } D. void doSomething() throws IOException, EOFException { ... }

D

20. What does the following code print? public class A { static int x; public static void main(String[] args) { A that1 = new A(); A that2 = new A(); that1.x = 5; that2.x = 1000; x = -1; System.out.println(x); } } A. 0 B. 5 C. 1000 D. -1

-25

20. What is -50 >> 1? A. A negative number with very large magnitude. B. A positive number with very large magnitude. C. -100 D. -25 E. 100 F. 25

A

20. What is the difference between the rules for method-call conversion and the rules for assignment conversion? A. There is no difference; the rules are the same. B. Method-call conversion supports narrowing, assignment conversion does not. C. Assignment conversion supports narrowing, method-call conversion does not. D. Method-call conversion supports narrowing if the method declares that it throws ClassCastException.

There is no difference; the rules are the same.

20. What is the difference between the rules for method-call conversion and the rules for assignment conversion? A. There is no difference; the rules are the same. B. Method-call conversion supports narrowing, assignment conversion does not. C. Assignment conversion supports narrowing, method-call conversion does not. D. Method-call conversion supports narrowing if the method declares that it throws ClassCastException.

E

20. When is it appropriate to write code that constructs and throws an error? A. When a public method's preconditions are violated B. When a public method's postconditions are violated C. When a nonpublic method's preconditions are violated D. When a nonpublic method's postconditions are violated E. Never

Never

20. When is it appropriate to write code that constructs and throws an error? A. When a public method's preconditions are violated B. When a public method's postconditions are violated C. When a nonpublic method's preconditions are violated D. When a nonpublic method's postconditions are violated E. Never

B, D

20. Which of the following are true? (Choose 2) A. Primitives are passed by reference. B. Primitives are passed by value. C. References are passed by reference. D. References are passed by value.

C

20. Which of the following are valid arguments to the DataInputStream constructor? A. File B. FileReader C. FileInputStream D. RandomAccessFile

A, D

20. Which of the following statements are true? A. StringBuilder is generally faster than StringBuffer. B. StringBuffer is generally faster than StringBuilder. C. StringBuilder is threadsafe; StringBuffer is not. D. StringBuffer is threadsafe; StringBuilder is not.

StringBuilder is generally faster than StringBuffer. StringBuffer is threadsafe; StringBuilder is not.

20. Which of the following statements are true? A. StringBuilder is generally faster than StringBuffer. B. StringBuffer is generally faster than StringBuilder. C. StringBuilder is threadsafe; StringBuffer is not. D. StringBuffer is threadsafe; StringBuilder is not.

b, e

21. What interfaces can be implemented in order to create a class that can be serialized? (Choose all that apply.) A. No interfaces need to be implemented. All classes can be serialized. B. Have the class declare that it implements java.io.Serializable. There are no methods in the interface. C. Have the class declare that it implements java.io.Serializable, which defines two methods: readObject and writeObject. D. Have the class declare that it implements java.io.Externalizable, which defines two methods: readObject and writeObject. E. Have the class declare that it implements java.io.Externalizable, which defines two methods: readExternal and writeExternal

b, c

23. Suppose you want to create a class that compiles and can be serialized and deserialized without causing an exception to be thrown. Which statements are true regarding the class? (Choose all correct options.) A. If the class implements java.io.Serializable and does not implement java.io.Externalizable, it must have a no-args constructor. B. If the class implements java.io.Externalizable, it must have a no-args constructor. C. If the class implements java.io.Serializable and does not implement java.io.Externalizable, its nearest superclass that doesn't implement Serializable must have a no-args constructor. D. If the class implements java.io.Externalizable, its nearest superclass that doesn't implement Externalizable must have a no-args constructor.

D

23. Which class provides locale-sensitive text formatting for date and time information? A. java.util.TimeFormat B. java.util.DateFormat C. java.text.TimeFormat D. java.text.DateFormat

c, d

24. Suppose you want to use a DateFormat to format an instance of Date. What factors influence the string returned by DateFormat's format() method? A. The operating system B. The style, which is one of SHORT, MEDIUM, or LONG C. The style, which is one of SHORT, MEDIUM, LONG, or FULL D. The locale

B

24. The following line of code is valid. int x = 9; byte b = x; A. True B. False

NumberFormat nf = NumberFormat.getInstance(loc); String s = nf.format(f);

25. How do you generate a string representing the value of a float f in a format appropriate for a locale loc? A. NumberFormat nf = NumberFormat.getInstance(loc); String s = nf.format(f); B. NumberFormat nf = new NumberFormat(loc); String s = nf.format(f); C. NumberFormat nf = NumberFormat.getInstance(); String s = nf.format(f, loc); D. NumberFormat nf = new NumberFormat(loc); String s = nf.format(f, loc);

a

25. How do you generate a string representing the value of a float f in a format appropriate for a locale loc? A. NumberFormat nf = NumberFormat.getInstance(loc); String s = nf.format(f); B. NumberFormat nf = new NumberFormat(loc); String s = nf.format(f); C. NumberFormat nf = NumberFormat.getInstance(); String s = nf.format(f, loc); D. NumberFormat nf = new NumberFormat(loc); String s = nf.format(f, loc);

A, B, C

25. Which of the following code snippets compile? (choose 3) A. Integer i = 7; B. Integer i = new Integer(5); int j = i; C. byte b = 7; D. int i = 7; byte b = i; E. None of the above

c

26. Given the following code: 1. String scanMe = "aeiou9876543210AEIOU"; 2. Scanner scanner = new Scanner(scanMe); 3. String delim = ?????; // WHAT GOES HERE? 4. scanner.useDelimiter(delim); 5. while (scanner.hasNext()) 6. System.out.println(scanner.next()); What code at line 3 produces the following output? aeiou AEIOU A. String delim = "d+"; B. String delim = "\d+"; C. String delim = "\\d+"; D. String delim = "d*"; E. String delim = "\d*"; F. String delim = "\\d*";

String delim = "\\d+";

26. Given the following code: 1. String scanMe = "aeiou9876543210AEIOU"; 2. Scanner scanner = new Scanner(scanMe); 3. String delim = ?????; // WHAT GOES HERE? 4. scanner.useDelimiter(delim); 5. while (scanner.hasNext()) 6. System.out.println(scanner.next()); What code at line 3 produces the following output? aeiou AEIOU A. String delim = "d+"; B. String delim = "\d+"; C. String delim = "\\d+"; D. String delim = "d*"; E. String delim = "\d*"; F. String delim = "\\d*";

D

26. What will be the output of the following code? public class StringTest { public static void main(String [] a) { String s1 = "test string"; String s2 = "test string"; if (s1 == s2) { System.out.println("same"); } else { System.out.println("different"); } } } A. The code will compile but not run. B. The code will not compile. C. "different" will be printed out to the console. D. "same" will be printed out to the console. E. None of the above

B

27. Java arrays always start at index 1. A. True B. False

System.out.format("%-20.15f", d);

27. Which line prints double d in a left-justified field that is 20 characters wide, with 15 characters to the right of the decimal point? A. System.out.format("%20.5f", d); B. System.out.format("%20.15f", d); C. System.out.format("%-20.5f", d); D. System.out.format("%-20.15f", d);

d

27. Which line prints double d in a left-justified field that is 20 characters wide, with 15 characters to the right of the decimal point? A. System.out.format("%20.5f", d); B. System.out.format("%20.15f", d); C. System.out.format("%-20.5f", d); D. System.out.format("%-20.15f", d);

a, d

28. Suppose MyThread extends java.lang.Thread, and MyRunnable implements java.lang .Runnable (but does not extend Thread). Both classes have no-args constructors. Which of the following cause a thread in the JVM to begin execution? (Choose all correct options.) A. (new MyThread()).start(); B. (new MyThread()).run(); C. (new MyRunnable()).run(); D. (new Thread(new MyRunnable())) E. .start();

C

28. Which of the following statements accurately describes how variables are passed to methods? A. Arguments are always passed by value. B. Arguments are always passed by reference. C. Arguments that are primitive type are passed by value. D. Arguments that are passed with the & operator are passed by reference.

D

29. How do you change the value that is encapsulated by a wrapper class after you have instan- tiated it? A. Use the setXXX() method defined for the wrapper class. B. Use the parseXXX() method defined for the wrapper class. C. Use the equals() method defined for the wrapper class. D. None of the above.

a

29. What will be the outcome when the following application is executed? public class ThreadTest { public void newThread() { Thread t = new Thread() { public void run() { System.out.println("Going to sleep"); try { sleep(5000); } catch (InterruptedException e) {} System.out.println("Waking up"); } }; t.start(); try { t.join(); } catch (InterruptedException e) {} System.out.println("All done"); } public static void main(String [] args) { new ThreadTest().newThread(); } } A. The code prints "Going to sleep," then "Waking up," and then "All done." B. The code prints "All done," then "Going to sleep," and then "Waking up." C. The code prints "All done" only. D. The code prints "Going to sleep" and then "Waking up." E. The code does not compile

The code prints "Going to sleep," then "Waking up," and then "All done."

29. What will be the outcome when the following application is executed? public class ThreadTest { public void newThread() { Thread t = new Thread() { public void run() { System.out.println("Going to sleep"); try { sleep(5000); } catch (InterruptedException e) {} System.out.println("Waking up"); } }; t.start(); try { t.join(); } catch (InterruptedException e) {} System.out.println("All done"); } public static void main(String [] args) { new ThreadTest().newThread(); } } A. The code prints "Going to sleep," then "Waking up," and then "All done." B. The code prints "All done," then "Going to sleep," and then "Waking up." C. The code prints "All done" only. D. The code prints "Going to sleep" and then "Waking up." E. The code does not compile.

B

3. A thread wants to make a second thread ineligible for execution. To do this, the first thread can call the yield() method on the second thread. A. True B. False

B, C, E

3. Given the following: public abstract class Abby { abstract provideMe(); } public class SubAbby extends Abby { } Which statements are true? A. Abby generates a compiler error. B. SubAbby generates a compiler error. C. If SubAbby were declared abstract, it would compile without error. D. If SubAbby were declared abstract, it could be instantiated. E. Abby is a legal type for variables.

D

3. Suppose you want to write a class that offers static methods to compute hyperbolic trigonometric functions. You decide to subclass java.lang.Math and provide the new functionality as a set of static methods. Which one statement is true about this strategy? A. The strategy works. B. The strategy works, provided the new methods are public. C. The strategy works, provided the new methods are not private. D. The strategy fails because you cannot subclass java.lang.Math. E. The strategy fails because you cannot add static methods to a subclass.

The strategy fails because you cannot subclass java.lang.Math.

3. Suppose you want to write a class that offers static methods to compute hyperbolic trigonometric functions. You decide to subclass java.lang.Math and provide the new functionality as a set of static methods. Which one statement is true about this strategy? A. The strategy works. B. The strategy works, provided the new methods are public. C. The strategy works, provided the new methods are not private. D. The strategy fails because you cannot subclass java.lang.Math. E. The strategy fails because you cannot add static methods to a subclass.

A

3. What is the minimal modification that will make this code compile correctly? 1. final class Aaa 2. { 3. int xxx; 4. void yyy() { xxx = 1; } 5. } 6. 7. 8. class Bbb extends Aaa 9. { 10. final Aaa finalref = new Aaa(); 11. 12. final void yyy() 13. { 14. System.out.println("In method yyy()"); 15. finalref.xxx = 12345; 16. } 17. } A. On line 1, remove the final modifier. B. On line 10, remove the final modifier. C. Remove line 15. D. On lines 1 and 10, remove the final modifier. E. The code will compile as is. No modification is needed.

On line 1, remove the final modifier.

3. What is the minimal modification that will make this code compile correctly? 1. final class Aaa 2. { 3. int xxx; 4. void yyy() { xxx = 1; } 5. } 6. 7. 8. class Bbb extends Aaa 9. { 10. final Aaa finalref = new Aaa(); 11. 12. final void yyy() 13. { 14. System.out.println("In method yyy()"); 15. finalref.xxx = 12345; 16. } 17. } A. On line 1, remove the final modifier. B. On line 10, remove the final modifier. C. Remove line 15. D. On lines 1 and 10, remove the final modifier. E. The code will compile as is. No modification is needed.

C

3. Which of the following are legal loop constructions? (Choose all that apply.) A. while (int i<7) { i++; System.out.println("i is " + i); } B. int i = 3; while (i) { System.out.println("i is " + i); } C. int j = 0; for (int k=0, j+k != 10; j++,k++) { System.out.println("j=" + j + ", k=" + k); } D. int j=0; do { System.out.println("j=" + j++); if (j==3) continue loop; } while (j<10);

int j = 0; for (int k=0, j+k != 10; j++,k++) { System.out.println("j=" + j + ", k=" + k); }

3. Which of the following are legal loop constructions? (Choose all that apply.) A. while (int i<7) { i++; System.out.println("i is " + i); } B. int i = 3; while (i) { System.out.println("i is " + i); } C. int j = 0; for (int k=0, j+k != 10; j++,k++) { System.out.println("j=" + j + ", k=" + k); } D. int j=0; do { System.out.println("j=" + j++); if (j==3) continue loop; } while (j<10);

A

3. Which of the following expressions results in a positive value in x? A. int x = -1; x = x >>> 5; B. int x = -1; x = x >>> 32; C. byte x = -1; x = x >>> 5; D. int x = -1; x = x >> 5;

B, C

3. Which of the following signatures are valid for the main() method entry point of an application? (Choose all that apply.) A. public static void main() B. public static void main(String arg[]) C. public void main(String [] arg) D. public static void main(String[] args) E. public static int main(String [] arg)

D

3. Which of the statements below are true? (Choose all that apply.) A. To change the current working directory, call the setWorkingDirectory() method of the File class. B. To change the current working directory, call the cd() method of the File class. C. To change the current working directory, call the changeWorkingDirectory() method of the File class. D. None of the above.

E, F

3. You have been given a design document for a veterinary registration system for implementation in Java. It states: "A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet that has a flag indicating whether it has been neutered, and a textual description of its markings." Given that the Pet class has already been defined, which of the following fields would be appropriate for inclusion in the Cat class as members? (Choose all that apply.) A. Pet thePet; B. Date registered; C. Date vaccinationDue; D. Cat theCat; E. boolean neutered; F. String markings;

a, b, d

30. Given the following class: class Classy { synchronized void notStaticMethod() { for (long n=0; n<100000000000L; n++) System.out.println(n); } synchronized static void staticMethod() { for (long n=0; n<100000000000L; n++) System.out.println(n); } } Suppose thread A and thread B both have references to each of two instances of Classy. These references are named classy1 and classy2. Which statements are true? (Choose all correct options.) A. If thread A is executing classy1.staticMethod(), then thread B may not execute classy1.staticMethod(). B. If thread A is executing classy1.staticMethod(), then thread B may not execute classy2.staticMethod(). C. If thread A is executing classy1.notStaticMethod(), then thread B may not execute classy1.staticMethod(). D. If thread A is executing classy1.notStaticMethod(), then thread B may not execute classy1.notStaticMethod(). E. If thread A is executing classy1.notStaticMethod(), then thread B may not execute classy2.notStaticMethod().

A

30. Suppose you are writing a class that provides custom deserialization. The class implements java.io.Serializable (and not java.io.Externalizable). What method should imple- ment the custom deserialization, and what is its access mode? A. private readObject B. public readObject() C. private readExternal() D. public readExternal()

c, e

31. Suppose threads aThread and bThread are both accessing a shared object named sharedOb, and aThread has just executed: sharedOb.wait(); What code can bThread execute in order to get aThread out of the waiting state, no matter what other conditions prevail? A. aThread.notify(); B. aThread.notifyAll(); C. aThread.interrupt(); D. sharedOb.notify(); E. sharedOb.notifyAll();

b, c

32. Suppose class Car has public variables forceOnGasPedal and forceOnBrakePedal, and a public method respondToPedalChanges(). Class Driver manipulates an instance of Car by changing the variables and then calling the method. Which statements are true? (Choose all that apply.) A. The Car and Driver classes are loosely coupled. B. The Car and Driver classes are tightly coupled. C. This degree of coupling is desirable. D. This degree of coupling is undesirable.

b, d

33. Suppose class Home has methods chopWood() and carryWater(); it also has a method called chopWoodAndCarryWater(), which just calls the other two methods. Which statements are true? (Choose all that apply.) A. chopWoodAndCarryWater() is an example of appropriate cohesion. B. chopWoodAndCarryWater() is an example of inappropriate cohesion. C. chopWoodAndCarryWater() is an example of appropriate coupling. D. chopWoodAndCarryWater() is an example of inappropriate coupling.

b, c, d

34. Suppose class Lemon extends class Citrus. Given the following code: Lemon lem = new Lemon(); Citrus cit = new Citrus(); Which lines compile without error? (Choose all that apply.) A. lem = cit; B. cit = lem; C. lem = (Lemon)cit; D. cit = (Citrus)lem; E. cit = (Object)lem;

a, c

35. Suppose classes Lemon and Grapefruit extend class Citrus. Which statements are true regard- ing the following code? 1. Grapefruit g = new Grapefruit(); 2. Citrus c = (Citrus)g; 3. Lemon lem = (Lemon)c; A. The cast in line 2 is not necessary. B. Line 3 causes a compiler error. C. The code compiles, and throws an exception at line 3. D. The code compiles and runs without throwing any exceptions.

a, b

36. Suppose class aaa.Aaa has a method called callMe(). Suppose class bbb.Bbb, which extends aaa.AAA, wants to override callMe(). Which access modes for callMe() in aaa.AAA will allow this? A. public B. protected C. default D. private

b

37. What happens when you try to compile the following code and run the Zebra application? class Animal { float weight; Animal(float weight) { this.weight = weight; } } class Zebra extends Animal { public static void main(String[] args) { Animal a = new Animal(222.2f); Zebra z = new Zebra(); } } A. Class Animal generates a compiler error. B. Class Zebra generates a compiler error. C. The code compiles without error. The application throws an exception when the Animal constructor is called. D. The code compiles without error. The application throws an exception when the Zebra constructor is called. E. The code compiles and runs without error.

a, b

38. Given the following code: 1. class Xyz { 2. float f; 3. Xyz() { 4. ??? // What goes here? 5. } 6. Xyz(float f) { 7. this.f = f; 8. } 9. } What code at line 4 results in a class that compiles? A. super(); B. this(1.23f); C. this(1.23f); super(); D. super(1.23f); this(1.23f);

a

39. What relationship does the extends keyword represent? A. "is a" B. "has a" C. Polymorphism D. Multivariance E. Overloading

D

4. A thread's run() method includes the following lines: 1. try { 2. sleep(100); 3. } catch (InterruptedException e) { } Assuming the thread is not interrupted, which one of the following statements is correct? A. The code will not compile, because exceptions cannot be caught in a thread's run() method. B. At line 2, the thread will stop running. Execution will resume in, at most, 100 milliseconds. C. At line 2, the thread will stop running. It will resume running in exactly 100 milliseconds. D. At line 2, the thread will stop running. It will resume running some time after 100 milliseconds have elapsed.

D

4. Given the following code: class Xxx { int[] ages; int[] heights = new int[10]; } Which statements are true? A. ages is initialized to null. B. ages is initialized to a reference to an array with zero elements. C. heights is initialized to null. D. heights is initialized to a reference to an array with zero elements. E. heights is initialized to a reference to an array with 10 elements.

A

4. How do you use the File class to list the contents of a directory? A. String[] contents = myFile.list(); B. File[] contents = myFile.list(); C. StringBuilder[] contents = myFile.list(); D. The File class does not provide a way to list the contents of a directory.

D

4. If all three top-level elements occur in a source file, they must appear in which order? A. Imports, package declarations, classes/interfaces/enums B. Classes/interfaces/enums, imports, package declarations C. Package declaration must come first; order for imports and class/interfaces/enum definitions is not significant D. Package declaration, imports, class/interface/enum definitions. E. Imports must come first; order for package declarations and class/interface/enum definitions is not significant

E

4. In the following code, what are the possible types for variable result? (Choose the most complete true answer.) 1. byte b = 11; 2. short s = 13; 3. result = b * ++s; A. byte, short, int, long, float, double B. boolean, byte, short, char, int, long, float, double C. byte, short, char, int, long, float, double D. byte, short, char E. int, long, float, double

B, C

6. Consider the following class definition: 1. public class Test extends Base { 2. public Test(int j) { 3. } 4. public Test(int j, int k) { 5. super(j, k); 6. } 7. } Which of the following are legitimate calls to construct instances of the Test class? (Choose all that apply.) A. Test t = new Test(); B. Test t = new Test(1); C. Test t = new Test(1, 2); D. Test t = new Test(1, 2, 3); E. Test t = (new Base()).new Test(1);

D

4. What would be the output from this code fragment? 1. int x = 0, y = 4, z = 5; 2. if (x > 2) { 3. if (y < 5) { 4. System.out.println("message one"); 5. } 6. else { 7. System.out.println("message two"); 8. } 9. } 10. else if (z > 5) { 11. System.out.println("message three"); 12. } 13. else { 14. System.out.println("message four"); 15. } A. message one B. message two C. message three D. message four

message four

4. What would be the output from this code fragment? 1. int x = 0, y = 4, z = 5; 2. if (x > 2) { 3. if (y < 5) { 4. System.out.println("message one"); 5. } 6. else { 7. System.out.println("message two"); 8. } 9. } 10. else if (z > 5) { 11. System.out.println("message three"); 12. } 13. else { 14. System.out.println("message four"); 15. } A. message one B. message two C. message three D. message four

A, C

4. Which of the following expressions are legal? (Choose 2) A. String x = "Hello"; int y = 9; x += y; B. String x = "Hello"; int y = 9; if (x == y) {} C. String x = "Hello"; int y = 9; x = x + y; D. String x = "Hello"; int y = 9; y = y + x;

E

4. Which of the following statements is true? A. Transient methods may not be overridden. B. Transient methods must be overridden. C. Transient classes may not be serialized. D. Transient variables must be static. E. Transient variables are not serialized.

A

4. Which one statement is true about the following code fragment? 1. import java.lang.Math; 2. Math myMath = new Math(); 3. System.out.println("cosine of 0.123 = " + 4. myMath.cos(0.123)); A. Compilation fails at line 2. B. Compilation fails at line 3 or 4. C. Compilation succeeds, although the import on line 1 is not necessary. During execution, an exception is thrown at line 3 or 4. D. Compilation succeeds. The import on line 1 is necessary. During execution, an exception is thrown at line 3 or 4. E. Compilation succeeds and no exception is thrown during execution.

Compilation fails at line 2.

4. Which one statement is true about the following code fragment? 1. import java.lang.Math; 2. Math myMath = new Math(); 3. System.out.println("cosine of 0.123 = " + 4. myMath.cos(0.123)); A. Compilation fails at line 2. B. Compilation fails at line 3 or 4. C. Compilation succeeds, although the import on line 1 is not necessary. During execution, an exception is thrown at line 3 or 4. D. Compilation succeeds. The import on line 1 is necessary. During execution, an exception is thrown at line 3 or 4. E. Compilation succeeds and no exception is thrown during execution.

C

4. You have been given a design document for a veterinary registration system for implementation in Java. It states: "A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet that has a flag indicating if it has been neutered, and a textual description of its markings." Given that the Pet class has already been defined and you expect the Cat class to be used freely throughout the application, how would you make the opening declaration of the Cat class, up to but not including the first opening brace? Use only these words and spaces: boolean, Cat, class, Date, extends, Object, Owner, Pet, private, protected, public, String. A. protected class Cat extends Owner B. public class Cat extends Object C. public class Cat extends Pet D. private class Cat extends Pet

d

40. When should objects stored in a Set implement the java.util.Comparable interface? A. Always B. When the Set is generic C. When the Set is a HashSet D. When the Set is a TreeSet E. Never 41. Given the following class: class Xyzzy { int a, b; public boolean equals(Object x) { Xyzzy that = (Xyzzy)x; return this.a == that.a; } Which methods below honor the hash code contract? A. public int hashCode() { return a; } B. public int hashCode() { return b; } C. public int hashCode() { return a+b; } D. public int hashCode() { return a*b; } E. public int hashCode() { return (int)Math.random(); }

a, e

41. Given the following class: class Xyzzy { int a, b; public boolean equals(Object x) { Xyzzy that = (Xyzzy)x; return this.a == that.a; } Which methods below honor the hash code contract? A. public int hashCode() { return a; } B. public int hashCode() { return b; } C. public int hashCode() { return a+b; } D. public int hashCode() { return a*b; } E. public int hashCode() { return (int)Math.random(); }

a

42. Give the following declarations: Vector plainVec; Vector<String> fancyVec; If you want a vector in which you know you will only store strings, what are the advantages of using fancyVec rather than plainVec? A. Attempting to add anything other than a string to fancyVec results in a compiler error. B. Attempting to add anything other than a string to fancyVec causes a runtime exception to be thrown. C. Attempting to add anything other than a string to fancyVec causes a checked exception to be thrown. D. Adding a string to fancyVec takes less time than adding one to plainVec. E. The methods of fancyVec are synchronized.

d

43. The declaration of the java.util.Collection interface is interface Collection <E> The addAll() method of that interface takes a single argument, which is a reference to a collection whose elements are compatible with E. What is the declaration of the addAll() method? A. public boolean addAll(Collection c) B. public boolean addAll(Collection c extends E) C. public boolean addAll(Collection ? extends E) D. public boolean addAll(Collection<? extends E> c)

a, b, c

44. The java.util.Arrays class has a binarySearch(int[] arr, int key) method. Which statements are true regarding this method? (Choose all that apply.) A. The method is static. B. The return value is the index in the array of key. C. The elements of the array must be sorted when the method is called. D. After the method returns, the elements of the array are sorted, even if they weren't sorted before the call.

b, c

45. Given the following class: package ocean; public class Fish { protected int size; protected void swim() { } } Which of the following may appear in a subclass of Fish named Tuna that is not in the ocean package? A. void swim() { } B. public void swim() { } C. size = 12; D. (new Tuna()).size = 12;

a

46. Given the following class: public class App { public static void main(String[] args) { System.out.println(args.length); } } Assuming App.class is stored in an appropriate location in file appjar.jar, what is printed when you type the following command line? java -cp appjar.jar -ea App 1 2 3 4 A. 4 B. 5 C. 6 D. 7 E. 8 F. 9

c

47. Given the following classes: public class Wrapper { public int x; } public class Tester { private static void bump(int n, Wrapper w) { n++; w.x++; } public static void main(String[] args) { int n = 10; Wrapper w = new Wrapper(); w.x = 10; bump(n, w); // Now what are n and w.x? } } When the application runs, what are the values of n and w.x after the call to bump() in the main() method? A. n is 10, w.x is 10 B. n is 11, w.x is 10 C. n is 10, w.x is 11 D. n is 11, w.x is 11

n is 10, w.x is 11

47. Given the following classes: public class Wrapper { public int x; } public class Tester { private static void bump(int n, Wrapper w) { n++; w.x++; } public static void main(String[] args) { int n = 10; Wrapper w = new Wrapper(); w.x = 10; bump(n, w); // Now what are n and w.x? } } When the application runs, what are the values of n and w.x after the call to bump() in the main() method? A. n is 10, w.x is 10 B. n is 11, w.x is 10 C. n is 10, w.x is 11 D. n is 11, w.x is 11

a

48. When does the string created on line 2 become eligible for garbage collection? 1. String s = "aaa"; 2. String t = new String(s); 3. t += "zzz"; 4. t = t.substring(0); 5. t = null; A. After line 3 B. After line 4 C. After line 5 D. The string created on line 2 does not become eligible for garbage collection in this code.

a, d

49. Suppose you want to run the following command line on a Windows system: java -classpath somewhere;elsewhere aaa.bbb.MyApplication On a Unix system the command line would be: java -classpath somewhere:elsewhere aaa.bbb.MyApplication Assume the CLASSPATH variable is not set. Which must be true in order for the application to run? A. Class MyApplication must contain the statement package aaa.bbb;. B. Class MyApplication must be in a directory named aaa and must contain the statement package bbb;. C. Class MyApplication must contain either the statement package somewhere.aaa.bbb; or the statement package elsewhere.aaa.bbb;. D. The file MyApplication.class must be found either in somewhere\aaa\bbb or in elsewhere\aaa\bbb. (Substitute forward slashes for backslashes on a Unix system.)

E

5. A monitor called mon has 10 threads in its waiting pool; all these waiting threads have the same priority. One of the threads is thr1. How can you notify thr1 so that it alone moves from the Waiting state to the Ready state? A. Execute notify(thr1); from within synchronized code of mon. B. Execute mon.notify(thr1); from synchronized code of any object. C. Execute thr1.notify(); from synchronized code of any object. D. Execute thr1.notify(); from any code (synchronized or not) of any object. E. You cannot specify which thread will get notified.

D

5. Consider the following class: 1. class Cruncher { 2. void crunch(int i) { 3. System.out.println("int version"); 4. } 5. void crunch(String s) { 6. System.out.println("String version"); 7. } 8. 9. public static void main(String args[]) { 10. Cruncher crun = new Cruncher(); 11. char ch = 'p'; 12. crun.crunch(ch); 13. } 14. } Which of the following statements is true? (Choose one.) A. Line 5 will not compile, because void methods cannot be overridden. B. Line 12 will not compile, because no version of crunch() takes a char argument. C. The code will compile but will throw an exception at line 12. D. The code will compile and produce the following output: int version. E. The code will compile and produce the following output: String version.

The code will compile and produce the following output: int version.

5. Consider the following class: 1. class Cruncher { 2. void crunch(int i) { 3. System.out.println("int version"); 4. } 5. void crunch(String s) { 6. System.out.println("String version"); 7. } 8. 9. public static void main(String args[]) { 10. Cruncher crun = new Cruncher(); 11. char ch = 'p'; 12. crun.crunch(ch); 13. } 14. } Which of the following statements is true? (Choose one.) A. Line 5 will not compile, because void methods cannot be overridden. B. Line 12 will not compile, because no version of crunch() takes a char argument. C. The code will compile but will throw an exception at line 12. D. The code will compile and produce the following output: int version. E. The code will compile and produce the following output: String version.

A

8. In the following code fragment, line 4 is executed. 1. String s1 = "xyz"; 2. String s2 = "xyz"; 3. if (s1 == s2) 4. System.out.println("Line 4"); A. True B. False

C

5. Consider the following classes, declared in separate source files: 1. public class Base { 2. public void method(int i) { 3. System.out.print("Value is " + i); 4. } 5. } 1. public class Sub extends Base { 2. public void method(int j) { 3. System.out.print("This value is " + j); 4. } 5. public void method(String s) { 6. System.out.print("I was passed " + s); 7. } 8. public static void main(String args[]) { 9. Base b1 = new Base(); 10. Base b2 = new Sub(); 11. b1.method(5); 12. b2.method(6); 13. } 14. } What output results when the main method of the class Sub is run? A. Value is 5Value is 6 B. This value is 5This value is 6 C. Value is 5This value is 6 D. This value is 5Value is 6 E. I was passed 5I was passed 6

Value is 5This value is 6

5. Consider the following classes, declared in separate source files: 1. public class Base { 2. public void method(int i) { 3. System.out.print("Value is " + i); 4. } 5. } 1. public class Sub extends Base { 2. public void method(int j) { 3. System.out.print("This value is " + j); 4. } 5. public void method(String s) { 6. System.out.print("I was passed " + s); 7. } 8. public static void main(String args[]) { 9. Base b1 = new Base(); 10. Base b2 = new Sub(); 11. b1.method(5); 12. b2.method(6); 13. } 14. } What output results when the main method of the class Sub is run? A. Value is 5Value is 6 B. This value is 5This value is 6 C. Value is 5This value is 6 D. This value is 5Value is 6 E. I was passed 5I was passed 6

A, E

5. Consider the following line of code: int[] x = new int[25]; After execution, which statements are true? (Choose 2) A. x[24] is 0 B. x[24] is undefined C. x[25] is 0 D. x[0] is null E. x.length is 25

C

5. Given a class with a public variable theTint of type Color, which of the following methods are consistent with the JavaBeans naming standards? A. public Color getColor() B. public Color getTint() C. public Color getTheTint() D. public Color gettheTint() E. public Color get_theTint()

public Color getTheTint()

5. Given a class with a public variable theTint of type Color, which of the following methods are consistent with the JavaBeans naming standards? A. public Color getColor() B. public Color getTint() C. public Color getTheTint() D. public Color gettheTint() E. public Color get_theTint()

12

5. How many bytes does the following code write to file dest? 1. try { 2. FileOutputStream fos = newFileOutputStream("dest"); 3. DataOutputStream dos = new DataOutputStream(fos); 4. dos.writeInt(3); 5. dos.writeDouble(0.0001); 6. dos.close(); 7. fos.close(); 8. } 9. catch (IOException e) { } A. 2 B. 8 C. 12 D. 16 E. The number of bytes depends on the underlying system.

C

5. How many bytes does the following code write to file dest? 1. try { 2. FileOutputStream fos = newFileOutputStream("dest"); 3. DataOutputStream dos = new DataOutputStream(fos); 4. dos.writeInt(3); 5. dos.writeDouble(0.0001); 6. dos.close(); 7. fos.close(); 8. } 9. catch (IOException e) { } A. 2 B. 8 C. 12 D. 16 E. The number of bytes depends on the underlying system.

A

5. What is -8 % 5? A. -3 B. 3 C. -2 D. 2

Compilation succeeds. No exception is thrown during execution.

5. Which one statement is true about the following code fragment? 1. String s = "abcde"; 2. StringBuffer s1 = new StringBuffer("abcde"); 3. if (s.equals(s1)) 4. s1 = null; 5. if (s1.equals(s)) 6. s = null; A. Compilation fails at line 1 because the String constructor must be called explicitly. B. Compilation fails at line 3 because s and s1 have different types. C. Compilation succeeds. During execution, an exception is thrown at line 3. D. Compilation succeeds. During execution, an exception is thrown at line 5. E. Compilation succeeds. No exception is thrown during execution.

E

5. Which one statement is true about the following code fragment? 1. String s = "abcde"; 2. StringBuffer s1 = new StringBuffer("abcde"); 3. if (s.equals(s1)) 4. s1 = null; 5. if (s1.equals(s)) 6. s = null; A. Compilation fails at line 1 because the String constructor must be called explicitly. B. Compilation fails at line 3 because s and s1 have different types. C. Compilation succeeds. During execution, an exception is thrown at line 3. D. Compilation succeeds. During execution, an exception is thrown at line 5. E. Compilation succeeds. No exception is thrown during execution.

True

8. In the following code fragment, line 4 is executed. 1. String s1 = "xyz"; 2. String s2 = "xyz"; 3. if (s1 == s2) 4. System.out.println("Line 4"); A. True B. False

D

5. Which statement is true about the following code fragment? 1. int j = 2; 2. switch (j) { 3. case 2: 4. System.out.println("value is two"); 5. case 2 + 1: 6. System.out.println("value is three"); 7. break; 8. default: 9. System.out.println("value is " + j); 10. break; 11. } A. The code is illegal because of the expression at line 5. B. The acceptable types for the variable j, as the argument to the switch() construct, could be any of byte, short, int, or long. C. The output would be the text value is two. D. The output would be the text value is two followed by the text value is three. E. The output would be the text value is two, followed by the text value is three, fol- lowed by the text value is 2.

The output would be the text value is two followed by the text value is three.

5. Which statement is true about the following code fragment? 1. int j = 2; 2. switch (j) { 3. case 2: 4. System.out.println("value is two"); 5. case 2 + 1: 6. System.out.println("value is three"); 7. break; 8. default: 9. System.out.println("value is " + j); 10. break; 11. } A. The code is illegal because of the expression at line 5. B. The acceptable types for the variable j, as the argument to the switch() construct, could be any of byte, short, int, or long. C. The output would be the text value is two. D. The output would be the text value is two followed by the text value is three. E. The output would be the text value is two, followed by the text value is three, fol- lowed by the text value is 2.

E

5. Which statement is true about this application? 1. class StaticStuff 2 { 3. static int x = 10; 4. 5. static { x += 5; } 6. 7. public static void main(String args[]) 8. { 9. System.out.println("x = " + x); 10. } 11. 12. static {x /= 5; } 13. } A. Lines 5 and 12 will not compile because the method names and return types are missing. B. Line 12 will not compile because you can only have one static initializer. C. The code compiles and execution produces the output x = 10. D. The code compiles and execution produces the output x = 15. E. The code compiles and execution produces the output x = 3.

d

50. What is -15 % -10? A. 0 B. 5 C. 10 D. -5 E. -1

D

6. Consider the following application: 1. class Q6 { 2. public static void main(String args[]) { 3. Holder h = new Holder(); 4. h.held = 100; 5. h.bump(h); 6. System.out.println(h.held); 7. } 8. } 9. 10. class Holder { 11. public int held; 12. public void bump(Holder theHolder) { 13. theHolder.held++; } 14. } 15. } What value is printed out at line 6? A. 0 B. 1 C. 100 D. 101

B, E, F

6. Consider the following class hierarchy and code fragment: 1. try { 2. // assume s is previously defined 3. URL u = new URL(s); 4. // in is an ObjectInputStream 5. Object o = in.readObject(); 6. System.out.println("Success"); 7. } 8. catch (MalformedURLException e) { 9. System.out.println("Bad URL"); 10. } 11. catch (StreamCorruptedException e) { 12. System.out.println("Bad file contents"); 13. } 14. catch (Exception e) { 15. System.out.println("General exception"); 16. } 17. finally { 18. System.out.println("Doing finally part"); 19. } 20. System.out.println("Carrying on"); What lines are output if the constructor at line 3 throws a MalformedURLException? (Choose all that apply.) A. Success B. Bad URL C. Bad file contents D. General exception E. Doing finally part F. Carrying on

A

6. If you attempt to compile and execute the following application, will it ever print out the message In xxx? 1. class TestThread3 extends Thread { 2. public void run() { 3. System.out.println("Running"); 4. System.out.println("Done"); 5. } 6. 7. private void xxx() { 8. System.out.println("In xxx"); 9. } 10. 11. public static void main(String args[]) { 12. TestThread3 ttt = new TestThread3(); 13. ttt.xxx(); 14. ttt.start(); 12. } 13. } A. Yes B. No

Yes

6. If you attempt to compile and execute the following application, will it ever print out the message In xxx? 1. class TestThread3 extends Thread { 2. public void run() { 3. System.out.println("Running"); 4. System.out.println("Done"); 5. } 6. 7. private void xxx() { 8. System.out.println("In xxx"); 9. } 10. 11. public static void main(String args[]) { 12. TestThread3 ttt = new TestThread3(); 13. ttt.xxx(); 14. ttt.start(); 12. } 13. } A. Yes B. No

A

6. In the following code fragment, after execution of line 1, sbuf references an instance of the StringBuffer class. After execution of line 2, sbuf still references the same instance. 1. StringBuffer sbuf = new StringBuffer("abcde"); 2. sbuf.insert(3, "xyz"); A. True B. False

True

6. In the following code fragment, after execution of line 1, sbuf references an instance of the StringBuffer class. After execution of line 2, sbuf still references the same instance. 1. StringBuffer sbuf = new StringBuffer("abcde"); 2. sbuf.insert(3, "xyz"); A. True B. False

B

6. What does the following code fragment print out at line 9? 1. FileOutputStream fos = new FileOutputStream("xx"); 2. for (byte b=10; b<50; b++) 3. fos.write(b); 4. fos.close(); 5. RandomAccessFile raf = new RandomAccessFile("xx", "r"); 6. raf.seek(10); 7. int i = raf.read(); 8. raf.close() 9. System.out.println("i = " + i); A. The output is i = 30. B. The output is i = 20. C. The output is i = 10. D. There is no output because the code throws an exception at line 1. E. There is no output because the code throws an exception at line 5.

B

6. What is 7 % -4? A. -3 B. 3 C. -4 D. 4

B

6. Which of the following statements are true regarding the following method? void callMe(String... names) { } A. It doesn't compile. B. Within the method, names is an array containing Strings. C. Within the method, names is a list containing Strings. D. The method may be called only from within the enclosing class.

Within the method, names is an array containing Strings.

6. Which of the following statements are true regarding the following method? void callMe(String... names) { } A. It doesn't compile. B. Within the method, names is an array containing Strings. C. Within the method, names is a list containing Strings. D. The method may be called only from within the enclosing class.

D

6. Which of the following statements is true? (Choose one.) A. Object references can be converted in assignments but not in method calls. B. Object references can be converted in method calls but not in assignments. C. Object references can be converted in both method calls and assignments, but the rules governing these conversions are very different. D. Object references can be converted in both method calls and assignments, and the rules governing these conversions are identical. E. Object references can never be converted.

Object references can be converted in both method calls and assignments, and the rules governing these conversions are identical.

6. Which of the following statements is true? (Choose one.) A. Object references can be converted in assignments but not in method calls. B. Object references can be converted in method calls but not in assignments. C. Object references can be converted in both method calls and assignments, but the rules governing these conversions are very different. D. Object references can be converted in both method calls and assignments, and the rules governing these conversions are identical. E. Object references can never be converted.

E

6. Which statement is true about this code? 1. class HasStatic 2. { 3. private static int x = 100; 4. 5. public static void main(String args[]) 6. { 7. HasStatic hs1 = new HasStatic(); 8. hs1.x++; 9. HasStatic hs2 = new HasStatic(); 10. hs2.x++; 11. hs1 = new HasStatic(); 12. hs1.x++; 13. HasStatic.x++; 14. System.out.println("x = " + x); 15. } 16. } A. Line 8 will not compile because it is a static reference to a private variable. B. Line 13 will not compile because it is a static reference to a private variable. C. The program compiles and the output is x = 102. D. The program compiles and the output is x = 103. E. The program compiles and the output is x = 104.

B

7. A Java monitor must either extend Thread or implement Runnable. A. True B. False

False

7. A Java monitor must either extend Thread or implement Runnable. A. True B. False

A, D

7. A file is created with the following code: 1. FileOutputStream fos = new FileOutputStream("datafile"); 2. DataOutputStream dos = new DataOutputStream(fos); 3. for (int i=0; i<500; i++) 4. dos.writeInt(i); You would like to write code to read back the data from this file. Which solutions will work? (Choose all that apply.) A. Construct a FileInputStream, passing the name of the file. Onto the FileInputStream, chain a DataInputStream, and call its readInt() method. B. Construct a FileReader, passing the name of the file. Call the file reader's readInt() method. C. Construct a PipedInputStream, passing the name of the file. Call the piped input stream's readInt() method. D. Construct a RandomAccessFile, passing the name of the file. Call the random access file's readInt() method. E. Construct a FileReader, passing the name of the file. Onto the FileReader, chain a DataInputStream, and call its readInt() method.

C

7. Consider the following application: 1. class Q7 { 2. public static void main(String args[]) { 3. double d = 12.3; 4. Decrementer dec = new Decrementer(); 5. dec.decrement(d); 6. System.out.println(d); 7. } 8. } 9. 10. class Decrementer { 11. public void decrement(double decMe) { 12. decMe = decMe - 1.0; 13. } 14. } What value is printed out at line 6? A. 0.0 B. 1.0 C. 12.3 D. 11.3

A, C

7. Consider the following class definition: 1. public class Test extends Base { 2. public Test(int j) { 3. } 4. public Test(int j, int k) { 5. super(j, k); 6. } 7. } Which of the following forms of constructor must exist explicitly in the definition of the Base class? Assume Test and Base are in the same package. (Choose all that apply.) A. Base() { } B. Base(int j) { } C. Base(int j, int k) { } D. Base(int j, int k, int l) { }

Base() { } Base(int j, int k) { }

7. Consider the following class definition: 1. public class Test extends Base { 2. public Test(int j) { 3. } 4. public Test(int j, int k) { 5. super(j, k); 6. } 7. } Which of the following forms of constructor must exist explicitly in the definition of the Base class? Assume Test and Base are in the same package. (Choose all that apply.) A. Base() { } B. Base(int j) { } C. Base(int j, int k) { } D. Base(int j, int k, int l) { }

A, E, F

7. Consider the following class hierarchy and code fragment: 1. try { 2. // assume s is previously defined 3. URL u = new URL(s); 4. // in is an ObjectInputStream 5. Object o = in.readObject(); 6. System.out.println("Success"); 7. } 8. catch (MalformedURLException e) { 9. System.out.println("Bad URL"); 10. } 11. catch (StreamCorruptedException e) { 12. System.out.println("Bad file contents"); 13. } 14. catch (Exception e) { 15. System.out.println("General exception"); 16. } 17. finally { 18. System.out.println("Doing finally part"); 19. } 20. System.out.println("Carrying on"); What lines are output if the constructor at line 3 throws a MalformedURLException? (Choose all that apply.) A. Success B. Bad URL C. Bad file contents D. General exception E. Doing finally part F. Carrying on

C

7. Consider the following code. Which line will not compile? 1. Object ob = new Object(); 2. String[] stringarr = new String[50]; 3. Float floater = new Float(3.14f); 4. ob = stringarr; 5. ob = stringarr[5]; 6. floater = ob; 7. ob = floater; A. Line 4 B. Line 5 C. Line 6 D. Line 7

Line 6

7. Consider the following code. Which line will not compile? 1. Object ob = new Object(); 2. String[] stringarr = new String[50]; 3. Float floater = new Float(3.14f); 4. ob = stringarr; 5. ob = stringarr[5]; 6. floater = ob; 7. ob = floater; A. Line 4 B. Line 5 C. Line 6 D. Line 7

D

7. Given the following code, and making no other changes, which combination of access modifiers (public, protected, or private) can legally be placed before aMethod() on line 3 and be placed before aMethod() on line 8? 1. class SuperDuper 2. { 3. void aMethod() { } 4. } 5. 6. class Sub extends SuperDuper 7. { 8. void aMethod() { } 9. } A. line 3: public; line 8: private B. line 3: protected; line 8: private C. line 3: default; line 8: private D. line 3: private; line 8: protected E. line 3: public; line 8: protected

C, D, E

7. Given the following: public class Food { } public class Fruit extends Food { } public class Citrus extends Fruit { } public class Pomelo extends Citrus { } public class SuperDuper { public Fruit feedMe() { return new Fruit(); } } public class Subby extends SuperDuper { public ????? feedMe() { return new Pomelo (); } } Which of the following are legal return types for feedMe() in class Subby? A. Object B. Food C. Fruit D. Citrus E. Pomelo

A

7. In the following code fragment, after execution of line 1, sbuf references an instance of the StringBuffer class. After execution of line 2, sbuf still references the same instance. 1. StringBuffer sbuf = new StringBuffer("abcde"); 2. sbuf.append("xyz"); A. True B. False

True

7. In the following code fragment, after execution of line 1, sbuf references an instance of the StringBuffer class. After execution of line 2, sbuf still references the same instance. 1. StringBuffer sbuf = new StringBuffer("abcde"); 2. sbuf.append("xyz"); A. True B. False

B

7. What results from running the following code? 1. public class Xor { 2. public static void main(String args[]) { 3. byte b = 10; // 00001010 binary 4. byte c = 15; // 00001111 binary 5. b = (byte)(b ^ c); 6. System.out.println("b contains " + b); 7. } 8. } A. The output: b contains 10 B. The output: b contains 5 C. The output: b contains 250 D. The output: b contains 245

E

8. Consider the following class hierarchy and code fragment: 1. try { 2. // assume s is previously defined 3. URL u = new URL(s); 4. // in is an ObjectInputStream 5. Object o = in.readObject(); 6. System.out.println("Success"); 7. } 8. catch (MalformedURLException e) { 9. System.out.println("Bad URL"); 10. } 11. catch (StreamCorruptedException e) { 12. System.out.println("Bad file contents"); 13. } 14. catch (Exception e) { 15. System.out.println("General exception"); 16. } 17. finally { 18. System.out.println("Doing finally part"); 19. } 20. System.out.println("Carrying on"); What lines are output if the method at line 5 throws an OutOfMemoryError? (Choose all that apply.) A. Success B. Bad URL C. Bad file contents D. General exception E. Doing finally part F. Carrying on

D

8. Consider the following code: 1. Dog rover, fido; 2. Animal anim; 3. 4. rover = new Dog(); 5. anim = rover; 6. fido = (Dog)anim; Which of the following statements is true? (Choose one.) A. Line 5 will not compile. B. Line 6 will not compile. C. The code will compile but will throw an exception at line 6. D. The code will compile and run. E. The code will compile and run, but the cast in line 6 is not required and can be eliminated.

A, B, C, E

8. Consider the following definition: 1. public class Outer { 2. public int a = 1; 3. private int b = 2; 4. public void method(final int c) { 5. int d = 3; 6. class Inner { 7. private void iMethod(int e) { 8. 9. } 10. } 11. } 12. } Which variables can be referenced at line 8? (Choose all that apply.) A. a B. b C. c D. d E. e

E

8. Given the following class: class A extends java.util.Vector { private A(int x) { super(x); } } Which statements are true? A. The compiler creates a default constructor with public access. B. The compiler creates a default constructor with protected access. C. The compiler creates a default constructor with default access. D. The compiler creates a default constructor with private access. E. The compiler does not create a default constructor.

The compiler does not create a default constructor.

8. Given the following class: class A extends java.util.Vector { private A(int x) { super(x); } } Which statements are true? A. The compiler creates a default constructor with public access. B. The compiler creates a default constructor with protected access. C. The compiler creates a default constructor with default access. D. The compiler creates a default constructor with private access. E. The compiler does not create a default constructor.

A

8. How can you force garbage collection of an object? A. Garbage collection cannot be forced. B. Call System.gc(). C. Call System.gc(), passing in a reference to the object to be garbage-collected. D. Call Runtime.gc(). E. Set all references to the object to new values (null, for example).

C

8. What results from attempting to compile and run the following code? 1. public class Conditional { 2. public static void main(String args[]) { 3. int x = 4; 4. System.out.println("value is " + 5. ((x > 4) ? 99.99 : 9)); 6. } 7. } A. The output: value is 99.99 B. The output: value is 9 C. The output: value is 9.0 D. A compiler error at line 5

D

8. Which modifier or modifiers should be used to denote a variable that should not be written out as part of its class's persistent state? (Choose the shortest possible answer.) A. private B. protected C. private protected D. transient E. volatile

transient

8. Which modifier or modifiers should be used to denote a variable that should not be written out as part of its class's persistent state? (Choose the shortest possible answer.) A. private B. protected C. private protected D. transient E. volatile

E

8. Which of the following is true? A. Readers have methods that can read and return floats and doubles. B. Readers have methods that can read and return floats. C. Readers have methods that can read and return doubles. D. Readers have methods that can read and return ints. E. None of the above.

A

8. Which of the following methods in the Thread class are deprecated? A. suspend() and resume() B. wait() and notify() C. start() and stop() D. sleep() and yield()

suspend() and resume()

8. Which of the following methods in the Thread class are deprecated? A. suspend() and resume() B. wait() and notify() C. start() and stop() D. sleep() and yield()

E

9. Consider the following code: 1. Cat sunflower; 2. Washer wawa; 3. SwampThing pogo; 4. 5. sunflower = new Cat(); 6. wawa = sunflower; 7. pogo = (SwampThing)wawa; Which of the following statements is true? (Choose one.) A. Line 6 will not compile; an explicit cast is required to convert a Cat to a Washer. B. Line 7 will not compile, because you cannot cast an interface to a class. C. The code will compile and run, but the cast in line 7 is not required and can be eliminated. D. The code will compile but will throw an exception at line 7, because runtime conversion from an interface to a class is not permitted. E. The code will compile but will throw an exception at line 7, because the runtime class of wawa cannot be converted to type SwampThing.

The code will compile but will throw an exception at line 7, because the runtime class of wawa cannot be converted to type SwampThing.

9. Consider the following code: 1. Cat sunflower; 2. Washer wawa; 3. SwampThing pogo; 4. 5. sunflower = new Cat(); 6. wawa = sunflower; 7. pogo = (SwampThing)wawa; Which of the following statements is true? (Choose one.) A. Line 6 will not compile; an explicit cast is required to convert a Cat to a Washer. B. Line 7 will not compile, because you cannot cast an interface to a class. C. The code will compile and run, but the cast in line 7 is not required and can be eliminated. D. The code will compile but will throw an exception at line 7, because runtime conversion from an interface to a class is not permitted. E. The code will compile but will throw an exception at line 7, because the runtime class of wawa cannot be converted to type SwampThing.

B

9. In the following code fragment, line 4 is executed. 1. String s1 = "xyz"; 2. String s2 = new String(s1); 3. if (s1 == s2) 4. System.out.println("Line 4"); A. True B. False

False

9. In the following code fragment, line 4 is executed. 1. String s1 = "xyz"; 2. String s2 = new String(s1); 3. if (s1 == s2) 4. System.out.println("Line 4"); A. True B. False

C

9. This question concerns the following class definition: 1. package abcde; 2. 3. public class Bird { 4. protected static int referenceCount = 0; 5. public Bird() { referenceCount++; } 6. protected void fly() { /* Flap wings, etc. */ } 7. static int getRefCount() { return referenceCount; } 8. } Which statement is true about class Bird and the following class Parrot? 1. package abcde; 2. 3. class Parrot extends abcde.Bird { 4. public void fly() { 5. /* Parrot-specific flight code. */ 6. } 7. public int getRefCount() { 8. return referenceCount; 9. } 10. } A. Compilation of Parrot.java fails at line 4 because method fly() is protected in the superclass, and classes Bird and Parrot are in the same package. B. Compilation of Parrot.java fails at line 4 because method fly() is protected in the superclass and public in the subclass, and methods may not be overridden to be more public. C. Compilation of Parrot.java fails at line 7 because method getRefCount() is static in the superclass, and static methods may not be overridden to be nonstatic. D. Compilation of Parrot.java succeeds, but a runtime exception is thrown if method fly() is ever called on an instance of class Parrot. E. Compilation of Parrot.java succeeds, but a runtime exception is thrown if method getRefCount() is ever called on an instance of class Parrot.

Compilation of Parrot.java fails at line 7 because method getRefCount() is static in the superclass, and static methods may not be overridden to be nonstatic.

9. This question concerns the following class definition: 1. package abcde; 2. 3. public class Bird { 4. protected static int referenceCount = 0; 5. public Bird() { referenceCount++; } 6. protected void fly() { /* Flap wings, etc. */ } 7. static int getRefCount() { return referenceCount; } 8. } Which statement is true about class Bird and the following class Parrot? 1. package abcde; 2. 3. class Parrot extends abcde.Bird { 4. public void fly() { 5. /* Parrot-specific flight code. */ 6. } 7. public int getRefCount() { 8. return referenceCount; 9. } 10. } A. Compilation of Parrot.java fails at line 4 because method fly() is protected in the superclass, and classes Bird and Parrot are in the same package. B. Compilation of Parrot.java fails at line 4 because method fly() is protected in the superclass and public in the subclass, and methods may not be overridden to be more public. C. Compilation of Parrot.java fails at line 7 because method getRefCount() is static in the superclass, and static methods may not be overridden to be nonstatic. D. Compilation of Parrot.java succeeds, but a runtime exception is thrown if method fly() is ever called on an instance of class Parrot. E. Compilation of Parrot.java succeeds, but a runtime exception is thrown if method getRefCount() is ever called on an instance of class Parrot.

B

9. What does the following code do? Integer i = null; if (i != null & i.intValue() == 5) System.out.println("Value is 5"); A. Prints "Value is 5". B. Throws an exception.

D

9. What is the range of values that can be assigned to a variable of type short? A. Depends on the underlying hardware B. 0 through 216 − 1 C. 0 through 232 − 1 D. −215 through 215 − 1 E. −231 through 231 − 1

B, C, D

9. Which of the following are appropriate situations for assertions? A. Preconditions of a public method B. Postconditions of a public method C. Preconditions of a private method D. Postconditions of a private method

B

9. Which of the following statements about threads is true? A. Every thread starts executing with a priority of 5. B. Threads inherit their priority from their parent thread. C. Threads are guaranteed to run with the priority that you set using the setPriority() method. D. Thread priority is an integer ranging from 1 to 100.

Threads inherit their priority from their parent thread.

9. Which of the following statements about threads is true? A. Every thread starts executing with a priority of 5. B. Threads inherit their priority from their parent thread. C. Threads are guaranteed to run with the priority that you set using the setPriority() method. D. Thread priority is an integer ranging from 1 to 100.

A

9. Which of the following statements are true? (Choose all that apply.) A. Given that Inner is a nonstatic class declared inside a public class Outer and that appro- priate constructor forms are defined, an instance of Inner can be constructed like this: new Outer().new Inner() B. If an anonymous inner class inside the class Outer is defined to implement the interface ActionListener, it can be constructed like this: new Outer().new ActionListener() C. Given that Inner is a nonstatic class declared inside a public class Outer and that appro- priate constructor forms are defined, an instance of Inner can be constructed in a static method like this: new Inner() D. An anonymous class instance that implements the interface MyInterface can be constructed and returned from a method like this: 1. return new MyInterface(int x) { 2. int x; 3. public MyInterface(int x) { 4. this.x = x;5. }6. };

Given that Inner is a nonstatic class declared inside a public class Outer and that appro- priate constructor forms are defined, an instance of Inner can be constructed like this: new Outer().new Inner()

9. Which of the following statements are true? (Choose all that apply.) A. Given that Inner is a nonstatic class declared inside a public class Outer and that appro- priate constructor forms are defined, an instance of Inner can be constructed like this: new Outer().new Inner() B. If an anonymous inner class inside the class Outer is defined to implement the interface ActionListener, it can be constructed like this: new Outer().new ActionListener() C. Given that Inner is a nonstatic class declared inside a public class Outer and that appro- priate constructor forms are defined, an instance of Inner can be constructed in a static method like this: new Inner() D. An anonymous class instance that implements the interface MyInterface can be constructed and returned from a method like this: 1. return new MyInterface(int x) { 2. int x; 3. public MyInterface(int x) { 4. this.x = x;5. }6. };

A, B

9. Which of the following types are legal arguments of a switch statement? A. enums B. bytes C. longs D. floats E. strings

enums bytes

9. Which of the following types are legal arguments of a switch statement? A. enums B. bytes C. longs D. floats E. strings

E

9. You execute the following code in an empty directory. What is the result? 1. File f1 = new File("dirname"); 2. File f2 = new File(f1, "filename"); A. A new directory called dirname is created in the current working directory. B. A new directory called dirname is created in the current working directory. A new file called filename is created in directory dirname. C. A new directory called dirname and a new file called filename are created, both in the current working directory. D. A new file called filename is created in the current working directory. E. No directory is created, and no file is created.

No directory is created, and no file is created.

9. You execute the following code in an empty directory. What is the result? 1. File f1 = new File("dirname"); 2. File f2 = new File(f1, "filename"); A. A new directory called dirname is created in the current working directory. B. A new directory called dirname is created in the current working directory. A new file called filename is created in directory dirname. C. A new directory called dirname and a new file called filename are created, both in the current working directory. D. A new file called filename is created in the current working directory. E. No directory is created, and no file is created.

A

A StringBuffer is slower than a StringBuilder, but a StringBuffer is threadsafe. A ⦁ True B ⦁ False

You cannot specify which thread will get notified.

A monitor called mon has 10 threads in its waiting pool; all these waiting threads have the same priority. One of the threads is thr1. How can you notify thr1 so that it alone moves from the Waiting state to the Ready state? (Choose one.) a. Execute notify(thr1); from within synchronized code of mon. b. Execute mon.notify(thr1); from synchronized code of any object. c. Execute thr1.notify(); from synchronized code of any object. d. Execute thr1.notify(); from any code (synchronized or not) of any object. e. You cannot specify which thread will get notified.

False

A signed data type has an equal number of non-zero positive and negative values available. a. True b. False

False

A thread wants to make a second thread ineligible for execution. To do this, the first thread can call the yield() method on the second thread. a. True b. False

Compilation of class B will fail. Compilation of class A will succeed.

Class 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. } Which is true about the two classes? (Choose one.) 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.

C

Consider the following code: 1. StringBuffer sbuf = new StringBuffer(); 2. sbuf = null; 3. System.gc(); Choose true statement. (Choose one.) a. After line 2 executes, the StringBuffer object is garbage collected. b. After line 3 executes, the StringBuffer object is garbage collected. c. After line 2 executes, the StringBuffer object is eligible for garbage collection. d. After line 3 executes, the StringBuffer object is eligible for garbage collection.

The code will compile and run.

Consider the following code: 1. Dog rover, fido; 2. Animal anim; 3. 4. rover = new Dog(); 5. anim = rover; 6. fido = (Dog)anim; Where: Mammal extends Animal Dog extends Mammal Which of the following statements is true? (Choose one.) a. Line 5 will not compile. b. Line 6 will not compile. c. The code will compile but will throw an exception at line 6. d. The code will compile and run. e. The code will compile and run, but the cast in line 6 is not required and can be eliminated.

x[24] is 0 x.length is 25

Consider the following line of code: (49) int[] x = new int[25]; After execution, which statements are true? (Choose two.) a. x[24] is 0 b. x[24] is undefined c. x[25] is 0 d. x[0] is null e. x.length is 25

Attempting to add anything other than a string to fancyVec results in a compiler error.

Give the following declarations: Vector plainVec; Vector<String> fancyVec; If you want a vector in which you know you will only store strings, what are the advantages of using fancyVec rather than plainVec? A. Attempting to add anything other than a string to fancyVec results in a compiler error. B. Attempting to add anything other than a string to fancyVec causes a runtime exception to be thrown. C. Attempting to add anything other than a string to fancyVec causes a checked exception to be thrown. D. Adding a string to fancyVec takes less time than adding one to plainVec. E. The methods of fancyVec are synchronized.

Compilation fails.

Give: 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? (Choose one.) 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.

None of the above

Given a string constructed by calling s = new String("xyzzy"), which of the calls modifies the string? (Choose one.) a. s.append("aaa"); b. s.trim(); c. s.substring(3); d. s.replace('z', 'a'); e. s.concat(s); f. None of the above

java.util.Arrays.equals(a1, a2);

Given arrays a1 and a2, which call returns true if a1 and a2 have the same length, and a1[i].equals(a2[i]) for every legal index i? (Choose one.) a. java.util.Arrays.equals(a1, a2); b. java.util.Arrays.compare(a1, a2); c. java.util.List.compare(a1, a2);

public int hashCode() { return a; } public int hashCode() { return (int)Math.random(); }

Given the following class: class Xyzzy { int a, b; public boolean equals(Object x) { Xyzzy that = (Xyzzy)x; return this.a == that.a; } Which methods below honor the hash code contract? A. public int hashCode() { return a; } B. public int hashCode() { return b; } C. public int hashCode() { return a+b; } D. public int hashCode() { return a*b; } E. public int hashCode() { return (int)Math.random(); }

public void swim() { } size = 12;

Given the following class: package ocean; public class Fish { protected int size; protected void swim() { } } Which of the following may appear in a subclass of Fish named Tuna that is not in the ocean package? A. void swim() { }; B. public void swim() { }; C. size = 12; D. (new Tuna()).size = 12;

line 3: private; line 8: protected

Given the following code, and making no other changes, which combination of access modifiers (public, protected, or private) can legally be placed before aMethod() on line 3 and be placed before aMethod() on line 8? (Choose one.) 1. class SuperDuper 2. { 3. void aMethod() { } 4. } 5. 6. class Sub extends SuperDuper 7. { 8. void aMethod() { } 9. } a. line 3: public; line 8: private b. line 3: protected; line 8: private c. line 3: default; line 8: private d. line 3: private; line 8: protected e. line 3: public; line 8: protected

D

Given the following code, what is the expected outcome? public class Test { public static void main(String [] a) { int [] b = [1,2,3,4,5,6,7,8,9,0]; System.out.println("a[2]=" + a[2]); } } A ⦁ The code compiles but does not output anything. B ⦁ "a[2]=3" is printed out to the console. C ⦁ "a[2]=2" is printed out to the console. D ⦁ The code does not compile. E ⦁ None of the above.

C

Given the following code, what keyword must be used at line 4 in order to stop execution of the for loop? boolean b = true; for (;;) { if (b) { <insert code> } // do something } A ⦁ stop B ⦁ continue C ⦁ break D ⦁ None of the above

C

Given the following code, what will be the outcome? public class Funcs extends java.lang.Math { public int add(int x, int y) { return x + y; } public int sub(int x, int y) { return x - y; } public static void main(String [] a) { Funcs f = new Funcs(); System.out.println("" + f.add(1, 2)); } } A ⦁ The code compiles but does not output anything. B ⦁ "3" is printed out to the console. C ⦁ The code does not compile. D ⦁ None of the above.

A

Given the following code, which of the results that follow would you expect? 1. package mail; 2. interface Box { protected void open(); void close(); public void empty(); } A ⦁ The code will not compile because of line 4. B ⦁ The code will not compile because of line 5. C ⦁ The code will not compile because of line 6. D ⦁ The code will compile

super(); this(1.23f);

Given the following code: 1. class Xyz { 2. float f; 3. Xyz() { 4. ??? // What goes here? 5. } 6. Xyz(float f) { 7. this.f = f; 8. } 9. } What code at line 4 results in a class that compiles? A. super(); B. this(1.23f); C. this(1.23f); super(); D. super(1.23f); this(1.23f);

public interface B extends A { }

Given: 1. public interface A { 2. String DEFAULT_GREETING = "Hello World"; 3. public void method1(); 4. } A programmer wants to create an interface called B that has A as its parent. Which interface declaration is correct? (Choose one.) a. public interface B extends A { } b. public interface B implements A {} c. public interface B instanceOf A {} d. public interface B inheritsFrom A { }

StackOverflowError

Given: 10. public class ClassA { 11. public void count(int i) { 12. count(++i); 13. } 14. } And: 20. ClassA a = new ClassA(); 21. a.count(3); Which exception or error should be thrown by the virtual machine? (Choose one.) a. StackOverflowError b. NullPointerException c. NumberFormatException d. IllegalArgumentException e. ExceptionlnlnitializerError

Compilation fails.

Given: 11. String test = "This is a test"; 12. String[] tokens = test.split("\s"); 13. System.out.println(tokens.length); What is the result? (Choose one.) a. 0 b. 1 c. 4 d. Compilation fails. e. An exception is thrown at runtime.

Shape s = new Circle(); s.setAnchor(10,10); s.draw();

Given: 11. public abstract class Shape { 12. int x; 13. int y; 14. public abstract void draw(); 15. public void setAnchor(int x, int y) { 16. this.x = x; 17. this.y = y; 18. } 19. } and a class Circle that extends and fully implements the Shape class. Which is correct? (Choose one.) a. Shape s = new Shape(); s.setAnchor(10,10); s.draw(); b. Circle c = new Shape(); c.setAnchor(10,10); c.draw(); c. Shape s = new Circle(); s.setAnchor(10,10); s.draw(); d. Shape s = new Circle(); s->setAnchor(10,10); s->draw(); e. Circle c = new Circle(); c.Shape.setAnchor(10,10); c.Shape.draw();

Compilation fails.

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? (Choose one.) a. Test b. Exception c. Compilation fails. d. NullPointerException

passed An AssertionError is thrown with the word "stuff" added to the stack trace.

Given: 12. public class AssertStuff { 14. public static void main(String [] args) { 15. int x= 5; 16. int y= 7; 18. assert (x > y): "stuff"; 19. System.out.println("passed"); 20. } 21. } And these command line invocations: java AssertStuff java -ea AssertStuff What is the result? (Choose one.) a. Passed Stuff b. Stuff Passed c. passed An AssertionError is thrown with the word "stuff" added to the stack trace. d. passed An AssertionError is thrown without the word "stuff" added to the stack trace. e. passed An AssertionException is thrown with the word "stuff" added to the stack trace. f. passed An AssertionException is thrown without the word "stuff" added to the stack trace.

The ownerName variable breaks encapsulation.

Given: 20. public class CreditCard { 22. private String cardlD; 23. private Integer limit; 24. public String ownerName; 26. public void setCardlnformation(String cardlD, 27. String ownerName, 28. Integer limit) { 29. this.cardlD = cardlD; 30. this.ownerName = ownerName; 31. this.limit = limit; 32. } 33. } Which is true? (Choose one.) a. The class is fully encapsulated. b. The code demonstrates polymorphism. c. The ownerName variable breaks encapsulation. d. The cardlD and limit variables break polymorphism. e. The setCardlnformation method breaks encapsulation.

Compilation fails due to an error in line 29.

Given: 23. Object [] myObjects = { 24. new Integer(12), 25. new String("foo"), 26. new Integer(5), 27. new Boolean(true) 28. }; 29. java.util.Array.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? (Choose one.) 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.

Line 57 will print the value 3.

Given: 55. int []x= {1, 2,3,4, 5}; 56. int y[] =x; 57. System.out.println(y[2]); Which is true? (Choose one.) 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.

Line.Point p = new Line.Point();

Given: 10. class Line { 11. public static class Point { } 12. } 13. 14. class Triangle { 15. // insert code here 16. } Which code, inserted at line 15, creates an instance of the Point class defined in Line? (Choose one.) a. Point p = new Point(); b. Line.Point p = new Line.Point(); c. The Point class cannot be instatiated at line 15. d. Line l = new Line() ; Point p = new l.Point();

Nav.Direction d = Nav.Direction.NORTH;

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? (Choose one.) a. Direction d = NORTH; b. Nav.Direction d = NORTH; c. Direction d = Direction.NORTH; d. Nav.Direction d = Nav.Direction.NORTH;

new Foo() { public int bar(){return 1; } }

Given: 10. interface Foo { int bar(); } 11. public class Sprite { 12. public int fubar( Foo foo) { return foo.bar(); } 13. public void testFoo() { 14. fubar( 15. // insert code here 16. ); 17. } 18. } Which code, inserted at line 15, allows the class Sprite to compile? (Choose one.) a. Foo { public int bar() { return 1; } } b. new Foo { public int bar() { return 1; } } c. new Foo() { public int bar(){return 1; } } d. new class Foo { public int bar() { return 1; } }

1 2 3

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? (Choose one.) 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.

A Exception

Given: class A { public void process() { System.out.print("A "); } public static void main(String[] args) { try { ((A)new B()).process(); } catch (Exception e) { System.out.print("Exception "); } } } class B extends A { public void process() throws RuntimeException { super.process(); if (true) throw new RuntimeException(); System.out.print("B"); } } What is the result? (Choose one.) a. Exception b. A Exception c. A Exception B d. A B Exception e. Compilation fails because of an error in line: public void process() throws RuntimeException f. Compilation fails because of an error in line: try { ((A)new B()).process(); }

There is no single technique that can guarantee non-deadlocking code

How can you ensure that multithreaded code does not deadlock? (Choose one.) a. Synchronize access to all shared variables. b. Make sure all threads yield from time to time. c. Vary the priorities of your threads. d. There is no single technique that can guarantee non-deadlocking code.

D

How many bits does a float contain? A. 1 B. 8 C. 16 D. 32 E. 64

8

How many bytes does the following code write to file dest? (Choose one.) 1. try { 2. FileOutputStream fos = newFileOutputStream("dest"); 3. DataOutputStream dos = new DataOutputStream(fos); 4. dos.writeInt(3); 5. dos.writeFloat(0.0001f); 6. dos.close(); 7. fos.close(); 8. } 9. catch (IOException e) { } a. 2 b. 8 c. 12 d. 16 e. The number of bytes depends on the underlying system.

TreeSet

If you need a Set implementation that provides value-ordered iteration, which class should you use? (Choose one.) a. HashSet b. LinkedHashSet c. TreeSet

True

In the following code fragment, after execution of line 1, sbuf references an instance of the StringBuffer class. After execution of line 2, sbuf still references the same instance. 1. StringBuffer sbuf = new StringBuffer("FPT"); 2. sbuf.append("-University"); a. True b. False

True

In the following code fragment, after execution of line 1, sbuf references an instance of the StringBuffer class. After execution of line 2, sbuf still references the same instance. 1. StringBuffer sbuf = new StringBuffer("FPT"); 2. sbuf.insert(3, "-University"); a. True b. False

E

In the following code, what are the possible types for variable result? (Choose the most complete true answer.) 1. byte b = 11; 2. short s = 13; 3. result = b * ++s; a. byte, short, int, long, float, double b. boolean, byte, short, char, int, long, float, double c. byte, short, char, int, long, float, double d. byte, short, char e. int, long, float, double

B

N=106 (4926) public class Test{ public static void main(String[] args){ String s1 = "xyz"; String s2 = new String(s1); s2.intern(); if (s1 == s2) System.out.println("Line 4"); if (s1.equals(s2)) System.out.println("Line 6"); } } What is the output? a. Line 4 b. Line 6 c. Line 4 Line 6 d. No output, compile error e. No output

A

QN=100 (305) MVC is short call of a. Model-View-Controller b. Multiple-View-Controller c. Metal-View-Controller

Model-View-Controller

QN=100 (305) MVC is short call of a. Model-View-Controller b. Multiple-View-Controller c. Metal-View-Controller

B

QN=101 (129) public class Test{ public static void main(String[] args){ byte b = 2; byte b1 = 3; b = b * b1; System.out.println("b="+b); } } What is the output? a. b=6 b. No output because of compile error at line: b = b * b1; c. No output because of compile error at line: System.out.println("b="+b); d. No output because of compile error at line: byte b = 2; e. No output because of compile error at line: byte b = 3;

No output because of compile error at line: b = b * b1;

QN=101 (129) public class Test{ public static void main(String[] args){ byte b = 2; byte b1 = 3; b = b * b1; System.out.println("b="+b); } } What is the output? a. b=6 b. No output because of compile error at line: b = b * b1; c. No output because of compile error at line: System.out.println("b="+b); d. No output because of compile error at line: byte b = 2; e. No output because of compile error at line: byte b = 3;

Have a nice day!

QN=102 (77) public class Test{ public static void main(String[] args){ Object ob1= new Object(); Object ob2= new Object(); if(ob1.equals(ob2)) System.out.println("ob1 equals ob2"); if(ob1==ob2) System.out.println("ob1==ob2"); System.out.println("Have a nice day!"); } } What is the output? a. ob1 equals ob2 Have a nice day! b. ob1==ob2 Have a nice day! c. Have a nice day! d. No output

C

QN=102 (77) public class Test{ public static void main(String[] args){ Object ob1= new Object(); Object ob2= new Object(); if(ob1.equals(ob2)) System.out.println("ob1 equals ob2"); if(ob1==ob2) System.out.println("ob1==ob2"); System.out.println("Have a nice day!"); } } What is the output? a. ob1 equals ob2 Have a nice day! b. ob1==ob2 Have a nice day! c. Have a nice day! d. No output

A

QN=103 (4924) public class Test{ public static void main(String[] args){ Object ob1= new Object(); Object ob2= ob1; if(ob1.equals(ob2)) System.out.println("ob1 equals ob2"); if(ob1==ob2) System.out.println("ob1==ob2"); System.out.println("Have a nice day!"); } } What is the output? a. ob1 equals ob2 ob1==ob2 Have a nice day! b. ob1 equals ob2 Have a nice day! c. ob1==ob2 Have a nice day! d. None of the above

ob1 equals ob2 ob1==ob2 Have a nice day!

QN=103 (4924) public class Test{ public static void main(String[] args){ Object ob1= new Object(); Object ob2= ob1; if(ob1.equals(ob2)) System.out.println("ob1 equals ob2"); if(ob1==ob2) System.out.println("ob1==ob2"); System.out.println("Have a nice day!"); } } What is the output? a. ob1 equals ob2 ob1==ob2 Have a nice day! b. ob1 equals ob2 Have a nice day! c. ob1==ob2 Have a nice day! d. None of the above

C

QN=107 (220) public class Test{ public static void main(String[] args){ String s1 = "xyz"; String s2 = new String(s1); s2=s2.intern(); if (s1 == s2) System.out.println("Line 4"); if (s1.equals(s2)) System.out.println("Line 6"); } } What is the output? (choose 1) a. Line 4 b. Line 6 c. Line 4 Line 6 d. No output, compile error e. No output

D

QN=108 (4941) Select correct statement about RMI. (choose 1) a. allow programmers to develop distributed Java programs with the same syntax and semantics used for non-distributed programs. b. use object serialization to marshal and unmarshal parameters, supporting true object-oriented polymorphism. c. RMI applications are often comprised of two separate programs: a server and a client. d. All the above

D

QN=109 (4943) Select correct statement(s) about remote class.(choose one) a. It must extend java.rmi.server.UnicastRemoteObject. b. It must implement the remote interface. c. It is the class whose methods provide services to clients. d. All the others choices

B, E, F

QN=11 (1417) 10. interface Foo { 11. int bar(); 12. } 13. 14. public class Beta { 15. 16. class A implements Foo { 17. public int bar() { return 1; } 18. } 19. 20. public int fubar( Foo foo) { return foo.bar(); } 21. 22. public void testFoo() { 23. 24. class A implements Foo { 25. public int bar() { return 2; } 26. } 27. 28. System.out.println( fubar( new A())); 29. } 30. 31. public static void main( String[] argv) { 32. new Beta().testFoo(); 33. } 34. } Which three statements are true? (Choose three.) a. Compilation fails. b. The code compiles and the output is 2. c. If lines 16, 17 and 18 were removed, compilation would fail. d. If lines 24, 25 and 26 were removed, compilation would fail. e. If lines 16, 17 and 18 were removed, the code would compile and the output would be 2. f. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1.

E

QN=110 (4942) Select correct statements about remote interface. (choose 1) a. A remote interface is an interface that describes the remotely accessible methods of a remote object. b. All remote interfaces must extend java.rmi.Remote. c. All methods in a remote interface must throw java.rmi.RemoteException d. The type of a remote reference is a remote interface e. All the others choices

C

QN=111 (4939) Select INCORRECT statement about serialization. (choose 1) a. The process of writing an object is called serialization. b. To serialize an object, first create an instance of java.io.ObjectOutputStream. c. When an Object Output Stream serializes an object that contains references to another object, every referenced object is not serialized along with the original object. d. When an object is serialized, it will probably be deserialized by a different JVM.

B

QN=112 (4940) Select INCORRECT statement about deserialize. (choose 1) a. Any JVM that tries to deserialize an object must have access to that object's class definition. b. We use readObject() method of ObjectOutputStream class to deserialize. c. The readObject method deserializes the next object in the stream and traverses its references to other objects recursively to deserialize all objects that are reachable from it.

D

QN=113 (4944) Select incorrect statement about RMI server.(choose 1) a. An RMI server is an application that creates one or more remote objects and makes them available to clients. b. An RMI server performs two tasks: 1. Create an instance of the remote object. 2. Bind the remote object to a name. c. The RMI registry is a program that associates names with RMI services. A server specifies a name for every remote object it provides. d. A client accesses a remote object by specifying only the server name.

C

QN=114 (4938) Select incorrect statement about ServerSocket class. (choose 1) a. The most useful form of the ServerSocket constructor is public ServerSocket(int portNumber) b. A server socket, on the other hand, makes itself available and then waits for clients to initiate connections. c. To make the new object available for client connections, call its accept() method, which returns an instance of ServerSocket d. There is no way to know how long the accept() call will take.

A

QN=115 (4937) Select incorrect statement about Socket class. (choose 1) a. The java.net.Socket class contains code that knows how to find and communicate with a server through UDP. b. One of its constructors is: public Socket(String servername, int portNumber) c. You do not directly read from or write to a socket d. The java.net.Socket class contains code that knows how to find and communicate with a server through TCP.

A

QN=116 (7257) Select the correct statement about JDBC two-tier processing model. a. A user's commands are delivered to the database or other data source, and the results of those statements are sent back to the user. b. Two-tier is referred to as a client/server configuration, with the user's machine as the Server, and the machine housing the data source as the Client. c. User's commands are sent to a "middle tier" of services, which then sends the commands to the data source.

D

QN=117 (7265) SQL keyword is followed by the selection criteria that specify the rows to select in a query a. FROM b. ORDER BY c. HAVING d. WHERE

B

QN=118 (7262) Statement objects return SQL query results as objects a. DataSet b. ResultSet c. RecordSet d. RowSet

C

QN=119 (4927) Study the statements: 1)When a JDBC connection is created, it is in auto-commit mode 2)Once auto-commit mode is disabled, no SQL statements will be committed until you call the method commit explicitly a. Only statement 1 is true b. Only statement 2 is true c. Both 1 and 2 are true d. Both 1 and 2 are not true

D

QN=12 (1527) 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. 23. public String getValue() { 24. return classC.getValue(); 25. } 26.} And: 30. class ClassC { 31. public String value; 32. 33. public String getValue() { 34. value = "ClassB"; 35. return value; 36. } 37.} ClassA a = new ClassA(); a.methodA(); What is the result? (Choose one.) a. Compilation fails. b. ClassC is displayed. c. The code runs with no output. d. An exception is thrown at runtime.

C, D

QN=128 (119) Suppose interface Inty defines five methods. Suppose class Classy declares that it implements Inty but does not provide implementations for any of the five interface methods. Which are true? (Choose two.) a. The class will not compile. b. The class will compile if it is declared public. c. The class will compile if it is declared abstract. d. The class may not be instantiated.

D

QN=120 (160) Suppose a method called finallyTest() consists of a try block, followed by a catch block, followed by a finally block. Assuming the JVM doesn't crash and the code does not execute a System.exit() call, under what circumstances will the finally block not begin to execute? (Choose one.) a. The try block throws an exception, and the catch block also throws an exception. b. The try block throws an exception that is not handled by the catch block. c. The try block throws an exception, and the catch block calls finallyTest() in a way that causes another exception to be thrown. d. If the JVM doesn't crash and the code does not execute a System.exit() call, the finally block will always execute.

A

QN=121 (56) Suppose a source file contains a large number of import statements and one class definition. How do the imports affect the time required to load the class? (Choose one.) a. Class loading takes no additional time. b. Class loading takes slightly more time. c. Class loading takes significantly more time.

B

QN=122 (55) Suppose a source file contains a large number of import statements. How do the imports affect the time required to compile the source file? (Choose one.) a. Compilation takes no additional time. b. Compilation takes slightly more time. c. Compilation takes significantly more time.

C

QN=123 (247) Suppose class A extends Object; Class B extends A; and class C extends B. Of these, only class C implements java.io.Externalizable. Which of the following must be true in order to avoid an exception during deserialization of an instance of C? (Choose one.) a. A must have a no-args constructor. b. B must have a no-args constructor. c. C must have a no-args constructor. d. There are no restrictions regarding no-args constructors.

B

QN=124 (246) Suppose class A extends Object; class B extends A; and class C extends B. Of these, only class C implements java.io.Serializable. Which of the following must be true in order to avoid an exception during deserialization of an instance of C? (Choose one.) a. A must have a no-args constructor. b. B must have a no-args constructor. c. C must have a no-args constructor. d. There are no restrictions regarding no-args constructors.

B

QN=125 (122) Suppose class A has a method called doSomething(), with default access. Suppose class B extends A and overrides doSomething(). Which access modes may not apply to B's version of doSomething()? (Choose one) a. public b. private c. protected d. Default

A, B

QN=126 (117) Suppose class Supe, in package packagea, has a method called doSomething(). Suppose class Subby, in package packageb, overrides doSomething(). What access modes may Subby's version of the method have? (Choose two.) a. public b. protected c. Default d. private

A

QN=127 (190) Suppose class X contains the following method: void doSomething(int a, float b) { ... } Which of the following methods may appear in class Y, which extends X? (Choose one.) a. public void doSomething(int a, float b) { ... } b. private void doSomething(int a, float b) { ... } c. public void doSomething(int a, float b) throws java.io.IOException { ... } d. private void doSomething(int a, float b) throws java.io.IOException { ... }

D

QN=13 11. public class Bootchy { (1515) 12. int bootch; 13. String snootch; 14. 15. public Bootchy() { 16. this("snootchy"); 17. System.out.print("first "); 18. } 19. 20. public Bootchy(String snootch) { 21. this(420, "snootchy"); 22. System.out.print("second "); 23. } 24. 25. public Bootchy(int bootch, String snootch) { 26. this.bootch = bootch; 27. this.snootch = snootch; 28. System.out.print("third "); 29. } 30. 31. public static void main(String[] args) { 32. Bootchy b = new Bootchy(); 33. System.out.print(b.snootch +" " + b.bootch); 34. } 35. } What is the result? (Choose one.) a. snootchy 420 third second first b. snootchy 420 first second third c. first second third snootchy 420 d. third second first snootchy 420 e. third first second snootchy 420 f. first second first third snootchy 420

A

QN=130 (158) Suppose salaries is an array containing floats. Which of the following are valid loop control statements for processing each element of salaries? (Choose one.) a. for (float f:salaries) b. for (int i:salaries) c. for (float f::salaries) d. for (int i::salaries)

A

QN=131 (139) Suppose the declared type of x is a class, and the declared type of y is an interface. When is the assignment x = y; legal? (Choose one.) a. When the type of x is Object b. When the type of x is an array c. Always d. Never

A

QN=132 (140) Suppose the type of xarr is an array of XXX, and the type of yarr is an array of YYY. When is the assignment xarr = yarr; legal? (Choose one.) a. Sometimes b. Always c. Never d. None of the others choices

A

QN=133 (183) Suppose x and y are of type TrafficLightState, which is an enum. What is the best way to test whether x and y refer to the same constant? (Choose one.) a. if (x == y) b. if (x.equals(y)) c. if (x.toString().equals(y.toString())) d. if (x.hashCode() == y.hashCode())

D

QN=134 (245) Suppose you are writing a class that will provide custom deserialization. The class implements java.io.Serializable (not java.io.Externalizable). What access mode should the readObject() method have? (Choose one.) a. public b. protected c. default d. private

D

QN=135 (243) Suppose you are writing a class that will provide custom serialization. The class implements java.io.Serializable (not java.io.Externalizable). What access mode should the writeObject() method have? (Choose one.) a. public b. protected c. default d. private

B

QN=136 (211) Suppose you want to create a custom thread class by extending java.lang.Thread in order to provide some special functionality. Which of the following must you do? (Choose one.) a. Declare that your class implements java.lang.Runnable. b. Override run(). c. Override start(). d. Make sure that all access to all data is via synchronized methods.

A

QN=138 (304) Swing components cannot be combined with AWT components. a. True b. False

A

QN=139 (299) The class is the primary class that has the driver information. a. DriverManager b. Driver c. ODBCDriver d. None of the others

A

QN=14 (306) A dialog prevents user input to other windows in the application unitl the dialog is closed. a. Modal b. Non-Modal c. Unmodel d. None of the above

C

QN=144 (115) This question concerns the following class definition: 1. package abcde; 2. 3. public class Bird { 4. protected static int referenceCount = 0; 5. public Bird() { referenceCount++; } 6. protected void fly() { /* Flap wings, etc. */ } 7. static int getRefCount() { return referenceCount; } 8. } Which statement is true about class Bird and the following class Parrot? (Choose one.) 1. package abcde; 2. 3. class Parrot extends abcde.Bird { 4. public void fly() { 5. /* Parrot-specific flight code. */ 6. } 7. public int getRefCount() { 8. return referenceCount; 9. } 10. } a. Compilation of Parrot.java fails at line 4 because method fly() is protected in the superclass, and classes Bird and Parrot are in the same package. b. Compilation of Parrot.java fails at line 4 because method fly() is protected in the superclass and public in the subclass, and methods may not be overridden to be more public. c. Compilation of Parrot.java fails at line 7 because method getRefCount() is static in the superclass, and static methods may not be overridden to be nonstatic. d. Compilation of Parrot.java succeeds, but a runtime exception is thrown if method fly() is ever called on an instance of class Parrot. e. Compilation of Parrot.java succeeds, but a runtime exception is thrown if method getRefCount() is ever called on an instance of class Parrot.

A

QN=145 (116) This question concerns the following class definition: 1. package abcde; 2. 3. public class Bird { 4. protected static int referenceCount = 0; 5. public Bird() { referenceCount++; } 6. protected void fly() { /* Flap wings, etc. */ } 7. static int getRefCount() { return referenceCount; } 8. } Which statement is true about class Bird and the following class Nightingale? (Choose one.) 1. package singers; 2. 3. class Nightingale extends abcde.Bird { 4. Nightingale() { referenceCount++; } 5. 6. public static void main(String args[]) { 7. System.out.print("Before: " + referenceCount); 8. Nightingale florence = new Nightingale(); 9. System.out.println(" After: " + referenceCount); 10. florence.fly(); 11. } 12. } a. The program will compile and execute. The output will be Before: 0 After: 2. b. The program will compile and execute. The output will be Before: 0 After: 1. c. Compilation of Nightingale will fail at line 4 because static members cannot be overridden. d. Compilation of Nightingale will fail at line 10 because method fly() is protected in the superclass. e. Compilation of Nightingale will succeed, but an exception will be thrown at line 10, because method fly() is protected in the superclass.

A, C, D

QN=146 (191) This question involves IOException, AWTException, and EOFException. They are all checked exception types. IOException and AWTException extend Exception, and EOFException extends OException. Suppose class X contains the following method: void doSomething() throws IOException{ ... } Which of the following methods may appear in class Y, which extends X? (Choose three.) a. void doSomething() { ... } b. void doSomething() throws AWTException { ... } c. void doSomething() throws EOFException { ... } d. void doSomething() throws IOException, EOFException { ... }

A

QN=147 (4930) URL referring to databases use the form: a. protocol:subprotocol:datasoursename b. protocol:datasoursename c. jdbc:odbc:datasoursename d. jdbc:datasoursename

C

QN=148 (142) What are the legal types for whatsMyType? (Choose one.) short s = 10; whatsMyType = !s; a. short b. int c. There are no possible legal types. d. long e. char f. byte

B

QN=150 (238) What does the following code fragment print out at line 9? (Choose one.) 1. FileOutputStream fos = new FileOutputStream("xx"); 2. for (byte b=10; b<50; b++) 3. fos.write(b); 4. fos.close(); 5. RandomAccessFile raf = new RandomAccessFile("xx", "r"); 6. raf.seek(10); 7. int i = raf.read(); 8. raf.close() 9. System.out.println("i = " + i); a. The output is i = 30. b. The output is i = 20. c. The output is i = 10. d. There is no output because the code throws an exception at line 1. e. There is no output because the code throws an exception at line 5.

D

QN=151 (126) What does the following code print? public class A { static int x; public static void main(String[] args) { A that1 = new A(); A that2 = new A(); that1.x = 5; that2.x = 1000; x = -1; System.out.println(x); } } a. 0 b. 5 c. 1000 d. -1

B

QN=152 (251) What happens when you try to compile and run the following application? (Choose one.) 1. import java.io.*; 2. 3. public class Xxx { 4. public static void main(String[] args) { 5. try { 6. File f = new File("xxx.ser"); 7. FileOutputStream fos = new FileOutputStream(f); 8. ObjectOutputStream oos = new ObjectOutputStream(fos); 9. oos.writeObject(new Object()); 10. oos.close(); 11. fos.close(); 12. } 13. catch (Exception x) { } 14. } 15. } a. Compiler error at line 9. b. An exception is thrown at line 9. c. An exception is thrown at line 10. d. No compiler error and no exception.

C

QN=153 (59) What happens when you try to compile and run the following code? public class Q { static String s; public static void main(String[] args) { System.out.println(">>" + s + "<<"); } } a. The code does not compile b. The code compiles, and prints out >><< c. The code compiles, and prints out >>null<<

The code compiles, and prints out >>null<<

QN=153 (59) What happens when you try to compile and run the following code? public class Q { static String s; public static void main(String[] args) { System.out.println(">>" + s + "<<"); } } a. The code does not compile b. The code compiles, and prints out >><< c. The code compiles, and prints out >>null<<

C

QN=154 (230) What happens when you try to compile and run this application? (Choose one.) 1. import java.util.*; 2. 3. public class Apple { 4. public static void main(String[] a) { 5. Set<Apple> set = new TreeSet<Apple>(); 6. set.add(new Apple()); 7. set.add(new Apple()); 8. set.add(new Apple()); 9. } 10. } a. Compiler error. b. An exception is thrown at line 6. c. An exception is thrown at line 7. d. An exception is thrown at line 8. e. No exception is thrown.

C

QN=155 (86) What is -50 >> 2 a. A negative number with very large magnitude. b. A positive number with very large magnitude. c. -13 d. -25 e. 13 f. 25

B

QN=156 (72) What is 7 % -4? a. -3 b. 3 c. -4 d. 4

A

QN=157 (71) What is -8 % 5? a. -3 b. 3 c. -2 d. 2

A

QN=158 (146) What is the difference between the rules for method-call conversion and the rules for assignment conversion? (Choose one.) a. There is no difference; the rules are the same. b. Method-call conversion supports narrowing, assignment conversion does not. c. Assignment conversion supports narrowing, method-call conversion does not. d. Method-call conversion supports narrowing if the method declares that it throws ClassCastException.

B

QN=178 (312) When the user selects a menu item, event is generated. a. Select event b. Action event c. Item event d. None of the others

A

QN=159 (109) What is the minimal modification that will make this code compile correctly? (Choose one.) 1. final class Aaa 2. { 3. int xxx; 4. void yyy() { xxx = 1; } 5. } 6. 7. 8. class Bbb extends Aaa 9. { 10. final Aaa finalref = new Aaa(); 11. 12. final void yyy() 13. { 14. System.out.println("In method yyy()"); 15. finalref.xxx = 12345; 16. } 17. } a. On line 1, remove the final modifier. b. On line 10, remove the final modifier. c. Remove line 15. d. On lines 1 and 10, remove the final modifier. e. The code will compile as is. No modification is needed.

B

QN=16 (198) A Java monitor must either extend Thread or implement Runnable. a. True b. False

D

QN=160 (54) What is the range of values that can be assigned to a variable of type byte? (Choose one.) a. Depends on the underlying hardware b. 0 through 2^8 − 1 c. 0 through 2^16 − 1 d. −2^7 through 2^7 − 1 e. −2^15 through 2^15 − 1

D

QN=161 (53) What is the range of values that can be assigned to a variable of type short? (Choose one.) a. Depends on the underlying hardware b. 0 through 2^16 − 1 c. 0 through 2^32 − 1 d. −2^15 through 2^15 − 1 e. −2^31 through 2^31 − 1

A

QN=162 (242) What is the result of attempting to compile and execute the following code fragment? Assume that the code fragment is part of an application that has write permission in the current working directory. Also assume that before execution, the current working directory does not contain a file called datafile. (Choose one.) 1. try { 2. RandomAccessFile raf = new 3. RandomAccessFile("datafile" ,"rw"); 4. BufferedOutputStream bos = new BufferedOutputStream(raf); 5. 6. DataOutputStream dos = new DataOutputStream(bos); 7. 8. dos.writeDouble(Math.PI); 9. dos.close(); 10. bos.close(); 11. raf.close(); 12. } 13. catch (IOException e) { } a. The code fails to compile. b. The code compiles but throws an exception at line 4. c. The code compiles and executes but has no effect on the local file system. d. The code compiles and executes; afterward, the current working directory contains a file called datafile.

D

QN=163 (83) What is the return type of the instanceof operator? a. A reference b. A class c. An int d. A boolean

E

QN=164 (249) What method of the java.io.File class can create a file on the hard drive? (Choose one.) a. newFile() b. makeFile() c. makeNewFile() d. createFile() e. createNewFile()

C

QN=165 (74) What results from attempting to compile and run the following code? 1. public class Conditional { 2. public static void main(String args[]) { 3. int x = 4; 4. System.out.println("value is " + ((x > 4) ? 99.99 : 9)); 5. } 6. } a. The output: value is 99.99 b. The output: value is 9 c. The output: value is 9.0 d. A compiler error at line 4

B

QN=166 (73) What results from running the following code? 1. public class Xor { 2. public static void main(String args[]) { 3. byte b = 10; // 00001010 binary 4. byte c = 15; // 00001111 binary 5. b = (byte)(b ^ c); 6. System.out.println("b contains " + b); 7. } 8. } a. The output: b contains 10 b. The output: b contains 5 c. The output: b contains 250 d. The output: b contains 245

A

QN=224 (84) Which of the following may appear on the left-hand side of an instanceof operator? a. A reference b. A class c. An interface d. A variable of primitive type

D

QN=167 (150) What would be the output from this code fragment? 1. int x = 0, y = 4, z = 5; 2. if (x > 2) { 3. if (y < 5) { 4. System.out.println("message one"); 5. } 6. else { 7. System.out.println("message two"); 8. } 9. } 10. else if (z > 5) { 11. System.out.println("message three"); 12. } 13. else { 14. System.out.println("message four"); 15. } a. message one b. message two c. message three d. message four

C

QN=168 (78) When a byte is added to a char, what is the type of the result? a. byte b. char c. int d. short e. You can't add a byte to a char.

C

QN=169 (144) When a negative byte is cast to a long, what are the possible values of the result? (Choose one.) a. Positive b. Zero c. Negative d. All the above

E

QN=17 (196) A monitor called mon has 10 threads in its waiting pool; all these waiting threads have the same priority. One of the threads is thr1. How can you notify thr1 so that it alone moves from the Waiting state to the Ready state? (Choose one.) a. Execute notify(thr1); from within synchronized code of mon. b. Execute mon.notify(thr1); from synchronized code of any object. c. Execute thr1.notify(); from synchronized code of any object. d. Execute thr1.notify(); from any code (synchronized or not) of any object. e. You cannot specify which thread will get notified.

D

QN=170 (143) When a negative long is cast to a byte, what are the possible values of the result? (Choose one.) a. Positive b. Zero c. Negative d. All the above

C

QN=171 (79) When a short is added to a float, what is the type of the result? a. short b. int c. float d. You can't add a short to a float.

E

QN=172 (1531) When comparing java.io.BufferedWriter to java.io.FileWriter, which capability exists as a method in only one of the two? (Choose one.) 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

A

QN=173 (165) When does an exception's stack trace get recorded in the exception object? (Choose one.) a. When the exception is constructed b. When the exception is thrown c. When the exception is caught d. When the exception's printStackTrace() method is called

B

QN=174 (163) When is it appropriate to pass a cause to an exception's constructor? (Choose one.) a. Always b. When the exception is being thrown in response to catching of a different exception type c. When the exception is being thrown from a public method d. When the exception is being thrown from a private method

E

QN=175 (166) When is it appropriate to write code that constructs and throws an error? (Choose one.) a. When a public method's preconditions are violated b. When a public method's postconditions are violated c. When a nonpublic method's preconditions are violated d. When a nonpublic method's postconditions are violated e. Never

B

QN=176 (141) When is x & y an int? (Choose one). a. Always b. Sometimes c. When neither x nor y is a float, a long, or a double d. None of the others

A

QN=177 (308) When the user attempts to close the frame window, event in generated. a. window closing b. window resize c. window move d. window close e. window closed

A

QN=181 (7267) Which are the correct statements used for getting connection object to connect to SQL Server database? a. String url ="jdbc:odbc:data_source_name"; Connection con = DriverManager.getConnection (url, "user", "password"); b. String url ="odbc:jdbc:data_source_name"; Connection con = DriverManager.getConnection (url, "user", "password"); c. String url ="jdbc:data_source_name:odbc"; Connection con = DriverManager.getConnection (url, "user", "password");

A

QN=182 (1879) Which class and static method can you use to convert an array to a List? (Choose one.) a. Arrays.asList b. Arrays.toList c. Arrays.createList d. Arrays.makeList

A

QN=183 (302) Which is four-step approach to help you organize your GUI thinking. (Choose one.) a. Identify needed components. Isolate regions of behavior. Sketch the GUI. Choose layout managers. b. Choose layout managers. Identify needed components. Isolate regions of behavior. Sketch the GUI. c. Identify needed components. Choose layout managers. Isolate regions of behavior. Sketch the GUI.

A

QN=184 (4928) Which is the four steps are used in working with JDBC? a. 1)Connect to the database 2)Create a statement and execute the query 3)Look at the result set 4)Close connection b. 1)Load driver 2)Create a statement and execute the query 3)Look at the result set 4)Close connection c. 1)Create a statement and execute the query 2)Load driver 3)Look at the result set 4)Close connection d. 1)Create a statement and execute the query 2)Create the connection 3)Look at the result set 4)Close connection

A

QN=185 (7256) Which JDBC processing model that requires a JDBC driver that can communicate with the particular data source being accessed? a. two-tier b. three-tier c. one-tier d. four-tier

C

QN=186 (229) Which line of code tells a scanner called sc to use a single digit as a delimiter? (Choose one.) a. sc.useDelimiter("d"); b. sc.useDelimiter("\d"); c. sc.useDelimiter("\\d"); d. sc.useDelimiter("d+"); e. sc.useDelimiter("\d+"); f. sc.useDelimiter("\\d+");

D

QN=187 (1411) Which Man class properly represents the relationship "Man has the best friend who is a Dog"? (Choose one.) 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> }

B, C

QN=188 (189) Which methods return an enum constant's name? (Choose two.) a. getName() b. name() c. toString() d. nameString() e. getNameString()

D

QN=189 (114) Which modifier or modifiers should be used to denote a variable that should not be written out as part of its class's persistent state? (Choose the shortest possible answer.) a. private b. protected c. private protected d. transient e. volatile

B

QN=19 (45) A signed data type has an equal number of non-zero positive and negative values available. a. True b. False

A, B, E

QN=190 (162) Which of the following are legal argument types for a switch statement? (Choose three.) a. byte b. int c. long d. float e. char f. String

E

QN=191 (177) Which of the following are legal enums? (Choose three.) a. enum Animals { LION, TIGER, BEAR } b. enum Animals { int age; LION, TIGER, BEAR; } c. enum Animals { LION, TIGER, BEAR; int weight; } d. enum Animals { LION(450), TIGER(450), BEAR; int weight; Animals(int w) { weight = w; } } e. enum Animals { LION(450), TIGER(450), BEAR; int weight; Animals() { } Animals(int w) { weight = w; } }

A, C

QN=192 (57) Which of the following are legal import statements? (Choose two.) a. import java.util.Vector; b. static import java.util.Vector.*; c. import static java.util.Vector.*; d. import java.util.Vector static;

C

QN=193 (149) Which of the following are legal loop constructions? (Choose one.) a. while (int i<7) { i++; System.out.println("i is " + i); } b. int i = 3; while (i) { System.out.println("i is " + i); } c. int j = 0; for (int k=0, j+k != 10; j++,k++) { System.out.println("j=" + j + ", k=" + k); } d. int j=0; do { System.out.println("j=" + j++); if (j==3) continue loop; } while (j<10);

F

QN=194 (161) Which of the following are legal loop definitions? (Choose one.) a. while (int a = 0) { /* whatever */ } b. while (int a == 0) { /* whatever */ } c. do { /* whatever */ } while (int a = 0) d. do { /* whatever */ } while (int a == 0) e. for (int a==0; a<100; a++) { /* whatever */ } f. None of the above.

A, B, D

QN=195 (159) Which of the following are legal? (Choose three.) a. for (int i=0, j=1; i<10; i++, j++) b. for (int i=0, j=1;; i++, j++) c. for (int i=0, float j=1; ; i++, j++) d. for (String s = ""; s.length()<10; s += '!') e. while(1)

A, B

QN=196 (62) Which of the following are legal? (Choose two.) a. double d = 1.2d; b. double d = 1.2D; c. double d = 1.2d5; d. double d = 1.2D5;

C, D

QN=197 (61) Which of the following are legal? (Choose two.) a. int a = abcd; b. int b = ABCD; c. int c = 0xabcd; d. int d = 0XABCD; e. int f = 0ABCD;

B

QN=20 (194) A thread wants to make a second thread ineligible for execution. To do this, the first thread can call the yield() method on the second thread. a. True b. False

C, D, E

QN=200 (228) Which of the following are methods of the java.util.SortedMap interface? (Choose three.) a. first b. last c. headMap d. tailMap e. subMap

F

QN=201 (227) Which of the following are methods of the java.util.SortedSet interface? (Choose one.) a. first b. last c. headSet d. tailSet e. subSet f. All the above

D

QN=202 (250) Which of the following are true? (Choose one.) a. System.out has a println() method. b. System.out has a format() method. c. System.err has a println() method. d. System.err has a format () method. e. All the above

D

QN=203 (207) Which of the following are true? (Choose one.) a. The JVM runs until there is only one daemon thread. b. The JVM runs until there are no daemon threads. c. The JVM runs until there is only one non-daemon thread. d. The JVM runs until there are no non-daemon threads.

B, C, D

QN=204 (206) Which of the following are true? (Choose three.) a. When an application begins running, there is one daemon thread, whose job is to execute main(). b. When an application begins running, there is one non-daemon thread, whose job is to execute main(). c. A thread created by a daemon thread is initially also a daemon thread. d. A thread created by a non-daemon thread is initially also a non-daemon thread.

A, D

QN=205 (210) Which of the following are true? (Choose two.) a. When you declare a method to be synchronized, the method always synchronizes on the lock of the current object. b. When you declare a method to be synchronized, you can specify the object on whose lock the method should synchronize. c. When you declare a block of code inside a method to be synchronized, the block always synchronizes on the lock of the current object. d. When you declare a block of code inside a method to be synchronized, you can specify the object on whose lock the block should synchronize.

C, D

QN=206 (185) Which of the following are true? (Choose two.) a. An enum definition should declare that it extends java.lang.Enum. b. An enum may be subclassed. c. An enum may contain public method definitions. d. An enum may contain private data.

B, D

QN=207 (65) Which of the following are true? (Choose two.) a. Primitives are passed by reference. b. Primitives are passed by value. c. References are passed by reference. d. References are passed by value.

A, C

QN=208 (188) Which of the following are true? (Choose two.) a. An anonymous inner class may implement at most one interface. b. An anonymous inner class may implement arbitrarily many interfaces. c. An anonymous inner class may extend a parent class other than Object. d. An anonymous inner class that implements one interface may extend a parent class other than Object. e. An anonymous inner class that implements several interfaces may extend a parent class other than Object.

C

QN=209 (253) Which of the following are valid arguments to the DataInputStream constructor? (Choose one.) a. File b. FileReader c. FileInputStream d. RandomAccessFile

At line 2, the thread will stop running. It will resume running some time after 100 milliseconds have elapsed.

QN=21 (195) A thread's run() method includes the following lines: 1. try { 2. sleep(100); 3. } catch (InterruptedException e) { } Assuming the thread is not interrupted, which one of the following statements is correct? a. The code will not compile, because exceptions cannot be caught in a thread's run() method. b. At line 2, the thread will stop running. Execution will resume in, at most, 100 milliseconds. c. At line 2, the thread will stop running. It will resume running in exactly 100 milliseconds. d. At line 2, the thread will stop running. It will resume running some time after 100 milliseconds have elapsed.

D

QN=21 (195) A thread's run() method includes the following lines: 1. try { 2. sleep(100); 3. } catch (InterruptedException e) { } Assuming the thread is not interrupted, which one of the following statements is correct? a. The code will not compile, because exceptions cannot be caught in a thread's run() method. b. At line 2, the thread will stop running. Execution will resume in, at most, 100 milliseconds. c. At line 2, the thread will stop running. It will resume running in exactly 100 milliseconds. d. At line 2, the thread will stop running. It will resume running some time after 100 milliseconds have elapsed.

E

QN=210 (252) Which of the following are valid mode strings for the RandomAccessFile constructor? (Choose one.) a. "r" b. "rw" c. "rws" d. "rwd" e. All the above

E

QN=211 (203) Which of the following calls may be made from a non-static synchronized method? (Choose one.) a. A call to the same method of the current object. b. A call to the same method of a different instance of the current class. c. A call to a different synchronized method of the current object. d. A call to a static synchronized method of the current class. e. All the above

A, D

QN=212 (226) Which of the following classes implement java.util.List? (Choose two.) a. java.util.ArrayList b. java.util.HashMap c. java.util.TreeSet d. java.util.Stack

B

QN=213 (1878) Which of the following classes implements a FIFO Queue? (Choose one.) a. HashSet b. LinkedList c. PriorityQueue d. CopyOnWriteArraySet

A, D, E

QN=214 (107) Which of the following declarations are illegal? (Choose three.) a. default String s; b. transient int i = 41; c. public final static native int w(); d. abstract double d; e. abstract final double hyperbolicCosine();

B, C

QN=215 (68) Which of the following expressions are legal? (Choose two.) a. int x = 6; x = !x; b. int x = 6; if (!(x > 3)) {} c. int x = 6; x = ~x;

A

QN=216 (70) Which of the following expressions are legal? (Choose two.) a. String x = "Hello"; int y = 9; x += y; b. String x = "Hello"; int y = 9; if (x == y) {} c. String x = "Hello"; int y = 9; x = x + y; d. String x = "Hello"; int y = 9; y = y + x;

B

QN=218 (1874) Which of the following interfaces does not allow duplicate objects? (Choose one.) a. Queue b. Set c. List

A

QN=219 (155) Which of the following is not appropriate situations for assertions? (Choose one) a. Preconditions of a public method b. Postconditions of a public method c. Preconditions of a private method d. Postconditions of a private method

C

QN=22 (7264) A(n) ______ object is used to submit a query to a database a. Command b. Connection c. Statement d. DriverManager

C

QN=220 (5695) Which of the following is NOTa valid comment: a. /** comment */ b. /* comment */ c. /* comment d. // comment

B

QN=221 (157) Which of the following is the most appropriate way to handle invalid arguments in a public method? a. Throw java.lang.InvalidArgumentException. b. Throw java.lang.IllegalArgumentException. c. Check for argument validity in an assert statement, which throws AssertionError when the arguments are invalid. d. Use non-assert code to check for argument validity. If invalid arguments are detected, explicitly throw AssertionError.

E

QN=222 (240) Which of the following is true? (Choose one.) a. Readers have methods that can read and return floats and doubles. b. Readers have methods that can read and return floats. c. Readers have methods that can read and return doubles. d. Readers have methods that can read and return ints. e. None of the above

E

QN=223 (184) Which of the following is(are) true? (Choose one.) a. An enum definition may contain the main() method of an application. b. You can call an enum's toString() method. c. You can call an enum's wait() method. d. You can call an enum's notify() method. e. All the above

A

QN=237 (182) Which of the following restrictions apply to anonymous inner classes? (Choose one.) a. They must be defined inside a code block. b. They may only read and write final variables of the enclosing class. c. They may only call final methods of the enclosing class. d. They may not call the enclosing class' synchronized methods.

B

QN=238 (164) Which of the following should always be caught? (Choose one.) a. Runtime exceptions b. Checked exceptions c. Assertion errors d. Errors other than assertion errors

B, D

QN=239 (47) Which of the following signatures are valid for the main() method entry point of an application? (Choose two.) a. public static void main() b. public static void main(String arg[]) c. public void main(String [] arg) d. public static void main(String[] args) e. public static int main(String [] arg)

C

QN=24 (66) After execution of the following code fragment, what are the values of the variables x, a, and b? 1. int x, a = 6, b = 7; 2. x = a++ + b++; a. x = 15, a = 7, b = 8 b. x = 15, a = 6, b = 7 c. x = 13, a = 7, b = 8 d. x = 13, a = 6, b = 7

C

QN=240 (201) Which of the following statements about the wait() and notify() methods is true? (Choose one.) a. The wait() and notify() methods can be called outside synchronized code. b. The programmer can specify which thread should be notified in a notify() method call. c. The thread that calls wait() goes into the monitor's pool of waiting threads. d. The thread that calls notify() gives up the lock.

B

QN=241 (200) Which of the following statements about threads is true? (Choose one.) a. Every thread starts executing with a priority of 5. b. Threads inherit their priority from their parent thread. c. Threads are guaranteed to run with the priority that you set using the setPriority() method. d. Thread priority is an integer ranging from 1 to 100.

D

QN=242 (125) Which of the following statements are true? (Choose one.) a. A final class must be instantiated. b. A final class may only contain final methods. c. A final class may not contain non-final data fields. d. A final class may not be extended.

A

QN=243 (176) Which of the following statements are true? (Choose one.) a. Given that Inner is a nonstatic class declared inside a public class Outer and that appropriate constructor forms are defined, an instance of Inner can be constructed like this: new Outer().new Inner() b. If an anonymous inner class inside the class Outer is defined to implement the interface ActionListener, it can be constructed like this: new Outer().new ActionListener() c. Given that Inner is a nonstatic class declared inside a public class Outer and that appropriate constructor forms are defined, an instance of Inner can be constructed in a static method like this: new Inner() d. An anonymous class instance that implements the interface MyInterface can be constructed and returned from a method like this: 1. return new MyInterface(int x) { 2. int x; 3. public MyInterface(int x) { 4. this.x = x; 5. } 6. };

F

QN=244 (118) Which of the following statements are true? (Choose one.) a. An abstract class may be instantiated. b. An abstract class must contain at least one abstract method. c. An abstract class must contain at least one abstract data field. d. An abstract class must be overridden. e. An abstract class must declare that it implements an interface. f. None of the above

D

QN=247 (127) Which of the following statements is correct? (Choose one.) a. Only primitives are converted automatically; to change the type of an object reference, you have to do a cast. b. Only object references are converted automatically; to change the type of a primitive, you have to do a cast. c. Arithmetic promotion of object references requires explicit casting. d. Both primitives and object references can be both converted and cast. e. Casting of numeric types may require a runtime check.

E

QN=248 (110) Which of the following statements is true? (Choose one.) a. Transient methods may not be overridden. b. Transient methods must be overridden. c. Transient classes may not be serialized. d. Transient variables must be static. e. Transient variables are not serialized.

D

QN=249 (132) Which of the following statements is true? (Choose one.) a. Object references can be converted in assignments but not in method calls. b. Object references can be converted in method calls but not in assignments. c. Object references can be converted in both method calls and assignments, but the rules governing these conversions are very different. d. Object references can be converted in both method calls and assignments, and the rules governing these conversions are identical. e. Object references can never be converted.

B, E

QN=25 (1410) Assume that country is set for each class. Given: 10. public class Money { 11. private String country, name; 12. public String getCountry() { return country; } 13.} and: 24. class Yen extends Money { 25. public String getCountry() { return super.country; } 26. } 28. class Euro extends Money { 29. public String getCountry(String timeZone) { 30. return super.getCountry(); 31. } 32. } Which two are correct? (Choose two.) a. Yen returns correct Country value. b. Euro returns correct Country value. c. An exception is thrown at runtime. d. Yen and Euro both return correct Country value. e. Compilation fails because of an error at line 25. f. Compilation fails because of an error at line 30.

E

QN=250 (233) Which of the statements below are true? (Choose one.) a. UTF characters are all 8 bits. b. UTF characters are all 16 bits. c. UTF characters are all 24 bits. d. Unicode characters are all 16 bits. e. Bytecode characters are all 16 bits.

D

QN=251 (235) Which of the statements below are true? (Choose one.) a. To change the current working directory, call the setWorkingDirectory() method of the File class. b. To change the current working directory, call the cd() method of the File class. c. To change the current working directory, call the changeWorkingDirectory() method of the File class. d. None of the above

D

QN=252 (234) Which of the statements below are true? (Choose one.) a. When you construct an instance of File, if you do not use the file-naming semantics of the local machine, the constructor will throw an IOException. b. When you construct an instance of File, if the corresponding file does not exist on the local file system, one will be created. c. When an instance of File is garbage collected, the corresponding file on the local file system is deleted. d. None of the above.

E

QN=255 (215) Which one statement is true about the following code fragment? (choose 1) 1. import java.lang.Math; 2. Math myMath = new Math(); 3. System.out.println("cosine of 0.123 = " + myMath.cos(0.123)); a. Compilation fails at line 2. b. Compilation fails at line 3 c. Compilation succeeds, although the import on line 1 is not necessary. During execution, an exception is thrown at line 3 d. Compilation succeeds. The import on line 1 is necessary. During execution, an exception is thrown at line 3 e. Compilation succeeds and no exception is thrown during execution.

E

QN=256 (216) Which one statement is true about the following code fragment? 1. String s = "FPT"; 2. StringBuffer s1 = new StringBuffer("FPT"); 3. if (s.equals(s1)) 4. s1 = null; 5. if (s1.equals(s)) 6. s = null; a. Compilation fails at line 1 because the String constructor must be called explicitly. b. Compilation fails at line 3 because s and s1 have different types. c. Compilation succeeds. During execution, an exception is thrown at line 3. d. Compilation succeeds. During execution, an exception is thrown at line 5. e. Compilation succeeds. No exception is thrown during execution.

C

QN=257 (213) Which one statement is true about the following code? 1. String s1 = "abc" + "def"; 2. String s2 = new String(s1); 3. if (s1 == s2) 4. System.out.println("== succeeded"); 5. if (s1.equals(s2)) 6. System.out.println(".equals() succeeded"); a. Lines 4 and 6 both execute. b. Line 4 executes and line 6 does not. c. Line 6 executes and line 4 does not. d. Neither line 4 nor line 6 executes.

A

QN=258 (192) Which one statement is true concerning the following code? 1. class Greebo extends java.util.Vector implements Runnable { 2. 3. public void run(String message) { 4. System.out.println("in run() method: " + message); 5. 6. } 7. } 8. 9. class GreeboTest { 10. public static void main(String args[]) { 12. Greebo g = new Greebo(); 13. Thread t = new Thread(g); 14. t.start(); 15. } 16. } a. There will be a compiler error, because class Greebo does not correctly implement the Runnable interface. b. There will be a compiler error at line 13, because you cannot pass a parameter to the constructor of a Thread. c. The code will compile correctly but will crash with an exception at line 13. d. The code will compile correctly but will crash with an exception at line 14. e. The code will compile correctly and will execute without throwing any exceptions.

D

QN=259 (151) Which statement is true about the following code fragment? (Choose one.) 1. int j = 2; 2. switch (j) { 3. case 2: 4. System.out.println("value is two"); 5. case 2 + 1: 6. System.out.println("value is three"); 7. break; 8. default: 9. System.out.println("value is " + j); 10. break; 11. } a. The code is illegal because of the expression at line 5. b. The acceptable types for the variable j, as the argument to the switch() construct, could be any of byte, short, int, or long. c. The output would be the text value is two. d. The output would be the text value is two followed by the text value is three. e. The output would be the text value is two, followed by the text value is three, followed by the text value is 2.

A, B, C, D, E

QN=26 (46) Choose the valid identifiers from those listed here. (Choose all that apply.) a. BigOlLongStringWithMeaninglessName b. $int c. bytes d. $1 e. finalist

A

QN=260 (80) Which statement is true about the following method? int selfXor(int i) { return i ^ i; } a. It always returns 0. b. It always returns 1. c. It always an int where every bit is 1. d. The returned value varies depending on the argument.

E

QN=261 (111) Which statement is true about this application? (Choose one.) 1. class StaticStuff 2 { 3. static int x = 10; 4. 5. static { x += 5; } 6. 7. public static void main(String args[]) 8. { 9. System.out.println("x = " + x); 10. } 11. 12. static {x /= 5; } 13. } a. Lines 5 and 12 will not compile because the method names and return types are missing. b. Line 12 will not compile because you can only have one static initializer. c. The code compiles and execution produces the output x = 10. d. The code compiles and execution produces the output x = 15. e. The code compiles and execution produces the output x = 3.

E

QN=262 (112) Which statement is true about this code? (Choose one.) 1. class HasStatic 2. { 3. private static int x = 100; 4. 5. public static void main(String args[]) 6. { 7. HasStatic hs1 = new HasStatic(); 8. hs1.x++; 9. HasStatic hs2 = new HasStatic(); 10. hs2.x++; 11. hs1 = new HasStatic(); 12. hs1.x++; 13. HasStatic.x++; 14. System.out.println("x = " + x); 15. } 16. } a. Line 8 will not compile because it is a static reference to a variable. b. Line 13 will not compile because it is a static reference to a private variable. c. The program compiles and the output is x = 102. d. The program compiles and the output is x = 103. e. The program compiles and the output is x = 104.

E, F

QN=263 (4929) Which statements about JDBC are NOT true? (choose 2) a. JDBC is a Java API for executing SQL statements. b. JDBC provides a standard API for tool/database developers and makes it possible to write database applications using a pure Java API. c. It consists of a set of classes and interfaces written in the Java programming language. d. The combination of Java and JDBC lets a programmer write it once and run it anywhere. e. JDBC is a Java database system. f. JDBC is a Java API for connecting to any kind of DBMS

A, B

QN=264 (1423) 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]=100; a[1]=200;}

B, E

QN=265 (1873) Which two of the following interfaces are at the top of the hierarchies in the Java Collections Framework? (Choose two.) a. Set b. Map c. Queue d. SortedMap e. Collection f. List

E

QN=266 (241) You execute the following code in an empty directory. What is the result? (Choose one.) 1. File f1 = new File("dirname"); 2. File f2 = new File(f1, "filename"); a. A new directory called dirname is created in the current working directory. b. A new directory called dirname is created in the current working directory. A new file called filename is created in directory dirname. c. A new directory called dirname and a new file called filename are created, both in the current working directory. d. A new file called filename is created in the current working directory. e. No directory is created, and no file is created.

C

QN=267 (171) You have been given a design document for a veterinary registration system for implementation in Java. It states: "A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet that has a flag indicating if it has been neutered, and a textual description of its markings." Given that the Pet class has already been defined and you expect the Cat class to be used freely throughout the application, how would you make the opening declaration of the Cat class, up to but not including the first opening brace? Use only these words and spaces: boolean, Cat, class, Date, extends, Object, Owner, Pet, private, protected, public, String. (Choose one.) a. protected class Cat extends Owner b. public class Cat extends Object c. public class Cat extends Pet d. private class Cat extends Pet

E, F

QN=268 (170) You have been given a design document for a veterinary registration system for implementation in Java. It states: "A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet that has a flag indicating whether it has been neutered, and a textual description of its markings." Given that the Pet class has already been defined, which of the following fields would be appropriate for inclusion in the Cat class as members? (Choose two.) a. Pet thePet; b. Date registered; c. Date vaccinationDue; d. Cat theCat; e. boolean neutered; f. String markings;

D

QN=27 (1520) Class 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. } Which is true about the two classes? (Choose one.) 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.

D, E

QN=28 (1521) Class TestException: 1. public class TestException extends Exception { 2. } Class A: 1. public class A { 2. 3. public String sayHello(String name) throws TestException { 4. 5. if(name == null) { 6. throw new TestException(); 7. } 8. 9. return "Hello "+ name; 10. } 11. 12. } A programmer wants to use this code in an application: 45. A a=new A(); 46. System.out.println(a.sayHello("John")); Which two are true? (Choose two.) a. Class A will not compile. b. Line 46 can throw the unchecked exception TestException. c. Line 45 can throw the unchecked exception TestException. d. Line 46 will compile if the enclosing method throws a TestException. e. Line 46 will compile if enclosed in a try block, where TestException is caught.

d

QN=29 (50) Consider the following application: 1. class Q6 { 2. public static void main(String args[]) { 3. Holder h = new Holder(); 4. h.held = 100; 5. h.bump(h); 6. System.out.println(h.held); 7. } 8. } 9. 10. class Holder { 11. public int held; 12. public void bump(Holder theHolder) { 13. theHolder.held++; 14 } 15. } 15. } What value is printed out at line 6? a. 0 b. 1 c. 100 d. 101

A

QN=3 (7259)__________ drivers that are written partly in the Java programming language and partly in native code. These drivers use a native client library specific to the data source to which they connect. Again, because of the native code, their portability is limited. a. Type 2 b. Type 3 c. Type 1 d. Type 4

C

QN=30 (51) Consider the following application: 1. class Q7 { 2. public static void main(String args[]) { 3. double d = 12.3; 4. Decrementer dec = new Decrementer(); 5. dec.decrement(d); 6. System.out.println(d); 7. } 8. } 9. 10. class Decrementer { 11. public void decrement(double decMe) { 12. decMe = decMe - 1.0; 13. } 14. } What value is printed out at line 6? a. 0.0 b. 1.0 c. 12.3 d. 11.3

B, C

QN=31 (173) Consider the following class definition: 1. public class Test extends Base { 2. public Test(int j) { 3. } 4. public Test(int j, int k) { 5. super(j, k); 6. } 7. } Which of the following are legitimate calls to construct instances of the Test class? (Choose two.) a. Test t = new Test(); b. Test t = new Test(1); c. Test t = new Test(1, 2); d. Test t = new Test(1, 2, 3); e. Test t = (new Base()).new Test(1);

A, C

QN=32 (174) Consider the following class definition: 1. public class Test extends Base { 2. public Test(int j) { 3. } 4. public Test(int j, int k) { 5. super(j, k); 6. } 7. } Which of the following forms of constructor must exist explicitly in the definition of the Base class? Assume Test and Base are in the same package. (Choose two.) a. Base() { } b. Base(int j) { } c. Base(int j, int k) { } d. Base(int j, int k, int l) { }

D

QN=33 (131) Consider the following class: 1. class Cruncher { 2. void crunch(int i) { 3. System.out.println("int version"); 4. } 5. void crunch(String s) { 6. System.out.println("String version"); 7. } 8. 9. public static void main(String args[]) { 10. Cruncher crun = new Cruncher(); 11. char ch = 'p'; 12. crun.crunch(ch); 13. } 14. } Which of the following statements is true? (Choose one.) a. Line 5 will not compile, because void methods cannot be overridden. b. Line 12 will not compile, because no version of crunch() takes a char argument. c. The code will compile but will throw an exception at line 12. d. The code will compile and produce the following output: int version. e. The code will compile and produce the following output: String version.

C

QN=34 (172) Consider the following classes, declared in separate source files: 1. public class Base { 2. public void method(int i) { 3. System.out.print("Value is " + i); 4. } 5. } 1. public class Sub extends Base { 2. public void method(int j) { 3. System.out.print("This value is " + j); 4. } 5. public void method(String s) { 6. System.out.print("I was passed " + s); 7. } 8. public static void main(String args[]) { 9. Base b1 = new Base(); 10. Base b2 = new Sub(); 11. b1.method(5); 12. b2.method(6); 13. } 14. } What output results when the main method of the class Sub is run?(Choose one.) a. Value is 5Value is 6 b. This value is 5This value is 6 c. Value is 5This value is 6 d. This value is 5Value is 6 e. I was passed 5I was passed 6

C

QN=35 (133) Consider the following code. Which line will not compile? (Choose one.) 1. Object ob = new Object(); 2. String[] stringarr = new String[50]; 3. Float floater = new Float(3.14f); 4. ob = stringarr; 5. ob = stringarr[5]; 6. floater = ob; 7. ob = floater; a. Line 4 b. Line 5 c. Line 6 d. Line 7

B, D

QN=36 (156) Consider the following code: 1. public class Assertification { 2. public static void main(String[] args) { 3. assert args.length == 0; 4 } 5. } Which of the following conditions must be true in order for the code to throw an AssertionError? Assume you are using release 5.0. (Choose two.) a. The source code must be compiled with the -source 1.5 flag. b. The application must be run with the -enableassertions flag or another assertionenabling flag. c. The args array must have exactly zero elements. d. The args array must have one or more elements.

A, E

QN=44 Consider the following line of code: (49) int[] x = new int[25]; After execution, which statements are true? (Choose two.) a. x[24] is 0 b. x[24] is undefined c. x[25] is 0 d. x[0] is null e. x.length is 25

E

QN=38 (135) Consider the following code: 1. Cat sunflower; 2. Washer wawa; 3. SwampThing pogo; 4. 5. sunflower = new Cat(); 6. wawa = sunflower; 7. pogo = (SwampThing)wawa; Which of the following statements is true? (Choose one.) Where: Mammal extends Animal Cat, SwampThing extends Mammal Cat implements Washer a. Line 6 will not compile; an explicit cast is required to convert a Cat to a Washer. b. Line 7 will not compile, because you cannot cast an interface to a class. c. The code will compile and run, but the cast in line 7 is not required and can be eliminated. d. The code will compile but will throw an exception at line 7, because runtime conversion from an interface to a class is not permitted. e. The code will compile but will throw an exception at line 7, because the runtime class of wawa cannot be converted to type SwampThing.

D

QN=39 (134) Consider the following code: 1. Dog rover, fido; 2. Animal anim; 3. 4. rover = new Dog(); 5. anim = rover; 6. fido = (Dog)anim; Where: Mammal extends Animal Dog extends Mammal Which of the following statements is true? (Choose one.) a. Line 5 will not compile. b. Line 6 will not compile. c. The code will compile but will throw an exception at line 6. d. The code will compile and run. e. The code will compile and run, but the cast in line 6 is not required and can be eliminated.

A

QN=4 (7261) drivers that are pure Java and implement the network protocol for a specific data source. The client connects directly to the data source. a. Type 4 b. Type 3 c. Type 2 d. Type 1

B, C, D, F

QN=40 (147) Consider the following code: 1. for (int i = 0; i < 2; i++) { 2. for (int j = 0; j < 3; j++) { 3. if (i == j) { 4. continue; 5. } 6. System.out.println("i = " + i + " j = " + j); 7. } 8. } Which lines would be part of the output? (Choose four.) a. i = 0 j = 0 b. i = 0 j = 1 c. i = 0 j = 2 d. i = 1 j = 0 e. i = 1 j = 1 f. i = 1 j = 2

D

QN=41 (148) Consider the following code: 1. outer: for (int i = 0; i < 2; i++) { 2. for (int j = 0; j < 3; j++) { 3. if (i == j) { 4. continue outer; 5. } 6. System.out.println("i = " + i + " j = " + j); 7. } 8. } Which lines would be part of the output? (Choose one.) a. i = 0 j = 0 b. i = 0 j = 1 c. i = 0 j = 2 d. i = 1 j = 0 e. i = 1 j = 1 f. i = 1 j = 2

B

QN=42 (136) Consider the following code: 1. Raccoon rocky; 2. SwampThing pogo; 3. Washer w; 4. 5. rocky = new Raccoon(); 6. w = rocky; 7. pogo = w; Which of the following statements is true? (Choose one.) Where: Mammal extends Animal Dog, Raccoon, Swamp Thing extends Mammal Raccoon implements Washer a. Line 6 will not compile; an explicit cast is required to convert a Raccoon to a Washer. b. Line 7 will not compile; an explicit cast is required to convert a Washer to a SwampThing. c. The code will compile and run. d. The code will compile but will throw an exception at line 7, because runtime conversion from an interface to a class is not permitted. e. The code will compile but will throw an exception at line 7, because the runtime class of w cannot be converted to type SwampThing.

A, B, C, E

QN=43 (175) Consider the following definition: 1. public class Outer { 2. public int a = 1; 3. private int b = 2; 4. public void method(final int c) { 5. int d = 3, f=10; 6. class Inner { 7. private void iMethod(int e) { 8. 9. } 10. } 11. } 12. } Which variables can be referenced at line 8? (Choose four.) a. a b. b c. c d. d e. e f. f

B, D

QN=45 (169) Consider these classes, defined in separate source files: 1. public class Test1 { 2. public float aMethod(float a, float b) throws IOException {.. } 3. } 1. public class Test2 extends Test1 { 2. 3.} Which of the following methods would be legal (individually) at line 2 in class Test2? (Choose two) a. float aMethod(float a, float b) {...} b. public int aMethod(int a, int b) throws Exception {...} c. public float aMethod(float a, float b) throws Exception {...} d. public float aMethod(float p, float q) {...}

A, C, E

QN=46 (167) Consider this class: 1. public class Test1 { 2. public float aMethod(float a, float b) { 3. } 4. 5. } Which of the following methods would be legal if added (individually) at line 4? (Choose three.) a. public int aMethod(int a, int b) { } b. public float aMethod(float a, float b) { } c. public float aMethod(float a, float b, int c) throws Exception { } d. public float aMethod(float c, float d) { } e. private float aMethod(int a, int b, int c) { }

C

QN=47 (1517) Give: 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? (Choose one.) 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.

F

QN=48 (212) Given a string constructed by calling s = new String("xyzzy"), which of the calls modifies the string? (Choose one.) a. s.append("aaa"); b. s.trim(); c. s.substring(3); d. s.replace('z', 'a'); e. s.concat(s); f. None of the above

A

QN=49 (231) Given arrays a1 and a2, which call returns true if a1 and a2 have the same length, and a1[i].equals(a2[i]) for every legal index i? (Choose one.) a. java.util.Arrays.equals(a1, a2); b. java.util.Arrays.compare(a1, a2); c. java.util.List.compare(a1, a2);

A

QN=5 (7260) drivers that use a pure Java client and communicate with a middleware server using a database-independent protocol. The middleware server then communicates the client's requests to the data source. a. Type 3 b. Type 2 c. Type 1 d. Type 4

A, B, E

QN=50 (248) Given the following class: public class Xyz implements java.io.Serializable { public int iAmPublic; private int iAmPrivate; static int iAmStatic; transient int iAmTransient; volatile int iAmVolatile; } Assuming the class does not perform custom serialization, which fields are written when an instance of Xyz is serialized? (Choose three.) a. iAmPublic b. iAmPrivate c. iAmStatic d. iAmTransient e. iAmVolatile

D

QN=51 (113) Given the following code, and making no other changes, which combination of access modifiers (public, protected, or private) can legally be placed before aMethod() on line 3 and be placed before aMethod() on line 8? (Choose one.) 1. class SuperDuper 2. { 3. void aMethod() { } 4. } 5. 6. class Sub extends SuperDuper 7. { 8. void aMethod() { } 9. } a. line 3: public; line 8: private b. line 3: protected; line 8: private c. line 3: default; line 8: private d. line 3: private; line 8: protected e. line 3: public; line 8: protected

A, B, D

QN=52 (187) Given the following code, which of the following will compile? (Choose three.) enum Spice { NUTMEG, CINNAMON, CORIANDER, ROSEMARY; } a. Spice sp = Spice.NUTMEG; Object ob = sp; b. Spice sp = Spice.NUTMEG; Object ob = (Object)sp; c. Object ob = new Object(); Spice sp = ob d. Object ob = new Object(); Spice sp = (Spice)ob; e. String ob = new String(); Spice sp = ob;

A, B

QN=53 (224) Given the following: List<String> names = new ArrayList<String>(); which of the following are legal? (Choose two.) a. Iterator<String> iter = names.iterator(); b. for (String s:names) c. while (String s:names)

F

QN=54 (1524) 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? (Choose one.) 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.

D

QN=55 (1407) Given: public class Bar { public static void main(String [] args) { int x =5; boolean b1 = true; boolean b2 = false; if((x==4) && !b2) System.out.print("l "); System.out.print("2 "); if ((b2 = true) && b1) System.out.print("3"); } } What is the result? (Choose one.) a. 2 b. 3 c. 1 2 d. 2 3 e. 1 2 3 f. Compilation fails.

A

QN=56 (1418) Given: 1. public interface A { 2. String DEFAULT_GREETING = "Hello World"; 3. public void method1(); 4. } A programmer wants to create an interface called B that has A as its parent. Which interface declaration is correct? (Choose one.) a. public interface B extends A { } b. public interface B implements A {} c. public interface B instanceOf A {} d. public interface B inheritsFrom A { }

B, D

QN=57 (1420) Given: 10. abstract public class Employee { 11. protected abstract double getSalesAmount(); 12. public double getCommision() { 13. return getSalesAmount() * 0.15; 14. } 15. } 16. class Sales extends Employee { 17. // insert method here 18. } Which two methods, inserted independently at line 17, correctly complete the Sales class? (Choose two.) a. double getSalesAmount() { return 1230.45; } b. public double getSalesAmount() { return 1230.45; } c. private double getSalesAmount() { return 1230.45; } d. protected double getSalesAmount() { return 1230.45; }

B, C, D

QN=58 (1530) Given: 10. class MakeFile { 11. public static void main(String[] args) { 12. try { 13. File directory = new File("d"); 14. File file = new File(directory,"f"); 15. if(!file.exists()) { 16. file.createNewFile(); 17. } 18. }catch (IOException e) { 19. e.printStackTrace (); 20. } 21. } 22. } The current directory does NOT contain a directory named "d." Which three are true? (Choose three.) a. Line 16 is never executed. b. An exception is thrown at runtime. c. Line 13 creates a File object named "d." d. Line 14 creates a File object named "f.' e. Line 13 creates a directory named "d" in the file system. f. Line 16 creates a directory named "d" and a file 'f' within it in the file system.

D

QN=59 (1415) 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? (Choose one.) a. Direction d = NORTH; b. Nav.Direction d = NORTH; c. Direction d = Direction.NORTH; d. Nav.Direction d = Nav.Direction.NORTH;

A

QN=6 (7258) ______ drivers that implement the JDBC API as a mapping to another data access API, such as ODBC. Drivers of this type are generally dependent on a native library, which limits their portability. a. Type 1 b. Type 2 c. Type 3 d. Type 4

C

QN=60 (1416) Given: 10. interface Foo { int bar(); } 11. public class Sprite { 12. public int fubar( Foo foo) { return foo.bar(); } 13. public void testFoo() { 14. fubar( 15. // insert code here 16. ); 17. } 18. } Which code, inserted at line 15, allows the class Sprite to compile? (Choose one.) a. Foo { public int bar() { return 1; } } b. new Foo { public int bar() { return 1; } } c. new Foo() { public int bar(){return 1; } } d. new class Foo { public int bar() { return 1; } }

A

QN=61 (1528) Given: 10. public class ClassA { 11. public void count(int i) { 12. count(++i); 13. } 14. } And: 20. ClassA a = new ClassA(); 21. a.count(3); Which exception or error should be thrown by the virtual machine? (Choose one.) a. StackOverflowError b. NullPointerException c. NumberFormatException d. IllegalArgumentException e. ExceptionlnlnitializerError

C

QN=62 (1419) Given: 11. public abstract class Shape { 12. int x; 13. int y; 14. public abstract void draw(); 15. public void setAnchor(int x, int y) { 16. this.x = x; 17. this.y = y; 18. } 19. } and a class Circle that extends and fully implements the Shape class. Which is correct? (Choose one.) a. Shape s = new Shape(); s.setAnchor(10,10); s.draw(); b. Circle c = new Shape(); c.setAnchor(10,10); c.draw(); c. Shape s = new Circle(); s.setAnchor(10,10); s.draw(); d. Shape s = new Circle(); s->setAnchor(10,10); s->draw(); e. Circle c = new Circle(); c.Shape.setAnchor(10,10); c.Shape.draw();

A

QN=63 (1425) 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? (Choose one.) 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.

C

QN=64 (1526) 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? (Choose one.) a. Test b. Exception c. Compilation fails. d. NullPointerException

B

QN=65 (1421) 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? (Choose one.) 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.

C

QN=67 (1519) Given: 12. public class AssertStuff { 14. public static void main(String [] args) { 15. int x= 5; 16. int y= 7; 18. assert (x > y): "stuff"; 19. System.out.println("passed"); 20. } 21. } And these command line invocations: java AssertStuff java -ea AssertStuff What is the result? (Choose one.) a. Passed Stuff b. Stuff Passed c. passed An AssertionError is thrown with the word "stuff" added to the stack trace. d. passed An AssertionError is thrown without the word "stuff" added to the stack trace. e. passed An AssertionException is thrown with the word "stuff" added to the stack trace. f. passed An AssertionException is thrown without the word "stuff" added to the stack trace.

D

QN=68 (1516) 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? (Choose one.) a. collie b. harrier c. Compilation fails. d. collie harrier e. An exception is thrown at runtime.

collie harrier

QN=68 (1516) 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? (Choose one.) a. collie b. harrier c. Compilation fails. d. collie harrier e. An exception is thrown at runtime.

D

QN=69 (1414) Given: 13. public class Pass { 14. public static void main(String [] args) { 15. int x = 5; 16. Pass p = new Pass(); 17. p.doStuff(x); 18. System.out.print(" main x = "+ x); 19. } 20. 21. void doStuff(int x) { 22. System.out.print("doStuff x = "+ x++); 23. } 24. } What is the result? (Choose one.) a. Compilation fails. b. An exception is thrown at runtime. c. doStuff x = 6 main x = 6 d. doStuff x = 5 main x = 5 e. doStuff x = 5 main x = 6 f. doStuff x = 6 main x = 5

doStuff x = 5 main x = 5

QN=69 (1414) Given: 13. public class Pass { 14. public static void main(String [] args) { 15. int x = 5; 16. Pass p = new Pass(); 17. p.doStuff(x); 18. System.out.print(" main x = "+ x); 19. } 20. 21. void doStuff(int x) { 22. System.out.print("doStuff x = "+ x++); 23. } 24. } What is the result? (Choose one.) a. Compilation fails. b. An exception is thrown at runtime. c. doStuff x = 6 main x = 6 d. doStuff x = 5 main x = 5 e. doStuff x = 5 main x = 6 f. doStuff x = 6 main x = 5

A

QN=7 (1513) 1. public class A { 2. public String doit(int x, int y) { 3. return "a"; 4. } 5. 6. public String doit(int... vals) { 7. return "b"; 8. } 9. } Given: 25. A a=new A(); 26. System.out.println(a.doit(4, 5)); What is the result? (Choose one.) a. Line 26 prints "a" to System.out. b. Line 26 prints "b" to System.out. c. An exception is thrown at line 26 at runtime. d. Compilation of class A will fail due to an error in line 6.

D

QN=70 (1413) Given: 13. public class Pass { 14. public static void main(String [] args) { 15. int x = 5; 16. Pass p = new Pass(); 17. p.doStuff(x); 18. System.out.print(" main x = "+ x); 19. } 20. 21. void doStuff(int x) { 22. System.out.print("doStuffx = "+ x++); 23. } 24. } What is the result? (Choose one.) a. Compilation fails. b. An exception is thrown at runtime. c. doStuffx = 6 main x = 6 d. doStuffx = 5 main x = 5 e. doStuffx = 5 main x = 6 f. doStuffx = 6 main x = 5

A

QN=85 (236) How do you use the File class to list the contents of a directory? (Choose one.) a. String[] contents = myFile.list(); b. File[] contents = myFile.list(); c. StringBuilder[] contents = myFile.list(); d. The File class does not provide a way to list the contents of a directory.

C

QN=71 (1409) Given: 20. public class CreditCard { 22. private String cardlD; 23. private Integer limit; 24. public String ownerName; 26. public void setCardlnformation(String cardlD, 27. String ownerName, 28. Integer limit) { 29. this.cardlD = cardlD; 30. this.ownerName = ownerName; 31. this.limit = limit; 32. } 33. } Which is true? (Choose one.) a. The class is fully encapsulated. b. The code demonstrates polymorphism. c. The ownerName variable breaks encapsulation. d. The cardlD and limit variables break polymorphism. e. The setCardlnformation method breaks encapsulation.

B

QN=72 (1412) Given: 23. Object [] myObjects = { 24. new Integer(12), 25. new String("foo"), 26. new Integer(5), 27. new Boolean(true) 28. }; 29. java.util.Array.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? (Choose one.) 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.

B, C, D

QN=73 Given: (1408) 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.

A, C

QN=74 (1522) 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? (Choose one.) a. c b. a c. ab d. ac e. bc f. abc

B

QN=75 (1422) Given: 55. int []x= {1, 2,3,4, 5}; 56. int y[] =x; 57. System.out.println(y[2]); Which is true? (Choose one.) 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.

B, E

QN=76 (1518) Given: 8. public class test { 9. public static void main(String [] a) { 10. assert a.length == 1; 11. } 12.} Which two will produce an AssertionError? (Choose two.) a. java test b. java -ea test c. java test file1 d. java -ea test file1 e. java -ea test file1 file2 f. java -ea:test test file1

B, E

QN=77 (1529) 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(1,3,"24") - "89"; b. StringBuffer s = new StringBuffer("123456789"); s.delete(0,3).replace( 1,3, "24").delete(4,6); c. StringBuffer s = new StringBuffer("123456789"); s.substring(3,6).delete( 1 ,3).insert( 1, "24"); d. StringBuilder s = new StringBuilder("123456789"); s.substring(3,6).delete( 1 ,2).insert( 1, "24"); e. StringBuilder s = new StringBuilder("123456789"); s.delete(0,3).delete( 1 ,3).delete(2,5).insert( 1, "24");

String[] contents = myFile.list();

QN=85 (236) How do you use the File class to list the contents of a directory? (Choose one.) a. String[] contents = myFile.list(); b. File[] contents = myFile.list(); c. StringBuilder[] contents = myFile.list(); d. The File class does not provide a way to list the contents of a directory.

B

QN=78 (1514) Given: 10. class Line { 11. public static class Point { } 12. } 13. 14. class Triangle { 15. // insert code here 16. } Which code, inserted at line 15, creates an instance of the Point class defined in Line? (Choose one.) a. Point p = new Point(); b. Line.Point p = new Line.Point(); c. The Point class cannot be instatiated at line 15. d. Line l = new Line() ; Point p = new l.Point();

B, D

QN=79 (1406) 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]);

B, E

QN=8 (1525) 1. 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.} 25. try { 26. A a=new A(); 27. a.method1(); 28. }catch (Exception e) { 29. System.out.print("an error occurred"); 30. } Which two are true if a NullPointerException is thrown on line 3 of class C? (Choose two.) 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.

A, B, D

QN=80 (1405) Given: 11. public interface Status { 12. /* insert code here */ int MY_VALUE = 10; 13. } Which three are valid on line 12? (Choose three.) a. final b. static c. native d. public e. protected f. abstract

B

QN=81 (1523) Given: class A { public void process() { System.out.print("A "); } public static void main(String[] args) { try { ((A)new B()).process(); } catch (Exception e) { System.out.print("Exception "); } } } class B extends A { public void process() throws RuntimeException { super.process(); if (true) throw new RuntimeException(); System.out.print("B"); } } What is the result? (Choose one.) a. Exception b. A Exception c. A Exception B d. A B Exception e. Compilation fails because of an error in line: public void process() throws RuntimeException f. Compilation fails because of an error in line: try { ((A)new B()).process(); }

D

QN=82 (209) How can you ensure that multithreaded code does not deadlock? (Choose one.) a. Synchronize access to all shared variables. b. Make sure all threads yield from time to time. c. Vary the priorities of your threads. d. There is no single technique that can guarantee non-deadlocking code.

A

QN=83 (52) How can you force garbage collection of an object? (Choose one.) a. Garbage collection cannot be forced. b. Call System.gc(). c. Call System.gc(), passing in a reference to the object to be garbage- collected. d. Call Runtime.gc(). e. Set all references to the object to new values (null, for example).

D

QN=84 (208) How do you prevent shared data from being corrupted in a multithreaded environment? (Choose one.) a. Mark all variables as synchronized. b. Mark all variables as volatile. c. Use only static variables. d. Access the variables only via synchronized methods.

B

QN=86 (237) How many bytes does the following code write to file dest? (Choose one.) 1. try { 2. FileOutputStream fos = newFileOutputStream("dest"); 3. DataOutputStream dos = new DataOutputStream(fos); 4. dos.writeInt(3); 5. dos.writeFloat(0.0001f); 6. dos.close(); 7. fos.close(); 8. } 9. catch (IOException e) { } a. 2 b. 8 c. 12 d. 16 e. The number of bytes depends on the underlying system.

B

QN=87 (204) How many locks does an object have? (Choose one.) a. One b. One for each method c. One for each synchronized method d. One for each non-static synchronized method

D

QN=88 (48) If all three top-level elements occur in a source file, they must appear in which order? (Choose one.) a. Imports, package declarations, classes/interfaces/enums b. Classes/interfaces/enums, imports, package declarations c. Package declaration must come first; order for imports and class/interfaces/enum definitions is not significant d. Package declaration, imports, class/interface/enum definitions. e. Imports must come first; order for package declarations and class/interface/enum definitions is not significant

B

QN=89 (123) If class Y extends class X, the two classes are in different packages, and class X has a protected method called abby(), then any instance of Y may call the abby() method of any other instance of Y. a. True b. False

B, E, F

QN=9 1. try { (152) 2. // assume s is previously defined 3. URL u = new URL(s); 4. // in is an ObjectInputStream 5. Object o = in.readObject(); 6. System.out.println("Success"); 7. } 8. catch (MalformedURLException e) { 9. System.out.println("Bad URL"); 10. } 11. catch (StreamCorruptedException e) { 12. System.out.println("Bad file contents"); 13. } 14. catch (Exception e) { 15. System.out.println("General exception"); 16. } 17. finally { 18. System.out.println("Doing finally part"); 19. } 20. System.out.println("Carrying on"); Where: IOException extends Exception StreamCorruptedException extends IOException MalformedURLException extends IOException What lines are output if the constructor at line 3 throws a MalformedURLException? (Choose three.) a. Success b. Bad URL c. Bad file contents d. General exception e. Doing finally part f. Carrying on

A

QN=90 (197) If you attempt to compile and execute the following application, will it ever print out the message In xxx? 1. class TestThread3 extends Thread { 2. public void run() { 3. System.out.println("Running"); 4. System.out.println("Done"); 5. } 6. 7. private void xxx() { 8. System.out.println("In xxx"); 9. } 10. 11. public static void main(String args[]) { 12. TestThread3 ttt = new TestThread3(); 13. ttt.xxx(); 14. ttt.start(); 12. } 13. } a. Yes b. No

C

QN=91 (1877) If you need a Set implementation that provides value-ordered iteration, which class should you use? (Choose one.) a. HashSet b. LinkedHashSet c. TreeSet

A

QN=92 (1875) In order for objects in a List to be sorted, those objects must implement which interface and method? (Choose one.) a. Comparable interface and its compareTo method. b. Comparable interface and its compare method c. Compare interface and its compareTo method d. Comparable interface and its equals method

We use readObject() method of ObjectOutputStream class to deserialize.

Select INCORRECT statement about deserialize. (choose 1) a. Any JVM that tries to deserialize an object must have access to that object's class definition. b. We use readObject() method of ObjectOutputStream class to deserialize. c. The readObject method deserializes the next object in the stream and traverses its references to other objects recursively to deserialize all objects that are reachable from it.

All the others choices

Select correct statement(s) about remote class.(choose one) a. It must extend java.rmi.server.UnicastRemoteObject. b. It must implement the remote interface. c. It is the class whose methods provide services to clients. d. All the others choices

All the others choices

Select correct statements about remote interface. (choose 1) a. A remote interface is an interface that describes the remotely accessible methods of a remote object. b. All remote interfaces must extend java.rmi.Remote. c. All methods in a remote interface must throw java.rmi.RemoteException d. The type of a remote reference is a remote interface e. All the others choices

E

Select the order of access modifiers from least restrictive to most restrictive. A ⦁ public, private, protected, default B ⦁ default, protected, private, public C ⦁ public, default, protected, private D ⦁ default, public, protected, private E ⦁ public, protected, default, private

A, C, D

Select the valid primitive data types. (Select all that apply) A ⦁ boolean B ⦁ bit C ⦁ char D ⦁ float E ⦁ All of the above

The style, which is one of SHORT, MEDIUM, LONG, or FULL The locale

Suppose you want to use a DateFormat to format an instance of Date. What factors influence the string returned by DateFormat's format() method? A. The operating system B. The style, which is one of SHORT, MEDIUM, or LONG C. The style, which is one of SHORT, MEDIUM, LONG, or FULL D. The locale

D

Suppose you want to write a class that offers static methods to compute hyperbolic trigonometric functions. You decide to subclass java.lang.Math and provide the new functionality as a set of static methods. Which one statement is true about this strategy? a. The strategy works. b. The strategy works, provided the new methods are public. c. The strategy works, provided the new methods are not private. d. The strategy fails because you cannot subclass java.lang.Math. e. The strategy fails because you cannot add static methods to a subclass.

public boolean addAll(Collection<? extends E> c)

The declaration of the java.util.Collection interface is interface Collection <E> The addAll() method of that interface takes a single argument, which is a reference to a collection whose elements are compatible with E. What is the declaration of the addAll() method? A. public boolean addAll(Collection c) B. public boolean addAll(Collection c extends E) C. public boolean addAll(Collection ? extends E) D. public boolean addAll(Collection<? extends E> c)

B

The developer can force garbage collection by calling System.gc(). A ⦁ True B ⦁ False

DataPacket and DataSocket

There are two classes in Java to enable communication using datagrams namely. a. DataPacket and DataSocket b. DatagramPacket and DatagramSocket c. DatagramPack and DatagramSock

A

This question concerns the following class definition: 1. package abcde; 2. 3. public class Bird { 4. protected static int referenceCount = 0; 5. public Bird() { referenceCount++; } 6. protected void fly() { /* Flap wings, etc. */ } 7. static int getRefCount() { return referenceCount; } 8. } Which statement is true about class Bird and the following class Nightingale? 1. package singers; 2. 3. class Nightingale extends abcde.Bird { 4. Nightingale() { referenceCount++; } 5. 6. public static void main(String args[]) { 7. System.out.print("Before: " + referenceCount); 8. Nightingale florence = new Nightingale(); 9. System.out.println(" After: " + referenceCount); 10. florence.fly(); 11. } 12. } A. The program will compile and execute. The output will be Before: 0 After: 2. B. The program will compile and execute. The output will be Before: 0 After: 1. C. Compilation of Nightingale will fail at line 4 because static members cannot be overridden. D. Compilation of Nightingale will fail at line 10 because method fly() is protected in the superclass. E. Compilation of Nightingale will succeed, but an exception will be thrown at line 10, because method fly() is protected in the superclass.

protocol:subprotocol:datasoursename

URL referring to databases use the form: a. protocol:subprotocol:datasoursename b. protocol:datasoursename c. jdbc:odbc:datasoursename d. jdbc:datasoursename

The output is i = 20.

What does the following code fragment print out at line 9? (Choose one.) 1. FileOutputStream fos = new FileOutputStream("xx"); 2. for (byte b=10; b<50; b++) 3. fos.write(b); 4. fos.close(); 5. RandomAccessFile raf = new RandomAccessFile("xx", "r"); 6. raf.seek(10); 7. int i = raf.read(); 8. raf.close() 9. System.out.println("i = " + i); a. The output is i = 30. b. The output is i = 20. c. The output is i = 10. d. There is no output because the code throws an exception at line 1. e. There is no output because the code throws an exception at line 5.

-1

What does the following code print? public class A { static int x; public static void main(String[] args) { A that1 = new A(); A that2 = new A(); that1.x = 5; that2.x = 1000; x = -1; System.out.println(x); } } a. 0 b. 5 c. 1000 d. -1

An exception is thrown at line 7.

What happens when you try to compile and run this application? (Choose one.) 1. import java.util.*; 2. 3. public class Apple { 4. public static void main(String[] a) { 5. Set<Apple> set = new TreeSet<Apple>(); 6. set.add(new Apple()); 7. set.add(new Apple()); 8. set.add(new Apple()); 9. } 10. } a. Compiler error. b. An exception is thrown at line 6. c. An exception is thrown at line 7. d. An exception is thrown at line 8. e. No exception is thrown.

Class Zebra generates a compiler error.

What happens when you try to compile the following code and run the Zebra application? class Animal { float weight; Animal(float weight) { this.weight = weight; } } class Zebra extends Animal { public static void main(String[] args) { Animal a = new Animal(222.2f); Zebra z = new Zebra(); } } A. Class Animal generates a compiler error. B. Class Zebra generates a compiler error. C. The code compiles without error. The application throws an exception when the Animal constructor is called. D. The code compiles without error. The application throws an exception when the Zebra constructor is called. E. The code compiles and runs without error.

-5

What is -15 % -10? A. 0 B. 5 C. 10 D. -5 E. -1

-13

What is -50 >> 2 a. A negative number with very large magnitude. b. A positive number with very large magnitude. c. -13 d. -25 e. 13 f. 25

-3

What is -8 % 5? a. -3 b. 3 c. -2 d. 2

3

What is 7 % -4? a. -3 b. 3 c. -4 d. 4

Negative

When a negative byte is cast to a long, what are the possible values of the result? (Choose one.) a. Positive b. Zero c. Negative d. All the above

All the above

When a negative long is cast to a byte, what are the possible values of the result? (Choose one.) a. Positive b. Zero c. Negative d. All the above

float

When a short is added to a float, what is the type of the result? a. short b. int c. float d. You can't add a short to a float.

writing a line separator to the stream

When comparing java.io.BufferedWriter to java.io.FileWriter, which capability exists as a method in only one of the two? (Choose one.) 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

After line 3

When does the string created on line 2 become eligible for garbage collection? 1. String s = "aaa"; 2. String t = new String(s); 3. t += "zzz"; 4. t = t.substring(0); 5. t = null; A. After line 3 B. After line 4 C. After line 5 D. The string created on line 2 does not become eligible for garbage collection in this code.

When the Set is a TreeSet

When should objects stored in a Set implement the java.util.Comparable interface? A. Always B. When the Set is generic C. When the Set is a HashSet D. When the Set is a TreeSet E. Never

bytecode

When you compile a program written in the Java programming language, the compiler converts the human-readable source file into platform- independent code that a Java Virtual Machine can understand. What is this platform-independent code called? a. bytecode b. binary code c. machine code d. cpu instruction

C

Which access modifier allows you to access method calls in libraries not created in Java? A ⦁ public B ⦁ static C ⦁ native D ⦁ transient E ⦁ volatile

None of the above.

Which of the following are legal loop definitions? (Choose one.) a. while (int a = 0) { /* whatever */ } b. while (int a == 0) { /* whatever */ } c. do { /* whatever */ } while (int a = 0) d. do { /* whatever */ } while (int a == 0) e. for (int a==0; a<100; a++) { /* whatever */ } f. None of the above.

double d = 1.2d; double d = 1.2D;

Which of the following are legal? (Choose two.) a. double d = 1.2d; b. double d = 1.2D; c. double d = 1.2d5; d. double d = 1.2D5;

int c = 0xabcd; int d = 0XABCD;

Which of the following are legal? (Choose two.) a. int a = abcd; b. int b = ABCD; c. int c = 0xabcd; d. int d = 0XABCD; e. int f = 0ABCD;

All the above

Which of the following are methods of the java.util.SortedSet interface? (Choose one.) a. first b. last c. headSet d. tailSet e. subSet f. All the above

All the above

Which of the following are true? (Choose one.) a. System.out has a println() method. b. System.out has a format() method. c. System.err has a println() method. d. System.err has a format () method. e. All the above

An enum may contain public method definitions. An enum may contain private data.

Which of the following are true? (Choose two.) a. An enum definition should declare that it extends java.lang.Enum. b. An enum may be subclassed. c. An enum may contain public method definitions. d. An enum may contain private data.

FileInputStream

Which of the following are valid arguments to the DataInputStream constructor? (Choose one.) a. File b. FileReader c. FileInputStream d. RandomAccessFile

All the above

Which of the following calls may be made from a non-static synchronized method? (Choose one.) a. A call to the same method of the current object. b. A call to the same method of a different instance of the current class. c. A call to a different synchronized method of the current object. d. A call to a static synchronized method of the current class. e. All the above

java.util.ArrayList java.util.Stack

Which of the following classes implement java.util.List? (Choose two.) a. java.util.ArrayList b. java.util.HashMap c. java.util.TreeSet d. java.util.Stack

LinkedList

Which of the following classes implements a FIFO Queue? (Choose one.) a. HashSet b. LinkedList c. PriorityQueue d. CopyOnWriteArraySet

Set

Which of the following interfaces does not allow duplicate objects? (Choose one.) a. Queue b. Set c. List

/* comment

Which of the following is NOTa valid comment: a. /*** comment **/ b. /** comment **/ c. /* comment d. // comment

Preconditions of a public method

Which of the following is not appropriate situations for assertions? (Choose one) a. Preconditions of a public method b. Postconditions of a public method c. Preconditions of a private method d. Postconditions of a private method

Throw java.lang.IllegalArgumentException.

Which of the following is the most appropriate way to handle invalid arguments in a public method? a. Throw java.lang.InvalidArgumentException. b. Throw java.lang.IllegalArgumentException. c. Check for argument validity in an assert statement, which throws AssertionError when the arguments are invalid. d. Use non-assert code to check for argument validity. If invalid arguments are detected, explicitly throw AssertionError.

None of the above

Which of the following is true? (Choose one.) a. Readers have methods that can read and return floats and doubles. b. Readers have methods that can read and return floats. c. Readers have methods that can read and return doubles. d. Readers have methods that can read and return ints. e. None of the above

All the above

Which of the following is(are) true? (Choose one.) a. An enum definition may contain the main() method of an application. b. You can call an enum's toString() method. c. You can call an enum's wait() method. d. You can call an enum's notify() method. e. All the above

B

Which of the following keywords is used to invoke a method in the parent class? A ⦁ this B ⦁ super C ⦁ final D ⦁ static

All of the above

Which of the following may legally appear as the new type (between the parentheses) in a cast operation? (Choose one.) a. Abstract classes b. Final classes c. Primitives d. All of the above

All of the others

Which of the following may legally appear as the new type (between the parentheses) in a cast operation? (Choose one.) a. Classes b. Interfaces c. Arrays of classes d. Arrays of interfaces e. All of the others

Checked exceptions

Which of the following should always be caught? (Choose one.) a. Runtime exceptions b. Checked exceptions c. Assertion errors d. Errors other than assertion errors

Only statement 2

Which of the following statements are true? 1)An abstract class may not have any final methods. 2)A final class may not have any abstract methods. a. Only statement 1 b. Only statement 2 c. Both statement 1 and 2

D

Which of the following statements are true? (Select all that apply.) A ⦁ A final object's data cannot be changed. B ⦁ A final class can be subclassed. C ⦁ A final method cannot be overloaded. D ⦁ A final object cannot be reassigned a new address in memory. E ⦁ None of the above.

None of the above

Which of the statements below are true? (Choose one.) a. To change the current working directory, call the setWorkingDirectory() method of the File class. b. To change the current working directory, call the cd() method of the File class. c. To change the current working directory, call the changeWorkingDirectory() method of the File class. d. None of the above

Compilation succeeds. No exception is thrown during execution.

Which one statement is true about the following code fragment? 1. String s = "FPT"; 2. StringBuffer s1 = new StringBuffer("FPT"); 3. if (s.equals(s1)) 4. s1 = null; 5. if (s1.equals(s)) 6. s = null; a. Compilation fails at line 1 because the String constructor must be called explicitly. b. Compilation fails at line 3 because s and s1 have different types. c. Compilation succeeds. During execution, an exception is thrown at line 3. d. Compilation succeeds. During execution, an exception is thrown at line 5. e. Compilation succeeds. No exception is thrown during execution.

It always returns 0.

Which statement is true about the following method? int selfXor(int i) { return i ^ i; } a. It always returns 0. b. It always returns 1. c. It always an int where every bit is 1. d. The returned value varies depending on the argument.

The code compiles and execution produces the output x = 3.

Which statement is true about this application? (Choose one.) 1. class StaticStuff 2 { 3. static int x = 10; 4. 5. static { x += 5; } 6. 7. public static void main(String args[]) 8. { 9. System.out.println("x = " + x); 10. } 11. 12. static {x /= 5; } 13. } a. Lines 5 and 12 will not compile because the method names and return types are missing. b. Line 12 will not compile because you can only have one static initializer. c. The code compiles and execution produces the output x = 10. d. The code compiles and execution produces the output x = 15. e. The code compiles and execution produces the output x = 3.

The program compiles and the output is x = 104.

Which statement is true about this code? (Choose one.) 1. class HasStatic 2. { 3. private static int x = 100; 4. 5. public static void main(String args[]) 6. { 7. HasStatic hs1 = new HasStatic(); 8. hs1.x++; 9. HasStatic hs2 = new HasStatic(); 10. hs2.x++; 11. hs1 = new HasStatic(); 12. hs1.x++; 13. HasStatic.x++; 14. System.out.println("x = " + x); 15. } 16. } a. Line 8 will not compile because it is a static reference to a variable. b. Line 13 will not compile because it is a static reference to a private variable. c. The program compiles and the output is x = 102. d. The program compiles and the output is x = 103. e. The program compiles and the output is x = 104.

B

________ is a set of java API for executing SQL statements. A. ODBC B. JDBC C. JAVADB D. None of the others

B

________ method is used to wait for a client to initiate communications. A. wait() B. accept() C. listen()

1. Which of these have highest precedence? a) () b) ++ c) *

a

What is the output of this program? class increment { public static void main(String args[]) { int g = 3; System.out.print(++g * 8); } } a) 25 b) 24 c) 32 d) 33

c

What is the output of this program? class Modulus { public static void main(String args[]) { double a = 25.64; int b = 25; a = a % 10; b = b % 10; System.out.println(a + " " + b); } } a) 5.640000000000001 5 b) 5.640000000000001 5.0 c) 5 5 d) 5 5.640000000000001

a

5. Which of these can not be used for a variable name in Java? a) identifier b) keyword c) identifier & keyword d) None of the mentioned

b

5. Which of these literals can be contained in a data type float variable? a) 1.7e-308 b) 3.4e-038 c) 1.7e+308 d) 3.4e-050

b

5. Which of these statement is incorrect? a) switch statement is more efficient than a set of nested ifs. b) two case constants in the same switch can have identical values. c) switch statement can only test for equality, whereas if statement can evaluate any type of boolean expression. d) it is possible to create a nested switch statements. View Answer

b

6. What is the output of this program? class selection_statements { public static void main(String args[]) { int var1 = 5; int var2 = 6; if ((var2 = 1) == var1) System.out.print(var2); else System.out.print(++var2); } } a) 1 b) 2 c) 3 d) 4

b

7. What is the output of this program? class array_output { public static void main(String args[]) { int array_variable [] = new int[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = i/2; array_variable[i]++; System.out.print(array_variable[i] + " "); i++; } } } a) 0 2 4 6 8 b) 1 2 3 4 5 c) 0 1 2 3 4 5 6 7 8 9 d) 1 2 3 4 5 6 7 8 9 10

b

7. What is the output of this program? class comma_operator { public static void main(String args[]) { int sum = 0; for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1) sum += i; System.out.println(sum); } } a) 5 b) 6 c) 14 d) compilation error

b

7. What is the output of this program? class conversion { public static void main(String args[]) { double a = 295.04; int b = 300; byte c = (byte) a; byte d = (byte) b; System.out.println(c + " " + d); } } a) 38 43 b) 39 44 c) 295 300 d) 295.04 300

b

8. What is the output of this program? class conversion { public static void main(String args[]) { double a = 295.04; int b = 300; byte c = (byte) a; byte d = (byte) b; System.out.println(c + " " + d); } } a) 38 43 b) 39 44 c) 295 300 d) 295.04 300

b

9. What is the output of this program? class Output { public static void main(String args[]) { int x , y = 1; x = 10; if (x != 10 && x / 0 == 0) System.out.println(y); else System.out.println(++y); } } a) 1 b) 2 c) Runtime error owing to division by zero in if condition. d) Unpredictable behavior of program.

b

9. What is the output of this program? class Output { public static void main(String args[]) { int x, y = 1; x = 10; if (x != 10 && x / 0 == 0) System.out.println(y); else System.out.println(++y); } } a) 1 b) 2 c) Runtime error owing to division by zero in if condition. d) Unpredictable behavior of program.

b

5. Which of these statements are incorrect? a) Equal to operator has least precedence. b) Brackets () have highest precedence. c) Division operator, /, has higher precedence than multiplication operator. d) Addition operator, +, and subtraction operator have equal precedence

c

5. Which one is a valid declaration of a boolean? a) boolean b1 = 1; b) boolean b2 = 'false'; c) boolean b3 = false; d) boolean b4 = 'true'

c

6. What is the output of this program? class evaluate { public static void main(String args[]) { int a[] = {1,2,3,4,5}; int d[] = a; int sum = 0; for (int j = 0; j < 3; ++j) sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]); System.out.println(sum); } } a) 38 b) 39 c) 40 d) 41

c

6. What is the output of this program? class operators { public static void main(String args[]) { int var1 = 5; int var2 = 6; int var3; var3 = ++ var2 * var1 / var2 + var2; System.out.print(var3); } } a) 10 b) 11 c) 12 d) 56

c

6. Which data type value is returned by all transcendental math functions? a) int b) float c) double d) long

c

7. Consider the following application: 1. class Q7 { 2. public static void main(String args[]) { 3. double d = 12.3; 4. Decrementer dec = new Decrementer(); 5. dec.decrement(d); 6. System.out.println(d); 7. } 8. } 9. 10. class Decrementer { 11. public void decrement(double decMe) { 12. decMe = decMe - 1.0; 13. } 14. } What value is printed out at line 6? A. 0.0 B. 1.0 C. 12.3 D. 11.3

c

8. What is the output of this program? class A { final public int calculate(int a, int b) { return 1; } } class B extends A { public int calculate(int a, int b) { return 2; } } public class output { public static void main(String args[]) { B object = new B(); System.out.print("b is " + b.calculate(0, 1)); } } a) b is : 2 b) b is : 1 c) Compilation Error. d) An exception is thrown at runtime.

c

8. What is the output of this program? class jump_statments { public static void main(String args[]) { int x = 2; int y = 0; for ( ; y < 10; ++y) { if (y % x == 0) continue; else if (y == 8) break; else System.out.print(y + " "); } } } a) 1 3 5 7 b) 2 4 6 8 c) 1 3 5 7 9 d) 1 2 3 4 5 6 7 8 9

c

8. What is the output of this program? class ternary_operator { public static void main(String args[]) { int x = 3; int y = ~ x; int z; z = x > y ? x : y; System.out.print(z); } } a) 0 b) 1 c) 3 d) -4

c

9. What is the output of this program? class increment { public static void main(String args[]) { int g = 3; System.out.print(++g * 8); } } a) 25 b) 24 c) 32 d) 33

c

9. Which of these lines of code will give better performance? 1. a | 4 + c >> b & 7; 2. (a | ((( 4 * c ) >> b ) & 7 )) a) 1 will give better performance as it has no parentheses. b) 2 will give better performance as it has parentheses. c) Both 1 & 2 will give equal performance. d) Dependent on the computer system.

c

What is the output of this program? class Output { public static void main(String args[]) { int x , y; x = 10; x++; --x; y = x++; System.out.println(x + " " + y); } } a) 11 11 b) 10 10 c) 11 10 d) 10 11

c

What is the output of this program? class average { public static void main(String args[]) { double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5}; double result; result = 0; for (int i = 0; i < 6; ++i) result = result + num[i]; System.out.print(result/6); } } a) 16.34 b) 16.566666644 c) 16.46666666666667 d) 16.46666666666666

c

What is the output of this program? class bitwise_operator { public static void main(String args[]) { int var1 = 42; int var2 = ~var1; System.out.print(var1 + " " + var2); } } a) 42 42 b) 43 43 c) 42 -43 d) 42 43

c

What is the output of this program? class increment { public static void main(String args[]) { double var1 = 1 + 5; double var2 = var1 / 4; int var3 = 1 + 5; int var4 = var3 / 4; System.out.print(var2 + " " + var4); } } a) 1 1 b) 0 1 c) 1.5 1 d) 1.5 1.0

c

1. What is the numerical range of a char in Java? a) -128 to 127 b) 0 to 256 c) 0 to 32767 d) 0 to 65535

d

1. Which of the following can be operands of arithmetic operators? a) Numeric b) Boolean c) Characters d) Both Numeric & Characters

d

10. What is the output of this program? class Output { public static void main(String args[]) { int a = 5; int b = 10; first: { second: { third: { if (a == b >> 1) break second; } System.out.println(a); } System.out.println(b); } } } a) 5 10 b) 10 5 c) 5 d) 10

d

10. What is the output of this program? class c { public void main( String[] args ) { System.out.println( "Hello" + args[0] ); } } a) Hello c b) Hello c) Hello world d) Runtime Error.

d

15. Which of the following operations might throw an ArithmeticException? A. >> B. >>> C. << D. None of these

d

16. Which of the following operations might throw an ArithmeticException? A. + B. - C. * D. / E. None of these

d

2. What is the prototype of the default constructor of this class? public class prototype { } a) prototype( ) b) prototype(void) c) public prototype(void) d) public prototype( )

d

2. Which of these can be returned by the operator & ? a) Integer b) Boolean c) Character d) Integer or Boolean

d

2. Which of these is returned by greater than, <, and equal to, ==, operator? a) Integers b) Floating - point numbers c) Boolean d) None of the mentioned [expand title="View Answer"] Answer:c Explanation: All relational operators return a boolean value i:e true and false. [/expand] 3. Which of the following operators can operate on a boolean variable? 1. && 2. == 3. ?: 4. += a) 3 & 2 b) 1 & 4 c) 1, 2 & 4 d) 1, 2 & 3 [expand title="View Answer"] Answer: d Explanation: Operator Short circuit AND, &&, equal to, == , ternary if-then-else, ?:, are boolean logical operators. += is an arithmetic operator it can operate only on numeric values. [/expand] 4. Which of these operators can skip evaluating right hand operand? a) ! b) | c) & d) && [expand title="View Answer"] Answer: d Explanation: Operator short circuit and, &&, and short circuit or, ||, skip evaluating right hand operand when output can be determined by left operand alone. [/expand] 5. Which of these statement is correct? a) true and false are numeric values 1 and 0. b) true and false are numeric values 0 and 1. c) true is any non zero value and false is 0. d) true and false are non numeric values. [expand title="View Answer"] Answer: d Explanation: true and false are keywords, they are non numeric values which do no relate to zero or non zero numbers. true and false are boolean values. [/expand] 6. What is the output of this program? class Relational_operator { public static void main(String args[]) { int var1 = 5; int var2 = 6; System.out.print(var1 > var2); } } a) 1 b) 0 c) true d) false

d

3. Literals in java must be appended by which of these? a) L b) l c) D d) L and I

d

3. What is the value stored in x in following lines of code? int x, y, z; x = 0; y = 1; x = y = z = 8; a) 0 b) 1 c) 9 d) 8

d

3. Which of the following are legal lines of Java code? 1. int w = (int)888.8; 2. byte x = (byte)100L; 3. long y = (byte)100; 4. byte z = (byte)100L; a) 1 and 2 b) 2 and 3 c) 3 and 4 d) All statements are correct.

d

4. Literal can be of which of these data types? a) integer b) float c) boolean d) all of the mentioned

d

4. Which of these jump statements can skip processing remainder of code in its body for a particular iteration? a) break b) return c) exit d) continue

d

4. Which of these occupy first 0 to 127 in Unicode character set used for characters in Java? a) ASCII b) ISO-LATIN-1 c) None of the mentioned d) ASCII and ISO-LATIN1

d

5. Which of these statements are incorrect? a) Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms. b) Assignment operators run faster than their equivalent long forms. c) Assignment operators can be used only with numeric and character data type. d) None

d

5. Which of these statements are incorrect? a) The left shift operator, <<, shifts all of the bite in a value to the left specified number of times. b) The right shift operator, >>, shifts all of the bite in a value to the right specified number of times. c) The left shift operator can be used as an alternative to multiplying by 2. d) The right shift operator automatically fills the higher order bits with 0.

d

7. What is the output of this program? class bool_operator { public static void main(String args[]) { boolean a = true; boolean b = !true; boolean c = a | b; boolean d = a & b; boolean e = d ? b : c; System.out.println(d + " " + e); } } a) false false b) true ture c) true false d) false true

d

7. What is the output of this program? class operators { public static void main(String args[]) { int x = 8; System.out.println(++x * 3 + " " + x); } } a) 24 8 b) 24 9 c) 27 8 d) 27 9

d

8. What is the output of this program? class leftshift_operator { public static void main(String args[]) { byte x = 64; int i; byte y; i = x << 2; y = (byte) (x << 2) System.out.print(i + " " + y); } } a) 0 64 b) 64 0 c) 0 256 d) 256 0

d

8. What is the output of this program? class variable_scope { public static void main(String args[]) { int x; x = 5; { int y = 6; System.out.print(x + " " + y); } System.out.println(x + " " + y); } } a) 5 6 5 6 b) 5 6 5 c) Runtime error d) Compilation error

d

9. What is the output of this program? class booloperators { public static void main(String args[]) { boolean var1 = true; boolean var2 = false; System.out.println((var2 & var2)); } } a) 0 b) 1 c) true d) false

d

9. What is the output of this program? class main_arguments { public static void main(String [] args) { String [][] argument = new String[2][2]; int x; argument[0] = args; x = argument[0].length; for (int y = 0; y < x; y++) System.out.print(" " + argument[0][y]); } } a) 1 1 b) 1 0 c) 1 0 3 d) 1 2 3

d

9. Which of these is incorrect string literal? a) "Hello World" b) "Hello\nWorld" c) "\"Hello World\"" d) "Hello world"

d

What is the output of this program? class Output { public static void main(String args[]) { int a = 1; int b = 2; int c; int d; c = ++b; d = a++; c++; b++; ++a; System.out.println(a + " " + b + " " + c); } } a) 3 2 4 b) 3 2 3 c) 2 3 4 d) 3 4 4

d

Line 4 Line 6

public class Test{ public static void main(String[] args){ String s1 = "xyz"; String s2 = "xyz"; if (s1 == s2) System.out.println("Line 4"); if (s1.equals(s2)) System.out.println("Line 6"); } } What is the output? a. Line 4 Line 6 b. Line 4 c. Line 6 d. No output, compile error e. No output

Line 6

public class Test{ public static void main(String[] args){ String s1 = "xyz"; String s2 = new String("xyz"); if (s1 == s2) System.out.println("Line 4"); if (s1.equals(s2)) System.out.println("Line 6"); } } What is the output? a. Line 4 Line 6 b. Line 4 c. Line 6 d. No output, compile error e. No output


Related study sets

CH 32 Assessment of Hematologic Function and Treatment Modalities (E1)

View Set

Personal Finance Ch 2 Financial Aspects of Career Planning

View Set

BUS 5113 Research Methods Chapter 4

View Set

MKT 402 consumer behavior final (WIP) ADD MORE TO IT

View Set

CH.16 PUBLIC HEALTH ENEMY NO.2 AND GROWING: POOR DIET AND PHYSICAL INACTIVITY

View Set

Solids, Liquids, Gases and Gas Laws

View Set

International Finance Test Questions

View Set

Promulgated Contracts - Test Questions Review

View Set

Caring for the Client with Diabetes Mellitus

View Set

LANDMARK CASES (6) (Right of Accused)

View Set