现在的位置: 首页 > 综合 > 正文

scjp学习笔记2

2014年09月05日 ⁄ 综合 ⁄ 共 7642字 ⁄ 字号 评论关闭

Question 30

===========================================================

What happens when you try to compile and run the following application?

1. import java.io.*;

2. class Parent {

3.    public static void main(String[] args) {

4.      FileOutputStream fos = new FileOutputStream("f");
    
5.      DataOutputStream dos = new DataOutputStream(fos);

6.      dos.writeFloat(1.234f);

7.      dos.close(); fos.close();

8.      FileInputStream fis = new FileInputStream("f");

9.      DataInputStream dis = new DataInputStream(fis);

10.      float f = dis.readFloat();

11.      dis.close(); fis.close();

12.      System.out.println(f);

13.    }

14. }

 

Only One:

1) Compiler error.
     //需要捕捉异常
2) The program prints out "-5.6".

3) The program prints out a numeric value other than -5.6.

 

 

Question 31

===========================================================

Does the following code compile? If not, where is the first compiler error?

1. interface Inter { }

2.

3. class A implements Inter { }

4.

5. class B extends A {

6.    void aMethod() {

7.      A a = new B();

8.      Inter i = a;

9.      A a1 = i;

10.    }

11. }

 

Only One:

1) The code compiles without error.

2) Compiler error at line 7.

3) Compiler error at line 8.

4) Compiler error at line 9.
//需要强制转换

 

 

 

 

 

Question 35

===========================================================

Consider the following code:

1. interface Inter { }

2. class A { }

3. class B extends A implements Inter { }

4. class C extends B {

5.    public static void main(String[] args) {

6.      A a = new A();

7.      B b = new B();

8.      C[] c = new C[10];

9.      if (a instanceof Inter)

10.        System.out.println("Hello");

11.      if (b instanceof A)

12.        System.out.println("Hello");

13.      if (c instanceof C[])

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

15.    }

16. }

When you run class C as an application, which of the following lines execute? Choose all correct options.

 

Mutiple:

1) Line 10
          ????
2) Line 12
     //
3) Line 14
     //

 

Question 36

===========================================================

You are given the class file for a class called Sssh. However, you do not have the source code or any information about the internals of the Sssh class. You do know that the class has a protected int variable called i. What does the following application print out?

1. class Mine extends Sssh {

2.    int z;

3.    public static void main(String[] args) {

4.       Mine m1 = new Mine(); m1.i = 5; m1.z = 12;

5.       Mine m2 = new Mine(); m2.i = 5; m2.z = 12;

6.       if (m1.equals(m2))

7.         System.out.println("YES");

8.       else

9.         System.out.println("NO");

10.    }

11.

12.    public boolean equals(Object ob) {

13.      if (z != ((Mine)ob).z)

14.        return false;

15.      return super.equals(ob);

16.    }

17. }

 

Only One:

1) YES

2) NO

3) It is impossible to know
   //

 

Question 37

===========================================================

What happens when you attempt to compile the following code?

1. class A            {   static void foo() {};   }

2. class B extends A {   void foo(int i) {};     }

 

Only One:

1) Compiler error at line 1.

2) Compiler error at line 2.

3) No compiler error.
        //

 

Question 38

===========================================================

What happens when you attempt to compile and execute the following application?

1. class A {

2.    final int y;

3.    public static void main(String[] args) { new A(); }

4.    A() { y = 5; }

5. }

 

Only One:

1) Compiler error at line 2.

2) Compiler error at line 3.

3) Compiler error at line 4.

4) Exception thrown at line 4.

5) The code compiles with no errors and runs without throwing any exceptions.
//

 

Question 39

===========================================================

Which of the following statements is/are true? Choose all correct options.

 

Mutiple:

1) An abstract variable may not be read by a subclass of the class that owns the variable.

2) An abstract variable may not be written by a subclass of the class that owns the variable.

3) An abstract method of a class may only read an abstract variable of the class.

4) An abstract method of a class may only read an abstract variable or a static variable of the class.

5) None of the above statements is true.
      //

 

Question 40

===========================================================

What happens when you attempt to compile and execute the following application?

1.   class A {

2.     static int si = 5;

3.     static { System.out.println(si); }

4.     A() { si++; }

5.

6.     public static void main(String[] args) {

7.       for (int i = 0; i < 3; i++)

8.         new A();

9.    }

10. }

 

