Java考试题之五
QUESTION 102 Given:
23. Object [] myObjects = {
24. new Integer(12),
25. new String("foo"),
26. new Integer(5),
27. new Boolean(true)
28. };
29. Arrays.sort(myObjects);
30. for(int i=0;i<myObjects.length; i++) {
31. System.out.print(myObjects[i].toString());
32. System.out.print("");
33. }
What is the result?
A. Compilation fails due to anerror in line 23.
B. Compilation fails due to anerror in line 29.
C. A ClassCastException occurs inline 29.
D. A ClassCastException occurs inline 31.
E. The value of all four objectsprints in natural order.
Answer: C
sort方法比较的必须是可以转化成相同的而且实现了Comparable接口的相同对象
QUESTION 103
Given:
1. public class Donkey {
2. public static void main(String[] args) {
3. boolean assertsOn = false;
4. assert (assertsOn) : assertsOn = true;
5. if(assertsOn) {
6. System.out.println("assert is on");
7. }
8. }
9. }
If class Donkey is invoked twice, the first time without assertions enabled, and the second time with
assertions enabled, what are the results?
A. no output
B. no output
assert is on
C. assert is on
D. no output
An AssertionError is thrown.
E. assert is on
An AssertionError is thrown.
Answer: D
考察assert,如果是断言有效的情况,即assert(false),就会抛出一个AssertionError
如果是true,则返回的是assert is on
QUESTION 105 Given:
11. public static voidmain(String[] args) {
12. try {
13. args = null;
14. args[0] = "test";
15. System.out.println(args[0]);
16. } catch (Exception ex) {
17. System.out.println("Exception");
18. } catch (NullPointerExceptionnpe) {
19. System.out.println("NullPointerException");
20. }
21. }
What is the result?
A. test
B. Exception
C. Compilation fails.
D. NullPointerException
Answer: C
首先是小异常处理,然后是大的异常处理!!!!如果返过来,就有问题了,导致编译失败!!!
QUESTION 106
Given:
22. public void go() {
23. String o = "";
24. z:
25. for(int x = 0; x < 3; x++) {
26. for(int y = 0; y < 2; y++) {
27. if(x==1) break;
28. if(x==2 && y==1) break z;
29. o = o + x + y;
30. }
31. }
32. System.out.println(o);
33. }
What is the result when the go() method is invoked?
A. 00
B. 0001
C. 000120
D. 00012021
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: C
考察break,当x=0,y=0,o=00;
当x=0,y=1,o=0001;
当x=1,y=0,break掉内循环,o=0001;
当x=2,y=0,o=000120;
当x=2,y=1,break掉断点处的外循环,o=000120;
QUESTION 109
Given:
1. public class Boxer1{
2. Integer i;
3. int x;
4. public Boxer1(int y) {
5. x = i+y;
6. System.out.println(x);
7. }
8. public static void main(String[] args) {
9. new Boxer1(new Integer(4));
10. }
11. }
What is the result?
A. The value "4" is printed at the command line.
B. Compilation fails because of an error in line 5.
C. Compilation fails because of an error in line 9.
D. A NullPointerException occurs at runtime.
E. A NumberFormatException occurs at runtime.
F. An IllegalStateException occurs at runtime.
Answer: D
第五行5. x = i+y;,还没有为i赋值(此时i为null),无法运算,注意看属性Integer i,没有给i赋值;
QUESTION 111
Given:
1. public class Venus {
2. public static void main(String[] args) {
3. int [] x = {1,2,3};
4. int y[] = {4,5,6};
5. new Venus().go(x,y);
6. }
7. void go(int[]... z) {
8. for(int[] a : z)
9. System.out.print(a[0]);
10. }
11. }
What is the result?
A. 1
B. 12
C. 14
D. 123
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: C
int[]... z表示可变参数,而可变的是数组的个数。一共有两个数组,输出每个数组的第一个元素
QUESTION 112
Given:
10. public class Foo {
11. static int[] a;
12. static { a[0]=2; }
13. public static void main( String[] args ) {}
14. }
Which exception or error will be thrown when a programmer attempts to run this code?
A. java.lang.StackOverflowError
B. java.lang.IllegalStateException
C. java.lang.ExceptionInInitializerError
D. java.lang.ArrayIndexOutOfBoundsException
Answer: C
还没new呢,就赋值。初始化错误。
QUESTION 113
Given:
11. class X { public void foo() { System.out.print("X "); } }
12.
13. public class SubB extends X {
14. public void foo() throws RuntimeException {
15. super.foo();
16. if (true) throw new RuntimeException();
17. System.out.print("B ");
18. }
19. public static void main(String[] args) {
20. new SubB().foo();
21. }
22. }
What is the result?
A. X, followed by an Exception.
B. No output, and an Exception is thrown.
C. Compilation fails due to an error on line 14.
D. Compilation fails due to an error on line 16.
E. Compilation fails due to an error on line 17.
F. X, followed by an Exception, followed by B.
Answer: A
多态性,首先是调用的SubB里面的foo()函数,而执行SubB里面的foo()函数首先就要super.foo();即调用父类的foo()函数输出X,后来就会抛出一个异常,回到调用该抛出异常的方法的地方。不会输出B
QUESTION 125
A team of programmers is reviewing a proposed API for a new utility class. After some discussion, they
realize that they can reduce the number of methods in the API without losing any functionality. If they
implement the new design, which two OO principles will they be promoting?
A. Looser coupling
B. Tighter coupling
C. Lower cohesion
D. Higher cohesion
E. Weaker encapsulation
F. Stronger encapsulation
Answer: A
内聚:一个模块内各个元素彼此结合的紧密程度
耦合:一个软件结构内不同模块之间互连程度的度量
这里减少API数量却不丢失任何功能,意思是各个模块之间的联系很小,互联程度很低,属于低耦合。
Java考试题之五的更多相关文章
- 【Java】实战Java虚拟机之五“开启JIT编译”
今天开始实战Java虚拟机之五“开启JIT编译” 总计有5个系列 实战Java虚拟机之一“堆溢出处理” 实战Java虚拟机之二“虚拟机的工作模式” 实战Java虚拟机之三“G1的新生代GC” 实战Ja ...
- [转]World Wind Java开发之五——读取本地shp文件
World Wind Java 使用IconLayer图层类表现点和多点数据,使用RenderableLayer图层表现线和面数据,一个图层只能对应一组shape文件.World Wind Java首 ...
- java设计模式之五原型模式(Prototype)
原型模式虽然是创建型的模式,但是与工程模式没有关系,从名字即可看出,该模式的思想就是将一个对象作为原型,对其进行复制.克隆,产生一个和原对象类似的新对象.本小结会通过对象的复制,进行讲解.在Java中 ...
- Java考试题之十
QUESTION 230 Given: 10. class One { 11. public One foo() { return this; } 12. } 13. class Two extend ...
- Java考试题之九
QUESTION 177 Given: 1. class TestException extends Exception { } 2. class A { 3. public ...
- Java考试题之八
QUESTION 139 Giventhe following directory structure: bigProject |--source | |--Utils.java ||--classe ...
- Java考试题之六
QUESTION 134 Given:11. class Snoochy {12. Boochy booch;13. public Snoochy() { booch = new Boochy(thi ...
- Java考试题之四
QUESTION 73 Given: 10: public class Hello { 11: String title; 12: int value; 13: public Hello() { 14 ...
- Java考试题之三
QUESTION 46Given:11. public class Test {12. public static void main(String [] args) {13. int x = 5;1 ...
随机推荐
- java多线程相关代码
1.创建线程的三种方式 使用Thread package com.wpbxx.test; //1.自定义一个类,继承java.lang包下的Thread类 class MyThread extends ...
- webpack2.0+ vue2.0
一 webpack 2.0 及用到的插件安装(默认已经有node环境) 1. package.json文件 (插件安装及插件的功能不详解) { "private": true, & ...
- VMware启动Centos时出现错误Cannot open the disk 'xxxxxxx.vmdk' or one of the snapshot disks it depends on. .
今天拔装虚拟机的硬盘的时候,没有关掉虚拟机,导致虚拟打开的时候出现:Cannot open the disk 'xxxxxxx.vmdk' or one of the snapshot disks i ...
- 转载---VisualStudioCode通过SSH远程编辑文件
最近需要长期修改远端服务器上的代码,调试.vim操作又不是很6,想到了远程操作的办法,找到一篇好用的bolg,记录一下. 原文链接:https://blog.csdn.net/qq_38401919/ ...
- Scrum立会报告+燃尽图(十月十一日总第二次):需求分析
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2191 Scrum立会master:张俊余 一.小组介绍 组长:付佳 组员 ...
- Answer the questions(回答自己的问题)
第一章: 问题:我们现在学了这个专业,如果想全面去了解,应该还要学习哪些课程? 回答:其实软件工程只是一个比较大的范畴,以后如果要出去工作,我们还要细分,比如说开发安卓,开发游戏,web架构方面等很多 ...
- Good Time 冲刺 一
2018/6/14 我们组之前没有开发小程序的经验,所以在尝试中不断探索与学习.在完成小程序的初步注册和界面完善后,我们组开始进行开发任务. 1.1成员简述: 王怡镔:“今天主要学习小程序开发知识及相 ...
- C语言中以十六进制输出字符型变量会出现'ffffff"的问题
最近在做一个C的嵌入式项目,发现在C语言中用printf()函数打印字符型变量时,如果想采用"%x"的格式将字符型变量值以十六进制形式打印出来,会出现一个小问题,如下: char ...
- #Leetcode# 692. Top K Frequent Words
https://leetcode.com/problems/top-k-frequent-words/ Given a non-empty list of words, return the k mo ...
- windows下python3.6安装pycryto or crypto or pycryptodome与使用
pycrypto,pycrytodome和crypto是一个东西,在很久以前,crypto在python上面的名字是pycrypto它是一个第三方库,但是已经停止更新三年了,所以不建议安装这个库: w ...