类的封装与继承习题(题)

更新时间:2023-05-04 09:31:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

. . .

1.以下关于继承的叙述正确的是( C )。

A、在Java中类只允许单一继承

B、在Java中一个类只能实现一个接口

C、在Java中一个类不能同时继承一个类和实现一个接口

D、在Java中接口只允许单一继承

2.有继承关系时用到的关键字是(A )。( 2 分)

A:extend

B:extends

C:implements

D:implement

3:Java变量中,以下不属于复合类型的数据类型是( D)。( 2 分)

A:类

B:字符型

C:数组型

D:接口

4:java中定义数组名为 abc,下面(B )可以得到数组元素的个数。( 2 分) A:abc.length( )

B:abc.length

C:len(abc)

D:ubound(abc)

5:下列说确的是(A )。( 2 分)

A:编译后的Java源程序根据文件中定义的类和接口的个数产生相应个数的.class字节码文件。

B:Java语言的源文件中不是任何地方都可以加注释的。

C:一个文件中可以有多个访问权限为public的类。

D:含有main()方法的类可以为public的类,也可以为其他类。

6:在调用构造函数时(C )( 2 分)

A:子类可以不加定义就使用父类的所有构造函数

B:不管类中是否定义了何种构造函数,创建对象时都可以使用默认构造函数C:先调用父类的构造函数

D:先调用形参多的构造函数

7:Java与C/C++比较说法错误的是(D)。( 2 分)

A:Java不提供goto语句,所以也就没指定goto作为关键字。

.. ..

. . .

B:Java没有指针。

C:Java没有联合和结构

D:Java只有单继承没有多重继承。

8:若需要定义一个类域或类方法,应使用( B)修饰符。( 2 分)

A:static

B:package

C:private

D:public

9:下列说法( C)是正确的。( 2 分)

A:程序执行到break语句是一定会结束所有的循环

B:程序执行到continue语句时会结束当前循环

C:break语句和continue语句都可以和标签协同使用

D:continue语句和break语句的作用相同

10:以下说确的是(D )。( 2 分)

A:每个Java类都至少有一个构造方法,如果没有则机器自动生成一个默认的构造方法。

B:如果类里定义了一个或多个构造方法,那么java也提供默认的构造方法

C:每个Java类中用户必须定义至少一个构造方法。

D:以上都不对

11:对封装的理解正确的是(A )。( 3 分)

A:封装就是把对象的属性和行为结合成一个独立的单位。

B:封装就是把对象完全隐蔽起来,不让外界访问。

C:封装性是一个使用问题。

D:封装和抽象是一回事。

12:对成员的访问控制保护最强的是(C )。( 3 分)

A:public

B:缺省

C:private

D:protected

.. ..

. . .

13:类Test1定义如下:

1.public class Test1{

2. public float aMethod(float a,float b){ }

3.

4.} 将以下( B)方法插入行3是不合法的。( 3 分)

A:public float aMethod(float a, float b,float c){ }

B:public float aMethod(float c,float d){ }

C:public int aMethod(int a, int b){ }

D:private float aMethod(int a,int b,int c){ }

14关于构造函数的说法( B)正确。( 2 分)

A:一个类只能有一个构造函数

B:一个类可以有多个不同名的构造函数

C:构造函数与类同名

D:构造函数必须自己定义,不能使用父类的构造函数

15:关于以下程序段,正确的说法是(D )。

1.String s1=”abc”+”def”; 2. String s2=new String(s1); 3. if (s1= =s2)4.System.out.println(“= = succeeded”); 5.if (s1.equals(s2)) 6.System.out.println(“.equals() succeeded”);( 2 分) A:程序将输出:= = succeeded

B:程序将输出: .equals() succeeded

C:程序将输出:= = succeeded .equals() succeeded

D:程序无输出

16:关于以下程序代码的说明正确的是( C)。

1.class HasStatic{ 2. private static int x=100; 3. public static void main(String args[ ]){ 4. HasStatic hs1=new HasStatic( ); 5. hs1.x++; 6. HasStatic hs2=new HasStatic( ); 7. hs2.x++; 8. hs1=new HasStatic( ); 9. hs1.x++; 10. HasStatic.x- -; 11.System.out.println(“x=”+x); 12. } 13. }( 2 分)

A:5行不能通过编译,因为引用了私有静态变量

B:10行不能通过编译,因为x是私有静态变量

C:程序通过编译,输出结果为:x=103

D:程序通过编译,输出结果为:x=102

.. ..

. . .

三、程序阅读题

1、以下程序的输出结果为___ Peter is 17 years old!___________________。

public class Person {

String name;

int age;

public Person(String name, int age) {

b543b0fc4531b90d6c85ec3a87c24028905f8517 = name;

this.age = age;

}

public static void main(String[] args) {

Person c = new Person("Peter", 17);

System.out.println(b543b0fc4531b90d6c85ec3a87c24028905f8517 + " is " + c.age + " years old!");

}

}

2、以下程序的输出结果为__课程号:101 __课程名:ASP __学分:_3_________________。public class Course {

private String cNumber;

private String cName;

private int cUnit;

public Course(String number, String name, int unit) {

cNumber = number;

cName = name;

cUnit = unit;

}

public void printCourseInfo() {

System.out.println("课程号:" + cNumber + " 课程名:" + cName + " 学分:" + cUnit);

}

}

class CourseTest {

public static void main(String[] args) {

Course c;

c = new Course("101", "ASP", 3);

c.printCourseInfo();

}

}

.. ..

. . .

3、以下程序的输出结果为_汤姆猫_体重:20斤_____________________。

