




已阅读5页,还剩20页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1.Which statement is true? A.An anonymous inner class may be declared as final. B.An anonymous inner class can be declared as private. C.An anonymous inner class can implement multiple interfaces . D.An anonymous inner class can access final variables in any enclosing scope. E.Construction of an instance of a static inner class requires an instance of the enclosing outer class.答案是D anonymous inner class can not declared with any modifyer. and can only implement one interface2. Which statement about static inner classes is true? A.An anonymous class can be declared as static B.A static inner class cannot be a static member of the outer class C.A static inner class does not require an instance of the enclosing class D.Instance members of a static inner class can be referenced using the class name of the static inner class 答案是Cbecause static , it neednt instance of enclosing outer class and its instance member need its instance.3.public class Foo public static void main(String sgf) StringBuffer a = new StringBuffer(A); StringBuffer b = new StringBuffer(B); operate(a,b); System.out.println(a+,+b); static void operate(StringBuffer x,StringBuffer y) x.append(y); y=x; What is the result? A.The code compiles and prints “A.B”. B.The code compiles and prints “A.A”. C.The code compiles and prints “B.B”. D.The code compiles and prints “AB.B”. E.The code compiles and prints “AB.AB”. 答案是Djava中都是按值传递的,所以a,b还是指向原来的地址空间,经过operate操作后,x更改了该地址空间的值,而y没有.public class Foo public static void main(String sgf) StringBuffer a = new StringBuffer(A); StringBuffer b = new StringBuffer(B); operate(a,b); /方法调用完以后,a对象的内容为:AB,b对象的内容为:B System.out.println(a+,+b); static void operate(StringBuffer x,StringBuffer y) /对象传递进来以后又分别复制了一个x和y对象x和y, x和x指向同一个对象。y和y指向同一个对象。 x.append(y); /所以执行此步操作以后,main中的x对象的内容也变化了。 /因为本来就是指向同一个对象吗! y=x; /执行此步操作以后,main中的y对象的内容没变! /因为此y费彼y也! 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.println(v.i); /End of amethod public void another(ValHold v, int i) i=0; v.i = 20; ValHold vh = new ValHold(); v = vh; System.out.println(v.i+ +i); /End of another 1) 10,0, 30 2) 20,0,30 3) 20,99,30 4) 10,0,20 答案是4)首先必须明白一点 参数传递到方法内的时候实际上是复制了一份 而且java并不直接处理对象,而是通过对象参考来处理的。 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.println(v.i); /End of amethod public void another(ValHold v, int i) /参数v,i传进来后进行复制,并且两个v都是指向同一个对象 i=0; v.i = 20; /这里就导致所指向的对象的i值变化 ValHold vh = new ValHold(); v = vh; /这里改变了v的对象参考,指向新的一个ValHold对象 System.out.println(v.i+ +i); /End of another Which of the following statements is/are true? Choose all correct options. A) At the moment a thread calls an objects wait() method, the thread must own that objects lock. B) At the moment a thread calls an objects wait() method, the thread loses that objects lock. C) At the moment a waiting thread is notified, it is given the lock of the object for which it was waiting. D) At any moment, a thread may not be waiting for the lock of more than one object. 首先,可以肯定c是错误的。因为当一个thread被notify后,它只是从wait pool 转向objects lock pool,不一定马上能够拿到lock. 其次,a,b两项应该是正确的。当一个thread在call wait()之前,它手中拿着该object的lock,当它call完wait( )之后,就把objects lock交出,排到wait pool里等待。Correct selection is: A, B, D A and B are true because the whole point of wait() is to force a thread that owns an objects lock to give up the lock and block. C is false: the notified thread must now contend for the objects lock. D is true because after the thread calls wait() on the first object, it is no longer running, so it cannot call wait() on another object.Given the following code class Base class Agg extends Base public String getFields() String name = Agg; return name; public class Avf public static void main(String argv) Base a = new Agg(); /Here What code placed after the comment /Here will result in calling the getFields method resulting in the output of the string Agg? 1) System.out.println(a.getFields(); 2) System.out.println(); 3) System.out.println(Base) a.getFields(); 4) System.out.println( (Agg) a).getFields(); answer 1:as reference type is Base ,not Agg,so method getFields not found . Base a=new Agg(); change Agg a =new Agg(); compile correct. answer 2:variable name not found. answer 3:the reason is the same as answer1 answer 4:correctWhich of the following thread state transitions are valid? A From ready to running. B From running to ready. C From running to waiting. D From waiting to running. E From waiting to ready. F From ready to waiting. thread has four states: ready, running, non-runnable(sleeping, waiting,Blocked),dead The relationship is shown as following ready running running - dead, non-runnable non-runnable - ready so the answer is A,B,C,E 请看以下例子: 1. public class Test public static void main(String argv) Test t = new Test(); t.amothed(); void amothed() byte a; System.out.println(a0); 编译时报错:变量a可能未被初始化。 2. public class Test public static void main(String argv) Test t = new Test(); t.amothed(); void amothed() byte a = new byte5; System.out.println(a0); 编译通过,输出0。 数组是会自动初始化的这肯定没错 但你必须先把它实例化 int a;/定义一个整型数组,但尚未实例化 a = new int3;/实例化a,同时a的所有元素被初始化为0 System.out.println(a0); System.out.println(a1); System.out.println(a2); For the following code: class Super int index = 5; public void printVal() System.out.println( Super ); class Sub extends Super int index = 2; public void printVal() System.out.println( Sub ); public class Runner public static void main( String argv ) Super sup = new Sub(); System.out.print( sup.index + , ); sup.printVal(); What will be printed to standard output? The code compiles and 5, Sub is printed to standard output.1 成员变量是编译时绑定, Super sup = new Sub(); /这里声明sup为Super, 所以在编译时,就确定了sup.index=5 System.out.print( sup.index + , ); /打印出5 成员函数则是动态绑定, sup.printVal(); /尽管sup被声明为super,但是实际上是new Sub(),所以在运行时,sup实际上指向的一个Sub对象。由于是动态绑定,所以这里执行的是Sub的方法。What happens when you try to compile and run the following program? class Mystery String s; public static void main(String args) Mystery m = new Mystery(); m.go(); void Mystery() s = constructor; void go() System.out.println(s); Select the one right answer. this code runs and writes null in the standard output构造函数没有返回值,即在构造函数前面不要写任何东西,void也不能写(除了可以写public). 如果写了void或者其他返回类型,比如此例中的 void Mystery();/这时,这个函数已经不是构造函数了,而且一个普通的函数,尽管与类和构造函数同名,但是他已经是一个普通的函数了,返回类型是void. 也就是说在此例中,其实是没有显示的写出构造函数。所以s当然为空。1)class A static int si=5; static System.out.println(si); A() si+; public static void main(String args) for (int i=0;i3;i+) new A(); 参考答案:print out 5 once当类加载的时候首先是初始化static类变量和执行static类方法,只有这一次机会。此时也许还没有一个类实例。以后所有对象都共享static变量。对static类变量的修改会影响到 所有的类实例。2)class Z public static void main(String args) System.out.println(AAA+new Z(); public String toString() System.out.println(#); return Z; 参考答案:# followed by AAAZ要打印AAA+new Z(),就要先把他翻译成字符串,要想翻译成字符串就要先执行z.toString(),所以先遇到了打印“#”,于是打印了出来,然后z.toString()执行完毕,然后开始执行打印AAA+z,顺序是这样的。What is the result of attempting to compile and run the following program? 1. public class Test 2. private int i = j; 3. private int j = 10; 4. 5. public static void main(String args) 6. System.out.println(new Test().i); 7. 8. Select one correct answer a. Compiler error complaining about access restriction of private variables of Test. b. Compiler error complaining about forward referencing. c. No error - The output is 0; d. No error - The output is 10;答案是: bJAVA中类的编译顺序是只要建立了对象实例,即NEW,就会先初始化变量,在调用CONSTRUCTOR,而变量的初始化是必须按顺序的,所以,第一题选BWhat is the output of the following program 1. public class Test 2. private int i = giveMeJ(); 3. private int j = 10; 4. 5. private int giveMeJ() 6. return j; 7. 8. 9. public static void main(String args) 10. System.out.println(new Test().i); 11. 12. Select one correct answer a. Compiler error complaining about access restriction of private variables of AQuestion. b. Compiler error complaining about forward referencing. c. No Compilation error - The output is 0; d. No Compilation error - The output is 10;答案是: c。生成test类,首先为i,j分配空间并初始化为0,然后执行i=giveMeJ(),这时j还是0,所以i的结果为0;然后执行j=10。所以最后输出为0。Which two cannot directly cause a thread to stop executing? A.exiting from a synchronized block B.calling the wait method on an object C.calling the notify method on an object D.calling a read method on an InputStream object E.calling the setPriority method on a thread object Answer:CEthere are 7 possible for causing thread to stop executing: 1exiting from synchronized block 2/calling the wait method on the object/ 3calling read method on the InputStream object 4/priorer thread enters ready state/ 5calling system.exit(0) 6/The thread executes a sleep() call 7A call to the threads stop method.2 ways to synchronize: 1.Synchronize the entire method Declare the method to be synchronized - very common practice. Thread should obtain the objects lock. 2.Synchronize part of the method Have to pass an arbitrary object which lock is to be obtained to execute the synchronized code block (part of a method). Synchronized(target) statements We can specify “this” in place object, to obtain very brief locking not very common If target is null, then the NullPointerException is thrown.Which of the following are correct, if you compile the following code? public class CloseWindow extends Frame implements WindowListener public CloseWindow() addWindowListener(this); / This is listener registration setSize(300, 300); setVisible(true); public void windowClosing(WindowEvent e) System.exit(0); public static void main(String args) CloseWindow CW = new CloseWindow(); A) Compile time error B) Run time error C) Code compiles but Frames does not listen to WindowEvents D) Compile and runs successfully这道题考得很隐蔽,考你implement 抽象类,必须实例化里面所有的方法,由于没有都实例化,所以答案是A先说抽象类吧,A类如果要继承抽象类B,那么A类必须对抽象类里的所有抽象方法实例化,即override这些抽象方法,同时去掉abstract 前缀。 WindowListener 是 interface,而接口里的方法默认都为抽象的,当然也就. 出题人认为:我们都应该猜到至少WindowListener有比如关闭.之类的方法,而此中没有对这些抽象方法实例话.so .Which statements about garbage collection are true? Select all valid answers. a) You can directly free the memory allocated by an object. b) You can directly run the garbage collector whenever you want to. c) The garbage collector informs your object when it is about to be garbage collected. d) The garbage collector reclaims an object memory as soon as it becomes a candidate for garbage collection. e) The garbage collector runs in low-memory situations.答案是ade对于声明为final类型的成员变量,可以在声明语句中进行初始化如 final int i=0; 也可以在类的构造函数中进行初始化,如果有多个构造函数,每个都必须进行初始化 class test final int i; test() i=0; 甚至可以在类的初始化block中进行初始化, class test final int i; int i=0; final变量只能初始化一次。Which of the following statements are true? 1) The x,y coordinates of an instance of MouseEvent can be obtained using the getX() and getY() methods 2) The x,y coordinates of an instance of MouseEvent can be obtained using the X and Y integer fields 3) The time of a MouseEvent can be extracted using the getTime() method 4) The time of a MouseEvent can be extracted using the when parameter of the MouseEvent constructor1,4.What will happen if you compile/run the folowing lines of code? 1: Vector a = new Vector(); 2: 3: a.addElement(10); 4: 5: System.out.println(a.elementAt(0); A) Prints 10. B) Prints 11. C) Compilation error at line 3. D) Prints some garbage. 答案是C。Vetor 接受的是一个对象,不是一个primitive typeWhich statements describe guaranteed behavior of the garbage collection and finalization mechanisms? 1) Objects are deleted when they can no longer be accessed through any reference. 2) The finalize() method will eventually be called on every object. 3) The finalize() method will never be called more than once on an object. 4) An object will not be garbage collected as long as it is possible for an active part of the program to access it through a reference. 5) The garbage collector will use a mark and sweep algorithm.finalize can be called explicitly,but the finalize method will be called before the garbage collector sweeps away the object.answer is 4,5serialization(串行化) 简单的不太确切的说就是把 对象 构造成 stream(数据串,所以叫串行化),发出去。 deserialization - 把接收到的 stream 重新构造成 对象. serialization主要用在 RMI(远程调用,2个对象通过socket通讯) transient 用于声明类的 成员变量 ,在java语言规格书中没有被详细说明,主要是用在 serialization 的情况, 以保护类中的某些信息. 比如说 类 classA 中有个变量 abc 被声明为transient,那么classA在串行化的时候,abc不被串行化. classA的1个实例在client端串行化为stream发出去,在server端被收到还原成classA,但是这时abc是不被还原的.这个程序之所以显得正确,是因为每个thread都非常之快地运行结束。 public class SyncTest public static void main(String args) final StringBuffer s1=new StringBuffer(); final StringBuffer s2=new StringBuffer(); new Thread() public void run() /只有拥有s1的锁,才可以执行后面的代码, synchronized(s1) /现在当前线程有S1的锁 s1.append(A); synchronized(s2) / 当前线程有S2的锁 s2.append(B); System.out.print(s1); System.out.print(s2); .start(); / 如果足够快的话,当前线程结束运行,释放S1和S2的锁。 new Thread() /此时上一个线程可能已经结束,S1和S2的锁都已经释放。 public void run() synchronized(s2) /当前线程有S2的锁 s2.append(C); synchronized(s1) /当前线程有S2的锁 s1.append(D); System.out.println(s2); System.out.println(s1); .start(); 你可以试验一下,在两个线程中各加上几个(),当第一个线程刚刚得到时,第二个线程已经得到了的锁。然后第一个线程在等,第二个线程等,就会形成死锁。 本身并没有提供避免这种死锁的方法,只有靠程序员自己去注意了。因此,良好的程序设计方法是,(尽量)保持同样的顺序去获取锁。class Mammal Mammal() System.out.println(Creating Mammal); public class Human extends Mammal public static void main(String argv) Human h = new Human(); Human() System.out.println(Creating Human); 输出: Creating Mammal Creating Human子类构造器会自动调用父类的缺省构造器。 相当于构造器中第一行加了一句 super();看一下类的初始化顺序,逐级向下,父类:先static,后非static,初始化成员变量,执行constructor,-子类:先static,后非static,初始化成员变量,执行constructor-。1) public class X 2) public Object m() 3) Object o = new Float(3.14f); 4) Object oa = new Object; 5) oa0 = o; 6) o = null; 7) oa0 = null; System.out.println(oa0); 9) 10) Which line is the earliest point the object a refered is definitely eligible to be garbage collectioned? a). After line 4 b). After line 5 c). After line 6 d). After line 7 e). After line 9Answer is D5) oa0 = o; /Then oa0 also point to the object created by: / Object o = new Float(3.14f); 6) o = null; / Now o is not point to the original object, but oa0 still point to it. The object can not be gc if there is any object reference point to it. 7) oa0 = null; /After here, no reference point to the original object. Can be gc. Here is the earliest point. 8)System.out.println(oa0); /You can say here is the second earliest point (?) :). Just choose Dclass Base int i=99; public void amethod() System.out.println(Base.amethod(); public class RType extends Base int i=-1; public static void main(String argv) Base b = new RType();/= Note the type System.out.println(b.i); b.amethod(); public void amethod() System.out.println(RType.amethod(); Base b = new RType(); The class type of b is Base, and the object reference it contain is RType. All the variable is compile time binding, so when ever we using b.i (variable), JVM going to find the value in Base (class type). Methods are late binding, so while we try b.amethod, JVM will looking for the method in RType (object reference type).Variables can also be overridden, its known as shadowing or hiding. But, member variable references are resolved at compile-time. So at the runtime, if the class of the object referred by a parent class reference variable, is in fact a sub-class having a shadowing member variable, only the parent class variable is accessed, since its already resolved at compile time based on the reference variable type. Only methods are resolved at run
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中国铝太师椅数据监测报告
- 煤层气修井工中秋节后复工安全考核试卷含答案
- 海洋地质调查员国庆节后复工安全考核试卷含答案
- 搪瓷花版饰花工中秋节后复工安全考核试卷含答案
- 供排水泵站运行工中秋节后复工安全考核试卷含答案
- 甲基叔丁基醚丁烯-1装置操作工中秋节后复工安全考核试卷含答案
- 工程项目物业管理前期工作指南
- 合同规划与执行细则操作指引
- 企业质量管理体系建设与规范流程
- 幼儿园大班垃圾分类教案与教学反思
- 膀胱镜检查术后护理常规
- 工程贴息合同协议
- 光伏施工项目危险源辨识与风险评价清单(LEC法)
- 屠宰企业规章制度
- 山东教育出版社小学五年级上册美术教案
- 基于3D视觉引导的工业机器人轮胎装配线设计
- 宠物托养创新创业路演
- 机关健康知识讲座
- 独角抱杆立杆施工方案
- 利用AI技术提升初中语文写作教学效果的实践课题申报书
- 2025年教育督导责任督学培训心得体会与收获
评论
0/150
提交评论