设为首页 加入收藏

TOP

J2SE综合测试题1(英文版)及答案.doc
2014-11-07 16:30:03 来源: 作者: 【 】 浏览:20
Tags:J2SE 综合 测试题 英文版 答案 .doc


_____________________________


public class Static {


static {


int x = 5;


}



static int x, y;



public static void main(String args[]) {


x–;


myMethod();


System.out.println(x + y + ++x);


}



public static void myMethod() {


y = x++ + ++x;


}


}





2. Given the following code, what will be the output


_____________________________


public class Value {


public int i = 15;


}



public class Test {


public static void main(String argv[]) {


Test t = new Test();


t.first();


}



public void first() {


int i = 5;


Value v = new Value();


v.i = 25;


second(v, i);


System.out.println(v.i);


}



public void second(Value v, int i) {


i = 0;


v.i = 20;


Value val = new Value();


v = val;


System.out.println(v.i + ” ” + i);


}


}




class MyParent {


int x, y;



MyParent(int x, int y) {


this.x = x;


this.y = y;


}



public int addMe(int x, int y) {


return this.x + x + y + this.y;


}



public int addMe(MyParent myPar) {


return addMe(myPar.x, myPar.y);


}


}



class MyChild extends MyParent {


int z;



MyChild(int x, int y, int z) {


super(x, y);


this.z = z;


}



public int addMe(int x, int y, int z) {


return this.x + x + this.y + y + this.z + z;


}



public int addMe(MyChild myChi) {


return addMe(myChi.x, myChi.y, myChi.z);


}



public int addMe(int x, int y) {


return this.x + x + this.y + y;


}


}



public class MySomeOne {


public static void main(String args[]) {


MyChild myChi = new MyChild(10, 20, 30);


MyParent myPar = new MyParent(10, 20);


int x = myChi.addMe(10, 20, 30);


int y = myChi.addMe(myChi);


int z = myPar.addMe(myPar);


System.out.println(x + y + z);


}


}




4. What will be the result of executing the following method ________


public void method1() {


boolean a = true;


boolean b = false;


boolean c = true;


if (a == true)


if (b == true)


if (c == true)


System.out.println(“Some things are true in this world”);


else


System.out.println(“Nothing is true in this world!”);


else if (a && (b = c))


System.out


.println(“It’s too confusing to tell what is true and what is false”);


else


System.out.println(“Hey this won’t compile”);


}



A.The code won’t compile.


B.”some things are true in this world” will be printed


C.”hey this won’t compile” will be printed


D.None of these




5. Which of the following collection classes from java.util package are Thread safe _________


A.Vector


B.ArrayList


C.HashMap


D.Hashtable



6. Given the following,


1. abstract class A {


2. abstract short m1() ;


3. short m2() { return (short) 420; }


4. }


5.


6. abstract class B extends A {


7. // missing code


8. short m1() { return (short) 42; }


9. }


which three of the following statements are true (Choose three.)__________


A. The code will compile with no changes.


B. Class B must either make an abstract declaration of method m2() or implement


method m2() to allow the code to compile.


C. It is legal, but not required, for class B to either make an abstract declaration of method


m2() or implement method m2() for the code to compile.


D. As long as line 8 exists, class A must declare method m1() in some way.


