Faculty of Engineering

School of Information Technology and Engineering

 

CSI 1102: Fundamentals of Software Design

 

Final Exam

 

Examiners:                                                                                                April 25, 2005, 14:00

Rimon Elias (CSI 1102A)                                                                                   Total marks: 68

Aziz Abdesselam (CSI 1102B)                                                                         Duration: 3 hours

Diana Inkpen (CSI 1102C)                                                                  Total number of pages: 18

 

 

Last Name:

 

 

First Name:

 

Student Number:

 

Group (A, B, C):

 

 

 

 

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.     Use both sides of these sheets if you need some extra space for rough work.

7.     At the end of the exam, when time is up:

a.     Stop working and turn your exam upside down.

b.     Remain silent.

c.      Do not move or speak until all exams have been picked up, and a TA or the Professor gives the go-ahead to leave.

 

A

B

C

D.1

D.2

D.3

D.4

Total

 

 

 

 

 

 

 

 

 

 

10

10

10

8

10

8

12

68


Part A [10 marks]

                                                                                                                       

State whether the following statements are True of False. Use “Answer Sheet: Part A” to indicate your answer.

 

1.     Objects are defined by a class that describes the characteristics common to all instances of the class.  

 

2.     All classes in Java are direct or indirect subclasses of the class Object, except user-defined classes.

 

3.     A static method can use non-static variables of the class.

 

4.     The wrapper class for the primitive type string is called String.

 

5.     The methods of an inner class have access to private variables of the outer class.

 

6.     Glass box testing maps a set of specific inputs to a set of expected outputs.

 

7.     The build and fix approach is an efficient development model to address errors.

 

8.     Iterative development process can be defined as a waterfall model with backtracking.

 

9.     A queue is a linear data structure.

 

10.  An array is a dynamic data structure.


 

Answer Sheet: Part A

 

Question

Answer

1.      

T

2.      

F

3.      

F

4.      

F

5.      

T

6.      

F

7.      

F

8.      

T

9.      

T

10.   

F


Part B [10 marks]

 

Indicate which one of the following choices is correct. (Note: Only one of the answers is correct.) Use “Answer Sheet: Part B” to indicate your answer.

 

1.     If a Java class implements two interfaces, the class needs to provide implementation (bodies) for ________

 

(a) none of the methods from the two interfaces.

(b) only the methods from the first interface.

(c) only the methods from the second interface.

(d) all the methods from the two interfaces.

 

2.     If mystery is a recursive method in a class, what does it return when it is called with an integer parameter of value 6?  (i.e., o1.mystery(6), where o1 is an object of that class.)

 

     public int mystery(int n)

     {   

         if (n == 2) return 0;

         else return n + mystery(n-2);

     }

 

(a)   8

(b)  10

(c)   12

(d)  4

 

3. If recur is a recursive method in a class, what does it return when it is called with two integer parameters of value 5 and 3?  (i.e., o1.recur(5,3), where o1 is an object of that class.)

 

      public int recur(int x, int y)

      {

         if (x == y) return x;

         else return recur(x-1, 1) + 2;

      }

 

(a)   8

(b)   9

(c)   10

(d)   11

 

Note: there was a typo in the questions, so you get 1 mark for any answer.


4. Which of the following statements is correct?

            (a) int[] index = {1,2,3,4};

            (b) int index[] = {1,2,3,4};

            (c) All of the above.

            (d) None of the above.

 

5. A test case includes ________

            (a) a set of inputs.

            (b) user actions or other initial conditions.

            (c) expected output.

            (d) All of the above.

 

6. The goal of a walkthrough is to ________

            (a) solve problems.

            (b) identify problems.

            (c) All of the above.

            (d) None of the above.

 

7. A method signature includes ________

            (a) the number, type and order of the parameters

            (b) the return type

            (c) Both (a) and (b)

            (d) None of the above

 

8. In a ________, the last node points to the first node.

            (a) circularly linked list

            (b) doubly linked list

            (c) tree

            (d) digraph

 

 

 

 


9. How many times will the following nested loop structure execute the inner-most statement (x++)?

      for (int j = 0; j < 100; j++)

      for (int k = 100; k > 0; k--)

        x++;

 

