Java基础练习题附答案.docx

上传人:rrsccc 文档编号:9357872 上传时间:2021-02-21 格式:DOCX 页数:44 大小:104.27KB
返回 下载 相关 举报
Java基础练习题附答案.docx_第1页
第1页 / 共44页
Java基础练习题附答案.docx_第2页
第2页 / 共44页
Java基础练习题附答案.docx_第3页
第3页 / 共44页
Java基础练习题附答案.docx_第4页
第4页 / 共44页
Java基础练习题附答案.docx_第5页
第5页 / 共44页
点击查看更多>>
资源描述

《Java基础练习题附答案.docx》由会员分享,可在线阅读,更多相关《Java基础练习题附答案.docx(44页珍藏版)》请在三一文库上搜索。

1、Java 基础练习题附答案一、简单 Java 程序调试1)以下哪个是 Java 应用程序 main 方法的有效定义 ?A. public static void main();B. public static void main( String args );C. public static void main( String args );D. public static void main( Graphics g );E. public static boolean main( String a );2) 编译和运行以下代码的结果为:public class MyMainpublic st

2、atic void main(String argv)System.out.println(Hello cruel world);A编译错误;B运行输出Hello cruel world ;C编译无错,但运行时指示没有定义构造方法。D编译无错,但运行时指示没有正确定义main 方法。3) 下列选项中不属于Java虚拟机的执行特点的一项是:A异常处理B多线程C动态链接D简单易学4) 不属于 Java 语言特点的一项是:A分布式 B. 安全性 C. 编译执行5) 以下程序的运行结果为:D.面向对象public class Testpublic static void main(String arg

3、v )System.out.println(x=+5);A. 5B. x=5C. x=+5D. x=56) 以下程序的运行结果为: public class Testpublic static void main(String argv )System.out.println(good+morning);A. goodmorningC. good morningB. good+morningD. good+morning二、 Java符号与表达式1) 现有一个 int 类型的整数和一个 double 类型的数进行加法运算,则得到的结果类型为:Aint 类型B. double 类型C. floa

4、t 类型D. long 类型2)下面程序段的输出结果是:inta = 2;System.out.print( a+);System.out.print( a);System.out.print(+a);A333B334C 234D2333) 以下代码的输出结果 ? public class Testint x=3;public static void main(String argv)int x= 012;System.out.println(x);A 12B012C 10D 34) 下列定义语句正确的是:A char c=/n;B int i=12;Cfloat f=2.8;Dboolean

5、 b=null;5)检查如下代码:public class Quiz2_lpublic static void main(String args)int a = 8;int b = -8;System.out.print(a1) );下列哪条语句正确描述了程序在编译和运行时的行为?A编译成功,输出为“32,-4 ”B. 编译成功,输出为“ 16,4 ”C. 编译成功,输出为“ 32,2147483644”D. 编译成功,输出为“ 16,2147483644”E. 编泽器拒绝表达式b,因为不能对一个负数应用 操作符6)以下代码中变量result 的可能类型有哪些 ?byteb = 11;shor

6、ts = 13;result = b * +s;A byte, short, int, long, float, doubleBboolean, byte, short, char, int, long, float, double C byte, short, char, int, long, float, doubleD byte, short, charEint, long, float, double7)以下代码的输出结果为:System.out.println( +2 + 3);System.out.println(2 + 3);System.out.println(2 + 3 +)

7、;System.out.println(2 + +3);A第 3 行出现编译错误B输出 23, 5, 5 和 23.C输出 5, 5, 5 和 23.D输出23, 5, 23 和 23.8)设有如下变量说明:byte myByte;int myInt;long myLong;char myChar;float myFloat;double myDouble;以下赋值语句哪个需要强制转换。A myInt = myByte;BmyInt = myLong;C myByte = 3;D myInt = myChar;EmyFloat = myDouble;FmyFloat = 3;G myDoubl

8、e = 3.0;9)考虑如下两语句:1. boolean passingScore = false & grade = 70;2. boolean passingScore = false & grade = 70;表达式 grade = 70 在什么地方被计算A在 1 和 2 中均计算B在 1 和 2 中均未计算C在 1 中计算,在 2 中未计算D在 2 中计算,在 1 中未计算E非法,因为false 应写 FALSE10)设有一个整数 x 其二进制值为 10011100 (用 1 字节表示 ),则执行如下语句后 z 的二进制值为:int y = 1 c?a:c;d=d2b? d:b;Sys

9、tem.out.println(b);A 2B 3C5D 7三、分支程序设计1)下列语句片段的结果为int result;int a=17,b=6;result=(a%b4)? a%b:a/b ;System.out.println(result);A. 0B. 1C. 2D. 52)以下程序的运行结果为:1. public class Conditional 2.public static void main(String args )3. int x = 4;4. System.out.println( value is +(x 4) ? 99.99 : 9);5. 6. A. 输出: v

10、alue is 99.99B. 输出 : value is 9C. 输出 : value is 9.0D. 在第 4 行出现编译错误3)以下代码段的输出结果为1. int x = 0, y = 4, z = 5;2. if (x 2)3. if (y 5)11. System.out.println(message three);12. 13. else 14. System.out.println( message four);15. A. message oneB. message twoC. message threeD. message four4) 以下程序的输出结果为:public

11、 class test public static void main(String args) int x=1,y=1,z=1;if(x-=1&y+=1|z+=1)System.out.println(x=+x+,y=+y+,z=+z);A x=0,y=2,z=1B x=1,y=2,z=1C x=0,y=1,z=1D x=0,y=2,z=25) 编译和运行以下代码结果为:1. public class EqualsTest2. public static void main(String args)3. byte A=(byte)4096;4. if(A=4096) System.out.p

12、rintln(Equal);5. else System.out.println(Not Equal);6. 7. A在第 3 行出现转换丢失精度的编译错误.B输出Not Equal.C输出Equal.6) 关于以下程序哪条叙述正确?1. intj = 2;2. switch ( j ) 3. case 2:4. System.out.println (value is two);5. case 2 + 1:6. System.out.println (value is three);7. break;8. default:9. System.out.println(value is + j)

13、;10. break;11. A. 第 5 行的表达式不合法;B. 变量 j 是可接受的,switch 中表达式可以是 byte, short, int,或 long 的任何类型;C. 输出为 value is twoD. 输出是 value is two 后跟 value is threeE. 输出是 value is two 后跟 value is 27)以下程序的编译运行结果为:1: public class Q102: 3: public static void main(String args)4: 5:int i = 10;6:int j = 10;7:boolean b = fa

14、lse;8:9:if( b = i = j)10:System.out.println(True);11:else12:System.out.println(False);13:14: A. 第 9 行出现编译错误;B. 第 9 行出现运行错误;C. 输出 TrueD. 输出 False8)以下程序的编译和运行结果为?class test static boolean check;public static void main(String args) int i;if(check = true)i=1;elsei=2;if(i=2) i=i+2;else i = i + 4;System.o

15、ut.println(i);A. 3B. 4C. 5D. 6E. 语句 if(i=2) 编译出错9) 以下代码:if (a 4)System.out.println(test1);else if (a 9)System.out.println(test2);elseSystem.out.println(test3);a 为何值将有输出结果test2 ?A. 小于 0B. 小于 4C. 4 和 9 之间D. 大于 9E. 无任何可能10)有如下代码段:switch ( x )case 1:System.out.println(One);break;case 2:case 3:System.out

16、.println(Two);break;default:System.out.println(end);变量 x 的取值下列哪些情形时,能使程序输出Two 。A. 1B. 2C. 3D. default11)以下程序的输出结果为public class test publicstatic void main(String agrs) char c1= B ,c2= C;if (c1+1c2) +c1;System.out.println(c1);A. BB. bC. CD. c12) 假设 a 是 int 类型变量,并初始化为 1,则下列哪个为合法的条件语句?A.if (a) B.if (a3

17、)C.if (a=2) D.if (true)四、循环程序设计1)执行以下程序后,输出结果为public class ex2public static void main(String args) int f=1;int k;for (k=2;k5;k+)f*=k;System.out.println(k);A. 0B. 1C. 5D. 4E. 242) 设有如下类class Looppublic static void main(String agrs) int x=0;int y=0;outer:for(x=0;x100;x+)middle:for(y=0;y100;y+)System.o

18、ut.println(x=+x+; y=+y);if(y=10) 在 处插入什么代码可以结束外循环 ? A continue middle;Bbreak outer; C break middle; D continue outer; Enone of these3)以下代码的运行结果为:public class Calc public static void main (String args ) int total = 0;for (int i = 0, j = 10; total 30; +i, -j) System.out.println( i = + i + : j = + j);t

19、otal += (i + j);System.out.println(Total + total);A. 产生运行错误B. 产生编译错误C. 输出 Total 0D. 产生如下输出 :i = 0 : j = 10i = 1 : j = 9i = 2 : j = 8Total 304)以下程序的运行结果为:public class test public static void main(String args) int i=0, j=2;do i=+i;j-; while(j0); System.out.println(i);A. 0B. 1C. 2.35)以下程序的运行结果为?class x

20、yz public static void main(String args) int i,j,k;for (i = 0; i 3; i+) for(j=1; j 4; j+) for(k=2; k5; k+) if(i = j)& (j=k)System.out.println(i);A. 0B. 1C. 2D. 3E. 46) 以下程序的运行结果为?class test public static void main(String args) int i,j=0;for(i=10;i-j) continue; while(i5);执行完后, i、 j 的值分别为:ACi=6i=6j=5j=

