Java基础部分题(参考答案)

更新时间:2024-01-20 11:22:01 阅读量: 教育文库 文档下载

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

基础语法1:

1、下列变量定义错误的是。

2、A) int a; B) double b=4.5; C) boolean b=true; D)float f=9.8; 3、下列数据类型的精度由高到低的顺序是:

a)float,double,int,long b)double,float,int,byte c)byte,long,double,float d)double,int,float,long 4、执行完下列代码后, int a=3; char b='5';

char c=(char)(a+b); c的值是?

A)?8? b)53 c)8 d)56 5、Unicode是一种_____________

A) 数据类型 B)java包 C)字符编码 D)java类 6、6+5%3+2的值是___________ A)2 B)1 C) 9 D)10

7、下面的逻辑表达式中合法的是__________

A)(7+8)&&(9-5) B)(9*5)||(9*7) C)9>6&&8<10 D)(9%4)&&(8*3) 8、java语言中,占用32位存储空间的是__________。

A、long,double B、long,float C、int,double D、int,float 9、假设int a=3,b=2,c=1,以下语句正确的是___________。

A)c=c/float(a//b) b) c=c/((float a)/b)

c) c=(float)c/(a/b) d) c= c/(int)(a/(float)b) 10、 2|3&5的值是________________。

A)1 B)2 C)3 D)5 11、指出下列正确的语句:

A) byte i = 389; B) long lv = i*3+4.5; C) int x = 87L; D) long l = 10;

11、指出下列类型转换中正确的是______________。

A)int i=’A’ B)long L=8.4f C)int i=(boolean)8.9 D)int i=8.3 12、以下的选项中能正确表示Java语言中的一个整型常量的是( )。 A) 12. B) -20 C) 1,000 D) 4 5 6 13、以下选项中,合法的赋值语句是( )。

A) a = = 1; B) ++ i; C) a=a + 1= 5; D) y = int ( i );

14、若所用变量都已正确定义,以下选项中,非法的表达式是( )。 A) a != 4||b==1 B) 'a' % 3 C) 'a' = 1/2 D) 'A' + 32 15、对于一个三位的正整数 n,取出它的十位数字k(k为整型)的表达式是( )。 A) k = n / 10 % 10 B) k = ( n - n / 100 * 100 ) C) k = n % 10 D) k = n / 10

16、现有一变量声明为boolean aa;下面赋值语句中正确的是( )。

A)aa=false; B)aa=False; C)aa=\ D)aa=0;

17、设有定义 int i = 6 ;,则执行以下语句后,i 的值为( )。 i += i - 1;

A) 10 B) 121 C) 11 D) 100 18、下列语句序列执行后,ch1 的值是( )。 char ch1=’A’,ch2=’W’;

if(ch1 + 2 < ch2 ) ++ch1;

A) ?A? B) ?B? C) ?C? D) B 19、下列语句序列执行后,i 的值是( )。 int i=8, j=16;

if( i-1 > j ) i--; else j--;

A) 15 B) 16 C) 7 D) 8 20、设有定义 float x=3.5f, y=4.6f, z=5.7f;则以下的表达式中,值为true的是( )。

A) x > y || x > z B) x != y

C) z > ( y + x ) D) x < y & ! ( x < z )

21、设有定义 int i=123; long j=456; 下面赋值不正确的语句是( )。

A) j=i; B) j=(long)i; C) i=(int)j; D) i=j; 22、下列的变量定义中,错误的是( )。()

A) int i; B) int i=10000; C) static int i=100; D) int 123_$; 23、以下的变量定义语句中,合法的是( )。 A) float $_*5= 3.4F; B) byte b1= 15678; C) double a =10000; D) int _abc_ = 3721L; 24、以下字符常量中不合法的是( )。 A) '|' B) '\\' C) \ D) '我'

25、若以下变量均已正确定义并赋值,下面符合Java语言语法的语句是( )。 A) b = a!=7 ; B) a = 7 + b + c=9; C) i=12.3* % 4; D) a = a + 7 = c + b;

26、执行下列程序段后,b, x, y的值分别是( )。 int x=6,y=8; boolean b;

b=x>y&&++x==--y;

