<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我思故我強

    第四部分:語言基礎

    第四部分:語言基礎

    • 能正確聲明包、import、類(包括內部類)、接口、方法(包括用于開始一個類的執行的main方法)、變量,并正確使用修飾符。
    • 能夠正確使用實現了接口的類,包括java.lang.Runnable接口或題目中指定的接口。
    • 知道命令行參數是如何傳遞給類的main方法的。
    • 知道哪些是JAVA的合法keyword。注意:考試中不會出現要你解釋keyword和各種常數這種深奧的問題。
    • 明白如果沒有顯式地賦值,各種變量或者數組的默認值是什么。
    • 掌握所有原始數據類型的取值范圍,如何聲明各種數據類型。

    §1.1.1      

    1. What will happen when you attempt to compile and run the following code?

    public class Static

    {

    static

    {

    int x = 5;

    }

    static int x,y;

    public static void main(String args[])

    {

           x--;

           myMethod();

           System.out.println(x + y + ++x);

    }

    public static void myMethod()

    {

    y = x++ + ++x;

    }

    }

    Choices:

    a. Compiletime error

    b. prints : 1

    c. prints : 2

    d. prints : 3

    e. prints : 7

    f. prints : 8

    ――――――――――――――――――――

    1) D is the correct choice. The above code will not give any compilation error. Note that "Static" is a valid class name. Thus choice A is incorrect. In the above code, on execution, first the static variables (x and y) will be initialized to 0. Then static block will be called and finally main() method will be called. The execution of static block will have no effect on the output as it declares a new variable (int x). The first statement inside main (x--) will result in x to be -1. After that myMethod() will be executed. The statement "y = x++ + ++x;" will be evaluated to y = -1 + 1 and x will become 1. In case the statement be "y =++x + ++x", it would be evaluated to y = 0 + 1 and x would become 1. Finally when System.out is executed "x + y + ++x" will be evaluated to "1 + 0 + 2" which result in 3 as the output. Thus choice D is correct.

     

    §1.1.2      

    Considering the following code, Which variables may be referenced correctly at line 12?

    1.public class Outer

    2.{

    3.public int a = 1;

    4.private int b = 2;

    5.public void method(final int c)

    6.{

    7.int d = 3;

    8.class Inner

    9.{

    10.private void iMethod(int e)

    11. {

    12.

    13.}

    14.}

    15.}

    16.}

    Choices:

    a. a

    b. b

    c. c

    d. d

    e. e

    A, B, C and E are correct. Since Inner is not a static inner class, it has a reference to an enclosing object, and all the variables of that object are accessible. Therefore A and B are correct, even if b is private. Variables in the enclosing method are only accessible when they are marked as final hence c is accessible but not d. E is obviously correct as it is a parameter to the method containing line 12 itself.

     

    §1.1.3      

    What will be the result of executing the following code?

    // Filename; SuperclassX.java

    package packageX;

    public class SuperclassX

    {

    protected void superclassMethodX()

    {

    }

    int superclassVarX;

    }

     

    // Filename SubclassY.java

    1.package packageX.packageY;

    2.

    3.public class SubclassY extends SuperclassX

    4.{

    5.SuperclassX objX = new SubclassY();

    6.SubclassY objY = new SubclassY();

    7.void subclassMethodY()

    8.{

    9.objY.superclassMethodX();

    10.int i;

    11.i = objY.superclassVarX;

    12.}

    13.}

    Choices:

    a.Compilation error at line 5

    b. Compilation error at line 9

    c. Runtime exception at line 11

    d. None of these

    ――――――――

    D is correct. When no access modifier is specified for a member, it is only accessible by another class in the package where its class is defined. Even if its class is visible in another package, the member is not accessible there. In the question above the variable superclassVarX has no access modifier specified and hence it cannot be accessed in the packageY even though the class SuperclassX is visible and the protected method superclassMethodX() can be accessed. Thus the compiler will raise an error at line 11.

     

    §1.1.4      

     Consider the class hierarchy shown below:

         --------------------------------------------------------------------

    class FourWheeler implements DrivingUtilities

    class Car extends FourWheeler

    class Truck extends FourWheeler

    class Bus extends FourWheeler

    class Crane extends FourWheeler

    ----------------------------------------------------------------------

       

    Consider the following code below:

    1.DrivingUtilities du;

    2.FourWheeler fw;

    3.Truck myTruck = new Truck();

    4.du = (DrivingUtilities)myTruck;

    5.fw = new Crane();

    6.fw = du;

    Which of the statements below are true?

    Choices:

    a. Line 4 will not compile because an interface cannot refer to an object.

    b. The code will compile and run.

    c. The code will not compile without an explicit cast at line 6, because going

    down the hierarchy without casting is not allowed.

    d.The code at line 4 will compile even without the explicit cast.

    e.The code will compile if we put an explicit cast at line 6 but will throw an exception at runtime.

    ―――――――――――

    C and D are correct. A and B are obviously wrong because there is nothing wrong in an interface referring to an object. C is correct because an explicit cast is needed to go down the hierarchy. D is correct because no explicit cast is needed at line 4, because we are going up the hierarchy. E is incorrect because if we put an explicit cast at line 6, the code will compile and run perfectly fine, no exception will be thrown because the runtime class of du (that is Truck) can be converted to type FourWheeler without any problem.

     

    §1.1.5      

    What will be printed when you execute the following code?

    class X

    {

    Y b = new Y();

           X()

    {

    System.out.print("X");

    }

    }

    class Y

    {

           Y()

    {

    System.out.print("Y");

    }

    }

    public class Z extends X

    {

           Y y = new Y();

           Z()

    {

    System.out.print("Z");

    }

           public static void main(String[] args)

    {

                   new Z();

           }

    }

    Choices:

    a. Z

    b. YZ

    c. XYZ

    d. YXYZ

    ―――――――――

    D is correct. A difficult but a fundamental question, please observe carefully. Before any object is constructed the object of the parent class is constructed(as there is a default call to the parent's constructor from the constructor of the child class via the super() statement). Also note that when an object is constructed the variables are initialized first and then the constructor is executed. So when new Z() is executed , the object of class X will be constructed, which means Y b = new Y() will be executed and "Y" will be printed as a result. After that constructor of X will be called which implies "X" will be printed. Now the object of Z will be constructed and thus Y y = new Y() will be executed and Y will be printed and finally the constructor Z() will be called and thus "Z" will be printed. Thus YXYZ will be printed.

     

    §1.1.6      

    What will happen when you attempt to compile and run the following code?

    class Base

    {

    int i = 99;

    public void amethod()

    {

           System.out.println("Base.amethod()");

         }

         Base()

    {

         amethod();

         }

    }

    public class Derived extends Base

    {

    int i = -1;

           

    public static void main(String argv[])

    {

         Base b = new Derived();

           System.out.println(b.i);

           b.amethod();

         }

         public void amethod()

    {

           System.out.println("Derived.amethod()");

         }

    }

    Choices:

    a. Derived.amethod()

    -1

    Derived.amethod()

    b. Derived.amethod()

    99

    c.Derived.amethod()

    99

    d. Derived.amethod()

    e.Compile time error

    ――――――――

    B is correct. The reason is that this code creates an instance of the Derived class but assigns it to a reference of a the Base class. In this situation a reference to any of the fields such as i will refer to the value in the Base class, but a call to a method will refer to the method in the class type rather than its reference handle. But note that if the amethod() was not present in the base class then compilation error would be reported as at compile time, when compiler sees the statement like b.amethod(), it checks if the method is present in the base class or not. Only at the run time it decides to call the method from the derived class.

     

    §1.1.7      

    Given the following code fragment:

     

      1) public void create() {

     

      2) Vector myVect;

     

      3) myVect = new Vector();

     

      4) }

     

      Which of the following statements are true?

      A. The declaration on line 2 does not allocate memory space for the

    variable myVect.

     

      B. The declaration on line 2 allocates memory space for a reference to a

    Vector object.

     

      C. The statement on line 2 creates an object of class Vector.

     

      D. The statement on line 3 creates an object of class Vector.

     

      E. The statement on line 3 allocates memory space for an object of class

    Vector

      翻譯

      給出下面的代碼片斷。。。下面的哪些陳述為true(真)?

      A. 第二行的聲明不會為變量myVect分配內存空間。

      B. 第二行的聲明分配一個到Vector對象的引用的內存空間。

      C. 第二行語句創建一個Vector類對象。

      D. 第三行語句創建一個Vector類對象。

      E. 第三行語句為一個Vector類對象分配內存空間。

     

      答案 A,D,E

     

      解析

      SL-275中指出:要為一個新對象分配空間必須執行new

    Xxx()調用,new調用執行以下的操作:

     

      1.為新對象分配空間并將其成員初始化為0或者null。

     

       2.執行類體中的初始化。(例如在類中有一個成員聲明int a=10;在第一步后a=0

    ,執行到第二步后a=10)

     

      3.執行構造函數。

     

      4.變量被分配為一個到內存堆中的新對象的引用。

    §1.1.8      

    Which of the following statements about variables and their scopes

    are true?

     

      A. Instance variables are member variables of a class.

     

      B. Instance variables are declared with the static keyword.

     

      C. Local variables defined inside a method are created when the method

    is executed.

     

      D. Local variables must be initialized before they are used.

     

      (acd)

     

      題目:下面關于變量及其范圍的陳述哪些是對的。

     

      A. 實例變量是類的成員變量。

     

      B. 實例變量用關鍵字static聲明。

     

      C. 在方法中定義的局部變量在該方法被執行時創建

     

      D. 局部變量在使用前必須被初始化。

     

      類中有幾種變量,分別是:局部變量(英文可以為:local\automatic\temporary\stac

    k

    variable)是定義在方法里的變量;實例變量(英文為:instance

    variable)是在方法外而在類聲明內定義的變量,有時也叫成員變量;類變量(英文為:cl

    ass

    variable)是用關鍵字static聲明的實例變量,他們的生存期分別是:局部變量在定義該變

    量的方法被調用時被創建,而在該方法退出后被撤銷;實例變量在使用new

    Xxxx()創建該類的實例時被創建,而其生存期和該類的實例對象的生存期相同;類變量在該

    類被加載時被創建,不一定要用new

    Xxxx()創建,所有該類的實例對象共享該類變量,其生存期是類的生存期。任何變量在使用

    前都必須初始化,但是需要指出的是局部變量必須顯式初始化,而實例變量不必,原始類型的

    實例變量在該類的構造方法被調用時為它分配的缺省的值,整型是0,布爾型是false,而浮點

    型是0.0f,引用類型(類類型)的實例變量的缺省值是null(沒有進行實際的初始化,對它的

    使用將引起NullPointException),類變量的規則和實例變量一樣,不同的是類變量的初始化

    是在類被加載時。

    §1.1.9      

      public class Parent {

      int change() {…}

      }

      class Child extends Parent {

     

      }

      Which methods can be added into class Child?

      A. public int change(){}

     

      B. int chang(int i){}

     

      C. private int change(){}

     

      D. abstract int chang(){}

     

      (ab)

     

      題目:哪些方法可被加入類Child。

     

      需要注意的是答案D的內容,子類可以重寫父類的方法并將之聲明為抽象方法,但是這引發的問題是類必須聲明為抽象類,否則編譯不能通過,而且抽象方法不能有方法體,也就是方法聲明后面不能帶上那兩個大括號({}),這些D都不能滿足。

    posted on 2009-10-16 11:40 李云澤 閱讀(253) 評論(0)  編輯  收藏 所屬分類: 面試筆試相關的SCJP認證學習

    主站蜘蛛池模板: 拔擦拔擦8x华人免费久久| 免费av一区二区三区| 大陆一级毛片免费视频观看| 亚洲视频一区在线| 999国内精品永久免费视频| 亚洲国产精品久久人人爱| 亚洲三级在线免费观看| tom影院亚洲国产一区二区| 24小时日本在线www免费的| 亚洲另类无码专区首页| 免费a级毛片无码a∨性按摩| 一级毛片免费播放视频| 国产亚洲精品va在线| 久久久久国产免费| 亚洲成AV人片久久| 国内精品免费视频自在线| 日韩大片在线永久免费观看网站 | 国产在线观看麻豆91精品免费| 亚洲精品第五页中文字幕| 成人奭片免费观看| 国产精品亚洲二区在线| 亚洲熟妇av一区二区三区| 无码日韩精品一区二区免费暖暖| 亚洲国产成人精品青青草原| 日韩免费福利视频| 日韩精品无码免费专区网站| 亚洲成av人片不卡无码| 国产人妖ts在线观看免费视频| 国产真人无码作爱免费视频| 亚洲色大成网站www永久| 欧洲精品免费一区二区三区| h在线看免费视频网站男男| 久久精品国产亚洲AV无码娇色 | 亚洲人成电影亚洲人成9999网| 色妞WWW精品免费视频| 日本一区二区在线免费观看| 91亚洲一区二区在线观看不卡| 国产男女猛烈无遮挡免费网站| 国产免费AV片在线观看| 亚洲成在人线在线播放无码| 亚洲日韩aⅴ在线视频|