Java习题三1.doc
文本预览下载声明
1.有关类Demo,哪句描述是正确的?
public class Demo extends Base
{
private int count;
public Demo()
{
System.out.println(A Demo object has been created);
}
protected void addOne()
{
count++;
}
}
当创建一个Demo类的实例对象时,count的值为0。
当创建一个Demo类的实例对象时,count的值是不确定的。
超类对象中可以包含改变count 值的方法。
Demo的子类对象可以访问count。
2.当编译和运行下列程序段时,会发生什么?
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class Cex
{
public static void main(String argv[])
{
Base b = new Base();
Sub s = (Sub) b;
}
}
通过编译和并正常运行。
编译时出现例外。
编译通过,运行时出现例外。ClassCaseException
3.如果任何包中的子类都能访问超类中的成员,那么应使用哪个限定词?
public
private
protected
transient
4.下面的哪个选项是正确的?
class ExSuper
{
String name;
String nick_name;
public ExSuper(String s,String t)
{
name = s;
nick_name = t;
}
public String toString()
{
return name;
}
}
public class Example extends ExSuper
{
public Example(String s,String t)
{
super(s,t);
}
public String toString()
{
return name +a.k.a+nick_name;
}
public static void main(String args[])
{
ExSuper a = new ExSuper(First,1st);
ExSuper b = new Example(Second,2nd);
System.out.println(a is+a.toString());
System.out.println(b is+b.toString());
}
}
编译时会出现例外。
运行结果为:
a is First
b is second
运行结果为:
a is First
b is Secong a.k.a 2nd
运行结果为:
a is First a.k.a 1nd
b is Second a.k.a 2nd
5.运行下列程序的结果是哪个?
abstract class MineBase
{
abstract void amethod();
static int i;
}
public class Mine extends MineBase
{
public static void main(String argv[])
{
int[] ar = new int[5];
for(i = 0;i ar.length;i++)
System.out.println(ar[i]);
}
}
打印5个0。
编译出错,数组ar[]必须初始化。
编译出错, Mine应声明为abstract。
出现IndexOutOfBoundes的例外。
6.下面哪个语句是正确的?
Object o = new Button(A);
Button b=new Object(B);
Panel p = new Frame();
Frame f = new Panel();
Panel p = new Panel();
7.指出下列程序的所有错误?
final class First
{
private int a = 1;
int b = 2;
}
class Second extends First
{
显示全部