Fill in the Blanks

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.
creation: 8/13/99; revised 03/06/00, 07/04/02

Method Parameters: 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.


1.      The following program is to print the number of characters in the string:

class HowLong
{
  public static void main ( String[] args )
  {
    String str = "Count Carefully!" ;
    int len =  .  ;
    System.out.println( "Number of characters: " + len );
  }
}


2.      Remember that the + operator takes two string objects and returns a new object that contains the concatenated characters of the first two objects. In other words, the first two objects are not changed, and a third object is created.

The following program is to concatenate the two named strings and print out the length of the result:

class HowLongTwo
{
  public static void main ( String[] args )
  {
    String str1 = "New strings " ;
    String str2 = "for old." ;

    int len = (  +  ) .  ;
    System.out.println( "Number of characters: " + len );
  }
}

The point of this exercise is to illustrate that temporary, unnamed objects have the same methods as named objects.



3.      Another way to create a string object that contains the concatenated characters of two other strings is by using the concat(String st) method. This method takes a parameter, which is the second string, whose characters are to be joined to the string whose method is running.

The following program is to do the same thing as the previous program:

class SameAgain
{
  public static void main ( String[] args )
  {
    String str1 = "New strings " ;
    String str2 = "for old." ;

    String str3 = str1. (  )  ;
    System.out.println( "Number of characters: " + str3. );
  }
}


4.      The following program draws a line on a graphics area between pointA and pointB. (You have not seen several of the features of this program before, but you can ignore them and look only at the Point objects.) Fill in the blanks so that the program next draws a line parallel to the first line, but moved 10 positions to the right.

class DrawLines extends Applet
{
  public void paint ( Graphics graph )
  {
    Point pointA = new Point( 10, 20 );
    Point pointB = new Point( 30, 70 );
    graph.drawLine( pointA.x, pointA.y, pointB.x, pointB.y );

    pointA.move( , 20 );
    pointB.move( , 70 );

    graph.drawLine( pointA.x, pointA.y, pointB.x, pointB.y );

  }
}


5.      In general, a conversion between two data types is not automatically performed if there is the risk of a of information.



6.      In each of the following situations, the compiler will automatically convert from the type of the actual parameter in the parameter list to the type of parameter that the method requires:

  • Convert from an integer type to another integer type with bits.
  • Convert from a floating point type to another floating point type with bits.
  • Convert from an integer type to a floating point type with number of bits (although this may result in loss of precision.)
  • Convert from an integer type to a floating point type with bits (this will not result in loss of precision.)


7.      In each of the following situations, the compiler will NOT automatically convert from the type of the actual parameter in the parameter list to the type of parameter that the method requires:

  • Convert from an integer type to another integer type with bits.
  • Convert from a floating point type to another floating point type with bits.
  • Convert from a floating point type to an integer type with number of bits.


8.      A is used to ask the compiler to perform a conversion that it would not automatically do.



9.      The programmer knows that the following program will work correctly. Fill in the blank to inform the compiler that it is OK to perform the conversion. Recall that most Math methods require double arguments.

class SinEg
{
  public static void main ( String[] args )
  {
    float angle = 2.56;

    System.out.println( "Sine of angle is " + Math.sin( () angle ) );
  }
}


10.      In the following situation, what can be said of the values of X and Y after the doSomething() method has returned?

. . .
int X = 41;
int Y = -12;
. . .
someObject.doSomething( X, Y );
. . .

The values of X and Y will be as before the method call, no matter what object and method are involved.



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.