Java程序设计智慧树知到超星尔雅网课答案
Java程序设计2019智慧树知到超星尔雅网课答案
2019智慧树知到超星尔雅网课答案大全
第1章单元测试
1、编译和运行以下代码的结果为: public class
MyMain{ public static void main(String
argv){ System.out.println(“Hello cruel world”); } }
答案:编译无错,但运行时指示找不到main方法
2、以下哪个是 Java 应用程序入口的 main 方法头 ?
答案:public static void main(String a[])
3、编译Java源程序文件将产生相应的字节码文件,字节码文件的扩展名为?
答案:class
4、main方法是Java Application程序执行的入口点,关于main 方法的方法头合法的有?
答案:public static void main(String[ ] args)、public static void main(String arg[ ])
5、每个源程序文件中只能定义一个类。
答案:错
第2章单元测试
1、在Java中,十进制数16的十六进制表示格式是?
答案:0 16
2、要产生[10,100]之间的随机整数使用哪个表达式?
答案:10+(int)(Math.random()*91)
3、下列符号中不能作为Java标识符的是?
答案:45thing
4、下面各项中定义变量及赋值不正确的是?
答案:float f = 45.0;
5、执行以下代码段后, x, a,和 b的值为? 1. int x, a = 6, b = 7; 2. x = a++ + b++;
答案:x= 13, a=7, b=8
6、下列哪个不是Java的保留字?
答案:cin
7、哪些赋值是合法的?
答案:long test = 012、float f = 412、double d =
0x12345678
8、下列代码中,将引入编译错误的行是 1 public class Exercise{ 2 public static void main(String args[]){ 3
float f = 0.0 ; 4 f = f + 1.0 ; 5 } 6 }
答案:第3行、第4行
9、下列哪些是合法标识符?
答案:$persons、TwoUsers
10、下列哪些是java中有效的整数表示形式?
答案:22、022、0x22
第3章单元测试
1、如何更改break语句使退出inner和middle循环,继续外
循环的下一轮? outer: for (int x = 0; x < 3; x++) { middle: for (int y = 0; y < 3; y++) { inner: for (int z = 0; z < 3; z++) { if (arr(x, y, z) == targetValue) break; } }}
答案:break middle;
2、以下程序的输出结果为?public class Test { public static void main(String args[]) { for ( int k = 0; k < 3; k++)
System.out.print(“k”); } }
答案:}
3、以下代码的调试结果为?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: }
答案:0
4、以下代码的调试结果为?以下程序的运行结果为public class test { public static void main(String args[]) { int i = 1; do { i–; } while (i > 2); System.out.println(i); }}
答案:0
5、下面的代码段执行之后count的值是什么? int count = 0; for (int i = 1; i < 4; i++) { count += i; }
System.out.println(count);
答案:6
6、以下程序的运行结果为: 1. public class Conditional { 2. public static void main(String args [] ) { 3. int x = 4; 4. Syst em.out.println( “value is “ + 5. ((x > 4) ?
99.99 : 9)); 6. } 7. }
答案:输出: value is 9.0
7、下列程序的运行结果?public class Test { public static void main(String a[]) { int x=3,y=4,z=5; if (x>3) { if
(y<2) System.out.println(“show one”); else
System.out.println(“show two”); } else { if (z>4) System.out.println(“show three”); else
System.out.println(“show four”); } }}
答案:该问题答案暂未收录,请手动搜索。
8、以下程序调试结果 public class test { public static
void main(String args[]) { int i=1, j=3; while (j>0)
{ j–; i++; } System.out.println(i); }}
答案:4
9、在switch(expression)语句中,expression的数据类型不能是?
答案:double、boolean
10、假设a是int类型变量,并初始化为1,则下列哪个为合法的条件语句?
答案:if (a<3) { }、if (true) { }
第4章单元测试
1、以下程序运行时输入: java Cycle hello two me
2public class Cycle{ public static void main(String
args[]){ System.out.println(args[1]); }}则运行结果为?
答案:two
2、public class test { public static void main(String args[]) {int m=0;for ( int k=0;k<2;k++)
method(m++);System.out.println(m); } public static void method(int m) { System.out.print(m); }}
答案:C
3、以下程序运行结果为: public class Q { public static void main(String argv[]) { int anar[]= new int[5]; System.out.println(anar[0]); } }
答案:0
4、下列程序的运行结果是:public class Test { public static void main(String args[]) { int m[]={1,2,3,4,5,6,7,8}; int sum = 0; for (int i=0;i<8;i++){ sum = sum + m[i]; if
(i==3) break; } System.out.println(sum); }}
答案:After swap1:circle1=1.0, circle2=2.0 After
swap2:circle1=2.0, circle2=1.0
5、下面定义和给数组初始化正确的是:
答案:String temp [] = {‘’a’’, ‘’b’’, ‘’c’’};
6、在注释//Start For loop 处要插入哪段代码可以实现根据变量i的值定位访问数组ia[]的所有元素。 public class
Lin{ public void amethod(){ int ia[] = new int[4];
//Start For loop { ia[i]=i;
System.out.println(ia[i]); } } }
答案:for (int i=0; i< ia.length;i++)
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< n1; k++) seeds[k]= seeds[k+1]; for (int i = 0;
i 答案:输出: 4 6 8 8 8 8 8、下列选项能正确定义一个整形数组的是: 答案:int scores[];、int[] scores; 9、设有如下代码: int[] x = new int[25]; 执行后,以下哪个说法正确? 答案:x[24] 为 0、x.length 为 25. 第5章单元测试 1、关于以下程序的说明,正确的是()1. class StaticStuff2. { 3. static int x=10;4. static { x+=5;}5. public static void main(String args[ ])6. {7.System.out.println(“x=” + x);8. }9. static { x/=3;}10. } 答案:输出22 2、以下程序编译和运行会发生什么 public class Q8 { int i = 20; static { int i = 10; } public static void main(String[] args) { Q8 a = new Q8(); System.out.println(a.i); } } 答案:输出 20. 3、给出如下类定义: public class test { test(int k) { } } 如果要创建一个该类的对象,正确的语句是: 答案:test obj1 = new test(5); 4、有如下代码:public class Person { … } 下列哪个符合该类的构造方法定义 答案:public Person() {…} 5、以下代码的输出结果? 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; } } 答案:6 6、设有如下程序:public class Test5 { public static void main (String args []) { /This is the start of a comment if (true) { Test5 = new test5(); System.out.println(“Done the test”); } / This is another comment */ System.out.println (“The end”); }}结果为? 答案:程序输出”The end” 7、给出下面的不完整的类代码: class Person { String name, department; int age; public Person(String n){ name = n; } public Person(String n, int a){ name = n; age = a; } public Person(String n, String d, int a) { // doing the same as two arguments version of constructor // including assignment name=n,age=a department = d; } } 下面的哪些表达式可以加到构造方法中的”doing the same as…”处? 答案:name=n;age=a;、this(n,a); 8、考虑如下类: public class Test { int j,k; public Test(int j ) { this(j,0); } public Test(int j, int k) { this.j=j; this.k=k; } } 以下哪些可正确创建Test对象?答案:Test t = new Test(l);、Test t = new Test(l, 2); 第6章单元测试 1、在Java中,如下的修饰符不是访问控制修饰符 答案:static 2、类Test1定义如下: 1. public class Test1{ 2. public float aMethod(float a, float b){ } 3. 4. } 将以下哪种方法插入行3是不合法的。( ) 答案:public float aMethod(float c,float d){ } 3、以下代码调试结果 class Base {} class Sub extends Base {} public class CEx{ public static void main(String argv[]){ Base b = new Base(); Sub s = (Sub) b; } } 答案:运行异常 4、如何定义一个不能有子类的类Key? 答案:final class Key { } 5、class Person { private int a; public int change(int m){ return m; } } public class Teacher extends Person { public int b; public static void main(String arg[]){ Person p = new Person(); Teacher t = new Teacher(); int i; // point x } } 在 // point x安排哪个语句合法? 答案:i = p.change(30); 6、如何能使程序调用Base类的构造方法输出”base constructor”; class Base{ Base(int i){ System.out.println(“base constructor”); } Base(){ } } public class Sup extends Base{ public static void main(String argv[]){ Sup s= new Sup(); //One } Sup() { //Two } public void derived() { //Three } } 答案:在//Two行之后放置super(10); 7、以下程序的输出为? 1: class MyClass 2: { 3: static int maxElements; 4: 5: MyClass(int maxElements) 6: { 7: this.maxElements = maxElements; 8: } 9: 10: } 11: 12: public class Q19 13: { 14: public static void main(String[] args) 15: { 16: 17: MyClass a = new MyClass(100); 18: MyClass b = new MyClass(100); 19: 20: if(a.equals(b)) 21: System.out.println(“Objects have the same values”); 22: else 23: System.out.println(“Objects have different values”); 24: } 25: } 答案:输出“Objects have different values” 8、在构造方法的哪个地方可以调用父类的构造方法? 答案:构造方法的第一条语句 9、定义常量时使用的关键字是 答案:final 10、设有如下代码: class Base{} public class MyCast extends Base{ static boolean b1=false; static int i = 1; static double d = 10.1; public static void main(String argv[]){ MyCast m = new MyCast(); Base b = new Base(); //Here } } 则在 //Here处插入哪个代码将不出现编译和运行错 误。 答案:b=m;、d =i; 第7章单元测试 1、测试如下代码: public class Ref{ public static void main(String[] args) { StringBuffer sbl = new StringBuffer(“Hello”); StringBuffer sb2 = new StringBuffer(“Hello”); boolean result = sbl.equals(sb2); System.out.println(result); } } 下述哪条语句正确描述了程 序编译和运行的行为? 答案:编译成功,输出为 false 2、String alphabet=”ABCDEFGHIJKLMNOPQ” 调用 alphabet.substring(6,10)返回什么子字符串? 答案:GHIJ 3、以下程序的调试结果为? 1. public class EqualsTest{ 2. public static void main(String args[]){ 3. Long LA = new Long(7); 4. Long LB = new Long(7); 5. if(LA==LB) System.out.println(“Equal”); 6. else System.out.println(“Not Equal”); 7. } 8. } 答案:输出”Not Equal” 4、有如下代码: public class Test{ public static void main(String args[]) { String str = new String(“World”); char ch[] = {‘H’,’e’,’l’,’l’,’o’}; change(str,ch); Sy stem.out.println(str + “and” + ch); } public static void change(String str, char ch[]) { str = “Changed”; ch[0] = ‘C’; } } 运行后输出的结果是: 答案:World and Cello 5、已知代码:String s = “story”;下列语句中合法的是: 答案:s += “books”;、String t = s.toLowerCase(); 第8章单元测试 1、以下程序的编译和运行结果为? 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”); } public void amethod(){ myfunc(); } } 答案:输出结果为 My Func 2、以下代码的调试结果为? abstract class MineBase { abstract void amethod(); static int i; } public class Mine extends MineBase{ public static void main(String argv[]){ int[] ar = new int[5]; for(i = 0;i < ar.length;i++) System.out.println(ar[i]); }} 答案:0 3、有关内嵌类以下哪个叙述为假? 答案:匿名内嵌类没有对应的字节码文件 4、以下哪个正确定义抽象类? 答案:abstract class Animal {abstract void growl();} 5、考虑如下代码,其中包括一个内嵌类: public final class Test4 { class Inner { void test() { if (Test4.this.flag) { sample(); } } } private boolean flag = false; public void sample() { System.out.println(“Sample”); } public Test4() { (new Inner()).test(); } public static void main(String args []) { new Test4(); } } 结果为? 答案:程序无输出,但正确终止 6、有关抽象类,以下哪点为真? 答案:不能对该类实例化、类定义包含abstract 关键字 7、设有类定义如下: class InOut{ String s= new String(“Between”); public void amethod(final int iArgs){ int iam=5; iam++; class Bicycle{ public void sayHello(){ //Here } } } public void another(){ int iOther; } } 以下哪些语句可以安排在//Here处 ? 答案:System.out.println(s);、System.out.println(iArgs); 8、在抽象类中,抽象方法定义正确的是? 答案:public abstract void method();、abstract void Method(); 9、设有如下代码: interface IFace{ } class CFace implements IFace{ } class Base{ } public class ObRef extends Base{ public static void main(String argv[]){ ObRef obj = new ObRef(); Base b = new Base(); Object obj1 = new Object(); IFace obj2 = new CFace(); //Here } } 则在 //Here处插入哪个代码将不出现编译和运行错误。 答案:obj1=obj2;、b=obj;、obj1=b; 10、下列说法正确的是下列说法正确的是 答案:购买实物的原始凭证,必须有验收证明、原始凭证必须记录真实,内容完整、有关现金和银行存款的收支凭证,如果填写错误,必须作废、一般原始凭证发生错误,必须按规定办法更正 第9章单元测试 1、自定义异常的父类是? 答案:Exception 2、在命令行输入如下命令,结果为 java myprog good morningpublic class myprog{ public static void main(String argv[]) { System.out.println(argv[2]); } } 答案:× 3、假设m()方法声明抛出IO异常,哪个书写合法. 答案:void m() throws IOException{} 4、下列关键字中用于明确抛出一个异常的是? 答案:throw 5、检查下面的代码: class E1 extends Exception{} class E2 extends E1{} public class Quiz6_l{ public static void f(boolean flag) throws E1,E2{ if(flag) { throw new E1(); } else { throw new E2(); } } public static void main(String[] args) { try{ f(true); } catch(E2 e2) { System.out.println(“Caught E2”); }catch(E1 e1) { System.out.println(“Caught El”); } } } 对上面的程序进 行编译、运行,下面的叙述哪个是正确的: 答案:编译成功,输出为: Caught E1 1、A:由于Qoiz6_1.main方法中没有声明抛出异常EE2,所以编 译会失败 答案:第一空: A 6、设有如下代码段 1 String s = null; 2 if ( s != null & s.length() > 0) 3 System.out.println(“s != null & s.length() > 0”); 4 if ( s != null && s.length() > 0) 5 System.out.println(“s != null & s.length() > 0”); 6 if ( s != null || s.length() > 0) 7 System.out.println(“s != null & s.length() > 0”); 8 if ( s != null | s.length() > 0) 9 System.out.println(“s != null | s.length() > 0”); 哪些行将抛出空指针异常? 答案:2,6,8 7、当2个实际参数分别为4和0时,以下方法调用的执行结果为:public void divide(int a, int b) { try { int c = a / b; } catch (Exceptio n e) { System.out.print(“Exception “); } finally { System.out.println(“Finally”); } } 答案:输出 Exception Finally 8、检查下面的代码: class E1 extends Exception{ } class E2 extends E1 { } public class Quiz6_5{ public static void main(String[] args){ try{ throw new E1(); } // –X– } } 下列语句,哪一个可以放到–X–位置,而且保证编译成功。答案:catch(Exception x){}、catch(El x){} 9、检查下面的代码: class E1 extends Exception{ }; class E2 extends E1{ } class SuperQuiz6_2 { } public class Quiz6_3 extends SuperQuiz6_2{ public void f(Boolean flag) throws E1{ //一一X一一 } } 下列的语句,哪—个可以放到 –X–位置,而且保证编译成功。 答案:throw new El();、throw new E2(); 10、以下叙述那个正确? 答案:一个try块必须至少跟一个finally 或 catch块.、如果catch 和 finally块均有,则catch 必须先于finally. 第10章单元测试 1、在Applet的方法中,下列哪个方法将在关闭浏览器时执行,以释放Applet占用的资源? 答案:destroy() 2、关于以下代码所画图形的说明,正确的是? 1.g.setColor(Color.black); 2.g.drawLine(10,10,10,50); 3.g.setColor(Color.red); 4.g.drawRect(100,100,150,150); 答案:一条40像素长的垂直黑线,一个边长为150像素的红色 正方形 3、paint()方法使用哪种类型的参数? 答案:Graphics 4、下列Applet类的方法中,在Applet的整个生命周期里至多只 能执行一次的是? 答案:init(); 5、为了向一个Applet传递参数,可以在HTML文件的LET标签中 使用PARAM选项,在Applet程序中参数时,应使用的方法是 答案:getParameter() 第11章单元测试 1、下列哪个容器类使用时必须加入到其他的容器中? 答案:Panel 2、在AWT中部件如何注册事件监听者? 答案:调用部件的addXXXListener()方法 3、通过哪个方法可以改变按钮的颜色? 答案:setBackground 4、新创建的 Frame是不可见的,使用哪个方法可使其可见 答案:setVisible(true) 5、Frame 的默认的布局管理器是下列哪一个 ? 答案:BorderLayout 6、有关事件监听者以下哪个为真? 答案:一个部件可有多个监听者、一个监听者可处理来自多个部件的事件. 7、哪个方法可得到 WindowEvent 中的事件源 ? 答案:getSource()、getWindow() 第12章单元测试 1、哪个关键字用于与锁标记打交道? 答案:synchronized 2、线程在生命周期要经历5种状态,如果线程当前是新建状态,则它可到达的下一个状态是? 答案:可运行状态 3、以下哪个方法用来定义线程的执行体? 答案:run() run() 4、下面说法不正确的是() 答案:竞单时制造企业和贸易公司只能在本地市场、永久开拓的市场中参与竞单; 5、下列程序的功能是在监控台上每一秒种显示一个字符串“Hello!”,能够填写在线程中下划线位置,使程序完整并能正 确运行的语句是public class Test implements Runnable{ public static void main(String args[]){ Test t=new Test(); Thread tt=new Thread(t); tt.start(); } public void run(){ for(;;){ try { ; } catch(e){ } System.out.println(“Hello”); } }} 答案:sleep(1000) InterrutpedException、Thread.sleep(1000) InterruptedException 第13章单元测试 1、用DataOuputStream的哪个方法写一个字符串到顺序文件? 答案:writeUTF 2、在读文件employ.dat时,使用该文件名作为字符串参数的类是? 答案:FileInputStream 3、给一个已存在的文件用FileWriter(‘report’) 给其写入数据,则结果为 ? 答案:文件被新文件替换; 4、以下哪个是构造 RandomAccessFile 的合法方式? 答案:RandomAccessFile(“data”, “r”); 5、在File类提供的方法中,用于创建目录的方法是? 答案:mkdir() 第14章单元测试 1、关于泛型,以下哪个叙述为假? 答案:泛型的类型参数允许基本类型 2、关于java.util.Collections类的叙述,以下哪个为真? 答案:Collections类提供有sort方法用于对列表的排序; 3、欲构造ArrayList类的一个实例,元素类型为String,作为 List类型的对象,下列哪个方法是正确的? 答案:List myList=new ArrayList (); 4、类 java.util.HashMap 实现的接口是? 答案:java.util.Map 5、设有泛型类的定义如下 class Test { } 则由该类创建对象时,使用正确的是? 答案:Test x = new Test(); 第15章单元测试 1、类 JoptionPane 中的哪个方法可以用来读取用户的输入 数据? 答案:showInputDialog( ) 2、哪个方法给JFrame窗体加入菜单条? 答案:setJMenuBar() 3、JPanel的默认布局管理器是? 答案:FlowLayout 4、JTextField 的事件监听器接口为? 答案:ActionListener 5、Swing部件中图形绘制一般是通过重写以下哪个方法? 答案:paintComponent