UNIVERSITY OF OTTAWA

FACULTY OF ENGINEERING

SCHOOL OF IT AND ENGINEERING

 

CSI1102/CSI1502A

 

Midterm

March 6, 2004

MEMORANDUM

 

Examiners:

Dr. Diana Inkpen (CSI1102 Groups A, D)                                         

Dr. Herna L Viktor (CSI1102 Groups B, C)

Dr. Fadi Malek (CSI1502A)

 

 

Total marks:                             60

Duration:                                  80 minutes

Total Number of pages:            14 (including 3 pages for rough work)

 

 

Important Regulations:

1.      Students are not allowed to bring in any notes.

2.      No calculators are allowed.

3.      Student identification cards (or another photo ID and signature) are required.

4.      An attendance sheet shall be circulated and should be signed by each student.

5.      Please answer all questions on this paper, in the spaces as indicated.

6.      All questions are related to the Java object-oriented language.

 

 

 


Part A: Multiple Choice                                                                                                 [30]

 

Indicate which one of the following choices is correct. (Note: Only one of the answers is correct and each question counts 2 marks each.) Use the answer sheet provided below to indicate your answer.

 

1.      What is a runtime error?

  1. An error identified by the compiler at the compilation time.
  2. An error identified by the compiler at the execution time.
  3. An error that occurs during the execution of a program, that causes a program to produce incorrect results.
  4. An error that occurs during the execution of a program, that causes a program to terminate abnormally.

 

2.      What does the Java interpreter do?

  1. Compiles a Java program into bytecode.
  2. Compiles Java bytecode into machine code.
  3. Executes Java bytecode line by line.
  4. Executes a Java program line by line.

 

 

3.      How many lines does the following program print?   

 

       System.out.println ("Roses are red,\nViolets " +

                        "are \nblue,\nSugar is sweet");

   System.out.print ("And so are");

   System.out.print ("you");

 

  1. 7
  2. 6
  3. 5
  4. 4
  5. 3

 

 

4.      Given an array called Scores, initialized as follows. What are the values of Scores[1].length?

 

        int [] [] Scores = { {3,4,5}, {6,7}, {8,9,10,11}};

 

  1. 2
  2. 3
  3. 1
  4. 4
  5. an error or exception will occur; it should be scores[1].length();

 


 

5.      How many times will the following loop execute?

for(j=1; j<=10; j=j-1);

 

a.       1.

b.      10.

c.       Never.

d.      Forever.

 

 

6.      The java data type for numbers such as 3.14159 is:

a.       double

b.      int

c.       real

d.      String

 

 

7.      Suppose that a class Xclass has a method with the signature public static void methodName()and that x is a reference to an object of type Xclass. Consider the following three Java statements that occur in a method belonging to another class Yclass.

1. Xclass.methodName();

2. x.methodName();

3. methodName();

 

Which one of the following statements is true?

a.       1 is the only valid Java statement

b.      2 is the only valid Java statement

c.       1 and 3 are the only valid Java statements

d.      1, 2 and 3 are all valid Java statements

 

 

8.      Which one of the following statements is true?

a.       The source code for a class can explicitly define zero, one or more constructors.

b.      The source code for a class must explicitly define exactly one constructor.

c.       The source code for a class must explicitly define at least one constructor.

d.      The source code for a class should never contain any explicitly defined constructors.

 

 

9.  The expression evaluated at the beginning of a switch statement may include

a.       All the integer primitive data types such as int, short and long

b.      The int and char primitive data types only.

c.       Any of the eight primitive data types

d.      The int primitive data type only


10.  The relationship between a class and an object is best described as

a.       classes are instances of objects

b.      objects are instances of classes

c.       objects and classes are the same thing

d.      classes are programs while objects are variables

e.       objects are the instance data of classes

 

11.  In order to preserve encapsulation of an object, we would do all of the following except for which one?

a.       Make the instance data private

b.      Define the methods in the class to access and manipulate the instance data

c.       Make the methods of the class public

d.      Make all the instance data public

e.       All of the above preserve encapsulation

 

12.  Suppose x and y are String variables with x = "Vacation" and y = null. If the statement x.length( ) + y.length( ); is executed, the result is:

a.       0

b.      8

c.       7

d.      12

e.       error or exception

 

13.  Suppose that x = "Vacation". If the operation y = "Vacation"; is performed, then the result of the equality test (x = = y) is

a.       true

b.      false

c.       x and y becomes aliases

d.      x is set to the value null

e.       a run-time error occurs

 

14. Which of the following is true regarding outer and inner classes?

a.       no method of an inner class can be static

b.      no method of an outer class can be static

c.       no instance data or method of an inner class can be static

d.      no instance data or method of an outer class can be static

e.       none of the above, there is no restriction on what can and cannot be static between inner and outer classes

 

15.  Why does the following code give a compilation error, and how should it be fixed?

 

 ArrayList A1 = new ArrayList();

 A1.add("Hello");

 

 int A2 [] = {1,2,3,4};

 System.out.println("A1 has " + A1.size + " elements.");

 System.out.println("A2 has " + A2.length + " elements.");

 

a) because of A1.size; it should be A1.length;

b) because of A1.size; it should be A1.size();

