论坛首页 入门技术论坛

SCJP认证试题(十)

浏览 2039 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-11-24  
 /**
 *
 * @author yaoyuan
 */
1	import java.util.*;
2		public class TestSet{
3			enum Example{ONE,TWO,THREE}
4			public static void main(String[] args){
5				Collection coll = new ArrayList();
6				coll.add(Example.Three);
7				coll.add(Example.Three);
8				coll.add(Example.Three);
9				coll.add(Example.TWO);
10				coll.add(Example.TWO);
11				coll.add(Example.ONE);
12				Set set = new HashSet(coll);
13			}
14		}


/**
*Which statements is true about the set variable on line 12?
*
*
*A      The set variable contains all six elements from the coll collection, and the order is guaranteed to be preserved
*B The set variable contains only three elements from the coll collection, and the order is guaranteed to be preserved
*C The set variable contains all six elements from the coll collection, but the order is NOT guaranteed to be preserved
*D The set variable contains only three elements from the coll collection, but the order is NOT guaranteed to be preserved
*/

// Answer : D


 /**
 *
 * @author yaoyuan
 */

11	public class Person{
12		private String name, comment;
13		private int age;
14		public Person(String n,int a, String c){
15			name = n; age = a, comment = c;
16		}
17		public boolean equals(Object o){
18			if(!(o instanceof Person)) return false;
19			Person p = (Person)o;
20			return age == p.age && name.equals(p.name);
21		}
22	}

/**
*What is the appropriated definition of the hashCode method in class Person?
*
*
*A      return super.hashCode();
*B return super.hashCode() + 7* age;
*C return name.hashCode() + comment.hashCode()/2;
*D return name.hashCode() + comment.hashCode()/2 - age*3;
*/

// Answer : B


 /**
 *
 * @author yaoyuan
 */

1	public class Person{
2		private String name;
3		public Person(String n){ this.name = name;}
4		public boolean equals(Person p){
5			return p.name.equals(this.name);
6		}
7	}

/**
*Which statements is true?
*
*
*A      The equals method does not properly override the Object equals method
*B Compilation fails because the private attribute p.name cannot be accessed in line 5
*C to work correctly with hash-based data structures, this class must also implement the hashCode method
*D When adding Person objects to java.util.Set collection, the equals method in line 4 will prevent duplicates
*/

// Answer : A


 /**
 *
 * @author yaoyuan
 */

1	import java.util.*;
2		public class Example{
3			public static void main(String[] args){
4				//insert code here
5				set.add(new Integer(1));
6				set.add(new Integer(2));
7				System.out.println(set);
8			}
9		}


/**
*Which code , inserted at line 4, guaranteed that this program will output[1,2]?
*
*
*A     Set set = new TreeSet();
*B Set set = new HashSet();
*C Set set = new SortedSet();
*D List set = new SortedList();
*E Set set = new LinkedHashSet();
*/

// Answer : A



 /**
 *
 * @author yaoyuan
 */

34	HashMap props = new HashMap();
35	props.put("key45", "some value");
36	props.put("key12", "some other value");
37	props.put("key39", "yet another value");
38	Set s = props.ketSet();
39	//insert code here

/**
*What inserted at line 39, will sort the keys in the props HashMap?
*
*A      Array.sort(s);
*B s = new TreeSet(s);
*C Collections.sort(s);
*D s = new SortedSet(s);
*/

// Answer : B

/**
*
* @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
 */

1	public class Threads2 implements Runnable{
2		
3		public void run(){
4			System.out.println("run.");
5			throw new RuntimeException("Problem");
6		}
7		public static void main(String[] args){
8			Thread t = new Thread(new Threads2());
9			t.start();
10			System.out.println("End of method");
11		}
12	}

/**
*Which to can be results?(Choose two)
*
*
*A      java.lang.RuntimeException:Problem
*B run.
java.lang.RuntimeException:Problem
*C End of method
java.langRuntimeException:Problem
*D End of method
run
java.langRuntimeException:Problem
*E run.
* java.langRuntimeException:Problem
* End of method
*/

// Answer : D E



 /**
 *
 * @author yaoyuan
 */

public class NamedCounter{
	private final String name;
	private int count;
	public NamedCounter(String name){this.name = name};
	public String getName(){return name;}
	public void increment(){count++;}
	public int getCount(){return count;}
	public void reset(){count=0;}	
}

/**
*Which three changes should be made to adapt this class to be used safely by multiple threads?(Choose three)
*
*
*
*A declare reset() using the synchronized keyword
*B declare getName() using the synchronized keyword
*C declare getCount() using the synchronized keyword
*D declare the constructor using the synchronized keyword
*E declare increment() using the synchronized keyword
*/

// Answer : A C E


 /**
 *
 * @author yaoyuan
 */

1	public class TestSeven extends Thread{
2		private static int x;
3		public synchronized void doThings(){
4			int current = x;
5			current++;
6			x = current;
7		}
8		public void run(){
9			doThings();
10		}
11	}

/**
*Which statements is true?
*
*
*A      Compilation fails
*B An exception is thrown at runtime
*C Synchronizing the run() method would make the class thread-safe
*D The data in variable "x" are protected from concurrent access problems
*E Declaring the doThings() method as static would make the class thread-safe
*F Wrapping the statements with in doThings() in a synchronized (new Object()){} block would make the class thread-safe
*/

// Answer : E

 /**
 *
 * @author yaoyuan
 */

7	void waitForSignal(){
8		Object obj = new Object();
9		synchronized (Thread.currentThread()){
10			obj.wait();
11			obj.notify();
12		}
13	}

/**
*What statement is true?
*
*
*A      This code may throw an InterruptedException
*B This code may throw 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 A call to notify() or notifyAll() from another thread may cause this method to complete normally
*/

// Answer : B
论坛首页 入门技术版

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