java第二次小测试
- 格式:doc
- 大小:86.00 KB
- 文档页数:19
java语言二级考试及答案1. 以下哪个选项是Java语言中正确的数据类型?A. 整型B. 字符串C. 布尔型D. 所有选项答案:D2. Java中,哪个关键字用于声明一个类?A. classB. structC. interfaceD. enum答案:A3. 在Java中,以下哪个选项不是控制流语句?A. ifB. switchC. forD. try答案:D4. Java中,以下哪个选项是用于抛出异常的关键字?A. throwB. catchC. finallyD. try答案:A5. 在Java中,以下哪个选项是用于声明一个接口的关键字?A. classB. structC. interfaceD. enum答案:C6. Java中,以下哪个选项不是访问修饰符?A. publicB. privateC. protectedD. global答案:D7. 在Java中,哪个关键字用于定义一个方法?A. functionB. methodC. defD. void答案:B8. Java中,以下哪个选项不是集合框架中的接口?A. ListB. SetC. MapD. Array答案:D9. 在Java中,哪个关键字用于创建一个新的对象实例?A. newB. createC. instanceD. object答案:A10. Java中,以下哪个选项不是多线程编程的关键字?A. synchronizedB. volatileC. threadD. concurrent答案:D。
三、实验内容1、题目:利用数组实现斐波那契数列的输出2.实验程序代码:public class shulie{public static void main(String arg[]){int a[]=new int[20];a[0]=1;a[1]=1;int i,j;for(i=2;i<20;i++){a[i]=a[i-1]+a[i-2];}System.out.println("斐波那契数列的输出");for(i=0;i<20;i++){if(i%5==0){System.out.println();}System.out.print("a["+i+"]="+a[i]);}}}3.实验结果1.题目: 从键盘上输入10个数:(1)从小到大排序后(要求至少实现2中排序方式)输出。
(2)再从键盘上输入一个数,分别编写顺序查找和二分查找程序。
2.实验程序代码import java.util.Scanner;public class shuzu{public static void paixu1(int c[]) throws Exception //选择法{int i,j,temp,k,p,q;for(i=0;i<9;i++){k=i;for(j=i+1;j<10;j++){if(c[j]>c[k]) k=j;else{k=i;}if(k!=i){temp=c[k];c[k]=c[i];c[i]=temp;}}}for(i=0;i<10;i++){System.out.println("c[i]="+c[i]);}System.out.println("请输入查找的数字");Scanner s = new Scanner(System.in);q=s.nextInt();System.out.println("********************************************");System.out.println("请选择查找算法:");System.out.println("1:代表是顺序查找法");System.out.println("2:代表是折半查找法");System.out.println("********************************************");Scanner m = new Scanner(System.in);p=m.nextInt();if(p==1){sort1(c,q);}else{sort2(c,q);}}public static void paixu2(int b[]) throws Exception //冒泡法{int i,j,temp,p,q;for(i=0;i<9;i++)for(j=0;j<9-i;j++){if(b[j]>b[j+1]){temp=b[j];b[j]=b[j+1];b[j+1]=temp;}}for(i=0;i<10;i++){System.out.println("b[i]="+b[i]);}System.out.println("请输入一个寻找的数");Scanner s = new Scanner(System.in);q=s.nextInt();System.out.println("********************************************");System.out.println("请选择查找算法:");System.out.println("1:代表是顺序查找法");System.out.println("2:代表是折半查找法");System.out.println("********************************************");Scanner m= new Scanner(System.in);p=m.nextInt();if(p==1){sort1(b,q);}if(p==2){sort2(b,q);}}public static void sort1(int b[],int k) //顺序查找{int i,n=0;for(i=0;i<10;i++){if(b[i]==k){break;}else{n++;}}System.out.println(k+"在数组中的第"+n+"个位置");}public static void sort2(int b[],int k) //折半查找法{int start,end,i;for(start=0,end=9;start<end;){i=start+(end-start)/2;if(b[i]==k){System.out.println(k+"在数组中的第"+i+"个位置");break;}if(b[i]>k){end=i;}if(b[i]<k){start=i;}}}public static void main(String arg[])throws Exception{int i,n,m,p;Scanner s = new Scanner(System.in);int a[]= new int[10];for( i = 0; i < 10; i++)a[i]=s.nextInt();System.out.println("********************************************");System.out.println("请选择排序算法:");System.out.println("1:代表是选择法");System.out.println("2:代表是冒泡法");System.out.println("********************************************");Scanner z= new Scanner(System.in);n=z.nextInt();if(n==1){paixu1(a);}else{paixu2(a);}}}3,实验结果四实验总结1.java中开辟数组单元是用运算符new分配内存再赋值格式如下:数组名= new 数据类型[size]2.在java程序中如果需要从键盘输入数字,则需要在java开头输入import java.util.Scanner;,即定义scanner;在需要输入数字的地方输入:Scanner x=new Scanner(System.in);//构造一个Scanner对象,其传入参数为System.inint i=x.nextInt();//读取一个int数值.同时注意: 如果在函数中需要输入数字,则在开头必须输入throws Exception,避免出现错误.即:必须对其捕获或声明以便抛出。
全国计算机二级《Java》题库及答案1. 信息隐蔽的概念与下述哪一种概念直接相关()。
A. 软件结构定义B. 模块独立性C. 模块类型划分D. 模块耦合度【答案】B2. 下列各选项中,不属于序言性注释的是()。
A. 程序标题B. 程序设计者C. 主要算法D. 数据状态【答案】D 本资料来源公_众.号:文得课堂,更多计算机等级考试题库及视频,上公_众.号:文得课堂查找。
3. 对建立良好的程序设计风格,下面描述正确的是()。
A. 程序应简单、清晰、可读性好B. 符号名的命名只要符合语法C. 充分考虑程序的执行效率D. 程序的注释可有可无【答案】A4. 下列叙述中,不符合良好程序设计风格要求的是()。
A. 程序的效率第一,清晰第二B. 程序的可读性好C. 程序中要有必要的注释D. 输入数据前要有提示信息【答案】A5. 结构化程序所要求的基本结构不包括()。
A. 顺序结构B. GOTO跳转C. 选择(分支)结构D. 重复(循环)结构【答案】B6. 下列选项中不属于结构化程序设计原则的是()。
A. 可封装B. 自顶向下C. 模块化D. 逐步求精【答案】A7. 结构化程序设计的基本原则不包括()。
A. 多态性B. 自顶向下C. 模块化D. 逐步求精【答案】A8. 结构化程序设计主要强调的是()。
A. 程序的规模B. 程序的效率C. 程序设计语言的先进性D. 程序易读性【答案】D9. 结构化程序设计的3种结构是()。
A. 顺序结构,分支结构,跳转结构B. 顺序结构,选择结构,循环结构C. 分支结构,选择结构,循环结构D. 分支结构,跳转结构,循环结构【答案】B10. 结构化程序的三种基本控制结构是()。
A. 顺序、选择和重复(循环)B. 过程、子程序和分程序C. 顺序、选择和调用D. 调用、返回和转移【答案】A 本资料来源公_众.号:文得课堂,更多计算机等级考试题库及视频,上公_众.号:文得课堂查找。
11. 下列能生成Java文档的命令是()。
达内corejava第二次考试试题类型:单选题试题1已知变量a和obj的定义如下:Object[] a1 = { "a", "b", "c" };Object obj = a1;则下面语句运行时会抛出异常的是( )。
CA.Object[] a2 = (Object[])obj;B.String str1 = (String)((Object[])obj)[1]C.String[] a3 = (String[])objD.String str2 = (String)a1[2]试题2已知类Foo的定义如下:public class Foo {int value;Foo(int value) {this.value = value;}}运行下面程序段:Foo[] a1 = { new Foo(1), new Foo(2), new Foo(3) }; Foo[] a2 = new Foo[a1.length];System.arraycopy(a1, 0, a2, 0, a1.length);System.out.println((a1 == a2) + "," + (a1[1] == a2[1]));控制台输出的结果是( )。
AA.false,trueB.false,falseC.true,trueD.true,false试题3已知类Foo的定义如下:public class Foo {int value;Foo(int value) {this.value = value;}public boolean equals(Object obj) {if (obj instanceof Foo) {Foo foo = (Foo) obj;return value == foo.value;} else {return false;}}运行下面程序段:ArrayList list = new ArrayList();HashSet set = new HashSet();list.add(new Foo(1));set.add(new Foo(1));System.out.println(list.contains(new Foo(1)) + "," + set.contains(new Foo(1)));控制台输出的结果是( )。
二级JAVA机试-212(总分:100.00,做题时间:90分钟)一、1.基本操作题(总题数:1,分数:30.00)请完善程序(程序文件名:Java_1.java)并进行调试。
请在下画线处填入正确内容,然后删除下画线。
请勿删除注释行和其他已有的语句内容。
题目要求:阅读Java_1.java程序,该程序是分析球队能否升级的程序,每个球队由10名队员组成。
程序要求考核队员及格否的标记数,如考核及格输入1,否则输入2。
完成程序,运行后完成10个整数(1或2)的输入,如图1所示。
运行结果可能如图2或图3所示。
源程序://分析球队升级程序import javax.swing.JOptionPane;public class Java_1public static void main(String args[])//初始化变量int passes=0, //考试及格的队员数failures=0, //考试不及格的队员数student=1, //学生计数器result; //分析结果String input, //输入值output; //输出字符串//10个队员,用计数器控制循环while(student<=10)input=JOptionPane.sbowlnputDialog("输入队员考核结果(1=及格, 2=不及格)");result=Integer.parseInt( (1) );if(result==1)passes=passes+1;else failures=failures+1;student=student+1;//最终分析output="及格:"+passes+"/n不及格:"+failures;if(passes>8)output=output+"/n球队升级";JOptionPane.showMessageDialog(null, (2) , "球队升级分析结果", JOptionPane.INFORMATION_MESSAGE);System.exit(0);(分数:30.00)填空项1:__________________ (正确答案:input)解析:[解析] 程序通过对话框获取数据,然后根据数据判断,并用对话框显示结果。
CoreJava第二次内测有答案《CoreJava第二次内测》一、单选题(共30分,每题2分)1.定义一个只能接收整型数字的ArrayList,下面正确的是(c)A.ArrayList al=new ArrayList();B.B.ArrayList al=new ArrayList();C.ArrayList al=new ArrayList< Integer >();D.ArrayList al=new ArrayList< int >();2.List接口的特点是哪项?( C )A.不允许重复元素,元素有顺序B.不允许重复元素,元素无顺序c.允许重复元素,元素有顺序D.允许重复元素,元素无顺序3.创建一个只能存放String的ArrayList的语句是哪项?( B )A. ArrayList al=new ArrayList();B. ArrayList al=new ArrayList();C. ArrayList al=new ArrayList();D. ArrayList al =new List();4.现有public class Demo{public static void main (String[] args){List al=new ArrayList();a1.add("l"); a1.add("2");a1.add("2"); a1.add("3");System.out.println (al); } }上述程序执行后的输出是哪项?( A )A.[1,2,2,3] B.[1,2,3,3]C. [1,2,3] D.[2,1,3,2]5.实现了Set接口的类是哪项?( C )A.ArrayList B.HashTableC.HashSet D.Collection6.现有:class AddStuff2 {public static void main(String [] args) {TreeSett=new TreeSet();if (t.add("one"))if (t.add("two"))if (t.add ("three"))t.add("four");for (String s :t)System.out.print (s);} }结果为:( C )A. one three twoB. one two three fourC. four one three twoD.编译失败7.为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为( A )。
java二级考试真题及答案Java二级考试真题及答案一、单项选择题(每题2分,共40分)1. Java中,以下哪个关键字用于定义一个类?A. classB. interfaceC. structD. enum答案:A2. 在Java中,哪个关键字用于声明一个方法?A. functionB. methodC. defD. void答案:D3. 下列哪个选项是Java中的一个基本数据类型?A. StringB. intC. ListD. Map答案:B4. 在Java中,以下哪个关键字用于实现多态?A. extendsB. implementsC. interfaceD. abstract答案:A5. 下列哪个选项是Java中合法的标识符?A. 2variableB. variable$C. variableD. variable!答案:C6. 在Java中,哪个关键字用于抛出异常?A. throwB. throwsC. catchD. finally答案:B7. 在Java中,以下哪个关键字用于定义一个接口?A. classB. interfaceC. abstractD. enum答案:B8. 下列哪个选项是Java中的集合框架的一部分?A. ArrayListB. StringC. HashMapD. Both A and C答案:D9. 在Java中,哪个关键字用于声明一个抽象方法?A. abstractB. finalC. staticD. synchronized答案:A10. 在Java中,哪个关键字用于声明一个同步方法?A. synchronizedB. volatileC. transientD. strictfp答案:A11. 在Java中,哪个关键字用于声明一个静态方法?A. staticB. finalC. abstractD. synchronized答案:A12. 在Java中,哪个关键字用于声明一个常量?A. constB. finalC. volatileD. transient答案:B13. 在Java中,哪个关键字用于声明一个线程安全的类?A. synchronizedB. volatileC. synchronizedD. threadsafe答案:C14. 在Java中,哪个关键字用于声明一个内部类?A. innerB. nestedC. staticD. inner class答案:B15. 在Java中,哪个关键字用于声明一个泛型?A. genericB. templateC. typeD. <>答案:D16. 在Java中,哪个关键字用于声明一个枚举?A. enumB. enumerationC. listD. set答案:A17. 在Java中,哪个关键字用于声明一个注解?A. annotationB. @interfaceC. declareD. define答案:B18. 在Java中,哪个关键字用于声明一个单例模式?A. singletonB. uniqueC. instanceD. private答案:D19. 在Java中,哪个关键字用于声明一个线程?A. threadB. runnableC. executeD. task答案:B20. 在Java中,哪个关键字用于声明一个方法重载?A. overloadB. overloadingC. overloadD. method答案:C二、多项选择题(每题3分,共30分)21. 下列哪些是Java中的基本数据类型?A. byteB. shortC. intD. longE. floatF. doubleG. charH. boolean答案:A, B, C, D, E, F, G, H22. 下列哪些是Java中的集合框架接口?A. ListB. SetC. MapD. QueueE. Stack答案:A, B, C, D23. 下列哪些是Java中的异常处理关键字?A. tryB. catchC. finallyD. throwE. throws答案:A, B, C, D, E24. 下列哪些是Java中的访问修饰符?A. publicB. privateC. protectedD. defaultE. internal25. 下列哪些是Java中的控制流语句?A. ifB. elseC. switchD. whileE. forF. do-while答案:A, B, C, D, E, F26. 下列哪些是Java中的泛型类型?A. List<String>B. Map<Integer, String>C. Set<Character>D. Queue<Number>27. 下列哪些是Java中的注解?A. @OverrideB. @DeprecatedC. @SuppressWarningsD. @Test答案:A, B, C, D28. 下列哪些是Java中的I/O流?A. FileInputStreamB. FileOutputStreamC. FileReaderD. FileWriterE. BufferedInputStream答案:A, B, C, D, E29. 下列哪些是Java中的多线程关键字?A. synchronizedB. volatileC. waitD. notifyE. join答案:A, B, C, D, E30. 下列哪些是Java中的网络编程类?A. SocketB. ServerSocketC. URLD. HttpURLConnectionE. DatagramSocket答案:A, B, C, D, E三、填空题(每题4分,共20分)31. Java中,用于定义一个类的关键字是______。
请用Eclipse编写代码,完成以下题目。
1.编写程序,输出斐波那契数列的前10个数。
运行结果如图1所示。
提示:斐波那契数列数字排列规律为:1,1,2,3,5,8,13,21……
图1 斐波那契数列运行结果
2.编写程序,实现猜数小游戏。
随机生成一个0~99(包括0和99)的数字,从控制台输
入猜测的数字,输出提示太大还是太小,继续猜测,直到猜到为止,游戏过程中,记录猜对所需的次数,游戏结束后公布结果。
程序运行结果如图2所示。
猜测次数与游戏结果对照见表1所示。
表1 猜测次数与游戏结果对照表
提示:产生
图2 猜数游戏运行结果
要求:代码规范,有注释,能正确运行出结果,.java源文件打包后,以自己名字命名提交。
一、选择题1.两个关系在没有公共属性时,共自然连接操作表现为 ()A结果为空关系B无意义的操作C等值连接操作D笛卡尔操作2.一个栈的入栈序列是1,2,3,…,n,其输出序列为P1,P2,P3,…,Pn,若P1=n,则P1为 ()A.iB.n=iC.n-i+1D.不确定3. 用黑盒技术设计测试用例的方法之一为 ()A.因果图B.逻辑覆盖C.循环覆盖D.基本路径测试4. 在数据库设计的四个阶段中,为关系模式选择存取方法(建立存取路径)应该是在()阶段。
A.需求分析B.概念设计C.逻辑设计D.物理设计6. 在关系数据库模型中,通常可以把()称为属性,其值称为属性值。
A.记录B.基本表C.模式D.字段7. 结构化程序设计的一种基本方法是 ()A.筛选法B.递归法C.归纳法D.逐步求精法8. 源程序中应包含一些内部文档,以帮助阅读和理解源程序,源程序的内部文档通常包括选择合适的标识符、注解和 ()A.程序的视觉组织B.尽量不用或少用GOTO语句C.检查输入数据的有效性D.设计良好的输出报表9. 软件详细设计的主要任务是确定每个模块的 ()A.算法和使用的数据结构B.外部接口C.功能D.编程10. 在C+ +程序中,对象之间的相互通信通过 ()A.继承实现B.调用成员函数实现C.封装实现D.函数重载实现11.下面说法哪些是正确的( )A.Applet可以访问本地文件B.对static方法的调用需要类实例C.socket类在ng中D.127.0.0/1地址代表本机12.下面说法不正确的是( )A.Java中线程是抢占式的B.Java中线程是分时的C.Java中的线程可以共享数据D.Java中的线程可以共享代码13.下面属于Java线程同步方法的方法有( )A.joiny()B.run()C.wait()D.destroy()14.下列哪个方法可用于创建一个可运行的类( )A.public class X implements Runable{ public void run(){......} }B.public class X implements Thread{ public void run(){......} }C.public class X implements Thread{ public int run(){......} }D.public class X implements Runable{ protected void run(){......} }15.下列说法不正确的是( )A.IOException必须被捕获或抛出B.java语言会自动初始化成员变量的值C.java语言不允许同时继承一个类并实现一个接口D.java语言会自动回收内存中的垃圾16.Java程序的执行过程中用到一套JDK工具,其中java.exe是指( )A.Java文档生成器B.Java解释器C.Java编译器D.Java类分解器17.Java语言中,下列标识符错误的是( )A.—sys1B.&—mC.ID.40name18.在Java中,属于整数类型变量的是( )A.singleB.doubleC.byteD.char19.Applet类的直接父类是( )ponent类B.Container类C.Frame类D.Panel类20.Frame的默认的布局管理器是下列哪一个( )A.FlowLayoutB.BorderLayoutC.GridLayoutD.CardLayout21.在下列事件处理机制中哪个不是机制中的角色( )A.事件B.事件源C.事件接口D.事件处理者22.下列语句片段int a=10,b=4,c=20,d=6;System.out.println(a++*b+c*--d);的结果为( )A.144B.28C.140D.不能执行23.下列语句片段:int a=-67,b=116,c=78;int d=~a|b&c;System.out.println(d)的结果为( )A.70B.67C.78D.5624.对象使用时,下面描述错误的是( )A.通过“.”运算符调用成员变量和方法B.通过成员变量的访问权限设定限制自身对这些变量方法的调用C.将一个对象申明为类的成员时,必须在使用前为其分配内存D.在方法中使用对象作为参数时,采用引用调用25.执行下列代码后,哪个结论是正确的String[] s=new String[10];A.s[10]为″″B.s[9]为nullC.s[0]为未定义D.s.length为10126.Java编程所必须的默认引用包为( )A.java.sys包ng包C.java.new包D.以上都不是27.定义一个类名为“MyClass.java”的类,并且该类可被一个工程中的所有类访问,那么该类的正确声明应为:( )A.private class MyClass extends ObjectB.class MyClass extends ObjectC.public class MyClassD.private class MyClass extends Object28.内部类是在一个类内嵌套定义的类。
57. 在Java语言中,类Student是类Person的子类,Student的构造方法中有一句"super()",该语句表达了什么概念?(选择1项)a. 调用类Worker中定义的super()方法b. 调用类Person中定义的super()方法c. 调用类Person的构造方法d. 语法错误58. 现在需要建立自制类MyClass,同时需要将此类放入自制包MyPackage,正确的声明方法为?(选择1项)a. public class MyPackage.MyClass{}b. public class MyClass in MyPackage{}c. package MyPackage;public class MyClass{}d. public package MyPackage;class MyClass{}65、Hashtable实现以下哪个接口?A. java.util.MapB. java.util.ListC. java.util.HashableD. java.util.Collection66、下面关于集合的说法正确的是?A. List 接口继承了 Collection 接口以定义一个不允许重复项的有序集合。
B. ArrayList 和 LinkedList是List接口的实现类。
C. 要支持随机访问,选择LinkedList类较好,而顺序的访问列表元素使用ArrayList类更好。
D. Set 接口继承 Collection 接口,而且它允许集合中存在重复项。
67、下面关于ArrayList的说法正确的是?A.ArrayList()可以构造一个初始容量为 5 的空列表。
B.ArrayList初始化时用户可以自定义ArrayList对象的初始容量。
C.ArrayList对象中只能存放同一数据类型的数据。
D.ArrayList对象中不能存放NULL。
Given:1. public class Base{2. public static final String FOO = "foo";3. public static void main(String[] args){4. Base b = new Base();5. Sub s = new Sub();6. System.out.print(Base.FOO);7. System.out.print(Sub.FOO);8. System.out.print(b.FOO);9. System.out.print(s.FOO);10. System.out.print(((Base)s).FOO); //S变为父类,调用父类的函数11. }12. }13. class Sub extends Base{public static final String FOO = "bar";} //子类特有,与父类相同,把父类隐藏What is the result?A. foofoofoofoofooB. foobarfoobarbarC. foobarfoofoofooD. foobarfoobarfooE. barbarbarbarbarF. foofoofoobarbarG. foofoofoobarfoo第32題ABCFGiven:1. class Mammal{}2.3. class Raccoon extends Mammal4. Mammal m = new Mammal();5. }6.7. class BabyRaccoon extends Mammal{}Which four statments are true? (Choose four.)A. Raccoon is-a Mammal.B. Raccoon has-a Mammal.C. BabyRaccoon is-a Mammal.D. BabyRaccoon is-a Raccoon.E. BabyRaccoon has-a Mammal.F. BabyRaccoon is-a BabyRaccoon.Given: //方法重写修饰符不能更私有2. public class Hi{3. void m1(){}4. protected void m2(){}5. }6. class Lois extends Hi{7. //insert code here8. }Which four code fragments, inserted independently at line 7, will compile? (Choose four.)A. public void m1(){}B. protected void m1(){}C. private void m1(){}D. void m2(){}E. pubic void m2(){}F. protected void m2(){}G. private void m2(){}第34題ACDGWhich four statements are true? (Choose four.)A. Has-a relationships should never be encapsulated(封装).B. Has-a relationships should be implemented using inheritance.C. Has-a relationships can be implemented using instance variables.D. Is-a relationships can be implemented using the extends keyword.E. Is-a relationships can be implemented using the implements (继承)keyword.F. The relationship between Movie and Actress is an example of an is-a relationship.G. An array or a collection can be used to implement a one-to-many has-a relationship.第35題 EGiven:10. public class Hello{11. String title;12. int value;13. public Hello(){ //构造器14. title += " World";15. }16. public Hello(int value){17. this.value = value;18. title = "Hello";19. Hello(); //构造器不能调用可以用super 和this关键字调用20. }21. }and:30. Hello c = new Hello(5);31. System.out.print(c.title);What is the result?A. HelloB. Hello WorldC. Compilation fails.D. Hello World 5E. The code runs with no output.F. An exception is thrown at runtime.第36題Given:public class Doubler{public static int doubleMe(Holder h){return h.getAmount() * 2;}}and:public class Holder {int amount = 10;public void doubleAmount(){amount = Doubler.doubleMe(this);}public in getAmount(){return amount;}//more code here}Place the code framgmets in position to reduce the coupling between Doubler and Holder. public class Doubler{public static int doubleMe( Place here h){ //intreturn Place here * 2; //h}}public class Holder {int amount = 10;public void doubleAmount(){amount = Doubler.doubleMe( Place here );}//amount public in getAmount(){return amount;}//more code here}Code Fragmentsvoid Holder int Doublerh.getAmount() h this amount第37題 DGiven:21. abstract class C1{22. public C1(){System.out.print(1);}23. }24. class C2 extends C1{25. public C2(){System.out.print(2);}26. }27. class C3 extends C2{28. public C3(){System.out.print(3);}29. }30. public class Ctest{31. public static void main(String[] a){new C3();}32. }What is the result?A. 3B. 23C. 32D. 123E. 321F. Compilation fails.G. An exception is thrown at runtime.第38題CDGiven:1. class One{2. public One foo(){return this;}3. }4. class Two extends One{5. public One foo(){return this;}6. }7. class Three extends Two{8. //insert method here9. }Which two methods, inserted individually, correctly complete the Three class? (Choose two.)A. public void foo(){}B. public int foo(){return 3;}C. public Two foo(){return this;}D. public One foo(){return this;}E. public Object foo(){return this;}第39題ABCEGiven:11. public class ItemTest{12. private final int id;13. public ItemTest(int id){this.id = id;}14. public void updateId(int newId){id = newId;} //方法内不能给final直接赋值15.16. public static void main(String[] args){17. ItemTest fa = new ItemTest(42);18. fa.updateId(69);19. System.out.println(fa.id);20. }21. }Which four statments are true? (Choose four.)A. Compilation fails.B. An exception is thrown at runtime.C. The attribute id in the ItemTest object remains unchanged.D. The attribute id in the ItemTest object is modified to the new value.E. A new ItemTest object is created with the preferred value in the id attribute.第40題 BGiven:1. class Foo{2. private int x;3. public Foo(int x){this.x = x;}4. public void setX(int x){this.x = x;}5. public int getX(){return x;}6. }7.8. public class Gamma{9. static Foo fooBar(Foo foo){10. foo = new Foo(100);11. return foo;12. }13. public static void main(String[] args){14. Foo foo = new Foo(300);15. System.out.print(foo.getX() + "-");16.17. Foo fooFoo = fooBar(foo);18. System.out.print(foo.getX() + "-");19. System.out.print(fooFoo.getX() + "-");20.21. foo = fooBar(fooFoo);22. System.out.print(foo.getX() + "-");23. System.out.print(fooFoo.getX());24. }25. }What is the output?A. 300-100-100-100-100B. 300-300-100-100-100C. 300-300-300-100-100D. 300-300-300-300-100第41題 DGiven:1. public class KungFu{2. public static void main(String[] args){3. Integer x = 400; //integer 在-128~127以外new4. Integer y = x;5. x++;6. StringBuilder sb1 = new StringBuilder("123");7. StringBuilder sb2 = sb1; //s1 new了一个新空间s1 s2同时指向这个空间.method后new空间的值变化二者同时变8. sb1.append("5");9. System.out.println((x == y) + " " + (sb1 == sb2));//s1只要变s2跟着变10. }11. }What is the result?A. true trueB. false trueC. true falseD. false falseE. Compilation fails.F. An exception is thrown at runtime.第42題1. class A{2. public String doit(int x, int y){3. return "a";4. }5.6. public String doit(int... vals){7. return "b";8. }9. }Given:25. A a = new A();26. System.out.println(a.doit(4, 5));What is the result?A. Line 26 prints "a" to System.out.B. Line 26 prints "b" to System.out.C. An exception is thrown at runtime.D. Compilation of class A will fail due to an error in line 6.第43題 DGiven:1. class Plant{2. private String name;3. public Plant(String name){ = name;}4. public String getName(){return name;}5. }6. public class Tree extends Plant{7. public void growFruit(){}8. public void dropLeaves(){}9. }What statement is true?A. The code will compile without changes.B. The code will compile if public Tree(){Plant();} is added to the Tree class.C. The code will compile if public Plant(){Tree();} is added to the Plant class.D. The code will compile if public Plant(){this("fern");} is added to the Plant class.E. The code will compile if public Plant(){Plant("fern");} is added to the Plant class.第44題AEGiven:1. class Employee{2. String name; double baseSalary;3. public Employee(String name, double baseSalary){4. = name;5. this.baseSalary = baseSalary;6. }7. }8. public class SalesPerson extends Employee{9. double commission;10. public SalesPerson(String name,11. double baseSalary, double commission){12. //insert code here13. }14. }Which two code fragments, inserted independently at line 12, will compile? (Choose two.)A. super(name, baseSalary);B. mission = commission;C. super();mission = commission;D. mission = commission;super();E. super(name, baseSalary);mission = commission;F. mission = commission;super(name, baseSalary);G. super(name, baseSalary, commission);第45題ACGiven that:Gadget has-a Sprocket andGadget has-a Spring andGadget is-a Widget andWidget has-a SprocketWhich two code fragments represent these relationships? (Choose two.)A. class Widget{Sprocket s;}class Gadget extends Widget{Spring s;}B. class Widget{}class Gadget extends Widget{Spring s1; Sprocket s2;}C. class Widget{Sprocket s1; Spring s2;}class Gadget extends Widget{}D. class Gadget{Spring s;}class Widget extends Gadget{Sprocket s;}E. class Gadget{}class Widget extends Gadget{Sprocket s1; Spring s2;}F. class Gadget{Spring s1; Sprocket s2;}class Widget extends Gadget{}第46題 CGiven:10. public class Pizza{11. ArrayList toppings;12.13. public final void addTopping(String topping){14. toppings.add(topping);15. }16.17. public void removeTopping(String topping){18. toppings.remove(topping);19. }20. }And:30. class PepperoniPizza extends Pizza{31. public void addTopping(String topping){32. System.out.println("Cannot add Toppings");33. }34.35. public void removeTopping(String topping){36. System.out.println("Cannot remove pepperoni");37. }38. }And:50. Pizza pizza = new PepperoniPizza();51. pizza.addTopping("Mushrooms");52. pizza.removeTopping("Pepperoni");What is the result?A. Compilation fails.B. Cannot add ToppingsC. The code runs with no output.D. A NullPointerException is thrown in Line 4.第48題 EGiven:1. public class Venus{2. public static void main(String[] args){3. int[] x = {1, 2, 3};4. int y[] = {4, 5, 6};5. new Venus().go(x, y);6. }7. void go(int[]... z){ //代表多个一维数组8. for(int[] a : z)9. System.out.print(a[0]);10. }11. }What is the result?A. 1B. 12C. 14D. 123E. Compilation fails.F. An exception is thrown at runtime.第49題Place code framgmets into position so the output is: The quantity is 420 Place here update(int quantity, int adjust){Place here}public void callUpdate(){int quant = 100;Place hereSystem.out.println("The quantity is " + quant);}Code Fragmentspublic int quantity = quantity + adjust; update(quant, 320); public void quant = update(quant, 320);quantity = quantity + adjust;return quantity;第50題DEGiven:1. public abstract class Shape{2. private int x;3. private int y;4. public abstract void draw();5. public void setAnchor(int x, int y){6. this.x = x;7. this.y = y;8. }9. }Which two classes use the Shape class correctly? (Choose two.)A. public class Circle implements Shape{private in radius;}B. public abstract class Circle extends Shape{private in radius;}C. public class Circle extends Shape{private in radius;public void draw();}D. public abstract class Circle implements Shape{private in radius;public void draw();}E. public class Circle extends Shape{private in radius;public void draw(){/* code here */}}F. public abstract class Circle implements Shape{private in radius;public void draw(){/* code here */}}第51題 AGiven:1. public interface A{2. public void doSomething(String thing);3. }1. public class AImpl implements A{2. public void doSomething(String msg){}3. }1. public class B{2. public A doit(){3. //more code here4. }5.6. public String execute(){7. //more code here8. }9. }1. public class C extends B{2. public AImpl doit(){3. //more code here4. }5.6. public Object execute(){7. //more code here8. }9. }Which statement is true about the classes and interfaces?A. Compilation will succeed for all classes and interfaces.B. Compilation of class C will fail because of an error in line 2.C. Compilation of class C will fail because of an error in line 6.D. Compilation of class AImpl will fail because of an error in line 2.第54題 AGiven:1. package test;2.3. class Target{4. public String name = "hello";5. }What can directly access and change the value of the variable name?A. any classB. only the Target classC. any class in the test packageD. any class that extends Target第55題 DGiven:11. abstract class Vehicle{public int speed(){return 0;}}12. class Car extends Vehicle{public int speed(){return 60;}}13. class RaceCar extends Car{public int speed(){return 150;}} ...21. RaceCar racer = new RaceCar();22. Car car = new RaceCar();23. Vehicle vehicle = new RaceCar();24. System.out.println(racer.speed() + ", " + car.speed()25. + ", " + vehicle.speed());What is the result?A. 0, 0, 0B. 150, 60, 0C. Compilation fails.D. 150, 150, 150 、//徐方法调用E. An exception is thrown at runtime.第56題 CGiven:5. class Building{}6. public class Barn extends Building{7. public static void main(String[] args){8. Building build1 = new Building();9. Barn barn1 = new Barn();10. Barn barn2 = (Barn)build1;11. Object obj1 = (Object)build1;12. String str1 = (String)build1;13. Building build2 = (Building)barn1;14. }15. }Which is true?A. if line 10 is removed, the compilation succeeds.B. if line 11 is removed, the compilation succeeds.C. if line 12 is removed, the compilation succeeds.D. if line 13 is removed, the compilation succeeds.E. More than one line must be removed for compilation to succeed.第57題 DGiven:21. class Money{22. private String country = "Canada";23. public String getC(){return country;}24. }25. class Yen extends Money{26. public String getC(){return super.country;} //country是父类私有的不能调用只能调用方法27. }28. public class Euro extends Money{29. public String getC(){return super.getC();}30. public static void main(String[] args){31. System.out.print(new Yen().getC() + " " + new Euro().getC());32. }33. }What is the result?A. CanadaB. null CanadaC. Canada nullD. Canada CanadaE. Compilation fails due to an error on line 26.F. Compilation fails due to an error on line 29.第58題 BGiven:10. interface Foo{}11. class Alpha implements Foo{}12. class Beta extends Alpha{}13. class Delta extends Beta{14. public static void main(String[] args){15. Beta x = new Beta();16. //insert code here17. }18. }Which code, inserted at line 16, will cause a ng.ClassCastException?A. Alpha a = x;B. Foo f = (Delta)x; //不能转换为子类C. Foo f = (Alpha)x;D. Beta b = (Beta)(Alpha)x;第59題 DGiven the following directory structure:bigProject //包|--source //| |--Utils.java||--classes|And the following command line invocation:javac –d classes source/Utils.javaAssume the current directory is bigProject, what it the result?A. If the compile is successful, Utils.class is added to the source directory.B. The compiler returns an invalid flag error.C. If the compile is successful, Utils.class is added to the classes directory.D. If the compile is successful, Utils.class is added to the bigProject directory.第61題 CGiven two files, GrizzlyBear.java and Salmon.java:1. package animals.mammals;2.3. public class GrizzlyBear extends Bear{4. void hunt() {5. Salmon s = findSalmon();6. s.consume();7. }8. }1. package animals.fish;2.3. public class Salmon extends Fish {4. public void consume() { /* do stuff */ }5. }If both classes are in the correct directories for their packages, and the Mammal class correctly defines the findSalmon() method, which change allows this code to compile?A. add import animals. mammals.*; at line 2 in Salmon.javaB. add import animals.fish.*; at line 2 in GrizzlyBearjavaC. add import animals.fish.Salmon.*; at line 2 in GrizzlyBear.javaD. add import animals. mammals.GrizzlyBear*; at line 2 in Salmon.java第62題 CGiven:31. class Foo{32. public int a = 3;33. public void addFive(){ a += 5; System.out.print("f "); }34. }35. class Bar extends Foo{36. public int a = 8;37. public void addFive(){this.a += 5; System.out.print("b ");} //this调用当前的a=838. }Invoked with:Foo f = new Bar();f.addFive();System.out.println(f. a);What is the result?A. b 3B. b 8C. b 13D. f 3E. f 8F. f 13G. Compilation fails,H. An exception is thrown at runtime.第63題CEFGiven:11. class ClassA{}12. class ClassB extends ClassA{}13. class ClassC extends ClassA{}and:21. ClassA p0 = new ClassA();22. ClassB p1 = new ClassB();23. ClassC p2 = new ClassC();24. ClassA p3 = new ClassB();25. ClassA p4 = new ClassC();Which three are valid? (Choose three.)A. p0 = p1; //对象之间的赋值B. p1 = p2,C. p2 = p4;D. p2 = (ClassC)p1;E. p1 = (ClassB)p3;F. p2 = (ClassC)p4;第64題AA BAGiven:class A {String name = "A";String getName() {return name;}String greeting(){return "class A";}}class B extends A {String name = "B";String greeting() {return "class B";}}public class Client {public static void main( String[] args ) {A a = new A();A b = new B();System.out.println(a.greeting() + "has name" + a.getName());System.out.println(b.greeting() + "has name" + b.getName());}}Place the names "A" and "B" in the following output Namesclass Place here has name Place hereclass Place here has name Place hereNamesA B第65題Replace two of the Modifiers that appear in the Single class to make the code compile.Note: Three modifiers will not be used and four modifiers in the code will remain unchanged.Codepublic class Single {private static Single instance;public static Single getInstance() {if (instance == null) instance = create(); //静态方法必须调用静态方法return instance;}private Single() { } //PROTECTEDprotected Single create() { return new Single(); } //+static}class SingleSub extends Single {}Modifiersfinalprotectedprivateabstractstatic第66題AFGiven:5. class Thingy{ Meter m = new Meter(); }6. class Component {void go() { System.out.print("c");}}7. class Meter extends Component {void go() { System.out.print("m"); }}8.9. class DeluxeThingy extends Thingy {10. public static void main(String[] args) {11. DeluxeThingy dt = new DeluxeThingy();12. dt.m.go();13. Thingy t = new DeluxeThingy();14. t.m.go();15. }16. }Which two are true? (Choose two.)A. The output is mm.B. The output is mc.C. Component is-a Meter.D. Component has-a Meter.E. DeluxeThingy is-a Component.F. DeluxeThingy has-a Component.第67題ABGiven:10. abstract public class Employee {11. protected abstract double getSalesAmount();12. public double getCommision() {13. return getSalesAmount() * 0.15;14. }15. }16. class Sales extends Employee {17. //insert method here18. }Which two methods, inserted independently at line 17, correctly complete the Sales class? (Choose two.)A. double getSalesAmount() { return 1230.45; }B. public double getSalesAmount() { return 1230.45; }C. private double getSalesAmount() { return 1230.45; }D. protected double getSalesAmount() { return 1230.45; }第69題ABDGiven11. public interface Status {12. /* insert code here */ int MY_VALUE = 10;13. }Which three are valid on line 12? (Choose three.)A. finalB. staticC. nativeD. publicE. privateF. abstract C. protected。