Java基础练习题附答案.doc

上传人:w****2 文档编号:6537337 上传时间:2020-02-28 格式:DOC 页数:44 大小:137.50KB
返回 下载 相关 举报
Java基础练习题附答案.doc_第1页
第1页 / 共44页
Java基础练习题附答案.doc_第2页
第2页 / 共44页
Java基础练习题附答案.doc_第3页
第3页 / 共44页
点击查看更多>>
资源描述
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 static 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. 编译执行 D.面向对象5) 以下程序的运行结果为: public class Testpublic static void main(String argv ) System.out.println(x=+5);A. 5 B. x=5 C. x=+5 D. x=56) 以下程序的运行结果为: public class Testpublic static void main(String argv ) System.out.println(good+morning);A. goodmorning B. good+morning C. good morning D. good+morning二、Java符号与表达式1) 现有一个int类型的整数和一个double类型的数进行加法运算,则得到的结果类型为: Aint类型 B. double类型 C. float类型 D. long类型 2)下面程序段的输出结果是: int a = 2; System.out.print( a+); System.out.print( a); System.out.print(+a); A333 B334 C234 D233 3) 以下代码的输出结果?public class Test int x=3; public static void main(String argv) int x= 012; System.out.println(x); A12 B012 C10 D3 4) 下列定义语句正确的是:Achar c=/n; Bint i=12; Cfloat f=2.8; Dboolean b=null;5)检查如下代码: public class Quiz2_l public 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的可能类型有哪些? byte b = 11; short s = 13; result = b * +s;Abyte, short, int, long, float, doubleBboolean, byte, short, char, int, long, float, doubleCbyte, short, char, int, long, float, doubleDbyte, short, charEint, long, float, double7)以下代码的输出结果为: System.out.println( +2 + 3); System.out.println(2 + 3); System.out.println(2 + 3 +); 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; 以下赋值语句哪个需要强制转换。AmyInt = myByte;BmyInt = myLong;CmyByte = 3;DmyInt = myChar;EmyFloat = myDouble;FmyFloat = 3;GmyDouble = 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; System.out.println(b); A2 B3 C5 D7三、分支程序设计1)下列语句片段的结果为int result; int a=17,b=6; result=(a%b4)? a%b:a/b ;System.out.println(result); A. 0 B. 1 C. 2 D. 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. 输出:value is 99.99 B. 输出: value is 9 C. 输出: value is 9.0 D. 在第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 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.println(Equal);5. else System.out.println(Not Equal);6. 7. A在第3行出现转换丢失精度的编译错误.B输出 Not Equal.C输出 Equal.6) 关于以下程序哪条叙述正确?1. int j = 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);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 = false;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; else i=2; if(i=2) i=i+2; else i = i + 4; System.out.println(i); A. 3 B. 4 C. 5 D. 6 E. 语句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. 小于 0 B. 小于 4 C. 4 和9之间 D. 大于9 E. 无任何可能10)有如下代码段:switch ( x ) case 1:System.out.println(One);break; case 2: case 3:System.out.println(Two);break; default:System.out.println(end);变量x的取值下列哪些情形时,能使程序输出Two 。 A. 1 B. 2 C. 3 D. default11)以下程序的输出结果为 public class test public static void main(String agrs) char c1=B,c2=C; if (c1+1c2) +c1; System.out.println(c1); A. B B. b C. C D. c 12) 假设a是int类型变量,并初始化为1,则下列哪个为合法的条件语句? A. if (a) B. if (a3) C. if (a=2) D. if (true) 四、循环程序设计1)执行以下程序后,输出结果为public class ex2 public static void main(String args) int f=1; int k; for (k=2;k5;k+) f*=k; System.out.println(k); A. 0 B. 1 C. 5 D. 4 E. 242) 设有如下类class Loop public static void main(String agrs) int x=0;int y=0; outer: for(x=0;x100;x+) middle: for(y=0;y100;y+) System.out.println(x=+x+; y=+y); if(y=10) 在处插入什么代码可以结束外循环?Acontinue middle;Bbreak outer;Cbreak middle;Dcontinue 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); total += (i + j); System.out.println(Total + total); A. 产生运行错误B. 产生编译错误C. 输出 Total 0D. 产生如下输出: i = 0 : j = 10 i = 1 : j = 9 i = 2 : j = 8 Total 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. 0 B. 1 C. 2 .35)以下程序的运行结果为? class xyz 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. 0 B. 1 C. 2 D. 3 E. 4 6) 以下程序的运行结果为?class test public static void main(String args) int i,j=0; for(i=10;i-j) continue; while(i5); 执行完后,i、j的值分别为: A i=6 j=5 B i=5 j=5 C i=6 j=4 D i=5 j=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. 127 B.136 C. 147 D.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); System.out.println(b1 + , + i1 + , + f1); A. false,9,4.3 B. true,11,1.3 C. false,8,1.3 D. true,8,7.3五、方法设计1)以下代码的输出结果?public class Test static int x=5; public static void main(String argv) change(x); x+; System.out.println(x); static void change(int m) m+=2; A. 7 B. 6 C. 5 D. 8 2) 以下代码的输出结果?public class Test int x=5; 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. 7 B. 6 C. 5 D. 8 3) 以下代码的输出结果?public class Test public static void main(String argv) String x=hello; change(x); System.out.println(x); static void change(String m) m=m+2; A. hello B. hello2 C. 编译报错 D. 运行报错,不能将串与整数相加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 + ) ); 如果执行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; else return n*method(n,m-1); A. 2048 B. 1024 C. 2036 D.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. 000 B. 012 C.123 D.111六、数组的使用1)输入如下命令运行Java应用程序。 java MyTest 1 2 3则命令行参数数组args中得到的值哪个正确?A. args0 = MyTest 1 2 3B. args0 = 1 2 3C. args0 = 1D. args1= 1 2 32) 在注释/Start For loop 处要插入哪段代码可实现根据变量i的值定位数组ia的元素?public class Lin public void amethod() int ia = new int4; /Start For loop iai=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. for (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 = new int 1010;5) 给出下面代码: public class Person static 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 public 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输出: 1 2 3 4 6B输出: 4 6 8 8 8 C输出: 2 3 4 6 8 D输出: 2 3 4 6 七、类与对象编程1) 以下程序的运行结果为:public class My int value; public static void main(String args) My x=new My(); if (x=null) System.out.println(No Object); else System.out.println(x.value); A. 0 B. 1 C. No Object D. 编译错误 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); A. 3 B. 4 C.5 D.6 E.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); else System.out.println(B.k); k+; class B static int k=6; A. 3 B. 4 C.5 D.编译错误 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; 9: System.out.println(maxElements); 10: 11: public static void main(String args) 12: Q21 a = new Q21(); 13: Q21 b = new Q21(999); 14: 15: A. 输出100 和 999. B. 输出999 和 100. C. 第2行出现编译错误,变量 maxElements未初始化.D. 12行出现编译错误.5)以下的程序的调试结果为 public class Scope int i; public static void main(String argv) Scope s = new Scope(); s.amethod(); public static void amethod() System.out.println(i); A. 输出结果为:0 B. 无输出 C. 编译错误 D. 输出null6)给出下面代码: public class Person static int arr = new int10;public static void main(String a) System.out.println(arr1); 以下那个说法正确? A. 编译时将产生错误; B. 编译时正确,运行时将产生错误; C. 输出0; D. 输出null。7)以下的程序的调试结果为?public class As int i = 10; int j; char z= 1; boolean b; public static void main(String argv) As a = new As(); a.amethod(); public void amethod() System.out.println(j); System.out.println(b); A输出0 和falseB. 输出0 和trueC. 编译错误,b 未初始化D. 编译错误, z 必须赋字符值8)以下的程序的调试结果为?public class MyAr public static void main(String argv) MyAr m = new MyAr(); m.amethod(); public void amethod() static int i; System.out.println(i); A. 输出结果为 0B. 运行出错C. 输出结果为 nullD. 编译错误9) 以下程序的运行结果为?class ValHold public int i = 10;public class ObParm public static void main(String argv) ObParm o = new ObParm(); o.amethod(); public void amethod() int i = 99; ValHold v = new ValHold(); v.i=30; another(v,i); System.out.print( v.i ); public void another(ValHold v, int i) i=0; v.i = 20; ValHold vh = new ValHold(); v = vh; System.out.print(v.i); System.out.print(i); A10030 B. 20030 C. 209930 D. 10020八、继承与多态1)以下程序调试结果为:class Base Base() int i = 100; System.out.print (i); public class Pri extends Base static int i = 200; public static void main(String argv) Pri p = new Pri(); System.out.print(i); A编译错误 B200 C100200 D100 (2) 以下程序调试结果为: public class Test int m=5; public void some(int x) m=x; public static void main(String args ) new Demo().some(7); class Demo extends Test int m=8; public void some(int x) super.some(x); System.out.println(m); A5 B8 C7 D无任何输出 E编译错误 3) 试完成下述程序片段:public class Point() int x,y; public Point(int x,int y) =x; =y; . A Point.x Point.y B无解 C x1 y1 Dthis.x this.y(4)考虑如下类:1. class Test(int i) 2. void test(int i) 3. System.out.println(I am an int.);4. 5. void test(String s) 6. System.out.println(I am a string.);7. 8.9. public static void main(String args) 10. Test t=new Test();11. char ch=y;12. t.test(ch);13. 14. 以下哪条为真?A行 5 不能通过编译,方法不能被覆盖.B行 12 不能通过编译, 因为没有一个test()方法含字符参数.C代码可以编译但在12行将出现异常.D代码可以编译且产生如下输出: I am an int.E代码可以编译且产生如下输出: I am a String.(5) 类Test1定义如下:1public class Test12 public float aMethod(float a,float b) 3 4 将以下哪种方法插入行3是不合法的。( )Apublic float aMethod(float a, float b,float c) Bpublic float aMethod(float c,float d) Cpublic int aMethod(int a, int b) Dprivate float aMethod(int a,int b,int c) 6)考虑如下代码:class Treeclass Pine extends Treeclass Oak extends Treepublic class Forest public static void main( String args ) Tree tree = new Pine(); if( tree instanceof Pine ) System.out.println( Pine ); if( tree instanceof Tree ) System.out.println( Tree ); if( tree instanceof Oak ) System.out.println( Oak ); else System.out.println( Oops ); 则输出结果中有哪些?APine BTree CForest DOops E无输出7)以下程序的编译和运行结果为?abstract class Base abstract public void myfunc(); public void another() System.out.println(Another method); public class Abs extends Base public static void main(String argv) Abs a = new Abs(); a.amethod(); public void myfunc() System.out.println(My Func
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 小学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!