Shared Flashcard Set

Details

SCJP 6 Chapter 8
Flashcards for the Sierra/Bates SCJP 6 exam prep book. Chapter 8 - Inner Classes
23
Software
Professional
04/17/2011

Additional Software Flashcards

 


 

Cards

Term
Where can a method-local inner class be instantiated?
Definition
Only with the method where the inner class is defined. It cannot be instantiated in an other method.
(p. 671)
Term
T/F: Method-local inner class objects can access the enclosing (outer) class's private (or any other) members
Definition
T
(p. 671)
Term
T/F: Method-local inner class objects can access the local variables of the method it is in.
Definition
F - local variables only exist for the lifetime of the method, but a method-local inner class object might exist after the method completes.
(p. 671)
Term
T/F: Method-local inner class objects can access the local variables marked final of the method it is in.
Definition
T
(p. 671)
Term
Which of the following modifiers are acceptable for method-local inner classes:
a) public
b) private
c) protected
d) static
e) abstract
f) transient
g) final
Definition
Only e and g, and never at the same time.
(p. 672)
Term
What members, if any, of the enclosing class does a method-local inner class, declared in a static method, have access to?
Definition
Only the static members of the outer class (no access to the instance variables)
(p. 672)
Term
An outter class instance cannot be referenced from within an inner class.
True or False?
Definition
False
Term
An Outter Class instance from within the inner class code, use _____.______.

Complete the two blanks.
Definition
Outter Class Name
this.

ex. outerclassname.this
Term
An inner class is a member of the outer class just as instance variables and methods are. (True or False?
Definition
True.
Term
An inner class can be protected. True or False?
Definition
True
Term
To use an inner class you must make an instance of it within the method but below the inner classes definitions.
True or False?
Definition
True
Term

Which of the following statements will compile?

 

public class TestClass {


public static void main(String[] args) {


final int x = Integer.parseInt("techflow");

Runnable r =

 

new Runnable ();                      //1

Runnable runTechflow =

 

new Thread(){};    //2

Runnable run =

 

new Runnable() {                 //3

   public void run(){}

};

Runnable running = 

 

new Runnable() {         //4

  private void runit(){}

  public void run(){}

};

}

 

Definition

All of them except line 1, because you can't instantiate an interface.   pg 678

Term

class Orange {
  public void bite() {
    System.out.println("yum");
  }
}

class Fruit {
  Orange o = new Orange() { //1
    public void bite() {  //2
      System.out.println("anonymous yum"); //3
    } //4
  }//5
}

 

Choose all statements that are correct:
a)  Line 1: is a legal declaration to an instance of an anonymous subclass
b)  Line 2: cannot override bite() method.
c)  Line 4: needs a semi-colon after the curly brace
d)  Line 5: needs a semi-colon after the curly brace
e)  None of the above

Definition
a, d (pg 674)
Term

What happens when the following is run:
A. Prints nothing
B. Prints "foofy"
C. Compilation error
D. Exception

 

class MyWonderfulClass {
  void go() {
    Bar b = new Bar();
    b.doStuff(new Foo() {
    public void foof() {
       System.out.println("foofy");
    }
   })
  }
}

 

interface Foo {
  void foof();
}
class Bar {
  void doStuff(Foo f) { }
}

Definition

C (p 679, 680) Argument-defined anonymous inner classes end with "});"

Term
T/F: A static nested class is simply a class that's a static member of the enclosing class, since there is no such thing as a static class.
Definition
T (p. 681) The static modifier says that the nested class is a static member of the outer class. That means it can be accessed, as with other static members, without having an instance of the outer class.
Term

Given the following Static Nested Class:

 

 class BigOuter {
   static class Nest {}
 }

 

What is the syntax for instantiating the static nested class:
A. BigOuter.Nest n = new BigOuter().new Nest();
B. BigOuter.Nest n = BigOuter.Nest;
C. BigOuter.Nest n = new BigOuter.Nest();
D. BigOuter.Nest n = new BigOuter.Nest;

Definition

 C (p. 681). Choice A would be the answer if Nest was a non-static inner class.

Term
T/F: a static nested class does not have access to the instance
variables and nonstatic methods of the outer class.
Definition
True (p. 681). This also includes the MyOuter.this reference.
Term
What would be the output of new Food().popIt()?
a) prints "popcorn" and "anonymous sizzling popcorn"
b) prints "anonymous popcorn" and "anonymous sizzling popcorn"
c) compilation error

class Popcorn {
   public void pop() {
      System.out.println("popcorn");
   }
}
class Food {
   Popcorn p = new Popcorn() {
      public void sizzle() {
        System.out.println("anonymous sizzling popcorn");
      }
      public void pop() {
         System.out.println("anonymous popcorn");
      }
   };
   public void popIt() {
      p.pop();
      p.sizzle();
   }
}
Definition

c. Not legal to call sizzle() on Popcorn.

 

(p. 676)

Term
What is the output?
a) prints nothing
b) prints "anonymous popcorn"
c) compilation error

public void main(String[] args)
{
	Popcorn p = new Popcorn() {
		public void pop() {
			System.out.println("anonymous popcorn");
		}
	}
}
Definition

c. Missing semi-colon.

 

(p. 675)

Term
Given:
class MyOuter {
class MyInner{ }
}

When compiled, what are the resulting class file names?
Definition
MyOuter.class
MyOuter$MyInner.class

pg 665
Term
What are the two ways to define a thread?
When would you use each method?
Definition
Extend java.lang.Thread class
Implement Runnable interface

Extend Thread only if you are creating more specific Thread based behaviour

Implement Runnable in most cases if you just need a thread.

pg 705.
Term
Which of these are valid Thread constructors?
a. Thread()
b. Thread(Object target)
c. Thread(Runnable target)
d. Thread(String name, Runnable target)
e. Thread(String name)
Definition
a,c,e

d is not valid because the argument order is reversed. Thread(Runnable target, String name) would be valid.

pg 708
Term
Is this legal?

class MyRunnable implements Runnable {
public void run() {
System.out.println("Im running!");
}
}
public class TestThread {
public static void main(String [] args) {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
r.start();
}
}
Definition
No, the start method is called on the Thread object not the Runnable.

pg 709.
Supporting users have an ad free experience!