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


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

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




###############
test 4
###############

*****   =======================
4.1 What will happen when you invoke the following method?
void infiniteLoop(){
   byte b = 1;
   while ( ++b > 0 );
   System.out.println("Welcome to My World!");
}
A. The loop never ends(infinite loop).
B. Prints "Welcome to My World!".
C. Compilation error : ++ operator should not be used for byte type variables.
D. Prints nothing.
Answer: B
NOte: Here the variable 'b' will go up to 127. After that overflow will occur and 'b' will be set to -ve value, the loop ends and prints "Welcome to My World!".

****    =======================
4.2 Which of the following statements are true about java.util.IdentityHashMap?
A. It implements Map interface
B. It is synchronized
C. In an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2).
D. In an IdentityHashMap, two keys k1 and k2 are considered equal if and only if k1.equals(k2) OR if both k1 and k2 are null
E. It violates general contract of the interface Map
F. It keeps its keys in a sorted order
Answer: ACE

****    ===================
4.3 Which of the following results in a positive value of a?
A. int  a = -1; a = a >>> 5;
B. int  a = -1; a = a >>> 32;
C. byte a = -1; a = (byte)(a >>> 30);
D. int  a = -1; a = a >> 5;
Answer: AC

****    =====================
4.4 Select the statements which are valid from the list below.
A. The primitive data type "char" is 16-bits wide.
B. Unicode uses 16-bits to represent a character.
C. Unicode uses 32-bits to represent a character.
D. UTF uses 24-bits to represent a character.
E. UTF uses as many bits as are needed to represent a character.
Answer: ABE

***     =====================
4.5 You are writing a set of classes related to cooking and have created your own exception hierarchy derived from java.lang.Exception as follows:
  Exception
  +-- BadTasteException
         +-- BitterException
         +-- SourException
Your base class, "BaseCook" has a method declared as follows:
  int rateFlavor(Ingredient[] list) throws BadTasteException
class "TexMexCook", derived from BaseCook has a method which overrides BaseCook.rateFlavor(). Which of the following are legal declarations of the overriding method?
A. int rateFlavor(Ingredient[] list) throws BadTasteException
B. int rateFlavor(Ingredient[] list) throws Exception
C. int rateFlavor(Ingredient[] list) throws BitterException
D. int rateFlavor(Ingredient[] list)
Answer: ACD
Note: the overriding method must throw the same exception or a subclass.the overriding method need not have to throw an exception.

*****   =====================
4.6 Which is the most appropriate code snippet that can be inserted at line 18 in the following code?
(Assume that the code is compiled and run with assertions enabled)
1.  import java.util.*;
2.  
3.  public class AssertTest
4.  {
5.      private HashMap cctld;
6.      
7.      public AssertTest()
8.      {
9.          cctld = new HashMap();
10.         cctld.put("in", "India");
11.         cctld.put("uk", "United Kingdom");
12.         cctld.put("au", "Australia");
13.         // more code...
14.     }
15.     // other methods ....  
16.     public String getCountry(String countryCode)
17.     {
18.         // What should be inserted here?
19.         String country = (String)cctld.get(countryCode);
20.         return country;
21.     }
22. }
A. assert countryCode != null;
B. assert countryCode != null : "Country code can not be null" ;
C. assert cctld != null : "No country code data is available";
D. assert cctld : "No country code data is available";
E. assert cctld.size() != 0 : "No country code data is available";
Answer: C
Note: assertions should be used to check for conditions that should never happen.

**      =================
4.7 Which of the following will print exactly 7?
1.  class MyClass
2.  {
3.      public static void main(String args[])
4.      {
5.          double x = 6.5;
6.          System.out.println(Math.floor(x+1));
7.          System.out.println(Math.ceil(x));
8.          System.out.println(Math.round(x));
9.      }
10. }
A. Line 6 only
B. Lines 6 and 7 only
C. Lines 6, 7 and 8
D. None of these
Answer: D 8
Note: is double and output is 7.0

*****   ========================
4.8 Consider the following statement:
   Thread myThread = new Thread();
Which of the following statements are true regarding myThread?
A. The thread myThread is now in a runnable state.
B. The thread myThread has a priority of 5.
C. On calling the start() method on myThread, the run method in the Thread class will be executed.
D. On calling the start() method on myThread, the run method in the calling class will be executed.
Answer: C
Note: the priority of myThread will be inherited from the Thread that called the constructor.

 


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

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