§1.1.1 一
Which of the following lines will print false?
1.public class MyClass
2.{
3.static String s1 = "I am unique!";
4.public static void main(String args[])
5.{
6.String s2 = "I am unique!";
7.String s3 = new String(s1);
8.System.out.println(s1 == s2);
9.System.out.println(s1.equals(s2));
10.System.out.println(s3 == s1);
11.System.out.println(s3.equals(s1));
12.System.out.println(TestClass.s4 == s1);
13.}
14.}
15.
16.class TestClass
17.{
18.static String s4 = "I am unique!";
19.}
Choices:
a. Lines 10 and 12
b. Line 12 only
c. Line 8 and 10
d. None of these
――――――――――
D is correct. Only line 10 will print false. Strings are immutable objects. That is, a string is read only once the string has been created and initialized, and Java optimizes handling of string literals; only one anonymous string object is shared by all string literals with the same contents. Hence in the above code the strings s1, s2 and s4 refer to the same anonymous string object, initialized with the character string: "I am unique!". Thus s1 == s2 and TestClass.s4 will both return true and obviously s1.equals(s2) will return true. But creating string objects using the constructor String(String s) creates a new string, hence s3 == s1 will return false even though s3.equals(s1) will return true because s1 and s3 are referring to two different string objects whose contents are same.
What is displayed when the following code is compiled and executed?
String s1 = new String("Test");
String s2 = new String("Test");
if (s1==s2)
System.out.println("Same");
if (s1.equals(s2))
System.out.println("Equals");
Choices:
a. Same
Equals
b. Equals
c. Same
d. The code compiles, but nothing is displayed upon execution.
e. The code fails to compile.
B is correct. Here s1 and s2 are two different object references, referring to different objects in memory. Please note that operator == checks for the memory address of two object references being compared and not their value. The "equals()" method of String class compares the values of two Strings. Thus s1==s2 will return "false" while s1.equals(s2) will return "true". Thus only "Equals" will be printed.
§1.1.3 三
Given the following code, what will be the output?
class Value
{
public int i = 15;
}
public class Test
{
public static void main(String argv[])
{
Test t = new Test();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
Choices:
a. 15 0
20
b. 15 0
15
c. 20 0
20
d. 0 15
20
―――――――――――――――
A is correct. When we pass references in Java what actually gets passed is the value of that reference (i.e. memory address of the object being referenced and not the actual object referenced by that reference) and it gets passed as value (i.e a copy of the reference is made). Now when we make changes to the object referenced by that reference it reflects on that object even outside of the method being called but any changes made to the reference itself is not reflected on that reference outside of the method which is called. In the example above when the reference v is passed from method first() to second() the value of v is passed. When we assign the value val to v it is valid only inside the method second() and thus inside the method second() what gets printed is 15 (initial value of i in the object referenced by val), then a blank space and then 0 (value of local variable i). After this when we return to the method first() v actually refers to the same object to which it was referring before the method second() was called, but one thing should be noted here that the value of i in that object (referred by v inside the method first()) was changed to 20 in the method second() and this change does reflect even outside the method second(), hence 20 gets printed in the method first(). Thus overall output of the code in consideration is 15 0 20
§1.1.4 四
What will happen when you attempt to compile and run the following code?
interface MyInterface
{
}
public class MyInstanceTest implements MyInterface
{
static String s;
public static void main(String args[])
{
MyInstanceTest t = new MyInstanceTest();
if(t instanceof MyInterface)
{
System.out.println("I am true interface");
}
else
{
System.out.println("I am false interface");
}
if(s instanceof String)
{
System.out.println("I am true String");
}
else
{
System.out.println("I am false String");
}
}
}
Choices:
a. Compiletime error
b. Runtime error
c. Prints : "I am true interface" followed by " I am true String"
d. Prints : "I am false interface" followed by " I am false String"
e. Prints : "I am true interface" followed by " I am false String"
f. Prints : "I am false interface" followed by " I am true String"
――――――――――――
E is the correct choice. The "instanceof" operator tests the class of an object at runtime. It returns true if the class of the left-hand argument is the same as, or is some subclass of, the class specified by the right-hand operand. The right-hand operand may equally well be an interface. In such a case, the test determines if the object at left-hand argument implements the specified interface. In the above case there will not be any compiletime or runtime error. The result of "t instance of MyInterface" will be true as "t" is the object of MyInstanceTest class which implements the MyInstance interface. But the result of "s instanceof String" will be false as "s" refers to null. Thus the output of the above program will be : "I am true interface" followed by " I am false String". Thus choice E is correct and others are incorrect.
§1.1.5 五
What will happen when you attempt to compile and run the following code snippet?
String str = "Java";
StringBuffer buffer = new StringBuffer(str);
if(str.equals(buffer)){
System.out.println("Both are equal");
}else{
System.out.println("Both are not equal");
}
A. it will print – both are not equal
B. it will print – both are equal
C. compile time error
D. Runtime error
A is the correct choice. The equals method overridden in String class returns true if and only if the argument is not null and is a String object that represents the same sequence of characters as this String object. Hence, though the contents of both str and buffer contain "Java", the str.equals(buffer) call results in false.
The equals method of Object class is of form -public boolean equals(Object anObject). Hence, comparing objects of different classes will never result in compile time or runtime error.
10. Which of the following statements are true?
A. The equals() method determines if reference values refer to the same
object.
B. The == operator determines if the contents and type of two separate
objects match.
C. The equals() method returns true only when the contents of two
objects match.
D. The class File overrides equals() to return true if the contents and
type of two separate objects match.
翻譯
下面的哪些敘述為真。
A. equals()方法判定引用值是否指向同一對象。
B. == 操作符判定兩個分立的對象的內容和類型是否一致。
C. equals()方法只有在兩個對象的內容一致時返回true。
D. 類File重寫方法equals()在兩個分立的對象的內容和類型一致時返回true。
答案 A,D
解析 嚴格來說這個問題的答案是不確定的,因為equals()方法是可以被重載的,但是
按照java語言的本意來說:如果沒有重寫(override)新類的equals(),則該方法和
==
操作符一樣在兩個變量指向同一對象時返回真,但是java推薦的是使用equals()方法來判斷
兩個對象的內容是否一樣,就像String類的equals()方法所做的那樣:判定兩個String對象的
內容是否相同,而==操作符返回true的唯一條件是兩個變量指向同一對象。從這個意義上來說
選擇給定的答案。從更嚴格的意義來說正確答案應該只有d。
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的操作將導致自動類型轉換,而且是帶符號的。
String s= "hello";
String t = "hello";
char c[] = {'h','e','l','l','o'} ;
Which return true?
A. s.equals(t);
B. t.equals(c);
C. s==t;
D. t.equals(new String("hello"));
E. t==c.
(acd)
題目:哪些返回true。
這個在前面第10題的equals()方法和==操作符的討論中論述過。==操作符比較的是操作
符兩端的操作數是否是同一個對象,而String的equals()方法比較的是兩個String對象的內容
是否一樣,其參數是一個String對象時才有可能返回true,其它對象都返回假。需要指出的是
由于s和t并非使用new創建的,他們指向內存池中的同一個字符串常量,因此其地址實際上是
相同的(這個可以從反編譯一個簡單的測試程序的結果得到,限于篇幅不列出測試代碼和反編
譯的分析),因此答案c也是正確的。
Class Teacher and Student are subclass of class Person.
Person p;
Teacher t;
Student s;
p, t and s are all non-null.
if(t instanceof Person) { s = (Student)t; }
What is the result of this sentence?
A. It will construct a Student object.
B. The expression_r is legal.
C. It is illegal at compilation.
D. It is legal at compilation but possible illegal at runtime.
(c)
題目:類Teacher和Student都是類Person的子類
…
p,t和s都是非空值
…
這個語句導致的結果是什么
A. 將構造一個Student對象。
B. 表達式合法。
C. 編譯時非法。
D. 編譯時合法而在運行時可能非法。
instanceof操作符的作用是判斷一個變量是否是右操作數指出的類的一個對象,由于ja
va語言的多態性使得可以用一個子類的實例賦值給一個父類的變量,而在一些情況下需要判斷
變量到底是一個什么類型的對象,這時就可以使用instanceof了。當左操作數是右操作數指出
的類的實例或者是子類的實例時都返回真,如果是將一個子類的實例賦值給一個父類的變量,
用instanceof判斷該變量是否是子類的一個實例時也將返回真。此題中的if語句的判斷沒有問
題,而且將返回真,但是后面的類型轉換是非法的,因為t是一個Teacher對象,它不能被強制
轉換為一個Student對象,即使這兩個類有共同的父類。如果是將t轉換為一個Person對象則可
以,而且不需要強制轉換。這個錯誤在編譯時就可以發現,因此編譯不能通過。