难度级别: 简单

程序

程序一

1) 以下程序的输出是什么?

class Base {
    public void show() {
       System.out.println("Base::show() called");
    }
}
  
class Derived extends Base {
    public void show() {
       System.out.println("Derived::show() called");
    }
}
  
public class Main {
    public static void main(String[] args) {
        Base b = new Derived();;
        b.show();
    }
}
复制代码

a) Derived::show() called
b) Base::show() called

 


程序二

2) 以下程序的输出是什么?

class Base {
    final public void show() {
       System.out.println("Base::show() called");
    }
}
  
class Derived extends Base {
    public void show() {
       System.out.println("Derived::show() called");
    }
}
  
class Main {
    public static void main(String[] args) {
        Base b = new Derived();;
        b.show();
    }
}
复制代码
   
A Base::show() called
B Derived::show() called
C Compiler Error
D Runtime Error
         

程序三

3) 以下程序的输出是什么?

class Base {
    public static void show() {
       System.out.println("Base::show() called");
    }
}
  
class Derived extends Base {
    public static void show() {
       System.out.println("Derived::show() called");
    }
}
  
class Main {
    public static void main(String[] args) {
        Base b = new Derived();;
        b.show();
    }
}
复制代码
   
A Base::show() called
B Derived::show() called
C Compiler Error
   

问题四

4) 关于 Java 中的继承,下列哪项是正确的?

1) 私有方法是最终的。
2) 受保护的成员可以在包内访问,在包
   外继承类。
3) 受保护的方法是最终的。
4)我们不能覆盖私有方法。
复制代码
   
A 1、2 和 4
B 只有 1 和 2
C 1、2 和 3
D 2、3 和 4

 


程序五

5) 以下程序的输出是什么?

class Base {
    public void Print() {
        System.out.println("Base");
    }         
}
 
class Derived extends Base {    
    public void Print() {
        System.out.println("Derived");
    }
}
 
class Main{
    public static void DoPrint( Base o ) {
        o.Print();   
    }
    public static void main(String[] args) {
        Base x = new Base();
        Base y = new Derived();
        Derived z = new Derived();
        DoPrint(x);
        DoPrint(y);
        DoPrint(z);
    }
}
复制代码
   
A Base Derived Derived
B Base Base Derived
C Base Derived Base
D Compiler Error

 


问题六

6) 预测以下程序的输出。请注意, fun() 在 base 中是 public ,而在衍生中是 private 。

class Base {
    public void foo() { System.out.println("Base"); }
}
  
class Derived extends Base {
    private void foo() { System.out.println("Derived"); } 
}
  
public class Main {
    public static void main(String args[]) {
        Base b = new Derived();
        b.foo();
    }
} 
复制代码
   
A Base
B Derived
C Compiler Error
D Runtime Error

 


问题七

下列关于 Java 继承的说法正确的是 
1) 在 Java 中,所有类都直接或间接地从 Object 类继承。Object 类是所有类的根。
2) Java 中不允许多重继承。
3) 与 C++ 不同的是,Java 中没有像继承类型那样可以指定继承是受保护的、公共的还是私有的。
复制代码
   
A 1、2和3
B 1 和 2
C 2 和 3
D 1 和 3

 


程序八

8) 以下程序的输出是什么?

class Grandparent {
    public void Print() {
        System.out.println("Grandparent's Print()");
    }
}
  
class Parent extends Grandparent {
    public void Print() {
        System.out.println("Parent's Print()");
    }
}
  
class Child extends Parent {
    public void Print() {
        super.super.Print(); 
        System.out.println("Child's Print()");
    }
}
  
public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.Print();
    }
}
复制代码
   
A Compiler Error in super.super.Print()
B Grandparent's Print() Parent's Print() Child's Print()
C Runtime Error

 


程序九

9) 以下程序的输出是什么?

final class Complex {
 
    private final double re;
    private final double im;
 
    public Complex(double re, double im) {
         = re;
        this.im = im;
    }
 
    public String toString() {
        return "(" + re + " + " + im + "i)";
    }
}
 
class Main {
  public static void main(String args[])
  {
       Complex c = new Complex(10, 15);
       System.out.println("Complex number is " + c);
  }         
}
复制代码
   
A Complex number is (10.0 + 15.0i)
B Compiler Error
C Complex number is SOME_GARBAGE
D Complex number is Complex@8e2fb5

 


文章后半部分是程序的输出及解析

【Java练习题】Java 程序的输出 | 第二十四套(继承)_c++


输出及解析

程序一输出

输出

a
复制代码

说明

在上面的程序中,b 是一个 Base 类型的引用,指的是一个 Derived 类的对象。在 Java 中,函数默认是虚拟的。所以运行时多态发生并调用派生的 fun()。


程序二输出

输出

C
复制代码

说明

最终方法不能被覆盖。


程序三输出

答案

A
复制代码

说明

与 C++ 一样,当函数是静态的时,不会发生运行时多态性。


程序四答案

答案

A
复制代码

程序五答案

答案

A
复制代码

说明:

参见【Java练习题】Java 程序的输出 | 第二套(含解析)的问题 1


程序六答案

答案

C
复制代码

说明:

对覆盖基类函数的派生类函数进行更严格的访问是编译器错误。


问题七答案

答案

C
复制代码

说明:

参见 C++ 和 Java 中继承的比较


程序八答案

答案

A
复制代码

说明:

在 Java 中,不允许执行 super.super。我们只能使用 Parent 访问祖父母的成员。例如,以下程序运行良好。

// 猜测输出
// 文件名 Main.java 
class Grandparent { 
    public void Print() { 
        System.out.println("Grandparent's Print()"); 
    } 
} 
 
class Parent extends Grandparent { 
    public void Print() { 
    	super.Print();  
        System.out.println("父母的打印()"); 
    } 
} 
 
class Child extends Parent { 
    public void Print() { 
        super.Print();  
        System.out.println("孩子的打印()"); 
    } 
} 
 
class Main { 
    public static void main(String[] args) { 
        Child c = new Child(); 
        c.打印();
    }
}
复制代码

问题九答案

答案

A
复制代码

以上就是本篇文章的所有内容了