21、4BDi=5i=5j=5j=68)以下程序的输出结果为:public class example public static void main(String args) int s=0;for (int i=0;i3*i;j-)s += i*j;System.out.println(s);A. 127B.136C. 147D.1539) 以下程序的输出结果为 :public class example public static void main(String args) int i=0;for (i=0;i 4) & (i1- 10);f1 += 1.0; while (!b1);Sys

22、tem.out.println(b1 + , + i1 + , + f1);A. false,9,4.3C. false,8,1.3B. true,11,1.3D. true,8,7.3五、方法设计1)以下代码的输出结果 ?public class Teststatic int x=5;public static void main(String argv)change(x);x+;System.out.println(x);static void change(int m)m+=2;A. 7B. 6C. 5D. 82) 以下代码的输出结果 ? public class Testint x=5

23、;public static void main(String argv) Test t=new Test();t.x+;change(t);System.out.println(t.x);static void change(Test m) m.x+=2;A. 7B. 6C. 5D. 83)以下代码的输出结果 ?public class Testpublic static void main(String argv)String x=hello;change(x);System.out.println(x);static void change(String m)m=m+2;A. hello

24、C. 编译报错B. hello2D. 运行报错,不能将串与整数相加4)设有如下类:class MyPoint void myMethod() int x, y;x = 5; y = 3;System.out.print( ( + x + , + y + ) );switchCoords( x, y );System.out.print( ( + x + , + y + ) );void switchCoords( int x, int y ) int temp;temp = x;x = y;y = temp;System.out.print( ( + x + , + y + ) );如果执行

25、myMethod()方法,则输出结果为 ?A. (5, 3) (5, 3) (5, 3)B. (5, 3) (3, 5) (3, 5)C. (5, 3) (3, 5) (5, 3)5)以下程序的输出结果为:public class test public static void main(String args) int s=0;for (int k=0;k=10;k+)s+=method(2,k)-1;System.out.println(s);public static int method(int n,int m) if (m=0)return 1;elsereturn n*method

26、(n,m-1);A. 2048B. 1024C. 2036D.20006) 以下程序的输出结果为 : public class test public static void main(String args) int m=0;for ( int k=0;k2;k+) method(m+); System.out.println(m);public static void method(int m) System.out.print(m);A. 000B. 012C.123D.111六、数组的使用1)输入如下命令运行Java应用程序。java MyTest123则命令行参数数组args 中得到

27、的值哪个正确?A. args0 = MyTest 1 2 3B. args0 = 123C. args0 = 1D. args1= 1232) 在注释 /Start For loop 处要插入哪段代码可实现根据变量的元素 ?i 的值定位数组iapublic class Linpublic void amethod()int ia = new int4;/Start For loopiai=i;System.out.println(iai);A. for (int i=0; i ia.length() -1; i+)B. for (int i=0; i ia.length(); i+)C. fo

28、r (int i=1; i 4; i+)D. for (int i=0; i ia.length;i+)3)以下代码的调试结果 ?public class Q public static void main(String argv) int anar= new int5;System.out.println(anar0);A. 编译错误: anar 在引用前未初始化。B. nullC. 0D. 54) 下列创建二维整型数组正确语句是:A. int a = new int 10,10;B. int a1010 = new int ;C. int a = new int 1010;D. int a

29、 = new int 1010;5) 给出下面代码:public class Personstatic int arr = new int10;public static void main(String a) System.out.println(arr1);以下那个说法正确?A. 编译时将产生错误;B. 编译时正确,运行时将产生错误;C. 输出 0;D. 输出 null 。6)设有如下说明 :char c = new char100;则, c50 的值为 ?A. 50B. /u0000C. D. 不定E. 为 null ,直到被赋值。7) 设有如下程序,其调试结果为:class Q2 pu

30、blic static void main(String args) int seeds = 1,2,3,4,6,8;int n= seeds.length;for (int i = 0; i 3; i+)for (int k = 0; k n-1; k+)seedsk= seedsk+1;for (int i = 0; i n-1; i+)System.out.print(/t+seedsi);A输出: 12346B输出: 46888C输出: 23468D输出: 2346七、类与对象编程1) 以下程序的运行结果为:public class Myint value;public static

31、void main(String args) My x=new My();if (x=null)System.out.println(No Object);elseSystem.out.println(x.value);A. 0B. 1C. No ObjectD. 编译错误E. null( 2)以下程序的运行结果为:public class A static int k=3;public static void main(String args) int k=4;A x1=new A(); x1.k+;A x2=new A(); x2.k+;k+;System.out.println(x1.k

32、);A. 3B. 4C.5D.6E.73) 编译和运行以下程序结果为:public class A static int k=3;static int m;public static void main(String args) k+;if (m=0)System.out.println(k);elseSystem.out.println(B.k);k+;class B static int k=6;A. 3B. 4C.5D.编译错误E.64)编译和运行以下程序结果为:1: public class Q21 2: int maxElements;3:void Q21()4:maxElements = 100;5:System.out.println(maxElements);6: 7:Q21(int i) 8:maxElements = i

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1