(Quiz) SCJP (Sun Certified Java Program) Inner Classes
QUIZ: SCJP (Sun Certified Java Program) Inner Classes
Questions 1 What will happen when you attempt to compile and run this program?
public class Outer {
public String name = "Outer";
public static void main(String argv[]) {
Inner i = new Inner();
i.showName();
}//End of main
private class Inner {
String name =new String("Inner");
void showName() {
System.out.println(name);
}
}//End of Inner class
}
(a) Compile and run with output of "Outer".
(b) Compile and run with output of "Inner".
(c) Compile time error because Inner is declared as private.
(d) Compile time error because of the line creating the instance of Inner.
Answers:1 (d)
This looks like a question about inner classes but it is also a reference to the fact that the main method is static and thus you cannot directly access a non static method. The line causing the error could be fixed by changing it to say
Inner i = new Outer().new Inner();
Then the code would compile and run producing the output "Inner"
Questions 2 Which of the following statements are true?
(a) An inner class may be defined as static.
(b) There are NO circumstances where an inner class may be defined as private.
(c) An anonymous class may have only one constructor.
(d) An inner class may extend another class.
Questions 2 (a), (d)
A static inner class is also sometimes known as a top level nested class. There is some debate if such a class should be called an inner class. I tend to think it should be on the basis that it is created inside the opening braces of another class. How could an anonymous class have a constructor?. Remember a constructor is a method with no return type and the same name as the class. Inner classes may be defined as private
Questions 3 Given the following class definition, which of the following statements would be legal after the comment //Here? [7]
class InOut{
String s= new String("Between");
public void amethod(final int iArgs) {
int iam;
class Bicycle{
public void sayHello(){
//Here
}//End of bicycle class
}
}//End of amethod
public void another(){
int iOther;
}
}
(a) System.out.println(s);
(b) System.out.println(iOther);
(c) System.out.println(iam);
(d) System.out.println(iArgs);
Questions 3 (a), (d)
A class within a method can only see final variables of the enclosing method. However it the normal visibility rules apply for variables outside the enclosing method.
Questions 4 Which of the following statements are true?
(a) Adding more classes via import statements will cause a performance overhead, only import classes you actually use.
(b) Under no circumstances can a class be defined with the private modifier.
(c) A inner class may under some circumstances be defined with the protected modifier.
(d) An interface cannot be instantiated.
Questions 4 (c), (d)
The import statement allows you to use a class directly instead of fully qualifying it with the full package name, adding more classess with the import statement does not cause a runtime performance
overhad. I assure you this is true. An inner class can be defined with the private modifier.
Questions 5 The following is an outline of code for top-level class. Assume that both classes have correct constructors taking no parameters.
class NormalClass {
static class NestedClass {
// methods and variables of NestedClass
}
// methods and variables of NestedClass
}
Which of the following code fragments shows the correct way to declare and initialize a reference to a NestedClass object?
(a) NormalClass.NestedClass myNC = new NormalClass.NestedClass();
(b) NestedClass myNC = new NormalClass().new NestedClass();
(c) NestedClass myNC = new NormalClass.NestedClass();
Questions 5 (a)
(a) shows both correct reference variable declaration and a correct constructor. (b), (c) are incorrect declaration of the reference variable because the name of a nested class starts with the enclosing class name.
Questions 6 The following is an outline of code for top-level class. Assume that both classes have correct constructors taking no parameters.
class BaseClass {
static class NestedClass {
// methods and variables of NestedClass
}
// methods and variables of BaseClass
}
Which of the following code fragments shows the correct way to declare and initialize a reference to a NestedClass object?
(a) BaseClass.NestedClass myNC = new BaseClass.NestedClass();
(b) NestedClass myNC = new BaseClass().new NestedClass();
(c) NestedClass myNC = new BaseClass.NestedClass();
(d) BaseClass.NestedClass nbc = new BaseClass().new NestedClass();
Questions 6 (d)
(d) shows both correct reference variable declaration and a correct constructor. This statement creates a BaseClass object, which the NestedClass must have. (b), (c) are incorrect declaration of the reference variable because the name of a nested class starts with the enclosing class name. Answer (a) does not create a BaseClass object for the NestedClass to be associated with.
Questions 7 Which of the following statements about this code are true?
public class Morecombe {
public static void main(String argv[]){
Morecombe m = new Morecombe();
m.go(new Turing(){});
}
public void go(Turing t){
t.start();
}
}
class Turing extends Thread {
public void run(){
for(int i =0; i < 2; i++){
System.out.println(i);
}
}
}
(a) Compilation error due to malformed parameter to go method.
(b) Compilation error, class Turing has no start method.
(c) Compilation and output of 0 followed by 1.
(d) Compilation but runtime error.
Questions 7 (c)
The creation of an anonymous class as a parameter to go is fairly strange as you would expect it to override a method in its parent class (Turing). You don't have to though. The fact that class Turing extends Thread means the anonymous instance that is passed to go has a start method which then calls the run method.
- guru's blog
- Login to post comments