(a)   100

(b)  200

(c)   10,000

(d)  20,000

(e)   1,000,000

 

10. Java does not support multiple inheritance, but some of the abilities of multiple inheritance are available by ________

(a)   importing classes.

(b)  implementing more than one interface.

(c)   overriding parent class methods.

(d)  creating aliases.

(e)   using public rather than protected or private modifiers.

 


Answer Sheet: Part B

 

Question

Answer

1.      

D

2.      

B

3.      

B

4.      

C

5.      

D

6.      

B

7.      

A

8.      

A

9.      

C

10.   

B

 


 

Part C [10 marks]

 

Complete the following sentences by filling in the missing words. Use “Answer Sheet: Part C” to indicate your answer.

                                                                                                    

 

1. A software version that is made available to the user is called a ________.

 

2. A _________ is a program created to explore a particular concept.

 

3. __________ describes the numeric relationship between objects.

 

4. When an object is passed as a parameter, it is passed by _________.

 

5. A recursive method has a ________ case and a recursive part.

 

6. A _________ is a geometric shape than can consist of the same pattern repeated in different scales and orientations.  

 

7. An object reference is a variable that stores the ________ of an object.

 

8. A _________ is an example of a FIFO data structure.

 

9. __________ operation removes an item from the top of the stack while peek (or top) operation retrieves the top without removing it.

 

10. In order to preserve encapsulation and allow variables to be inherited the access modifier ____________ should be used.

 

 


Answer Sheet: Part C

 

Question

Answer

1.      

release

2.      

prototype

3.      

(cardinality/multiplicity)

4.      

reference

5.      

base

6.      

fractal

7.      

address (name)

8.      

queue

9.      

pop

10.   

protected


D.1 [8 marks]

 

1.  Assume that the Continent and Country classes are defined as follows.                

 

public class Continent

{

protected String name;

protected String location;

 

public Continent(String name, String address)

{

this.name = name;

this.location = address;

}

 

public String toString ()

{

return name + " in " + location;

}

}

 

public class Country extends Continent

{

private int population;

 

public Country(String name, String location)

{

            super(name, location);

            population = 2;

}

 

public Country(String name, String location, int population)

{

            super(name, location);

            this.population = population + 10000;

}

 

public void setPopulation(int population)

{

            this.population = population;

}

 

public int getPopulation()

{

            return population;

}

 

public  String  toString ()

{

      return name + " in "  + location + ": Population " + population + "\n";

}

}


(a) [4 marks] What outputs are produced by the following code fragment?

 

Country countr1 = new Country("Bora Bora", "Pacific");
System.out.println(countr1);

Country countr2 = new Country("France", "Europe", 20000);
System.out.println(countr2);


Use “Answer Sheet: Part D.1.a” to indicate your answer.

 

Answer Sheet: Part D.1.a

 

                                                                                                                                               

Bora Bora in Pacific: Population 2

 

France in Europe: Population 30000

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                               

 

 

 


 

(b) [4 marks] Suppose the instance data of the Continent class were declared private instead of protected. Explain how you would modify your code to handle this and show the final, modified code fragment. Use “Answer Sheet: Part D.1.b” to indicate your answer.

 

Answer Sheet: Part D.1.b

 

 

// in the Country class

public String toString()

 {

 

      return super.toString() + ": Population " + population + "\n";

  }

 

 

 

 

 

 

 

 

 

 

 

 

                                               

 




D.2 [10 marks] What does the following code print? Use “Answer Sheet: Part D.2” to indicate your answer.

 

public class Expect {

 

      public static void main(String[] args) {

      int a=0, b=5;

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

      try {

            b += 2;

            a = b/c[5];

            c[2] = 10/a;

            c[3] = 5/(b-b);

      }

      catch(ArithmeticException e1) {

            System.out.println("There is a problem at the level e1");

            a = c[3]/b;

            b += 3;

      }

      catch(Exception e2) {

            System.out.println("There is a problem at the level e2");

            a = 7/c[2];

            b += 5;

      }

      finally {

            a *= 3;

            b /= 2;

      }

     

      System.out.println("a= " + a);

      System.out.println("b= " + b);

      System.out.print("The array c is: ");

      for( int i=0; i<c.length; i++) System.out.print(c[i] + " ");

 

      }

}

 