c) because of A2.length; it should be A2.size;

d) because of A2.length; it should be A2.length();

e) because of A2.length; it should be A2.size();

 

 

Answer sheet: Part A

 

Question

Answer

1.       

D

2.       

C

3.       

C

4.       

A

5.       

D

6.       

A

7.       

A

8.       

A

9.       

B

10.   

B

11.   

D

12.   

E

13.   

B

14.   

C

15.   

B

 

 

 


PART B                                                                                                                           [10]

 

Complete the following sentences by filling in the missing words:

 

1.      The ………….…operator returns a reference to a newly created object.           [1]

2.      The integer data types in Java are…………………... and the floating data types are …………                                                                                                                  [1]

3.      A static method is invoked through ……………………..                                 [1]

4.      The Java ………. operand is a ternary operator because it requires three operators. [1]

5.      The do statement is similar to the while statement except that its termination condition is ……...... of the loop body.                                                                            [1]

6.      The + operator performs ............. if at least one of the operands is
a string.                                                                                                                         [1]

7.      An UML …...  diagram shows the values of the instance data of each
object, and does not show the methods.                                                                         [1]

8.      An instance variable with ……..  visibility cannot directly be accessed from outside the class.                                                                                                                        [1]

9.      A service method should be declared with ..……. visibility.                                [1]

10.  When an array is passed to a method, it is passed by ………                                         [1]

 


Answer sheet: Part B                                                                                    

 

Question

Answer

1.       

new

2.       

(byte, short, int and long)

(float and double)

 

3.       

the name of the class that contains it

4.       

conditional

5.       

at the end

6.       

concatenation

7.       

object

8.       

 

private

9.       

 

public

10.   

reference


Part C                                                                                                                              [20]

 

1.  Consider the following program.

 

public class Example

{ public static void main (String[] args)

   { 

    Tester tester = new Tester();

    int a1 = 50;

    Point a2 = new Point(1,2);

    Point.z = 30;

 

    System.out.println ("Before: a1 is " + a1 + ",  a2 is " + a2);

 

    tester.changeValues (a1, a2);

    a2.setY(7);

     

    System.out.println ("After : a1 is " + a1 + ",  a2 is " + a2);

   }

}

 

public class Point

{

   private int x, y;

   public static int z;

 

   public Point (int x1, int y1)

    { x = x1;  y = y1;  z = 1;  }

 

   public void setX (int x1)

    { x = x1; }

 

   public void setY (int y1)

    { y = y1; }

 

   public String toString ()

    { return "x " + x + " y " + y + " z " + z++; }

}

 

public class Tester

{ 

   public void changeValues (int f1, Point f2)

   {

      System.out.println ("Before: f1 is " + f1 + ",  f2 is " + f2);

 

      f1 = 90;

      f2.setX(3);

      Point.z = 20;

 

      System.out.println ("After : f1 is " + f1 + ",  f2 is " + f2);

   }

}

 


Answer sheet: Part C, Question 1.

 

What output is produced when Example.class is executed? Fill in the gaps in the answer sheet below.                                                                                                                 [12]

 

 

Before: a1 is 50,  a2 is x 1 y 2 z 30

Before: f1 is 50,  f2 is x 1 y 2 z 31

After : f1 is 90,  f2 is x 3 y 2 z 20

After : a1 is 50,  a2 is x 3 y 7 z 21

 

One mark per answer  à 12 marks

                                             

 




2. Consider the following code fragment.                                  

 

public class Problem2

{

      public static void main(String[] args)

      {

            String quote = "Imagi e";

            final int MAX = 8;

            int[] countlist = new int[MAX];

 

            WalkThrough walk = new  WalkThrough(quote);

 

            for (int i = 0 ; walk.hasMoreElements(); i++)

            {

                  countlist[i] = i*MAX;

                  System.out.print(walk.value() + "*");

                  walk.nextElement();

            }

            System.out.println("!!!");

 

            for (int i = 0; i < MAX; i++)

                  System.out.print(" " + countlist[i]);

      }

}

 

public interface Iterator

{

      public boolean hasMoreElements();

      public char nextElement();

      public void reset();

      public char value();

}

 

 

class WalkThrough implements Iterator

{

      String string;

      int index;

 

      public WalkThrough(String s)

      {  string = s;

            index = 0; }

     

      public boolean hasMoreElements()

      { return index < string.length(); }

     

      public char nextElement()

      {

            System.out.println(index);

            return getNextelement();

      }

 


      private char getNextelement()

      {

            String tempstring = string.toUpperCase();

            char temp = tempstring.charAt(index);

            index++;

 

      return temp;

      }

 

      public void reset()

      { index = 0; }

     

      public char value()

      {

            char temp = string.charAt(index);

            return temp;

      }

}

 

 

What output is produced when Problem2.class is executed?                                          [8]

 

 

Answer Sheet: Part C, Question 2

 

 

I*0                               1 mark if exactly correct
m*1                             1 mark if lowercase letters: m, a, g, i, e
a*2                              1 mark for new lines (println)
g*3
i*4                               1 mark if exactly correct up to here
 *5                               1 mark
e*6
!!!                                1 mark  if exactly correct
0 8 16 24 32 40 48 0   2 marks if exactly correct