Page: 14
8. OOPs
Question: 59 Click the Exhibit button. 11. class Payload { 12. private int weight; 13. public Payload(int wt) { weight = wt; } 13. public void setWeight(mt w) { weight = w; } 15. public String toString { return Integer.toString(weight); } 16. } 17. 18. public class TestPayload { 19. static void changePayload(Payload p) { 20. /* insert code here */ 21. } 22. 23. public static void main(String[] args) { 24. Payload p = new Payload(); 25. p.setWeight(1024); 26. changePayload(p); 27. System.out.println(”The value of p is “+ p); 28. } 29. } Which statement, placed at line 20, causes the code to print “The value of p is 420.”? A. p.setWeight(420); B. p.changePayload(420); C. p = new Payload(420); D. Payload.setWeight(420); E. p = Payload.setWeight(420); F. p = new Payload(); p.setWeight(420); Answer: A 60. Which statement(s) are true? (Choose all that apply.) A. Has-a relationships always rely on inheritance. B. Has-a relationships always rely on instance variables. C. Has-a relationships always require at least two class types. D. Has-a relationships always rely on polymorphism. E. Has-a relationships are always tightly coupled. Answer: -> B is correct. -> A and D describe other OO topics. C is incorrect because a class can have an instance of itself. E is incorrect because while has-a relationships can lead to tight coupling, it is by no means always the case. 61. Given: class Clidders { public final void flipper() { System.out.println("Clidder"); } } public class Clidlets extends Clidders { public void flipper() { System.out.println("Flip a Clidlet"); super.flipper(); } public static void main(String [] args) { new Clidlets().flipper(); } } What is the result? A. Flip a Clidlet B. Flip a Clidder C. Flip a Clidder Flip a Clidlet D. Flip a Clidlet Flip a Clidder E. Compilation fails. Answer: -> E is correct. final methods cannot be overridden. ->A, B, C, and D are incorrect based on the above. 62. Given: public abstract interface Frobnicate { public void twiddle(String s); } Which is a correct class? (Choose all that apply.) A. public abstract class Frob implements Frobnicate { public abstract void twiddle(String s) { } } B. public abstract class Frob implements Frobnicate { } C. public class Frob extends Frobnicate { public void twiddle(Integer i) { } } D. public class Frob implements Frobnicate { public void twiddle(Integer i) { } } E. public class Frob implements Frobnicate { public void twiddle(String i) { } public void twiddle(Integer s) { } } Answer: ->B is correct, an abstract class need not implement any or all of an interface’s methods. E is correct, the class implements the interface method and additionally overloads the twiddle() method. -> A is incorrect because abstract methods have no body. C is incorrect because classes implement interfaces they don’t extend them. D is incorrect because overloading a method is not implementing it.
Page: 14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|