JAVA Review Questions 3

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

BCDF

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

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.

ABD

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.

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.

E

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

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

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

ABD

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 += '!')

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.

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

F

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.

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.

ABE

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

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

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

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

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

ABE

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

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

F

2. 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 1 B. Line 2 C. Line 3 D. Line 4 E. Line 5 F. Line 6 G. Line 7 H. Line 8 I. Line 9

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.

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

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

B

3. Will the following code compile? 1. byte b = 2; 2. byte b1 = 3; 3. b = b * b1; A. Yes B. No

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

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

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.

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

BEF

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

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.

AEF

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 methods at lines 3 and 5 complete successfully without throwing any exceptions? (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

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

BCD

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


संबंधित स्टडी सेट्स

Chapter 18: Drugs used for Seizure Disorders

View Set

Unit 2: High-risk Intrapartum 40%

View Set

100 szónak is egy a vége - Zoli

View Set

Vállalatgazdaságtan 14 kérdés

View Set

PCM - Approach to Pediatric Physical Examination (04/26/18)

View Set

OT Final Exam: Book Themes and Classifications

View Set