SCJP认证试题(二)
/** * * @author yaoyuan */public class Main implements Runnable{public void run(){System.out.print("running");}public static void main(String[] args){Thread t = new Thread(new Main());t.run();t.run();t.start();}}/**
* What is result ?
*
*
*A compilcation fails
*BAn Exception is thrown at runtime
*CThe code executes and prints "running"
*DThe code executes and prints "runningrunning"
*EThe code executes and prints "runningrunningrunning"
*/
// Answer : E
/**API详解
*
public void start()
使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
结果是两个线程并发地运行;当前线程(从调用返回给 start 方法)和另一个线程(执行其 run 方法)。
多次启动一个线程是非法的。特别是当线程已经结束执行后,不能再重新启动。
public void run()
如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。
Thread 的子类应该重写该方法。
*/
/** * * @author yaoyuan */public class Threads1{int x = 0;public class Runner implements Runnable{public void run(){int current = 0;for(int i=0;i<4;i++){current = x;System.out.print(current + ", ");x = current + 2;}}}public static void main(String[] args){new Thread1().go();}public void go(){Runnable r1 = new Runner();new Thread(r1).start();new Thread(r1).start();}}
/**
*Which two are possible results?
*
*
*A 0,2,4,4,6,8,10,6,
*B0,2,4,6,8,10,2,4,
*C0,2,4,6,8,10,12,14
*D0,0,2,2,4,4,6,6,8,8,10,10,12,12,14,14
*E0,2,4,6,8,10,12,14,0,2,4,6,8,10,12,14
*/
// Answer : A C
/**解题帮助
*
*int x = 0; x是全局变量,Thread1和Thread2公有一个x
*int current = 0;current是局部变量,每次都被初始化为0
*
*
*/
/** * * @author yaoyuan */void waitForSignal(){Object obj = new Object();synchronized (Thread.currentThread()){obj.wait();obj.notify();}}
/**
* Which statement is true?
*
*
*A This code may thorw an InterruptedException
*BThis code may thorw an IllegalStateException
*C This code may throw a TimeOutException after ten minutes
*DThis code will not compile unless "obj.wait()" is replaced with "((Thread)obj).wait()"
*EReversing the order of obj.wait() and obj.notify() may cause this method to complete normally
*/
// Answer : B
/** * * @author yaoyuan */public class TestOne implements Runnable{public static void main(String[] args) throws Exception{Thread t = new Thread(new TestOne());t.start();System.out.print("Started");t.join();System.out.print("Complete");}public void run(){for(int i=0;i<4;i++){System.out.println(i);}}}
/**
*What can be a result ?
*
*
*A Compilation fails
*BAn exception thrown at runtime
*CThe code executes and prints "StartedComplete"
*DThe code executes and prints "StartedComplete"
*EThe code executes and prints "Started0123Complete"
*/
// Answer : E
/**
*
* @author yaoyuan
*/
/**
*Which two code fragments will execute the method doStuff() in a separate thread?
*
*
*
*A new Thread(){
*public void run(){
*doStuff();
*}
*};
*
*Bnew Thread(){
*public void start(){
*doStuff();
*}
*};
*
*Cnew Thread(){
*public void start(){
*doStuff();
*}
*};
*run();
*
*Dnew Thread(){
*public void run(){
*doStuff();
*}
*}
*start();
*
*Enew Thread(new Runnable(){
*public void run(){
*doStuff();
*}
*});
*run();
*
*Fnew Thread(new Runnable(){
*public void run(){
*doStuff();
*}
*});
*start();
*/
// Answer : DF
/**
*
* @author yaoyuan
*/
/**
*Which three will compile and run without exception?(choose three)
*
*
*A private synchronized object;
*
*Bvoid go(){
*synchronized(){
*/ code here/
*}
*}
*
*Cpublic synchronized void go(){
*/ code here /
*}
*
*Dprivate synchronized(this) void go(){
*/ code here /
*}
*
*Evoid go(){
*synchronized(object.class){
*/ code here /
*}
*}
*
*Fvoid go{
*synchronized(o){
*/ code here /
*}
*}
*/
// Answer : C E F
/**
*
*synchronized关键字的问题,这里不在多说了(敲的有点累^_^)
*
*/
/** * * @author yaoyuan */class Computation extends Thread{private int num;private boolean isComplete;private int result;public Computation(int num){this.num = num;}public synchronized void run(){result = num * 2;isComplete = true;notify();}public synchronized int getResult(){while(!isComplete){try{wait();}catch(InterruptedException e){}}return result;}public static void main(String[] args){Computation[] computations = new Computation;for(int i=0;i<computations.length;i++){computations = new Computation(i);computations.start();}for(Computation c : computations){System.out.print(c.getResult() + " ");}}}
/**
*What is Result ?
*
*
*AThe code will deadlock
*BThe code may run with no output
*CAn exception thrown at runtime
*DThe code may run with output "06"
*EThe code may run with output "2064"
*FThe code may run with output "0246"
*/
// Answer : F
/** * * @author yaoyuan */public class Main{public static void main(String[] args) throws Exception{Thread.sleep(3000);System.out.print("sleep");}}
/**
*What is Result ?
*
*
*ACompilation fails
*BAn exception is thrown at runtime
*CThe code executes normally and prints "sleep"
*DThe code executes normally, but nothing is printed
*/
// Answer : C
/**
*
* @author yaoyuan
*/
/**
*Which two statements are true about has-a and is a relationships?(choose two)
*
*
*AInheritance represents an is-a relationship
*BInheritance represents an has-a relationship
*CInheritance must be used when creating a has-a relationship
*DInstance variables can be used when creating a has-a relationship
*/
// Answer : A D
/** * * @author yaoyuan */package yaoyuan;class Target{public String name = "hello";}
/**
*What can directly access and change the value of the variable name?
*
*
*A any class
*Bonly the Target class
*Cany class in the yaoyuan package
*Dany class that extends Target
*/
// Answer : C
页:
[1]