SCJP 5 Mock Questions on Declarations, Initialization and Scoping

Given:

5. enum Towns1{NY, LA, SF}
6.
7. public class DeclareEnum {
8.
9. enum Towns2{NY, LA, SF};
10.
11. public static void main(String [] args) {
12. enum Towns3{NY, LA, SF};
13. }
14. }


What is the result?


A
The code compiles.
B
Compilation fails due to an error on line 5.
C
Compilation fails due to an error on line 9.
D
Compilation fails due to an error on line 12.
E
Compilation fails due to errors on lines 5 and 12.
F
Compilation fails due to errors on lines 9 and 12.

Reference
Close
JLS 3.0, 8.9

Option D is correct. An enum may NOT be declared in a method.





---------------------------------

Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.1 > Question 2
Your answer is correct.
Given three different source files:


1. package com.sun2;
2. public enum Seasons {SUMMER, FALL, WINTER, SPRING }


And:


1. import com.sun2.Seasons;
2. class Enum3a {
3. Seasons s = Seasons.FALL;
4. }


And:


1. import com.sun2.*;
2. class Enum3b {
3. Seasons s = Seasons.FALL;
4. }


Which is true?


A
Both classes, Enum3a and Enum3b, will compile.
B
Neither class, Enum3a nor Enum3b, will compile.
C
Class Enum3a will compile, class Enum3b will NOT compile.
D
Class Enum3b will compile, class Enum3a will NOT compile.

Reference
Close
JLS 3.0, 8.9

Option A is correct. An enum can be imported.
---------------------------------------------


Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.2 > Question 3
Your answer is incorrect. The correct answer(s) are highlighted.
Given:


1. abstract class Color {
2. protected abstract String getRGB();
3. }
4.
5. public class Blue extends Color {
6. // insert code here
7. }


And four declarations:


public String getRGB() { return "blue"; }
String getRGB() { return "blue"; }
private String getRGB() { return "blue"; }
protected String getRGB() { return "blue"; }


How many, inserted independently at line 6, will compile?


A
0
B
1
C
2
D
3
E
4

Reference
Close
JLS 3.0, 8.9

Option C is correct. Normal overriding rules apply, so the public and protected declarations are correct.

-----------------------------------------------

Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.2 > Question 4
Your answer is correct.
Given:


1. interface Animal {
2. void eat();
3. }
4.
5. // insert code here
6.
7. public class HouseCat extends Feline {
8. public void eat() { }
9. }


And five declarations:


abstract class Feline implements Animal { }
abstract class Feline implements Animal { void eat(); }
abstract class Feline implements Animal { public void eat(); }
abstract class Feline implements Animal { public void eat() { } }
abstract class Feline implements Animal { abstract public void eat(); }


How many, inserted independently at line 5, will compile?


A
0
B
1
C
2
D
3
E
4
F
5

Reference
Close
JLS 3.0, 8

Option D is correct. The 1st, 4th, and 5th declarations are legal. Declarations 2 and 3 are illegal because the 'no body' methods must also be abstract.

-------------------------------------------

Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.3 > Question 5
Your answer is correct.
Given:


1. class CopyArray {
2. public static void main(String [] args) {
3. int [] x = {1, 2 ,3};
4. // insert code here
5. }
6. }


Which two, inserted independently at line 4, will compile? (Choose two.)


A
int [] y1 = x;
B
int [] y2; y2 = x;
C
int [] y3 = x.copy();
D
int [] y4; for(int z : x) { y4[z] = x[z]; }

Reference
Close
JLS 3.0,

Option C is incorrect because arrays do not have a copy method. Option D is incorrect because y4 was NOT initialized.

---------------------------------------------

Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.3 > Question 6
Your answer is incorrect. The correct answer(s) are highlighted.
Given the five declarations:


int a_really_really_really_long_variable_name = 5;
int _hi =6;
int big = Integer.getInteger("7");
int $dollars = 8;
int %percent = 9;


How many will compile?


A
1
B
2
C
3
D
4
E
5

Reference
Close
JLS 3.0,

Option D is correct because the only illegal declaration is the one that starts with the %. The declaration of 'big' is legal because of autoboxing.

