QUESTION 46
Given:
11. public class Test {
12. public static void main(String [] args) {
13. int x = 5;
14. boolean b1 = true;
15. boolean b2 = false;
16.
17. if ((x == 4) && !b2 )
18. System.out.print("1 ");
19. System.out.print("2 ");
20. if ((b2 = true) && b1 )
21. System.out.print("3 ");
22. }
23. }
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:
11. static void test() throws Error {
12. if (true) throw new AssertionError();
13. System.out.print("test ");
14. }
15. public static void main(String[] args) {
16. try { test(); }
17. catch (Exception ex) { System.out.print("exception "); }
18. System.out.print("end ");
19. }
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

11. foo();和12. System.out.print(x + ", ");随时都可以被打断,所以他两个必须有原子性,且要求x每加一就输出一次,synchronized 修饰符修饰foo正好可以满足。

QUESTION 56
Given:
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 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:

12. Date date = new Date();

13. df.setLocale(Locale.ITALY);

14. 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:
12. String csv = "Sue,5,true,3";
13. Scanner scanner = new Scanner( csv );
14. scanner.useDelimiter(",");
15. 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:

31. void go() throws Exception {

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

33. t.start();

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

35. //insert code here

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

37. } }

38. public void run() {

39. try {

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

41. // insert the same code here

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

43. }

44. } catch (Exception e) { }

45. }

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:
10. interface A { void x(); }
11. class B implements A { public void x() {} public void y() {} }
12. class C extends B { public void x() {} } And:
20. java.util.List<A> list = new java.util.ArrayList<A>();
21. list.add(new B());
22. list.add(new C());
23. for (A a : list) {
24. a.x();
25. a.y();
26. }
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()方法!

Java考试题之三的更多相关文章

  1. 实战Java虚拟机之三“G1的新生代GC”

    今天开始实战Java虚拟机之三:“G1的新生代GC”. 总计有5个系列 实战Java虚拟机之一“堆溢出处理” 实战Java虚拟机之二“虚拟机的工作模式” 实战Java虚拟机之三“G1的新生代GC” 实 ...

  2. 乐字节Java反射之三:方法、数组、类加载器和类的生命周期

    本文承接上一篇:乐字节Java发射之二:实例化对象.接口与父类.修饰符和属性 继续讲述Java反射之三:方法.数组.类加载器 一.方法 获取所有方法(包括父类或接口),使用Method即可. publ ...

  3. java设计模式之三单例模式(Singleton)

    单例对象(Singleton)是一种常用的设计模式.在Java应用中,单例对象能保证在一个JVM中,该对象只有一个实例存在.这样的模式有几个好处: 1.某些类创建比较频繁,对于一些大型的对象,这是一笔 ...

  4. 玩玩微信公众号Java版之三:access_token及存储access_token

    微信官方参考文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183   基本说明: access_token是 ...

  5. JAVA日常之三

    一.Main方法的args参数 args[] 是程序运行前可传入的参数,比如 java HelloWorld a,那么在HelloWorld的main方法里面 args就是{"a" ...

  6. Java多线程之三volatile与等待通知机制示例

    原子性,可见性与有序性 在多线程中,线程同步的时候一般需要考虑原子性,可见性与有序性 原子性 原子性定义:一个操作或者多个操作在执行过程中要么全部执行完成,要么全部都不执行,不存在执行一部分的情况. ...

  7. Java设计模式之三 ----- 建造者模式和原型模式

    前言 在上一篇中我们学习了工厂模式,介绍了简单工厂模式.工厂方法和抽象工厂模式.本篇则介绍设计模式中属于创建型模式的建造者模式和原型模式. 建造者模式 简介 建造者模式是属于创建型模式.建造者模式使用 ...

  8. Java考试题之十

    QUESTION 230 Given: 10. class One { 11. public One foo() { return this; } 12. } 13. class Two extend ...

  9. Java考试题之九

    QUESTION 177 Given: 1.     class TestException extends Exception { } 2.     class A { 3.     public ...

随机推荐

  1. oracle 查看锁表及解锁的语句

    解锁语句: alter system kill session 'sid, serial#'; alter system kill session '23, 1647'; 查询那些对象被锁: sele ...

  2. 英特尔® 实感™ 摄像头 (F200) 应用如何实现最佳用户体验

    英特尔开发人员专区原文 由于视频不能直接嵌入, 请点击视频标题观看.谢谢. 英特尔® 实感™ 技术支持我们重新定义如何与计算设备交互,包括允许用户通过手势自然交互. 为了帮助大家了解使用英特尔® 实感 ...

  3. 《图解 HTTP 》阅读 —— 第五章

    第5章 与HTTP协作的web服务器 一台服务器可以托管多个域名. 在相同的IP地址下,虚拟主机可以寄存多个不同主机名和域名的网站,所以在发送HTTP请求时,必须在Host首部内指定完整的主机名和域名 ...

  4. 内网集群准同步shell脚本

    在公司的内网中配置集群同步,可能是代理问题,ntpd和chrony都没有用,所以只好写shell脚本解决 前提条件集群中各台机器已经配置好了免密登录 一.免密登录配置 1. 用 root 用户登录.每 ...

  5. Python3实现机器学习经典算法(三)ID3决策树

    一.ID3决策树概述 ID3决策树是另一种非常重要的用来处理分类问题的结构,它形似一个嵌套N层的IF…ELSE结构,但是它的判断标准不再是一个关系表达式,而是对应的模块的信息增益.它通过信息增益的大小 ...

  6. ES6的新特性(2)——let 与 const 增强变量声明

    let 与 const 增强变量声明 ES6 新增了let命令,用来声明局部变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效,而且有暂时性死区的约束. 先看个var的常见 ...

  7. PHP中的数据类型

    PHP中包含8种数据类型,其中包括4种标量:整型,浮点型,字符串,布尔值:2种复合类型:数组和对象:一种resource类型,剩下的一种是NULL类型. 整型 PHP中的整型可以是负,也可以是正,而整 ...

  8. 关于《数据结构》课本KMP算法的理解

    数据结构课上讲的KMP算法和我在ACM中学习的KMP算法是有区别的,这里我对课本上的KMP算法给出我的一些想法. 原理和之前的KMP是一样的https://www.cnblogs.com/wkfvaw ...

  9. 20162328蔡文琛 Bag类

    在刚刚开始着手这个作业时,想的是使用for循环来自己写出add等方法来,但是在看过API后知道了Arraylist这个java已有的列表类,于是就只用ArrayList的方法很快的就做了出来.在进行B ...

  10. postion一句话很管用

    relative和absolute有本质区别,relative是相对与postion为默认值的时候元素自身位置来定位:而absolute是相对最近position为relative或absolute的 ...