creation: 8/13/99

Fill in the Blanks

Instructions:   This is an ungraded fill-in-the-blank exercise. Each question consists of a sentence with one or two words left out. A button represents the missing word(s). For each question, think of the word or phrase that should fill each blank, then click on the buttons to see if you are correct. No grade is calculated for this exercise.


This exercise reviews the "scope" of variables and parameters. The scope of a variable or formal parameter is the section of code that can "see" (can use) the parameter.

The scope of an instance variable includes each method body (list of statements) and each constructor body.

1.       In the following program skeleton, click on each button where it would be OK to have the statement:

target = 25

In other words, click on each button where the variable target is in scope.

class ScopeEg1
{
  int target;

  ScopeEg1()
  {
     ; 
    . . . .
  }

  void aMethod()
  {
     ;
      . . . .
  }

  void bMethod()
  {
     ;
       . . . .
  }

}

class AnotherClass
{
  int sum;
  AnotherClass()
  {
     ; 
    . . . .
  }

  void anotherMethod()
  {
     ;
      . . . .
  }

  void someMethod()
  {
     ;
       . . . .
  }
}

class TesterClass
{
  public static void main (String[] args )
  {
     ;
  }

}


"Outsiders" can access instance variables of an object using "dot notation" unless the instance variable is private (or has default access and is in a different package...but ignore this for now.)

2.       In the following program skeleton, click on each button where it would be OK to have the statement: target = 25

class ScopeEg2
{
  int target;

  . . . .

}

class AnotherClass
{
  int sum;
  ScopeEg2 first = new ScopeEg2() ;

  void anotherMethod()
  {
    first .  ; 

    . . . .    
  }

  void someMethod()
  {
    first .  ; 

    . . . .    
  }
}

class TesterClass
{
  public static void main (String[] args )
  {
    first .  ;
  }

}


3.       In the following program skeleton, click on each button where it would be OK to have the statement target = 25

class ScopeEg3
{
  private int target;

  ScopeEg3()
  {
     ; 
    . . . .
  }

  void aMethod()
  {
     ;
      . . . .
  }

  void bMethod()
  {
     ;
       . . . .
  }
  . . . .

}

class AnotherClass
{
  int sum;
  ScopeEg3 first = new ScopeEg3() ;

  void anotherMethod()
  {
    first .  ; 

    . . . .    
  }

  void someMethod()
  {
    first .  ; 

    . . . .    
  }
}

class TesterClass
{
  public static void main (String[] args )
  {
    ScopeEg3 second = new ScopeEg3() ;

    second .  ;
  }

}


Formal parameters can only be seen by the body of their own method.

4.       In the following program skeleton, click on each button where it would be OK to have the statement System.out.println( data );

class SomeClass
{
  int sum;

  void aMethod( int data )
  {
     ; 

    . . . .    
  }

  void bMethod()
  {
     ; 

    . . . .    
  }
}

class TesterClass
{
  SomeClass some;

  public static void main (String[] args )
  {
    some = new SomeClass();
    some.aMethod( 99 );

     ;
    
  }

}


It is OK for formal parameters in two different methods to use the same identifier.

5.       In the following program skeleton, click on each button where it would be OK to have the statement sum = data ;

class SomeClass
{
  int sum;

  void aMethod( int data )
  {
     ; 

    . . . .    
  }

  void bMethod( int data )
  {
     ; 

    . . . .    
  }

  void cMethod( int value )
  {
     ; 

    . . . .    
  }

}


A local variable can only be seen in the body of its method by statements following its declaration. It is OK for local variables in different methods to use the same name.

6.       In the following program skeleton, click on each button where it would be OK to have the statement value = 5;

class SomeOtherClass
{
  int sum;

  void aMethod( int data )
  {
    int value;

     ; 

    . . . .    
  }

  void bMethod( int data )
  {
     ; 

    . . . .    
  }

  void cMethod( )
  {
    . . . .    

     ; 

    int value;

    . . . .    
  }


  void dMethod( )
  {
    double value;

     ; 

    . . . .    
  }
}


If a local variable has the same name as an instance variable the local variable will be the one seen by the statements in its method that follow its declaration. (Although it is correct syntax to have both local and instance variables use the same name, it is probably a bad idea since it confuses humans.)

7.       In the following program skeleton, decide if each statements sets the instance variable sum or the local variable sum.

class YetOtherClass
{
  int sum;

  void aMethod( int data )
  {

    sum = data ;  ; 

    . . . .    
  }

  void bMethod( int data )
  {
    int sum;

    sum = data ;  ; 

    . . . .    
  }

  void cMethod( )
  {
    . . . .    

    sum = 23 ;  ; 

    int sum;

    . . . .    
  }

}


If a local variable has the same name as an instance variable and you want to specify the instance variable, use this.

8.       In the following program skeleton, decide if each statements sets the instance variable sum or the local variable sum.

class AfurtherClass
{
  int sum;

  void aMethod( int data )
  {
    int sum;

    this.sum = data ;  ; 

    . . . .    
  }

  void bMethod( int data )
  {
    int sum;

    sum = data ;  ; 

    . . . .    
  }

}


If a paramenter has the same name as an instance variable and you want to specify the instance variable, use this. This is often done with constructors, where it is probably less confusing to use the same name for both.

9.       In the following program skeleton, decide if each statements sets the instance variable sum or the parameter sum.

class AfurtherClass
{
  int sum;

  AfurtherClass( int sum )  // constructor
  {
    this.sum = sum ;  ; 

    . . . .    
  }

  void bMethod( int sum )
  {

    sum = 32 ;  ; 

    . . . .    
  }

}


An "outsider" can change a private instance variable of an object by using an access method of the object (if there is one.)

10.       In the following program skeleton, click on those buttons next to statements that change sum.

class SimpleClass
{
  private int sum;

  SimpleClass( int s )
  {
    sum = s ;  ; 

    . . . .    
  }

  void setSum ( int s )
  {

    sum = s ;  ; 

    . . . .    
  }

}

class TesterClass
{
  public static void main ( String[] args )
  {
    SimpleClass sim = SimpleClass( 34 );   ; 

    sim.sum = 77 ;   ; 

    sim.setSum( 14 ) ;   ; 
  }
}


End of the Exercise. If you want to do it again, click on "Refresh" in your browser window. Click here to go back to the main menu.