JAVA认证考试:SCJP考题1(详细答案)


作者: | 来源:blogjava | 日期:06-28 | 字体: [ ]

本文简介:What will happen when you attempt to compile and run the following code



5. The class AssertionError has "is -a" relationship with these classes (choose two)
A.RuntimeException
B.Error
C.VirtualMachineError
D.IllegalAccessException
E.Throwable
B and E are correct. The class AssertionError is an Error, which denotes an “incorrect condition” as opposed to an “unusual condition” (Exception).  Since, the class Error descends from Throwable, AssertionError also has “is-a” relationship with Throwable. Here is the hierarchy –java.lang.Object
|
+-java.lang.Throwable
       |
       +-java.lang.Error                            Exception
             |                                        |
             +-java.lang.AssertionError               +RuntimeException IOException
Want to know more?
You can find more information about this as an answer to the question - “Why is AssertionError a subclass of Error rather than RuntimeException?” at - http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html #design-faq-error

6. What will be the result of executing the following code?
1. boolean a = true;
2. boolean b = false;
3. boolean c = true;
4. if (a == true)
5.   if (b == true)
6.       if (c == true) System.out.println("Some things are true in this world");
7.       else System.out.println("Nothing is true in this world!");
8.   else if (a && (b = c))   //这里是赋值,不是比较
System.out.println("It's too confusing to tell what is true and what is false");
9.   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
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 evaluates 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) evaluates to true and subsequently a && (b = c) evaluates 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.

7. What will happen when you attempt to compile and run the following code?
interface MyInterface{}
public class MyInstanceTest implements MyInterface{
static String s;
public static void main(String args[]){
MyInstanceTest t = new MyInstanceTest();
if(t instanceof MyInterface){
System.out.println("I am true interface");
}else {
System.out.println("I am false interface");
}
if(s instanceof String){
System.out.println("I am true String");
}else {
System.out.println("I am false String");
}
}
}
A.compile time error
B.runtime error
C.prints: “I am true interface” followed by “I am true String”
D.prints: “I am false interface” followed by “I am false String”
E.prints: “I am true interface” followed by “I am false String”
F.prints: “I am false interface” followed by “I am true String”
E is the correct choice. The "instanceof" operator tests the class of an object at runtime. It returns true if the class of the left-hand argument is the same as, or is some subclass of, the class specified by the right-hand operand. The right-hand operand may equally well be an interface. In such a case, the test determines if the object at left-hand argument implements the specified interface.
In the above case there will not be any compiletime or runtime error. The result of "t instance of MyInterface" will be true as "t" is the object of MyInstanceTest class which implements the MyInstance interface. But the result of "s instanceof String" will be false as "s" refers to null. Thus the output of the above program will be : "I am true interface" followed by " I am false String". Thus choice E is correct and others are incorrect.

8. What results from attempting to compile and run the following code?
public class Ternary{
public static void main(String args[]){
int a = 5;
System.out.println("Value is - " + ((a < 5) ? 9.0 : 9));
}
}
A.print:Value is -9
B.print:Value is -5
C.Compilation error
D.None of these
D is correct. The code compiles successfully. In this code the optional value for the ternary operator, 9.0(a double) and 9(an int) are of different types. The result of a ternary operator must be determined at the compile time, and here the type chosen using the rules of promotion for binary operands, is double. Since the result is a double, the output value is printed in a floating point format. The choice of which value to be printed is made on the basis of the result of the comparison "a < 5" which results in false, hence the variable "a" takes the second of the two possible values, which is 9, but because the result type is promoted to double, the output value is actually written as 9.0, rather than the more obvious 9, hence D is correct.

9. In the following pieces of code, A and D will compile without any error. True/False?
A: StringBuffer sb1 = "abcd";
B: Boolean b = new Boolean("abcd");
C: byte b = 255;
D: int x = 0x1234;
E: float fl = 1.2;
True
False
The code segments B and D will compile without any error. A is not a valid way to construct a StringBuffer, you need to creat a StringBuffer object using "new". B is a valid construction of a Boolean (any string other than "true" or "false" to the Boolean constructor will result in a Boolean with a value of "false"). C will fail to compile because the valid range for a byte is -128 to +127 (ie, 8 bits,signed). D is correct, 0x1234 is the hexadecimal representation in java. E fails to compile because the compiler interprets 1.2 as a double being assigned to a float (down-casting), which is not valid. You either need an explicit cast (as in "(float)1.2") or "1.2f", to indicate a float.

10. Considering the following code, Which variables may be referenced correctly at line 12?
1.public class Outer
2.{
3.public int a = 1;
4.private int b = 2;
5.public void method(final int c)
6.{
7.int d = 3;
8.class Inner
9.{
10.private void iMethod(int e)
11. {
12.
13.}
14.}
15.}
16.}
a b c d e
A, B, C and E are correct. Since Inner is not a static inner class, it has a reference to an enclosing object, and all the variables of that object are accessible. Therefore A and B are correct, even if b is private. Variables in the enclosing method are only accessible when they are marked as final hence c is accessible but not d. E is obviously correct as it is a parameter to the method containing line 12 itself.


用户名: 新注册) 密码: 匿名评论 [所有评论]

评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • java认证考试 java考试题 java程序员考试 java工程师考试
如果你觉得一篇文章有用,你可以在每篇后面参与评论,或者查看其他人的评论,请保证你的评论对大家友好。
点这里评论
或者您可以来资源论坛参与讨论,一切都是免费的,不过可能需要麻烦您注册一下。
点这里讨论
把你的文章登陆在这里,让大家来分享你的文章。请立即登陆发表!
点这里投稿