JAVA 测试II

JAVA EXAM 26--49

JAVA 测试I http://www.jianshu.com/p/76b36d18844d
JAVA 测试II http://www.jianshu.com/p/e7f7d29b185a
JAVA 测试III http://www.jianshu.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()不能直接构造, 必须含有参数,无参构造是私有的.故这个题目有错误. 4
如果没有错误, 则选 3, 因为线程默认的优先级是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没有参数为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

问创建了几个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并运行, 即使是个死循环

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是构造参数传进来的Runnable的派生类. 现在YourThread重写了run方法.所以不会执行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, 编译成功. 内部类可以访问外部类的任意变量, 且内部类所在的函数并不是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, 静态方法不能访问外部类的静态变量

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, 因为方法名, 方法的参数相同, 所以是覆盖, 不是重载. 又因为返回类型不同, 所以无法覆.

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的参数不同,属于重载, 不是覆盖. 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, 编译失败, 因为父类没有默认的构造方法, 而子类却没有调用父类的带参构造方法.

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, 提示变量已经定义过.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容