我思故我强

第五部分:操作与赋值

第五部分:操作与赋值

  • 能知道各种操作符(包括赋值操作符和intanceof操作符)应用于不同类型的结果。
  • 知道String,Boolean和类使用equals()方法后的结果。
  • 知道当对已经知道值的变量进行&,|,&&,||操作时,哪些操作数被运算了,表达式最终的结果是怎样的。
  • 知道对象和原始类型数据传入方法后,在方法内部进行赋值或其它修改操作的结果。

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

 

§1.1.2      

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.

 

§1.1.6      

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。

 

§1.1.7      

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的操作将导致自动类型转换,而且是带符号的。

 

§1.1.8      

  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也是正确的。

§1.1.9      

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对象则可

以,而且不需要强制转换。这个错误在编译时就可以发现,因此编译不能通过。

 

posted on 2009-10-16 11:41 李云泽 阅读(345) 评论(0)  编辑  收藏 所属分类: 面试笔试相关的SCJP认证学习


只有注册用户登录后才能发表评论。


网站导航: