1、类的封装与继承习题题1以下关于继承的叙述正确的是( C )。A、在Java中类只允许单一继承B、在Java中一个类只能实现一个接口C、在Java中一个类不能同时继承一个类和实现一个接口D、在Java中接口只允许单一继承2.有继承关系时用到的关键字是(A )。( 2 分)A:extendB:extendsC:implementsD:implement3:Java变量中,以下不属于复合类型的数据类型是( D)。( 2 分)A:类B:字符型C:数组型D:接口4:java中定义数组名为 abc,下面(B )可以得到数组元素的个数。( 2 分)A:abc.length( )B:abc.lengthC:l
2、en(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)。(
3、 2 分)A:Java不提供goto语句,所以也就没指定goto作为关键字。B:Java没有指针。C:Java没有联合和结构D:Java只有单继承没有多重继承。8:若需要定义一个类域或类方法,应使用( B)修饰符。( 2 分)A:staticB:packageC:privateD:public9:下列说法( C)是正确的。( 2 分)A:程序执行到break语句是一定会结束所有的循环B:程序执行到continue语句时会结束当前循环C:break语句和continue语句都可以和标签协同使用D:continue语句和break语句的作用相同10:以下说法正确的是(D )。( 2 分)A:每个J
4、ava类都至少有一个构造方法,如果没有则机器自动生成一个默认的构造方法。B:如果类里定义了一个或多个构造方法,那么java也提供默认的构造方法C:每个Java类中用户必须定义至少一个构造方法。D:以上都不对11:对封装的理解正确的是(A )。( 3 分)A:封装就是把对象的属性和行为结合成一个独立的单位。B:封装就是把对象完全隐蔽起来,不让外界访问。C:封装性是一个使用问题。D:封装和抽象是一回事。12:对成员的访问控制保护最强的是(C )。( 3 分)A:publicB:缺省C:privateD:protected13:类Test1定义如下: 1.public class Test12. p
5、ublic 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:构造函数与类同
6、名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:程序将输出:= = succeededB:程序将输出: .equals() succeededC:程序将输出:= = succeeded .equal
7、s() succeededD:程序无输出16:关于以下程序代码的说明正确的是( C)。1class 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);
8、12 13 ( 2 分)A:5行不能通过编译,因为引用了私有静态变量B:10行不能通过编译,因为x是私有静态变量C:程序通过编译,输出结果为:x=103D:程序通过编译,输出结果为:x=102三、程序阅读题1、以下程序的输出结果为_ Peter is 17 years old!_。public class Person String name; int age; public Person(String name, int age) this.name = name; this.age = age; public static void main(String args) Person c =
9、 new Person(Peter, 17); System.out.println(c.name + 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; p
10、ublic 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;
11、 public void setWeight(float weight) this.weight = weight; private void out() System.out.println(name + 体重: + weight + 斤); public static void main(String args) Tom.name = 汤姆猫; Tom cat = new Tom(); cat.setWeight(20); cat.out(); 4、以下程序的输出结果:姓名:Tom 年龄:15家庭住址: 金水区 学校:九中 电话: 66123456public class Father S
12、tring name, address, tel; int age; public Father(String name, int age) this.name = 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; pu
13、blic 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 i
14、nt a = 1, 2, 3, 4, 5 ; void out() for (int j = 0; j a.length; j+) System.out.print(aj + ); 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.to
15、String( ); 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 ,
16、 80 , 90 ; for ( i = 0 ; i a.length ; i + ) if ( ai%3 = = 0 ) s += ai ; 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
17、, 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=301以下程序的输出结果为 相等_ 。class StringTest1 public static void main(String args) String s1=hello; String s2=new Stri
18、ng(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 ( ajak ) k = j; int temp
19、 =ai; ai = ak; ak = temp; for ( i =0 ; ia.length; i+ ) System.out.print(ai+ ); 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 = n
20、ew 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 ); publi
21、c 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类,添
22、加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();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1