E. If line 6 were replaced with ‘class B extends A {‘ the code would compile.


F. If class A was not abstract and method m1() on line 2 was implemented, the


code would not compile.




7. Given the following,


1. package testpkg.p1;


2. public class ParentUtil {


3. public int x = 420;


4. protected int doStuff() { return x; }


5. }


1. package testpkg.p2;


2. import testpkg.p1.ParentUtil;


3. public class ChildUtil extends ParentUtil {


4. public static void main(String [] args) {


5. new ChildUtil().callStuff();


6. }


7. void callStuff() {


8. System.out.print(“this ” + this.doStuff() );


9. ParentUtil p = new ParentUtil();


10. System.out.print(” parent ” + p.doStuff() );


11. }


12. }


which statement is true _______________


A. The code compiles and runs, with output this 420 parent 420.


B. If line 8 is removed, the code will compile and run.


C. If line 10 is removed, the code will compile and run.


D. Both lines 8 and 10 must be removed for the code to compile.


E. An exception is thrown at runtime.




8. Which two are equal (Choose two.)


A. 32 / 4;


B. (8 >> 2) << 4;


C. 2 ^ 5;


D. 128 >>> 2;


E. (2 << 1) * (32 >> 3);


F. 2 >> 5;




9. Given the following,


1. class Test {


2. public static void main(String [] args) {


3. int x= 0;


4. int y= 0;


5. for (int z = 0; z < 5; z++) {


6. if (( ++x > 2 ) && (++y > 2)) {


7. x++;


8. }


9. }


10. System.out.println(x + ” ” + y);


11. }


12. }


What is the result ______________




10. Given the following,


1. public class If2 {


2. static boolean b1, b2;


3. public static void main(String [] args) {


4. int x = 0;


5. if ( !b1 ) {


6. if ( !b2 ) {


7. b1 = true;


8. x++;


9. if ( 5 > 6 ) {


10. x++;


11. }


12. if ( !b1 ) x = x + 10;


13. else if ( b2 = true ) x = x + 100;


14. else if ( b1 | b2 ) x = x + 1000;


15. }


16. }


17. System.out.println(x);


18. }


19. }


what is the result _________________



11. Which two of these statements are true about constructors (Choose two.)_________


A. Constructors must not have arguments if the superclass constructor does not have


arguments.


B. Constructors are not inherited.


C. Constructors cannot be overloaded.


D. The first statement of every constructor is a legal call to the super() or this()method.



12. Given the following,


1. import java.util.*;


2. class Ro {


3. public static void main(String [] args) {


4. Ro r = new Ro();


5. Object o = r.test();


6. }


7.


8. Object test() {


9.


10.


11. }


12. }


which two of the following code fragments inserted at lines 9/10 will not compile


(Choose two.)____________


A. char [] [] c = new char [2][2];


return c;


B. return (Object) 7;


C. return (Object) (new int [] {1,2,3} );


D. ArrayList a = new ArrayList();


return a;


E. return (Object) “test”;


F. return (Float) 4.3;



13. Which two statements are true about wrapper or String classes (Choose two.)________


A. If x and y refer to instances of different wrapper classes, then the fragment x.equals(y)


will cause a compiler failure.


B. If x and y refer to instances of different wrapper classes, then x == y can sometimes be


true.


C. If x and y are String references and if x.equals(y) is true, then x == y is true.


D. If x, y, and z refer to instances of wrapper classes and x.equals(y) is true, and


y.equals(z) is true, then z.equals(x) will always be true.


E. If x and y are String references and x == y is true, then y.equals(x) will be true.



14. Given the following,


public class SyncTest {


public static void main (String [] args) {


Thread t = new Thread() {


Foo f = new Foo();


public void run() {


f.increase(20);


}


};


t.start();


}


}


class Foo {


private int data = 23;


public void increase(int amt) {


int x = data;


data = x + amt;


}


}


and assuming that data must be protected from corruption, what—if anything—can you add


to the preceding code to ensure the integrity of data ____________



A. Synchronize the run method.


B. Wrap a synchronize(this) around the call to f.increase().


C. The existing code will not compile.


D. The existing code will cause a runtime exception.


E. Put in a wait() call prior to invoking the increase() method.


F. Synchronize the increase() method



15. In a JSP custom tag, which method would you use to access the JSP implicit variable that references the application scope ________________


A. PageContext.getOut()
B. JspFactory.getPagetContext()
C. TagSupport.getValue(String)
D. PageContext.getServletContext()



16. When a session becomes invalid, which method will be invoked on a session attribute object that implements the corresponding interface ___________
A. sessionDestroyed of the HttpSessionListener interface
B. valueUnbound of the HttpSessionBindingListener interface
C. attributeRemoved of the HtppSessionAttributeListener interface
D. sessionWillPassivate of the HttpSessionActivationListener interface



17.
Which two are equivalent (Choose two)______________


A. <%= YoshiBean.size%>
B. <%= YoshiBean.getSize()%>
C. <%= YoshiBean.getProperty(“size”)%>
D.
E.
F.
G.



18. Given the following,


11. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
12. PrintWriter out = response.getWriter();
13. out.print(“

Hello

”);
14.
15. response.sendError(HttpServletResponse.SC_NOT_FOUND);
16. }
Which, inserted at line 14, will ensure that an IllegalStateException is thrown at line 15


____________________


A.response.flushBuffer();
B. response.resetBuffer();
C. response.clearStatus();
D. if(response.getError()==-1)
E. if(response.getStatus()==-1)
F. if(response.isCommitted()==false)



19. Given the following,


11. public class Acc extends HttpServlet{
12. private StringBuffer bar = new StringBuffer(“bar”);
13. private static StringBuffer xyz = new StringBuffer();
14. public void doPost(HttpServletRequest req, HttpServletResponse resp){
15. StringBuffer yoshi = new StringBuffer(“yoshi”);
16. HttpSession session = req.getSession();
17. }
18. }


Which two variables reference objects that are thread-safe (Choose two)____________


A. req at line 14
B. yoshi at line 15
C. bar at line 12
D. xyz at line 13
E. session at line 16



20.
Given that the deployment descriptor for a Web application named test contains:

servlet1
*.bop

Assume the URL for the entry point of the web application is “http://servername/”.
Which will invoke the servlet instance named servlet1 ____________


A. http://servername/test.bop
B. http://servername/ servlet1/bar.bop
C. http://servername/test/ servlet1/bop
D. http://servername/baz/bat/boo.bop
E. http://servername/test/ servlet1/bar/baz.hop































答案:


Answer :


1.prints: 3


The above code will not give any compilation error. Note that “Static” is a valid class name. Thus choice A is incorrect.


In the above code, on execution, first the static variables (x and y) will be initialized to 0. Then static block will be called and finally main() method will be called. The execution of static block will have no effect on the output as it declares a new variable (int x).


The first statement inside main (x–) will result in x to be -1.


After that myMethod() will be executed. The statement “y = x++ + ++x;”


will be eva luated to y = -1 + 1 and x will become 1.


In case the statement be “y =++x + ++x”, it would be eva luated to y = 0 + 1 and x would become 1.


Finally when System.out is executed “x + y + ++x” will be eva luated to “1 + 0 + 2″ which result in 3 as the output.



========================


2.


The output is : 15 0 20


When we pass references in Java what actually gets passed is the value of that reference


(i.e. memory address of the object being referenced and not the actual object referenced by that reference) and


it gets passed as value (i.e a copy of the reference is made).


Now when we make changes to the object referenced by that reference it reflects on that object


even outside of the method being called but any changes made to the reference itself is not reflected on


that reference outside of the method which is called.


In the example above when the reference v is passed from method first() to second() the value of v is passed.


When we assign the value val to v it is valid only inside the method second() and


thus inside the method second() what gets printed is 15 (initial value of i in the object referenced by val),


then a blank space and then 0 (value of local variable i). After this when we return to the method first() v


actually refers to the same object to which it was referring before the method second() was called,


but one thing should be noted here that the value of i in that object (referred by v inside the method first())


was changed to 20 in the method second() and this change does reflect even outside the method second(),


hence 20 gets printed in the method first(). Thus overall output of the code in consideration is 15 0 20


===========================


3.The result is : 300


In the above code, MyChild class overrides the addMe(int x, int y) method of the MyParent class. And in both the MyChild and MyParent class, addMe() method is overloaded.


There is no compilation error anywhere in the above code.On execution, first, the object of MyChild class will be constructed. Please note that there is a super() call from the constructor of MyChild class,


which will call the constructor of MyParent class. This will cause the value of z variable of MyChild class to be 30 and x, y variables of MyParent class will become 10 and 20 respectively.


The next statement will again call the constructor of MyParent class with same x and y values. This is followed by execution of addMe() method of MyChild class with x as 10, y as 20 and z as 30.


Also x and y are inherited by MyChild class from the MyParent class. Thus in the addMe() method of the MyChild class, the value of this.x will be 10, this.y will be 20 and this.z will be 30. The return value of this method will be “10 + 10 + 20 + 20 + 30 + 30″,


which is equal to 120. Thus x will become 120.


This is followed by the invocation of the other addMe() method which takes object reference of the MyChild class. From this method, the method which was called earlier is invoked.


This call is exactly the same as the earlier one. Thus the value of y will also be 120 like x.


Now the addMe() method of MyParent class is invoked.


This method invokes another addMe() method of the same class.


Its equivalent to the invocation of addMe(int x, int y) method with x as 10 and y as 20.


Also the value of instance variables x and y of My Parent class is 10 and 20 respectively.


The value of z will be eva luated to “10 + 10 + 20 + 20″, which is equal to 60. Thus the value of x, y and z after all the invocations will be 120, 120 and 60 respectively. As a result of this finally,


“120 + 120 + 60″ which is equal to 300 will be printed.



===========================


4.D is correct.


This is a very good question to test the concepts of execution flow in case of if conditions.


The rule for attaching else statements with if conditions is the same as attaching close brackets with open brackets.


A close bracket attaches with the closest open bracket, which is not already closed. Similarly an else statement attaches with the closest if statement, which doesn’t have an else statement already, attached to it. So the else statement at line 7 attaches to the if statement at line 6.


The else statement at line 8 attaches to the if statement at line 5. The else statement at line 9 attaches to the if statement at line 8.


Now let’s look at the execution. At line 4 since a is equal to true the execution falls to line 5. At line 5 since b is not true the execution goes to the corresponding else statement at line 8.


Now it eva luates the condition inside the if statement. Please note here that an assignment statement also has a value equal to the value being assigned, hence (b = c) eva luates to true and subsequently a && (b = c)


eva luates to true and “It’s too confusing to tell what is true and what is false” will be printed. Hence the correct answer is choice D.


==============================


5.A and D are correct


6.


A, C, and E. A and C are correct, because an abstract class does not need to


implement any of its superclass’ methods. E is correct because as it stands, it is a valid concrete


extension of class A.


B is wrong because an abstract class does not need to implement any of its superclass’


methods. D is wrong because a class that extends another class is free to add new methods. F is


