我思故我強(qiáng)

          第四部分:語(yǔ)言基礎(chǔ)

          第四部分:語(yǔ)言基礎(chǔ)

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

          §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. 第二行的聲明不會(huì)為變量myVect分配內(nèi)存空間。

            B. 第二行的聲明分配一個(gè)到Vector對(duì)象的引用的內(nèi)存空間。

            C. 第二行語(yǔ)句創(chuàng)建一個(gè)Vector類對(duì)象。

            D. 第三行語(yǔ)句創(chuàng)建一個(gè)Vector類對(duì)象。

            E. 第三行語(yǔ)句為一個(gè)Vector類對(duì)象分配內(nèi)存空間。

           

            答案 A,D,E

           

            解析

            SL-275中指出:要為一個(gè)新對(duì)象分配空間必須執(zhí)行new

          Xxx()調(diào)用,new調(diào)用執(zhí)行以下的操作:

           

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

           

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

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

           

            3.執(zhí)行構(gòu)造函數(shù)。

           

            4.變量被分配為一個(gè)到內(nèi)存堆中的新對(duì)象的引用。

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

           

            題目:下面關(guān)于變量及其范圍的陳述哪些是對(duì)的。

           

            A. 實(shí)例變量是類的成員變量。

           

            B. 實(shí)例變量用關(guān)鍵字static聲明。

           

            C. 在方法中定義的局部變量在該方法被執(zhí)行時(shí)創(chuàng)建

           

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

           

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

          k

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

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

          ass

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

          量的方法被調(diào)用時(shí)被創(chuàng)建,而在該方法退出后被撤銷;實(shí)例變量在使用new

          Xxxx()創(chuàng)建該類的實(shí)例時(shí)被創(chuàng)建,而其生存期和該類的實(shí)例對(duì)象的生存期相同;類變量在該

          類被加載時(shí)被創(chuàng)建,不一定要用new

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

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

          實(shí)例變量在該類的構(gòu)造方法被調(diào)用時(shí)為它分配的缺省的值,整型是0,布爾型是false,而浮點(diǎn)

          型是0.0f,引用類型(類類型)的實(shí)例變量的缺省值是null(沒(méi)有進(jìn)行實(shí)際的初始化,對(duì)它的

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

          是在類被加載時(shí)。

          §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的內(nèi)容,子類可以重寫父類的方法并將之聲明為抽象方法,但是這引發(fā)的問(wèn)題是類必須聲明為抽象類,否則編譯不能通過(guò),而且抽象方法不能有方法體,也就是方法聲明后面不能帶上那兩個(gè)大括號(hào)({}),這些D都不能滿足。

          posted on 2009-10-16 11:40 李云澤 閱讀(256) 評(píng)論(0)  編輯  收藏 所屬分類: 面試筆試相關(guān)的SCJP認(rèn)證學(xué)習(xí)

          主站蜘蛛池模板: 西宁市| 固始县| 壤塘县| 博客| 金乡县| 驻马店市| 颍上县| 福海县| 清丰县| 肃南| 德兴市| 彰化市| 曲麻莱县| 兴化市| 铁岭县| 海伦市| 英德市| 汝州市| 青岛市| 石台县| 睢宁县| 河北省| 五指山市| 台山市| 正镶白旗| 搜索| 宜都市| 长阳| 龙门县| 阿坝| 尉氏县| 紫阳县| 龙泉市| 理塘县| 商都县| 灵丘县| 辰溪县| 华宁县| 萨嘎县| 阿拉善右旗| 盐源县|