QUESTION 46
Given:

  1. public class Test {
  2. public static void main(String [] args) {
  3. int x = 5;
  4. boolean b1 = true;
  5. boolean b2 = false;
  6. if ((x == 4) && !b2 )
  7. System.out.print("1 ");
  8. System.out.print("2 ");
  9. if ((b2 = true) && b1 )
  10. System.out.print("3 ");
  11. }
  12. }
    What is the result?
    A. 2
    B. 3
    C. 1 2
    D. 2 3
    E. 1 2 3
    F. Compilation fails.
    G. An exception is thrown at runtime.

如果if后面没有语句块,就是紧跟的一句语句受if影响

&与&&的区别,&&有短路功能当第一个语句是false时将不判断第二个语句,&符号两边不是boolean值时,执行位运算。

注意第20行代码处是b2=true,只有一个等号;是赋值操作!!!!

QUESTION 47
Given:

  1. static void test() throws Error {
  2. if (true) throw new AssertionError();
  3. System.out.print("test ");
  4. }
  5. public static void main(String[] args) {
  6. try { test(); }
  7. catch (Exception ex) { System.out.print("exception "); }
  8. System.out.print("end ");
  9. }
    What is the result?
    A. end
    B. Compilation fails.
    C. exception end
    D. exception test end
    E. A Throwable is thrown by main.
    F. An Exception is thrown by main.
    Answer: E

如果抛出一个异常,就不会执行下面的内容,而是返回调用产生异常的方法那里去。Error类和Exception类同继承自Throwable类,main函数不能处理Error类异常,所以一个Thorwable被main抛出。
QUESTION 55
Given:

  1. public class TestFive {
  2. private int x;
  3. public void foo() {
  4. int current = x;
  5. x = current + 1;
  6. }
  7. public void go() {
  8. for(int i = 0; i < 5; i++) {
  9. new Thread() {
  10. public void run() {
  11. foo();
  12. System.out.print(x + ", ");
  13. } }.start();
  14. } }
    Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ? (Choose two.)
    A. move the line 12 print statement into the foo() method
    B. change line 7 to public synchronized void go() {
    C. change the variable declaration on line 2 to private volatile int x;
    D. wrap the code inside the foo() method with a synchronized( this ) block
    E. wrap the for loop code inside the go() method with a synchronized block synchronized(this) { // for loop
    code here }
    Answer: AD

  15. foo();和12. System.out.print(x + ", ");随时都可以被打断,所以他两个必须有原子性,且要求x每加一就输出一次,synchronized 修饰符修饰foo正好可以满足。
    QUESTION 56
    Given:
  16. public class Threads2 implements Runnable {
  17. public void run() {
  18. System.out.println("run.");
  19. throw new RuntimeException("Problem");
  20. }
  21. public static void main(String[] args) {
  22. Thread t = new Thread(new Threads2());
  23. t.start();
  24. System.out.println("End of method.");
  25. }
  26. }
    Which two can be results? (Choose two.)
    A. java.lang.RuntimeException: Problem
    B. run.
    java.lang.RuntimeException: Problem
    C. End of method.
    java.lang.RuntimeException: Problem
    D. End of method.
    run.
    java.lang.RuntimeException: Problem
    E. run.
    java.lang.RuntimeException: Problem
    End of method.
    Answer: DE

子线程和主线程的速度不一样,System.out.println("run.");和System.out.println("End of method.");哪一个首先执行也不一定,但是必须是现输出run再有一个异常。异常输出后还要往下执行。

QUESTION 62 Given:

  1. Date date = new Date();

  2. df.setLocale(Locale.ITALY);

  3. String s = df.format(date);

The variable df is an object of typeDateFormat that has been initialized in line 11. What is the result if thiscode is run on December 14, 2000?

A. The value of s is 14-dic-2000.

B. The value of s is Dec 14, 2000.

C. An exception is thrown atruntime.

D. Compilationfails because of an error in line 13.
DateFormat没有setLocale()方法,setLocale是MessageFormat的方法

QUESTION 63

Given:

  1. public class KungFu {

  2. public static voidmain(String[] args) {

  3. Integer x = 400;

  4. Integer y = x;

  5. x++;

  6. StringBuilder sb1 = newStringBuilder("123");

  7. StringBuilder sb2 = sb1;

  8. sb1.append("5");

  9. System.out.println((x==y) +" " + (sb1==sb2));

  10. }

  11. }

What is the result?

A. true true

B. false true

C. C. true false

D. false false

E. Compilation fails.

F. An exception is thrown atruntime.

Answer: B

sb1,sb2,指向的都是同一个地址;

QUESTION 64
Given that the current directory is empty, and that the user has read and write privileges to the current
directory, and the following:

  1. import java.io.*;
  2. public class Maker {
  3. public static void main(String[] args) {
  4. File dir = new File("dir");
  5. File f = new File(dir, "f");
  6. }
  7. }
    Which statement is true?
    A. Compilation fails.
    B. Nothing is added to the file system.
    C. Only a new file is created on the file system.
    D. Only a new directory is created on the file system.
    E. Both a new file and a new directory are created on the file system.

没有调用创建方法,只是申明了Fiel类

QUESTION 65
Given:

  1. String csv = "Sue,5,true,3";
  2. Scanner scanner = new Scanner( csv );
  3. scanner.useDelimiter(",");
  4. int age = scanner.nextInt();
    What is the result?
    A. Compilation fails.
    B. After line 15, the value of age is 5.
    C. After line 15, the value of age is 3.
    D. An exception is thrown at runtime.

获取的标记与期望类型的模式不匹配

useDelimiter 将此扫描器的分割模式设置为从指定String构造的模式。

Delimiter英文意思为分隔符;useDelimiter( )方法默认以空格作为分隔符;当然也修改,如:

useDelimiter(","); //以','为分隔符

useDelimiter("\n"); //“\n”换行符(回车)作为输入的分隔符。

QUESTION 67 Given that Triangleimplements Runnable, and:

  1. void go() throws Exception {

  2. Thread t = new Thread(newTriangle());

  3. t.start();

  4. for(int x = 1; x < 100000;x++) {

  5. //insert code here

  6. if(x%100 == 0)System.out.print("g");

  7. } }

  8. public void run() {

  9. try {

  10. for(int x = 1; x < 100000;x++) {

  11. // insert the same code here

  12. if(x%100 == 0)System.out.print("t");

  13. }

  14. } catch (Exception e) { }

  15. }

Which two statements, insertedindependently at both lines 35 and 41, tend to allow both threads totemporarily pause and allow the other thread to execute? (Choose two.)

A. Thread.wait();

B. Thread.join();

C. Thread.yield();//线程舍弃

D. Thread.sleep(1);// 线程休眠

E. Thread.notify();

Answer: CD
QUESTION 71
Given:

  1. interface A { void x(); }
  2. class B implements A { public void x() {} public void y() {} }
  3. class C extends B { public void x() {} } And:
  4. java.util.List list = new java.util.ArrayList();
  5. list.add(new B());
  6. list.add(new C());
  7. for (A a : list) {
  8. a.x();
  9. a.y();
  10. }
    What is the result?
    A. The code runs with no output.
    B. An exception is thrown at runtime.
    C. Compilation fails because of an error in line 20.
    D. Compilation fails because of an error in line 21.
    E. Compilation fails because of an error in line 23.
    F. Compilation fails because of an error in line 25.
    Answer: F

实现了A接口的类的对象a仅仅有x()方法,没有y()方法!

OCJP视频课堂,具体讲解:https://edu.csdn.net/course/detail/7811