A) true , 6, 8 B) false , 7, 7 C) true, 7, 7 D) false, 6, 8

1、以下代码的输出结果是__j____。 int i=9; char c=?a?; char d=(char)(c+i); System.out.println(d);

2、面代码执行完后的输出是_b is:false___-。 int x=3; int y=4; boolean b=true; System.out.println(\3、表达式(-100%3)的值是__-1____。

4、int x=2,y=4,z=3 则x>y&&z>y的结果是_____false_____。 5、写出定义双精度浮点型常量PI,值为3.1415926的语句:____ final double PI=3.1415926____。

6、在Java语言中,逻辑常量只有true 和 ____false___ 两个值。 7、表达式1/2*3的计算结果是___0___。

8、执行以下程序段后:a = _6__, b = __18___ 。 int a = 5, b;

b = ++a * 3;

9、Java中的的字符使用的是 16 位的____unicode____ 编码。

10、 Java语言中的浮点型数据根据数据存储长度和数值精度的不同,进一步分为float和 __double__两种具体类型。

11、当整型变量n的值不能被13除尽时,其值为 false 的Java语言表达式是___n==0____ 。

12、表达式3/6 * 5的计算结果是___0___ 。

13、若a,b为int型变量且已分别赋值为2,4。表达式!(++a!=b--)的值是__false____ 。

14、若a,b为int型变量且已分别赋值为2,6。表达式(a++)+(++b) +a*b的值是__30__ 。 基础语法2:

1.下列语句序列执行后,m 的值是( )。

int a=10, b=3, m=5;

if( a==b ) m+=a; else m=++a*m; A) 15 B) 50 C) 55 D) 5 2.下列语句序列执行后,k 的值是( )。

int i=4,j=5,k=9,m=5;

if(i>j||m

int i=10, j=18, k=30; switch( j - i )

{ case 8 : k++;

case 9 : k+=2;

case 10: k+=3; default : k/=j; }

A) 31 B) 32 C) 2 D) 33

4.若a和b均是整型变量并已正确赋值,正确的switch语句是( )。 A) switch(a+b); { ...... } B) switch( a+b*3.0 ) { ...... } C) switch a { ...... } D) switch ( a%b ) { ...... } 5.下列语句序列执行后,r 的值是( )。

char ch='8'; int r=10; switch( ch+1 ) {

case '7': r=r+3; case '8': r=r+5;

case '9': r=r+6; break; default: r=r+8;

}

A) 13 B) 15 C) 16 D) 18 6.设int 型变量 a、b,float 型变量 x、y,char 型变量 ch 均已正确定义并赋值,正确的switch语句是( )。

A) switch (x + y) { ...... } B) switch ( ch + 1 ) { ...... } C) switch ch { ...... } D) switch ( a + b ); { ...... } 7.下列语句序列执行后,k 的值是( )。

int x=6, y=10, k=5; switch( x%y ) {

case 0: k=x*y; case 6: k=x/y;

case 12: k=x-y; default: k=x*y-x;

}

A) 60 B) 5 C) 0 D) 54 8.下列语句序列执行后,r 的值是( )。

char ch='A'; int r=6; switch( ch+5 ) {

case 'A': r=r+3; case 'B': r=r+5;

case 'C': r-=6; break; default : r/=2;

}

A) 11 B) 3 C) 2 D) 9 9.以下for循环的执行次数是()。

for(int x=0;(x==0)&(x>4);x++);

A)无限次 B)一次也不执行 C)执行4次 10.下列语句序列执行后,j 的值是( )。 int j=1;

for( int i=5; i>0; i-=2 ) j*=i;

A) 15 B) 1 C) 60 D) 0 11.以下for循环的执行次数是( )。

for(int x=0;(x==0)&(x<4);x++);

A)无限次 B)一次 C)执行4次 D)执行3次12.下列语句序列执行后,j 的值是( )。

int j=2;

for( int i=7; i>0; i-=2 ) j*=2;

A) 15 B) 1 C) 60 D) 32

13.以下由 for 语句构成的循环执行的次数是( )。

for ( int i = 0; true ; i++) ;

A)有语法错,不能执行 B)无限次 C)执行1次 D)一次也不执行 14.下列语句序列执行后,i 的值是( )。

