Docsity

Final Exam Questions to Resolution - Data Structures | CS 310, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
A Previous CS 310 Final Exam
July 20, 2004
Print Your Name:
Read the following now.
•You have 100 minutes to work on 9 questions, totaling 200 points.
•To ensure partial credit, show all your work!
•Write your name on all pages of the exam.
•Write down your answers clearly. I reserve the right to take off points due to poor
writing or English structures.
•One blank page is provided at the end for your convenience.
STOP! Do not turn to the next page until instructed to do so.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Final Exam Questions to Resolution - Data Structures | CS 310 and more Exams Data Structures and Algorithms in PDF only on Docsity!

A Previous CS 310 Final Exam

July 20, 2004

Print Your Name:

Read the following now.

  • You have 100 minutes to work on 9 questions, totaling 200 points.
  • To ensure partial credit, show all your work!
  • Write your name on all pages of the exam.
  • Write down your answers clearly. I reserve the right to take off points due to poor writing or English structures.
  • One blank page is provided at the end for your convenience.

STOP! Do not turn to the next page until instructed to do so.

  1. (20 points in total) Consider the array representation of a maximum heap:

(a) (10pt) Show the heap as a tree after inserting 99.

(b) (10pt) Starting from the original heap, show the heap as an array after removing the top element, 85.

  1. (35 points in total) Consider the C++ classes below.

class X { int a, b; X() {a=10; b=256;} virtual void f () = 0; void g () {a *= 2;} };

class Y : public X { Y() : X() {} // the a and b of Y are initialized // by the constructor of X f () {b /= 2;} };

class Z : public Y { Z() : Y() {} // the a and b of Z are initialized // by the constructor of Y, hence by // the constructor of X f () {b /= 4;} g () {a *= 4;} };

class W : public X { int c; W() : X() {c=27} // the a and b of W are initialized // by the constructor of X f () {b /= 8;} h () {c /= 3;} };

Y y; Z z; W w; X *x_ptr = &z;

(a) (5pt) Name the base class(es) in the above code.

(b) (5pt) Explain abstract class in two sentences or less.

(c) (2pt) Name the abstract class(es) in the above code.

(d) (6pt) Give the values of y.a and y.b after the execution of y.f() and y.g().

(e) (6pt) Give the values of z.a and z.b after the execution of z.f() and z.g().

(f) (6pt) Let us reinitialize z, that is, z.a=10 and z.b=256. Give the values of z.a and z.b after the execution of x ptr->f() and x ptr->g().

(g) (5pt) Circle the C++ statements below that are illegal.

  • w = y;
  • z = y;
  • y = z;
  • x ptr = new W;
  • x ptr = new X;
  1. (25 points in total) Consider a class that maintains the academic records of students.

struct StuRecord { string name; // including both first and last names float avg_gpa; }; struct ListNode { StuRecord stu_rec; ListNode* next; }; class Students{ public: add (string name, float avg_gpa); update_gpa (string name, float new_gpa); remove (string name); private: ListNode* head; // a pointer to the first ListNode };

Our goal is to develop an outstanding-students-iterator, called OSI, which enumerates all students with GPA above a given threshold. For example, the following loop will print the names of all GPA-3.75-and-above students in a Students object stu.

for (OSI p=stu.first_os(3.75); p!=stu.end_os(); ++p) cout << (*p).name << ‘‘\n’’;

(a) (8pt) Declare the data members of class OSI. Don’t worry about constructors, destructors, and any other method. class OSI { private: // declare your data members here

pulbic: // constructors/methods here; not required in your answer };

(b) (7pt) Implement the deference operator of class OSI.

StuRecord& OSI::operator* () {

(c) (10pt) Implement the pre-increment operator of class OSI.

OSI OSI::operator++ () {

  1. (20 points in total) Perform Dijkstra’s shortest path algorithm on the directed graph shown below, using 7 as the starting point.

23

10

6

3

4

4 5

2

8 8

18

24

10

20

(a) (5pt) Show the initial contents of the dist and pred arrays.

(b) (15pt) Show the contents of the dist and pred arrays after the completion of 4 iterations.

  1. (20 points in total) Perform Kruskal’s minimum spanning tree algorithm on the graph shown below. Consider the first four edges that are admitted to the spanning tree.

23

10

6

3

4

5

2

8

18

24

10

20

(^7 )

  • (5pt) Identify the first edge, and show the trees as defined by the parent relation- ship of vertices after the selection of the first edge.
  • (5pt) Identify the second edge, and show the trees as defined by the parent rela- tionship of vertices after the selection of the first edge.
  • (5pt) Identify the second edge, and show the trees as defined by the parent rela- tionship of vertices after the selection of the first edge.
  • (5pt) Identify the forth edge, and show the trees as defined by the parent rela- tionship of vertices after the selection of the first edge.