我想考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