created: 8/13/1999; small edits 4/24/2006, 07/04/2011, 08/03/2014

Fill in the Blanks

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 correct to have the statement: target = 25

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

class ScopeEg1
{
  public int target;

  public ScopeEg1()
  {
     ; 
    . . . .
  }

  public void aMethod()
  {
     ;
      . . . .
  }

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

}

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

  public void anotherMethod()
  {
     ;
      . . . .
  }

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

public 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
{
  public int target;

  . . . .

}

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

  public void anotherMethod()
  {
    first .  ; 

    . . . .    
  }

  public void someMethod()
  {
    first .  ; 

    . . . .    
  }
}

public 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;

  public ScopeEg3()
  {
     ; 
    . . . .
  }

  public void aMethod()
  {
     ;
      . . . .
  }

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

}

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

  public void anotherMethod()
  {
    first .  ; 

    . . . .    
  }

  public void someMethod()
  {
    first .  ; 

    . . . .    
  }
}

public 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.   Click on each button where it would be OK to have the statement System.out.println( data );

class SomeClass
{
  public int sum;

  public void aMethod( int data )
  {
     ; 

    . . . .    
  }

  public void bMethod()
  {
     ; 

    . . . .    
  }
}

public 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
{
  public int sum;

  public void aMethod( int data )
  {
     ; 

    . . . .    
  }

  public void bMethod( int data )
  {
     ; 

    . . . .    
  }

  public 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.   Click on each button where it would be OK to have the statement value = 5;

class SomeOtherClass
{
  public int sum;

  public void aMethod( int data )
  {
    int value;

     ; 

    . . . .    
  }

  public void bMethod( int data )
  {
     ; 

    . . . .    
  }

  public void cMethod( )
  {
    . . . .    

     ; 

    int value;

    . . . .    
  }


  public 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.   Decide if each statements sets the instance variable sum or the local variable sum.

class YetOtherClass
{
  public int sum;  // the instance variable

  public void aMethod( int data )
  {

    sum = data ;  ; 

    . . . .    
  }

  public void bMethod( int data )
  {
    int sum;  // a local variable

    sum = data ;  ; 

    . . . .    
  }

  public 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
{
  public int sum;  // the instance variable

  public void aMethod( int data )
  {
    int sum;  // a local variable

    this.sum = data ;  ; 

    . . . .    
  }

  public void bMethod( int data )
  {
    int sum;  // a local variable

    sum = data ;  ; 

    . . . .    
  }

}


If a parameter 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.   Decide if each statement sets the instance variable sum or the parameter sum.

class AfurtherClass
{
  public int sum;  // instance variable

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

    . . . .    
  }

  public 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;

  public SimpleClass( int s )
  {
    sum = s ;  ; 

    . . . .    
  }

  public 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.

go to home page