QUESTION 37
Given:
1. class Super {
2. private int a;
3. protected Super(int a) { this.a = a; }
4. } ...
11. class Sub extends Super {
12. public Sub(int a) { super(a); }
13. public Sub() { this.a = 5; }
14. }
Which two, independently, will allow Sub to compile? (Choose two.)
A. Change line 2 to:
public int a;
B. Change line 2 to:
protected int a;
C. Change line 13 to:
public Sub() { this(5); }
D. Change line 13 to:
public Sub() { super(5); }
E. Change line 13 to:

public Sub() { super(a); }

A.B改变父类的属性,对于子类的this.a不起作用;子类调用父类的属性,super.a

子类构造器会隐式调用父类无参构造器,如果父类没有需要显示调用有参构造器

QUESTION 38
Given:
1. public class Base {
2. public static final String FOO = "foo";
3. public static void main(String[] args) {
4. Base b = new Base();
5. Sub s = new Sub();
6. System.out.print(Base.FOO);
7. System.out.print(Sub.FOO);
8. System.out.print(b.FOO);
9. System.out.print(s.FOO);
10. System.out.print(((Base)s).FOO);
11. } }
12. class Sub extends Base {public static final String FOO="bar";} What is the result?
A. foofoofoofoofoo
B. foobarfoobarbar
C. foobarfoofoofoo
D. foobarfoobarfoo
E. barbarbarbarbar
F. foofoofoobarbar
G. foofoofoobarfoo

(((Base)s).FOO)由外向内转型

QUESTION 40
Given:
2. public class Hi {
3. void m1() { }
4. protected void() m2 { }
5. }
6. class Lois extends Hi {
7. // insert code here
8. }
Which four code fragments, inserted independently at line 7, will compile? (Choose four.)
A. public void m1() { }
B. protected void m1() { }
C. private void m1() { }
D. void m2() { } protected>default级别
E. public void m2() { }
F. protected void m2() { }
G. private void m2() { }

子类覆写方法时不能降低访问级别

考察重写,子类中的重写方法的作用域不可以reduce(减小)。public>protected>default>private

QUESTION 41
Which two code fragments are most likely to cause a StackOverflowError? (Choose two.)
A. int []x = {1,2,3,4,5};
for(int y = 0; y < 6; y++)
System.out.println(x[y]);
B. static int[] x = {7,6,5,4};
static { x[1] = 8;
x[4] = 3; } //不是StackOverflowError异常
C. for(int y = 10; y < 10; y++)
doStuff(y);
D. void doOne(int x) { doTwo(x); }
void doTwo(int y) { doThree(y); }
void doThree(int z) { doTwo(z); }
E. for(int x = 0; x < 1000000000; x++)
doStuff(x);
F. void counter(int i) { counter(++i); }

StackOverflowError,递归过深错误,递归没有出口

QUESTION 42
Given:
11. class A {
12. public void process() { System.out.print("A,"); }
13. class B extends A {
14. public void process() throws IOException {
15. super.process();
16. System.out.print("B,");
17. throw new IOException();
18. }
19. public static void main(String[] args) {
20. try { new B().process(); }
21. catch (IOException e) { System.out.println("Exception"); }
22. }
What is the result?
A. Exception
B. A,B,Exception
C. Compilation fails because of an error in line 20.
D. Compilation fails because of an error in line 14.
E. A NullPointerException is thrown at runtime.
Answer: D

第十四行抛出了一个父类没有的异常,错误。子类不可以抛出父类没有的异常。

QUESTION 43
Given:
11. public void go(int x) {
12. assert (x > 0);
13. switch(x) {
14. case 2: ;
15. default: assert false;
16. }
17. }
18. private void go2(int x) { assert (x < 0); }
Which statement is true?
A. All of the assert statements are used appropriately.
B. Only the assert statement on line 12 is used appropriately.
C. Only the assert statement on line 15 is used appropriately.
D. Only the assert statement on line 18 is used appropriately.
E. Only the assert statements on lines 12 and 15 are used appropriately.
F. Only the assert statements on lines 12 and 18 are used appropriately.
G. Only the assert statements on lines 15 and 18 are used appropriately.

使用assert的原则:1.只能验证private方法中的参数,不能验证public方法的参数;2.不能验证命令行参数;3.可以验证不应该发生的分支语句;4.不能以任何方式改变程序的状态。

QUESTION 45 Given:

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

12. String str = "null";

13. if (str == null) {

14. System.out.println("null");

15. } else (str.length() == 0) {   //这一行,应该为else if

16. System.out.println("zero");

17. } else {

18. System.out.println("some");

19. }

20. }

What is the result?

A.   null

B.   zero

C.   some

D.   Compilationfails.

E.   An exception is thrown atruntime.

最新文章

  1. 转:聊聊mavenCenter和JCenter
  2. eclipse导出jar包的方法
  3. RecContentType有哪些
  4. Linux 计划任务 Crontab 笔记与总结(3)Crontab 配置文件
  5. debian 学习记录-3 -关于linux -1
  6. C# ashx与html的联合使用
  7. Windows系统基本概念
  8. React的React Native
  9. tomcat7的web.xml的xml片段与注解资源的发现处理逻辑
  10. java调用webservice,restful
  11. AspectJ开发
  12. VisualStudio2008+水晶报表的使用
  13. Linux记录-AWK语法(转载)
  14. js-for (var in )遍历顺序乱了
  15. .net下WinDbg使用说明
  16. C#中二进制、十进制和十六进制互相转换的方法
  17. PHP 利用redis 做统计缓存mysql的压力
  18. TP 模板的变量输出
  19. sqli-labs学习笔记 DAY5
  20. JavaScript经常使用对象

热门文章

  1. rocketmq Lock failed,MQ already started -c参数
  2. Appium+python HTML测试报告(1)(转)
  3. Linux 安装Redis&lt;集群版&gt;(使用Mac远程访问)
  4. Zookeeper-----Zookeeper概述
  5. Parcel 打包器简单使用记录
  6. MongoDB Chapter1:Introduction
  7. Python常用模块之PIL
  8. React Native 【学习总结】-【常用命令】
  9. 对石家庄铁道大学网站UI的分析
  10. “我爱淘”第二冲刺阶段Scrum站立会议6