JAVA 測試I

JAVA EXAM 1---25


JAVA 測試I http://www.reibang.com/p/76b36d18844d
JAVA 測試II http://www.reibang.com/p/e7f7d29b185a
JAVA 測試III http://www.reibang.com/p/64d3495989a5

1

public class Test {
    public void main(){
        System.out.println("Hi");
    }

    public static void main (String [] args)
    {
        Test t=new Test();
        t.main();
    }
}

What will be happen if you compile and run above code?

  1. It will not compile
  2. It will not run
  3. It will compile but will not run
  4. It will output “Hi”

ANS: 4, 因為兩個函數(shù)參數(shù)不同, 屬于函數(shù)重載. 函數(shù)重載只考慮參數(shù)問題.

2

After execution of the code fragment below, what are the value of the variables x1, y1, and z1?

int x=10; int y =10; int z=10; int x1, y1, z1;
x1=++y;  //x1 = 11
y1=z++;  //y1 = 10
z1=z;    // z1 = 11

Choose the one below:

  1. x1 = 10 , y1 = 10 and z1=10
  2. x1 = 10, y1 = 11, and z1=10
  3. x1=10, y1 = 10, and z1=11
  4. x1=11, y1=10, and z1=11

選4

3

What will be the output?

public class Test {
    public int addTest(int x, int y) {
        x = x + 1;
        y = y + 1;
        int z = (x + y);
        return z;
    }

    public static void main(String[] args) {
        int x = 10;
        int y = 10;
        int z = 0;
        Test t = new Test();
        z = t.addTest(x, y);
        System.out.println("x =" + x + ",y =" + y + ",z = " + z);

    }
}

Choose the one below:

  1. x=10, y=10, z=22
  2. x=11, y=11, z=22
  3. x=10, y=10, z=20
  4. x=11, y=11, z=20

選1, 因為函數(shù)傳的是?, 不是地址.

4

What will be the output of the program.?
Assume that MyList class is declared in MyList.java
and ListManager class is declared in ListManager.java file.

public class Test {
    int size = 1;
    public static void main(String[] args)
    {
        Test list = new Test();
        list.size = 10;
        ListManager lm = new ListManager();
        lm.expandList(list);
        System.out.println("list.size = " + list.size);
    }
}  //end of MyList

public class ListManager {
    public void expandList(Test l)
    {
        l.size = l.size + 10;
    }
}

Choose the one below:

  1. size=0
  2. size=10
  3. size=11
  4. size=20

選4, 因為傳的是類, 屬于址傳遞.

5

If int x = -1 then which of the following expression results in a positive value in x?

  1. x=x>>>32 // --> -1
  2. x=x>>>5 // --> 134217727
  3. x=x>>5 // ---> -1
  4. x=~x // ---> 0

選2, 因為>>>是無符號右移,空位補0, -1的二進制是32個1, >>是有符號右移空位補符號位-1的符號位是1,4不正確, 題目是問正數(shù).

6

6 . Which of the following lines of code would print “Equal” when you run it?

  1. int x=1; float y=1.0F; if(x==y){ System.out.println("Equal");} //Equal
  2. int x=1; Integer y= new Integer(1); if(x==y) {System.out.println("Equal");} // Equal
  3. Integer x=new Integer(1); Integer y=new Intger(1); if(x==y){ System.out.println("Equal");} //不輸出
  4. String x="1"; String y="1"; if (x==y) { System.out.println("Equal");} //Equal

1 2 4都會輸出 Equal,

7

Which of the following declarations are correct for the top level class?

  1. public synchronized class MyTest extends Thread
  2. private class MyTest extends Thread
  3. public abstract class MyTest extends Thread
  4. class MyTest extends Thread

選擇3, 4

8

Consider the following lines of code:

public class Test {
    String x;

    public void testDemo(int n) {
        String y;
        if (n > 0) {
            y = "Hello";
        }
        System.out.println(x + y);
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.testDemo(2);
    }
}  //end of MyList
  1. It will produce compiler warning that variable y may not have been initialized
  2. It will produce compiler warning that variable x may not have been initialized
  3. It will output “Hello”
  4. It will output “nullHello”

選2, 因為沒有初始化y,編譯失敗

9

Consider that Parent and Child classes are defined in two different files as below:

class Parent {
    public Parent() {
        System.out.println("I am Parent");
    }
}

class Child extends Parent {
    public Child(int x) {
        System.out.println("I am Child");
    }

    public static void main(String[] args) {
        Child c = new Child(10);
    }
}
  1. It will not compile.
  2. It will compile successfully. It will output “I am Parent” and then “I am Child.”
  3. It will compile successfully. It will output “I am Child” and then “I am Parent.”
  4. It will compile successfully, but will not run.

這個題目有問題, Child和Parent首先得在一個包,第二Child類前得有public才能運行.
所以,要么編譯成功,輸出2, 要么編譯成功但不會輸出選4,

10

Consider following code:

import java.util.Vector;
public class Test {
    static int size;
    public expandList(int newSize) {
        ListExpander lexp = new ListExpander();
        Vector expandedList = lexp.expand();
        class ListExpander {
            public Vector expand() {
                Vector v = new Vector(this.size + newSize);
                return v;
            }
        }
    }
}
  1. compiler error, “cannot refer inside an inner class to a static variable.”
  2. compiler error, “cannot refer inside an inner class to to a non-final variable newSize defined in a different method.”
  3. Both of the above
  4. None of the above

如果expandList函數(shù)前有返回類型, 那么就選2 newSize需要聲明為final類型, 如果沒有,就選4

11

Consider following code:

public class Parent{
    public  int size =0;
    static class InnerClass{
        public void incrementParentSize(){
            XXX=XXX+10;
        }
    }
}

In above code, how can you access ‘size’ variable (of outer class Parent) inside innerclass at the place of ‘XXX’ ?

  1. super.size
  2. this.size
  3. Parent.size
  4. Can not access it`

選4, 靜態(tài)內(nèi)部類無法訪問非靜態(tài)成員, super是父類, this是當前類

12

Assume that Parent and Child classes are in different files:

public class Parent {
    public Parent(int x, int y)
    {
        System.out.println("Created Parent");
    }

}//end of Parent class

public class Child extends Parent {
    public Child(int x, int y) {
        super(x, y);
    }

    public Child(int x, int y, int z) {
        System.out.println("Creating child");
        this(x, y);
    }
    public static void main(String[] args) {
        Child c = new Child(1, 2, 3);
    }
}

What will happen if you try to compile and run above program?

  1. It will compile successfully. It will output “Created Parent” and then “Creating child”
  2. It will compile successfully. It will output “Creating child” and then “Created Parent”
  3. It will not compile giving warning, “Explicit constructor invocation must be first statement in constructor.”
  4. It will not compile giving warning, “Expression is not a valid block statement.”

一定不會編譯的,選3, 4

13

Consider following code:

public class OuterClass{
    class InnerClass{
    }
    public void innerClassDemo(){
        //Explicit instance of InnerClass
    }
}

In above code, how can you explicitly create an instance of InnerClass?

  1. InnerClass i=InnerClass();
  2. InnerClass i=OuterClass.InnerClass();
  3. InnerClass i=new OuterClass ().new InnerClass();
  4. InnerClass i=new OuterClass.InnerClass();

前兩個都沒有new關(guān)鍵字, 選3, 4

14

Please select valid(有效) array declaration(s):

  1. int x[20];
  2. int []x=new int[20];
  3. int [][] x=new int [20][];
  4. int [][][] x=new int[20][20][20];
  5. int [] x={1,2,3};

2, 3, 4, 5都正確, 1錯誤

15

Consider following code:

public class Test {
    protected void demo() throws NumberFormatException, ArrayIndexOutOfBoundsException {
        //something here
    }
    public void demo(String s) {
        //something here
    }
}//end of Test class

Please select true statement(s) for demo code?

  1. It is an example of overloading method
  2. It is an example of overriding method
  3. Both of the above
  4. None of the above

選1, 方法重載, overloading

16

For the following code, please consider that super class is defined in question #15:

class MyTest extends Test {
    private void demo() throws IndexOutOfBoundsException, ClassNotFoundException
    {
        //something here
    }
}//end of MyTest class

What will happen if you try to compile above code ?

  1. It will compile successfully.
  2. Compiler error: Exception java.lang.ClassNotFoundException in throws clause of void MyTest.demo() is not compatible with void Test.demo().
  3. Compiler error: Cannot reduce visibility of the inherited method from Test.
  4. Both B and C

選4, MyTest中的demo()無法覆蓋Test中的demo()
正在嘗試分配更低的訪問權(quán)限; 以前為protected

17

Consider the following code:

public class Test {
    public void demo(String[] list) {
        try {
            String s = list[list.length + 1];
            System.out.println(s);
        } catch (ArrayIndexOutOfBoundsException e) {
            return;
        } finally {
            System.out.println("Finally here.");
        }
    }
    public static void main(String[] args) {
        Test t = new Test();
        String[] list = {"one","two"};
        t.demo(list);
        System.out.println("Done !");
    }
}//end of Test class

What happen if you try compile and run above code ?

  1. It will not compile.
  2. It will output “null” and then “Finally here.”
  3. It will output “Done!”
  4. It will output “Finally here” and then “Done!”

選4, 遇到catch里面return的時候,不執(zhí)行,finally一定會執(zhí)行!

18

Please consider following code:

public class Test{
    public static void demo(String s)
    {
      debug("In demo:"+s);
    }
    private void debug(String s){
        System.out.println(s);
    }
    public static void main(String [] args){
        Test.demo(“Hello”);
    }
}

What will happen if you try to compile and run above code ?

  1. It will compile successfully, but will not run.
  2. It will compile successfully, and outputs “In demo:Hello.”
  3. It will not compile with error message “Can not make a static reference to the instance method named.”
  4. None of the above

選3, 靜態(tài)方法不能調(diào)用非靜態(tài)方法

19

Consider the following code:

/**
 * File Drawable.java
 */

public interface Drawable {

    public void draw();

    public void fill();

} /**
 * End of file Drawable.java
 */

/** File Circle.java */
public class Circle implements Drawable {
    int center=0;
    public void draw(){
        System.out.println(“Drawing circle”);
    }
    public static void main(String [] args){
        Circle c=new Circle();
        c.draw()
    }
} /** End of file Circle.java */

If you attempt to compile and run Circle class what will be output?

  1. It will compile successfully, and outputs “Drawing circle.”
  2. It will not compile, and reports error: “class Circle must implement inherited abstract method void Drawable.fill.”
  3. It will not compile, and reports error: “Method Drawable.fill requires a body instead of a semicolon.”
  4. None of the above

選2, 因為只實現(xiàn)了接口的一個方法.

20

What will be the output?

int x=2; int y=3; int z=4;
if(x>2){
    System.out.println(“Tested x”);
}if(y<3){
   System.out.println(“Tested y”);
}if (z<=3){
   System.out.println(“Tested z”);
}

Choose the one below:

  1. Tested x.
  2. Tested y.
  3. Tested z.
  4. None of the above.

選4

21

Consider the following code:

for(int i=0;i<2;i++)
{
    for(int j=i;j<3; j++)
    {
        if (i==j)
        {
            continue;
        }
        System.out.println("i="+i+" j="+j);
    }
}

Which lines would be part of the output?

  1. i = 0 j = 1
  2. i = 0 j = 2
  3. i = 1 j = 2
  4. None of the above

選1 2 3,

22

Consider the following code:

int j=0;
for( int i=0;i<2;i++)
{
    for (j=i; j<3; j++)
    {
        continue;
    }
    System.out.println(“i = “+i+” j = “+j);
}

Which lines would be part of the output?

  1. i = 0 j = 0
  2. i = 1 j = 1
  3. i = 0 j = 3
  4. i = 1 j =3

選3 4

23

Consider the following code:

int i=0; int j=0;
for( i=0;i<2;i++)
{
    for (j=i; j<3; j++)
    {
        break;
    }
    System.out.println("i = "+i+" j = "+j);
}
```
Which **lines** would be part of the output?
1. i = 0 j = 0
2. i = 1 j = 1
3. i = 0 j = 3
4. i = 1 j = 3
> 選1  2

#### 24
Consider the following code:
```
int i, j=0;
outer:
for( i=0;i<2;i++)
{
    for (j=i; j<3; j++)
    {
        continue outer;
    }
  System.out.println("i = "+i+" j = "+j);
}
```
Which lines would be part of the output?
1. i = 0 j = 0
2. i = 1 j = 1
3. i = 0 j = 3
4. None of the above
> 選4, 什么都輸出, 這里是 java label標號的用法

#### 25
Consider the following code:
```
int i, j=0;
for( i=0;i<2;i++)
{
    inner:
    for(j=i; j<3; j++)
    {
        break inner;
    }
    System.out.println("i = "+i+" j = "+j);
}
```
Which **lines** would be part of the output?
1. i = 0 j = 0
2. i = 1 j = 1
3. i = 0 j = 3
4. None of the above
> 選 1 2,
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末臀稚,一起剝皮案震驚了整個濱河市翎卓,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖供炎,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蝌箍,居然都是意外死亡魁巩,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門帽借,熙熙樓的掌柜王于貴愁眉苦臉地迎上來珠增,“玉大人,你說我怎么就攤上這事砍艾〉俳蹋” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵脆荷,是天一觀的道長凝垛。 經(jīng)常有香客問我懊悯,道長,這世上最難降的妖魔是什么梦皮? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任炭分,我火速辦了婚禮,結(jié)果婚禮上剑肯,老公的妹妹穿的比我還像新娘捧毛。我一直安慰自己,他們只是感情好让网,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布呀忧。 她就那樣靜靜地躺著,像睡著了一般溃睹。 火紅的嫁衣襯著肌膚如雪而账。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天丸凭,我揣著相機與錄音福扬,去河邊找鬼。 笑死惜犀,一個胖子當著我的面吹牛铛碑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播虽界,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼汽烦,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了莉御?” 一聲冷哼從身側(cè)響起撇吞,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎礁叔,沒想到半個月后牍颈,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡琅关,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年煮岁,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片涣易。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡画机,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出新症,到底是詐尸還是另有隱情步氏,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布徒爹,位于F島的核電站荚醒,受9級特大地震影響芋类,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜界阁,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一梗肝、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧铺董,春花似錦、人聲如沸禀晓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽粹懒。三九已至重付,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間凫乖,已是汗流浹背确垫。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留帽芽,地道東北人删掀。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像导街,于是被迫代替她去往敵國和親披泪。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容