Only One:

1) Compiler error at line 3.

2) The program prints out "5" once.
     //
3) The program prints out "5" three times.

4) The program prints out three lines: "5", "6", and "7".

 

 

Question 41

===========================================================

Given the following code, which access modifiers can legally be placed before aMethod() on line 6? Choose all correct options.

1. class Thing {

2.    void aMethod() { }

3. }

4.

5. class SubThing extends Thing {

6.    long aMethod() { return 123L; }

7. }

 

Mutiple:

1) public

2) protected
  
3) private
    
4) None of the above
    //

 

Question 42

===========================================================

Does this code compile?

1.   class SubEx extends Exception { }

2.

3.   class Parent {

4.     void foo(int i) throws SubEx {

5.       if (i > 20)

6.         throw new Exception();

7.     }

8.   }

9.

10.   class Kid extends Parent {

11.     void foo(int i) throws Exception {

12.       if (i < 20)

13.         throw new SubEx();

14.     }

15.   }

 

Only One:

1) Yes

2) No
     //

 

Question 43

===========================================================

Does this code compile?

1.   class SubEx extends Exception {   SubEx(String s) {super(s);}   }

2.   class SubSubEx extends SubEx   { }
   //要说明调用的父级的构造方法
3.

4.   class Parent {

5.     void foo(int i) throws Exception {

6.       if (i > 20)

7.         throw new Exception();

8.     }

9.   }

10.

11.   class Kid extends Parent {

12.     void foo(int i) throws SubEx {

13.       if (i < 20)

14.         throw new SubEx();
   //没有该构造方法
15.     }

16.   }

 

Only One:

1) Yes
    
2) No
     //  

 

Question 44

===========================================================

Does the following code compile?

1. class X {

2.    public void xxx(int i) { }

3.    public int   xxx(int i) { return 1; }

4. }

 

Only One:

1) Yes

2) No
       //

 

Question 45

===========================================================

What happens when you try to compile and execute the Gorilla application?

1. class Mammal {

2.    public void whoAreYou() { System.out.println("A Mammal"); }

3. }

4.

5. class Primate extends Mammal {

6.    public void whoAreYou() { System.out.println("A Primate"); }

7. }

8.

9. class Gorilla extends Primate {

10.    public void whoAreYou() { super.super.whoAreYou(); }

11.    public static void main(String[] args) {

12.      (new Gorilla()).whoAreYou();

13.    }

14. }

 

Only One:

1) Compiler error.
   //
2) The code prints out "A Mammal".

3) The code prints out "A Primate".

 

 

Question 46

===========================================================

What happens when you try to compile and execute the following application?

1. class X   {

2.    public static void main(String[] args) {

3.      String s = "abcde";

4.      char c = nthChar(s, 1000);

5.      System.out.println("nth char = " + c + "!");

6.    }

7.

8.    static char nthChar(String s, int n) {

9.      if (n >= s.length())

10.        throw new ArrayIndexOutOfBoundsException();

11.      return s.charAt(n);

12.    }

13. }

 

Only One:

1) Compiler error at line 4.

2) Compiler error at line 8.
   //???
3) The code compiles, and prints out "nth char = !".

4) The code compiles, and prints out "nth char = a!".

5) The code compiles, and prints out an exception message.
     //

 

Question 47

===========================================================

When this method is called, does the exception handler on line 7 execute?
1. double squirt() {
2.    try {
3.      double d = -1.0;
4.      double d1 = Math.sqrt(d);
5.    }
6.    catch (Exception e) {
7.      System.out.println("Caught one.");
8.    }
9. }

 

Only One:

1) Yes
    
2) No
     //

 

 

Question 51

===========================================================

How many objects are created by the following code?

1. StringBuffer s1 = new StringBuffer("abc");

2. StringBuffer s2 = s1;

3. StringBuffer s3 = new StringBuffer("abc");

 

Only One:

1) None

2) 1

3) 2
//       ????????
4) 3
     //

 

Question 52

===========================================================

Which of the following statements is/are true about this code?

1. public class X {

2.    public static void main(String[] args) {

3.      Double d1 = new Double(1);

4.      Double d2 = new Double(1);

5.      if (d1==d2)

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

7.      if (d1.equals(d2))

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

9.    }

10. }

 

Mutiple:

1) The code does not compile.

2) Line 6 executes.

3) Line 8 executes.
//

 

抱歉!评论已关闭.