笔试之SCJP6

  1、

  public static void main(String [] args) {
    
System.out.println(args.length > 4 &&
  args[4].equals("-d"));

}

此题它给出的答案是编译错误, 事实上此代码片段并没有编译错误, 运行打印出“false”。 理由就是&&是短路与,在判断出args.length>4是错误时就不会再去进行后面的判断了, 因此也就不会报越界错误了。

 

2、

 public char getResult(){return 1;}
   
    Given:
     11. public static void main( String[] args ) {
     12. Integer a = new Integer(10);
     13. Integer b = new Integer(10);
     14. Integer c = a;


     Actualtests.com - The Power of Knowing
     
     310-035


     15. int d = 10;
     16. double e = 10.0;
     17. }
     Which three evaluate to true? (Choose three)

     A. (a == c)

     B. (d == e)

     C. (b == d)

      D. (a == b)

      E. (b == c)
      F. (d == 10.0)  // 列出此题的目的只是因为自己在之前的笔试中遇到过类似的并且做错了, 起警示作用 。

 

3、

 Given:
     11. String a = null;
     12. a.concat("abc");
     13. a.concat("def");
     14. System.out.println(a); What is the result?

     A. abc
     B. null
     C. abcdef
     D. Compilation fails.
     E. The code runs with no output.
     F. An exception is thrown at runtime. Answer: F
     Explanation:
     Exception in thread "main" java.lang.NullPointerException at X.main(X.java:12)


     Given that b and c refer to instances of wrapper classes, which two statements are true? (Choose two)

     A. b.equals(b) returns true.
     B. b.equals(c) returns the same result as b == c.
     C. b.eqials(c) can return false even if c.equals(b) returns true.
     D. b.equals(c) throws an exception if b and c are different wrapper types.

     E. b.equals(c) returns false if the type of wrapper objects being compared are
     different.

解析: 此题答案为BC, 但是我觉得答案错误了, 我的答案是AE。

   B的话要分情况理解, 假如覆盖了equals的话则不好判断, 假如没有覆盖则B也正确。 假如此处的包装类是专指基本类型对应的包装类的话那么B错误。

  C 我觉得是明显的错误, equals具有自发性。  D的话并不会抛异常, 其会打印 false。

 

4、 

Given:
   1. public class SyncTest {
   2. private int x;
   3. private int y;
   4. private synchronized void setX( int i ) { x = i; }
   5. private synchronized void setY( int i ) { y = i; }
   6. public void setXY( int i ) { setX(i); setY(i); }
   7. public synchronized boolean check() { return x != y; }
   8. }
   Under which condition will check return true when called from a different class?

   A. check can never return true.
   B. check can return true when setXY is called by multiple threads.
   C. check can return true when multiple threads call setX and setY separately.
   D. check can return true only if SyncTest is changed to allow x and y to be set separately.

  此题答案是B,   但是目前仍然没有弄得很明白,  有待对线程进一步的关注!

 

5、

What happens when thread X executes a wait() method on object A, without owning object A's lock?
  A. Compilation fails.
  B. An exception is thrown.
  C. The wait() method has no effect.
  D. Thread X receives the lock immediately.
  E. Object A moves the thread to the wait pool.

此题答案为B, ^_^ ^_^  又一道 线程题。

 

6、

  Given:
   1. class MyThread extends Thread {
   2. public void run() { System.out.println("AAA"); }
   3. public void run(Runnable r) { System.out.println("BBB"); }
   4.
   5. public static void main(String[] args) {
   6. new Thread(new MyThread()).start();
   7. }
   8. }
   What is the result?

   A. AAA B. BBB
   C. Compilation fails.
   D. The code runs with no output.

答案为: A。

 

7、 一道较综合的线程题

 Given:
   1. public class X implements Runnable {
   2. private int x;
   3. private int y;
   4.
   5. public static void main(String [] args) {
   6. X that = new X();
   7. (new Thread( that )).start();
   8. (new Thread( that )).start();
   9. }
   10.
   11. public void run() {
   12. for (;;) {
   13. synchronized (this) {
   14. x++;

  15. y++;
   16. }
   17.
   System.out.println(Thread.currentThread().getName() +
   18. "x = " + x + ", y = " +
   y);
   19. }
   20. }
   21. }
   What is the result?

   A. Compilation fails.
   B. The program prints pairs of values for x and y that might not always be the same on the same line (for example, "x = 2, y = 1").
   C. The program prints pairs of values for x and y that are always the same on the same line (for example, "x = 1, y = 1").
   In addition, each value appears only once (for example, "x = 1, y = 1" followed by "x = 2, y = 2").
   The thread name at the start of the line shows that both threads are executing concurrently.
   D. The program prints pairs of values for x and y that are always the same on the same line (for example, "x = 1, y = 1").
   In addition, each value appears only once (for example, "x = 1, y = 1" followed by "x = 2, y = 2").
   The thread name at the start of the line shows that only a single thread is actually executing.
 

解析 :  答案是D。   ^_^  ^_^  运行结果确实验证了D, 但是有一点差错的是打印的线程名有两个呀 ...  ...

 

8、

 Which statement is true?

    A. To call the wait() method, a thread most own the lock of the current thread.
    B.. To call the wait() method, a thread must own the lock of the object on which the call is to be made
    C. To call the join() method, a thread must own the lock of the object on which the call is to be made.
    D. To call the sleep() method, a thread must own the lock of the object which the call
    is to be made.
    E. To call the yield() method, a thread must own the lock of the object on which the cal is to be made.

 答案:  B

 

9、

Given:
 1. public class A extends Thread {
 2. A() {
 3. setDaemon(true);
 4. }
 5.
 6. public void run() {
 7. (new B()).start();
 8. try {
 9. Thread.sleep(60000);
 10. } catch (InterruptedException x) {}
 11. System.out.println("A done");
 12. }
 13.
 14. class B extends Thread {
 15. public void run() {
 16. try {
 17. Thread.sleep(60000);
 18. } catch (InterruptedException x) {}
 19. System.out.println("B done");
 21. }
 22. }
 23.
 24. public static void main(String[] args) {
 25. (new A()).start();
 26. }
 27. }
 What is the result?

 A. A done B. B done C. A done B done
 D. B done
 A done
 E. There is no exception that the application will print anything.
 F. The application outputs "A done" and "B done", in no guaranteed order.

答案为: E。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值