UNIVERSITY OF OTTAWA

FACULTY OF ENGINEERING

SCHOOL OF IT AND ENGINEERING

 

CSI1102/CSI1502A

 

Midterm

March 5, 2005

 

Examiners:

Rimon Elias (CSI 1102 Group A)

Aziz Abdesselam (CSI 1102 Group B)

Diana Inkpen (CSI 1102 Group C)                                        

Wissam Itani (CSI 1502A)

 

 

 

Name

 

Student Number

 

Group (A, B, C)

 

 

Total marks:                            50

Duration:                                 80 minutes

Total Number of pages:          12

 

 

Important Regulations:

1.     Students are allowed to bring in a single letter-size page of notes.

2.     No calculators are allowed.

3.     A student identification cards (or another photo ID and signature) is 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 indicated spaces.

6.     The last page contains some extra space for rough work.

 

 



Part A                                                                                                              [7 marks]

                                                                                                                        (1 mark each)

 

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

 

 

1.      The following expression creates an object of class Student:

            Student John;                   

 

2.     An 8 bits datatype can encode a maximum of 254 values.   

 

3.     Service methods declared with public visibility violate encapsulation.

 

4.     Two or more methods within the same class may have the same name.

 

5.     A static method of a class can be used without creating objects of the class.

 

6.     An inner class has access to the private data of its outer class.

 

7.     An array subscript should normally be of data type float.

 

 

Answer sheet: Part A

 

Question

Answer

1.      

F

2.      

F

3.      

F

4.      

T

5.      

T

6.      

T

7.      

F


Part B: Multiple Choice                                                                         [8 marks]

 

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

 

 

1.     A computer can directly understand only its own ___________.

a)     machine language

b)     assembly language

c)     high-level language

d)     none of the above

 

2.     Which of the following is an invalid identifier (variable name)?

a)     _Test

b)     TEST

c)     5test

d)     test1

 

3.     The OR (||) operator

a)     has higher precedence than the AND (&&) operator

b)     stops evaluation upon finding one condition to be true

c)     associates from right to left

d)     is a ternary operator

 

4.     An aggregate object

a)     contains references to other objects

b)     is represented by an open diamond at the aggregate end in a UML class diagram.

c)     both (a) and (b)

d)     none of the above

 

5.     A two dimensional array in Java is:

a)     an array of arrays

b)     referenced using 2 index values

c)     all of the above

d)     none of the above

 

 

Questions 6, 7, and 8 refer to the following UML diagram:

 

6.     UML diagram is a /an:

a)     Object UML diagram

b)     Class UML diagram

c)     Both (a) and (b)

d)     None of the above

 

7.     variables numFaces and faceValue are:

a)     private

b)     public

c)     static

d)     none of the above

 

8.     SnakeEyes is a driver class that will create _________ objects of the class Die.

a)     zero

b)     one

c)     two

d)     three

 

Answer sheet: Part B

 

Question

Answer

1.      

A

2.      

C

3.      

B

4.      

C

5.      

C

6.      

B

7.      

A

8.      

C

Part C

 

Complete the following sentences by filling in the missing words:              [7 marks]

                                                                                                                 (1 mark each)

 

 

1.     Variable names actually correspond to locations in ___________   

 

 

2.     Support methods should be declared with _______ visibility. 

 

 

3.     A ________ is a special method that has the same name as the class and has no return type.

 

 

4.      The _________  ________ is used in Java to free memory that is no longer used.  

 

 

 

5.     A ________ class is used in Java to transform a primitive type into an object.

 

 

 

6.     The elements of an array are related by the fact that they have the same _________.

 

 

 

7.     The process of placing the elements of an array in order is called  ________ the array.


 

Answer sheet: Part C

 

Question

Answer

1.      

memory

2.      

private

3.      

constructor

4.      

garbage collector

5.      

wrapper

6.      

type

7.      

sorting


Part D

 

D.1.  What does the following code print?                                                   [15 marks]

 

public class Midterm

{

   //------------------------------------------------------

   // Sets prices for some fruits

   //------------------------------------------------------

