当前位置:首页 > Java习题三1剖析
1.有关类Demo,哪句描述是正确的?
public class Demo extends Base {
private int count; public Demo()
{
System.out.println(\ }
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 +\ }
public static void main(String args[])
{
ExSuper a = new ExSuper(\ ExSuper b = new Example(\ System.out.println(\ System.out.println(\ } }
① 编译时会出现例外。 ② 运行结果为:
a is First b is second ③ 运行结果为:
a is First
b is Secong a.k.a 2nd ④ 运行结果为:
a is First a.k.a 1nd
nd
b is Second a.k.a 2 5.运行下列程序的结果是哪个? abstract class MineBase {
abstract void amethod(); static int i; }
public class Mine extends MineBase {
}
① 打印5个0。
② 编译出错,数组ar[]必须初始化。 ③ 编译出错, Mine应声明为abstract。 ④ 出现IndexOutOfBoundes的例外。 6.下面哪个语句是正确的?
① Object o = new Button(\② Button b=new Object(\③ 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() { }
public static void main(String argv[]) {
int[] ar = new int[5];
for(i = 0;i < ar.length;i++) System.out.println(ar[i]); }
}
② class B {
int method1(int i) { } int method2(int j) { } }
③ class B implements A {
int method1(int i) { } int method2(int j) { } }
④ class B extends A {
int method1(int i) { } int method2(int j) { } }
⑤ class B implements A {
int method2(int j) { } int method1(int i) { }
}
9.在// point x处的哪些声明是句法上合法的 class Person
{
private int a;
public int change(int m) {
return m; } }
public class Teacher extends Person {
public int b;
public static void main(String arg[]) {
Person p = new Person(); Teacher t = new Teacher(); int i; // point x } }
① i = m; ② i = b; ③ i = p.a;
共分享92篇相关文档