Classes/Objects  «Prev  Next»


Anonymous Class - Quiz

Each question is worth one point. Select the best answer or answers for each question.
 
1. Under which circumstances is it appropriate to declare an anonymous class?
Please select all the correct answers.
  A. To implement an event listener interface
  B. To extend an event adapter class
  C. To define a small interface
  D. To define a set of constants

2. What is wrong with the following program?

class Outer{
 public static void main(String[] args){
  new Inner(); 
 } 
 class Inner { 
  Inner() {} 
 }  
}
Please select the best answer.
  A. Inner should extend Outer.
  B. Inner should be declared before main().
  C. No enclosing instance of Outer is created before Inner's constructor is invoked.
  D. Outer should be declared public.

3. What is wrong with the following program?
static class Outer{
 public static void main(String[] args){
 //
 }
 private class Inner{
  Inner() {} 
 }
}

Please select the best answer.
  A. Inner should be declared static.
  B. A non-inner class cannot be declared static.
  C. Inner should not be private.
  D. Outer should be public.

4. Which of the following are characteristics of local inner classes?
Please select all the correct answers.
  A. They may access final local variables and parameters of the enclosing method which have been assigned a value.
  B. They must be declared private.
  C. They must be declared public to be accessible outside of their enclosing method.
  D. They may not be accessed outside of the code block in which they are declared.

5. Which of the following lines of output are displayed by this program?
class Outer {
 String s1 = "Java";
 String s2 = "2";

 public static void main(String[] args) { 
  Outer outer = new Outer();  
 }
 Outer() { 
  Inner inner = new Inner();  
 }
 class Inner {
  String s1 = "Certification";
  String s2 = "Exam";
        
  Inner() {
   System.out.println(Outer.this.s1);
   System.out.println(this.s1);
   System.out.println(s2);
  }
 }
}

Please select all the correct answers.
  A. Java
  B. 2
  C. Certification
  D. Exam

6. Which of the following are true about the Outer program?
class OuterInnerStatic {
 static String s1 = "Java"; 
 static String s2 = "2";
 public static void main(String[] args) { 
  Inner inner = new Inner();  
}

 static class Inner {
  String s1 = "Certification";
  String s2 = "Exam";
  Inner() {
   System.out.println(OuterInnerStatic.s1);
   System.out.println(this.s1);
   System.out.println(s2);
  }
 }
}

Please select all the correct answers.
  A. It leads to a compilation error because Outer.s1 should be referenced as Outer.this.s1.
  B. It leads to a compilation error because Inner is created before an instance of its enclosing scope.
  C. It compiles and executes to display the lines:
Java
Certification
Exam
  D. Since the Inner class is static, it does not require an instance of Outer to be created.

7. Which of the following are characteristics of interfaces?
Please select all the correct answers.
  A. All method declarations are implicitly public and abstract.
  B. All constant declarations are implicitly public, static, and final.
  C. Methods may not be declared as native, final, static, or synchronized.
  D. All interfaces extend Object.

8. Which of the following are characteristics of constructors?
Please select the best answer.
  A. They must be declared public.
  B. They may be inherited from a superclass.
  C. They may not throw exceptions.
  D. They have the same name as the class in which they are declared.

9. Which are true about subclasses and superclasses?
Please select all the correct answers.
  A. A class is considered to be a subclass of itself.
  B. A class extends its superclass.
  C. All classes are subclasses of Object.
  D. A class may extend more than one interface.

10. Which of the following are characteristics of final classes?
Please select the best answer.
  A. A final class may only be used within its package.
  B. A final class is static.
  C. A final class is abstract.
  D. A final class may not be extended.