   public static void main (String[] args)

   {

      PartD1 tester = new PartD1();

    

      double price1 = 0.86, price2 = 0.87;

     

      Fruit a1 = new Fruit("Apples", price1);

      Fruit a2 = new Fruit("Oranges", price2);

      double a3 = a1.getPrice() + a2.getPrice();

 

      System.out.println (a1 + "\t" + a2 + "\t\t" + a3);

      tester.changeValues (a1, a2, a3);

      System.out.println (a1 + "\t" + a2 + "\t\t" + a3);

 

   }

}

 

//////////////////////////////////////////

public class PartD1

{

   public void changeValues (Fruit a1, Fruit a2, double a3)

   {

      a1.setValue("Grapes", a1.getPrice()*3);

      a2 = new Fruit("Bananas", a2.getPrice());

      a3 = a1.getPrice() + a2.getPrice();

    

      System.out.println (a1 + "\t" + a2 + "\t\t" + a3);

   }

}

///////////////////////////////////////

public class Fruit

{

   private String fruit;

   private double price;

 

   public Fruit (String fruit, double price)

   {

     setValue (fruit, price);

   }

 

   public void setValue (String fruit, double price)

   {

      this.fruit = fruit;     

      this.price = price;

   }

 

   public double getPrice ()

   {

      return price;

   }

   

   public String toString ()

   {

      return fruit + " = " + price;

   }

}

 

 

Answer sheet Part D, Question 1.

                                               

 

Text Box: Apples = 0.86		Oranges = 0.87		1.73									
Grapes = 2.58		Bananas = 0.87		3.45
Grapes = 2.58		Oranges = 0.87		1.73




D.2. What does the following code print? Fill in the blanks in the answer sheet below.                                                                                                                             [7 marks]

 

public class ShapeDriver

{

   public static void main (String[] args)

   {

      Shape s1 = new Shape (5);

      System.out.println (s1);

 

      Shape s2 = new Shape (7);

      System.out.println (s2);

 

      Shape s3 = new Shape (4);

      System.out.println (s3);

 

      System.out.println ("N is: " + Shape.getN());

   }

}

////////////////////////////

public class Shape

{

   private int numLines;

   private static int N = 0;

 

   public Shape (int val)

   {

      numLines = val;

      N = N + val;

   }

 

   public String toString()

   {

    return "Number of lines is " +numLines + " and N is " +N;

   }

 

   public static int getN ()

   { 

      return N;

   }

}

 

Answer Sheet Part D, Question 2

Number of lines is __5___ and N is __5__

Number of lines is __7___ and N is __12__

Number of lines is __4___ and N is __16__

N is: __16___

D. 3.       Write a program that asks the user for a width and height of a rectangle and then outputs a hollow rectangle.  The part that reads input is already written for you. The program only accepts valid input (numbers >= 3). Fill in the code for outputting the rectangle. The rectangle is composed of the character '*' and ' '.  Therefore, a rectangle with a width of 8 and a height of 5 would appear as follows:               [6 marks]

********                                                     

*      *

*      *

*      *

********

Text Box: import cs1.Keyboard;

public class rectangle {
public static void main(String args[])
{
   		int Width=0; int Height=0; int Counter1, Counter2;

while (Width < 3)
{
 System.out.println("Enter the width of the rectangle");
 Width = Keyboard.readInt();
 if (Width < 3)
System.out.println("Error in input, try again");
   		}

while (Height < 3)
{
 System.out.println("Enter the height of the rectangle");
             Height=Keyboard.readInt();
 if (Height < 3)
System.out.println("Error in input, try again");	
}

// D.3 answer 

for (Counter1 = 0; Counter1 < Width; Counter1++)
  System.out.print("*");
   		System.out.println();

for(Counter2 = 0; Counter2 < Height-2; Counter2++)
{System.out.print("*");
for (Counter1 = 0; Counter1 < Width-2; Counter1++)
System.out.print(" ");
System.out.print("*");
System.out.println();
}

for (Counter1 = 0; Counter1 < Width; Counter1++)
System.out.println("*");
System.out.println();
}
}