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:
- Priority of t1=5 and Priority of t2=10
- Priority of t1=9 and Priority of t2=10
- Priority of t1=9 and Priority of t2=5
- 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?
- “Running thread1” following “Running thread2”
- “Running thread2” following “Running thread1”
- It will not compile because in Thread1 and Thread2 start() is not defined .
- 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?
- It will throw an exception at th.run() because run() was called before calling start().
- It will throw an exception at th.demo() because Thread variable th was already stopped calling stop().
- It will output “Done” following “Demo”
- 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
- 2
- 3
- 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:
- It will not compile at if(s==sb) because operands on both side are not compatible
- It will print s1.equals(sb1)
- It will print s.equals(sb)
- 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?
- Only b1 thread will get chance to run
- Only b2 thread will get chance to run
- Both thread will get chance to run sharing CPU time
- 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?
- adding yield() into run method
- adding try{sleep(1000);}catch (InterruptedException e){} into run method
- adding wait(1000) into run method
- 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?
- It will output “Running MyThread.”
- It will output “Running YourThread.”
- It will output both “Running MyThread,” and “Running YourThread.”
- 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?
- it compiles successfully, but throws NullpointerException at if (s==t)
- It will not compile.
- It compiles successfully and output “s equal to t”
- 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?
- Compile error produced, “variable s may not have been initialized.”
- It compile successfully, but throws NullpointerException at if ( s.equals(null) )
- It compile successfully, and outputs “s is null.”
- 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?
- Compiler error reported, “Cannot refer inside an inner class to a non-final variable ‘size’ defined in a different method.”
- Compiler error reported, “Cannot refer inside an inner class to a private member variable ‘list’ defined in enclosing class MyList.”
- Compiler error reported, “Cannot refer inside an inner class to a static member variable MAX_SIZE defined in enclosing class MyList.”
- 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?
- publicVariable
- privateVariable
- SIZE
- 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:
- Compiler error reported, “Can not extend a final class.”
- Compiler error reported, “Must implement method int compareTo(Object).”
- Compile and run successfully.
- 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?
- Compiler object for the method of a Child class, “Can not reduce the visibility of the inherited method.”
- Compiler object for demo() method of a Child class, “Inherited method is not compatible with void Parent.demo() throws Exception.”
- Compile successfully.
- 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?
- Compiler object for the method of a Child class, “Can not widen the visibility of the inherited method.”
- 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().”
- Compile successfully
- 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?
- Compiler object for the method of a Child class, “Can not widen the visibility of the inherited method.”
- Compiler object for the method of a Child class, “Return type is not compatible with void Parent.demo().”
- Compile successfully.
- 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?
- Compiler object for the method of a Child class, “Can not widen the visibility of the inherited method.”
- Compiler object for the method of a Child class, “inherited method void Child.demo() is not compatible with void Parent.demo().”
- Compiler object for the method of a Child class, “The instance method can not override the static method from Parent.”
- 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?
- Compiler error reported , “Type mismatch: Cannot convert from Salesman to Employee.”
- It compile successfully and outputs 1000.
- It compiles successfully and outputs 1100.
- 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:
- It will compile successfully.
- Compiler error reported, “An abstract method must be defined.”
- Compiler error reported, “Invalid declaration of class.”
- 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:
- Compiler error reported, “Method requires a body instead of semicolon.”
- Compiler error reported, “Abstract methods are only defined by abstract classes.”
- Compile successfully.
- 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:
- public void addItem(Vector item)
- public void addItem(Object [] items) throws Exception
- protected void addItem(Object item)
- 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:
- Compile successfully.
- Compiler error reported, because Parent class did not declare constructor with arguments ().
- Compiler error reported, because Child class did not declare a constructor.
- 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:
- public int
- int
- final int
- 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:
- Compile successfully, but throws IndexOutOfBoundException during runtime.
- Compile error reported, “Local name i is already defined.”
- Throws NullPointerException during runtime
- None of the above
选 2, 提示变量已经定义过.