UNIVERSITY OF OTTAWA

FACULTY OF ENGINEERING

SCHOOL OF IT AND ENGINEERING

 

CSI1102 – Group A

 

Midterm Test

February 2003

 

Examiners:

Prof Abdulmotaleb El Saddik (Group A)                                          

Prof Herna L Viktor (Groups B, C)

 

Total marks: 60

Duration: 75 minutes

 

1.     Answer all the questions on this question paper.

2.     The last page contains some space for rough work.

3.     This is a closed book test.

 

 

Good luck!

 

     


Part A [10 marks]

 

State whether the following statements are True of False. Use the answer sheet provided below to indicate your answer.

 

1.     If the following statement is performed:   CD[ ] mycollection = new CD[200]; where CD is a previously defined class, then mycollection[5] is a CD object.

2.     All programs must be translated to a particular CPU’s machine language in order to be executed. Java program does not need.

3.     Constants are similar to variables, but they hold a particular value for the duration of their existence.

4.     A package is often used to group related classes under a common name.

5.     Implementation is the least creative of all development activities.

6.     To solve a problem we need to write a step-by-step process, which we refer to as an algorithm. Algorithm is often expressed in pseudocode.

7.     An aggregate object is composed, in part, of other objects, forming a has-a relationship.

8.     Objects should be encapsulated. The remainder of a program should interact with an object only through a well-defined interface.

9.     The reserved word null represents a reference that point to a valid object.

10.   A static variable cannot be shared among all instances of a class. It belongs only to the first instance of a class.

 

 

Answer sheet: Part A

 

Question

True/False

1.      

T

2.      

F

3.      

T

4.      

T

5.      

T

6.      

T

7.      

T

8.      

T

9.      

F

10.   

F

 


 

Part B [10 marks]: Terminology

 

 

Briefly explain, in your own words, what the following concepts entail. Use the answer sheet provided below to indicate your answer.

 

Note: Each of these five questions count 2 marks each.

 

1.     Which of the following are not valid identifiers? Why?

·       Name

·       name

·       9743

·       Viktor&ElSaddik

·       Assignment_4

 

Self-review question 1.16

 

2.     What does the new operator accomplish?

 

Self-review question 2.16

 

3.     Name the four basic activities that are involved in a software development process.

 

Self-review question 3.1

 

4.     What is an aggregate object?

 

Self-review question 4.13

 

5.     What is the difference between a Class and an Interface?

 

Self-review question 5.9

 


Part C [20 marks]: Program Execution

 

 

Answer all the following questions. Use the answer sheet provided below to indicate your answer.

 

1.     Assume values is an int array that is currently filled to capacity, with the following values:

 

                       

 

 

a.      Assume values are passed to a method as a parameter. Is it passed by reference or value? Explain your answer.  [2]

 

b.     What is returned by values.length? [2]

 

c.      The aim of the following code fragment is to increment each element in values by one. Will this code segment achieve the correct effect? Explain your answer. [4]

 

      for (j=0; j<values.length–1; j++) values[j]++;

 

 

a.     Passed by reference, since an array is an object.

b.     Answer:  7

c.      Answer: It will not produce the correct effect, since it terminates to early. The correct fragment is for(j=0; j<values.length; j++)  values[j]++;

 

 

 

 

2.     Use the following class definition:

                                   

public class StaticExample

            {

                        public static String myString;

private static int x;

                                               

                        public StaticExample (int y)

                        {

                                    x = y;

                                    myString = “yes”;

                        }

 

                        public int incr( )

                        {

                                    x++;

                                    return x;

                                    }

                        }

 


a)     What is the value of z after the third statement executes below? Explain your answer.  [4]

StaticExample a = new StaticExample(5);

StaticExample b = new StaticExample(12);

int z = a.incr( );

 

b)     If there are 4 objects of type StaticExample, how many different instances of x are there? Explain your answer.     [4]

 

c)     Do you agree with the visibility modifiers for the instance variables? Explain your answer.  [4]

 

a) Answer: 13

Explanation:  Since instance data x is shared between a and b, it is first initialized to 5, it is then changed to 12 when b is instantiated, and then it is incremented to 13 when a.incr( ) is performed.  So, incr returns the value 13.

 

b.) Answer: 1

Explanation:  Because x is a static instance data, it is shared among all objects of the StaticExample class, and therefore, since at least one object exists, there is exactly one instance of x.

 

c) Answer: Poor choice of public myString; It should be private not public; private is better since it enforces encapsulation and abstraction. We refer to the IV through a method.


PART D [20 marks]: Program design

 

Answer all the following questions. Use the answer sheet provided below to indicate your answer.

 

Consider a class called BallPlayer, which implements an interface called doBallPlay.  The BallPlayer class is invoked by the MyBall driver class.

 

The BallPlayer class contains the following instance variables:

 

private String name;

private String position;

private int numAtBats;

private int numSingles;

private int numDoubles;

private int numTriples;

private int numHomeRuns;

private double battingAverage;

 

1.     Which of the above-mentioned instance variables were defined in the doBallPlay interface? Explain your answer. [1]

 

Answer: None, since none of them are public.

 

 

2.     Create a UML class diagram to illustrate the relationship between BallPlayer, doBallPlay and MyBall.    [6]

 

Similar to Figure 5.6, indicating IV, methods and the interface.

 

 

3.     Write the constructor, which is used to create a new player. This constructor is passed the player’s name and position.  [4]

 

 

public BallPlayer(String newName, String newPosition)

{

    name = newName;

    position = newPosition;

    numAtBats = 0;

    numSingles = 0;

    numDoubles = 0;

    numTriples = 0;

    numHomeRuns = 0;

    battingAverage = 0.0;

}

Notice that we not only initialize the instance data that were passed as parameters, but also all other data instances.

 


                                                              

4.     Write a method called computeBattingAverage that computes and returns the player’s batting average. The batting average is equal to the total number of hits (singles, double, triples, home runs) divided by the number of at bats, as stored in the numAtBats variable.  [5]

 

public double computeBattingAverage( )

{

  if (numAtBats == 0) battingAverage = 0.0;

  else battingAverage = (numSingles + numDoubles + numTriples + numHomeRuns) / numAtBats;

 

  return battingAverage;

 }

 

 

 

5.     Write a toString method that returns the player’s name, position and batting average formatted to have 1 digit to the left of the decimal and 3 digits to the right of the decimal. Assume that we use an object of the DecimalFormat class to format the battingAverage and that the DecimalFormat class has been imported into BallPlayer.  [4]

 

The DecimalFormat class contains the following methods, defined as follows:

 

DecimalFormat (String pattern)

// creates a new DecimalFormat object with the specified pattern

 

String format (double number)

// returns a string containing the specified number formatted according to the decimal format.                                                         

                                                                                                            .                                           

 

Answer:

public String toString( )

{

       DecimalFormat df = new DecimalFormat("0.000");

       return name + "\t" + position + "\t" + df.format(battingAverage);

}