1、Java习题三1剖析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+; 1 当创建一个Demo类的实例对象时,count的值为0。2 当创建一个Demo类的实例对象时,count的值是不确定的。3 超类对象中可以包含改变count 值的方法。4 Demo的子类对象可以访问count。2当编译和运行下列程序段时,会发生什
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; 1 通过编译和并正常运行。 2 编译时出现例外。3 编译通过,运行时出现例外。ClassCaseException3如果任何包中的子类都能访问超类中的成员,那么应使用哪个限定词? 1 public 2 private3 protected4 transient4下面的哪个选项是正确的? cla
3、ss 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
4、(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();1 编译时会出现例外。 2 运行结果为:a is Firstb is second3 运行结果为:a is Firstb is Secong a.k.a 2nd4 运行结果为: a is First a.k.a 1ndb is Second a.k.a 2nd5运行下列程序的结果是哪
5、个?abstract class MineBase abstract void amethod(); static int i; public class Mine extends MineBase public static void main(String argv) int ar = new int5; for(i = 0;i ar.length;i+) System.out.println(ari); 1 打印5个0。 2 编译出错,数组ar必须初始化。3 编译出错, Mine应声明为abstract。4 出现IndexOutOfBoundes的例外。6下面哪个语句是正确的? 1 Ob
6、ject o = new Button(A); 2 Button b=new Object(B);3 Panel p = new Frame();4 Frame f = new Panel();5 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); 1 println()参数应为字符串,因此此处不能调用该方法。 2 因为变量a 是p
7、rivate ,所以在其他类中不能访问a。3 Second 不能继承First。4 关键字final不能修饰类。8接口A的定义如下,指出下列哪些类实现了该接口? interface A int method1(int i); int method2(int j); 1 class B implements A int method1() int method2() 2 class B int method1(int i) int method2(int j) 3 class B implements A int method1(int i) int method2(int j) 4 class
8、 B extends A int method1(int i) int method2(int j) 5 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 Pe
9、rson();Teacher t = new Teacher();int i;/ point x1 i = m;2 i = b;3 i = p.a;4 i = p.change(30);5 i = t.b.10下面的哪些叙述为真1 equals()方法判定引用值是否指向同一对象2 = 操作符判定两个分立的对象的内容和类型是否一致。3 equals()方法只有在两个对象的内容一致时返回true。4 类File重写方法equals()在两个分立的对象的内容和类型一致时返回true。严格来说这个问题的答案是不确定的,因为equals()方法是可以被重载的,但是按照java语言的本意来说:如果没有重写
10、(override)新类的equals(),则该方法和 = 操作符一样在两个变量指向同一对象时返回真,但是java推荐的是使用equals()方法来判断两个对象的内容是否一样,就像String类的equals()方法所做的那样:判定两个String对象的内容是否相同,而=操作符返回true的唯一条件是两个变量指向同一对象。从这个意义上来说选择给定的答案。从更严格的意义来说正确答案应该只有d11下面关于继承的哪些叙述是正确的。1 equals()方法只有在两个对象的内容一致时返回true。2 在java中一个类只能实现一个接口。3 在java中一个类不能同时继承一个类和实现一个接口。4 类Fil
11、e重写方法equals()在两个分立的对象的内容和类型一致时返回true。121) class Person 2) public void printValue(int i, int j) /*/ 3) public void printValue(int i)/*.*/ 4) 5) public class Teacher extends Person 6) public void printValue() /*.*/ 7) public void printValue(int i) /*.*/8) public static void main(String args)9) Person
12、t = new Teacher();10) t.printValue(10);11) 12) 第十行的声明将调用哪些方法。 on line 2 on line 3 on line 6 on line 7上转型对象调用子类中继承的或者重写的方法13public void test() try oneMethod();System.out.println(condition 1); catch (ArrayIndexOutOfBoundsException e) System.out.println(condition 2);catch(Exception e) System.out.printl
13、n(condition 3);finally System.out.println(finally);在oneMethod()方法运行正常的情况下将显示什么? condition 1 condition 2 condition 3 finally14在Java API文档中下面的哪些部分被包括在内 类及用途的描述 父类的方法的列表 成员变量的列表 类层次15public class Parent public int addValue( int a, int b) int s;s = a+b;return s;class Child extends Parent 哪些方法可以加入类Child中
14、 int addValue( int a, int b )/ do something. public void addValue ()/ do something. public int addValue( int a )/ do something. public int addValue( int a, int b )throws MyException /do something.16下面的那个java源文件代码片断是对的。 package testpackage;public class Test/do something. import java.io.*;package test
15、package;public class Test/ do something. import java.io.*;class Person/ do something.public class Test/ do something. import java.io.*;import java.awt.*;public class Test/ do something.17public class Parent int change() class Child extends Parent哪些方法可被加入类Child。 public int change() int change(int i)
16、private int change() abstract int chang()18class Parent String one, two;public Parent(String a, String b)one = a;two = b;public void print() System.out.println(one); public class Child extends Parent public Child(String a, String b)super(a,b);public void print()System.out.println(one + to + two);pub
17、lic static void main(String arg)Parent p = new Parent(south, north);Parent t = new Child(east, west);p.print();t.print(); 在编译时出错。 southeast south to north east to west south to north east south east to west19给出下面的代码: 1) class Parent 2) private String name;3) public Parent()4) 5) public class Child e
18、xtends Parent 6) private String department;7) public Child() 8) public String getValue() return name; 9) public static void main(String arg) 10) Parent p = new Parent();11) 12) 哪些行将导致错误。 line 3 line 6 line 7 line 8/ private access line 1020类Teacher和Student都是类Person的子类p,t和s都是非空值if(t instance of Perso
19、n) s = (Student)t; 这个语句导致的结果是什么 将构造一个Student对象。 表达式合法。 编译时非法。 编译时合法而在运行时可能非法。21给出下面的代码:class Person String name,department;public void printValue()System.out.println(name is +name);System.out.println(department is +department);public class Teacher extends Person int salary;public void printValue()/
20、doing the same as in the parent method printValue()/ including print the value of name and department.System.out.println(salary is +salary);下面的哪些表达式可以加入printValue()方法的doing the same as.部分。 printValue(); this.printValue(); person.printValue(); super.printValue().22给出下面的不完整的方法:1)2) success = connect()
21、;3) if (success=-1) 4) throw new TimedOutException();5) 6)TimedOutException 不是一个RuntimeException。下面的哪些声明可以被加入第一行完成此方法的声明。 public void method() public void method() throws Exception public void method() throws TimedOutException public void method() throw TimedOutException public throw TimedOutExcepti
22、on void method()23定义一个类名为MyClass.java的类,并且该类可被一个工程中的所有类访问,那么该类的正确声明应为: private class MyClass extends Object class MyClass extends Object public class MyClass public class MyClass extends Object 24哪个关键字可以抛出异常? transient finally throw static 25System类在哪个包中? java.util java.io java.awt java.lang26运行下列程序
23、,会产生什么结果: class Outer1 private int a; void foo(double d,final float f) String s; final boolean b; class Inner void methodInner() System.out.println(in the Inner); public static void main(String args) Outer1 me=new Outer1(); me.foo(123,123); System.out.println(outer); outer27下列方法属于java.lang.Math类的有(方
24、法名相同即可): random() abs() sqrt() sin() 28下面程序的运行结果是什么? public class Test extends TT public static void main(String args) Test t = new Test(Tom); public Test(String s) super(s); System.out.println(How do you do?); public Test() this(I am Tom); class TT public TT() System.out.println(What a pleasure!);
25、public TT(String s) this(); System.out.println(I am +s); What a pleasure!I am TomHow do you do?29给定下面的未完成的代码片断: public class Example int x,y; public Example(int a) x = a; public Example(int a, int b) /和上面一个参数的构造方法做同样的操作,包括赋值 x=a y = b; 如果要用最简捷的一行代码实现/和上面一个参数的注释所指出的功能,请写出你认为最合适的一行代码: 30final, finally
26、, finalize的区别?31abstract class和interface有什么区别?32Static Nested Class 和 Inner Class的不同?33接口是否可继承接口? 抽象类是否可实现(implements)接口? 抽象类是否可继承实体类(concrete class)?(抽象类是否可继承实体类,但前提是实体类必须有明确的构造函数)34数组有没有length()这个方法? String有没有length()这个方法?35Constructor是否可被override?36是否可以继承String类?不能public final class String37try 里有一个return语句,那么紧跟在这个try后的finally 里的code会不会被执行,什么时候被执行,在return前还是后?38abstract class Name private String name; public abstract boolean isStupidName(String name) /抽象方法只声明不实现,所以没有而加; 是否有错,若有,说明错误原因39public class Something void doSometh
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1