Page: 3
2. Declarations and Access Control
Q: 6 Given: 11. class Cup { } 12. class PoisonCup extends Cup { } ... 21. public void takeCup(Cup c) { 22. if (c instanceof PoisonCup) { 23. System.out.println("Inconceivable!"); 24. } else if (c instanceof Cup) { 25. System.out.println("Dizzying intellect!"); 26. } else { 27. System.exit(0); 28. } 29. } And the execution of the statements: Cup cup = new PoisonCup(); takeCup(cup); What is the output? A. Inconceivable! B. Dizzying intellect! C. The code runs with no output. D. An exception is thrown at runtime. E. Compilation fails because of an error in line 22. Answer: A Q: 7 Click the Exhibit button. public class A { private int counter=0; public static int getInstanceCount() { return counter; } public A() { counter++; } } Given this code from Class B: 25. A a1 = new A(); 26. A a2 = new A(); 27. A a3 = new A(); 28. System.out.println(A.getInstanceCount()); What is the result? A. Compilation of class A fails. B. Line 28 prints the value 3 to System.out. C. Line 28 prints the value 1 to System.out. D. A runtime error occurs when line 25 executes. E. Compilation fails because of an error on line 28. Answer: A Q:8 Given: 11. String[] elements = { "for", "tea", "too" }; 12. String first = (elements.length > 0) ? elements[0] : null; What is the result? A. Compilation fails. B. An exception is thrown at runtime. C. The variable first is set to null. D. The variable first is set to elements[0]. Answer: D Q:09 Given: 11. interface DeclareStuff { 12. public static final int EASY = 3; 13. void doStuff(int t); } 14. public class TestDeclare implements DeclareStuff { 15. public static void main(String [] args) { 16. int x = 5; 17. new TestDeclare().doStuff(++x); 18. } 19. void doStuff(int s) { 20. s += EASY + ++s; 21. System.out.println("s " + s); 22. } 23. } What is the result? A. s 14 B. s 16 C. s 10 D. Compilation fails. E. An exception is thrown at runtime. Answer: D Q: 10 Given: 1. public class TestString1 { 2. public static void main(String[] args) { 3. String str = "420"; 4. str += 42; 5. System.out.print(str); 6. } 7. } What is the output? A. 42 B. 420 C. 462 D. 42042 E. Compilation fails. F. An exception is thrown at runtime. Answer: D Q: 11 Given: 11. class Converter { 12. public static void main(String[] args) { 13. Integer i = args[0]; 14. int j = 12; 15. System.out.println("It is " + (j==i) + " that j==i."); 16. } 17. } What is the result when the programmer attempts to compile the code and run it with the command java Converter 12? A. It is true that j==i. B. It is false that j==i. C. An exception is thrown at runtime. D. Compilation fails because of an error in line 13. Answer: D Q: 12 Given: 10. int x = 0; 11. int y = 10; 12. do { 13. y--; 14. ++x; 15. } while (x < 5); 16. System.out.print(x + "," + y); What is the result? A. 5,6 B. 5,5 C. 6,5 D. 6,6 Answer: B
Page: 3
1
2
3
4
5
6
|