当前位置:文档之家› JAVA基础第章继承与多态练习题

JAVA基础第章继承与多态练习题

JAVA基础第章继承与多态练习题
JAVA基础第章继承与多态练习题

第4章继承与多态一.选择题

1. 编译和运行以下两文件结果是( D )。

//文件P1.java

package MyPackage;

class P1{

void afancymethod(){

System.out.println("What a fancy method");

}

}

//文件P2.java

package YourPackage;

import MyPackage.*;

public class P2 extends P1{

public static void main(String argv[]){

P2 p2 = new P2();

p2.afancymethod();

}

}

A.两个均通过编译,P2运行时输出What a fancy method

B.没一个通过编译

C.两个均通过编译,但P2运行时出错

D.P1 通过编译,但P2出现编译错误

2.下列程序运行的结果是(A )。

package a;

package b;

public class D{

public static void main(String args[]) {

System.out.println("^_^,今天心情不错!");

}

}

A.出现编译错误

B.^_^,今天心情不错!

C.通过编译,运行时出错

D.以上都不对

3.Java的核心类库中哪个包,Java系统能自动引入(B )。

A.java.io B.https://www.doczj.com/doc/ba6178622.html,ng

C.https://www.doczj.com/doc/ba6178622.html, D.java.util

4.下列程序运行结果是( A )。

private class Base{

Base(){

int i = 100;

System.out.println(i);

}

public class Pri extends Base{

static int i = 200;

public static void main(String argv[]){

Pri p = new Pri();

System.out.println(i);

}

}

A.编译错误B.200 C.100 200 D.100

5.下列程序运行结果是(C )。

class Base{

Base(){

int i = 100;

System.out.println(i);

}

}

public class Pri extends Base{

static int i = 200;

public static void main(String argv[]){

Pri p = new Pri();

System.out.println(i);

}

}

A.编译错误B.200 C.100 200 D.100 6.如何定义一个不能有子类的类Key( B )。

A.class Key { } B.final class Key { }

C.public class Key { } D.class Key {final int i;} 7.哪个选项可以做为以下方法的覆盖方法(A )。public void add(i nt a) {…} A.public void add(int b) {…}B.void add(int a) {…}

C.public int add(int a) {…}D.public void add(float a) {…} 8.在子类构造方法的哪个地方可以调用超类的构造方法(B )。

A.任何地方

B.构造方法的第一条语句

C.构造方法的最后一条语句

D.不能在子类构造方法中调用超类的构造方法

9.下列程序的运行结果是(C )。

public class Test {

public static void test() {

this.print();

}

public static void print() {

System.out.println("Test");

}

public static void main(String args []) {

test();

}

A.输出Test

B.无输出结果

C.类编译错误,指示不能在static上下文中使用this

D.以上都不对

10.设有如下代码:

1. class Example{

2. String str;

3. Example(){

4. str= "example";

5. }

6. Example(String s){

7. str=s;

8. }

9. }

10. class Demo extends Example{

11. }

12. public class Test{

13. public void f () {

14. Example ex = new Example("Good");

15. Demo d = new Demo("Good");

16. }

17. }

以下哪行将导致错误( D )。

A.第3行B.第6行C.第10行 D.第15行11.在Java中,如下的修饰符不是访问控制修饰符( A )。

A.static B.public C.protected D.private 12.试完成下述程序片段( D )。

public class Point{

int x,y;

public Point(int x,int y){

( )=x;

( )=y;

}

...

}

A.Point.x Point.y

B.无解

C.x1 y1

D.this.x this.y

13.在JAVA 中( C )。

A.一个子类可以有多个父类,一个父类也可以有多个子类

B.一个子类可以有多个父类,但一个父类只可以有一个子类

C.一个子类只可以有一个父类,但一个父类可以有多个子类

14.什么是在子类中创建一个和父类具有一样特征的方法,特征包括方法名字,参数个数,参数类型和方法返回值类型(A )。

A.覆盖(overloading) B.重载(overriding) C.继承(inheritance) D.none

15.哪个关键词在子类中用来访问与父类中一样的方法(A )。

A.super B.this C.static D.以上没有

16.哪个关键词用来引用当前类的对象(B )。

A.super B.this C.static D.以上没有

17.哪个修饰符定义的方法和变量只在定义它们的类中可见,而在其他的任何类中它们都不可见(C )。

A.protected B.public C.private D.none of the above

18.1. 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 t = new Teacher();

10. t.printValue(10);

11. }

12. }