wrong because it is legal to extend an abstract class from a concrete class.


==============================


7.


C. The ParentUtil instance p cannot be used to access the doStuff() method. Because


doStuff() has protected access, and the ChildUtil class is not in the same package as


the ParentUtil class, doStuff() can be accessed only by instances of the ChildUtil class (a


subclass of ParentUtil).


==============================


8.


B and D. B and D both eva luate to 32. B is shifting bits right then left using the signed


bit shifters >> and <<. D is shifting bits using the unsigned operator >>>, but since the


beginning number is positive the sign is maintained.


==============================


9.


The result is : 8 2


The first two iterations of the for loop both x and y are incremented. On the third


iteration x is incremented, and for the first time becomes greater than 2. The short circuit or


operator || keeps y from ever being incremented again and x is incremented twice on each of


the last three iterations.



==============================


10.The result is : 101


As instance variables, b1 and b2 are initialized to false. The if tests on lines 5 and 6


are successful so b1 is set to true and x is incremented. The next if test to succeed is on line 13


(note that the code is not testing to see if b2 is true, it is setting b2 to be true). Since line 13


was successful, subsequent else-if’s (line 14) will be skipped.


===================================


11.


B and D are simply stating two rules about constructors.


A is wrong because subclass constructors do not have to match the arguments of the


superclass constructor. Only the call to super() must match. C is incorrect because


constructors can be and are frequently overloaded.


=====================================


12.B,F


B and F are both attempting to cast a primitive to an object—can’t do it.


A, C, D, and E all return objects. A is an array object that holds other arrays. C is an array


object. D is an ArrayList object. E is a string cast to an object.


=====================================


13.D,E.


D describes an example of the equals() method behaving transitively. By


the way, x, y, and z will all be the same type of wrapper. E is true because x and y are referring


to the same String object.


A is incorrect—the fragment will compile. B is incorrect because x == y means that the


two reference variables are referring to the same object. C will only be true if x and y refer


to the same String. It is possible for x and y to refer to two different String objects with the


same value.


========================================


14.F


F is correct because synchronizing the code that actually does the increase will protect the


code from being accessed by more than one thread at a time.


A is incorrect because synchronizing the run() method would stop other threads from


running the run() method (a bad idea) but still would not prevent other threads with other


runnables from accessing the increase() method. B is incorrect for virtually the same reason


as A—synchronizing the code that calls the increase() method does not prevent other


code from calling the increase() method. C and D are incorrect because the program


compiles and runs fine. E is incorrect because it will simply prevent the call to increase()


from ever happening from this thread.


==============================================


15.D



16. B


比較一下 HttpSessionAttributeListener HttpSessionBindingListener:



HttpSessionAttributeListener 有三個 method


void attrubuteAdded(HttpSessionBindingEvent se)


void attrubuteRemoved(HttpSessionBindingEvent se)


void attrubuteReplaced(HttpSessionBindingEvent se)


需要在 deployment descriptor 設定


不能在分散式環境下使用



HttpSessionBindingListener 有兩個 method


void valueBound(HttpSessionBindingEvent se)


void valueUnbound(HttpSessionBindingEvent se)


不在 deployment descriptor 設定


attribute 需要要實作 HttpSessionBindingListener 這個介面


可以在分散式環境下使用


針對那些attribute, 所以是HttpSessionBindingListener


另外還有幾個很像的:


HttpSessionListener, 這個應該是用來記錄整個web applicationsession的建立, 消失


HttpSessionAttributeListener, 這個是用來偵測整個web application所有的session, attribute的建立與消失



HttpSessionBindingListener是由attribute自己實作, 在自己被bound, unbound時會呼叫


The HttpSessionListener and HttpSessionAttributeListener are configured


in the deployment descriptor. Therefore, even if a session attribute


implements these interfaces, the sessionDestroyed() and attributeRemoved()


methods will not be called on that attribute.


An HttpSessionActivationListener is also configured in the deployment


descriptor, and it is used when a session is passivated or activated.


========================


17.Answer: B.G


========================


18.A


public void sendError(int sc) throws java.io.IOException


Sends an error response to the client using the specified status code and clearing the buffer.


If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.


如果 response 已經被 commit 送出,就會有 IllegalStateException


response 沒有被 commit,就直接送出 404 error



public void flushBuffer() throws java.io.IOException


Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written.


buffer 清空送出



public void resetBuffer()


Clears the content of the underlying buffer in the response without clearing headers or status code. If the response has been committed, this method throws an IllegalStateException.


重設一個空 buffer



public boolean isCommitted()


Returns a boolean indicating if the response has been committed. A commited response has already had its status code and headers written.


判斷有無被 commit


========================


19.AB


========================


20.A









】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Spring如何配置一个bean来从JNDI.. 下一篇嵌入式开发常见问题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: