美文网首页
JAVA 测试II

JAVA 测试II

作者: tenlee | 来源:发表于2016-07-28 14:14 被阅读42次

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

    相关文章

      网友评论

          本文标题:JAVA 测试II

          本文链接:https://www.haomeiwen.com/subject/kqyajttx.html