第10行将调用的会是哪个方法( D )。

A.on line 2 B.on line 3 C.on line 6 D.on line 7

19.以下代码运行结果是(C )。

class Base {}

class Sub extends Base {}

class Sub2 extends Base {}

class CEx{

public static void main(String argv[]){

Base b = new Base();

Sub s = (Sub) b;

}

}

A.编译通过B.编译错误C.运行异常D.以上都不对

20.设有如下类定义:

class BaseWidget {

String name="BaseWidget";

void speak(){

System.out.println("I am a "+name);

}

}

class TypeAWidget extends BaseWidget{

TypeAWidget(){

name="TypeA";

}

以下哪段代码将正确编译和执行( B )。

A.Object a=new BaseWidget(); a.speak();

B.BaseWidget b=new TypeAWidget(); b.speak();

C.TypeAWidget c=new BaseWidget(); c.speak();

D.以上都不对

21.设有文件Derived.java中代码如下.

public class Base extends Object{

String objType;

public Base(){

objType="I am a Base type";

}

}

public class Derived extends Base{

public Derived() {

objType="I am a Derived type";

}

public static void main(String args[]){

Derived D=new Derived();

}

}

编译程序将出现何问题( B )。

A.将创建Base.class 和Derived.class 两个文件

B.编译程序将指示第1行有问题

C.编译程序将在第7行出错

D.以上都不对

22.哪种访问组合可放在第3行aMethod前和第8行的aMethod前(C )。

1. class SuperDuper

2. {

3. void aMethod() { }

4. }

5.

6. class Sub extends SuperDuper

7. {

8. void aMethod() { }

9. }

A.line 3: public; line 8: private

B.line 3: protected; line 8: private

C.line 3: private; line 8: protected

D.line 3: public; line 8: protected

23.以下类:

1. public class Base {

2. public void method(int i) {

3. System.out.print( "Value is" + i);

4. }

1. class Sub extends Base {

2. public void method (int j) {

3. System.out.print( "This value is" + j);

4. }

5. public void method(String s) {

6. System.out.print("I was passed " + s);

7. }

8. public static void main{String args[]) {

9. Base bl = new Base();

10. Base b2 = new Sub();

11. bl . method (5);

12. b2 . method (6);

13. }

14. }

Sub类的main方法的执行结果为( C )。

A.Value is 5Value is 6

B.This value is 5This value is 6

C.Value is 5This value is 6

D.This value is 5Value is 6

24.下列程序的运行的结果是(A )。

class parent {

void test() {

System.out.print("parent");

}

}

public class child extends parent {

void test() {

super.test();

System.out.print(" child");

}

public static void main(String args[]){

child x=new child();

x.test();

}

}

A.parent child B.child C.parent D.child parent 25.下列程序的运行的结果是(D )。

class parent{

parent(String s){

s="parent";

}

void test() {

System.out.print("parent");

}

public class child extends parent {

void test() {

super.test();

System.out.print(" child");

}

public static void main(String args[]){

child x=new child();

x.test();

}

}

A.parent child B.child C.parent D.编译错误26.下列程序的运行的结果是(D )。

class parent{

parent(String s){

s="parent";

}

void test() {

System.out.print("parent");

}

}

public class child extends parent {

child(String s){

s="child";

}

void test() {

super.test();

System.out.print(" child");

}

public static void main(String args[]){

child x=new child();

x.test();

}

}

A.parent child B.child C.parent D.编译错误27.看下列程序

package a;

class parent{

private int i=20;

protected int j=30;

public int k=40;

int h=50;

}

class child extends parent {

void f(){ }

在子类child的方法f()中不可以操作的变量是( A )。

A.i B.j C.k D.h 28.看下列程序

package a;

public class parent{

private int i=20;

protected int j=30;

public int k=40;

int h=50;

}

package b;

import a.parent;

class child extends parent {

void f(){ }

}

在子类child的方法f(A)中不可以操作的变量是( D )。

A.i B.j C.k D.h 29.看下列程序

package a;

class parent{

private int i=20;

protected int j=30;

public int k=40;

int h=50;

}

class child1 extends parent { }

class child2 extends child1{

void f(){ }

}

在子类child2的方法f()中不可以操作的变量是( A )。

A.i B.j C.k D.h 30.如下类的声明:

class A {}

则类A的父类是( C )。

A.没有父类B.本身C.Object D.Lang

31.下列程序的运行结果是(C )。

class parent{

int i=20;

int j=30;

void f(){

System.out.print(" "+i);

}

}

class child extends parent {

int k=40;

void f(){

System.out.print(" "+i);

}

void g(){

System.out.print(" "+k);

}

public static void main(String args[]){

parent x=new child();

System.out.print(x.i);

x.f();

child x1=(child)x;

System.out.print(" "+x1.i);

x1.f();

}

}

A.30 30 30 30 B.20 20 20 20 C.20 30 30 30 D.都不对

32.什么样的方法不能被重写(B )。

A.私有方法

B.最终方法(final方法)

C.受保护的方法

D.都不对

33.如果一个成员变量声明时必须赋给初值,而且不能再发生变化,那么这个成员变量是( B )。

A.私有变量

B.最终变量(常量)

C.受保护的变量

D.都不对

34.关于重载和重写的叙述正确的是(D)。

A.重载是多态的一种,而重写不是

B.重载是子类中定义的方法和父类中某个方法相同

C.重写是一个类中多个同名的方法,并且方法的参数不同

D.重写方法时不允许降低方法的访问权限

二.编程题

1.创建2个包:a和b。在包a中编写一个公共类A,类A中有:2个public double类型的属性c、d;一个构造方法public A(double x,double y)对c,d进行初始化;还有一个方法public double add()返回c与d的和。在包b中编写一个主类B,在类B的main方法中创建类A的对象e,

并用对象e调用方法add求2个数的和。

1.package a;

public class A {

public double c,d;

public A(double x, double y){

c = x;

d = y;

}

public double add(){

}

}

package b;

import a.A;

class B {

public static void main(String[] args) {

A e = new A(1,2)

System.out.println("两个数之和:" + e.add());

}

}

2. 编写一个类A,该类创建的对象可以调用方法f输出小写的英文字母表。然后再编写一个A类

的子类B,要求子类B必须继承A类的方法f(不允许重写),子类B创建的对象不仅可以调用方法f输出小写的英文字母表,而且可以调用子类新增的方法g输出大写的英文字母表。

最后编写主类C,在主类的main方法中测试类A与类B。

2.class A {

void f(){

System.out.println("A输出小写的英文字母表:");

char c;

for(c='a';c<='z';c++) {

System.out.print(" "+c);

}

System.out.println();

}

}

class B extends A{

void g(){

char c;

System.out.println("B输出大写的英文字母表:");

for(c='A'; c<='Z'; c++)

System.out.print(" "+c);

System.out.println();

}

}

class C{

public static void main(String args[]){

A a = new A();

B b=new B();

a.f();

b.f();

b.g();

}

}

3.编写一个Java应用程序,该程序包括3个类: A类、B类和主类E。其中类B是类A的子类,在子类B中新增了成员变量和成员方法,并且隐藏了父类A的成员变量和重写了父类A的成员

来测试上转型对象的一些特性。

3.class A {

int i=1;

int j=10;

void a(){

System.out.println("a of A");

}

void b(){

System.out.println("b of A");

}

}

class B extends A {

int j = 20;

int k =200;

void b(){

System.out.println("b of B");

}

void c(){

System.out.println("c of B");

}

}

class E {

public static void main(String[] args){

A a = new B();

System.out.println("a.i=" + a.i); //extends

System.out.println("a.j=" + a.j); //隐藏

a.a(); //extends

a.b(); //重写

}

}

4.编写一个Java应用程序,该程序包括3个类:类人猿类、People类和主类E。要求:1)类人猿中有个构造方法:类人猿(String s),并且有个public void speak()方法,在speak方法中输出“咿咿呀呀......”的信息。

2)People类是类人猿的子类,在People类中重写方法speak(),在speak方法中输出“小样的,不错嘛!”的信息。

3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”。

4)在主类E的main方法中创建类人猿与People类的对象类测试这两个类的功能。4.class 类人猿{

类人猿(String s){ }

public void speak(){

System.out.println("咿咿呀呀......");

}

}

class People extends 类人猿{

super("");

}

public void speak(){

System.out.println("小样的,不错嘛!");

}

void think(){

System.out.println("别说话!认真思考!");

}

}

class E{

public static void main(String[] args){

类人猿yuan = new 类人猿("");

yuan.speak();

People p = new People();

p.speak();

p.think();

}

}

相关主题
文本预览
相关文档 最新文档