Wednesday, July 16, 2008

SCJP 6.0 Imports and Static Exam Objectives Enums as well

Given the following class named TestCode, in a package named com.mcnz.tests, which of the provided options do NOT successfully reference the final variable named CFRB? (i.e, which of the following will not compile?)

package com.mcnz.tests;
public class TestCode {
public final long CFRB = 1010;
}


a)
package com.mcnz;
public class DetestableCode extends com.mcnz.tests.TestCode {
static{
DetestableCode dc = new DetestableCode();
System.out.println(dc.CFRB);
}
}

b)
package com.mcnz;
import com.mcnz.tests.*;
public class DetestedCode {
static{
System.out.println(TestCode.CFRB);
}
}


c)
package com.mcnz.tests;
public class TestedCode {
public void radio() {
{System.out.println(TestCode.CFRB);}
}
}


d)
package com.mcnz.tests;
public class TesterCode {
static {System.out.println(TestCode.CFRB);}
}

Only option a) will compile, making b) c) and d) the correct answer. This means b) c) and d) will not compile.

Constants in Java are typically decorated with the final and static keywords together. However, if the keyword static is missing from a final variable, the final variable must be referenced through an instance. Only variables decorated with the static keyword can be accessed by only using the class name, such as CFRB. Options b) c) and d) all use static notation to reference the non-static, final variable, making them all fail in compilation, and thus making them the correct answers to this quiz question.


###########################################

Given the following class named TestCode, in a package named com.mcnz.tests, which of the provided options will successfully reference the final variable named CFRB?

package com.mcnz.tests;
public class TestCode {
public static final long CFRB = 1010;
}


a)

package com.mcnz;
import com.mcnz.tests.TestCode.*;
public class DetestedCode {
static{
System.out.println(CFRB);
}
}
b)
package com.mcnz;

public class DetestableCode extends com.mcnz.tests.TestCode {
static{
System.out.println(CFRB);
}
}

c)
package com.mcnz.tests;
import com.mcnz.tests.TestCode;
public class TestedCode {
public void radio() {
{System.out.println(CFRB);}
}
}


d)
package com.mcnz.tests;
import static com.mcnz.tests.TestCode.*;
public class TesterCode {
static {System.out.println(CFRB);}
}


Options b) and d) will successfully compile.

Option a) appears to be attempting to perform a static import, but the static keyword is missing after the import keyword, and as such, this option will not compile, with a compile error indicating that CFRB cannot be resolved.

Option b) will indeed compile. Referencing the static final variable without the name of the class preceeding the name CFRB, along with the lack of a static import statement for com.mcnz.tests.TestCode.*, would lead one to believe that the code might not compile. However, a subclass always inherits all of the properties of the parent class, be it static, non-static or even final variables. As such, the DetestableCode class, which extends TestCode, can indeed reference the constant variable CFRB directly.

Option c) will also generate a message indicating the "CFRB cannot be resolved." Simply importing the class that contains the static final property is not enough - to directly reference the property named CFRB, TestCode would either need to properly perform a static import of the TestCode class, or use the actual name TestCode when referencing the CFRB variable, such as TestCode.CFRB.

Option d) uses the correct syntax for performing a static import, and as such, the static final property named CFRB can be referenced directly, by name only, without the name of the class preceeding the name of the property.

######################################

Which of the following enum declarations would compile?


a)
public enum JavaCerts {
SCJA, SCJP, SCWCD, SCBCD, SCMAD, SCDJWS, SCEA
}
b)
public enum JavaCerts {
SCJA, SCJP, SCWCD, SCBCD, SCMAD, SCDJWS, SCEA;
}
c)

public enum JavaCerts {
{SCJA, SCJP, SCWCD, SCBCD, SCMAD, SCDJWS, SCEA}
}

d)

public enum JavaCerts {
{SCJA, SCJP, SCWCD, SCBCD, SCMAD, SCDJWS, SCEA;}
}

Options a) and b) are correct.

Options c) and d) incorrectly places parenthesis around the elements of the enum, causing the compiler to choke up a message such as: "Unexpected end of file. Initializer expected before this token."

The elements of the enum must be declared, and the enum initialized, directly inside the body of the enum, and not within and sub-blocks. Options a) and b) both follow this pattern, making them both correct.


#############################################################################

If possible, hich of the following lines of code placed on //line 4 would allow the code to compile successfully?


package net.mcnz;
public class Goals {
public static void main(String args[]){
//line 4
System.out.println(myCert);
}
}

interface Certs{
enum SunCert {
SCJA, SCJP, SCWCD, SCBCD, SCMAD, SCDJWS, SCEA
}
}


a) SunCert myCert = new Certs.SunCert(SCJP);
b) Certs.SunCert myCert = new Certs.SunCert(SCJP);
c) Certs.SunCert myCert = Certs.SunCert.SCJP;
d) An enum cannot be defined within an interface, so nothing on line 4 would allow this code to compile.

Option c) is correct.

When referencing an enum, the new keyword simply does not come into play. As a result, a) and b) are incorrect.

An enum can indeed be defined within an interface, although it certainly seems strange. Nevertheless, it can be done, so d) is incorrect.

Option c) is correct. The enum is within an interface, so the syntax would be the name of the interface followed by the name of the enum when the type is declared, Certs.SunCert. When the enum is initialized, the syntax would be the name of the interface, the name of the enum and then the property of the enum, all referenced using dot notation, Certs.SunCert.SCJP.



#######################################################

Which line of code, placed within the brackets of the viewEar method call in the main method, would cause the literal String true to be printed to the console?

package net.mcnz;
public class Inner {
public static void main(String args[]){
Inner.viewEar(/* Code Goes here */);
}
public static void viewEar(Ear e) {
System.out.println(e.isWaxy());
}
}

interface Ear { public abstract boolean isWaxy(); }


a) implements Ear {public boolean isWaxy(){return true;}}
b) implements Ear() {public boolean isWaxy(){return true;}}
c) new Ear {public boolean isWaxy(){return true;}}
d) new Ear() {public boolean isWaxy(){return true;}}
e) new class Ear {public boolean isWaxy(){return true;}}
f) new class Ear() {public boolean isWaxy(){return true;}}

Option d) is correct.

To implement an inner class that provides an implementation for an interface, the new keyword preceeds the name of the interface, with the name of the interface having open and closed brackets following it, as though we were calling a constructor for a class, new Ear(). From there, parenthesis surround the normal, concrete implementation of any of the abstract methods defined in the interface.

new Ear() {public boolean isWaxy(){return true;}}

When this inner class implementation of the Ear interface is provided, the program returns the literal String true to the console.


Java is an object oriented programming language. So, when we work in Java, we work with objects.

In the line of code:

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

we see a special object called the System.

Now, objects are made up of all sorts of fabulous things, including properties, methods, and sometimes, other objects. In the case of the System, it contains a special object that can print out to the console, and quite appropriately, this object that can print to the console is named out.

Furthermore, the object named out has a method called print() and another method called println, to which we can pass text and have that text printed out to the console.

So, System is an object, out is an object contained by the system, and print or println are methods that can be called on the System's object named out.

Now, when we refer to objects, and then any objects they may have, and then any methods or properties that we are allowed to call, we use a dot (.) notation. So, when we call the System object, ask for the object named out, and then call the print method on the out object, the code looks like this:

System.out.print("Hello World");

No comments: