难度级别: 简单
程序
程序一
1) 以下程序的输出是什么?
public class A extends B
{
public static String sing()
{
return "fa";
}
public static void main(String[] args)
{
A a = new A();
B b = new A();
System.out.println(a.sing() + " " + b.sing());
}
}
class B
{
public static String sing()
{
return "la";
}
}
复制代码
程序二
2) 以下程序的输出是什么?
class Building
{
Building()
{
System.out.println("Geek-Building");
}
Building(String name)
{
this();
System.out.println("Haiyong-building: String Constructor" + name);
}
}
public class House extends Building
{
House()
{
System.out.println("Haiyong-House ");
}
House(String name)
{
this();
System.out.println("Haiyong-house: String Constructor" + name);
}
public static void main(String[] args)
{
new House("Haiyong");
}
}
复制代码
程序三
3) 以下程序的输出是什么?
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();
}
}
程序四
4) 以下程序的输出是什么?
public class EH
{
public static void main(String args[])
{
int divisor =0;
int dividend = 11;
try
{
int result=dividend/divisor;
System.out.println("结果是"+result);
}
catch(Exception e)
{
System.out.println("发生异常");
}
catch(ArithmeticException ae)
{
System.out.println("被零除");
}
finally
{
System.out.println("我们做完了!");
}
}
}
复制代码
程序五
5) 以下程序的输出是什么?
abstract class Vibrate
{
static String s = "-";
Vibrate()
{
s += "v";
}
}
public class Echo extends Vibrate
{
Echo()
{
this(7);
s += "e";
}
Echo(int x)
{
s += "e2";
}
public static void main(String[] args)
{
System.out.print("made " + s + " ");
}
static
{
Echo e = new Echo();
System.out.print("block " + s + " ");
}
}
复制代码
文章后半部分是程序的输出及解析

输出及解析
程序一输出
输出:
fa la
复制代码
说明:
B b = new A(); b 是 B 类型的对象,因此 b.sing() 指的是 B 类的方法 sing
程序二输出
输出:
Haiyong-Building
Haiyong-House
Haiyong-house: String ConstructorGeek
复制代码
说明:
构造函数调用它们的超类默认构造函数,它首先执行,并且构造函数可以被重载。由于 this(),第一个带有一个参数的 House 构造函数被调用,并且流程转移到 house 类的无参数构造函数。从这里开始,由于超类默认构造函数,构建的无参数构造函数被调用。因此显示的顺序。
程序三输出
输出:
编译器错误
复制代码
说明:
最终方法不能被覆盖。但是,如果我们删除关键字 final,则输出将是
Derived::show() called
复制代码
程序四答案
输出
编译器错误
复制代码
说明:
显示异常 ArithmeticException 已被捕获。catch 块的终端 排序很重要 更具体/子类 (ArithmeticException) 需要更早出现,更通用/超类 (Exception) 需要稍后编写。如果算术异常和一般异常的顺序互换,程序将正确执行。
程序五答案
输出 :
block -ve2e made -ve2e
复制代码
说明:
静态初始化块是创建 Echo 实例的唯一地方。然后创建 Echo 实例,Echos no-arg 构造函数调用其 1-arg 构造函数,然后调用 Vibrates 构造函数(然后秘密调用 Objects 构造函数)。那时,各种构造函数都会执行,从 Objects 构造函数开始,然后返回到 Echos 无参数构造函数。
以上就是本篇文章的所有内容了
















