六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 70|回复: 0

SCJP认证试题(十)

[复制链接]

升级  84.67%

51

主题

51

主题

51

主题

秀才

Rank: 2

积分
177
 楼主| 发表于 2013-1-27 04:40:35 | 显示全部楼层 |阅读模式
/** * * @author yaoyuan */1import java.util.*;2public class TestSet{3enum Example{ONE,TWO,THREE}4public static void main(String[] args){5Collection coll = new ArrayList();6coll.add(Example.Three);7coll.add(Example.Three);8coll.add(Example.Three);9coll.add(Example.TWO);10coll.add(Example.TWO);11coll.add(Example.ONE);12Set 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
*BThe set variable contains only three elements from the coll collection, and the order is guaranteed to be preserved
*CThe set variable contains all six elements from the coll collection, but the order is NOT guaranteed to be preserved
*DThe set variable contains only three elements from the coll collection, but the order is NOT guaranteed to be preserved
*/

// Answer : D


/** * * @author yaoyuan */11public class Person{12private String name, comment;13private int age;14public Person(String n,int a, String c){15name = n; age = a, comment = c;16}17public boolean equals(Object o){18if(!(o instanceof Person)) return false;19Person p = (Person)o;20return 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();
*Breturn super.hashCode() + 7* age;
*Creturn name.hashCode() + comment.hashCode()/2;
*Dreturn name.hashCode() + comment.hashCode()/2 - age*3;
*/

// Answer : B


/** * * @author yaoyuan */1public class Person{2private String name;3public Person(String n){ this.name = name;}4public boolean equals(Person p){5return p.name.equals(this.name);6}7}
/**
*Which statements is true?
*
*
*A      The equals method does not properly override the Object equals method
*BCompilation fails because the private attribute p.name cannot be accessed in line 5
*Cto work correctly with hash-based data structures, this class must also implement the hashCode method
*DWhen adding Person objects to java.util.Set collection, the equals method in line 4 will prevent duplicates
*/

// Answer : A


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

/**
*Which code , inserted at line 4, guaranteed that this program will output[1,2]?
*
*
*A     Set set = new TreeSet();
*BSet set = new HashSet();
*CSet set = new SortedSet();
*DList set = new SortedList();
*ESet set = new LinkedHashSet();
*/

// Answer : A



/** * * @author yaoyuan */34HashMap props = new HashMap();35props.put("key45", "some value");36props.put("key12", "some other value");37props.put("key39", "yet another value");38Set s = props.ketSet();39//insert code here
/**
*What inserted at line 39, will sort the keys in the props HashMap?
*
*A      Array.sort(s);
*Bs = new TreeSet(s);
*CCollections.sort(s);
*Ds = 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();}};
*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 : D F


/** * * @author yaoyuan */1public class Threads2 implements Runnable{23public void run(){4System.out.println("run.");5throw new RuntimeException("Problem");6}7public static void main(String[] args){8Thread t = new Thread(new Threads2());9t.start();10System.out.println("End of method");11}12}
/**
*Which to can be results?(Choose two)
*
*
*A      java.lang.RuntimeException:Problem
*Brun.
java.lang.RuntimeException:Problem
*CEnd of method
java.langRuntimeException:Problem
*DEnd of method
run
java.langRuntimeException:Problem
*Erun.
*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)
*
*
*
*Adeclare reset() using the synchronized keyword
*Bdeclare getName() using the synchronized keyword
*Cdeclare getCount() using the synchronized keyword
*Ddeclare the constructor using the synchronized keyword
*Edeclare increment() using the synchronized keyword
*/

// Answer : A C E


/** * * @author yaoyuan */1public class TestSeven extends Thread{2private static int x;3public synchronized void doThings(){4int current = x;5current++;6x = current;7}8public void run(){9doThings();10}11}
/**
*Which statements is true?
*
*
*A      Compilation fails
*BAn exception is thrown at runtime
*CSynchronizing the run() method would make the class thread-safe
*DThe data in variable "x" are protected from concurrent access problems
*EDeclaring the doThings() method as static would make the class thread-safe
*FWrapping the statements with in doThings() in a synchronized (new Object()){} block would make the class thread-safe
*/

// Answer : E

/** * * @author yaoyuan */7void waitForSignal(){8Object obj = new Object();9synchronized (Thread.currentThread()){10obj.wait();11obj.notify();12}13}
/**
*What statement is true?
*
*
*A      This code may throw an InterruptedException
*BThis code may throw an IllegalStateException
*CThis code may throw a TimeoutException after ten minutes
*DThis code will not compile unless "obj.wait()" is replaced with "((Thread)obj).wait()"
*EA call to notify() or notifyAll() from another thread may cause this method to complete normally
*/

// Answer : B
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表