代做CS 138、C++编程设计代写
CS 138 - Sample Final Exam
• The exam is 150 minutes long.
• Please read all instructions carefully.
• There are 4 questions on this exam, some with multiple parts. This exam will be graded out of a
maximum of 104 points.
• The exam is a closed book and notes. You are not permitted to access any electronic devices during
the duration of the exam. You are not allowed to consult another person to find out the answers.
Do not open the exam book without the proctor’s permission. Please make sure to sign the exam
book. Please do not talk among yourselves during the exam. If you have any questions, consult the
proctor. Appropriate university policies will apply if you are caught cheating by the proctor.
• Please write your answers in the appropriate space provided in your respective exam books. Please
make sure to write your names and IDs.
• Solutions will be graded on correctness, clarity, completeness and brevity. Most problems have a
relatively simple solution. Partial solutions may be given partial credit.
• Follow the instructions given by the proctor throughout the exam. If you need to step out of the
exam hall for washroom visits, then please talk to the proctor.
NAME:
Email ID:
Student ID:
In accordance with the letter and the spirit of the University of Waterloo honor code, I pledge
that I will neither give nor receive assistance on this examination.
SIGNATURE:
Problem Max points Points Received
Q1 20
Q2 30
Q3 30
Q4 24
Total 104
1
Question 1: True/False questions (20 points)
·Please specify if the following assertions are True or False. Each sub-question is worth 2 points. For
each question you answer False, justify to get full points.
1. Virtual functions in C++ can be static.
Answer:
2. Hash functions are deterministic, meaning the same input will always produce the same hash code.
Answer:
3. An abstract class in C++ can have both concrete (non-pure virtual) and pure virtual functions.
Answer:
4. C++ allows for function overloading based solely on return type.
Answer:
5. Hash functions must be invertible, allowing the original data to be recovered from its hash code.
Answer:
6. Initializer lists allow for uniform initialization syntax in C++, regardless of whether you are initializing a built-in type or a user-defined type.
Answer:
7. Static methods can be used to modify the state of a static field directly without needing an instance
of the class.
2
Answer:
8. Instantiating a template with a user-defined type requires that the type has specific methods or
behaviors defined.
Answer:
9. The end iterator in C++ points to the last element of a container, allowing direct access to that
element.
Answer:
10. Doubly linked lists guarantee constant-time access to elements at arbitrary positions due to their
bidirectional nature.
Answer:
3
Question 2: Short Answer Questions (30 points)
For each of the sub-questions below, provide a concise, correct, and complete answer. Each sub-question
is worth 5 points.
1. What is the difference between a const pointer and a pointer to a const variable?
Answer:
2. 1: #include <iostream>
2: #include <string>
3: using namespace std;
4:
5: class Vehicle {
6: public:
7: Vehicle();
8: Vehicle(string type);
9: virtual ~Vehicle();
10: void displayType() const;
11: private:
12: string type;
13: };
14:
15: // Method definitions
16: Vehicle::Vehicle() {
17: this->type = "car";
18: }
19:
20: Vehicle::Vehicle(string type) {
21: this->type = type;
22: }
23:
24: Vehicle::~Vehicle() {}
25:
26: void Vehicle::displayType() const {
27: cout << "This is a " << this->type << endl;
28: }
29:
30: int main(int argc, char* argv[]) {
31: Vehicle car {"sedan"};
32: car.displayType();
4
33:
34: Vehicle bike {};
35: bike.displayType();
36:
37: Vehicle* bus = new Vehicle {"bus"};
38: bus->displayType();
39:
40: Vehicle* ptr = bus;
41: ptr->displayType();
42: ptr->type = "truck";
43: ptr->displayType();
44:
45: delete ptr;
46: delete bus;
47:
48: return 0;
49: }
The provided code crashes when executed. Why? Explain your answer. Be specific about where
the problem(s) sites and what exact error(s) will you get.
Answer:
5
3. #include <iostream>
class Circle {
public:
double radius;
double area();
};
double Circle::area() {
return 3.14159 * radius * radius;
}
int main() {
Circle myCircle;
myCircle.radius = 5.0;
std::cout << "The area of the circle is: " << myCircle.area() << std::endl;
return 0;
}
The Circle class does not have a constructor. Do you think this code will execute ? Explain your
answer.
Answer:
4. class Balloon {
public:
...
Balloon (); // Default ctor
Balloon (string shellColour);
Balloon (string c, int size);
Balloon (int i, string c);
...
};
int main (...) {
Balloon rb {"red"};
6
Balloon rbc1 {rb};
}
Will the last line of the main function execute correctly (note that a copy constructor is not defined)?
Answer:
7
5. What are the advantages of using the heap?
Answer:
6. What is the significance of BSTs in terms of the complexity of insertion, deletion and search?
Answer:
8
Question 3 (30 points)
For each of the sub-questions below, provide a concise, correct, and complete answer. Each of the following sub-questions below is worth 6 points.
In class, we learned about different STL container classes. Suppose this time, we want to create our
own implementation of these classes but with some OO inheritance hierarchy. We start with an Abstract
Base Class Sequence for all sequence containers, and a concrete child class Vector. Internally, Vector
uses a dynamic array to store the elements, with an additional field capacity representing this dynamic
array’s size. The field size indicates how many slots are actually being used in the array.
class Sequence {
private:
int size;
protected:
Sequence(): size {0} {}
void setSize(int size) { this -> size = size; }
public:
virtual ~Sequence() {}
virtual string& at(int index) = 0;
virtual void push_back(const string& item) = 0;
virtual void pop_back() = 0;
int getSize() const { return size; }
};
class Vector: public Sequence {
private:
string* theArray;
int capacity;
void increaseCapacity();
public:
Vector();
~Vector();
virtual void push_back(const string& item) override;
virtual void pop_back() override;
virtual string& at(int index) override;
};
string& Vector::at(int index) {
9
if(index >= 0 && index < getSize()) {
return theArray[index];
}
cerr << "Error: Out of bounds" << endl;
assert(false);
}
1. We want our Vector to be able to change its capacity dynamically. To achieve this, Implement
a private helper method increaseCapacity() that allocates a new dynamic array with double
the original capacity, copies the contents of the original array to the new array, replaces the old
array with the new array, and finally disposes of the old array. You may assume the preconditions
capacity > 0 and capacity == size.
Answer:
2. Implement the push back() and pop back() methods for Vector. Both of these methods should
update the field size. When the Vector is full, push back() should call increaseCapacity() before pushing the new item. You don’t need to shrink the capacity in pop back(). You may assume
your increaseCapacity() is implemented correctly.
Answer:
10
3. The implementation of Vector::at() performs bound checking before returning the item at the
given index. We want to perform the same bound checking for all future child classes of Sequence,
but that would require us to implement bound checking for every new child class. We can save this
effort by using the Template Method design pattern:
class Sequence {
private:
// ...
virtual string& getItemAt(int index) = 0; // virtual helper method
protected:
// ...
public:
// ...
string& at(int index); // template method
};
class vector: public Sequence {
private:
// ...
virtual string& getItemAt(int index) override;
public:
// ...
};
We can do the same for push back() and pop back(), but we will leave them as they are for now.
Implement the template method Sequence::at() and the new helper method Vector::getItemAt()
such that calling Vector::at() has the same behaviour as the original.
Answer:
11
4. Let’s implement a new concrete subclass List that uses a linked list.
class List: public Sequence {
private:
struct Node {
string val;
Node* next;
};
Node* head;
virtual string& getItemAt(int index) override;
public:
// ...
virtual void push_back(const string& item) override;
virtual void pop_back() override;
};
Implement the methods of List. You can choose to let the field head point to the “front” or the
“back” of the linked list, as long as you keep it consistent among your methods. You don’t need to
implement the constructor and the destructor. Your implementation shouldn’t leak any memory.
Answer:
5. Now that we have some Sequence classes, let’s use them to implement something else. We can use
the abstract class Sequence to implement a Stack:
template <typename T> class Stack {
private:
12
Sequence* theStack;
public:
Stack(): theStack { new T{} } {}
~Stack() { delete theStack; }
void push(const string& value);
void pop();
string top() const;
bool isEmpty();
};
Note that assigning new T to theStack in the constructor forces T to be a concrete sub-type of
Sequence. (We will assume that all subclass of Sequence has a default constructor.)
Implement the remaining methods. Since Sequence::at() already does bound checking, you don’t
need to do it again when you use it here. You may also assume that T::pop back() will abort via
assertion if the Sequence is empty.
Answer:
13
Question 4 (24 points)
For each of the sub-questions below, provide a concise, correct, and complete answer. Each of the following sub-questions below is worth 6 points.
In this question, we will start from an abstract base class Sequence and extend it to a Deque (doubleended queue). A deque is a more complex sequence container that allows insertion and removal of elements
from both the front and the back. For this implementation, internally, Deque will utilize a dynamic array
to manage its elements, similar to Vector, but with the capability to efficiently add or remove elements
at both ends. Starting with the Sequence abstract base class, we will focus on implementing the Deque
class with the necessary modifications to support dynamic resizing and double-ended operations.
class Sequence {
private:
int size;
protected:
Sequence(): size {0} {}
void setSize(int size) { this -> size = size; }
public:
virtual ~Sequence() {}
virtual string& at(int index) = 0;
virtual void push_back(const string& item) = 0;
virtual void pop_back() = 0;
int getSize() const { return size; }
};
class Deque : public Sequence {
private:
std::string* theArray;
int capacity;
int front; // Index of the front element
int rear; // Index just past the last element
void increaseCapacity();
public:
Deque();
~Deque();
void push_front(const std::string& item);
void pop_front();
virtual void push_back(const std::string& item) override;
virtual void pop_back() override;
14
virtual std::string& at(int index) override;
};
1. Implementing increaseCapacity() for Deque: To support dynamic resizing, especially when either
front or rear operations exceed the current capacity, you are asked to implement increaseCapacity().
This method is expected to double the capacity of the deque, properly repositioning elements to
maintain the deque’s order. You are expected to place the elements in the original deque at the
center of the new dequeue to account for insertion in both front and rear of the dequeue.
2. Your second task is to implement double-ended operations push front, pop front, push back and
pop back: These methods adjust the class variables front and rear accordingly. They also call
increaseCapacity() when necessary.
Answer:
15
3. Please adjust the at() method for Deque: Given Deque’s dynamic resizing and double-ended nature, its at() method must consider the front index’s offset when accessing elements.
Answer:
16
4. Lastly, proper resource management is crucial, especially for dynamic array allocation. Please implement the constructor and destructor of Deque. Please implement the constructor as no input
parameters but assume the class receives a default value for the deque capacity of 16.
Answer:
请加QQ:99515681 邮箱:99515681@qq.com WX:codinghelp
- WhatsApp营销软件,ws自动筛选工具/ws筛选神器/ws协议号
- Ins群发消息工具,Instagram模拟器群发软件,助你实现营销梦想!
- 创意如梦 业务如画 WhatsApp拉群工具为您的品牌敲开创意的大门
- instagram营销软件,ins群发拉群,天宇爆粉【TG:@cjhshk199937】
- EEE-6512 代写、代做 java/c++编程语言
- 杭州威雅夏校:四大主题营联动英国威科姆阿贝夏校,释放夏日多巴胺!
- 代写CourseManager编程、代做java程序语言
- 新华三助力中国邮政私有云平台建设
- 康郡机电有限公司获得超过4000万美元的投资,以进一步开发其革命性的智能手机成像技术
- 网络专业人士的得力助手:探索WhatsApp协议号注册器的魅力
- 外贸初体验 WhatsApp拉群工具如何在我业务中点燃用户体验的激情之火
- TPS2300IPW: Enhancing Power Management Efficiency for Modern Electronics | ChipsX
- 代做RISC-V、C/C++编程设计代写
- Optimisation代做、代写Optimisation编程设计
- 虚拟财富的笑料梦境:在虚拟财富的梦幻跨境电商 WhatsApp 群发云控世界中发生的科技笑话,会让你捧腹大笑
- Telegram超好用的批量群发营销软件,电报群发工具推荐
- ICL晶体植入术更适合哪些人?北京爱尔英智眼科医院周继红为您解答
- 创新仙方 科技魔法师的奇迹心得 WhatsApp拉群营销工具点亮商业星空
- 赣州装修装饰品牌:打造您的理想家园
- 中国电化铝网:行业先锋,引领电化铝新时代
- instagram新手营销推广必备工具,ins独家自动群发软件
- Telegram/TG群发加速器,电报/TG智能营销软件,TG/纸飞机自助采集群发平台
- instagram群发利器,精准采集用户,助你爆粉引流!
- 自免赛道捷报!国产抗IL-17单抗III期临床数据荣登BJD
- 碧桂园服务助农|心选X 秭归县农业农村局首场溯源圆满收官!
- 代写Programming with Python
- Instagram营销软件购买指南,Ins自动引流推广采集软件一键搞定!
- instagram多功能自动群发引流营销软件,ins出海营销必备神器
- 河北机械设备平台:打造行业新标杆,引领机械装备智能化新篇章
- Ins一键群发工具,Instagram群发群控工具,让你的营销更高效!
推荐
- 苹果罕见大降价,华为的压力给到了? 1、苹果官网罕见大降价冲上热搜。原因是苹 科技
- 疫情期间 这个品牌实现了疯狂扩张 记得第一次喝瑞幸,还是2017年底去北京出差的 科技
- 如何经营一家好企业,需要具备什么要素特点 我们大多数人刚开始创办一家企业都遇到经营 科技
- 升级的脉脉,正在以招聘业务铺开商业化版图 长久以来,求职信息流不对称、单向的信息传递 科技
- 创意驱动增长,Adobe护城河够深吗? Adobe通过其Creative Cloud订阅捆绑包具有 科技
- 老杨第一次再度抓握住一瓶水,他由此产生了新的憧憬 瘫痪十四年后,老杨第一次再度抓握住一瓶水,他 科技
- 智慧驱动 共创未来| 东芝硬盘创新数据存储技术 为期三天的第五届中国(昆明)南亚社会公共安 科技
- 全力打造中国“创业之都”名片,第十届中国创业者大会将在郑州召开 北京创业科创科技中心主办的第十届中国创业 科技
- B站更新决策机构名单:共有 29 名掌权管理者,包括陈睿、徐逸、李旎、樊欣等人 1 月 15 日消息,据界面新闻,B站上周发布内部 科技
- 丰田章男称未来依然需要内燃机 已经启动电动机新项目 尽管电动车在全球范围内持续崛起,但丰田章男 科技