




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、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 ins
2、tance 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 cl
3、ass 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
4、 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?
5、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還是指向原來的地址空間,經(jīng)過operate操作后,x更改了該地址空間的值,而y沒有.public class Foo public static void main(St
6、ring sgf) StringBuffer a = new StringBuffer(A); StringBuffer b = new StringBuffer(B); operate(a,b); /方法調(diào)用完以后,a對象的內(nèi)容為:AB,b對象的內(nèi)容為:B System.out.println(a+,+b); static void operate(StringBuffer x,StringBuffer y) /對象傳遞進(jìn)來以后又分別復(fù)制了一個x和y對象x和y, x和x指向同一個對象。y和y指向同一個對象。 x.append(y); /所以執(zhí)行此步操作以后,main中的x對象的內(nèi)容也轉(zhuǎn)變了。
7、 /由于原來就是指向同一個對象嗎! y=x; /執(zhí)行此步操作以后,main中的y對象的內(nèi)容沒變! /由于此y費(fèi)彼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
8、 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)首先必需明白一點(diǎn) 參數(shù)傳遞到方法內(nèi)的時候?qū)嶋H上是復(fù)制了一份 而且java并不挺直處理對象,而是通過對象參考來處理的。 public static void main(String argv) ObParm
9、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) /參數(shù)v,i傳進(jìn)來后進(jìn)行復(fù)制,并且兩個v都是指向同一個對象 i=0; v.i = 20; /這里就導(dǎo)致所指向的對象的i值轉(zhuǎn)變 ValHold vh = new ValHold(); v = vh; /這里轉(zhuǎn)變了v的
10、對象參考,指向新的一個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
11、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是錯誤的。由于當(dāng)一個thread被notify后,它只是從wait pool 轉(zhuǎn)向objects lock pool,不確定馬上能夠拿到
12、lock. 其次,a,b兩項(xiàng)應(yīng)當(dāng)是正確的。當(dāng)一個thread在call wait()之前,它手中拿著該object的lock,當(dāng)它c(diǎn)all完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 thre
13、ad 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; publ
14、ic 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)
15、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 th
16、e 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 relations
17、hip 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
18、 public static void main(String argv) Test t = new Test(); t.amothed(); void amothed() byte a = new byte5; System.out.println(a0); 編譯通過,輸出0。 數(shù)組是會自動初始化的這確定沒錯 但你必需先把它實(shí)例化 int a;/定義一個整型數(shù)組,但尚未實(shí)例化 a = new int3;/實(shí)例化a,同時a的全部元素被初始化為0 System.out.println(a0); System.out.println(a1); System.out.println(a2); For
19、 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 +
20、 , ); sup.printVal(); What will be printed to standard output? The code compiles and 5, Sub is printed to standard output.成員變量是編譯時綁定, Super sup = new Sub(); /這里聲明sup為Super, 所以在編譯時,就確定了sup.index=5 System.out.print( sup.index + , ); /打印出5 成員函數(shù)則是動態(tài)綁定, sup.printVal(); /盡管sup被聲明為super,但是實(shí)際上是new Sub(),所以在
21、運(yùn)行時,sup實(shí)際上指向的一個Sub對象。由于是動態(tài)綁定,所以這里執(zhí)行的是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
22、. this code runs and writes null in the standard output構(gòu)造函數(shù)沒有返回值,即在構(gòu)造函數(shù)前面不要寫任何東西,void也不能寫(除了可以寫public). 假如寫了void或者其他返回類型,比如此例中的 void Mystery();/這時,這個函數(shù)已經(jīng)不是構(gòu)造函數(shù)了,而且一個一般的函數(shù),盡管與類和構(gòu)造函數(shù)同名,但是他已經(jīng)是一個一般的函數(shù)了,返回類型是void. 也就是說在此例中,其實(shí)是沒有顯示的寫出構(gòu)造函數(shù)。所以s當(dāng)然為空。1)class A static int si=5; static System.out.println(si); A
23、() si+; public static void main(String args) for (int i=0;i3;i+) new A(); 參考答案:print out 5 once當(dāng)類加載的時候首先是初始化static類變量和執(zhí)行static類方法,只有這一次機(jī)會。此時或許還沒有一個類實(shí)例。以后全部對象都共享static變量。對static類變量的修改會影響到 全部的類實(shí)例。2)class Z public static void main(String args) System.out.println(AAA+new Z(); public String toString() Sy
24、stem.out.println(#); return Z; 參考答案:# followed by AAAZ要打印AAA+new Z(),就要先把他翻譯成字符串,要想翻譯成字符串就要先執(zhí)行z.toString(),所以先遇到了打印“#”,于是打印了出來,然后z.toString()執(zhí)行完畢,然后開頭執(zhí)行打印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
25、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 e
26、rror - The output is 10;答案是: bJAVA中類的編譯挨次是只要建立了對象實(shí)例,即NEW,就會先初始化變量,在調(diào)用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(Stri
27、ng 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
28、 output is 10;答案是: c。生成test類,首先為i,j支配空間并初始化為0,然后執(zhí)行i=giveMeJ(),這時j還是0,所以i的結(jié)果為0;然后執(zhí)行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 a
29、n 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/ 5
30、calling 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
31、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 followi
32、ng 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 stati
33、c 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 抽象類,必需實(shí)例化里面全部的方法,由于沒有都實(shí)例化,所以答案是A先說抽象類吧,A類假如要繼承抽象類B,那么A類必需對抽象類里的全部抽象方法實(shí)例化,即override這些抽象
34、方法,同時去掉abstract 前綴。 WindowListener 是 interface,而接口里的方法默認(rèn)都為抽象的,當(dāng)然也就. 出題人認(rèn)為:我們都應(yīng)當(dāng)猜到至少WindowListener有比如關(guān)閉.之類的方法,而此中沒有對這些抽象方法實(shí)例話.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 garba
35、ge 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對于聲明為fin
36、al類型的成員變量,可以在聲明語句中進(jìn)行初始化如 final int i=0; 也可以在類的構(gòu)造函數(shù)中進(jìn)行初始化,假如有多個構(gòu)造函數(shù),每個都必需進(jìn)行初始化 class test final int i; test() i=0; 甚至可以在類的初始化block中進(jìn)行初始化, 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 usi
37、ng 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 constr
38、uctor1,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 statem
39、ents 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
40、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 garb
41、age collector sweeps away the object.answer is 4,5serialization(串行化) 簡潔的不太精確的說就是把 對象 構(gòu)造成 stream(數(shù)據(jù)串,所以叫串行化),發(fā)出去。 deserialization - 把接收到的 stream 重新構(gòu)造成 對象. serialization主要用在 RMI(遠(yuǎn)程調(diào)用,2個對象通過socket通訊) transient 用于聲明類的 成員變量 ,在java語言規(guī)格書中沒有被具體說明,主要是用在 serialization 的狀況, 以疼惜類中的某些信息. 比如說 類 classA 中有個變量 abc 被
42、聲明為transient,那么classA在串行化的時候,abc不被串行化. classA的1個實(shí)例在client端串行化為stream發(fā)出去,在server端被收到還原成classA,但是這時abc是不被還原的.這個程序之所以顯得正確,是由于每個thread都格外之快地運(yùn)行結(jié)束。 public class SyncTest public static void main(String args) final StringBuffer s1=new StringBuffer(); final StringBuffer s2=new StringBuffer(); new Thread() pu
43、blic void run() /只有擁有s1的鎖,才可以執(zhí)行后面的代碼, synchronized(s1) /現(xiàn)在當(dāng)前線程有S1的鎖 s1.append(A); synchronized(s2) / 當(dāng)前線程有S2的鎖 s2.append(B); System.out.print(s1); System.out.print(s2); .start(); / 假如足夠快的話,當(dāng)前線程結(jié)束運(yùn)行,釋放S1和S2的鎖。 new Thread() /此時上一個線程可能已經(jīng)結(jié)束,S1和S2的鎖都已經(jīng)釋放。 public void run() synchronized(s2) /當(dāng)前線程有S2的鎖 s2.
44、append(C); synchronized(s1) /當(dāng)前線程有S2的鎖 s1.append(D); System.out.println(s2); System.out.println(s1); .start(); 你可以試驗(yàn)一下,在兩個線程中各加上幾個(),當(dāng)?shù)谝粋€線程剛剛得到時,其次個線程已經(jīng)得到了的鎖。然后第一個線程在等,其次個線程等,就會形成死鎖。 本身并沒有供應(yīng)避開這種死鎖的方法,只有靠程序員自己去留意了。因此,良好的程序設(shè)計方法是,(盡量)保持同樣的挨次去獵取鎖。class Mammal Mammal() System.out.println(Creating Mammal)
45、; 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子類構(gòu)造器會自動調(diào)用父類的缺省構(gòu)造器。 相當(dāng)于構(gòu)造器中第一行加了一句 super();看一下類的初始化挨次,逐級向下,父類:先static,后非static,初始化成員變量,執(zhí)行constructor,-子類:先static,后非static,初始化成
46、員變量,執(zhí)行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). Af
47、ter 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
48、 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.printl
49、n(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 ref
50、erence 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 ov
51、erridden, 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
52、, since its already resolved at compile time based on the reference variable type. Only methods are resolved at run-time. public class Shadow public static void main(String s) S1 s1 = new S1(); S2 s2 = new S2(); System.out.println(s1.s); / prints S1 System.out.println(s1.getS(); / prints S1 System.o
53、ut.println(s2.s); / prints S2 System.out.println(s2.getS(); / prints S2 s1 = s2; System.out.println(s1.s); / prints S1, not S2 - / since variable is resolved at compile time System.out.println(s1.getS(); / prints S2 - / since method is resolved at run time class S1 public String s = S1; public Strin
54、g getS() return s; class S2 extends S1 public String s = S2; public String getS() return s; In the above code, if we didnt have the overriding getS() method in the sub-class and if we call the method from sub-class reference variable, the method will return only the super-class member variable value
55、. For explanation, see the following point. Also, methods access variables only in context of the class of the object they belong to. If a sub-class method calls explicitly a super class method, the super class method always will access the super-class variable. Super class methods will not access t
56、he shadowing variables declared in subclasses because they dont know about them. (When an object is created, instances of all its super-classes are also created.) But the method accessed will be again subject to dynamic lookup. It is always decided at runtime which implementation is called. (Only st
57、atic methods are resolved at compile-time) public class Shadow2 String s = main; public static void main(String s) S2 s2 = new S2(); s2.display(); / Produces an output S1, S2 S1 s1 = new S1(); System.out.println(s1.getS(); / prints S1 System.out.println(s2.getS(); / prints S1 since super-class metho
58、d always / accesses super-class variable class S1 String s = S1; public String getS() return s; void display() System.out.println(s); class S2 extends S1 String s = S2; void display() super.display(); / Prints S1 System.out.println(s); / prints S2 With OO languages, the class of the object may not b
59、e known at compile-time (by virtue of inheritance). JVM from the start is designed to support OO. So, the JVM insures that the method called will be from the real class of the object (not with the variable type declared). This is accomplished by virtual method invocation (late binding). Compiler wil
60、l form the argument list and produce one method invocation instruction its job is over. The job of identifying and calling the proper target code is performed by JVM. JVM knows about the variables real type at any time since when it allocates memory for an object, it also marks the type with it. Obj
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 五項(xiàng)試驗(yàn)考試試題及答案
- pon網(wǎng)絡(luò)考試題及答案
- 高原駕駛測試題及答案
- 順豐員工考試試題及答案
- 2025年《企業(yè)人力資源管理師》專業(yè)綜合知識考試題庫與答案
- 2025職業(yè)衛(wèi)生技術(shù)人員評價方向考試題庫(含答案)
- 醫(yī)療質(zhì)量安全(不良)事件管理辦法試題測試題庫含答案
- 醫(yī)療機(jī)構(gòu)《醫(yī)療衛(wèi)生機(jī)構(gòu)醫(yī)療廢物管理辦法》培訓(xùn)考核試題及答案
- 2025年醫(yī)療廢物分類處置試題及答案
- 數(shù)字化物流商業(yè)運(yùn)營 課件 模塊七 數(shù)字化設(shè)施選址與流程優(yōu)化
- 氯甲烷泄露應(yīng)急預(yù)案
- 2.PaleoScan詳細(xì)操作流程
- 林業(yè)有害生物防治知識競賽真題模擬匯編(共184題)
- PLC西門子S7-1200應(yīng)用技術(shù)完整全套教學(xué)課件
- 蘇州銀行總行信息科技部招聘考試真題2022
- 銅陵橫港化工園區(qū)總體發(fā)展規(guī)劃(2021-2035年)環(huán)境影響報告書
- 專升本數(shù)學(xué)教材(新)
- 安裝電工電氣調(diào)試員安全技術(shù)操作規(guī)程
- 柴芍六君子湯加減治療脾胃病三則
- GB/T 11547-2008塑料耐液體化學(xué)試劑性能的測定
- (完整版)人工智能介紹課件
評論
0/150
提交評論