scjp考题1(详尽答案)
2008-02-23 09:24:53来源:互联网 阅读 ()
说明:本文可能由Matrix原创,也可能由Matrix的会员整理,或者由
Matrix的Crawler在全球知名Java或者其他技术相关站点抓取并永久
保留镜像,Matrix会保留所有原来的出处URL,并在显著地方作出说明,
如果你发觉出处URL有误,请联系Matrix改正.
1. 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 : methodB();
System.out.println(i);
}
public void methodB(){ //无返回值
System.out.println("The value must not be negative");
}
public static void main(String args[]){
AssertTest test = new AssertTest();
test.methodA(-10);
}
}
A.it will print -10
B.it will result in AssertionError showing the message-“the value must not be negative”.
C.the code will not compile.
D.None of these.
C is correct. An assert statement can take any one of these two forms -
assert Expression1;
assert Expression1 : Expression2;
Note that, in the second form; the second part of the statement must be an expression- Expression2. In this code, the methodB() returns void, which is not an expression and hence it results in a compile time error. The code will compile if methodB() returns any value such as int, String etc.
Also, in both forms of the assert statement, Expression1 must have type boolean or a compile-time error occurs.
2. What will happen when you attempt to compile and run the following code?
public class Static{
static{
int x = 5; //在static内有效
}
static int x,y; //初始化为0
public static void main(String args[]){
x--; //-1
myMethod();
System.out.println(x y x);
}
public static void myMethod(){
y = x x; //y=-1 1 x=1
}
}
A.compiletime error
B.prints: 1
C.prints: 2
D.prints: 3
E.prints: 7
F.prints: 8
D is the correct choice. 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 evaluated to y = -1 1 and x will become 1. In case the statement be "y = x x", it would be evaluated to y = 0 1 and x would become 1. Finally when System.out is executed "x y x" will be evaluated to "1 0 2" which result in 3 as the output. Thus choice D is correct.
3. Given the following code, what will be the output?
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);
}
}
A.15 0 20
B.15 0 15
C.20 0 20
D.0 15 20
A is correct. 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
4. What will happen when you attempt to compile and run the following code?
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash