面向对象编程题目.doc
《面向对象编程题目.doc》由会员分享,可在线阅读,更多相关《面向对象编程题目.doc(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、面向对象编程(1)Given the following Java code:class Animal public String noise () return peep class Dog extends Animal public String noise () return back; class Cat extends Animal public String noise () return move; . . . Animal animal = new Dog();Cat cat = ( Cat ) animal;System.out.printIn( cat.noise() )
2、;What is the result? A. peepB. backC. moveD. Compilation fails.E. An exception is thrown at runtime (2)Given the following Java code:10.11.12.13.14.15.16.interface A public int getValue (); class B implements A public int getValue () return 1; class C extends B / insert code hereWhat three code frag
3、ments inserted individually at line 15, make used of polymorphism? (choose three) A. public void add(C c) c.getValue(); B. public void add(B b) b.getValue(); C. public void add(A a) a.getValue(); D. public void add(A a, B b) a.getValue(); E. public void add(C c1 C c2) c1.getValue(); (3)Add methods t
4、o the Beta class to make it compile correctly. class Alpha public void bar ( int x ) public void bar (int x ) public class Beta extends Alpha place here place here place here Methodsprivate void bar ( int x ) public void bar ( int x ) public int bar ( String x ) return 1; public Alpha bar ( int x )
5、public void bar ( int x, int y ) public int bar ( int x ) return x; (4)Given the following Java code:class Test1public Test1 foo() return this; class Test2 extends Test1 public Test1 foo() return this; class Test3 extends Test2 / insert method hereWhich two methods, inserted individually, correctly
6、complete the Test3 class?(choose two) A. public void foo() B. public int foo() return 3; C. public Test2 foo() return this; D. public Test1 foo() return this; (5)Given the following Java code:public class Bootchyint bootch;String snootch;public Bootchy() this( snootchy );System.out.print( first ) ;p
7、ublic Boothchy( String snootch ) this( 420, snootchy );System.out.print( second ) ;public Bootchy(int bootch, String snootch ) this.bootch = bootch;this. snootch = snootch;System.out.print( third );public static void main(String args ) Bootchy b= new Bootchy ();System.out.print(b.snootch + + b.bootc
8、h );What is the result? (运行结果)(6)Given the following Java code: 09.10.11.12.13.14.15.16.17.18.19.20.21.public class Teststatic class A void process () throws Exception throw new Exception () ; static class B extends A void process () System.out.println( B ); public static void main (String args ) A
9、a = new B ();a.process ();What is the result?A. BB. The code exception is thrown at runtime (执行时抛出exception)C. The code run with no output (沒有任何输出)D. Compilation fails because of an error in line 15. E. Compilation fails because of an error in line 18. F. Compilation fails because of an error in lin
10、e 19. (7)Given the following Java code:1011.12.13.14.15.16.17.18.interface Foo class Alpha implements Foo class Beta extends Alpha class Delta extends Beta public static void main(String args) Beta x = new Beta ();/ insert code hereWhich code, inserted at line 16 will cause a java.lang.ClassCastExce
11、ption?A. Alpha a = x;B. Foo f = (Delta)x;C. Foo f = (Alpha)x;D. Beta b = (Beta)(Alpha)x;(8)Given the following Java code:public class Personprivate String name, comment;private int age;public Person(String n, int a, String c) name = n; age = a; comment = c;public Boolean equals(Object o)if (! (o ins
12、tanceof Person) return false;Person p = (Person)o;return age = p.age & name.equals(p.name);What is the appropriated definition of the hashCode method in class Person? A. return super.hashCode();B. return name.hashCode()+age*7;C. return namehashcode() + comment.hashCode()/2;D. return name.hashCode()
13、+ comment.hashCode()/2 - age*3;(9)Given the following code:01.02.03.04.05.06.07.public class Personprivate String name;public Person (String name) this.name = name; public Boolean equals(Person p)return p.name.equals(this.name);Which statement is true? A. The equals method does NOT properly override
14、 the object Object.equals method. B. Compilation fails because the private attribute p.name cannot be accessed in line 5. C. To work correctly with hash-based data structures, this class must also implement the hashCode method. D. When adding Person objects to java.util. Set collection, the equals m
15、ethod in line 4 will prevent duplicates.(10)Place the Output Options in the Actual Output Sequence to indicate the output from this code: class Alpha public void foo(String. args) System.out.print(Alpha:foo );public void bar(String a) System.out.print(Alpha:bar );public class Beta extends Alpha publ
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象 编程 题目
限制150内