CMPUT 102: Structural Programming and Data Structures
Term Exam 1: Monday February 14th, 2000
Instructor : Osmar Zaïane
Section : B1
Version : A

The answers are shown in red.


Section 1: Fill in the blanks [10 points] (one point each)

After I write my program with an editor, I use a(n) compiler before I execute my program. That particular tool points out syntax or compile-time errors in my program.

My Java application runs inside a(n) Java virtual machine or interpreter.
When my Java program runs inside a web browser, it is called a(n) applet.

Objects in the same class have something in common: a set of messages called a protocol.

Thestate of an object is what makes it different from other objects in the same class.

The import statement is used to access classes in a package.

To see the top element of a stack without creating a side effect, I should send to the stack the message peek().

When a Java application is launched, the main method is invoked. When a Java applet is launched, the web browser creates an applet object and sends the init() message to the object.

The following are possible answers: (Applet, Compiler, Compile-time, Import, Init, Interpreter, Java Virtual Machine, Main, Messages, Paint, Parameter, Peek, Pop, Program, Protocol, Push, Run, State, Syntax)


Section 2: Multiple choices [32 points] (4 points each)

Circle exactly ONE choice as the best answer to each question.
  1. A, B, and C are variables of type int. Which of the following program segments would NOT have the effect of interchanging the values of the variables A and B.
    a) C=A; A=B; B=C;
    b) C=A; B=A; A=B;
    c) A=A+B; B=A-B; A=A-B;
    d) C=B; B=A; A=C;

  2. Here are the BNF production rules of a foolsequence:
    ::= <pre><root><suf>
    <pre> ::= A|B|C
    <root> ::= <pre>C|CC<root>
    <suf> ::= <pre>|X|Y|Z

    Which of these is NOT a legal foolsequence?
    a) CCCC
    b) CCCCC
    c) AACC
    d) BCCCCCCBCY

  3. Which of these sentences is wrong?
    a) Many variables can be bound to the same object at the same time.
    b) A variable can be bound to many objects at the same time.
    c) When not bound to an object, a variable has the value Null.
    d) A constant, once bound to an object, can not be rebound to another object.
    e) A variable can be bound to only one object at a time.

  4. To declare a method farewell which returns nothing and has two parameters, a string and an integer, I should use:
    a) public null farewell(String myName, Integer myTokens)
    b) public void farewell(String myName, Integer myTokens)
    c) public String farewell(String myName, Integer myTokens)
    d) public static farewell(String myName, Integer myTokens)

  5. Which one of the following kinds of variables has the largest scope?
    a) public static variable
    b) method parameter
    c) instance variable
    d) local variable
    e) private static variable

  6. A method dispatch is the association between a message and an instance method. Which of the following is NOT true?
    To perform method dispatch the compiler:
    a) checks the signature of the methods.
    b) checks the class of the object receiver.
    c) checks the order of the arguments and parameters.
    d) checks the types of the arguments and parameters.
    e) checks the class of the empty argument list.

  7. What is the output of this statement:
    System.out.print("abcdefghijk".substring(3,6));
    a) defg
    b) abcdef
    c) cdefgh
    d) def

  8. Which of the following sets of statements will cause a compiler error?
    a) int a;
    final int b;
    a=5;
    b=6;
    a=a*b;

    b) int a;
    final int b;
    a=5;
    b=6;
    b= a*b;


    c) double a;
    float b;
    a=5.0;
    b=500f;
    a=b/a;

    d) float a;
    double b;
    a=5.0f;
    b=500;
    b=2*a/b;

Section 3: Answer brief questions [14 points]

Answer briefly in the space provided.
  1. [4 points] What object is the following phrase referencing?
    "Hello".toUpperCase().toLowerCase();
    "hello"

    Which part of the previous phrase is a reference to "HELLO"?
    "Hello".toUpperCase()

  2. [4 points] Put a circle around the receiving object for each of these two statements:
    System.out.print("Hello");

    name=this.greeting();

  3. [6 points] What is printed out with the following set of Java statements?

    String X;
    String Y;
    X= "Hello ";
    Y= X.concat("my friend");
    X=Y.concat(" Fred");
    System.out.println(X);
    System.out.println(Y);

    Hello my friend Fred
    Hello my friend


Section 4: Writing code [24 points]

  1. [8 points] You are asked to write a small program that prints today's date, reads 3 integers from the keyboard, and prints the sum and average of the entered 3 integers. Note that when sent to an Integer object, the message intValue() returns an int value. For the sake of time and space, do not print the prompts.
    import java.util.*;
    public class myTest {
       /* Reads 3 numbers and displays their sum and average */

            public static void main (String args[]) {

           /* program statements go here */

            Integer firstNum, secondNum, thirdNum;
            int sum;

            System.out.println(new Date());
            firstNum = Keyboard.in.readInteger();
            secondNum = Keyboard.in.readInteger();
            thirdNum = Keyboard.in.readInteger();

            sum = firstNum.intValue() + secondNum.intValue() + thirdNum.intValue();

            System.out.print("Sum of the 3 numbers = ");
            System.out.println(sum);
            System.out.print("Average of the 3 numbers = ");
            System.out.println(sum/3f);
            }
    }

    Another possible solution could be:

    import java.util.*;
    public class myTest {
       /* Reads 3 numbers and displays their sum and average */

            public static void main (String args[]) {

           /* program statements go here */

            int firstNum, secondNum, thirdNum, sum;

            System.out.println(new Date());
            firstNum = Keyboard.in.readInteger().intValue();
            secondNum = Keyboard.in.readInteger().intValue();
            thirdNum = Keyboard.in.readInteger().intValue();

            sum = firstNum + secondNum + thirdNum;

            System.out.print("Sum of the 3 numbers = ");
            System.out.println(sum);
            System.out.print("Average of the 3 numbers = ");
            System.out.println(sum/3f);
            }
    }

  2. [2 points] Is the statement import java.util.*; necessary? Why/why not?
    Yes, the statement is necessary because we need the Date class in order to print today's date.

  3. [8 points] Write a method called sumThree that gets 3 Integer objects as arguments and returns their sum as an int value.
    public int sumThree(Integer number1, Integer number2, Integer number3) {

        return(number1.intValue() + number2.intValue() + number3.intValue());
    }

  4. [6 points] Indicate how you would change your main method in order to use sumThree, had you had an object test of the class myTest created as follows:

    myTest test;
    test=new myTest();

    and sumThree was an instance method of the class myTest.
    public static void main (String args[]) {

        /* program statements go here */

        myTest test;
        test = new myTest();

        Integer firstNum, secondNum, thirdNum;
        int sum;

        System.out.println(new Date());
        firstNum = Keyboard.in.readInteger();
        secondNum = Keyboard.in.readInteger();
        thirdNum = Keyboard.in.readInteger();

        sum = test.sumThree(firstNum, secondNum, thirdNum);

        System.out.print("Sum of the 3 numbers = ");
        System.out.println(sum);
        System.out.print("Average of the 3 numbers = ");
        System.out.println(sum/3f);
    }

Section 5: Tracing code [20 points]

Consider the following Java class:
public class myQuestion {

/* instance variables */

	private int r;
	private int n;
	private Stack s;
	
	public myQuestion(int x, int y) {

		this.r = x;
		this.n = y;
		this.s = new Stack();
	}

	public void a1 (String ms, int mn) {

		this.r = this.r + mn;
		this.n = this.n + 1;
		this.s.push(ms);
	}

	public double disc() {
		double value;

		value = this.r * 0.25;
		return value;
	}

	public display() {

		String t;

		System.out.println(this.r); 
		System.out.println(this.n); 
		t = this.s.pop();
           System.out.println(t);
		t = this.s.pop();
           System.out.println(t);
		t = this.s.pop();
           System.out.println(t);
	}
}

a ) [12 points] What is the output of the program segment:

myQuestion q;
q=new myQuestion(0,0);
q.a1("potato", 9);
q.a1("avocado", 4);
q.a1("tomato",7);
q.display();
System.out.println(q.disc());

20
3
tomato
avocado
potato
5

b) [8 points] What is the output of the program segment:

myQuestion q;
q=new myQuestion(20,2);
q.a1("bread", 5);
q.a1("milk",7);
q.a1("pop",8);
q.display();
System.out.println(q.disc());

40
5
pop
milk
bread
10