码迷,mamicode.com
首页 > 编程语言 > 详细

Java试题二

时间:2018-07-05 18:24:04      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:ESS   cat   使用   one   bar   efault   hat   分支   语句   

QUESTION 37
Given:

  1. class Super {
  2. private int a;
  3. protected Super(int a) { this.a = a; }
  4. } ...
  5. class Sub extends Super {
  6. public Sub(int a) { super(a); }
  7. public Sub() { this.a = 5; }
  8. }
    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:

  1. public class Hi {
  2. void m1() { }
  3. protected void() m2 { }
  4. }
  5. class Lois extends Hi {
  6. // insert code here
  7. }
    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:

  1. class A {
  2. public void process() { System.out.print("A,"); }
  3. class B extends A {
  4. public void process() throws IOException {
  5. super.process();
  6. System.out.print("B,");
  7. throw new IOException();
  8. }
  9. public static void main(String[] args) {
  10. try { new B().process(); }
  11. catch (IOException e) { System.out.println("Exception"); }
  12. }
    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:

  1. public void go(int x) {
  2. assert (x > 0);
  3. switch(x) {
  4. case 2: ;
  5. default: assert false;
  6. }
  7. }
  8. 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:

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

  2. String str = "null";

  3. if (str == null) {

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

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

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

  7. } else {

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

  9. }

  10. }

What is the result?

A. null

B. zero

C. some

D. Compilationfails.

E. An exception is thrown atruntime.

Java试题二

标签:ESS   cat   使用   one   bar   efault   hat   分支   语句   

原文地址:http://blog.51cto.com/2096101/2136777

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!