Loyola College in Maryland

CS 201 - Computer Science I
Spring 2009


Loyola College > Department of Computer Science > Dr. James Glenn > CS 201 > Homework > Quiz #2 Practice Problems > Solutions

Problem 0: Review old homeworks and labs, including vocabulary from previous chapters.


Problem 1: Review the following vocabulary and, where appropriate, the corresponding Java syntax.


Problem 2: Show what is displayed by each System.out.println statement in the following code fragment.

String s = new String("This quiz is fun!");

char letter = s.charAt(2);
System.out.println(letter);           // i

int index1 = s.indexOf('s');
int index2 = s.indexOf('s', 6);
System.out.println(index1);           // 3
System.out.println(index2);           // 11

String sub1 = s.substring(1, 3);
System.out.println(sub1);             // hi

String sub2 = s.substring(index2 + 2);
System.out.println(sub2);             // fun!

Problem 3: In the program below, circle one example of each of the following. Write the corresponding part letter next to each circle.

  1. an instantiation
  2. a reference
  3. an method name
  4. the invocation of a class (static) method
  5. the invocation of an instance (non-static) method in the String class
  6. the invocation of an instance (non-static) method in another class
  7. an argument
import java.util.Scanner;

public class UsernameGenerator
{
    public static void main(String[] args)
    {
        final int LONGEST_USERNAME = 10;

        Scanner scan = new Scanner(System.in);                        // (a)

        System.out.println("Please enter a name (first last):");
        String fullName = scan.nextLine();                            // (f)

        char firstInitial = fullName.charAt(0);                       // (c)

        int lastIndex = fullName.indexOf(' ') + 1;
        String lastName = fullName.substring(lastIndex);              // (b)

        String username = (String.valueOf(firstInitial) + lastName);  // (d)

        username = username.toLowerCase();                            // (e)

        int finalLength = Math.min(LONGEST_USERNAME, username.length());
        username = username.substring(0, finalLength);                // (g)

        System.out.println(username);
    }
}

Problem 4: Suppose the documentation for the Team and Game classes describe constructors and methods as shown below. In the given program, determine which statements are legal and which are illegal. Consider only whether the statements adhere to Java's rules; it is possible for non-sensical or useless statements to be legal.

Team Game
Team(String name)
String getName()
void addLoss()
void addWin()
Game(Team home, Team away)
Game(Team home, Team away, int hSco, int aSco)
Team getWinner()
int getTotalPoints()
void setScore(Team t, int newScore)
public class Problem4
{
  public static void main(String[] args)
  {
    Team t1;
    new Team("Virginia") = t1;                // illegal: wrong direction for assignment
    t1 = new Team("My applet is 400x400");    // legal, but odd: we now have a team called "My applet is 400x400"
   
    Team t2;
    t2 = t1;

    Game g;
    g = t1;                                   // illegal: the types must be compatible across an assignment; Team cannot be assigned to Game
    g = new Game(t1, t2, 0, 1);
    g = new Game("Loyola (MD)", "Loyola (IL)", 72, 64); // illegal: the first two arguments must be Teams, not Strings
    g = new Game(t1, new Team("Maryland"));

    t1.Team("Virginia");                      // illegal: constructors are invoked with new, not .
    t1.getWinner();                           // illegal: getWinner works with Games, not Teams   

    int i;
    i = g.getTotalPoints(t1);                 // illegal: getTotalPoints takes no arguments
    i = t2.addLoss();                         // illegal: addLoss does not return an int

    t1.getName();                             // legal, but probably useless: we're ignoring the returned value

    g.getWinner().addLoss();                  // legal, but probably odd
    g.setScore(100, t1);                      // illegal: arguments are in the wrong order
}

Valid HTML 4.01 Transitional