University of Toronto
Scarborough College
CSC A06F: Introduction to Computer Programming
Section L01
Fall 1997

Midterm examination SOLUTIONS
Question 1 [10 marks]

Choose five of the seven terms and symbols in the following Java declaration, and explain what each of them means.

   public static void main (String[] args) {

a. public
indicates this method can be called from outside the class.

b. static
indicates that no object has to be instantiated in order to call this method. It is a `class' method.

c. void
indicates that the method does not return a value.

d. main
is the name of the method. (The main method.)

e. String[]
indicates the 1st parameter is an array of strings.

f. args
is the name of the 1st parameter.

g. `{'
is punctuation to open the block for the method.

Question 2 [8 marks]

Consider the following Java program.

   import java.io.*;
   public class Unknown {
      public static void main (String[] args) throws IOException {

         // read in three numbers
         DataInputStream stdin = new DataInputStream (System.in);
         int num1 = Integer.parseInt (stdin.readLine());
         int num2 = Integer.parseInt (stdin.readLine());
         int num3 = Integer.parseInt (stdin.readLine());
      
         if (num1 < num2) {
            if (num1 < num3)
               System.out.println (num1);
            else
               System.out.println (num3);
         }
         else {
            if (num2 < num3) 
               System.out.println (num2);
            else
               System.out.println (num3);
         }
      }
   }

a. [2] What does this program output when given the numbers 3, 2, 1, in that order, as input?

1

b. [2] What does this program output when given the numbers 10, 20, 10, in that order, as input?

10

c. [2] What does this program output when given the numbers -10, 0, 10, in that order, as input?

-10

d. [2] What is the purpose of this program?

The purpose is to print the smallest of the three numbers.

Question 3 [6 marks]

Consider the following fragment of code, whose purpose is to calculate tex2html_wrap_inline126 , where N is a non-negative integer. Remember that tex2html_wrap_inline140 .

  0   // assume that N >= 0
  1   int exp = 1;
  2   int i = 1;
  3   while (i < N) {
  4      exp = exp * 2;
  5      i++;
  6   }

a. [1] This code has a bug, i.e., it does not do what it is supposed to do. What line has the bug?

line 2 or line 3

b. [2] Briefly explain what is wrong.

The loop doesn't go far enough; e.g., for tex2html_wrap_inline126 it really calculates tex2html_wrap_inline128 .

c. [3] Write a correct version of the line that has the bug.

For line 2: int i = 0;
OR line 3: while (i <= N) {

Question 4 [4 marks]

Assume the following 2 lines of Java code have been executed.

   String a = new String ("Java is neat!");
   String b = new String ("Java is neat!");

a. [2] Explain why the condition `(a == b)' is false.

This is false because it is comparing the references to the objects. Although the objects have the same value, they are different objects, so their references are different.

b. [2] Explain why the condition `(a.equals(b))' is true.

This is true because the `equals' method on Strings compares the values of the Strings. They are the same.

Question 5 [10 marks]

Write a Java method that takes 2 arrays of integers as parameters and returns a third array in which each element is the sum of the corresponding elements in the 2 arrays. You can assume that the arrays are equal in length, and that they have been initialized appropriately. (Remember that if a is a reference to an array, then a.length is the length of the array.)

For example, given the arrays (3,5,2,5) and (7,6,5,4), the method should return the array (10,11,7,9).

int[] add (int[] a, int[] b) {
   int[] temp = new int[a.length];
   for (int i=0; i<a.length; i++) {
      temp[i] = a[i] + b[i];
   }
   return temp;
}

Question 6 [10 marks]

Write down the result of evaluating each of the following expressions.

a. [2] (8 - 3) * 4 + 2

Result: 22

b. [2] "Only " + 62 + "days until Christmas!"

Result: "Only 62days until Christmas!"

c. [2] (int) 279.99

Result: 279

d. [2] a = 5 + 5

Result: 10

e. [2] 3 + 3 < 3 * 3 && 2 + 2 == 2 * 2

Result: true

Question 7 [10 marks]

a. [6] Write a Java class called BankAccount that stores the current balance of the account as an instance variable and that has two methods to debit (subtract) and credit (add) an amount to the current balance. Also define a constructor for the account that takes the initial balance of the account as its only parameter. Use the appropriate visibility modifiers (i.e., public and private) on the instance variable and methods.

  class BankAccount {

     private float balance;

     public BankAccount (float bal) {
        balance = bal;
     }

     public void debit (float amt) {
        balance -= amt;
     }

     public void crebit (float amt) {
        balance += amt;
     }
  }

b. [4] Write a main method that instantiates two separate bank accounts. It should initialize them to contain $100 each. Then write statements that transfer $50 from the first account to the second account.

  public class Bank {
     public static void main (String[] args) {

        BankAccount a1 = new BankAccount(100.0);
        BankAccount a2 = new BankAccount(100.0);
        a1.debit (50.0);
        a2.credit (50.0);
     }
  }





Philip Edmonds
Tue Oct 28 18:04:12 EST 1997