-------------------------------------------

Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.4 > Question 7
Your answer is correct.
Given the four declarations:


void doStuff(int[] intArgs...)
void doStuff(int... intArgs)
void doStuff(varargs.int intArgs)
void doStuff(int intArgs)


How many will accept a variable number of arguments?


A
0
B
1
C
2
D
3
E
4

Reference
Close
JLS 3.0,

Option B is correct because the only correct syntax is (int... intArgs).
------------------------------------------------


Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.4 > Question 8
Your answer is incorrect. The correct answer(s) are highlighted.
Given:


1. class Banana {
2. int x = 1;
3. public static void main(String [] args) {
4. int x = 2;
5. Banana b = new Banana();
6. b.go();
7. }
8. { x += x; }
9. void go() {
10. ++x;
11. System.out.println(x);
12. }
13. }


What is the result?


A
1
B
2
C
3
D
5
E
Compilation fails.

Reference
Close
JLS 3.0,

Option C is correct. The variable x on line 2 is not the same x as on line 4. The initialization block runs after the new Banana object is created.
------------------------------------------------------------------

Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.5 > Question 9
Your answer is incorrect. The correct answer(s) are highlighted.
Given:


1. class SuperFoo {
2. SuperFoo doStuff(int x) {
3. return new SuperFoo();
4. }
5. }
6.
7. class Foo extends SuperFoo {
8. // insert code here
9. }


And four declarations:


Foo doStuff(int x) { return new Foo(); }
Foo doStuff(int x) { return new SuperFoo(); }
SuperFoo doStuff(int x) { return new Foo(); }
SuperFoo doStuff(int y) { return new SuperFoo(); }


How many, inserted independently at line 8, will compile?


A
0
B
1
C
2
D
3
E
4


Reference
Close
JLS 3.0,

Option D is correct. Foo doStuff() cannot return a SuperFoo and co-variant returns are legal.
---------------------------------------------------------------

Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.5 > Question 10
Your answer is incorrect. The correct answer(s) are highlighted.
Given:


1. class FWD {
2. int doMud(int x) { return 1; }
3. }
4. class Subaru extends FWD {
5. int doMud(int... y) { return 2; }
6. int doMud(int z) { return 3; }
7. }
8. class Race {
9. public static void main(String [] args) {
10. int s = new Subaru().doMud(7);
11. System.out.println(s);
12. }
13. }


What is the result?


A
1
B
2
C
3
D
7
E
Compilation fails.
F
The output is NOT predictable.

Reference
Close
JLS 3.0,

Option C is correct. If the JVM has a choice, it will select a method without varargs before selecting a method with varargs.
-----------------------------------------------------------------------------

Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.6 > Question 11
Your answer is incorrect. The correct answer(s) are highlighted.
Given:


1. class Top {
2. static int x = 1;
3. public Top() { x *= 3; }
4. }
5. class Middle extends Top {
6. public Middle() { x += 1; }
7. public static void main(String [] args) {
8. Middle m = new Middle();
9. System.out.println(x);
10. }
11. }


What is the result?


A
1
B
2
C
3
D
4
E
6
F
Compilation fails.
Reference
Close
JLS 3.0,

Option D is correct. The Top constructor completes before the Middle constructor.
----------------------------------------------------------------------------------------------

Location: ... > Section 1: Declarations, Initialization and Scoping > Objective 1.6 > Question 12
Your answer is correct.
Given:


1. class Beverage {
2. Beverage() { System.out.print("beverage "); }
3. }
4. class Beer extends Beverage {
5. public static void main(String [] args) {
6. Beer b = new Beer(14);
7. }
8. public int Beer(int x) {
9. this();
10. System.out.print("beer1 ");
11. }
12. public Beer() { System.out.print("beer2 "); }
13. }


What is the result?


A
beer1 beverage
B
beer2 beverage
C
beverage beer1
D
beverage beer2 beer1
E
Compilation fails.
F
An exception is thrown at runtime.

Reference
Close
JLS 3.0,

Option E is correct. The declaration on line 8 looks like a constructor, but it is a method because it has a return type. Compilation fails because this(); on line 9, is allowed only in a constructor.

Comments

Popular posts from this blog

Request you ebooks here

Request any Ebooks u want, here......