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


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

本文简介:Which of the following are valid constructors of the class java.lang.AssertionError


*****   =====================
8.4 What is the output of the following code?
1.  public class Note
2.  {
3.          public static void main(String args[])
4.      {
5.              String name[] = {"Killer","Miller"};
6.              String name0 = "Killer";        
7.              String name1 = "Miller";
8.              swap(name0, name1);
9.              System.out.println(name0 + ", " + name1);
10.             swap(name);
11.             System.out.println(name[0] + ", " + name[1]);
12.         }
13.         public static void swap(String name[])
14.     {
15.             String temp;
16.             temp = name[0];
17.             name[0] = name[1];
18.             name[1] = temp;
19.         }  
20.         public static void swap(String name0, String name1)
21.     {
22.             String temp;
23.             temp = name0;
24.             name0 = name1;
25.             name1 = temp;
26.         }    
27.     } // end of Class Note
A. Killer, Miller followed by Killer, Miller
B. Miller, Killer followed by Killer, Miller
C. Killer, Miller followed by Miller, Killer
D. Miller, Killer followed by Killer, Miller
Answer: C
Note: In java all parameters are passed by value. In case of primitives, the copy of the variable is passed while case of object references, its the copy of the reference which is passed.

*****   ===================
8.5 Comment if the following code implements equals method of Object class correctly?     Select the most appropriate answer.
1.  public class BetterString
2.  {
3.      String str;
4.      
5.      public BetterString(String s)
6.      {
7.          str = s;
8.      }
9.      
10.     public boolean equals(Object obj)
11.     {
12.         if(obj instanceof BetterString)
13.         {
14.             if(obj == this)
15.                 return true;
16.             else
17.                 return str.equals(obj);
18.         }
19.         else if(obj instanceof String)
20.         {
21.             return str.equals(obj);
22.         }
23.         return false;          
24.     }
25. }
A. The code implements equals method correctly
B. The code implements equals method correctly, but does not provide any additional feature over the equals method provided by String class
C. The code does not implement equals method correctly
D. The given code does not contain enough information to comment on the correctness of the equals method implementation
E. None of these
Answer: C
Note: The code attempts to provide equals comparison with a String. String is a built in final class in Java. It is inappropriate as well as incorrect to provide an equals comparison with any built in Java class, as it violates "symmetry" requirement mentioned in the general contract of Object's equals method. It says - for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. This is not valid for the above code, because if an object of class BetterString is passed to the equals method of String class, it would result in false;irrespective of its contents.

****    ===================
8.6 A Java monitor must either extend Thread or implement Runnable. True/False?
A. True
B. False
Answer: B
Note: as a monitor is any object that has synchronized code. Thus a Java monitor need not extends Thread class or implement Runnable interface.

***     =====================
8.7 What will be the result of executing the following code?
Given that Test1 is a class.
1. Test1[] t1 = new Test1[10];
2. Test1[][] t2 = new Test1[10][];
3. System.out.println(t2[0][0]);
A. The code will not compile as we are using t2[0][0] without initializing
B. The code will compile and print null
C. The code will compile but throw a runtime exception
D. None of these
Answer: C
Note: In case of arrays initialization is supposed to be complete when we specify the leftmost dimension of the array. The problem occurs at runtime if we try to access an element of the array which has not been initialized (specification of size).

*****   =========================
8.8 What will be the result of executing the following code?
int i = 0;
int myArray[] = new int[0];
for (i = 0; i < myArray.length; i++){
   System.out.println(myArray[i]);
}
A. The code will not compile as the size of array should be at least 1
B. The code will compile but throw an exception at runtime
C. The code will compile and "0" will be printed
D. None of these
Answer: D
Note:  The code will compile and run perfectly fine but it will not print anything. It is perfectly legal in Java to create arrays of size zero. The flow of control will never enter the for loop as the condition at the beginning of for loop is never satisfied. But note that If you directly print "myArray" and not in the for loop,then complie error that ArrayIndexOutOfBoundException(???).

***     ===================
8.9 For what reasons might a thread stop execution?
A. A thread with higher priority began execution.
B. The thread's wait() method was invoked.
C. The thread invoked its yield() method.
D. The thread's pause() method was invoked. //has destroy(), hasn’t pause().
E. The thread's sleep() method was invoked.
Answer: ABCE

****    =======================
8.10 Which of the following can be used for reading the system property named "count"?
The value of this property will be a string representing a number in the range 1 to 1000.
A. Integer.decode("count");
B. Integer.getProperty("count");
C. Integer.getSystemProperty("count");
D. Integer.getInteger("count");
E. Integer.valueOf("count");
Answer: D
Note:  The static method getInteger(String propertyName) is of form:
public static Integer getInteger(String nm)
This method returns the integer value of the system property with the specified name.


##########
test 9
##########

**      ==================
9.1 What will be the output when you compile and execute the following program?
class Base extends Thread {
   public void run() {
       while(true)
       System.out.println("Start running..");  
   }
   static public void main(String[] a) {
       Base anObj = new Base();
       anObj.start();
   }
}
A. Start running..
B. Start running..  
  ..............
  in an infinite loop
C. Compilation error. Cannot extend Thread class
D. No output
Answer: B

**      =======================
9.2 If a thread executes wait(), and subsequently gets notified, it immediately proceeds with execution of the code that follows the wait() call. True/False?
A. True
B. False
Answer: B

*****   =======================
9.3 Will there be any problem in a multi-threaded context if only local variables are declared in a method?
A. Yes, you need synchronized in the blocks of code which read and write a local variable value.
B. Yes, you need synchronized in the blocks of code which read (but not write) a local variable value
C. Absolutely no
D. None of the above.
Answer: C
Note: In case you have local variables (variables declared in a method), then there is no problem of synchronization in a multi-threaded context. The local variables are accessed from the thread's stack, the instance variables are accessed through object references, and static variables are accessed through class or object references. The local variables in the methods that the thread is executing are separate for different threads. There is no way for one thread to access the local variables of another thread. While objects and their instance variables can be shared between threads in a java program, for that one thread needs to pass the object reference to the other thread. The static variables are automatically shared between all threads in a java program.
 


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

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