e01 : Midterm Exam
num | ready? | description | exam date |
---|---|---|---|
e01 | true | Midterm Exam | Wed 11/08 12:30PM |
Instructions for the exam
- Exam will be in person during lecture time
- The exam will be closed book, no laptops or electronic devices are allowed.
- Posting the exam questions online or collaborating with anyone is a serious violation of academic integrity. Seeking or giving help to others will result in an F in the course.
- Seating will be assigned, please look for an announcement on Piazza. I will post a link to the seating chart here a day before the exam.
Instructor and TA prepared Review problems
Please note that the review problems are not an exhaustive list. These are just a sample of the material for the exam.
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
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
- Constructor (different forms and why they are needed)
- The big 3- Destructor, 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
- Unit testing classes
- Binary search trees (all operations covered in class)
- Recursion on linked lists and BST: you should be able to do any of the problems in lab04 recursively
- The big 3 for linked-lists and BST - why and how to overload the de-stor, copy-c’tor and copy assignment
Past exams
Past exams available on gauchospace. Again, please don’t use these as your only source of preparing for the exam.