1. public class SimpleCalc {
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(”Value is: “+ calculator.value);
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.
您可能感兴趣的试卷
你可能感兴趣的试题
1. class Super {
2. private int a;
3. protected Super(int a) { this.a = a; }
4. }
.....
11. class Sub extends Super {
12. public Sub(int a) { super(a); }
13. public Sub() { this.a= 5; }
14. }
Which two, independently, will allow Sub to compile?()
A. Change line 2 to: public int a;
B. Change line 2 to: protected int a;
C. Change line 13 to: public Sub() { this(5); }
D. Change line 13 to: public Sub() { super(5); }
E. Change line 13 to: public Sub() { super(a); }
1. class Pizza {
2. java.util.ArrayList toppings;
3. public final void addTopping(String topping) {
4. toppings.add(topping);
5. }
6. }
7. public class PepperoniPizza extends Pizza {
8. public void addTopping(String topping) {
9. System.out.println(”Cannot add Toppings”);
10. }
11. public static void main(String[] args) {
12. Pizza pizza = new PepperoniPizza();
13. pizza.addTopping(”Mushrooms”);
14. }
15. }
What is the result?()
A. Compilation fails.
B. Cannot add Toppings
C. The code runs with no output.
D. A NullPointerException is thrown in Line 4.
public class Base {
public static final String FOO = “foo”;
public static void main(String[] args) {
Base b = new Base();
Sub s = new Sub();
System.out.print(Base.FOO);
System.out.print(Sub.FOO);
System.out.print(b.FOO);
System.out.print(s.FOO);
System.out.print(((Base)s).FOO);
} }
class Sub extends Base {public static final String FOO=bar;}
What is the result?()
A. foofoofoofoofoo
B. foobarfoobarbar
C. foobarfoofoofoo
D. foobarfoobarfoo
E. barbarbarbarbar
F. foofoofoobarbar
G. foofoofoobarfoo
class ClassA {
public int numberOfinstances;
protected ClassA(int numberOfinstances) {
this.numberOflnstances = numberOfinstances;
}
}
public class ExtendedA extends ClassA {
private ExtendedA(int numberOfinstances) {
super(numberOflnstances);
}
public static void main(String[] args) {
ExtendedA ext = new ExtendedA(420);
System.out.print(ext.numberOflnstances);
}
}
Which is true?()
A. 420 is the output.
B. An exception is thrown at runtime.
C. All constructors must be declared public.
D. Constructors CANNOT use the private modifier.
E. Constructors CANNOT use the protected modifier.
interface A { public int getValue() }
class B implements A {
public int getValue() { return 1; }
}
class C extends B {
// insert code here
}
Which three code fragments, inserted individually at line 15, make use of polymorphism?()
A. public void add(C c) { c.getValue(); }
B. public void add(B b) { b.getValue(); }
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(); }
1. interface A { public void aMethod(); }
2. interface B { public void bMethod(); }
3. interface C extends A,B { public void cMethod(); }
4. class D implements B {
5. public void bMethod() { }
6. }
7. class E extends D implements C {
8. public void aMethod() { }
9. public void bMethod() { }
10. public void cMethod() { }
11. }
What is the result?()
A. Compilation fails because of an error in line 3.
B. Compilation fails because of an error in line 7.
C. Compilation fails because of an error in line 9.
D. If you define D e = new E(), then e.bMethod() invokes the version of bMethod()defined in Line 5.
E. If you define D e = (D)(new E()),then e.bMethod() invokes the version of bMethod() defined in Line 5.
F. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.
1. class SuperClass {
2. public a geta() {
3. return new a();
4. }
5. }
6. class SubClass extends SuperClass {
7. public b geta() {
8. return new b();
9. }
10. }
Which is true?()
A. Compilation will succeed if a extends b.
B. Compilation will succeed if b extends a.
C. Compilation will always fail because of an error in line 7.
D. Compilation will always fail because of an error in line 8.
10. interface A { void x(); }
11. class B implements A { public void x() { } public voidy() { } }
12. class C extends B { public void x() {} }
And:
20. java.util.List list = new java.util.ArrayList();
21. list.add(new B());
22. list.add(new C());
23. for (A a:list) {
24. a.x();
25. a.y();;
26. }
What is the result?()
A. The code runs with no output.
B. An exception is thrown at runtime.
C. Compilation fails because of an error in line 20.
D. Compilation fails because of an error in line 21.
E. Compilation fails because of an error in line 23.
F. Compilation fails because of an error in line 25.
abstract class A {
abstract void al();
void a2() { }
}
class B extends A {
void a1() { }
void a2() { }
}
class C extends B { void c1() { } }
and:
A x = new B(); C y = new C(); A z = new C();
Which four are valid examples of polymorphic method calls?()
A. x.a2();
B. z.a2();
C. z.c1();
D. z.a1();
E. y.c1();
F. x.a1();
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();
23. Vehicle vehicle = new RaceCar();
24. System.out.println(racer.speed() + “, „ + car.speed()
25. + “, “+ vehicle.speed());
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.
最新试题
Which statement is true?()
What is the appropriate definition of the hashCode method in class Person?()
What is the result?()
Which two can be results?()
What is the output if the main() method is run?()
What is the result?()
What is the result?()
Which statement is true?()
Which two are possible results?()
Which statement is true about the set variable on line 12?()