📄 mockcert06.txt
字号:
1. class HasStatic
2. {
3. private static int x = 100;
4.
5. public static void main(String args[])
6. {
7. HasStatic hs1 = new HasStatic();
8. hs1.x++;
9. HasStatic hs2 = new HasStatic();
10. hs2.x++;
11. hs1 = new HasStatic();
12. hs1.x++;
13. HasStatic.x++;
14. System.out.println("x = " + x);
15. }
16. }
Only One:
1) Line 8 will not compile, because it is a static reference to a private variable.
2) Line 13 will not compile, because it is a static reference to a private variable.
3) The program compiles, and the output is x = 102.
4) The program compiles, and the output is x = 103.
5) The program compiles, and the output is x = 104.
Question 27
===========================================================
Given the code shown, and making no other changes, which access modifiers (public, protected, or private) can legally be placed before aMethod() on line 3?
1. class SuperDuper
2. {
3. void aMethod() { }
4. }
5.
6. class Sub extends SuperDuper
7. {
8. void aMethod() { }
9. }
Only One:
1) public
2) protected
3) private
Question 28
===========================================================
Which modifier or modifiers should be used to denote a variable that should not be written out as part of its class's persistent state? (Choose the shortest possible answer.)
Mutiple:
1) private
2) protected
3) private protected
4) transient
5) private transient
Question 29
===========================================================
Given this class definition:
1. package abcde;
2.
3. public class Bird {
4. protected static int referenceCount = 0;
5. public Bird() { referenceCount++; }
6. protected void fly() { /* Flap wings, etc. */ }
7. static int getRefCount() { return referenceCount; }
8. }
Which one statement is true about class Bird above and class Parrot below?
1. package abcde;
2.
3. class Parrot extends abcde.Bird {
4. public void fly() {
5. /* Parrot-specific flight code. */
6. }
7. public int getRefCount() {
8. return referenceCount;
9. }
10. }
Only One:
1) Compilation of Parrot.java fails at line 4, because method fly() is protected in the superclass, and classes Bird and Parrot are in the same package.
2) Compilation of Parrot.java fails at line 4, because method fly() is protected in the superclass and public in the subclass, and methods may not be overridden to be more public.
3) Compilation of Parrot.java fails at line 7, because method getRefCount() is static in the superclass, and static methods may not be overridden to be non-static.
4) Compilation of Parrot.java succeeds, but a runtime exception is thrown if method fly() is ever called on an instance of class Parrot.
5) Compilation of Parrot.java succeeds, but a runtime exception is thrown if method getRefCount() is ever called on an instance of class Parrot.
Question 30
===========================================================
Given this class definition:
1. package abcde;
2.
3. public class Bird {
4. protected static int referenceCount = 0;
5. public Bird() { referenceCount++; }
6. protected void fly() { /* Flap wings, etc. */ }
7. static int getRefCount() { return referenceCount; }
8. }
Which one statement is true about class Bird above and class Nightingale below?
1. package singers;
2.
3. class Nightingale extends abcde.Bird {
4. Nightingale() { referenceCount++; }
5.
6. public static void main(String args[]) {
7. System.out.print("Before: " + referenceCount);
8. Nightingale florence = new Nightingale();
9. System.out.println(" After: " + referenceCount);
10. florence.fly();
11. }
12. }
Only One:
1) The program will compile and execute. The output will be Before: 0 After: 2
2) The program will compile and execute. The output will be Before: 0 After: 1
3) Compilation of Nightingale will fail at line 4, because static members cannot be overridden.
4) Compilation of Nightingale will fail at line 10, because method fly() is protected in the superclass.
5) Compilation of Nightingale will succeed, but an exception will be thrown at line 10, because method fly() is protected in the superclass.
Question 31
===========================================================
Which of the following statements is correct? (Choose one.)
Only One:
1) Only primitives are converted automatically; to change the type of an object reference, you have to do a cast.
2) Only object references are converted automatically; to change the type of a primitive, you have to do a cast.
3) Arithmetic promotion of object references requires explicit casting.
4) Both primitives and object references can be both converted and cast.
5) Casting of numeric types may require a runtime check.
Question 32
===========================================================
Which one line in the following code will not compile?
1. byte b = 5;
2. char c = `5';
3. short s = 55;
4. int i = 555;
5. float f = 555.5f;
6. b = s;
7. i = c;
8. if (f > b)
9. f = i;
Only One:
1) Line 1
2) Line 3
3) Line 5
4) Line 6
5) Line 9
Question 33
===========================================================
Will the following code compile?
1. byte b = 2;
2. byte b1 = 3;
3. b = b * b1;
Only One:
1) Yes
2) No
Question 34
===========================================================
In the code below, what are the possible types for variable result? (Choose the most complete true answer.)
1. byte b = 11;
2. short s = 13;
3. result = b * ++s;
Only One:
1) byte, short, int, long, float, double
2) boolean, byte, short, char, int, long, float, double
3) byte, short, char, int, long, float, double
4) byte, short, char
5) int, long, float, double
Question 35
===========================================================
Consider the following class:
1. class Cruncher {
2. void crunch(int i) {
3. System.out.println("int version");
4. }
5. void crunch(String s) {
6. System.out.println("String version");
7. }
8.
9. public static void main(String args[]) {
10. Cruncher crun = new Cruncher();
11. char ch = `p';
12. crun.crunch(ch);
13. }
14. }
Which of the statements below is true? (Choose one.)
Only One:
1) Line 5 will not compile, because void methods cannot be overridden.
2) Line 12 will not compile, because there is no version of crunch() that takes a char argument.
3) The code will compile but will throw an exception at line 12.
4) The code will compile and produce the following output: int version
5) The code will compile and produce the following output: String version
Question 36
===========================================================
Which of the statements below is true? (Choose one.)
Only One:
1) Object references can be converted in assignments but not in method calls.
2) Object references can be converted in method calls but not in assignments.
3) Object references can be converted in both method calls and assignments, but the rules governing these conversions are very different.
4) Object references can be converted in both method calls and assignments, and the rules governing these conversions are identical.
5) Object references can never be converted.
Question 37
===========================================================
Consider the following code. Which line will not compile?
1. Object ob = new Object();
2. String stringarr[] = new String[50];
3. Float floater = new Float(3.14f);
4.
5. ob = stringarr;
6. ob = stringarr[5];
7. floater = ob;
8. ob = floater;
Only One:
1) Line 1
2) Line 3
3) Line 5
4) Line 6
5) Line 7
Question 38
===========================================================
Assume this class hierarchy:
Animal
|
Mammal
|
--------------------------------------------
| | | |
Dog Cat Raccoon SwampThing
(implements (implements
Washer) Washer)
Consider the following code:
1. Dog rover, fido;
2. Animal anim;
3.
4. rover = new Dog();
5. anim = rover;
6. fido = (Dog)anim;
Which of the statements below is true? (Choose one.)
Mutiple:
1) Line 5 will not compile.
2) Line 6 will not compile.
3) The code will compile but will throw an exception at line 6.
4) The code will compile and run.
5) The code will compile and run, but the cast in line 6 is not required and can be eliminated.
Question 39
===========================================================
Assume this class hierarchy:
Animal
|
Mammal
|
--------------------------------------------
| | | |
Dog Cat Raccoon SwampThing
(implements (implements
Washer) Washer)
Consider the following code:
1. Cat sunflower;
2. Washer wawa;
3. SwampThing pogo;
4.
5. sunflower = new Cat();
6. wawa = sunflower;
7. pogo = (SwampThing)wawa;
Which of the statements below is true? (Choose one.)
Only One:
1) Line 6 will not compile; an explicit cast is required to convert a Cat to a Washer.
2) Line 7 will not compile, because you cannot cast an interface to a class.
3) The code will compile and run, but the cast in line 7 is not required and can be eliminated.
4) The code will compile but will throw an exception at line 7, because runtime conversion from an interface to a class is not permitted.
5) The code will compile but will throw an exception at line 7, because the runtime class of wawa cannot be converted to type SwampThing.
Question 40
===========================================================
Assume this class hierarchy:
Animal
|
Mammal
|
--------------------------------------------
| | | |
Dog Cat Raccoon SwampThing
(implements (implements
Washer) Washer)
Consider the following code:
1. Raccoon rocky;
2. SwampThing pogo;
3. Washer w;
4.
5. rocky = new Raccoon();
6. w = rocky;
7. pogo = w;
Which of the following statements is true? (Choose one.)
Only One:
1) Line 6 will not compile; an explicit cast is required to convert a Raccoon to a Washer.
2) Line 7 will not compile; an explicit cast is required to convert a Washer to a SwampThing.
3) The code will compile and run.
4) The code will compile but will throw an exception at line 7, because runtime conversion from an interface to a class is not permitted.
5) The code will compile but will throw an exception at line 7, because the runtime class of w cannot be converted to type SwampThing.
Question 41
===========================================================
Consider the following code:
1. for (int i = 0; i < 2; i++) {
2. for (int j = 0; j < 3; j++) {
3. if (i == j) {
4. continue;
5. }
6. System.out.println("i = " + i + " j = " + j);
7. }
8. }
Which lines would be part of the output?
Mutiple:
1) i = 0 j = 1
2) i = 0 j = 2
3) i = 1 j = 0
4) i = 1 j = 1
5) i = 1 j = 2
Question 42
===========================================================
Consider the following code:
1. outer: for (int i = 0; i < 2; i++) {
2. for (int j = 0; j < 3; j++) {
3. if (i == j) {
4. continue outer;
5. }
6. System.out.println("i = " + i + " j = " + j);
7. }
8. }
Which lines would be part of the output?
Only One:
1) i = 0 j = 0
2) i = 0 j = 1
3) i = 0 j = 2
4) i = 1 j = 0
5) i = 1 j = 1
Question 43
===========================================================
Which of the following are legal loop constructions? (Choose one or more.)
Mutiple:
1) 1. while (int i < 7) {
2. i++;
3. System.out.println("i is " + i);
4. }
2) 1. int i = 3;
2. while (i) {
3. System.out.println("i is " + i);
4. }
3) 1. int j = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -