Java习题三1剖析
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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
{
public void method()
{
System.out.println(a + b);
}
}
①println()参数应为字符串,因此此处不能调用该方法。
②因为变量a 是private ,所以在其他类中不能访问a。
③Second 不能继承First。
④关键字final不能修饰类。
8.接口A的定义如下,指出下列哪些类实现了该接口?interface A
{
int method1(int i);
int method2(int j);
}
①class B implements A
{
int method1() { }
int method2() { }