SCJP考试练习题1

本文是关于SCJP考试的练习题目及解析,涵盖了Java关键字、垃圾收集、方法重载、逻辑运算符以及TreeSet类的特点。题目涉及Java语言的基础知识和核心概念。
摘要由CSDN通过智能技术生成

我想考SCJP,现在刚开始准备,Java基础很一般。现在开始做些题,练练手,往大家多提意见,分析的不对一定帮我指出,谢谢大家!

 

1.Which of the following are Java keywords?
A. goto
B. malloc
C. extends
D. FALSE Select all correct answers

答案:AC

分析:关键字列表
  abstract, boolean, break, byte, case
  catch, char ,class, continue, default
  do, double, else, extends, false
  final ,finally, float, for, if
  implements, import ,instanceof, int, interface
  long, native ,new, null, package
  private, protected, public, return, short
  static ,super, switch, synchronized ,this
  throw, throws, transient, true, try
  void ,volatile, while 
  保留字
  const,goto
      关键字和保留字都是小写的

--------------------------------------------------------------------------

 

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

分析:调用 gc 方法暗示着 Java 虚拟机做了一些努力来回收未用对象,以便能够快速地重用这些对象当前占用的内存。当控制权从方法调用中返回时,虚拟机已经尽最大努力从所有丢弃的对象中回收了空间。 调用 System.gc() 实际上等效于调用: Runtime.getRuntime().gc()。由于垃圾回收算法的复杂性,就算java虚拟机进行了垃圾回收,也未必将对象回收。

 

---------------------------------------------------------------------------

3.Consider the following class: 
  1. class Test{ 
  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”); 
答案:BC

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

 

---------------------------------------------------------------------------

5.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

答案:AC

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

Collection  
├List(有序,允许相同元素,允许null)  
│├LinkedList (非同步) 
│├ArrayList   (非同步)
│└Vector   (同步)
│ └Stack   (同步)
└Set(有序,不允许相同元素,最多允许1个null)

   ├HashSet(非同步) 
   ├LinkedHashSet(非同步)

   ├TreeSet(非同步)
   └EnumSet (非同步,不允许null)
 
Map(有序,一组key-value映射,不允许相同key)  
├Hashtable (不允许null的key、value)  
├HashMap (允许null的key、value) 
└WeakHashMap  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值