JAVA基础面试题-1.doc
文本预览下载声明
JAVA语言基础笔试题-1
Question 1 (43)
Given:
1. class TestA {
2. public void start() { System.out.println(”TestA”); }
3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println(”TestB”); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start(); // TestA x=new TestB(); x.start();
8. }
9. }
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
答案:B
考点:继承环境下,父类引用变量指向子类的问题。
说明:继承环境下,父类引用变量指向子类对象时调用的方法应从子类先找,如果子类没有找到再从父类中查找。
Question 2
Given:
1. interface TestA { String toString(); }
2. public class Test {
3. public static void main(String[] args) {
4. System.out.println(new TestA() {
5. public String toString() { return “test”; }
6. });
7. }
8. }
What is the result?
A. test
B. null
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 1.
E. Compilation fails because of an error in line 4.
F. Compilation fails because of an error in line 5.
答案:A
考点:接口的考察。
说明:本体在输出语句中直接创建TestA的对象,并在语句中完成并且调用toString()方法。
Question 3
Given:
11. public abstract class Shape {
12. int x;
13. int y;
14. public abstract void draw();
15. public void setAnchor(int x, int y) {
16. this.x = x;
17. this.y = y;
18. }
19. }
and a class Circle that extends and fully implements the Shape class.
Which is correct?
A. Shape s = new Shape();
s.setAnchor(10,10);
s.draw();
B. Circle c = new Shape();
c.setAnchor(10,10);
c.draw();
C. Shape s = new Circle();
s.setAnchor(10,10);
s.draw();
D. Shape s = new Circle();
s-setAnchor(10,10);
s-draw();
E. Circle c = new Circle();
c.Shape.setAnchor(10,10);
c.Shape.draw();
答案:B
考点:考察子类继承和抽象类的问题。
说明:在继承和抽象类中,子类对象不能调用父类的方法,当父类引用变量指向之类对行时,指代的是父类的部分,不能调用子类的方法。抽象类中未完成的放法不能被调用,只有完成了方法才能被调用。
Question 4
Given:
10. abstract public class Employee {
11. protected abstract double getSalesAmount();
12. public double getCommision() {
13. return getSalesAmount() * 0.15;
14. }
15. }
16. class Sales extends Employee {
17. /
显示全部