public class Tom {

private float weight;

private static String name;

public void setWeight(float weight) {

this.weight = weight;

}

private void out() {

System.out.println(name + "体重:" + weight + "斤");

}

public static void main(String[] args) {

b543b0fc4531b90d6c85ec3a87c24028905f8517 = "汤姆猫";

Tom cat = new Tom();

cat.setWeight(20);

cat.out();

}

}

4、以下程序的输出结果::Tom 年龄:15家庭住址: 金水区学校:九中 : 66123456 public class Father {

String name, address, tel;

int age;

public Father(String name, int age) {

b543b0fc4531b90d6c85ec3a87c24028905f8517 = name;

this.age = age;

}

void out() {

System.out.print(":" + name);

System.out.print(" 年龄:" + age);

}

void outOther() {

System.out.print(" 家庭住址:" + address);

System.out.print(" :" + tel);

}

}

.. ..

. . .

class Son extends Father {

String school;

public Son(String name, int age) {

super(name, age);

}

void out() {

super.out();

super.outOther();

System.out.println(" 学校:" + school);

}

public static void main(String args[]) {

Son son = new Son("Tom", 15);

son.address = "金水区";

son.school = "九中";

son.tel = "66123456";

son.out();

}

}

5、下列程序的运行结果是__12345___________________。public class MyClass {

int a[] = { 1, 2, 3, 4, 5 };

void out() {

for (int j = 0; j < a.length; j++)

System.out.print(a[j] + "");

}

public static void main(String[] args) {

MyClass my = new MyClass();

my.out();

}

}

1、import java.io.*;

public class abc

{

public static void main(String args [ ])

{

.. ..

. . .

AB s = new AB("Hello!","I love JAVA.");

System.out.println(s.toString( ));

}

}

class AB {

String s1;

String s2;

public AB(String str1, String str2)

{

s1 = str1;

s2 = str2;

}

public String toString( )

{

return s1+s2;

}

}

运行结果:____ Hello! I love JAVA.________________

2、 import java.io.* ;

public class abc

{

public static void main(String args[ ])

{ int i, s = 0 ;

int a[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 }; for ( i = 0 ; i < a.length ; i ++ )

if ( a[i]%3 = = 0 ) s += a[i] ;

System.out.println("s="+s);

}

}

运行结果:__s=180__________________

3、import java.io.* ;

public class abc

.. ..

. . .

{

public static void main(String args[ ])

{

System.out.println("a="+a+"\nb="+b);

}

}

class SubClass extends SuperClass

{ int c;

SubClass(int aa, int bb, int cc)

{

super(aa, bb);

c=cc;

}

}

class SubSubClass extends SubClass

{ int a;

SubSubClass(int aa, int bb, int cc)

{ super(aa, bb, cc);

A = aa+bb+cc;

}

void show()

{

System.out.println("a="+a+"\nb="+b+"\nc="+c);

}

}

运行结果:a=60

b=20

c=30

1.以下程序的输出结果为相等____________________ 。

class StringTest1

{

public static void main(String[] args)

{

String s1="hello";

.. ..

. . .

String s2=new String("hello");

if(s1.equals(s2)){

System.out.println("相等");

}else{

System.out.println("不相等");

}

}

}

2.以下程序段的输出结果为 56789 ____________________ 。

public class TestArray

{

public static void main(String args[ ]){

int i , j ;

int a[ ] = { 5,9,6,8,7};

for ( i = 0 ; i < a.length-1; i ++ ) {

int k = i;

for ( j = i ; j < a.length ; j++ )

if ( a[j]

int temp =a[i];

a[i] = a[k];

a[k] = temp;

}

for ( i =0 ; i

System.out.print(a[i]+" ");

System.out.println( );

}

}

3.写出以下程序的功能。

import java.io.*;

public class TestFile

{

public static void main(String args[]) throws Exception .. ..

. . .

{

BufferedReader br = new BufferedReader(

new InputStreamReader(System.in));

BufferedWriter bw = new BufferedWriter(new FileWriter(“input.txt"));

String s;

while (true)

{

System.out.print("请输入一个字符串: ");

System.out.flush();

s=br.readLine();

if (s.length()==0) break;

bw.write(s);

bw.newLine();

}

bw.close();

}

}

功能:判断输入字符串是否等于input.txt文件中的字符串

4.阅读以下程序,写出输出结果。

class Animal {

Animal() {

System.out.print ("Animal "); }

}

public class Dog extends Animal {

Dog() {

System.out.print ("Dog "); }

public static void main(String[] args) {

Dog snoppy= new Dog(); }

}

输出结果:_ Animal ___ Dog ________________

.. ..

. . .

五、程序设计题

1、按以下要求编写程序

(1) 创建一个Rectangle类,添加width和height两个成员变量

(2) 在Rectangle中添加两种方法分别计算矩形的周长和面积

(3) 编程利用Rectangle输出一个矩形的周长和面积

解答:package training5;

//(1) 创建一个Rectangle类,添加width和height两个成员变量

//(2) 在Rectangle中添加两种方法分别计算矩形的周长和面积

//(3) 编程利用Rectangle输出一个矩形的周长和面积

public class Rectangle {

private int width;

private int height;

public Rectangle(int width, int height) {

this.width = width;

this.height = height;

}

public void square(){

int square;

square=width*height;

System.out.println("面积:"+square);

}

public void zhouc(){

int zhouc;

zhouc=2*(width+height);

System.out.println("周长;"+zhouc);

}

public static void main(String args[]){

Rectangle jux=new Rectangle(3,5);

jux.square();

jux.zhouc();

}

}

.. ..

本文来源:https://www.bwwdw.com/article/3aee.html

Top