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:
- 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()不能直接構造, 必須含有參數(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?
- “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沒有參數(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?
- 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
問創(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:
- 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并運行, 即使是個死循環(huán)
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是構造參數(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?
- 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, 編譯成功. 內(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?
- publicVariable
- privateVariable
- SIZE
- 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:
- 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, 因為方法名, 方法的參數(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?
- 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的參數(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:
- 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, 編譯失敗, 因為父類沒有默認的構造方法, 而子類卻沒有調(diào)用父類的帶參構造方法.
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, 提示變量已經(jīng)定義過.