e01 : Midterm Exam-I
num | ready? | description | exam date |
---|---|---|---|
e01 | true | Midterm Exam-I | Wed 04/24 09:30AM |
Instructions for the exam
- You may bring one 8.5” x 11” piece of paper with notes on both sides. This paper will be collected along with your exam, so make your own copy if you would like to keep it.
- No calculators or phones are allowed in the exam
- No other notes or books are allowed
- There are no makeups for this exam
- You must write your name on every single sheet of paper including your notes
- We will provide you with scratch paper
- We will provide you the table of operator precedence
Collaborative notes
A fun way to prepare for the exam is to work with your classmates to create notes collaborative. I have created a document with the above list of topics. Jump right in and add content, code examples and other useful information. I will review and comment your notes as will the tutors and TAs! Students who contribute will get star points :)
Click here to contribute to Collaborative Notes
Review problems
Solutions available HERE
1) Given the head to a linked list, write a function to determine if there’s a cycle. (A cycle appears when a Node’s next pointer points to a Node previously visited)
2) Write two functions to return the sum of a linked list’s values - one iterative and one recursive.
3) Find and explain any errors in this class definition:
class Student{
public:
Student(string name, int ID, double gpa){
this.name = name;
ID_number = ID;
GPA = gpa;
}
string getName() const{ return name;}
double getGPA() const{ return GPA;}
private:
string name;
double GPA;
const ID_number;
};
4) The StudentList class represents this year’s graduating class. Implement getAvgGPA() function. This should return the average GPA of the graduating class. Implement the getValedictorianName() function. The valedictorian is the student with the highest GPA. If there are multiple students with the highest GPA, return any one of their names.
class StudentList{
public:
StudentList(const StudentList& source);
StudentList();
~StudentList();
string getValedictorianName() const; // implement this
double getAvgGPA() const; // implement this
private:
class Node{
public:
Node();
Node(Student& s);
Student data;
Node* next;
};
Node* head;
Node* tail;
};
5) OutFit is a class that has objects of type ClothingItem as private member variables. (1) Implement the constructor for the OutFit class. (Keep in mind private member variables of a given class may only be accessed directly within that class’s member functions).
class ClothingItem{
public:
ClothingItem();
~ClothingItem();
string getBrand() const;
string getColor() const;
int getSize() const;
void setBrand(string BrandOfItem);
void setColor(string ColorOfItem);
void setSize(int SizeOfItem);
private:
string brand;
string color;
int size;
};
class OutFit{
public:
OutFit(ClothingItem Top, ClothingItem Bottoms); //Implement
~OutFit();
friend bool operator==(const OutFit& FirstOutfit; const OutFit& SecondOutfit); //implement
private:
ClothingItem tShirt;
ClothingItem Pants;
};
6) Looking at the above classes, implement the == operator used to check if two outfits are the same. (Two outfits are considered the same if they have the same tShirt and same bottoms, two tShirts and two bottoms are considered the same if they are of the same brand, color, and size).
7) IntList is a class that defines a LinkedList of integers. Write a non-recursive implementation of the reverse() function that reverses the order of the linked list. The class definition is given below.
class IntList {
public:
// constructor and 3 methods already done in intlist.cpp (NO CHANGE):
IntList(); // constructor
void append(int value); // append value at end of list
void reverse(); // function to implement, reverse the linked list
~IntList(); // destructor
private:
Node* reverse(Node *node); // helper function for reverse(), returns the new head node
// definition of Node structure (DO NOT CHANGE):
struct Node {
int info;
Node* next;
};
Node* first; // pointer to first node (DO NOT CHANGE):
};
8) Write the reverse() function again, but this time recursively. You will likely need to use the private helper function to accomplish this. The private helper function returns a pointer to a Node, so try using it to return the last node (which will be the new first node in the list)
9) Consider the following definition of the class LinkedList:
class LinkedList {
public:
LinkedList(){head = 0; tail = 0;} //constructor
~LinkedList(); //destructor
void append(int value); //already implemented, takes an integer value and adds a Node with that value to the end of the linked list
private:
class Node{
int data;
Node *next;
}
Node *head;
Node *tail;
void clear(Node *h); // Recursively deletes all the nodes starting at h
};
Implement the private method clear(Node *h)
10) Using the LinkedList definition from above, describe what happens when this code is run. Draw a pointer diagram if you need to.
LinkedList l1;
l1.append(1);
l1.append(2);
l1.append(3);
LinkedList l2(l1);
Solutions available HERE
Reference material
You are expected to know the material from the following lectures, labs and homeworks:
- Lecture 1 to lecture 7, including all code implemented in class
- Homework 1 to 3
- Labs 0 to 3
Study guide
- Converting procedural code to OOP
- Basic design of classes - member variables and methods. Corrrect declaration of accessors and modifiers and the appropriate use of the const keyword
- Distinguishing between class vs.instance
- Understanding how to create instances of a class on the stack and heap
- Member functions and non-member functions - why and how
- Operator overloading - why and how
- The big 4- Constructor, de-constructor, copy-constructor, assignment operator (default behavior, overloaded implementation and identifying when each is invoked in any given code)
- Linked-lists - you may be asked to implement any function that involves a linked list
- The big 4 for linked-lists - why and how to overload the c’tor, de-stor, copy-c’tor and copy assignment
- Unit testing classes
- Recursion on linked lists: you should be able to do any of the problems in lab02 recursively
Past exams
Link to the past exam is available on gauchospace.