In the following pieces of code, A and D will compile without any error. True/False?
A: StringBuffer sb1 = "abcd";
B: Boolean b = new Boolean("abcd");
C: byte b = 255;
D: int x = 0x1234;
E: float fl = 1.2;
Choices:
A. True
B. False
―――――――――――――――――――――――――――――――
答案 B. The code segments B and D will compile without any error. A is not a valid way to construct a StringBuffer, you need to creat a StringBuffer object using "new". B is a valid construction of a Boolean (any string other than "true" or "false" to the Boolean constructor will result in a Boolean with a value of "false"). C will fail to compile because the valid range for a byte is -128 to +127 (ie, 8 bits,signed). D is correct, 0x1234 is the hexadecimal representation in java. E fails to compile because the compiler interprets 1.2 as a double being assigned to a float (down-casting), which is not valid. You either need an explicit cast (as in "(float)1.2") or "1.2f", to indicate a float.
What will be the result of executing the following code?
Given that Test1 is a class.
1. Test1[] t1 = new Test1[10];
2. Test1[][] t2 = new Test1[5][];
3. if (t1[0] == null)
4. {
5.t2[0] = new Test1[10]
6.t2[1] = new Test1[10]
7.t2[2] = new Test1[10]
8.t2[3] = new Test1[10]
9.t2[4] = new Test1[10]
10. }
11. System.out.println(t1[0]);
12. System.out.println(t2[1][0]);
Choices:
a. The code will not compile because the array t2 is not initialized in an unconditional statement before use.
b. The code will compile but a runtime exception will be thrown at line 12.
c. The code will compile but a runtime exception will be thrown at line 11.
d. None of these.
―――――――――――――――――――――
D is correct. Though we cannot use local variables without initializing them (compilation error), there is an exception to it. In case of arrays initialization is supposed to be complete when we specify the leftmost dimension of the array. The problem occurs at runtime if we try to access an element of the array which has not been initialized (specification of size). In the question above the array t2 is initialized before use, therefore there will be no problem at runtime also and the lines 11 and 12 will both print null.
Which declarations of identifiers are legal?
A. $persons
B. TwoUsers
C. *point
D. this
E. _endline
答案 A,B,E
解析 Java的標識符可以以一個Unicode字符,下滑線(_),美元符($)開始,后續字
符可以是前面的符號和數字,沒有長度限制,大小寫敏感,不能是保留字。
Which of the following answer is correct to express the value 8 in octal number?
A. 010
B. 0x10
C. 08
D. 0x8
翻譯
下面的哪些答案可以用以表示八進制值8。
答案 A
解析 八進制值以0開頭,以0x開頭的為十六進制值,八進制中不能出現數字8,最大只有7。
Which are not Java keywords?
A. TRUE
B. sizeof
C. const
D. super
E. void
翻譯
哪些不是Java關鍵字。
答案 A,B
解析
A:不是,Java中有true,但是這也不是關鍵字而是字面量(literal)。
B:不是,Java中不需要這個操作符,所有的類型(原始類型)的大小都是固定的。
C、D、E都是,需要說明的是const是java中未被使用的關鍵字。
Which statements about Java code security are true?
A. The bytecode verifier loads all classes needed for the execution of a program.
B. Executing code is performed by the runtime interpreter.
C. At runtime the bytecodes are loaded, checked and run in an interpreter.
D. The class loader adds security by separating the namespaces for the
classes of the local file system from those imported from network sources.
――――――――――――――
答案 BCD
§1.1.7 七
題目:下面有關java代碼安全性的敘述哪些是對的。
A. 字節碼校驗器加載查詢執行需要的所有類。
B. 運行時解釋器執行代碼。
C. 在運行時,字節碼被加載,驗證然后在解釋器里面運行。
D. 類加載器通過分離本機文件系統的類和從網絡導入的類增加安全性。
SL275中描述的Java程序運行的過程是這樣的:類加載器(class loader)加載程序運行所需要的所有類,它通過區分本機文件系統的類和網絡系統導入的類增加安全性,這可以限制任何的特洛伊木馬程序,因為本機類總是先被加載,一旦所有的類被加載完,執行文件的內存劃分就固定了,在這個時候特定的內存地址被分配給對應的符號引用,查找表(lookuo table)也被建立,由于內存劃分發生在運行時,解釋器在受限制的代碼區增加保護防止未授權的訪問;然后字節碼校驗器(byte code verifier)進行校驗,主要執行下面的檢查:類符合JVM規范的類文件格式,沒有違反訪問限制,代碼沒有造成堆棧的上溢或者下溢,所有操作代碼的參數類型都是正確的,沒有非法的數據類型轉換(例如將整型數轉換成對象類型)發生;校驗通過的字節碼被解釋器(interpreter)執行,解釋器在必要時通過運行時系統執行對底層硬件的合適調用。后三個答案是SL275中的原話。
§1.1.8 八
Which fragments are correct in Java source file?
A. package testpackage;
public class Test{//do something...}
B. import java.io.*;
package testpackage;
public class Test{// do something...}
C. import java.io.*;
class Person{// do something...}
public class Test{// do something...}
D. import java.io.*;
import java.awt.*;
public class Test{// do something...}
------------------------
答案 ACD
§1.1.9 九
Which of the following statements are legal?
A. long l = 4990;
B. int i = 4L;
C. float f = 1.1;
D. double d = 34.4;
E. double t = 0.9F.
----------------------------
答案 ADE