scjp学习笔记3
2008-01-24 23:20
253 查看
Question 54
===========================================================
What happens when you attempt to compile and run the following application?
1. class Parent {
2. public static void main(String[] args) {
3. Integer i = new Integer(5);
4. int j = i.parseInt("6");
5. System.out.println("j = " + j);
6. }
7. }
Only One:
1) Compiler error.
// ???
2) The application prints "j = 5".
3) The application prints "j = 6".
//静态方法也可以用对象使用
Question 55
===========================================================
True or false: the equals() method in the Object class compares the data of two objects bit by bit, checking for identical data.
Only One:
1) True
// ???
2) False
//
Question 56
===========================================================
Which (if any) of the lines shown are printed out by the following application?
1. class Z {
2. public static void main(String[] args) {
3. String s = new String("hello");
4. if (s == s+"world")
5. System.out.println("AAA");
6. StringBuffer sb = new StringBuffer("hello");
7. if (sb == sb.append("world"))
8. System.out.println("BBB");
9. }
10. }
Only One:
1) "AAA"
2) "BBB"
//
3) Neither of the above
Question 62
===========================================================
When you run the following application, which type of component is responsible for painting the pixel at the extreme bottom-left of the interior of the Frame? (Note: the call to getComponentCount() returns the number of components that the frame contains.)
import java.awt.*;
class A {
public static void main(String[] args) {
Frame f = new Frame();
f.setSize(500, 500);
f.setLayout(new GridLayout(2, 3));
outer: for (int y = 0; y < 3; y++) {
for (int x = 0; x < 2; x++) {
if (x==1 && y==1)
break outer;
f.add(new Button("BTN"));
}
}
while (f.getComponentCount() < 6)
f.add(new Checkbox());
f.setVisible(true);
}
}
Only One:
1) A button
2) A checkbox
Question 63
===========================================================
What is the result of attempting to compile and execute the following code?
1. public class X {
2. public static void main(String[] args) {
3. byte b1 = 1;
4. byte b2 = 127;
5. short s = b1 + b2;
6. }
7. }
Only One:
1) Compiler error at line 3.
2) Compiler error at line 4.
3) Compiler error at line 5.
//
4) Exception thrown at line 5.
5) The code compiles, and executes without throwing any exceptions.
Question 68
===========================================================
Can use of the following class create a memory leak in a single-threaded application?
1. class MyStack {
2. private Object[] data = new Object[1000];
3. private int stackPtr = 0;
4.
5. public void push(Object pushMe) throws Exception {
6. if (stackPtr >= 1000)
7. throw new Exception();
8. data[stackPtr++] = pushMe;
9. }
10.
11. public Object pop() throws Exception {
12. if (stackPtr == 0)
13. throw new Exception();
14. return data[--stackPtr];
15. }
16. }
Only One:
1) Yes
//
2) No
Question 69
===========================================================
When the following application is executed, what is printed out? What would be printed out if line 9 were omitted?
1. class W {
2. public int i;
3. W() { i = 5; }
4. }
5.
6. class X extends W {
7. public int j;
8. X() {
9. super();
10. j = 1111;
11. }
12. public static void main(String[] args) {
13. X theX = new X();
14. System.out.println(theX.i);
15. }
16. }
Only One:
1) The program prints out "0". If line 9 were omitted, the program would print out "0".
2) The program prints out "0". If line 9 were omitted, the program would print out "5".
3) The program prints out "5". If line 9 were omitted, the program would print out "0".
4) The program prints out "5". If line 9 were omitted, the program would print out "5".
//
Question 71
===========================================================
In the following code, does line 7 compile?
1. class OutThing {
2. private int i;
3. private final int j = 6;
4.
5. class InThing {
6. private void foo() {
7. int k = i + j;
8. }
9. }
10. }
Only One:
1) Yes
//
2) No
Question 72
===========================================================
What is the result of attempting to compile the following code and then execute the class Outward?
1. class Outward {
2. final int i = 10;
3.
4. public static void main(String[] args) {
5. new Outward ();
6. }
7.
8. Outward {
9. (new Inward()).foo();
10. }
11.
12. class Inward {
13. void foo() {
14. int i = 20;
15. System.out.println("i = " + i);
16. }
17. }
18. }
Only One:
1) Compiler error on line 14;
2) Line 15 prints out "i = 0".
3) Line 15 prints out "i = 10".
4) Line 15 prints out "i = 20".
//
5) The code compiles successfully. The value printed on line 15 cannot be determined before runtime.
Question 79
===========================================================
What does the following application print out?
1. import java.io.*;
2. class Parent {
3. public static void main(String[] a) {
4. try {
5. FileOutputStream fos = new FileOutputStream("x");
6. DataOutputStream dos = new DataOutputStream(fos);
7. dos.writeDouble(12345.98765);
8. dos.close(); fos.close();
9. FileInputStream fis = new FileInputStream("x");
10. DataInputStream dis = new DataInputStream(fis);
11. byte b = dis.readByte();
12. dis.close(); fis.close();
13. System.out.println("b = " + b);
14. } catch (IOException e) { System.out.println("STRESS"); }
15. }
16. }
Only One:
1) "12345"
2) "12345.98765"
3) "STRESS"
4) None of the above
//????
Question 81
===========================================================
What happens when you try to compile and run the following code?
1. class X {
2. public static void main(String[] a) {
3. try {
4. short s = -1;
5. byte b = (byte)s;
6. System.out.println("b = " + b);
7. }
8. catch (Exception e) { System.out.println("TROUBLE!"); }
9. }
10. }
Only One:
1) Compiler error at line 4.
2) Compiler error at line 5.
3) The code compiles and prints "b = 0" ... but if the cast on line 5 were omitted, then line 5 would not compile.
4) The code compiles and prints "b = 0"; the cast on line 5 does no harm, but can omitted without causing a compiler error.
5) The code compiles and prints "b = -1" ... but if the cast on line 5 were omitted, then line 5 would not compile.
//
Question 83
===========================================================
In the following code, what are the legal types for the variable vari, which is declared on line 2? Choose all correct options.
1. class C {
2. public ???? vari;
3.
4. public static void main(String[] args) {
5. foo(vari);
6. }
7.
8. static void foo(short f) { System.out.println("s = " + s); }
9. }
Mutiple:
1) boolean
2) byte
//
3) char
//???
4) double
5) short
//
Question 85
===========================================================
What does the following application print out?
1. class Eek {
2. static int nth;
3. public int i;
4. private double j;
5.
6. Eek() {
7. i = nth; j = nth * 3.456; nth++;
8. }
9.
10. public boolean equals(Object other) {
11. return (i == ((Eek)other).i);
12. }
13.
14. public static void main(String[] args) {
15. Eek e1 = new Eek(); Eek e2 = new Eek();
16. e1.i = 1;
17. if (e1.equals(e2))
18. System.out.println("YES");
//
19. else
20. System.out.println("NO");
21. }
22. }
Only One:
1) YES
//
2) NO
Question 91
===========================================================
Which of the following statements is/are true? Choose all correct options.
Mutiple:
1) An abstract class must contain at least one abstract method.
2) If a class contains one or more abstract methods, then the class must be declared abstract.
//
3) Neither of the above is true.
Question 92
===========================================================
Does this code compile?
1. class SubExA extends Exception { }
2. class SubExB extends Exception { }
3.
4. class Parent {
5. void foo(int i) throws SubExA {
6. if (i > 20)
7. throw new SubExA();
8. }
9. }
10.
11. class Kid extends Parent {
12. void foo(int i) throws SubExB {
13. if (i < 20)
14. throw new SubExB();
15. }
16. }
Only One:
1) Yes
// ???
2) No
//
Question 93
===========================================================
What is the result of attempting to compile the following code and then execute the class Ocean?
1. class Fish { }
2. class Shark extends Fish { }
3. class Guppy extends Fish { }
4.
5. public class Ocean {
6. public static void main(String[] args) {
7. Fish f = new Shark();
8. Guppy g = (Guppy)f;
9. }
10. }
Only One:
1) Compiler error at line 7.
2) Compiler error at line 8.
3) Exception thrown at line 7.
4) Exception thrown at line 8.
//
5) The code compiles and executes with no problems.
// ???
Question 94
===========================================================
What does the following application print out?
1. class Fruit {
2. void whoYou() { System.out.println("Fruit"); }
3. }
4.
5. class Citrus extends Fruit {
6. void whoYou() { System.out.println("Citrus"); }
7. }
8.
9. class Tangerine extends Citrus {
10. void whoYou() { super.whoYou(); }
11.
12. public static void main(String[] args) {
13. Fruit f = new Tangerine();
14. f.whoYou();
15. }
16. }
Only One:
1) "Fruit"
2) "Citrus"
//
===========================================================
What happens when you attempt to compile and run the following application?
1. class Parent {
2. public static void main(String[] args) {
3. Integer i = new Integer(5);
4. int j = i.parseInt("6");
5. System.out.println("j = " + j);
6. }
7. }
Only One:
1) Compiler error.
// ???
2) The application prints "j = 5".
3) The application prints "j = 6".
//静态方法也可以用对象使用
Question 55
===========================================================
True or false: the equals() method in the Object class compares the data of two objects bit by bit, checking for identical data.
Only One:
1) True
// ???
2) False
//
Question 56
===========================================================
Which (if any) of the lines shown are printed out by the following application?
1. class Z {
2. public static void main(String[] args) {
3. String s = new String("hello");
4. if (s == s+"world")
5. System.out.println("AAA");
6. StringBuffer sb = new StringBuffer("hello");
7. if (sb == sb.append("world"))
8. System.out.println("BBB");
9. }
10. }
Only One:
1) "AAA"
2) "BBB"
//
3) Neither of the above
Question 62
===========================================================
When you run the following application, which type of component is responsible for painting the pixel at the extreme bottom-left of the interior of the Frame? (Note: the call to getComponentCount() returns the number of components that the frame contains.)
import java.awt.*;
class A {
public static void main(String[] args) {
Frame f = new Frame();
f.setSize(500, 500);
f.setLayout(new GridLayout(2, 3));
outer: for (int y = 0; y < 3; y++) {
for (int x = 0; x < 2; x++) {
if (x==1 && y==1)
break outer;
f.add(new Button("BTN"));
}
}
while (f.getComponentCount() < 6)
f.add(new Checkbox());
f.setVisible(true);
}
}
Only One:
1) A button
2) A checkbox
Question 63
===========================================================
What is the result of attempting to compile and execute the following code?
1. public class X {
2. public static void main(String[] args) {
3. byte b1 = 1;
4. byte b2 = 127;
5. short s = b1 + b2;
6. }
7. }
Only One:
1) Compiler error at line 3.
2) Compiler error at line 4.
3) Compiler error at line 5.
//
4) Exception thrown at line 5.
5) The code compiles, and executes without throwing any exceptions.
Question 68
===========================================================
Can use of the following class create a memory leak in a single-threaded application?
1. class MyStack {
2. private Object[] data = new Object[1000];
3. private int stackPtr = 0;
4.
5. public void push(Object pushMe) throws Exception {
6. if (stackPtr >= 1000)
7. throw new Exception();
8. data[stackPtr++] = pushMe;
9. }
10.
11. public Object pop() throws Exception {
12. if (stackPtr == 0)
13. throw new Exception();
14. return data[--stackPtr];
15. }
16. }
Only One:
1) Yes
//
2) No
Question 69
===========================================================
When the following application is executed, what is printed out? What would be printed out if line 9 were omitted?
1. class W {
2. public int i;
3. W() { i = 5; }
4. }
5.
6. class X extends W {
7. public int j;
8. X() {
9. super();
10. j = 1111;
11. }
12. public static void main(String[] args) {
13. X theX = new X();
14. System.out.println(theX.i);
15. }
16. }
Only One:
1) The program prints out "0". If line 9 were omitted, the program would print out "0".
2) The program prints out "0". If line 9 were omitted, the program would print out "5".
3) The program prints out "5". If line 9 were omitted, the program would print out "0".
4) The program prints out "5". If line 9 were omitted, the program would print out "5".
//
Question 71
===========================================================
In the following code, does line 7 compile?
1. class OutThing {
2. private int i;
3. private final int j = 6;
4.
5. class InThing {
6. private void foo() {
7. int k = i + j;
8. }
9. }
10. }
Only One:
1) Yes
//
2) No
Question 72
===========================================================
What is the result of attempting to compile the following code and then execute the class Outward?
1. class Outward {
2. final int i = 10;
3.
4. public static void main(String[] args) {
5. new Outward ();
6. }
7.
8. Outward {
9. (new Inward()).foo();
10. }
11.
12. class Inward {
13. void foo() {
14. int i = 20;
15. System.out.println("i = " + i);
16. }
17. }
18. }
Only One:
1) Compiler error on line 14;
2) Line 15 prints out "i = 0".
3) Line 15 prints out "i = 10".
4) Line 15 prints out "i = 20".
//
5) The code compiles successfully. The value printed on line 15 cannot be determined before runtime.
Question 79
===========================================================
What does the following application print out?
1. import java.io.*;
2. class Parent {
3. public static void main(String[] a) {
4. try {
5. FileOutputStream fos = new FileOutputStream("x");
6. DataOutputStream dos = new DataOutputStream(fos);
7. dos.writeDouble(12345.98765);
8. dos.close(); fos.close();
9. FileInputStream fis = new FileInputStream("x");
10. DataInputStream dis = new DataInputStream(fis);
11. byte b = dis.readByte();
12. dis.close(); fis.close();
13. System.out.println("b = " + b);
14. } catch (IOException e) { System.out.println("STRESS"); }
15. }
16. }
Only One:
1) "12345"
2) "12345.98765"
3) "STRESS"
4) None of the above
//????
Question 81
===========================================================
What happens when you try to compile and run the following code?
1. class X {
2. public static void main(String[] a) {
3. try {
4. short s = -1;
5. byte b = (byte)s;
6. System.out.println("b = " + b);
7. }
8. catch (Exception e) { System.out.println("TROUBLE!"); }
9. }
10. }
Only One:
1) Compiler error at line 4.
2) Compiler error at line 5.
3) The code compiles and prints "b = 0" ... but if the cast on line 5 were omitted, then line 5 would not compile.
4) The code compiles and prints "b = 0"; the cast on line 5 does no harm, but can omitted without causing a compiler error.
5) The code compiles and prints "b = -1" ... but if the cast on line 5 were omitted, then line 5 would not compile.
//
Question 83
===========================================================
In the following code, what are the legal types for the variable vari, which is declared on line 2? Choose all correct options.
1. class C {
2. public ???? vari;
3.
4. public static void main(String[] args) {
5. foo(vari);
6. }
7.
8. static void foo(short f) { System.out.println("s = " + s); }
9. }
Mutiple:
1) boolean
2) byte
//
3) char
//???
4) double
5) short
//
Question 85
===========================================================
What does the following application print out?
1. class Eek {
2. static int nth;
3. public int i;
4. private double j;
5.
6. Eek() {
7. i = nth; j = nth * 3.456; nth++;
8. }
9.
10. public boolean equals(Object other) {
11. return (i == ((Eek)other).i);
12. }
13.
14. public static void main(String[] args) {
15. Eek e1 = new Eek(); Eek e2 = new Eek();
16. e1.i = 1;
17. if (e1.equals(e2))
18. System.out.println("YES");
//
19. else
20. System.out.println("NO");
21. }
22. }
Only One:
1) YES
//
2) NO
Question 91
===========================================================
Which of the following statements is/are true? Choose all correct options.
Mutiple:
1) An abstract class must contain at least one abstract method.
2) If a class contains one or more abstract methods, then the class must be declared abstract.
//
3) Neither of the above is true.
Question 92
===========================================================
Does this code compile?
1. class SubExA extends Exception { }
2. class SubExB extends Exception { }
3.
4. class Parent {
5. void foo(int i) throws SubExA {
6. if (i > 20)
7. throw new SubExA();
8. }
9. }
10.
11. class Kid extends Parent {
12. void foo(int i) throws SubExB {
13. if (i < 20)
14. throw new SubExB();
15. }
16. }
Only One:
1) Yes
// ???
2) No
//
Question 93
===========================================================
What is the result of attempting to compile the following code and then execute the class Ocean?
1. class Fish { }
2. class Shark extends Fish { }
3. class Guppy extends Fish { }
4.
5. public class Ocean {
6. public static void main(String[] args) {
7. Fish f = new Shark();
8. Guppy g = (Guppy)f;
9. }
10. }
Only One:
1) Compiler error at line 7.
2) Compiler error at line 8.
3) Exception thrown at line 7.
4) Exception thrown at line 8.
//
5) The code compiles and executes with no problems.
// ???
Question 94
===========================================================
What does the following application print out?
1. class Fruit {
2. void whoYou() { System.out.println("Fruit"); }
3. }
4.
5. class Citrus extends Fruit {
6. void whoYou() { System.out.println("Citrus"); }
7. }
8.
9. class Tangerine extends Citrus {
10. void whoYou() { super.whoYou(); }
11.
12. public static void main(String[] args) {
13. Fruit f = new Tangerine();
14. f.whoYou();
15. }
16. }
Only One:
1) "Fruit"
2) "Citrus"
//
相关文章推荐
- SCJP学习笔记
- scjp学习笔记2
- 参加SCJP认证时学习的笔记
- scjp学习笔记
- 参加SCJP认证时学习的笔记 .
- 《SCJP试题1000例》学习笔记1
- 《SCJP试题1000例》学习笔记2
- java期末复习之SCJP题库学习笔记
- 统计学习方法学习笔记(五)--决策树
- Scala学习笔记
- python学习笔记
- Python学习笔记(八)-(2)matplotlib作图之legend
- cocos2d-x学习笔记【三】添加精灵及相关操作
- JSP学习笔记之三
- JAVA基础18天速成学习笔记之第13天
- 算法学习笔记二:插入排序及循环不变式
- 【原】Java学习笔记011 - 数组
- ubuntu 操作系统学习笔记之用户与用户组
- WCF学习笔记--WCF提供的9个标准绑定
- 学习笔记 之方法