注意:按Java规范书写程序代码,如果你认为程序有错误,请指出,并说明程序错误原因。
1.编译会成功吗?
class Demo
{
public static void func()
{
try
{
throw new Exception();
}
finally
{
System.out.println("B");
}
}
public static void main(String[] args)
{
try
{
func();
System.out.println("A");
}
catch(Exception e)
{
System.out.println("C");
}
System.out.println("D");
}
}
编译失败。因为在函数func内抛出了异常,又没有对其进行捕获或者异常声明以便抛出。
修改方法:在func头部后面进行异常声明。
public static void func()throws Exception
编译成功后输出的值为:
注意:A的值是不会打印的,因为下面的语句不会执行到。
2.写出程序结果
class Text
{
Text()
{
System.out.println("Test");
}
}
class Demo extends Text
{
Demo()
{
System.out.println("Demo Test");
}
public static void main(String[] args)
{
new Demo();
new Text();
}
}
考查的是子类的实例化过程。
注意:子类构造函数的第一行有一条隐式语句super(),它会访问父类中空参数的构造函数。
3.写出程序结果
interface A{}
class B implements A
{
public String func()
{
return "func";
}
}
class Demo
{
public static void main(String[] args)
{
A a=new B();
System.out.println(a.func());
}
}
结果:编译失败。 因为A接口中并未定义func方法,无法调用。
注意:编译时看父类(如果没有给定的方法则编译失败),运行时看子类(调用子类的方法)。
4.补充下面的代码
interface Inner
{
void show(int a,int b);
void func();
}
class Demo
{
public static void main(String[] args) {
//补足代码;调用两个函数,要求用匿名内部类
}
}
补充:
interface Inner
{
void show(int a,int b);
void func();
}
class Demo
{
public static void main(String[] args) {
//建立内部类
Inner ir=new Inner()
{
public void show(int a,int b)
{
System.out.println("a="+a+","+"b="+b);
}
public void func()
{
System.out.println("Hello");
}
};
ir.show(4,5);
ir.func();
}
}
5.写出程序结果
class TD
{
int y=6;
class Inner
{
static int y=3;
void show()
{
System.out.println(y);
}
}
}
class TC
{
public static void main(String[] args)
{
TD.Inner ti=new TD().new Inner();
ti.show();
}
}
结果:
编译失败,非静态内部类中不可以定义静态成员。
内部类中如果定义了静态成员,该内部类必须被静态修饰。
6.
选择题,写出错误答案错误的原因,用单行注释的方式。
class Demo
{
int show(int a,int b){return 0;}
}
下面那些函数可以存在于Demo的子类中。
A.public int show(int a,int b){return 0;}//可以,覆盖。
B.private int show(int a,int b){return 0;}//不可以,权限不够。
C.private int show(int a,long b){return 0;}//可以,和父类不是一个函数。没有覆盖,相当于重载。
D.public short show(int a,int b){return 0;}//不可以,因为该函数不可以和给定函数出现在同一类中,或者子父类中。
E.static int show(int a,int b){return 0;}//不可以,静态只能覆盖静态。
7. 写出程序结果
class Fu
{
int num=4;
void show()
{
System.out.println("showFu");
}
}
class Zi extends Fu
{
int num=5;
void show()
{
System.out.println("showZi");
}
}
class T
{
public static void main(String[] args)
{
Fu f=new Zi();
Zi z=new Zi();
System.out.println(f.num);
System.out.println(z.num);
f.show();
z.show();
}
}
结果:
注意:变量不存在覆盖,随着类走 ;子类只会覆盖掉父类的方法。
8.写出程序的结果
class Demo
{
public static void main(String[] args)
{
try
{
showExce();
System.out.println("A");
}
catch(Exception e)
{
System.out.println("B");
}
finally
{
System.out.println("C");
}
System.out.println("D");
}
public static void showExce()throws Exception
{
throw new Exception();
}
}
结果:B,C, D
9.
class Super
{
int i=0;
public Super(String s)
{
i=1;
}
}
class Demo extends Super
{
public Demo(String s)
{
i=2;
}
public static void main(String[] args)
{
Demo d=new Demo("yes");
System.out.println(d.i);
}
}
结果:
编译失败,因为父类中缺少空参数的构造函数。
或者子类应该通过super语句指定要调用的父类中的构造函数。
此处可以在子类构造函数的第一行加上super(s);即可,输出2.
10.
class Super
{
public int get(){return 4;}
}
class Demo15 extends Super
{
public long get(){return 5;}
public static void main(String[] args)
{
Super s=new Demo15();
System.out.println(s.get());
}
}
编译失败,因为子类父类中的get方法没有覆盖。但是子类调用时候不能明确返回的值是什么类型。
所以这样的函数不可以存在子父类中。
11.
class Demo
{
public static void func()
{
try
{
throw new Exception();
System.out.println("A");
}
catch(Exception e)
{
System.out.println("B");
}
}
public static void main(String[] args)
{
try
{
func();
}
catch(Exception e)
{
System.out.println("C");
}
System.out.println("D");
}
}
结果:
编译失败。 因为打印“A”的输出语句执行不到。
记住:throw单独存在,下面不要定义语句,因为执行不到。
12
class Exc0 extends Exception{}
class Exc1 extends Exc0{}
class Demo
{
public static void main(String[] args)
{
try
{
throw new Exc1();
}
catch(Exception e)
{
System.out.println("Exception");
}
catch(Exc0 e)
{
System.out.println("Exc0");
}
}
}
编译失败。
当有多个catch时,父类的catch要放在最下面。
13.补足代码
interface Test
{
void func();
}
class Demo
{
public static void main(String[] args)
{
//补足代码;(匿名内部类)
}
void show(Test t)
{
t.func();
}
}
补充代码:
interface Test
{
void func();
}
class Demo
{
public static void main(String[] args)
{
//补足代码;(匿名内部类)
new Demo().show(new Test()
{
public void func(){}
});
}
void show(Test t)
{
t.func();
}
}
14.补足compare函数内的代码,不许添加其他函数。
class Circle
{
private double radius;
public Circle(double r)
{
radius=r;
}
public Circle compare(Circle cir)
{
//补充代码
}
}
class TC
{
public static void main(String[] args)
{
Circle cir1=new Circle(1.0);
Circle cir2=new Circle(2.0);
Circle cir;
cir=cir1.compare(cir2);
if(cir1==cir)
System.out.println("圆1的半径比较大");
else
System.out.println("圆2的半径比较大");
}
}
补充的内容为:
//补充程序代码
/*方法一
if(this.radius>cir.radius)
return this;
return cir;
*/
//方法二
return (this.radius>cir.radius)?this: cir;
15.在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。
class SearchArrayIndex
{
//由题意知该方法中有两个形参,一个为传入的字符数组,另一个是寻找的字符
public int getIndex(char[] arr,char key)
{
//如果传入的字符数组为null,则抛出异常
if(arr==null)
throw new IllegalArgumentException("数组不能为空");
//遍历数组
//如果数组某个元素和这个比较的字符相等返回这这个元素索引号
for(int x=0;x<arr.length;x++)
{
if(arr[x]==key);
return x;
}
//找不到给定的字符,返回-1
return -1;
}
}
public class HelloWorld
{
public static void main(String[] args) {
char[] arr={'a','b','e','g','f'};
SearchArrayIndex s=new SearchArrayIndex();
int index=s.getIndex(arr,'e');
if(index==-1)
System.out.println("查找的字符不存在");
else
System.out.println("要寻找的字符第一次出现的位置为:"+index);
}
}
16.
public class Demo
{
private static int j = 0;
private static boolean methodB(int k)
{
j += k;
return true;
}
public static void methodA(int i)
{
boolean b;
b = i < 10 | methodB (4);
b = i < 10 || methodB (8);
}
public static void main (String args[] )
{
methodA (0);
System.out.println(j); //4
}
}
结果:4
17
class Circle
{
private static double pi=3.14;
private double radius;
public Circle(double r)
{
radius=r;
}
public static double compare(Circle[] cir)
{
//补充代码
}
}
class TC
{
public static void main(String[] args)
{
Circle cir[]=new Circle[3];//创建了一个类类型数组。
cir[0]=new Circle(1.0);
cir[1]=new Circle(2.0);
cir[2]=new Circle(4.0);
System.out.println("最大的半径值是:"+Circle.compare(cir));
}
}
补充的代码是:
//程序代码,其实就是在求数组中的最大值。
int max = 0;//double max = cir[0].radius;
for(int x=1; x<cir.length; x++)
{
if(cir[x].radius>cir[max].radius)
max = x;
}
return cir[max].radius;
18.
class Test
{
public static String output="";
public static void foo(int i)
{
try
{
if(i==1)
throw new Exception();
output+="1";
}
catch(Exception e)
{
output+="2";
return;
}
finally
{
output+="3";
}
output+="4";
}
public static void main(String args[])
{
foo(0);
System.out.println(output);
foo(1);
System.out.println(output);
}
}
结果:
注意:第二次调用foo(1)时出现异常,最后的语句output+="4";不会被执行到。