int s=1,i=1;

while( i<=4 ) {s*=i;i++;}

A) 6 B) 4 C) 24 D) 5 15.下列语句序列执行后,j 的值是( )。

int j=8, i=6;

while( i >4 ) i-=2; - -j;

A) 5 B) 6 C) 7 D) 8 16. 若有循环:

int x=5,y=20;

do { y-=x; x+=2; }while(x

A) 2次 B) 1次 C) 0次 D) 3次 17.以下由do-while语句构成的循环执行的次数是()。

int m = 8;

D)执行3次 do { ++m; } while ( m < 8 ); A)一次也不执行 B)执行1次

C)8次 D)有语法错,不能执行 18.下列语句序列执行后,i的值是( )。

int i=10;

do { i/=2; } while( i>1 );

A) 1 B) 5 C) 2 D) 0 19.下列语句序列执行后,i的值是()。

int i=10;

do { i/=2; } while( i--> 1 );

A) 1 B) 5 C) 2 D) -1

20.下列循环中,执行break outer语句后,所列(C)语句将被执行。

outer:

for(int i=1;i<10;i++){ inner:

for(int j=1;j<10;j++){ if(i*j>50) break outer;

System.out.println(i*j); } next:

A)标号为inner的语句 B)标号为outer的语句 C)标号为next的语句 D)以上都不是 21.下列循环中,执行continue outer语句后,()说法正确。

outer:

for(int i=1;i<10;i++){ inner:

for(int j=1;j<10;j++){ if(i*j>50)

continue outer;

System.out.println(i*j); } }

A)程序控制在外层循环中并且执行外层循环的下一迭代 B)程序控制在内层循环中并且执行内层循环的下一迭代 C)执行标号为next的语句 D)以上都不是

27.下面是一个java应用程序(Application),它的功能是在屏幕上输出26个小写英文字母,其中每个字母相隔一个制表符,请完成程序填空。 public _class__ Class1{

public static void main( String args[] ){ char c='a';

for (int i=1;i<=26;i++)

{ System.out.print(_(char)(c+i-1)+”\\t”_); } } }

5.编写方法,计算一个整数各位数字的和。使用如下方法说明: public static int sumDigits(long n) 例如:sumDigits(234)返回2+3+4=9 public static init sumDigits(long n){

int s=0; while(n>0){ s=s+n; n=n/10; }

System.out.println(n+”各个位上数字和为:”+s); }

面向对象2: 一、选择题

1 定义类头时,不可能用到的关键字是()。

A) class B)private C)extends D)public 2.下列类定义中,不正确的是()。

A) class x { .... } B) class x extends y { .... } C) static class x implements y1,y2 { .... } D) public class x extends Applet { .... }

3.下列类头定义中,错误的是()。

A)public x extends y {...} B) public class x extends y {...} C)class x extends y implements y1 {...} D)class x {...}

4.设 A为已定义的类名,下列声明A类的对象a的语句中正确的是( )。 A) float A a; B) public A a=A( ); C) A a=new int( ); D) static A a=new A( );

5.设 A为已定义的类名,下列声明A类的对象a的语句中正确的是()。 A) public A a=new A( ); B) public A a=A( ); C) A a=new class( ); D) a A;

6.设 X 、Y 均为已定义的类名,下列声明类X的对象x1的语句中正确的是()。 A) public X x1= new Y( ); B) X x1= X ( ); C) X x1=new X( ); D) int X x1;

7. 设X 、Y为已定义的类名,下列声明X类的对象x1的语句中正确的是()。 A) static X x1; B) public X x1=new X(int 123); C) Y x1; D) X x1= X( );

8.通过类MyClass中的不含参数的构造方法,生成该类的一个对象obj,可通过以下语句实现: ___MyClass obj=new MyClass()___。

9. 设i , j为类X中定义的int型变量名,下列X类的构造方法中不正确的是( )。 A) void X(int k ){ i=k; } B) X(int k ){ i=k; }

C) X(int m, int n ){ i=m; j=n; } D) X( ){i=0;j=0; }

10. 有一个类A,以下为其构造方法的声明,其中正确的是( )。 A)public A(int x){...} B)static A(int x){...} C)public a(int x){...} D)void A(int x){...}

11. 有一个类A,以下为其构造方法的声明,其中正确的是( )。

A)void A(int x){...} B)A(int x){...} C)a(int x){...} D)void a(int x){...} 12. 设i、j、k为类X中定义的int型变量名,下列类X的构造方法中不正确的是( )。 A) X( int m){ ... } B) void X( int m){ ... } C) X( int m, int n){ ... } D) X( int h,int m,int n){ ... }

13. 设i , j为类X中定义的double型变量名,下列X类的构造方法中不正确的是( )。

A) double X(double k ){ i=k; return i; } C) X( ){i=6;j=8; }

B) X(double m, double n ){ i=m; j=n; } D) X(double k ){ i=k; } 二、填空题

1.下面是一个类的定义,请完成程序填空。 public class _MyClass__{ int x, y;

Myclass ( int i, int j__) // 构造方法 { x=i; y=j; } }

14. 下面是一个类的定义,请将其补充完整。 class Student_ { String name; int age; Student( _String __ s, int i){ name=s; age=i; } }

2.下面是一个类的定义,请将其补充完整。 class __myclass___ // 定义名为myclass的类 {

___static___ int var=666;

static int getvar(){ return var; } }

3.下面程序的功能是通过调用方法max()求给定的三个数的最大值, 请将其补充完整。 public class Class1{

public static void main( String args[] ){

int i1=1234,i2=456,i3=-987; int MaxValue;

MaxValue=__max(i1,i2,i3)__;

System.out.println(\三个数的最大值:\alue); }

public __static_ int max(int x,int y,int z) { int temp1,max_value; temp1=x>y?x:y;

max_value=temp1>z?temp1:z; return max_value; } }

4.下面是一个类的定义,请将其补充完整。 __class___ A { String s;

__static_ int a=666;

A(String s1) { s=s1; }

static int geta( ) { return a; } }

三、程序阅读题

1.下面是一个类的定义,根据题目要求回答以下问题. class B{ private int x; private char y; public B(int i,char j){ x=i; y=j; } public void show(){ System.out.println(\ } public void methodC(int x){ this.x=this.x+x; y++; show(); } }

(1)定义类B的一个对象b,将类中的变量x初始化为10、变量y初始化为?A?,请写出相应的语句。

(2)若在(1)问基础上有方法调用语句:b.show(); 则输出如何? (3)若在(1)问基础上增加语句: b.methodC(1); 则输出为何?

2.阅读程序,回答问题。 public class Test52{

static String str1=\ static String str2=\ public static void main(String args[])

{ System.out.print(str1); System.out.println(str2); } }

问题:1)这是哪种形式的 java 程序 ? 2)程序的输出是什么?

3. 写出下列程序的输出结果 public class Test{ public static void main(String[] args){ Count myCount = new Count(); int times = 0; for(int i=0;i<100;i++) increment(myCount , times); System.out.println(“count is” + myCount.count); System.out.println(“time is”+ times); } public static void increment(Count c , int times){ c.count++; times++; } }

class Count{ public int count; Count(int c){ count =c; } Count(){ count =1;

} }

4.写出下列程序的输出结果: public class Test{ public static void main(String[] args){ Cirecle circle1 = new Circle(1); Cirecle circle2 = new Circle(2); // Attempt to swap circle 1 with circle2 System.out.println(“Before swap:circle1 = ”+circle1.radius

}

+”circle2 = ”+circle2.radius);

swap(circle1,circle2);

System.out.println(“After swap: circle1 = ”+circle1.radius

+”circle2 = ”+circle2.radius);

}

public static void swap(Circle x , Circle y){ System.out.println(“Before swap: x = ”+x.radius

+”y = ”+y.radius);

Circle temp = x; x = y; y = temp; System.out.println(“Before swap: x = ”+x.radius

+”y = ”+y.radius); }

5.阅读下面程序,回答问题: public class Foo{ int i; static String s; void imethod(){ } static void smethod(){ } }

设f是Foo的一个实例,下列语句正确吗? System.out.println(f.i); System.out.println(f.s); f.imethod(); f.smethod();

System.out.println(Foo.i); System.out.println(Foo.s); Foo.imethod(); Foo.smethod();

6.下列程序的输出结果是什么? public class Foo{ static int i=0; static int j=0; public static void main(String[] args){ int i=2; int k=3; { int j=3; System.out.println(“i + j is ”+ i + j); } k = i +j; System.out.println(“k is ”+k);

{ return firstName + lastName; } }

class Person {

private Name name;//姓名 Person(Name n) { name=n; }

//填写访问器方法 }

class Test { public static void main(String[] args) {

Name theName=new Name(\张\三\Person p=new Person(theName);

System.out.println(p.getName());//输出结果:张三 } }

class Name {

private String firstName;//姓 private String lastName;//名 Name(String f,String l) { firstName=f; lastName=l; }

public String getFirstName() { return firstName; }

public void setFirstName(String fn) { firstName=fn; }

public String getLastName() { return lastName; } public void setLastName(String ln) { lastName=ln; } public String toString() { return firstName + lastName; } }

class Person {

private Name name;//姓名 Person(Name n) { name=n; } public Name getName() { return name; } public void setName(Name n) { name=n; } }

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

}

Name theName=new Name(\张\三\Person p=new Person(theName);

System.out.println(p.getName());//输出结果:张三

7. //类的定义

编写一个类实现复数的运算: 复数类 ComplexNumber 的属性:

m_dRealPart :实部,代表复数的实数部分; m_dMaginPart :虚部,代表复数的虚数部分; 复数类ComplexNumber 的方法:

ComplexNumber(double r , double i ) :构造方法,创建复数对象的同时完成复数的实部、虚部的初始化,r为实部的初值,i为虚部的初值; getRealPart() :获得复数对象的实部; getImaginPart() :获得复数对象的虚部;

setRealPart(double d ) :把当前复数对象的实部设置为给定的形式参数的数字;

setImaginaryPart ( double d ) :把当前复数对象的虚部设置为给定的形式参数的数字;

complexAdd(ComplexNumber c) :当前复数对象与形式参数复数对象相加,所得的结果也是复数值,返回给此方法的调用者;

complexMinus(ComplexNumber c) :当前复数对象与形式参数复数对象相减,所得的结果也是复数值,返回给此方法的调用者;

complexMulti(ComplexNumber c) :当前复数对象与形式参数复数对象相乘,所得的结果也是复数值,返回给此方法的调用者; 编程实现这个类,并验证所编写的类能否正常使用。 class ComplexNumber { double m_dRealPart; double m_dImaginPart; public ComplexNumber(double r, double i) { m_dRealPart = r; m_dImaginPart = i; } public double getRealPart() { return m_dRealPart; }

public double getImaginaryPart() { return m_dImaginPart; }

public void setRealPart(double d) { m_dRealPart = d; }

public void setImaginaryPart(double d) { m_dImaginPart = d; }

public ComplexNumber complexAdd(ComplexNumber c) { return (new ComplexNumber(this.m_dRealPart+c.m_dRealPart,

this.m_dImaginPart+c.m_dImaginPart));

}

public ComplexNumber complexMinus(ComplexNumber c) {

return (new ComplexNumber(this.m_dRealPart-c.m_dRealPart,

this.m_dImaginPart-c.m_dImaginPart));

} public ComplexNumber complexMulti(ComplexNumber c) { return (new ComplexNumber( this.m_dRealPart*c.m_dRealPart this.m_dImaginPart*c.m_dImaginPart, this.m_dRealPart*c.m_dImaginPart this.m_dImaginPart*c.m_dRealPart)); } }

public class TestComplexNumber {

- +

}

public static void main(String args[]) { ComplexNumber cn1 = new ComplexNumber(2,4); ComplexNumber cn2 = new ComplexNumber(1,-1); ComplexNumber cn3; cn3 = cn1.complexMulti(cn2); System.out.println(cn1.getRealPart()+\ System.out.println(cn2.getRealPart()+\ System.out.println(cn3.getRealPart()+\}

8. //类的定义

写一个名为Rectangle的类表示矩形。属性为宽width、高height和颜色color,widthhe height是double型的,color是String型的。假定所有矩形颜色相同,用一个类变量(类属性)表示颜色。要求提供访问器方法和计算矩形面积的findArea()方法。

写一个用户程序测试Rectangle类:创建两个Rectangle对象。给两个对象设置任意的宽和高。设矩形颜色为黄色。显示两个对象的属性并求面积。 答案:

public class Rectangle { private double width = 1; private double height = 1; private static String color = “white”; public Rectangle() { } public Rectangle(double width, double height , String color) { this.width = width; this.height = height; this.color = color; } public double getWidth() { return width; }

public void setWidth(double width) { this.width = width; }

public double getHeight() { return height; }

public void setHeight(double height) { this. height = height; }

public static String getColor() { return color; }

public static void setColor(String color) { this.color = color; }

public double findArea() { return width*height; }

public static void main(String[] args) { Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle();

r1.setWidth(2.0); r1.setHeight(3.0); r1.setColor(“红色”); r2.setWidth(5.0); r2.setHeight(4.0); r2.setColor(“黄色”);

System.out.println(r1.getWidth()); System.out.println(r1.getHeight());

}

}

System.out.println(r1.getColor()); System.out.println(r1.findArea()); System.out.println(r2.getWidth()); System.out.println(r2.getHeight()); System.out.println(r2.getColor()); System.out.println(r2.findArea());

9、//static修饰符

设计一个学生类,其中成员变量应包括:学号,姓名,性别,班级,并提供方法打印学生信息,和计算已经创建学生对象的数目。编写一个程序测试这个类。 public class Student {

private long id;

private String name; private char sex; private String banji;

private static int count=0;

public Student(long id,String name,char sex,String banji) {

this.id=id;

this.name=name; this.sex=sex;

this.banji=banji; count++; }

public void print() {

System.out.println(“该生信息如下:”);

System.out.println(“学号:”+id+” 姓名:”+name+” 性别:”+sex+” 班级:”+banji);

}

public static void count() {

System.out.println(“已经创建的学生个数为:”+count);

}

public static void main(String[] args) {

Student a=new Student(01,“zhangsan”,?男?,”计专01班”); a.print();

Student.count();

Student b=new Student(03,”刘倩”,?女?,”计专03班”); b.print();

Student.count();

} }

10、//static修饰符

编写一个类Teacher,描述教师的课时数量和计算课时的系数,均为double类型。Teacher类还有一个方法,courseCompute(),可计算教师的当量课时(课时量*系数),返回值类型为double,所有教师的课时系数相同。 编写一个测试类进行测试。

创建两个教师对象,计算的系数为1.2,输出计算后的两位老师的当量课时。 将系数修改后,输出修改后的当量课时。 public class Test{ public static void main(String[] args){ Teacher t1=new Teacher(96); Teacher t2=new Teacher(64); Teacher.classXishu=1.2; double realCourseNum=t1.courseCompute();

System.out.println(\第一位老师的实际课时数为:\realCourseNum=t2.courseCompute();

System.out.println(\第一位老师的实际课时数为:\Teacher.classXishu=1.5;

realCourseNum=t1.courseCompute();

System.out.println(\更改系数后,第一位老师的实际课时数为:\ realCourseNum=t2.courseCompute(); System.out.println(\更改系数后,第一位老师的实际课时数为:\ }

}

class Teacher{ public Teacher(int courseNum){ this.courseNum=courseNum; } int courseNum; static double classXishu; double courseCompute(){ return courseNum*classXishu; }

}

11、设计一个描述二维平面上点的类Position,该类需要描述点的横坐标和纵坐标,并提供访问器方法(set方法和get方法)和计算两点间距离的方法。写一个程序测试这个类 class Position { private int x = 0; private int y = 0; public Position() { } public Position (int x,int y) { this.x = x; this.y = y; } public void setX(int x) { this.x = x; } public int getX() { return x; } public void setY(int y) { this.y = y; } public int getY() {

}

class Test { public static void main(String[] args) { Position p1 = new Position (); Position p2 = new Position(3,5); System.out.println(p1.distance(p2)); } }

继承部分: 一、 选择题

1. Java语言的类间的继承关系是( )。

A) 多重的 B) 单重的 C) 线程的 D) 不能继承 2. 以下关于Java语言继承的说法正确的是( )。

A)Java中的类可以有多个直接父类 B)抽象类不能有子类

C)Java中的接口支持多继承 D)最终类可以作为其它类的父类 3. 现有两个类A、B,以下描述中表示B继承自A的是()。 A) class A extends B B) class B implements A C) class A implements B D) class B extends A 4. 下列选项中,用于定义接口的关键字是( )。

A)interface B) implements C) abstract D) class 5. 下列选项中,用于实现接口的关键字是( B)。

