摩卡笔试题.pdf
1:1.Give this class outline:2.class Example3.private int x;4./rest of class body.5.)6.Assuming that x invoked by the code java Example,which statement can made x be directly accessible in main()method of Example.java?J A.Change private int x to public int xJ B.change private int x to static int xnJ C.Change private int x to protected int xJ D.change private int x to final int x2:Which of the following answer is correct to express the value 8 in octal number?e A.010C B.OxIO口 C.08C D.0 x83:在下述选项时,没有构成死循环的程序是C A.int i=100 while(1)i=i%100+1;if(i100)break;C B.for区 C.int k=1000;do +k;while(k=10000);D.int s=36;while(s);-s;4:1.Give the following java class:2.public class Example3.static int x=new int15;4.public static void main(String args)5.System.out.println(x5);6.)7.)8.Which statement is corrected?A.When compile,some error will occur.B.When run,some error will occur.C.Output is zer|.0.Output is null.1.What will happen when you attempt to compile and run the following code?2.3.(Assume that the code is compiled and run with assertions enabled.)4.5.public class AssertTest6.7.public void methodA(int i)8.9.assert i=0:methodB();10.11.System.out.println(i);12.13.14.15.public void methodB()16.17.System.out.println(The value must not be negative);18.19.20.21.public static void main(String args)22.23.AssertTest test=new AssertTest();24.25.test.methodA(-10);26.27.28.29.A.it will print-10B.it will result in Assertion Error showing the messageJtthe value must not be negative.C.the code will not compile.D.None of these.1.1.public class X 2.2.public object m()3.3.object o=new float(3.14F);4.4.object oa=new object 1;5.5.oa0=o;6.6.o=null;7.7.oa0=null;8.8.return o;9.9.10.10.11.When is the float object created in line 3,eligible for garbage collection?cA.Just after line 5B.Just after line 60 C.Just after line 7J D.Just after line 8(that is,as the method returns)7:A class design requires that a member variable should be accessible only by same package,which modifer word should be used?J A.protected,B.publicJ C.no modifer-D.private8:Which statements about Java code security are not true?nJ A.The bytecode verifier loads all classes needed for the execution of a program.nJ B.Executing code is performed by the runtime interpreter.nJ C.At runtime the bytecodes are loaded,checked and run in an interpreter.n D.The class loader adds security by separating the namespaces for the classes of the localfile system from those imported from network sources.9:设有变量说明语句int a=1,b=0;则执行以下程序段的输出结果为()。switch(a)(case 1:switch(b)(case 0:printf(*0*H);break;case 1 :printf(*r*);break;)case2:printf(*2*);break;)printf(un);A.*0*B*Q*2*cQ*0*2*D.有语法错误10:Which method you define as the starting point of new thread in a class from which new thethread can be excution?J A.public void start()B.public void run()pJ C.public void runnable()nJ D.public static void main(String args)11:1.下述程序代码中有语法错误的行是()。2.inti,ia10,ib10;/*第一行*/3.for(i=0;i=9;i+)/*第 2 行*/4.iai=0;/*第 3 行*/5.ib=ia;/*第 4 行*/A.第1行B.第2行C.第3行D.第4行12:鉴于Java的特点,它最适合的计算环境是口 A.并行计算环境C B.分布式计算环境C c.高强度计算环境C D.开放式计算环境13:假 定a和b为int型变量,则执行下述语句组后,b的值为a=1;b=10;do(b-=a;a+;while(b-=0:methodBO;System.out.println(i);public void methodB0System.out.println(nThe value must not be negative);public static void main(String args)AssertTest test=new AssertTestOitest.methodA(-lO);)What will happen when you attempt to compile and run the following code?(Assume that the code is compiled and run with assertions enabled.)public class AssertTest(public void methodA(int i)assert i=0 methodBO;System.out.println(i);p u b l i c v o i d m e t h o d B()S y s t e m.o u t.p r i n t l n(T h e v a l u e m u s t n o t b e n e g a t i v e);p u b l i c s t a t i c v o i d m a i n(S t r i n g a r g s )As s e r t T e s t t e s t =n e w As s e r t T e s t O;t e s t.m e t h o d A(-l 0);j A.i t w i l l p r i n t -10J B.i t w i l l r e s u l t i n As s e r t i o n Er r o r s h o w i n g t h e m e s s a g e-ut h e v a l u e m u s t n o t b en e g a t i v e”.J C.t h e c o d e w i l l n o t c o m p i l e.i D.No n e o f t h e s e.4:In t h e f o l l o w i n g c o d e,w h i c h i s t h e e a r l i e s t s t a t e m e n t,w h e r e t h e o b j e c t o r i g i n a l l y h e l d in e,m a y b e g a r b a g e c o l l e c t e d-1.p u b l i c c l a s s T e s t 2.p u b l i c s t a t i c v o i d m a i n (S t r i n g a r g s )3.Em p l o y e e e =n e w Em p l o y e e(,Bo bM,48);4.e.c a l c u l a t e P a y O;5.System.out.println(e.printDetailsO)j6.e=null;7.e=new EmployeeCDenise,36);8.e.calculatePayO;9.System.out.println(e.printDetailsO)J10.11.)Only One-In the following code,which is the earliest statement,where the object originally held ine,may be garbage collected-1.public class Test 2.public static void main(String args)3.Employee e=new Employee(nBobH,48);4.e.calculatePayO,5.System.out.println(e.printDetailsO);6.e=null;7.e=new Employee(HDenisen,36);8.e.calculatePayO?9.System.out.println(e.printDetailsO);10.11.)Only One-J A.Line 10C B.Line 11C C.Line 7C D.Line 85:The following code is entire contents of a file called Example.java,causes precisely oneerror during compilationclass SubClass extends BaseClassclass BaseClass()String str;public BaseClass()System.out.println(aok,)jpublic BaseClass(String s)str=s;public class Example(public void method0SubClass s=new SubClass(hello);BaseClass b=new BaseClass(world);Which line would be cause the error?The following code is entire contents of a file called Example.java,causes precisely oneerror during compilation:class SubClass extends BaseClass)class BaseClass()String str;p u b l i c Ba s e Cl a s s()S y s t e m.o u t.p r i n t l n(uo k,);)p u b l i c Ba s e Cl a s s(S t r i n g s)s t r=s;p u b l i c c l a s s Ex a m p l e!p u b l i c v o i d m e t h o d O(S u b Cl a s s s=n e w S u b Cl a s s Ch e l l o);Ba s e Cl a s s b=n e w Ba s e Cl a s s(w o r l d);W h i c h l i n e w o u l d b e c a u s e t h e e r r o r?C A.9J*B.10口 C.UC D.126-W h a t i s w r i t t e n t o t h e s t a n d a r d o u t p u t g i v e n t h e f o l l o w i n gs t a t e m e n t-S y s t e m.o u t.p r i n t l n(4|7);S e l e c t t h e r i g h t a n s w e r*C A.4C B.5C C.6 D,77:S e l e c t v a l i d i d e n t i f i e r o f Ja v a-S e l e c t v a l i d i d e n t i f i e r o f Ja v a-A.%p a s s w dB.3d _ g a m eJ C.$c h a r g eo D.t h i s8:关于垃圾收集的哪些叙述是对的。C A.程序开发者必须自己创建一个线程进行内存释放的工作。C B.垃圾收集将检查并释放不再使用的内存。c.垃圾收集允许程序开发者明确指定并立即释放该内存。C D.垃圾收集能够在期望的时间释放被j a v a对象使用的内存。9:A c l a s s d e s i g n r e q u i r e s t h a t a p a r t i c u l a r m e m b e r v a r i a b l e m u s t b e a c c e s s i b l e f o r d i r e c ta c c e s s b y a n y s u b c l a s s e s o f t h i s c l a s s,b u t o t h e r w i s e n o t b y c l a s s e s w h i c h a r e n o tm e m b e r s o f t h e s a m e p a c k a g e.W h a t s h o u l d b e d o n e t o a c h i e v e t h i s?J A.T h e v a r i a b l e s h o u l d b e m a r k e d p u b l i cJ B.T h e v a r i a b l e s h o u l d b e m a r k e d p r i v a t eJ C.T h e v a r i a b l e s h o u l d b e m a r k e d p r o t e c t e dJ D.T h e v a r i a b l e s h o u l d h a v e n o s p e c i a l a c c e s s m o d i f i e r10:W h a t w i l l h a p p e n w h e n y o u a t t e m p t t o c o m p i l e a n d r u n t h e f o l l o w i n g c o d e?c l a s s Ba s ei n t i =99;p u b l i c v o i d a m e t h o d OSystem.out.println(,Base.amethodOn)JBaseOamethodO;public class Derived extends Baseint i=-1;public static void main(String argv)Base b=new Derived。;System.out.println(b.i);b.amethodO;public void amethodOSystem.out.println(HDerived.amethod()n);)Choices-What will happen when you attempt to compile and run the following code?class Baseint i=99;public void amethodO(System.out.println(Base.amethodO”);BaseOamethodO;public class Derived extends Baseint i=-1;public static void main(String argv)Base b=new Derived。;System.out.println(b.i);b.amethodO;public void amethodOSystem.out.println(nDerived.amethodO)JChoices-cA.Derived.amethodO _1 Derived.amethodOB.Derived.amethodO 99J C.Compile time errorJ D.Derived.amethodO11:Give the following method:public void method()String a,b;a=new String(a=null;a=b;System.out.println(a);)In the absence of compiler optimization,which is the earliest point the object a referedis definitely elibile to be garbage collection.C A.before line 5cB.before line 6C.before line 7J D.before line 912:以下的C程序代码片段运行后C和d的值分别是多少Int a=l,b=2;Int c,d;c=(a&b)&a;d=(a&b)&a;C A.0,0C B.0,1C c.i.oC D.1,113:What is the result when you compile and run the following code?public class ThrowsDemostatic void throwMethodOSystem.out.println(Inside throwMethod.n)Jthrow new IllegalAccessException(,demo,)public static void main(String args)trythrowMethodO;catch(IllegalAccessException e)System.out.println(Caught +e);Choices-What is the result when you compile and run the following code?public class ThrowsDemostatic void throwMethodOSystem.out.printlnClnside throwMethod.”);throw new IllegalAccessException(ndemoH)Jpublic static void main(String args)trythrowMethodO,catch(IllegalAccessException e)System.out.println(nCaught n+e);)Choices-J A.Compilation errorB.Runtime errorC.Compile successfully,nothing is printed.nJ D.Inside throwMethod.followed by caught-java.lang.IllegalAccessExcption-demo14:在软件生命周期中,下列哪个说法是不准确的?口 A.软件生命周期分为计划、开发和运行三个阶段C B.在计划阶段要进行问题焉醛和需求分析C C.在开发后期要进行编写代码和软件测试C D.在运行阶段主要是进行软件维护15:在下述选项时,没有构成死循环的程序是0 A.int i=100 while(1)i=i%100+1;if(i100)break;0 B.for(;);J C.int k=1000;do +k;while(k=10000);0 D.int s=36;while(s)L s;16:1.public class X 2.public object m 0 3.object o=new float(3.14F);4.object oa=new object 1;5.oa0=o;6.o=null;7.oa0=null;8.return o;9.)10.When is the float object created in line 3,eligible for garbage collection?1.public class X 2.public object m()3.object o=new float(3.14F);4.object oa=new object 1;5.oa0=o;6.o=null;7.oa0=null;8.return o;9.)10.)When is the float object created in line 3,eligible for garbage collection?J A.Just after line 50 B.Just after line 60 C.Just after line 7J D.Just after line 8(that is,as the method returns)简答题17:STRING 与 STRINGBUFFER 的区别。18:Is Tbmcat faster than serving static HTML pages than Apache httpd?19:一个类只有实现了 Serializable接口或者Externalizable接口,它的对象才可以被序列化。请问,这两种序列化机制有何区别?20:编写一个确定一字符串在另一字符串中出现次数的算法。例如字符串“this”在字符串“thisis my first program,this”中出现了 2次,不要使用库函数(方法)。21:Java中的异常处理机制的简单原理和应用。22:说说你对面向切面编程(AOP)的理解。23:请设计一个Java程序,程序中要进行数组操作和除法操作,要求对所设计的程序可能出现的异常进行处理。24:什么是异常?试列出三个系统定义的运行时异常类。25:多线程有哪些状态?JAVA认证真题:60道SCJP考试真题精解 1.Which of the following range of short is correct?A.-27-27-1B.0-216-1C.?215-215-1D.?231-231-1翻译下面哪些是short型的取值范围。答 案 C解析 短整型的数据类型的长度是16 bits,有符号。另外需要说明的是java中所有的整(Integral)数(包括 byte,short,int,long)全是有符号的。2.Which declarations of identifiers are legal?A.$personsB.Two UsersC.*pointD.thisE _endline翻译下面哪些是合法的标识符。答 案 A,B,E解 析 Java的标识符可以以一个Unicode字符,下滑线(J,美元符($)开始,后续字符可以是前面的符号和数字,没有长度限制,大小写敏感,不能是保留字。3.Which statement of assigning a long type variable to a hexadecimal value is correct?A.long number=345L;B.long number=0345;C.long number=0345L;D.long number=0 x345L翻译哪些是将一个十六进制值赋值给一个long型变量。答 案 D解析 十六进制数以Ox开头,long型数以L(大小写均可,一般使用大写,因为小写的I 和数字 1 不易区分)O4.Which of the following fragments might cause errors?A.String s=Gone with the wind;String t=goodString k=s+t;B.String s=Gone with the wind;String t;t=s3+“one”;C.String s=Gone with the wind*;String standard=s.toUpperCase();D.String s=home directory;String t=s-directory”;翻译下面的哪些程序片断可能导致错误。答 案 B,D解析A:String类型可以直接使用+进行连接运算。B:String是一种Object,而不是简单的字符数组,不能使用下标运算符取其值的某个元素,错误。C:toUpperCase()方法是String对象的一个方法,作用是将字符串的内容全部转换为大写并返回转换后的结果(String类型)。D:String类型不能进行减(-)运算,错误。5.Which are syntactically valid statement at/point x?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)A.i=m;B.i=b;C.i=p.a;D.i=p.change(30);E i=t.b.翻译在 point x 处的哪些申明是句法上合法的。答 案 D,E解析A:m 没有被申明过,不能使用。B:虽然b 是类Teacher的public成员变量,但是在静态方法中不能使用类中的非静态成员。C:a 是类Person的 private成员,在类外不能直接引用。D:change(int m)方法是public方法,并且返回一N n t型值,可以通过类的实例变量p 引用并赋值给一个in t型变量。E:b 是类Teacher的 public成员变量,且是in t型,可以通过类的实例变量t 引用并赋值给一个in t型变量。6.Which layout manager is used when the frame is resized the buttonss position in the Frame might be changed?A.Border LayoutB.Row LayoutC.Card LayoutD.Grid Layout翻译当Frame的大小被改变时Frame中的按钮的位置可能被改变时使用的哪一个布局管理器。答 案 B解析A:该布局管理器将容器划分为五个部分,容器大小的改变不会影响其中的组件的位置而是影响他们的大小。B:该布局管理器根据放入其中的组件的最合适大小调整组件的位置,根据组件放入的顺序安排,一行不能容纳时放入下一行,因此容器的大小改变可能改变组件的位置。C:该布局管理器显示放入该容器的当前页中的组件,一次显示一个,容器大小的改变不能影响其中组件的位置。D:该布局管理器将容器划分为固定的网格,组件加入后占据一个单元,各组件的相对位置不会因为容器的大小变化而变化,改变的只是组件的大小。7.Given the following code fragment:1)public void create()2)Vector myVect;3)myVect=new Vector();4)Which of the following statements are true?A.The declaration on line 2 does not allocate memory space for the variable myVect.B.The declaration on line 2 allocates memory space for a reference to a Vector object.C.The statement on line 2 creates an object of class Vector.D.The statement on line 3 creates an object of class Vector.E The statement on line 3 allocates memory space for an object of class Vector翻译给出下面的代码片断。下面的哪些陈述为true(真产A.第二行的声明不会为变量myVect分配内存空间。B.第二行的声明分配一个到Vector对象的引用的内存空间。C.第二行语句创建一个Vector类对象。D.第三行语句创建一个Vector类对象。E 第三行语句为一个Vector类对象分配内存空间。答 案 A.D.E解析SL-275中指出:要为一个新对象分配空间必须执行new Xxx()调用,new调用执行以下的操作:1 .为新对象分配空间并将其成员初始化为0 或者nulL2.执行类体中的初始化。(例如在类中有一个成员声明int a=10;在第一步后a=0,执行到第二步后a=10)3.执行构造函数。4.变量被分配为一个到内存堆中的新对象的引用。8.Which of the following answer is correct to express the value 8 in octal numbeA.010B.0 x10C.08D.0 x8翻译下面的哪些答案可以用以表示八进制值8。答 案 A解析 八进制值以0 开头,以 Ox开头的为十六进制值,八进制中不能出现数字8,最大只有 7。9.Which are not Java keywords?A.TRUEB.sizeofC.constD.superE void翻译哪些不是Java关键字。答 案 A,B解析A:不是,Java中有true,但是这也不是关键字而是字面量(hteral)。B:不是,Java中不需要这个操作符,所有的类型(原始类型)的大小都是固定的。C、D、E都是,需要说明的是const是 java中未被使用的关键字。10.Which of the following statements are true?A.The equals()method determines if reference values refer to the same object.B.The=operator determines if the contents and type of two separate objectsmatch.C.The equals()method returns true only when the contents of two objects match.D.The class Rie overrides equals()to return true if the contents and type of twoseparate objects match.翻译下面的哪些叙述为真。A.equals。方法判定引用值是否指向同一对象。B.=操作符判定两个分立的对象的内容和类型是否一致。C.equals。方法只有在两个对象的内容一致时返回true。D.类 File重写方法equals。在两个分立的对象的内容和类型一致时返回trueo答 案 AD解析 严格来说这个问题的答案是不确定的,因为equals。方法是可以被重载的,但是按照java语言的本意来说:如果没有重写(override)新类的equals。,则该方 法 和=操作符一样在两个变量指向同一对象时返回真,但是java推荐的是使用equals。方法来判断两个对象的内容是否一样,就像String类的equals。方法所做的那样:判定两个String对象的内容是否相同,而=操作符返回true的唯一条件是两个变量指向同一对象。从这个意义上来说选择给定的答案。从更严格的意义来说正确答案应该只有d11.Which statements about inheritance are true?A.In Java programming language only allows single inheritance.B.In Java programming language allows a class to implement only oneinterface.C.In Java programming language a class cannot extend a class and implementa interface together.D.In Java programming language single inheritance makes code morereliable.翻译下面关于继承的哪些叙述是正确的。A.在 java中只允许单一继承。B.在 java中一个类只能实现一个接口。C.在 java中一个类不能同时继承一个类和实现一个接口。D.java的单一继承使代码更可靠。答 案 A.D解析 在java中一个类只能有