⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java认证历年真题scjp考试真题和解析[1].txt

📁 包括多年scjp认证考试的很多题库及答案
💻 TXT
📖 第 1 页 / 共 4 页
字号:
JAVA认证历年真题:SCJP考试真题和解析[1] 
2007-04-10 03:53:20  作者:唐易龙  来源:ITZERO收集整理  浏览次数:96  文字大小:【大】【中】【小】
关键字:java 
 
例题1: 
  Choose the three valid identifiers from those listed below. 

  A. IDoLikeTheLongNameClass 

  B. $byte 

  C. const 

  D. _ok 

  E. 3_case 

  解答:A, B, D 

  点评:Java中的标示符必须是字母、美元符($)或下划线(_)开头。关键字与保留字不能作为标示符。选项C中的const是Java的保留字,所以不能作标示符。选项E中的3_case以数字开头,违反了Java的规则。 

  例题2: 

  How can you force garbage collection of an object? 

  A. Garbage collection cannot be forced 

  B. Call System.gc(). 

  C. Call System.gc(), passing in a reference to the object to be garbage collected. 

  D. Call Runtime.gc(). 

  E. Set all references to the object to new values(null, for example). 

  解答:A 

  点评:在Java中垃圾收集是不能被强迫立即执行的。调用System.gc()或Runtime.gc()静态方法不能保证垃圾收集器的立即执行,因为,也许存在着更高优先级的线程。所以选项B、D不正确。选项C的错误在于,System.gc()方法是不接受参数的。选项E中的方法可以使对象在下次垃圾收集器运行时被收集。
 例题3:
 Consider the following class: 

  1. class Test(int i) { 

  2. void test(int i) { 

  3. System.out.println(“I am an int.”); 

  4. } 

  5. void test(String s) { 

  6. System.out.println(“I am a string.”); 

  7. } 
  
  8. 
  
  9. public static void main(String args[]) { 

  10. Test t=new Test(); 

  11. char ch=“y”; 

  12. t.test(ch); 

  13. } 

  14. } 

  Which of the statements below is true?(Choose one.) 

  A. Line 5 will not compile, because void methods cannot be overridden. 

  B. Line 12 will not compile, because there is no version of test() that rakes a char argument. 

  C. The code will compile but will throw an exception at line 12. 

  D. The code will compile and produce the following output: I am an int. 

  E. The code will compile and produce the following output: I am a String. 

  解答:D 

  点评:在第12行,16位长的char型变量ch在编译时会自动转化为一个32位长的int型,并在运行时传给void test(int i)方法。

例题4: 

  Which of the following lines of code will compile without error? 

  A. 
  int i=0; 
  if (i) { 
  System.out.println(“Hi”); 
  } 

  B. 
  boolean b=true; 
  boolean b2=true; 
  if(b==b2) { 
  System.out.println(“So true”); 
  } 

  C. 
  int i=1; 
  int j=2; 
  if(i==1|| j==2) 
  System.out.println(“OK”); 

  D. 
  int i=1; 
  int j=2; 
  if (i==1 &| j==2) 
  System.out.println(“OK”); 

  解答:B, C 

  点评:选项A错,因为if语句后需要一个boolean类型的表达式。逻辑操作有^、&、| 和 &&、||,但是“&|”是非法的,所以选项D不正确。 

  例题5: 

  Which two demonstrate a "has a" relationship? (Choose two) 

  A. public interface Person { }

  public class Employee extends Person{ } 


  B. public interface Shape { } 
  public interface Rectandle extends Shape { } 

  C. public interface Colorable { } 
  public class Shape implements Colorable 
  { } 

  D. public class Species{ } 
  public class Animal{private Species species;} 

  E. interface Component{ } 
  class Container implements Component{ 
  private Component[] children; 
  } 

  解答:D, E 

  点评: 在Java中代码重用有两种可能的方式,即组合(“has a”关系)和继承(“is a”关系)。“has a”关系是通过定义类的属性的方式实现的;而“is a”关系是通过类继承实现的。本例中选项A、B、C体现了“is a”关系;选项D、E体现了“has a”关系。 

  例题6: 

  Which two statements are true for the class java.util.TreeSet? (Choose two) 
  
  A. The elements in the collection are ordered. 

  B. The collection is guaranteed to be immutable. 

  C. The elements in the collection are guaranteed to be unique. 

  D. The elements in the collection are accessed using a unique key. 
  E. The elements in the collection are guaranteed to be synchronized 
  解答:A, C 

  点评:TreeSet类实现了Set接口。Set的特点是其中的元素惟一,选项C正确。由于采用了树形存储方式,将元素有序地组织起来,所以选项A也正确。

例题7: 


  True or False: Readers have methods that can read and return floats and doubles. 

  A. Ture 

  B. False 

  解答:B 

  点评: Reader/Writer只处理Unicode字符的输入输出。float和double可以通过stream进行I/O. 

  例题8: 

  What does the following paint() method draw? 

  1. public void paint(Graphics g) { 

  2. g.drawString(“Any question”, 10, 0); 

  3. } 

  A. The string “Any question?”, with its top-left corner at 10,0 

  B. A little squiggle coming down from the top of the component. 

  解答:B 

  点评:drawString(String str, int x, int y)方法是使用当前的颜色和字符,将str的内容显示出来,并且最左的字符的基线从(x,y)开始。在本题中,y=0,所以基线位于最顶端。我们只能看到下行字母的一部分,即字母‘y’、‘q’的下半部分。 

  例题9: 

  What happens when you try to compile and run the following application? Choose all correct options. 

  1. public class Z { 

  2. public static void main(String[] args) { 

  3. new Z(); 

  4. } 

  5. 

  6. Z() { 

  7. Z alias1 = this; 

  8. Z alias2 = this; 

  9. synchronized(alias1) { 

  10. try { 

  11. alias2.wait(); 

  12. System.out.println(“DONE WAITING”); 

  13. } 

  14. catch (InterruptedException e) { 


  15. System.out.println(“INTERR 
  UPTED”); 

  16. } 

  17. catch (Exception e) { 
 
  18. System.out.println(“OTHER EXCEPTION”); 

  19. } 

  20. finally { 

  21. System.out.println 
  (“FINALLY”); 

  22. } 

  23. } 

  24. System.out.println(“ALL DONE”); 

  25. } 

  26. } 

  A. The application compiles but doesn't print anything. 

  B. The application compiles and print “DONE WAITING” 

  C. The application compiles and print “FINALLY” 

  D. The application compiles and print “ALL DONE” 

  E. The application compiles and print “INTERRUPTED” 

  解答:A 

  点评:在Java中,每一个对象都有锁。任何时候,该锁都至多由一个线程控制。由于alias1与alias2指向同一对象Z,在执行第11行前,线程拥有对象Z的锁。在执行完第11行以后,该线程释放了对象Z的锁,进入等待池。但此后没有线程调用对象Z的notify()和notifyAll()方法,所以该进程一直处于等待状态,没有输出。 


13. Which are not Java primitive types? 
  A. short

  B. Boolean

  C. unit

  D. float 
  翻译
  下面哪些不是java的原始数据类型。 

  答案 B,C 

  解析 Java的原始数据类型一共就八个,分别是:byte,short,int,long,boolean,char,float,double。注意这些是大小写敏感的,而Boolean是booelan的封装类(wrapper class)。 


  14. Use the operators "<<", ">>", which statements are true? 
  A. 0000 0100 0000 0000 0000 0000 0000 0000<<5 gives

  1000 0000 0000 0000 0000 0000 0000 0000

  B. 0000 0100 0000 0000 0000 0000 0000 0000<<5 gives

  1111 1100 0000 0000 0000 0000 0000 0000

  C. 1100 0000 0000 0000 0000 0000 0000 0000>>5 gives 

1111 1110 0000 0000 0000 0000 0000 0000

  D. 1100 0000 0000 0000 0000 0000 0000 0000>>5 gives

  0000 0110 0000 0000 0000 0000 0000 0000 
  翻译
  使用"<<"和 ">>"操作符的哪些陈述是对的。 

  答案 A,C
  
  解析 Java的移位操作符一共有三种,分别是”>>”,”>>>”,”<<”,执行的操作分别是有符号右移,无符号右移,左移,有符号右移的意思是说移入的最高位和原最高符号位相同,无符号右移是移入位始终补零,左移时最低位始终补零,最高位被舍弃。移位操作符另一个非常值得注意的特点是其右操作数是取模运算的,意思是说对于一个int型数据而言,对它移位32位的结果是保持不变而非变成零,即:a>>32的结果是a而不是0,同理,对long型数是对右操作数取64的模,a>>64==a;还有一点需要注意的是移位操作符”>>>”只对int型和long型有效,对byte或者short的操作将导致自动类型转换,而且是带符号的。

  15. Which of the following range of int is correct? 
  A. -27 -- 27-1 

  B. 0 -- 232-1 

  C. ?215 -- 215-1 

  D. ?231 -- 231-1 
  翻译
  int的取值范围是哪个。 

  答案 D 

  解析 int型是32位的。参看第一题的论述。

16. Which keyword should be used to enable interaction with the lock of an 
object? The flag allows exclusive access to that object. 
  A. transient

  B. synchronized

  C. serialize

  D. static 
  翻译
  下面的哪些关键字通常用来对对象的加锁,该标记使得对对象的访问是排他的 

  答案 B 
  
  解析 由于java是多线程的语言,多个线程可以”同时”访问同一数据区,而在处理某些数据时不希望其它的线程修改那些数据的值或者某些操作是不可打断的,要做到这个,可以使用synchronized关键字声明这一点。

  17. Which is the return type of the method main()? 
  A. int

  B. void

  C. boolean

  D. static 
  翻译
  main()方法的返回类型是什么? 

  答案 B 

  解析  在java中,程序运行的入口就是main()方法,它必须是这样的形式:public static void main(String args[])。但是严格来讲这个题目的答案还可以加上a和c,因为并没有限定是程序入口的main()方法,而main()方法是可以重载的。一般意义上的main()当然就是指我们刚开始所说的main()方法了。

  18. Given the following code:

  if (x>0) { System.out.println("first"); }

  else if (x>-3) { System.out.println("second"); }

  else { System.out.println("third"); }

  Which range of x value would print the string "second"? 
  A. x > 0

  B. x > -3

  C. x <= -3

  D. x <= 0 & x > -3 

  翻译
  给出下面的代码: 


  …

  x的取值在什么范围内时将打印字符串"second"。 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -