JAVA 測試II

JAVA EXAM 26--49

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

26

What will be the output?

Thread currentThread=Thread.currentThread();
int priority = currentThread.getPriority();
Thread t1=new Thread();
t1.setPriority(9);
ThreadGroup tgrp=new ThreadGroup();
tgrp.setMaxPriority(10);
Thread t2=new Thread(tgrp, "t2");
System.out.println("Priority of t1="+t1.getPriority());
System.out.println("Priority of t2="+t2.getPriority());

Choose the one below:

  1. Priority of t1=5 and Priority of t2=10
  2. Priority of t1=9 and Priority of t2=10
  3. Priority of t1=9 and Priority of t2=5
  4. Neither of above

ThreadGroup()不能直接構造, 必須含有參數(shù),無參構造是私有的.故這個題目有錯誤. 4
如果沒有錯誤, 則選 3, 因為線程默認的優(yōu)先級是5

27

Consider the following code:

/** File Thread1.java */
class Thread1 implements Runnable{
    public void run(){
        System.out.println("Running Thread1");
    }
}  /** End of file Thread1.java */

/** Thread2.java */
class Thread2 extends Thread{
    public void run(){
    System.out.println(“Running Thread2”);
    }

    public static void main(String [] args){
        Thread1 t1= new Thread1();
        Thread t2=new Thread2(t1);
        t1.start();
        t2.start();
    }
}
/** End of Thread2.java*/

If you try to compile and run above code what will be result?

  1. “Running thread1” following “Running thread2”
  2. “Running thread2” following “Running thread1”
  3. It will not compile because in Thread1 and Thread2 start() is not defined .
  4. It will not compile because constructor invoked to create Thread2 with arguments (Thread1) is not defined

選4, 因為Thread2沒有參數(shù)為Runnable的構造器

28

Consider the following code:

class MyThread extends Thread{
    public void run(){
        System.out.println("Done");
    }
    public void demo(){
        System.out.println("Demo");
    }
    
    public static void main(String args[]){
        MyThread th=new MyThread();
        th.run();
        th.stop();
        th.demo();
    }
}

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

  1. It will throw an exception at th.run() because run() was called before calling start().
  2. It will throw an exception at th.demo() because Thread variable th was already stopped calling stop().
  3. It will output “Done” following “Demo”
  4. Neither of the above.

3

29

Please consider following code:

String s1=" 5 + 5 = 10 ";
s1.trim();
s1.replace('+', '-');

How many String objects will be created after executing above code?

  1. 1
  2. 2
  3. 3
  4. 4

問創(chuàng)建了幾個String類, 應該是3個

30

What will be the output?

String s="Hi";
StringBuffer sb=new StringBuffer(s);
String s1=new String("There");
StringBuffer sb1=new StringBuffer(s1);
if(s==sb){
    System.out.println(“s==sb”);
}
if(s.equals(sb)){  // ---> false
    System.out.println(“s.equals(sb)”);
}
if(s1.equals(sb1)){  // ----> false
    System.out.println(“s1.equals(sb1)”);
}

Choose the one below:

  1. It will not compile at if(s==sb) because operands on both side are not compatible
  2. It will print s1.equals(sb1)
  3. It will print s.equals(sb)
  4. It will compile successfully, but it will not output anything

選1, 兩者類型不匹配, 無法比較.

31

Consider that following code is declared in BussyThread.java file

public class BussyThread extends Thread {
    public void run() {
        for (int i = 0; i < 10; i++) {
            i = i - 1;
        }//end of for loop
    }//end of run()

    public static void main(String args[]) {
        BussyThread b1 = new BussyThread();
        BussyThread b2 = new BussyThread();
        b1.start();
        b2.start();
    }
}//end of class

Above code will start two threads b1 and b2. Select True statements for above code?

  1. Only b1 thread will get chance to run
  2. Only b2 thread will get chance to run
  3. Both thread will get chance to run sharing CPU time
  4. Neither of the thread will be able to run.

選3, 都有機會獲得CPU并運行, 即使是個死循環(huán)

32

What changes in run() method of BussyThread will enable both threads to run?

  1. adding yield() into run method
  2. adding try{sleep(1000);}catch (InterruptedException e){} into run method
  3. adding wait(1000) into run method
  4. Neither of the above

1 2

33

Consider the following classes are in MyThread.java, YourThread.java, and Driver.java files:

public class MyThread implements Runnable{
    public void run(){
        System.out.println(“Running MyThread”);
    }
}//end of MyThread
public class YourThread extends Thread{
    public YourThread(Runnable r){
        super(r);
    }
    public void run(){
        System.out.println(“Running YourThread”);
    }
}//end of YourThread
public class Driver{
    public static void main(String args []){
        MyThread t1= new MyThread();
        YourThread t2 = new YourThread(t1);
        t2.start();
    }
}//end of class

If you try to run Driver class what will be result?

  1. It will output “Running MyThread.”
  2. It will output “Running YourThread.”
  3. It will output both “Running MyThread,” and “Running YourThread.”
  4. It will not run.

選2, 詳解:因為Thread的run方法的源代碼是

    public void run() {
        if (target != null) {
            target.run();
        }
    }

其中target是構造參數(shù)傳進來的Runnable的派生類. 現(xiàn)在YourThread重寫了run方法.所以不會執(zhí)行runnable的run方法了.

34

Consider following code:

String s=null;
String t="null";
if (s==t)
{
    System.out.println("s equal to t");
} else {
    System.out.println("s not equal to t");
}

what will result if you try to compile and run above code?

  1. it compiles successfully, but throws NullpointerException at if (s==t)
  2. It will not compile.
  3. It compiles successfully and output “s equal to t”
  4. It compiles successfully and output “s not equal to t”

選4, 可以正常編譯的. null也是可以比較的,不然if(xx != null)怎么正常運行(__)

35

Consider the following code:

public void demo(){
    String s[];
    if (s.equals(null))
    {
        System.out.println("s is null");
    } else {
        System.out.println("s is not equal");
    }
}

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

  1. Compile error produced, “variable s may not have been initialized.”
  2. It compile successfully, but throws NullpointerException at if ( s.equals(null) )
  3. It compile successfully, and outputs “s is null.”
  4. It compile successfully, and outputs “s is not null.”

選1, 編譯失敗, 提示s可能沒有初始化

36

Consider the following code:

public class MyList {
    private static final int MAX_SIZE = 10;
    private Object[] list = new Object[MAX_SIZE];
    public void add(Object obj) {
        int size = list.length;
        if (size >= MAX_SIZE) {
            class ListExpander {
                public void expand() {
                    Object temp[] = list;
                    list = new Object[size + MAX_SIZE];
                    for (int i = 0; i < temp.length; i++) {
                        list[i] = temp[i];
                    }
                }//end of  public void expand()
            } //end of class ListExpander
            ListExpander listExp = new ListExpander();
            listExp.expand();
            list[size] = obj;
        }//end of if
    }//end of add
}//end of class MyList

What will be result if you try to compile and run the above code?

  1. Compiler error reported, “Cannot refer inside an inner class to a non-final variable ‘size’ defined in a different method.”
  2. Compiler error reported, “Cannot refer inside an inner class to a private member variable ‘list’ defined in enclosing class MyList.”
  3. Compiler error reported, “Cannot refer inside an inner class to a static member variable MAX_SIZE defined in enclosing class MyList.”
  4. It compiles and runs successfully.

1, 編譯成功. 內(nèi)部類可以訪問外部類的任意變量, 且內(nèi)部類所在的函數(shù)并不是static

37

Consider following example of an inner class:

public class MyTest{
    public String publicVariable = "a";
    private String privateVariable = "b";
    public static int SIZE = 0;
    private static int MAX_SIZE = 0;
    public static void DemoHelper() {
        class demo {
            public demo() {
                System.out.println("Demo = " + XXX);
            }
        }
    }//end of inner class
}

which variable of the MyTest class will be able to use in place of XXX?

  1. publicVariable
  2. privateVariable
  3. SIZE
  4. MAX_SIZE

選3, 4, 靜態(tài)方法不能訪問外部類的靜態(tài)變量

38

What will be result if you try to compile and run following code?

public class Record extends String{}

Choose the one below:

  1. Compiler error reported, “Can not extend a final class.”
  2. Compiler error reported, “Must implement method int compareTo(Object).”
  3. Compile and run successfully.
  4. None of the above.

選1, String類是final類型,不允許繼承

39

Consider the following two classes:

public class Parent{
    protected void demo() throws Exception{}
} // end of Parent class

public class Child extends Parent{
    private void demo() {}
}

What will be result if you try to compile above two classes?

  1. Compiler object for the method of a Child class, “Can not reduce the visibility of the inherited method.”
  2. Compiler object for demo() method of a Child class, “Inherited method is not compatible with void Parent.demo() throws Exception.”
  3. Compile successfully.
  4. None of the above

選1, 編譯失敗, 提示正在嘗試分配更低的訪問權限, 可以是protect或者public, 都會正常編譯

40

Consider the following two classes:

public class Parent{
    protected void demo() {}
} // end of Parent class

public class Child extends Parent{
    public void demo() throws Exception{}
}

What will be result if you try to compile above two classes?

  1. Compiler object for the method of a Child class, “Can not widen the visibility of the inherited method.”
  2. Compiler object for demo() method of a Child class, “Exception java.lang.Exception in throws clause of void Child.demo() is not compatible with void Parent.demo().”
  3. Compile successfully
  4. None of the above

選2, 子類重寫父類的方法時候不能聲明拋出比父類大的異常

41

Consider the following two classes:

public class Parent{
    protected void demo() {}
} // end of Parent class

public class Child extends Parent{
    public int demo()
    {return 0;}
}

What will be result if you try to compile above two classes?

  1. Compiler object for the method of a Child class, “Can not widen the visibility of the inherited method.”
  2. Compiler object for the method of a Child class, “Return type is not compatible with void Parent.demo().”
  3. Compile successfully.
  4. None of the above

選2, 因為方法名, 方法的參數(shù)相同, 所以是覆蓋, 不是重載. 又因為返回類型不同, 所以無法覆.

42

Consider the following two classes:

public class Parent{
    protected static void demo() {}
} // end of Parent class

public class Child extends Parent{
    public void demo() {}
}

What will be result if you try to compile above two classes?

  1. Compiler object for the method of a Child class, “Can not widen the visibility of the inherited method.”
  2. Compiler object for the method of a Child class, “inherited method void Child.demo() is not compatible with void Parent.demo().”
  3. Compiler object for the method of a Child class, “The instance method can not override the static method from Parent.”
  4. Compile successfully.

選3, 此處和上題一樣, 無法覆蓋.

43

Consider that class Employee and Salesman are in different file called Employee.java and Salesman.java:

/** Employee.java file*/
public class Employee{
    int salary=1000;
    public int getSalary(){
        return salary;
    }
}
/**  End of Employee.java file*/

/** Salesman.java file*/
public class Salesman extends Employee{
    int commission =100;
    public int getSalary(){
        return salary+commission;
    }

    public static void main(String [] args){
        Salesman sm = new Salesman();
        Employee em = sm;
        System.out.println(em.getSalary());
    }
}
/**  End of Salesman.java file*/

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

  1. Compiler error reported , “Type mismatch: Cannot convert from Salesman to Employee.”
  2. It compile successfully and outputs 1000.
  3. It compiles successfully and outputs 1100.
  4. None of the above

選3, $1100$

44

Considering following code what will be the result if you try to compile the following code:

public abstract class Test{
    public void demo(){
        System.out.println(“demo”);
    }
}

Choose the one below:

  1. It will compile successfully.
  2. Compiler error reported, “An abstract method must be defined.”
  3. Compiler error reported, “Invalid declaration of class.”
  4. None of the above

選1, 可以正常編譯, 抽象類可以沒有抽象的方法.

45

Considering following code what will be the result if you try to compile the following code:

public class Test{
    public abstract void demo();
}

Choose the one below:

  1. Compiler error reported, “Method requires a body instead of semicolon.”
  2. Compiler error reported, “Abstract methods are only defined by abstract classes.”
  3. Compile successfully.
  4. None of the above.

選2, 抽象方法必須定義在抽象類中

46

The GenericList has the following method:

public void addItem(Object item)

You are writing a class GroceryList that extends GenericList. Which of the following would be legal declarations of overloading methods?
Choose the one below:

  1. public void addItem(Vector item)
  2. public void addItem(Object [] items) throws Exception
  3. protected void addItem(Object item)
  4. All of the above

1, 2, 因為1, 2的參數(shù)不同,屬于重載, 不是覆蓋. 3是屬于覆蓋, 覆蓋不能降訪問權限.

47

What will be result if you try to compile the following code?

public class Parent{
    String name = null;
    public Parent(String n){
        name = n;
    }
}

public class Child extends Parent{
    String type = "X";
}

Choose the one below:

  1. Compile successfully.
  2. Compiler error reported, because Parent class did not declare constructor with arguments ().
  3. Compiler error reported, because Child class did not declare a constructor.
  4. Both of the above B and C

選2, 編譯失敗, 因為父類沒有默認的構造方法, 而子類卻沒有調(diào)用父類的帶參構造方法.

48

What will be legal statement in the following method?

public void demo(int x){
    XXX y = 10;
}

Choose the one below:

  1. public int
  2. int
  3. final int
  4. static int

2, 3, 局部變量不能使用類變量的修飾符

49

What will be result if you try to compile and run following code fragement?

public void demo (String[] args){
    int i = 1;
    for(int i = 0; i < args.length; i++)
    {
        System.out.println(args[i]);
    }
}

Choose the one below:

  1. Compile successfully, but throws IndexOutOfBoundException during runtime.
  2. Compile error reported, “Local name i is already defined.”
  3. Throws NullPointerException during runtime
  4. None of the above

選 2, 提示變量已經(jīng)定義過.

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末辽幌,一起剝皮案震驚了整個濱河市窝撵,隨后出現(xiàn)的幾起案子郎仆,更是在濱河造成了極大的恐慌瘸彤,老刑警劉巖壹甥,帶你破解...
    沈念sama閱讀 212,718評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件空幻,死亡現(xiàn)場離奇詭異熙兔,居然都是意外死亡弛说,警方通過查閱死者的電腦和手機挽懦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來剃浇,“玉大人巾兆,你說我怎么就攤上這事』⑶簦” “怎么了角塑?”我有些...
    開封第一講書人閱讀 158,207評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長淘讥。 經(jīng)常有香客問我圃伶,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,755評論 1 284
  • 正文 為了忘掉前任窒朋,我火速辦了婚禮搀罢,結果婚禮上,老公的妹妹穿的比我還像新娘侥猩。我一直安慰自己榔至,他們只是感情好,可當我...
    茶點故事閱讀 65,862評論 6 386
  • 文/花漫 我一把揭開白布欺劳。 她就那樣靜靜地躺著唧取,像睡著了一般。 火紅的嫁衣襯著肌膚如雪划提。 梳的紋絲不亂的頭發(fā)上枫弟,一...
    開封第一講書人閱讀 50,050評論 1 291
  • 那天,我揣著相機與錄音鹏往,去河邊找鬼淡诗。 笑死,一個胖子當著我的面吹牛伊履,可吹牛的內(nèi)容都是我干的韩容。 我是一名探鬼主播,決...
    沈念sama閱讀 39,136評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼湾碎,長吁一口氣:“原來是場噩夢啊……” “哼宙攻!你這毒婦竟也來了?” 一聲冷哼從身側響起介褥,我...
    開封第一講書人閱讀 37,882評論 0 268
  • 序言:老撾萬榮一對情侶失蹤座掘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后柔滔,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體溢陪,經(jīng)...
    沈念sama閱讀 44,330評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,651評論 2 327
  • 正文 我和宋清朗相戀三年睛廊,在試婚紗的時候發(fā)現(xiàn)自己被綠了形真。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,789評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡超全,死狀恐怖咆霜,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情嘶朱,我是刑警寧澤蛾坯,帶...
    沈念sama閱讀 34,477評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站疏遏,受9級特大地震影響脉课,放射性物質(zhì)發(fā)生泄漏救军。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,135評論 3 317
  • 文/蒙蒙 一倘零、第九天 我趴在偏房一處隱蔽的房頂上張望唱遭。 院中可真熱鬧,春花似錦呈驶、人聲如沸拷泽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,864評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽跌穗。三九已至,卻和暖如春虏辫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背锈拨。 一陣腳步聲響...
    開封第一講書人閱讀 32,099評論 1 267
  • 我被黑心中介騙來泰國打工砌庄, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人奕枢。 一個月前我還...
    沈念sama閱讀 46,598評論 2 362
  • 正文 我出身青樓娄昆,卻偏偏與公主長得像,于是被迫代替她去往敵國和親缝彬。 傳聞我的和親對象是個殘疾皇子萌焰,可洞房花燭夜當晚...
    茶點故事閱讀 43,697評論 2 351

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