A)interface B) implements C) abstract D) class 二、 填空题

1. 如果子类中的某个变量的变量名与它的父类中的某个变量完全一样,则称子类中的这个变量________了父类的同名变量。

2. 属性的隐藏是指子类重新定义从父类继承来的__________。

3. 如果子类中的某个方法的名字、返回值类型和________与它的父类中的某个方法完全一样,则称子类中的这个方法覆盖了父类的同名方法。

return y; }

public double distance(Positon p) { int d1 = x - p.getX(); int d2 = y - p.getY(); return Math.sqrt(d1*d1+d2*d2); }

4. Java 仅支持类间的____重继承。

5. 抽象方法只有方法头,没有________。

6. Java 语言的接口是特殊的类,其中包含______常量和_______方法。 三、 程序阅读题

1.现有类说明如下,请回答问题: public class A{

String str1=\

String str2=\ are you? \

public String toString( ){ return str1+str2; } }

public class B extends A{

String str1=\,Bill.\

public String toString( ){ return super.str1+str1; } }

问题:

1)类A和类B是什么关系?

2)类A和类B都定义了str1属性和方法toString( ), 这种现象分别称为什么?若a是类A的对象,则a.toString( )的返回值是什么? 4)若b是类B的对象,则b.toString( )的返回值是什么? 2.现有一个类定义如下,请回答问题: class Employee{

String name; int age;

double wage; static int No=0;

Employee(String a1,int a2,double a3){

name=a1; age=a2; wage=a3; No++; } }

在使用该类时,已使用下面语句生成了该类的对象: Employee e1,e2; e1=new Employee(\王劲\ e2=new Employee(\张山\问题:

1)e2.name,e2.age,e2.wage的值各是什么?

2)生成对象e1、e2后,e1.No值为多少?能否通过类名做前缀引用属性No?

3.阅读程序,回答问题。 public class InheritTest1{

public static void main (String[] args){ A aa; B bb;

aa=new A( ); bb=new B( ); aa.show( ); bb.show(); } }

class A{

int a=1; double d=2.0;

void show( ){ System.out.println(\ } }

class B extends A{

3) float a=3.0f; String d=\ int b=4; void show( ) {

System.out.println(\ super.show( );

System.out.println(\ } }

问题:1)这是哪一类java程序? 2)类A和类B是什么关系?

3)按程序输出的格式写出程序运行后的结果.

4.现有类说明如下,请回答问题: class A{ int x=10;

int getA(){return x;} }

class B extends A{ int x=100;

int getB(){return x;} }

问题:1)类B是否能继承类A的属性x?

2)若b是类B的对象,则b.getB()的返回值是什么? 3)若b是类B的对象,则b.getA()的返回值是什么?

4)类A和类B都定义了x属性,这种现象称为什么?

5.有如下源程序,请回答问题: class A{ String s=\ } class B extends A{ String s=\ } public class TypeConvert{ public static void main(String args[]){ B b1,b2=new B(); A a1,a2; a1=(A)b2; a2=b2; System.out.println(a1.s); System.out.println(a2.s); b1=(B)a1; System.out.println(b1.s); System.out.println(b2.s); } }

问题: 该程序的四行输出各是什么? 6.现有类说明如下,请回答问题: public class A{

int x=888; String str=\

public String toString(){ return str+x; } }

public class B extends A{ String x=\

public String toString(){ return str+x+\ } }

问题:1)类A和类B是什么关系?

2)类A和类B都定义了x属性和方法toString(),这种现象分别称为什么? 3)若a是类A的对象,则a.toString( )的返回值是什么?

4)若b是类B的对象,则b.toString( )的返回值是什么?

7.运行类C的输出结果是什么? class A{ public A(){ System.out.println(“The default constructor of A is invoked”);

} }

class B extends A{ public B(){ } }

public class C{ public static void main(String[] args){ B b = new B(); } }

8.阅读下列程序写出输出结果: class A { String s=\

void show(){ System.out.println(s); } } class B extends A { String s=\

void show(){ System.out.println(s); }

} public class TypeConvert{ public static void main(String args[]){ B b1; B b2=new B(); A a1,a2; a1=(A)b2; a2=b2; System.out.println(a1.s); a1.show(); System.out.println(a2.s); a2.show(); b1=(B)a1; System.out.println(b1.s); b1.show(); System.out.println(b2.s); b2.show(); } }

四、编程题

1.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。

接口与内部类:

1. Java语言的类间的继承关系是( )。

A) 多重的 B) 单重的 C) 线程的 D) 不能继承 2. 以下关于Java语言继承的说法正确的是( )。

A) Java中的类可以有多个直接父类 B) 抽象类不能有子类

C) Java中的接口支持多继承 D) 最终类可以作为其他类的父类 3. 下列选项中,用于定义接口的关键字是( )。

A)interface B) implements C) abstract D) class 4. 下列选项中,用于实现接口的关键字是( )。

A)interface B) implements C) abstract D) class

5. 现有类A和接口B,以下描述中表示类A实现接口B的语句是( )。 A) class A implements B B) class B implements A C) class A extends B D) class B extends A 6. 下列选项中,定义接口MyInterface的语句正确的是 ( )。 A)interface MyInterface{ } B) implements MyInterface { } C) class MyInterface{ } D) implements interface My{ } 7. 接口中所有属性均为___ _____、___ ______和___ _____的。 8. Java 语言的接口中可以包含__ ____常量和___ ____方法。

9.一个类如果实现一个接口,那么它就必须实现接口中定义的所有方法,否则该类就必须定义成___ _____的。

10. 接口中所有方法均为________和___ _____的。

11. Java语言中,定义一个类A继承自父类B,并实现接口C的类头是____。 12.下面是定义一个接口A的程序,完成程序填空。 public _______A{

public static final double PI=Math.PI;

public ______ double area(double a, double b); }

13.下面是定义一个接口A的程序,完成程序填空。 public interface A{

public static ______ double PI=3.14159;

public abstract double area(double a, double b)_ ___ }

异常处理: 一、填空题

1.声明异常的关键字是______________ 2.抛出异常的关键字是______________ 3.不需要声明的异常是______________ 二、选择题

1.下列异常处理语句编写正确的是:( ) A)try{ System.out.println(2/0) ;} B)try( System.out.println(2/0))

catch(Exception e)

( System.out.println(e.getMessage()); ) C)try{ System.out.println(2/0) ;}

catch(Exception e)

{ System.out.println(e.getMessage()); }

D)try{ System.out.println(2/0) ;}

catch { System.out.println(e.getMessage()); } 三、程序阅读题

1.阅读下面程序回答问题: import java.io.*; public class Class1{ public static void main(String args[]){ int a=5; int b=0; System.out.println(a/b); try{ System.out.println(\ System.out.println(a/b); System.out.println(\ } catch(ArithmeticException e) { System.out.println(\除数为0,这是不行的!\ } finally { System.out.println(\被执行!\ } System.out.println(\异常已发生,但不影响程序的执行!\ }}

1)运行上述程序,输出结果是什么?(异常提示信息除外) 2)将变量b的初值改成5后,输出结果是什么? 2.阅读下面程序回答问题: import java.io.*; public class Class1{ public static void main(String args[]){ int a=5; int b=0; try{ System.out.println(\ System.out.println(a/b); System.out.println(\ } catch(ArithmeticException e) { System.out.println(\除数为0,这是不行的!\ } finally { System.out.println(\被执行!\ } System.out.println(\异常已发生,但不影响程序的执行!\ } }

1)运行上述程序,输出结果是什么?(异常提示信息除外) 2)将变量b的初值改成5后,输出结果是什么? GUI程序设计: 一、 选择题

1. FlowLayout的布局策略是( )

A)按添加的顺序由左至右将组件排列在容器中。 B)按设定的行数和列数以网格的形式排列组件。

C)将窗口划分成五部分,在这五个区域中添加组件。 D)组件相互叠加排列在容器中。

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

Top