Java试题二

举报
tea_year 发表于 2021/12/30 00:39:55 2021/12/30
【摘要】 OCJP视频课堂,具体讲解:https://edu.csdn.net/course/detail/7811   QUESTION 37 Given:1. class Super {2. private int a;3. protected Super(int a) { this.a = a; }4. } ...11. c...

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

 

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.

 

文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/54407012

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。