scjp考题1(详尽答案)

本文提供了一份详细的Java SCJP考试题目及解析,涵盖了assert语句的使用、静态变量和方法的执行顺序、类继承和方法重写等核心概念。通过实例代码分析了编译和运行结果,帮助考生深入理解Java编程的基础知识。
摘要由CSDN通过智能技术生成
本文Matrix永久镜像: http://www.matrix.org.cn/resource/article/1/1501.html
说明:本文可能由Matrix原创,也可能由Matrix的会员整理,或者由
Matrix的Crawler在全球知名Java或者其他技术相关站点抓取并永久
保留镜像,Matrix会保留所有原来的出处URL,并在显著地方作出说明,
如果你发觉出处URL有误,请联系Matrix改正.
好资源共享,这份考题有详尽答案,部分题目还有本人心得。这是试题1。
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?
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);
}
}
A.300
B.240
C.120
D.180
E.compile error
F.none of the above
A is the correct choice. 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 w
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值