教育资源为主的文档平台

当前位置: 查字典文档网> 所有文档分类> IT计算机> Java> JAVA:面向对象编程测题

JAVA:面向对象编程测题

上传者:李渊
|
上传时间:2016-09-17
|
次下载

JAVA:面向对象编程测题

面向对象编程

(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() ) ;

What is the result?  

A. peep
B. back
C. move
D. 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 here

}

What three code fragments 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 to 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

}

Methods

private void bar ( int x ) {}

public void bar ( int x ) {}

public int bar ( String x ) { return 1; }

public Alpha bar ( int x ) {}

public void bar ( int x, int y ) {}

public int bar ( int x ) { return x; }


(4)

Given the following Java code:

class Test1{

public Test1 foo() { return this; }

}
class Test2 extends Test1 {

public Test1 foo() { return this; }

}
class Test3 extends Test2 {

// insert method here

}

Which two methods, inserted individually, correctly 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 Bootchy{

int bootch;

String snootch;

 

public Bootchy() {

this( "snootchy" );

System.out.print( "first " ) ;

}

 

public 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.bootch );

}

}

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 Test{

static 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 a = new B ();

a.process ();

}

}

What is the result?

 

A. B
B. 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 line 19.


(7)

 

Given the following Java code:

10
11.
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 here

}

}