Answer Sheet: Part D.2

 

There is a problem at the level e2

a= 6

b= 6

The array c is: 1 2 3 4 5

 

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 



D.3 [8 marks] What does the following code print?   Use “Answer Sheet: Part D.3” to indicate your answer.

 

public class TestInner

{

   public static void main (String[] args)

   {

      Outer out = new Outer();

 

      System.out.println (out);

      System.out.println();

 

      out.change();

 

      System.out.println (out);

   }

}

 

 

public class Outer

{

   private String str;

   private Inner in1, in2;

 

   public Outer()

   {

     str = "Start.";

     in1 = new Inner (4);

     in2 = new Inner (6);

   }

 

   public void change()

   { 

      str = "End.";

      in1.num ++;

      in2.num ++;

   }

 

   public String toString()

   {

      return "Inner = " + in1 + "\n" + "Inner = " + in2;

   }

 

  // an inner class

  private class Inner

   {

      public int num;

 

      public Inner (int n)

      {

         num = n;

      }

 

      public String toString()

      { 

         return ++num + "\nOuter = " + str;

      }

   }

}

 

 

Answer Sheet: Part D.3

 

Inner = 5     

Outer = Start.

Inner = 7

Outer = Start.

 

Inner = 7

Outer = End.

Inner = 9

Outer = End.

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



D.4 [12 marks] What does the following code print? Use “Answer Sheet: Part D.4” to indicate your answer.

 

 

public class Students{

   //  Creates a StudentList object, inserts several students into

   //  the list, then prints it.

 

   public static void main (String[] args){

  

      StudentList studentClass = new StudentList();

     

      studentClass.insert (new Student(234, "Martin", 56, 77));

      studentClass.insert (new Student(123, "Mark", 50, 49));

      studentClass.insert (new Student(345, "Paul", 67, 76));

      studentClass.insert (new Student(333, "David", 34, 55));

     

      System.out.println (studentClass);

   }

}

//*******************************************************************

public class StudentList{

 

   private StudentNode list;

 

   StudentList(){    //  Sets up an initially empty list of students.

 

      list = null;

   }

 

   public void insert(Student st){

 

   //  Creates a new StudentNode object and inserts it into the list.

 

      StudentNode node = new StudentNode(st);

      StudentNode current;

      int count = 1;

           

      if (list == null) list = node;

      else

      if(st.getID() < list.getID()){

            node.next = list;

            list = node;     

      }

      else{

         current = list;

         while(current.next!= null && current.next.getID()<st.getID()){

            current = current.next;

            count ++;

         }

         node.next = current.next;

         current.next = node;

         count++;

      }

      node.setOrder(count);

   }

 

   public String toString(){

   // Returns this list of students as a string.

 

      String result = "";

      StudentNode current = list;

 

      while(current != null){        

         result += current.order + " " + current.student + "\n";

         current = current.next;

      }

      return result;

   }

   //*****************************************************************

   private class StudentNode{

   //  An inner class that represents a node in the student list.

 

      public Student student;

      public StudentNode next;

      public int order;

 

      public StudentNode(Student st){     //  Sets up the node

 

         student = st;

         next = null;

      }      

 

      public int getID(){ return student.getID();}

 

      public void setOrder(int whenInserted){ order = whenInserted;}

   }

}

//********************************************************************

public class Student{

 

   private int ID;

   private String name;

   private int mark;

 

   public Student(int ID, String name, int mark0, int mark1){

 

      this.ID = ID;

      this.name = name;

      mark = (mark0 + mark1)/2;

   }

 

   public int getID(){ return ID;}

 

   public String toString(){

 

      return "" + ID + " " + mark + (mark > 50 ? " passed" :" failed");

   }

}

 

 


Answer Sheet: Part D.4

 

1 123 49 failed

1 234 66 passed

3 333 44 failed

3 345 71 passed