Page: 4
2. Declarations and Access Control
Q: 13 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? A. public interface B extends A {} B. public interface B implements A {} C. public interface B instanceOf A {} D. public interface B inheritsFrom A {} Answer: A Q: 14 Given: 11. public enum Title { 12. MR("Mr."), MRS("Mrs."), MS("Ms."); 13. private final String title; 14. private Title(String t) { title = t; } 15. public String format(String last, String first) { 16. return title + " " + first + " " + last; 17. } 18. } 19. public static void main(String[] args) { 20. System.out.println(Title.MR.format("Doe", "John")); 21. } What is the result? A. Mr. John Doe B. An exception is thrown at runtime. C. Compilation fails because of an error in line 12. D. Compilation fails because of an error in line 15. E. Compilation fails because of an error in line 20. Answer: A Q: 15 Given: 1. package test; 2. 3. class Target { 4. public String name = "hello"; 5. } What can directly access and change the value of the variable name? A. any class B. only the Target class C. any class in the test package D. any class that extends Target Answer: C Q: 16 Given: 11. public class Ball{ 12. public enum Color { RED, GREEN, BLUE }; 13. public void foo(){ 14. // insert code here 15. { System.out.println(c); } 16. } 17. } Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE? A. for( Color c : Color.values() ) B. for( Color c = RED; c <= BLUE; c++ ) C. for( Color c ; c.hasNext() ; c.next() ) D. for( Color c = Color[0]; c <= Color[2]; c++ ) E. for( Color c = Color.RED; c <= Color.BLUE; c++ ) Answer: A Q: 17 Click the Task button. Solution: package alpha; public class Alpha{ private String alpha; public Alpha( ){ this("A") ; } protected Alpha(String a){ alpha=a; } } package beta; public class Beta extends alpha.Alpha{ private Beta(String a){ super(a); } } Q: 18 Given: 1. public class Target { 2. private int i = 0; 3. public int addOne(){ 4. return ++i; 5. } 6. } And: 1. public class Client { 2. public static void main(String[] args){ 3. System.out.println(new Target().addOne()); 4. } 5. } Which change can you make to Target without affecting Client? A. Line 4 of class Target can be changed to return i++; B. Line 2 of class Target can be changed to private int i = 1; C. Line 3 of class Target can be changed to private int addOne(){ D. Line 2 of class Target can be changed to private Integer i = 0; Answer: D
Page: 4
1
2
3
4
5
6
|