CS 15-111
Exam One
Study Topics

The Structure of the Exam is as follows The Best way to study for the test is to read the lecture notes, classwork and review lab assignments. Given below is a sample set of questions. Import <package-name>.*;
Public class <class-name> extends <some other class>
                                 implements <an interface>{
//declaration of instance variables
Private <data-type> <name>
Public <class-name>() {
}  // constructor method

Public <return-type> <method-name> ( <parameter-list>) {
}
}

access_modifiers method_modifiers return_type method_name( parameter_list )
          {
                  method body
          } SAMPLE QUESTIONS
1. An object is a class instance
    a. TRUE     b. FALSE
2. A class is a ____________ for objects
   a. instance     b. template    c. variable
3. Package is a collection of ___
   a. classes   b.  methods    c. variables
4. The key word "extends" refer to the notion of
   a. class defintion    b. inheritance    c. state of an object
5. The relationship "is-a" is
   a. inheritance    b. recursion  c. class containment
6. Which one of the following is not a primitive data type
   a. int    b. bool   c. float    d. char
7. The defintions  :  int []  list = new int(12) and
    int  list[12] are equivalent.
   a. TRUE     b. FALSE

WRITING NEW CLASS METHODS
1. Add a new method to the RandomMatrix class in lab2 to do the following
    a.  if the matrix is 2 by 2 then find the inverse of the matrix using
        inverse [ a   b]    is   1/(ad - bc) * [ d   -b]
                   [ c   d]                              [ -c   a]

    b. return true if the matrix is symmetric. i.e.   A[i][j] = A[j][i] for all i,j
    c. a method  isSquare  that returns true if the matrix is sqaure ie. rows = columns

2. Consider the elevator class defined in class. Write a method,
    pickPassenger  that will take a passenger object P as an object and
   add to the internal vector of passengers "in-order" of the destination.

TRACING/WRITING CODE
1. Assume the world of Karel the Robot.
What is the intent of the following code? Describe in words. Can you simplify it?
while (frontIsClear()) {
 if (frontIsClear()) {
  move();
 } else {
  turnAround();
 }
}

2.
    while (i < 5) {
       if (i < 2)  { turnLeft(); i = i + 1;}
       else
          { turnLeft(); turnLeft(); turnLeft(); i = i + 2;}
   }

 Assume that the Robot is facing east. Find the final direction if:
i = 1
i = 5
i = 3
 

3. Study the class defintion of matrix
public class Matrix
{
 public Matrix() {}       // unallocated vectors
 public Matrix(int n, int m); // dimensions given
public Matrix(int n, int m, final int fillValue);
        // and default fill value
     private int NumRows ;
     private int NumColumns ;
   public int final getRows();
  public int final getColumns();
     public void SetDimensions(int r, int c);
  };

a. How many methods are accessors? How many are mutators? How many are constructors?
b. Show Three different ways of creating a matrix.
c. Using the available methods only, write new methods to find the
       a. A boolean method that returns true if the matrix is symmetric (i.e M[i][j]=M[j][i])
       b. A boolean method to return true if the matrix is square.

================================
4. What relationship is satisfied for class composition? Class inheritance?
5. If passenger class object is contained in a elevator class, does elevator class
    has access to all the methods (private and public) of the passenger class?
6. "A Meal has a vegetable", which class contains an object from the other class?
7. Explain the code:  OnePerson(int I, string n){id=i; name=n;}

FIND SYNTAX/LOGIC ERRORS
1. Find ALL syntax errors of the following code:
    if  facingnorth() )
     {  turnLeft() }
    else;
      {  Move() ; }

2. Find all syntax errors
     while  (frontIsClear)
        { move();}

3. find redundant code
   if  (i < 1)
     { move(); }
   else
      if  (i >=1)
        { turnLeft();}
      else
         { pickBeeper();}
 
 


 
 
 

©Copyright 2001 Ananda Gunawardena. All rights reserved.

This document was last updated on 08/15/01 23:22:56