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


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

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



****    ====================
1.11 Which of the following are valid declarations for a native method?
A. public native void aMethod();
B. private native String aMethod();
C. private native boolean aMethod(int val){};
D. public native int aMethod(String test);
E. private native boolean aMethod(boolean flag, Integer val);
Answer: ABDE
Note: The 'native' does not affect the access qualifiers, so a native method can be public or private. It is also valid to return primitives as well as objects from a native method (so returning a String is valid) - it is also valid to pass in objects as well as primitives. Answer C is incorrect, because it attempts to define the actual method, by using '{...}'.

**      =======================
1.12 Which of the following will definitely stop a thread from executing?
A. wait()
B. notify()
C. yield()
D. suspend()
E. sleep()
Answer: ACDE

****    =======================
1.13 Which one of the following fragments shows the most appropriate way to throw an exception? Assume that any undeclared variables have been appropriately declared elsewhere and are in scope and have meaningful values.
A.  1.  Exception e = new MalformedURLException("Invalid URL");
   2.  if (!myURL.isValid()) // myURL is a URL object
   3.  {
   4.      throw e;
   5.  }
B.  1.  if (!myURL.isValid ())
   2.  {
   3.      throw new MalformedURLException("URL " + myURL.getName() + " is not valid");
   4.  }
C.  1.  if (!myURL.isValid ())
   2.  {
   3.      throw IOException;
   4.  }
D.  1.  if (!myURL.isValid ())
   2.  {
   3.      throw "Invalid URL";
   4.  }
E.  1.  if (!myURL.isValid ())
   2.  {
   3.      throw new IOException();
   4.  }
Answer: B



###########
test 2
###########

**      =============
2.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 {
   private void methodA(int i) {
       assert i >= 0 : methodB();
       System.out.println(i);
   }
   private String methodB(){
       return "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 with the message -"The value must not be negative".
C. The code will not compile.
D. None of these
Answer: B

***     ================
2.2 Which of the following are true?
A. Java uses a time-slicing scheduling system for determining which Thread will execute.
B. Java uses a pre-emptive, co-operative system for determining which Thread will execute.
C. Java scheduling is platform dependent and may vary from one implementation to another.
D. You can set the priority of a Thread in code.
Answer: CD

****    ==================
2.3 Which Java collection class can be used to maintain the entries in the order in which they were last accessed?
A. java.util.HashSet
B. java.util.LinkedHashMap
C. java.util.Hashtable
D. java.util.Vector
E. None of these
Answer: B
Note: LinkedHashMap is a very important class of Java's collection framework; it is important to understand its features and usages.

*****   ===================
2.4 What will happen when you attempt to compile and run the following code?
class MyClass {
   static String myName = "SCJP";
   MyClass getMyClass() {    
       System.out.println(myName);
       return null;    
   }
   public static void main(String[ ] args) {
       System.out.println( new MyClass().getMyClass().myName );
   }
}
A. Compile time error
B. Run time error
C. Prints SCJP twice
D. Prints SCJP once
E. None of the above
Answer: C
Note: compiler sees the return type of getMyClass method as MyClass and calling myName on MyClass is valid at compile time.
Also it doesn't give any runtime error which is not very obvious. Please note that a null reference may be used to access a class (static) variable without causing an exception. The static variable is called on the class itself and not on the object. Had the myName variable non-static, the above code at run time would give NullPointerException.

***     =====================
2.5 What results from running the code below?
int a = 5; int b = -2;
System.out.println(a % b);
A. A compiler error saying "%" operator is not valid for negative numbers
B. 1
C. 0
D. -1
E. None of the above
Answer: B
Note: To calculate a % b(a modulus b), the simplest way is, drop any negative signs from both the operands and calculate the result.

***     ================
2.6 A block of text contains 100 words. You are required to make a list of distinct words from those 100 words;moreover you are required to report how many distinct words exist in that block of text. Which Java collection class would you use;and which of its method would give the number of distinct words in the block?
A. java.util.LinkedList class, and its size() method
B. java.util.HashSet class, and its size() method
C. java.util.HashMap class, and its size() method
D. java.util.ArrayList class, and its size() method
E. java.util.Dictionary class, and its size() method
Answer: B

***     ================
2.7 What will be printed by the following code?
String banner = "One man, One vote";
int subInd1 = banner.lastIndexOf("One", 10);
System.out.println(subInd1);
A. 0
B. -1
C. 10
D. None of these
Answer: D 9


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

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