The following skeleton code defines the class “Fred”, which is used in the next few questions:public class Fred
{
    private int x;    public Fred()
    {
                               this(‘a’);
                               System.out.print("a");
    }
   
    public Fred(char c)
    {
           System.out.print(c);
    }    public void a()
    {
           System.out.print(“a”);
    }
}1. As a result of executing the following two lines of code, which create and use an instance of “Fred”, how many occurrences of the letter “a” will be printed to the standard output window? Fred f = new Fred();
f.a();(a) 1
(b)  2
(c)  3 
(d)  4
 2. As a result of executing the following two lines of code, how many occurrences of the letter “a” will be printed to the standard output window? Fred f = new Fred(‘a’);
f.a();(a) 1
(b)  2
(c)  3 
(d)  4
Consider the following code:         String s1 = new String("one");
         String s2 = new String("one"); Immediately after the execution of the above lines, the two Boolean expressions
“s1 == s2” and “s1.equals(s2)” respectively evaluate to be: a) false and true 
b) true and false
c) false and false
d) true and true

解决方案 »

  1.   

    哦,有3题
    第二题 2次
    第三题 false and true
      

  2.   

    第一题,程序调的是默认构建器,打印1次,默认构建器调用携参构建器,打印1次。程序中调用a()方法,打印第3次。
    第二题,程序调用携参构建器,打印1次。然后程序中调用a()方法,打印第2次。
    第三题,两个字符串内容一样,所以equals成立。但两个字符串是不同的对象,==不成立。