AnjoyoJava04章节考试题
java程序设计第1-4章练习题参考答案 (1)

一、选择题1.下面供选字符序列中,不属于...Java语言关键字的是( A ) A.goto B.null C.false D.native2.以下是关于Java语言关键字的叙述,其中正确的是( C )A.关键字可以用来描述类的成员类型,但是不能描述普通变量B.关键字由字母(包括英文字母、下划线字符、美元字符、文字字符)和数字组成C.关键字不可以用作变量、方法、类或标号的标识符D.break和main都是Java语言的关键字3.下述字符组合不能作为Java程序标识符的是(D)A.Aa12 B.XC.y D.5x4.在下述字符串中,不属于...Java语言关键字的是( C )A.float B.newC.java D.return5.下列字符组合不能作为Java整型常量的是( A )A.078B.0x3ACBC.5000D.0x3ABC6.设有如下程序:public class Test {public static void main(String[] args) {String foo=args[1];String bar=args[2];String baz=args[3];System.out.println("baz= "+baz);}}下述哪一行命令可以使得执行该程序的输出为baz= 2 ( C )A) java Test 2222 B) java Test 1 2 3 4C) java Test 4 2 4 2 D) java Test 4 3 2 17.在Java语言中,int类型数据的长度是( C )A.1个字节B.2个字节C.4个字节D.8个字节8. 用Java虚拟机执行类名为Hello的应用程序的正确命令是( D )。
A) java Hello.class B) Hello.class C) java Hello.java D) java Hello9.执行以下代码,输出结果是( B )int x=5,y=7,u=9,v=6;System.out.println(x>y ? x+2:u>v ? u-3:v+2);A.8B.6C.7D.true10.下列修饰符中与访问控制无关的是(C)。
AnjoyoAndroid04章节考试题

AnjoyoAndroid04章节考试题anjoyoandroid第四章考试题一、多项选择题:(共20题,每题3分)1.下面关于adapter描述有误的一项是()。
a、 Android有以下四种类型的适配器对象供开发人员使用:arrayadapter、simpleadapter、simplecursoradar和custom adapter。
b、android使用了一个抽象类――baseadapter作为各个adapter实体类的基类,并使用两个接口――listadapter和spinneradapter分别作为两种类型的adapterview――abslistview(包含listview和gridview)和absspinner(包含spinner和gallery)的适配接口。
c、 Android系统提供了几个默认的适配器类供开发者使用。
同时,开发人员还可以继承适配器类来定义适配器。
d、适配器对象充当适配器控件和数据源之间的桥梁。
它提供了一个访问数据源的条目,并将从数据源获得的数据逐项加载到适配器控件中。
2.以下关于适配器继承关系的描述错误之一是()。
a、自定义adapter继承自adapter。
b、arrayadapter继承自baseadapter。
c、simpleadapter继承自baseadapter。
d、cursoradapter继承自baseadapter。
3.以下对listview功能的描述不正确()。
a、采用mvc模式将前端显示和后端数据分离。
b、 Listview相当于MVC模式下的V(视图)。
c、listadapter对象相当于mvc模式中的c(控制器control)。
d、为listview提供数据的列表或数组相当于MVC模式下的m(数据模型)。
4.关于listview的以下XML属性描述错误之一是()。
a、在xml布局代码中将listview的位置设为占满整个activity,可以将listview 填充满整个activity。
Java程序设计 第4章习题参考答案[2页]
![Java程序设计 第4章习题参考答案[2页]](https://img.taocdn.com/s3/m/337805007dd184254b35eefdc8d376eeaeaa172c.png)
第4章习题参考答案一、简答题1.Java提供了哪些数据类型,全部写出来。
整型:byte,short,int,long数值型浮点型:float,double基本数据类型字符型:char数据类型布尔型:boolean类:class复合数据类型字符串:String接口:interface2.如何进行数据类型的转换?有2种方式:自动类型转换:运算时,不同类型的数据先转换成同一类型后再参与运算,转换的原则是位数少的类型转换成位数多的类型,由系统自动处理。
强制类型转换:当位数多的类型向位数少的类型进行转换时,需要用户明确说明要转换的类型,需要用户在程序中指明。
3.类的修饰符有哪些?有什么区别?类的修饰符包括访问控制修饰符、静态修饰符static、最终说明符final。
访问控制修饰符包括4种类型:privat、default、protected、public。
用static声明的成员变量被视为类的成员变量,而不能当成实例对象的成员变量,也就是说,静态变量是类固有的,可以被直接引用,而其它成员变量声明后,只有生成对象时才能被引用。
4.public的类和abstract的类有什么不一样?public的类是公共类,可以被继续,可以实例化,可以被其他包访问。
abstract的类是抽象类,其中一定含有抽象方法,abstract class的子类在继承它时,对非抽象方法既可以直接继承,也可以覆盖;而对抽象方法,可以选择实现,也可以通过再次声明其方法为抽象的方式,无需实现,留给其子类来实现,但此类必须也声明为抽象类。
5.什么是最终类?如何声明?最终类不能被继承,也没有子类。
final类中的方法默认是final的。
final方法不能被子类的方法覆盖,但可以被继承。
final成员变量表示常量,只能被赋值一次,赋值后值不再改变。
声明方法:final class ClassName。
二、操作题1.创建一个学生类Student,包括学号no、姓名name、年龄age、性别sex四个属性以及学习study、实践practice两个方法。
java认证 习题 第04章 有答案版 OK 该试题还有第03、05章

1、what is the result when you compile and run the following code? C Class Example{Public static void main(String []args){System.out.println(3/0);}}Select all right answer:A a compiler errorB a runtime errorC ng.ArtithmeticException :/by zeroD infinity考察算术运算,分母为0 ,非法2、which results is true when you compile and run the following code?Bboolean b1=true; Boolean b2=falseSelect all right answer:A 、b1==b2 B、b1||b2 C、b1|&b2 D、b1&&b2考察逻辑运算,A返回false, C语法错误运算符没有连接操作,D返回false3、what is the result when you compile and run the following code? C Class Example{Public static void main(String []args){int i=1;int j;j=i++;System.out.print(i+”,”+j);}}Select all right answer:A、1,1 B。
1,2 C、2,1 D、2,2运算过程是:先把i赋给j,此时i=1,y=1,然后i自动加1,并把结果赋给I, 则i变为24、what is the result when you compile and run the following code? A Class Example{Public static void main(String []args){int x=5;boolean y=true;System.out.print(x<y);}}Select all right answer:A、a compiler errorB、a running errorC、trueD、false考察关系运算,不能用0替代false5、what is the result when you compile and run the following code? A Class Example{Public static void main(String []args){Short s=new Short(“5”);Boolean b;// insert code here}}Which,inserted independently at //insert code here?,will compile ? B D Select all right answer:A b=(Number instanceof s);B b=(s instanceof Short);C b=s instanceof (Short);D b=(s instanceof Number);E b= s instanceof (Object);F b=(s instanceof String);考察instanceof运算符,A写反了,C.E语法错误F类型不兼容6、which of the following lines of code will compile without error? B CAint i=0;if (i) System.out.println(“Hi”);Bboolean b=true;boolean b2=true;if (b==b2) System.out.println(“so true”);Cint i=1;int j=2;if(i==1 || j==2)System.out.println(“OK”);Dint i=1;int j=2;if(i==1 &|j==2)System.out.println(“OK”);本题考察boolean表达式,A错在不能用数字充当boolean型值,D错在逻辑操作非法7、what is the result when you compile and run the following code? B D Class Example{Public static void main(String []args){Float f=new Float(4.2f);Float c;Double d=new Double(4.2);float f1=4.2f;c=f;}}Select all right answer:A f.equals(d)B c==fC c==dD c.equals(f)==比较内存地址,equals比较内容8、what is the result when you compile and run the following code? D Class Example{Public static void main(String []args){byte x=--64;byte y=--6;System.out.println(x/y+””+x%y);}}Select all right answer:A a compiler errorB a runtime errorC 10 4D 10 -49、what is the result when you compile and run the following code? G Class Example{public static void main(String []args){long x=42L;long x=44L;System.out.println(““+7+2+”“);System.out.println(foo()+x+5+”“);System.out.println(x+y+foo());}static String foo(){ return “foo”;}}Select all right answer:A 、9 foo 47 86 foo B、9 foo 47 4244foo C、9 foo 425 86 fooD、9 foo 425 4244 fooE、72 foo 47 86 fooF、72 foo 47 4244 fooG、72foo 425 86 foo H、72 foo 425 4244 foo I、compilation fails本题考察字符串连接,,两个操作数中有一个以上字符串就进行连接,俩操作数都是数值时做+运算10、what is the result when you compile and run the following code? A Class Example{public static void main(String []args){String s1=”Amit”;String s2=”Amit”;String s3=new String(“abcd”);String s4=new String(“abcd”);System.out.println(s1.equals(s2));System.out.println((s1==s2));System.out.println(s3.equals(s4));String s3=new String((s3==s4));}}Select all right answer:A true true true falseB true true true trueC true false true falseS1和S2内容一样,引用也一样,因为不是new的就都是从字符串池中取的,s3,s4内容一样,但不是指同一对象11、what is the result when you compile and run the following code? Bchar c=’c’;int i=10;double d=10;long l=1;String s=”Hello”Select all right answer:A c=c+iB s+=iC i+=sD c+s+=符号左侧应是字符串12、which of the following returns true when replace with *****? A Class Example{public static void main(String []args){Button b=new Button(“Button”);if(*******)System.out.println(“this is an instance of Button”);}}Select all right answer:A 、b instanceof Button B、Button instancof b C、b==Button D、Button==(Object)b 考察比较运算符,13、what is the result when you compile and run the following code? A import java.awt.*;Class Example{public static void main(String []args){try{Button[] var=new Button[5];System.out.println(var instanceof []);}catch(Exception e){System.out.println(“Exception thrown”);}}}Select all right answer:A a compiler errorB Exception thrownC trueD flaseE none of the above Instanceof右侧应该是类,接口或数组类型14、what is the result when you compile and run the following code? D 132import java.awt.*;Class Example{public static void main(String []args){int Output=10;boolean b1=false;if((b1==true)&&(Output+=10)==20)){System.out.println(“we are equal ”+Output);}elseSystem.out.println(“Not equal ”+Output);}}Select all right answer:A compile error ,attempting to perform binary comparison on logical data typeB Compilation and output of “we are equal 10”C compilation and output of “not equal 20”D compilation and output of “not equal 10”考察逻辑短路因为逻辑短路,所以(Output+=1没被执行15、what is the result when you compile and run the following code? F 145Class Example{ String s;public static void main(String []args){ Example ks=new Example();int j,i;i=ks.hai();j=ks.hello();System.out.println(i+””+j);}int hai(){if((s==null)||(s.length()==0)) {return 10;}else return 0;}int hello(){if((s==null)||(s.length()==20)) {return 10;}else return 0;}}Select all right answer:A 10 10B 10 nullC 0 0D compilation errorE An exception in haiF an excepton in hello考察逻辑短路,|不属于短路运算,所以s.lengh()会被执行,因为s为null,所以抛出异常课后作业:1. Given:class Hexy {public static void main(String[] args) {Integer i = 42;String s = (i<40)?"life":(i>50)?"universe":"everything";System.out.println(s);}}What is the result?A. nullB. lifeC. universeD. everythingE. Compilation fails.F. An exception is thrown at runtime.Answer:® 3 D is correct. This is a ternary nested in a ternary with a little unboxing thrown in.Both of the ternary expressions are false.®˚A, B, C, E, and F are incorrect based on the above.(Objective 7.6)2. Given:1. class Example {2. public static void main(String[] args) {3. Short s = 15;4. Boolean b;5. // insert code here6. }7. }Which, inserted independently at line 5, will compile? (Choose all that apply.)A. b = (Number instanceof s);B. b = (s instanceof Short);Self Test Answers 307C. b = s.instanceof(Short);D. b = (s instanceof Number);E. b = s.instanceof(Object);F. b = (s instanceof String);Answer:® 3 B and D correctly use boxing and instanceof together.®˚A is incorrect because the operands are reversed. C and E use incorrect instanceof syntax.F is wrong because Short isn't in the same inheritance tree as String. (Objective 7.6)3. Given:1. class Comp2 {2. public static void main(String[] args) {3. float f1 = 2.3f;4. float[][] f2 = {{42.0f}, {1.7f, 2.3f}, {2.6f, 2.7f}};5. float[] f3 = {2.7f};6. Long x = 42L;7. // insert code here8. System.out.println("true");9. }10. }And the following five code fragments:F1. if(f1 == f2)F2. if(f1 == f2[2][1])F3. if(x == f2[0][0])F4. if(f1 == f2[1,1])F5. if(f3 == f2[2])What is true?A. One of them will compile, only one will be true.B. Two of them will compile, only one will be true.C. Two of them will compile, two will be true.D. Three of them will compile, only one will be true.E. Three of them will compile, exactly two will be true.F. Three of them will compile, exactly three will be true.308 Chapter 4: OperatorsAnswer:® 3 D is correct. Fragments F2, F3, and F5 will compile, and only F3 is true.®˚A, B, C, E, and F are incorrect. F1 is incorrect because you can’t compare a primitive toan array. F4 is incorrect syntax to access an element of a two-dimensional array.(Objective 7.6)4. Given:class Fork {public static void main(String[] args) {if(args.length == 1 | args[1].equals("test")) {System.out.println("test case");} else {System.out.println("production " + args[0]);}}}And the command-line invocation:java Fork live2What is the result?A. test caseB. productionC. test case live2D. Compilation fails.E. An exception is thrown at runtime.Answer:®3 E is correct. Because the short circuit (||) is not used, both operands are evaluated. Sinceargs[1] is past the args array bounds, an ArrayIndexOutOfBoundsException is thrown.®˚A, B, C, and D are incorrect based on the above.(Objective 7.6)Self Test Answers 3095. Given:class Foozit {public static void main(String[] args) {Integer x = 0;Integer y = 0;for(Short z = 0; z < 5; z++)if((++x > 2) || (++y > 2))x++;System.out.println(x + " " + y);}}What is the result?A. 5 1B. 5 2C. 5 3D. 8 1E. 8 2F. 8 3G. 10 2H. 10 3Answer:® 3 E is correct. The first two times the if test runs, both x and y are incremented once (thex++ is not reached until the third iteration). Starting with the third iteration of the loop,y is never touched again, because of the short-circuit operator.®˚A, B, C, D, F, G, and H are incorrect based on the above.(Objective 7.6)6. Given:class Titanic {public static void main(String[] args) {Boolean b1 = true;boolean b2 = false;boolean b3 = true;if((b1 & b2) | (b2 & b3) & b3)System.out.print("alpha ");if((b1 = false) | (b1 & b3) | (b1 | b2))System.out.print("beta ");}}310 Chapter 4: OperatorsWhat is the result?A. betaB. alphaC. alpha betaD. Compilation fails.E. No output is produced.F. An exception is thrown at runtime.Answer:® 3 E is correct. In the second if test, the leftmost expression is an assignment, nota comparison. Once b1 has been set to false, the remaining tests are all false.®˚A, B, C, D, and F are incorrect based on the above.(Objective 7.6 )7. Given:class Feline {public static void main(String[] args) {Long x = 42L;Long y = 44L;System.out.print(" " + 7 + 2 + " ");System.out.print(foo() + x + 5 + " ");System.out.println(x + y + foo());}static String foo() { return "foo"; }}What is the result?A. 9 foo47 86fooB. 9 foo47 4244fooC. 9 foo425 86fooD. 9 foo425 4244fooE. 72 foo47 86fooF. 72 foo47 4244fooG. 72 foo425 86fooH. 72 foo425 4244fooI. Compilation fails.Self Test Answers 311Answer:® 3 G is correct. Concatenation runs from left to right, and if either operand is a String,the operands are concatenated. If both operands are numbers they are added together.Unboxing works in conjunction with concatenation.®˚A, B, C, D, E, F, H, and I are incorrect based on the above. (Objective 7.6)8. Place the fragments into the code to produce the output 33. Note, you must use each fragmentexactly once.CODE:class Incr {public static void main(String[] args) {Integer x = 7;int y = 2;x ___ ___;___ ___ ___;___ ___ ___;___ ___ ___;System.out.println(x);}}FRAGMENTS:y y y yy x x-= *= *= *=Answer:class Incr {public static void main(String[] args) {Integer x = 7;int y = 2;312 Chapter 4: Operatorsx *= x;y *= y;y *= y;x -= y;System.out.println(x);}}Yeah, we know it’s kind of puzzle-y, but you might encounter something like it on the real exam.(Objective 7.6)9. Given:1. class Maybe {2. public static void main(String[] args) {3. boolean b1 = true;4. boolean b2 = false;5. System.out.print(!false ^ false);6. System.out.print(" " + (!b1 & (b2 = true)));7. System.out.println(" " + (b2 ^ b1));8. }9. }Which are true?A. Line 5 produces true.B. Line 5 produces false.C. Line 6 produces true.D. Line 6 produces false.E. Line 7 produces true.F. Line 7 produces false.Answer:®3 A , D, and F is correct. The ^ (xor) returns true if exactly one operand is true. The! inverts the operand’s boolean value. On line 6 b2 = true is an assignment not acomparison, and it’s evaluated because & does not short-circuit it. ®˚B, C, and E are incorrect based on the above.(Objective 7.6)Self Test Answers 31310. Given:class Sixties {public static void main(String[] args) {int x = 5;int y = 7;System.out.print(((y * 2) % x));System.out.print(" " + (y % x));}}What is the result?A. 1 1B. 1 2C. 2 1D. 2 2E. 4 1F. 4 2G. Compilation fails.H. An exception is thrown at runtime.Answer:® 3 F is correct. The % (remainder a.k.a. modulus) operator returns the remainder of adivision operation.®˚A, B, C, D, E, G, and H are incorrect based on the above.。
第四章 Java章节习题

1.分别编写两个方法,一个方法的功能是求一个double型数值的向右取整,另一个方法求double型数值的向左取整。
数d的向右取整是大于等于d的最小整数,d的向左取整是小于等于d的最大整数。
例如:5.3的向右取整是6而向左取整是5.答:public class One{static int right(double x){if(x!=5)x=x+1;return (int)x;}static int left(double x){return (int)x;}public static void main(String args[]){System.out.println("5.3向左取整为:"+left(5.3));System.out.println("5.3向右取整为:"+right(5.3));}}程序运行结果为:2.写一个方法计算一个整数各位数字的和。
使用下述的方法说明:public static int sumDigits(long n)例如:sumDigits(276)返回2+7+6=15 。
提示:用求余运算符%分解数字,用除号/分离位数。
例如334%10=4而334/10=33.用循环反复分解和分离每位数字,直到所有的位数都被分解。
答:public class Two{public static int sumDigits(long n){long sum=0,t;while(n!=0)47{t=n%10;n=n/10;sum+=t;}return (int)sum;}public static void main(String args[]){System.out.println("334各个位数字之和为:"+sumDigits(334));}}程序运行结果为:3. 编写程序,用Math类中的sqrt方法打印下表:Number SquareRoot ....................................0 0.00002 1.41424 2.00006 2.44958 2.828410 3.162312 3.464114 3.741716 4.000018 4.242620 4.4721答:public class Three{public static void main(String args[]){System.out.println("Number SquareRoot");System.out.println("..................");for(int i=0;i<=20;)4748{System.out.printf("%d\t%.4f\n",i,Math.sqrt(i));i=i+2;}}}程序运行结果为:4. 请定义一个名为Rectangle的矩形类。
java试卷(第四章)

java试卷(第四章)(⼀)选择题1.假设⽅法没有任何返回值,下⾯哪个关键字可以⽤作此函数的返回类型?()A.v oidB.i ntC.d oubleD.p ublic2.⽅法包含哪些特征?()A.⽅法名B.⽅法名和参数列表C.返回类型、⽅法名和参数列表D.参数列表3.所⽤的java应⽤程序都要有⼀个()⽅法?A.p ublic static Main(String[] args)B.p ublic static Main(String args[])C.p ublic static void main(String[] args)D.p ublic void main(String[] args)4.⽅法的参数总是出现在()A.⼤括号B.圆括号C.花括号D.引号5.每当调⽤⼀个⽅法时,系统将参数、局部变量和系统寄存器存储在⼀个内存区域中,这个内存区域称为()A.堆B.存储器C.栈D.数组6.在调⽤参数⽅法时,实参的值赋给形参,这被称为()A.⽅法调⽤B.值传递C.参数传递D.参数名传递7.下⾯⽅法打印message:System.out.print(message);n--;}}那么nPrint(”a”,4)会输出什么()A.a aaaaB.a aaaC.a aD.调⽤⽆效8.下⾯⽅法打印messagestatic void nPrint(String message, int n) { while (n > 0) {System.out.print(message);n--;}}如果int k = 2;nPrint(“A message”,k);程序运⾏后k的结果是()A.0B.1C.2D.39.分析下⾯代码public class Test {public static void main(String[] args) { System.out.println(xMethod(5, 500L));}public static int xMethod(int n, long l) { System.out.println("int, long");return n;}public static long xMethod(long n, long l) { System.out.println("long, long");}运⾏结果是()A.程序显⽰的是int,long下⼀⾏5B.程序显⽰的是long,long下⼀⾏5C.程序正常运⾏,但是运⾏结果不是A和B中的结果D.程序不能正常运⾏,因为没有匹配的参数列表10.分析下⾯代码:class Test {public static void main(String[] args) {System.out.println(xmethod(5));}public static int xmethod(int n, long t) {System.out.println("int");return n;}public static long xmethod(long n) {System.out.println("long");return n;}}运⾏结果是()A.i nt 下⼀⾏5B.l ong下⼀⾏5C.程序正常运⾏,但是运⾏结果不是A和B中的结果D.程序不能正常运⾏,因为没有匹配的参数列表11.分析下⾯代码:public class Test {public static void main(String[] args) {System.out.println(max(1, 2));}public static double max(int num1, double num2) {System.out.println("max(int, double) is invoked");if (num1 > num2)return num1;elsepublic static double max(double num1, int num2) {System.out.println("max(double, int) is invoked");if (num1 > num2)return num1;elsereturn num2;}}运⾏结果是:()A.程序不能编译,因为不能在返回类型为⾮void的⽅法中有输出语句。
AnjoyoJava04章节考试题

第四章考试题一、选择题:(每题3分,共20题)1.下列关于数组的描述不正确的是( D )。
A、数组是用来存储一组相同数据类型数据的。
B、数组是编程语言中非常常见的一种数据结构。
C、数组可以通过整型索引访问数组中的每一个值。
D、在一个数组里面,既允许用于存储一种数据类型的数据,也允许存储多种数据类型数据。
2.下列关于数组定义错误的是()。
A、type[] var_name;B、int[10] a;C、String student[];D、boolean[] booleanArray;3.下列关于数组的说法中不正确的是(A )。
A、定义数组时必须分配内存。
B、数组的初始化操作分为静态初始化和动态初始化。
C、数组元素可以是基本数据类型、对象或其他数组。
D、数组是最简单的复合数据类型,是一系列数据的集合。
4.设有下列数组定义语句:int a[] = {1, 2, 3};则对此语句的叙述错误的是()。
A、定义了一个名为a的一维数组。
B、a数组有3个元素。
C、a数组元素的下标为1~3。
D、数组中每个元素的类型都是整数。
5.给出下面程序代码:byte[] a1, a2[];//此处的a2[]是二维数组byte a3[][];byte[][] a4;下列数组操作语句中哪一个是不正确的?()A、a2 = a1B、a2 = a3C、a2 = a4D、a3 = a46.执行语句:int[] x = new int[20];后,下面说法正确的是()。
A、x[19]为空B、x[19]未定义C、x[19]为0D、x[0]为空7.已知有定义:String s="I love",下面正确的表达式是()。
A、s += "you";B、char c = s[1];C、int len = s.length;D、String s = s.toLowerCase();8.下面代码运行后的输出结果为()。
Java基础第4章编程题答案

第四章编程题1. 按要求完成以下题目(1)Circle( ) // 将半径设为0Circle(double r ) // 创建 Circle 对象时将半径初始化为r③三个成员方法double getArea( ) //获取圆的面积double getPerimeter( ) // 获取圆的周长void show( ) //将圆的半径、周长、面积输出到屏幕(2)编写一个圆柱体类Cylinder ,它继承于上面的Circle 类。
还拥有:①一个成员变量double hight(私有,浮点型);// 圆柱体的高;②构造方法Cylinder (double r, double h )//创建Circle对象时将半径初始化为r③成员方法double getVolume( ) // 获取圆柱体的体积void showVolume( ) // 将圆柱体的体积输出到屏幕编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。
【参考答案】import java.math.*;class Circle{private double radius;public Circle() {radius=0;}public Circle(double r) {radius=r;}void setRadius(double r) {this.radius=r;}double getRadius(){return this.radius;}double getArea() {return Math.PI*radius*radius;}double getPerimeter() {return Math.PI*2*radius;}void show() {System.out.println("圆的半径为"+radius+"\n圆的周长为"+this.getPerimeter()+"\n圆的面积为"+this.getArea());}}class Cylinder extends Circle{double hight;public Cylinder(double r,double h) {super.setRadius(r);this.hight=h;}double getVolume(){double volume=super.getArea()*hight;return volume;}void showVolume() {System.out.println("圆柱的面积为"+this.getVolume());}}public class Example01 {public static void main(String[] args) {Circle circle=new Circle(5);circle.show();Cylinder cylinder=new Cylinder(2,5);cylinder.showVolume();}}2.按要求编写一个Java应用程序:(l)定义一个接口CanCry,描述会吼叫的方法public void cry()(2)分别定义狗类(Dog)和猫类(Cat),实现CanCry接口。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第四章考试题一、选择题:(每题3分,共20题)1.下列关于数组的描述不正确的是()。
A、数组是用来存储一组相同数据类型数据的。
B、数组是编程语言中非常常见的一种数据结构。
C、数组可以通过整型索引访问数组中的每一个值。
D、在一个数组里面,既允许用于存储一种数据类型的数据,也允许存储多种数据类型数据。
2.下列关于数组定义错误的是()。
A、type[] var_name;B、int[10] a;C、String student[];D、boolean[] booleanArray;3.下列关于数组的说法中不正确的是()。
A、定义数组时必须分配内存。
B、数组的初始化操作分为静态初始化和动态初始化。
C、数组元素可以是基本数据类型、对象或其他数组。
D、数组是最简单的复合数据类型,是一系列数据的集合。
4.设有下列数组定义语句:int a[] = {1, 2, 3};则对此语句的叙述错误的是()。
A、定义了一个名为a的一维数组。
B、a数组有3个元素。
C、a数组元素的下标为1~3。
D、数组中每个元素的类型都是整数。
5.给出下面程序代码:byte[] a1, a2[];byte a3[][];byte[][] a4;下列数组操作语句中哪一个是不正确的?()A、a2 = a1B、a2 = a3C、a2 = a4D、a3 = a46.执行语句:int[] x = new int[20];后,下面说法正确的是()。
A、x[19]为空B、x[19]未定义C、x[19]为0D、x[0]为空7.已知有定义:String s="I love",下面正确的表达式是()。
A、s += "you";B、char c = s[1];C、int len = s.length;D、String s = s.toLowerCase();8.下面代码运行后的输出结果为()。
public class Test04_01 {public static void main(String[] args) {AB aa = new AB();AB bb;bb = aa;System.out.println(bb.equals(aa));}}class AB{ int x = 100; }A、trueB、falseC、编译错误D、1009.下列说法错误的是()。
A、数组是一种复合数据类型。
B、数组是有限元素的有序集合。
C、在Java中,数组是作为常量来处理的。
D、数组中的元素具有相同的数据类型,并可用统一的数组名和下标来唯一确定其元素。
10.下列运行结果正确的是()。
public class Test04_02 {public static void main(String[] args) {int a[] = { 12, 39, 26, 41, 55, 63, 72, 40, 83, 95 };int i1 = 0, i2 = 0;for (int i = 0; i < a.length; i++)if (a[i] % 2 == 1)i1++;elsei2++;System.out.println(i1 + "," + i2);}}A、5,4B、5,5C、6,5D、6,411.在Java中,下面哪一个是不合法的声明()。
A、short x[]B、short[] yC、short[5] x2D、short z2[5]12.语句int[] a = new int[100]的含义是()。
A、数组 a 有100 个自然数B、数组a 有100 个整数C、数组a 中的最大数是100D、数组a 的下标自100 开始计数13.下列关于Java 的数据结构类型描述说法正确的是()。
A、有多维数组B、不可实现多维数组C、只有一个维数组,不可实现多维数组D、可用“一维数组的数组”实现多维数组14.数组用来存储一组的数据结构是()。
A、对象B、相同类型数据C、不同类型数据D、整数类型数据15.下面这段代码的正确输出结果是()。
public class Test04_03 {public static void main(String[] args) {int[] beforeArray = { 1, 2, 3, 4, 5, 6, 7 };int[] afterArray;afterArray = beforeArray;afterArray[3] = 3;System.out.println("After Modifying:");for (int i = 0; i < beforeArray.length; i++) {System.out.print("a[" + i + "]=" + beforeArray[i] + ",");}}}A、a[0]=1,a[1]=2,a[2]=3,a[3]=3,a[4]=5,a[5]=6,a[6]=7,B、a[0]=1,a[1]=2,a[2]=3,a[3]=4,a[4]=5,a[5]=6,a[6]=7,C、a[0]=1,a[1]=2,a[2]=4,a[3]=3,a[4]=5,a[5]=6,a[6]=7,D、a[0]=1,a[1]=2,a[2]=4,a[3]=4,a[4]=5,a[5]=6,a[6]=7,16.下面这段代码的正确输出结果是()。
public class Test04_04 {public static void main(String[] args) {int[] beforeArray = {1,2,3,4,5,6,7,8};int[] afterArray = new int[8];System.arraycopy(beforeArray, 2, afterArray, 3, 5);System.out.println("After Copying:");for (int i = 0; i < afterArray.length; i++) {System.out.print("b[" + i + "]=" + afterArray[i] + ",");}}}A、b[0]=3,b[1]=4,b[2]=5,b[3]=6,b[4]=7,b[5]=0,b[6]=0,b[7]=0,B、b[0]=0,b[1]=0,b[2]=3,b[3]=4,b[4]=5,b[5]=6,b[6]=7,b[7]=0,C、b[0]=0,b[1]=0,b[2]=3,b[3]=4,b[4]=5,b[5]=0,b[6]=0,b[7]=0,D、b[0]=0,b[1]=0,b[2]=0,b[3]=3,b[4]=4,b[5]=5,b[6]=6,b[7]=7,17.下面这段代码的正确输出结果是()。
public class Test04_05 {public static void main(String[] args) {String tomClass = "anjoyo";String leeClass = "Anjoyo";boolean result = false;if (tomClass.toLowerCase().equals(leeClass.toLowerCase())) { result = true;System.out.println(result);} else {result = false;System.out.println(result);}}}A、falseB、trueC、nullD、018.下面这段代码的正确输出结果是()。
public class Test04_06 {public static void main(String[] args) {String name = "安卓越科技";String address = "北京市海淀区";String strName = "我是".concat(name);String strAddress = "住在".concat(address);String newStr = strName.concat(strAddress);System.out.println(newStr);}}A、我是安卓越科技B、我是住在北京市海淀区C、安卓越科技住在北京市海淀区D、我是安卓越科技住在北京市海淀区19.下面这段代码的正确输出结果是()。
public class Test04_07 {public static void main(String[] args) {String fileName, email;fileName = "TestAnjoyo.java";int index1 = stIndexOf(".");if (index1 != -1) {String after = fileName.substring(index1);if (".java".equals(after)) {email = "anjoyo.student@";int index2 = stIndexOf("@");int index3 = stIndexOf(".");if (index2 != -1 && index3 != -1 && index2 < index3&& index3 - index2 != 1) {System.out.println("True");} else {System.out.println("False");}}} else {System.out.println("once more");}}}A、TrueB、FalseC、once moreD、程序出错20.下面这段代码的正确输出结果是()。
public class Test04_08 {public static void main(String[] args) {String str = "Hello";int index = str.indexOf("l");String firstStr = str.replace('l', 'f');String secondStr = str.replaceFirst("ll", "f");System.out.println(index + "," + firstStr + "," + secondStr);}}A、3,Heffo,HefoB、3,Hefo,HeffoC、2,Heffo,HeffoD、2,Heffo,Hefo二、简答题:(每题8分,共5题)1.请简述冒泡排序算法的运作。