CS 99

Summer 2001                                                                               8.01

 

 


Final Exam Sample Questions

 

The final exam concentrates on what we have covered since Prelim 2 (up to and including Lab 9) but may also include material covered by Prelim 1 and 2.  The sample questions below are generic, i.e., no attempt has been made to illustrate questions that might refer to the labs specifically.

 

Methods

 

1.       Write a method that takes an integer value and returns the number with its digits reversed.  For instance, given the number 7631, the method should return 1367.  If the number ends in a 0, or several zeros, the method should return a number shorter than the original. So if the user enters 3290, the method will return 923.

/* Returns a number whose digits are the reverse of the parameter n */
static int reverse( int n ) {


}

2.       Write a method that accepts a String and returns the number of punctuated sentences contained within it. For the purposes of this method, a punctuated sentence is one that ends in a period, question mark, or exclamation point.

// return the number of punctuated sentences in String s
static int numOfSentences( String s ) {


}

 

 

3.       Recall that a "perfect" number equals the sum of its divisors, including 1 but excluding itself.  Complete the implementation of the static boolean method isPerfect below.

// return true if p is a "perfect" number; false otherwise
static boolean isPerfect( int p ){


}

 

4.       Write a method the accepts a String and returns the length of the longest word contained within it.

// return longest word in String s
static int longestWord( String s ) {

}

 

5.       Write a static method consec( int n ) that returns true if the four consecutive integers starting with n are divisible by 5, 7, 9, and 11 respectively.

static boolean consec( int n ) {


}

 

 

Tracing loops

 

6.       Trace the sequence of assignments to the variables made by the program segment given below.  The first three assignments have been done for you.  You may not need all the columns provided.

int n = 57;
int x = 0;
int y = 1;
while ( n != 0 )

          if ( n%2 != 0 )
              n = n/3;

     else {
              n = n + 1;
              x *= 9;
              y += 10; 
          }

n:

57

 

 

 

 

 

 

 

 

 

 

 

 

 

x:

 

0

 

 

 

 

 

 

 

 

 

 

 

 

y:

 

 

1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7.       Trace the sequence of assignments to the variables made by the program segment given below.  You may not need each column. 

int p = 32;
int d = 3;
int n = 5;
while ( n < 10 ) {

          p = 32/d;

          do {
              if ( n%d > 1 )
                   n++;
          } while ( n%d != 0 );

d*=2;
}

p:

32

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

d:

 

3

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n:

 

 

5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Classes & Objects

 

8.       Fill in the blanks and methods in the class below.

class Rectangle {

    __________  int length, width;

    // Default constructor

          _________________ (  _______________ ) {



          }

          // Constructor
          _________________ ( _________________________________) {



          }

          public ______  area() {

              return ______________________________;

          }

 

          public _______ perimeter() {

              return _______________________________;

          }

          public ___________ setLength( __________________ ) {


          }

          public ___________ setWidth( ____________________ ) {


          }

 

          public ___________ getLength( ____________________ ) {


          }

          public ___________ getWidth( ____________________ ) {


          }


     }

 

9.       Write code to do the following.  Name the variables whatever you would like.

1. Create a TokenReader object to read in input from the keyboard.
2. Read in 2 integer input values from the user and use those values to instantiate a
     Rectangle with the width as the first value and the length as the second.
3. Create a new Triangle object with sides 9, 9, 9.
4. Print out the perimeter of the rectangle if its area is greater than the triangle's.

______________________________________________; // create TokenReader  object
______________________________________________;  // read in first value
______________________________________________;  // read in second value
______________________________________________; // create Rectangle
______________________________________________; // create Triangle
______________________________________________;  // Print out perimeter
______________________________________________;
______________________________________________;

 

Star Patterns

 

Use only the following types of print statements in the questions below.

10.   Write a program segment that reads in the size of a square and then prints a square of that size out of asterisks with a diagonal line across the middle.  So if your program takes in an input of 5, it should print:

* * * * *
* *      *
*   *    *
*     *  *
* * * * *

11.   Write a program segment that reads in a non-negative integer n and a triangle alternating  asterisks and @’s on each line.  Hence, if the program takes in an input of 6, the following pattern should be printed:

*
@@
***
@@@@
****
@@@@@
******

12.   Write a program segment that reads in a non-negative integer n and prints a checkerboard pattern with @’s and asterisks.  If the program takes in as input 3, it will print the following pattern:

@*@
*@*
@*@