Java PRO192

Ace your homework & exams now with Quizwiz!

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

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)

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

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

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.

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.

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

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.

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

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

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

Classes Data Methods

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

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

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

None of these

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

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.

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.

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

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.

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

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

/

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.

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

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

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

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

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.

name() toString()

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

char c = '\u1234';

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

A reference

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

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

After line 2 executes, the StringBuffer object is eligible for garbage collection.

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.

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

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

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.

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.

-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

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.

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

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.

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

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

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

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.

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.

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.

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

int x = -1; x = x >>> 5;

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;

Transient variables are not serialized.

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.

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.

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.

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

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.

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.

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.

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

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.

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.

False

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

12.3

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

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

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

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

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.

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

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

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

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.

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.

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

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.

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

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.

Yes.

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

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

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

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

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

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

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.

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

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

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

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

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.

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.

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.

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

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

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.

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.

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.

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

No

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

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

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


Related study sets

Exam 3 ch 20 diabetic emergencies

View Set

Chapter 35: Assessment of Immune function

View Set

2130 Q2, econ exam 1, Managerial Economics Exam 1, UNCC Econ 3125 Final Exam MC Prep, ECON 3125 Final Exam, exam 1 study, ECON 380, CHP 2 MC, ECON 3125 Metzgar, MGMT Econ final multiple choice, Managerial Economics Final, econ quiz 1 review, Econ Tes...

View Set

Chap 11 Texas Statutes and rules pertinent to life insurance only

View Set

MICROSOFT WORD ADVANCED SKILLS / HYPERLINKS

View Set