当前位置:首页 > SCJP题库 Module3 - 图文
22. int c = super.multiply(a, b); 父类中的multiply方法为static,不能用super调用,静态的可以直接用类名调用 23. return c; 24. } 25. } and:
30. SubCalc sc = new SubCalc();
31. System.out.println(sc.multiply(3,4));
32. System.out.println(SubCalc.multiply(2,2)); What is the result? A. 12 4
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 21. E. Compilation fails because of an error in line 22. F. Compilation fails because of an error in line 31.
Answer: E 比较重点,根本不知道这个知识,下次看到在好好记一遍!!
Question 11 Given:
1. public class Team extends java.util.LinkedList { 2. public void addPlayer(Player p) { 3. add(p); 4. }
5. public void compete(Team opponent) { /* more code here */ } 6. }
7. class Player { /* more code here */ } Which two are true? (Choose two.) A. This code will compile.
B. This code demonstrates(示范) proper(正当) design(设计) of an is-a relationship(关系关联).
C. This code demonstrates proper design of a has-a relationship. D. A Java programmer using the Team class could remove Player objects from a Team object. Answer: AD
Question 12 Given:
11. class ClassA {}
12. class ClassB extends ClassA {} 13. class ClassC extends ClassA {} and:
21. ClassA p0 = new ClassA(); 22. ClassB p1 = new ClassB(); 23. ClassC p2 = new ClassC(); 24. ClassA p3 = new ClassB(); 25. ClassA p4 = new ClassC();
Which three are valid 有效的? (Choose three.) A. p0 = p1; B. p1 =p2; C. p2 = p4;
D. p2 = (ClassC)p1; E. p1 = (ClassB)p3;
5
F. p2 = (ClassC)p4;
Answer: AEF 父类赋值给子类的时候才用强制转换
Question 13 Given:
11. class Animal { public String noise() { return \12. class Dog extends Animal {
13. public String noise() { return \14. }
15. class Cat extends Animal {
16. public String noise() { return \17. } .....
30. Animal animal = new Dog(); new了个狗 31. Cat cat = (Cat)animal; 但是强制转换了猫 32. System.out.printIn(cat.noise()); What is the result? A. peep B. bark C. meow
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: E 这种情况下,编译通过,但是运行时候抛出异常,一般情况是相信程序员的!
Question 14 Given:
11. class Cup { }
12. class PoisonCup extends Cup { } 21. public void takeCup(Cup c) { 22. if(c instanceof PoisonCup) {
23. System.out.println(\24. } else if(c instanceof Cup) {
25. System.out.println(\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 if.else 语句中 如果多个条件都成立的话,那么只执行第一个被判断成立里面的代码,之后的条件成立也不会调用了!!!
Question 15
1. public class SimpleCalc {
6
2. public int value;
3. public void calculate() { value += 7; } 4. } And:
1. public class MultiCalc extends SimpleCalc { 2. public void calculate() { value -= 3; } 3. public void calculate(int multiplier) { 4. calculate();
5. super.calculate(); 6. value *=multiplier; 7. }
8. public static void main(String[] args) { 9. MultiCalc calculator = new MultiCalc(); 10. calculator.calculate(2);
11. System.out.println(\12. } 13. }
What is the result? A. Value is: 8
B. Compilation fails. C. Value is: 12 D. Value is: -12
E. The code runs with no output.
F. An exception is thrown at runtime. Answer: A 无争议,子类和父类同时共用一个value,经过乱七八糟的计算,结果为8
Question 16 Given:
1. public class Blip {
2. protected int blipvert(int x) { return 0; } 3. }
4. class Vert extends Blip { 5. // insert code here 6. }
Which five methods, inserted independently at line 5, will compile? (Choose five.)
A. public int blipvert(int x) { return 0; } B. private int blipvert(int x) { return 0; } C. private int blipvert(long x) { return 0; } D. protected long blipvert(int x) { return 0; } E. protected int blipvert(long x) { return 0; } F. protected long blipvert(long x) { return 0; }
G. protected long blipvert(int x, int y) { return 0; }
Answer: ACEFG 哦也,这题自己做了,对了!考点:方法的覆盖,权限不能小于父类的权限
Question 17 Given:
11. abstract class Vehicle { public int speed() { return 0; } } 12. class Car extends Vehicle { public int speed() { return 60; } } 13. class RaceCar extends Car { public int speed() { return 150; }} ......
21. RaceCar racer = new RaceCar(); 22. Car car = new RaceCar();
7
23. Vehicle vehicle = new RaceCar();
24. System.out.println(racer.speed() + \25. + \What is the result? A. 0, 0,0
B. 150, 60, 0
C. Compilation fails. D. 150, 150, 150
E. An exception is thrown at runtime.
Answer: D 无争议,考点:多态性,调用的都是子类里的方法
Question 18 Given:
10. interface A { public int getValue() } 11. class B implements A {
12. public int getValue() { return 1; } 13. }
14. class C extends B { 15. // insert code here 16. }
Which three code fragments, inserted individually at line 15, make use of polymorphism? (Choose three.)
A. public void add(C c) { c.getValue(); } B. public void add(B b) { b.getValue(); } 有点争议,B类型的如果new B(),就是多态,如果new C()就不是
C. public void add(A a) { a.getValue(); }
D. public void add(A a, B b) { a.getValue(); } E. public void add(C c1, C c2) { c1.getValue(); } Answer: BCD
Question 19
Which three statements are true? (Choose three.) A. A final method in class X can be abstract if and only if X is abstract. 抽象类的方法不能用final修饰
B. A protected method in class X can be overridden by any subclass of X.
C. A private static method can be called only within other static methods in class X.
D. A non-static public final method in class X can be overridden in any subclass of X.
E. A public static method in class X can be called by a subclass of X without explicitly referencing the class X. F. A method with the same signature as a private final method in class X can be implemented in a subclass of X.
Public final method 是不能被覆盖的,但是private final method 就没有问题了,因为子类里面在写method和父类中的方法没有关系了,相当于新定义的,牛逼啊,不仔细研究肯本参悟不透啊,第二遍的时候在好好看看这里!
G. A protected method in class X can be overridden by a subclass of A only if the subclass is in the same package as X. Answer: BEF
Question 20 Given:
8
共分享92篇相关文档