Which code, inserted at line 16 will cause a java.lang.ClassCastException?

 

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 Person{

private 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 instanceof 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() + comment.hashCode()/2 - age*3;


(9)

 

Given the following code:

01.
02.
03.
04.
05.
06.
07.

public class Person{

private 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 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 method 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 {

public void foo(String a) {

System.out.print("Beta:foo ");

}
public void bar(String a) {

System.out.print("Beta:bar ");

}
public static void main(String[] args) {

Alpha a = new Beta();
Beta b = (Beta)a;
a.foo("test");
b.foo("test");
a.bar("test");
b.bar("test");

}

}

Actul Output Sequence:

Place here

Place here

Place here

Place here


Output Option:

Alpha:foo

Alpha:bar

Beta:foo

Beta:bar

 


(11)

 

Given the following Java code:

1.
2.
3.

public interface A {

public void doSomething(String thing);

}

1.
2.
3.

public class AImp1 implements A {

public void doSomething(String msg) {}

}

1.
2.
3.
4.
5.
6.
7.
8.
9.

public class B {

public A doit() {

// more code here

}

 

public String execute() {

// more code here

}

}

1.
2.
3.
4.
5.
6.
7.
8.
9.

public class C extends B {

public AImp1 doit() {

// more code here

}

 

public Object execute() {

// more code here

}

}

Which statement is true about the class and interfaces in this "Code"?

 


A. Compilation will succeed for all classes and interfaces.

B. Compilation of class C will fail because of an error int line 2.

C. Compilation of class C will fail because of an error int line 6.

D. Compilation of AImpl will fail because of an error in line 2.


(12)

Given the following Java code:

class A {

String name = "A" ;

String getName() {

return name;

}

String greeting() {

return "class A" ;

}

}
class B extends A {

String name = "B" ;

String greeting() {

return "class B" ;

}

}
public class Client {

public static void main(String[] args) {

A a = new A() ;

A b = new B() ;

System.out.println(a.greeting () + " has name " + a.getName () ) ;

System.out.println(b.greeting () + " has name " + b.getName () ) ;

}

}

Place the names "A" and "B" in the following output.
Class Place here has name Place here 
Class Place here has name Place here

Names:

A

B

 

 (13)

 

Given the following Java code:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

class SuperClass {

public A getA() {

return new A() ;

}

}
class SubClass extends SuperClass {

public B getA() {

return new B();

}

}

Which statement is true ?  

 

A. Compilation will succeed if A extends B 

 B. Compilation will succeed if B extends A

C. Compilation will always fail because of an error in line 7

D. Compilation will always fail because of an error in line 8


(14)

 

Given the following Java code:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

class Pizza {

java.util.ArrayList toppings;

public final void addTopping(String topping) {

toppings.add(topping);

}

}
public class PepperoniPizza extends Pizza {

public void addTopping(String topping) {

System.out.println("Cannot and Uoppings");

}

public static void main(String[] args) {

Pizza pizza = new PepperoniPizza();

Pizza.addTopping("Mushrooms");

}

}

What is the result ?  

 

A. Compilation fails
B. Cannot and Uoppings
C. The code runs with no output
D. A NullPointerException is thrown in Line 4


(15)

 

Given the following Java code:

10.
11.
12.
13.
14.
15.
16.
17.
18.
19.

interface A {

void x ();

}
class B implements A {

public void x () {}

public void y () {}

}
class C extends B {

public void x () {}

}

And :

20.
21.
22.
23.
24.
25.
26.

java.util.List <A> list = new java.util.ArrayList<A> ();

list.add(new B ());

list.add(new C ());

for (A a : list ) {

a.x();

a.y();

}


What is the result ? 

 

A. The code runs with no output  
B. An exception thrown at runtime 

C. Compilation fails because of an error in line 20

D. Compilation fails because of an error in line 21

E. Compilation fails because of an error in line 23

F. Compilation fails because of an error in line 25


(16)

 

Given the following Java code:

10.
11.
12.
13.
14.
15.

class One {

void foo() {}

}
class Two extends One {

// insert method here

}

Which three methods, inserted individually at line 14 will correctly class Two ?(Choose three)  

 

A. int foo(){/*more code here */}
B. void foo(){/*more code here */}
C. public void foo(){/*more code here*/}
D. private void foo(){/*more code here*/}
E. protected void foo(){/*more code here*/}


(17)

Which two statements are true about the hashCode method? (Choose two)

A. The hashCode method for a given class can be used to test for object equality and object inequality for that class. (hashCode method
B. The hashCode method is used by the java.util.SortedSet collection class to order the ements within that set.
C. The hashCode method for a given class can be used to test for object inequality, but NOT object equality for that class.
D. The only important characteristic of the values returned by a hashCode method is that distribution of values must follow a Gaussian distribution.
E. The hashCode method is used by the java.util.HashSet collection class to group the elements within that set into hash buckets for swift retrieval.


(18)

Given the following Java code:

class SomeException:
public class SomeException {
}

class A:
public class A {

public void doSomething() {}

}

class B:
public vlass B extends A {

public void soSomething() throws SomeException {}

}

Which statement is true about the two classes?


A. Compilation of both classes will fail. 

B. Compilation of both classes will succeed.
C. Compilation of class A will fail, Compilation of class B will succeed.

D. Compilation of class B will fail, Compilation of class A will succeed. 

版权声明:此文档由查字典文档网用户提供,如用于商业用途请与作者联系,查字典文档网保持最终解释权!

下载文档

热门试卷

2016年四川省内江市中考化学试卷
广西钦州市高新区2017届高三11月月考政治试卷
浙江省湖州市2016-2017学年高一上学期期中考试政治试卷
浙江省湖州市2016-2017学年高二上学期期中考试政治试卷
辽宁省铁岭市协作体2017届高三上学期第三次联考政治试卷
广西钦州市钦州港区2016-2017学年高二11月月考政治试卷
广西钦州市钦州港区2017届高三11月月考政治试卷
广西钦州市钦州港区2016-2017学年高一11月月考政治试卷
广西钦州市高新区2016-2017学年高二11月月考政治试卷
广西钦州市高新区2016-2017学年高一11月月考政治试卷
山东省滨州市三校2017届第一学期阶段测试初三英语试题
四川省成都七中2017届高三一诊模拟考试文科综合试卷
2017届普通高等学校招生全国统一考试模拟试题(附答案)
重庆市永川中学高2017级上期12月月考语文试题
江西宜春三中2017届高三第一学期第二次月考文科综合试题
内蒙古赤峰二中2017届高三上学期第三次月考英语试题
2017年六年级(上)数学期末考试卷
2017人教版小学英语三年级上期末笔试题
江苏省常州西藏民族中学2016-2017学年九年级思想品德第一学期第二次阶段测试试卷
重庆市九龙坡区七校2016-2017学年上期八年级素质测查(二)语文学科试题卷
江苏省无锡市钱桥中学2016年12月八年级语文阶段性测试卷
江苏省无锡市钱桥中学2016-2017学年七年级英语12月阶段检测试卷
山东省邹城市第八中学2016-2017学年八年级12月物理第4章试题(无答案)
【人教版】河北省2015-2016学年度九年级上期末语文试题卷(附答案)
四川省简阳市阳安中学2016年12月高二月考英语试卷
四川省成都龙泉中学高三上学期2016年12月月考试题文科综合能力测试
安徽省滁州中学2016—2017学年度第一学期12月月考​高三英语试卷
山东省武城县第二中学2016.12高一年级上学期第二次月考历史试题(必修一第四、五单元)
福建省四地六校联考2016-2017学年上学期第三次月考高三化学试卷
甘肃省武威第二十三中学2016—2017学年度八年级第一学期12月月考生物试卷

网友关注视频

北师大版数学四年级下册第三单元第四节街心广场
苏教版二年级下册数学《认识东、南、西、北》
19 爱护鸟类_第一课时(二等奖)(桂美版二年级下册)_T502436
化学九年级下册全册同步 人教版 第25集 生活中常见的盐(二)
人教版二年级下册数学
沪教版牛津小学英语(深圳用) 四年级下册 Unit 12
【部编】人教版语文七年级下册《逢入京使》优质课教学视频+PPT课件+教案,辽宁省
北师大版小学数学四年级下册第15课小数乘小数一
沪教版牛津小学英语(深圳用)五年级下册 Unit 1
二年级下册数学第一课
冀教版小学数学二年级下册第二周第2课时《我们的测量》宝丰街小学庞志荣
【部编】人教版语文七年级下册《老山界》优质课教学视频+PPT课件+教案,安徽省
沪教版牛津小学英语(深圳用) 四年级下册 Unit 8
沪教版八年级下册数学练习册一次函数复习题B组(P11)
沪教版牛津小学英语(深圳用) 五年级下册 Unit 7
三年级英语单词记忆下册(沪教版)第一二单元复习
沪教版八年级下册数学练习册21.4(1)无理方程P18
青岛版教材五年级下册第四单元(走进军营——方向与位置)用数对确定位置(一等奖)
外研版英语七年级下册module3 unit1第二课时
【部编】人教版语文七年级下册《泊秦淮》优质课教学视频+PPT课件+教案,湖北省
《空中课堂》二年级下册 数学第一单元第1课时
冀教版小学英语四年级下册Lesson2授课视频
苏科版八年级数学下册7.2《统计图的选用》
第12章 圆锥曲线_12.7 抛物线的标准方程_第一课时(特等奖)(沪教版高二下册)_T274713
二次函数求实际问题中的最值_第一课时(特等奖)(冀教版九年级下册)_T144339
第五单元 民族艺术的瑰宝_15. 多姿多彩的民族服饰_第二课时(市一等奖)(岭南版六年级上册)_T129830
沪教版八年级下次数学练习册21.4(2)无理方程P19
飞翔英语—冀教版(三起)英语三年级下册Lesson 2 Cats and Dogs
人教版历史八年级下册第一课《中华人民共和国成立》
冀教版英语五年级下册第二课课程解读