CS161 Final Exam Review, Fall 2015

Loop Invariants

Consider the following snippet of code that computes the quotient of x and y (x / y) and the remainder obtained by dividing x by y (x % y):

int r = x;
int q = 0;
while (y <= r) {
   r = r - y;
   q = 1 + q;
}

Use loop invariants to show that this code computes the quotient and remainder correctly.

Recursion

Implement a recursive method that accomplishes the above calculation for quotient and remainder and prints their values when done.

Sorting

Apply Insertion Sort, Selection Sort, and Bubble Sort to the following array of numbers. Show the numbers after each iteration of the outer loop.

6 9 3 8 10 1

What do the methods have in common in terms of making progress in sorting an array? What is the difference between the loop invariant of selection sort and the loop invariant of insertion sort?

You are writing a class called Student that stores student information. You would like to sort an array of Student objects, and someone gave you a method for sorting that has the signature public void sort(Comparable [] array). What do you need to do in order to use this method to sort the Student objects using this method?

Interfaces

Write an interface called CurrencyConversionInterface that provides functionality for currency conversion from one currency to another. Assume that a currency is represented by a String which is the name of the currency. Document what each method in the interface should do.

Inheritance

super versus this

overriding

polymorphism

private, public, protected

static

Let's consider a Java class that represents a Rectangle class and a Square class that extends it.

Why did we choose Rectangle as the parent class? Why not choose Square as the parent?

Complete the implementation of the two classes.

Can you assign a rectangle to a square? Can you assign a square to a rectangle? Explain!

public class Rectangle {
   private int x, y;
   private int width, height;

   public Rectangle(int height, int width, int x, int y) {
      this.x = x;
      this.y = y;
      this.height = height;
      this.width = width;
   }

   public int area(){

   }
}

public class Square extends Rectangle {
   public Square (int width, int x, int y) {


   }
}

Iterators

What is the purpose of an Iterator?

Is an Iterator a class, an abstract class, or an interface?

What methods must an Iterator implement?

Mathematical Induction

Prove

13 + 23 + 33 + ... + n3 = n2(n + 1)2 ⁄ 4

Prove

3n > n2

What is strong induction?

Counting

Sum and product rules

Inclusion-exclusion

Pigeon-hole principle

combinations and permutations

How many possible license plates are there among two states, if one state's license plates are three digits followed by three capital letters and the other state's license plates are two digits followed by three capital letters or four digits followed by one capital letter?

How many passwords of length 5 can be made from all lower case letters if no letters are repeated?

All of your friends are between 10 and 30 years old. How many of them must you ask their age to guarantee that at least two of them are the same age?

Linked Lists

Node class

head reference

Draw a linked list that represents this list of strings: "do" "not" "panic"

Write a single java statement that assigns a reference to the node containing "panic" to a variable named ref.

Write java statements to remove "not" from this list.