论坛首页 入门技术论坛

SCJP认证试题(二)

浏览 1964 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-11-12  
 /**
 *
 * @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
*B An Exception is thrown at runtime
*C The code executes and prints "running"
*D The code executes and prints "runningrunning"
*E The 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,
*B 0,2,4,6,8,10,2,4,
*C 0,2,4,6,8,10,12,14
*D 0,0,2,2,4,4,6,6,8,8,10,10,12,12,14,14
*E 0,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
*B This code may thorw an IllegalStateException
*C This code may throw a TimeOutException after ten minutes
*D This code will not compile unless "obj.wait()" is replaced with "((Thread)obj).wait()"
*E Reversing 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
*B An exception thrown at runtime
*C The code executes and prints "StartedComplete"
*D The code executes and prints "StartedComplete"
*E The 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();
* }
* };
*
*B new Thread(){
* public void start(){
* doStuff();
* }
* };
*
*C new Thread(){
* public void start(){
* doStuff();
* }
* };
* run();
*
*D new Thread(){
* public void run(){
* doStuff();
* }
* }
* start();
*
*E new Thread(new Runnable(){
* public void run(){
* doStuff();
* }
* });
* run();
*
*F new Thread(new Runnable(){
* public void run(){
* doStuff();
* }
* });
* start();
*/



// Answer : D  F



/**
*
* @author yaoyuan
*/


/**
*Which three will compile and run without exception?(choose three)
*
*
*A      private synchronized object;
*
*B void go(){
* synchronized(){
* / code here  /
* }
* }
*
*C public synchronized void go(){
* / code here /
* }
*
*D private synchronized(this) void go(){
* / code here /
* }
*
*E void go(){
* synchronized(object.class){
* / code here /
* }
* }
*
*F void 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[4];
		for(int i=0;i<computations.length;i++){
			computations[i] = new Computation(i);
			computations[i].start();
		}
		for(Computation c : computations){
			System.out.print(c.getResult() + " ");
		}
	
	}
}

/**
*What is Result ?
*
*
*A The code will deadlock
*B The code may run with no output
*C An exception thrown at runtime
*D The code may run with output "06"
*E The code may run with output "2064"
*F The 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 ?
*
*
*A Compilation fails
*B An exception is thrown at runtime
*C The code executes normally and prints "sleep"
*D The 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)
*
*
*A Inheritance represents an is-a relationship
*B Inheritance represents an has-a relationship
*C Inheritance must be used when creating a has-a relationship
*D Instance 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
*B only the Target class
*C any class in the yaoyuan package
*D any class that extends Target
*/

// Answer : C



   发表时间:2008-11-12  
so good
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics