2002examq - Page 1 of 10 A Bunch of SYSC 2002 Exam...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Page 1 of 10 A Bunch of SYSC 2002 Exam Questions from Many Different Exams Question 1 (11 marks) For this question, assume that all necessary #includes are present. a) Given the code below identify three mistakes and briefly explain each problem. [3 marks] line 1: int a, c; line 2: int b, *f, *g; line 3: *f = 2; line 4: f = &b; line 5: *f = 4; line 6: *g = new int; line 7: *f = a; line 8: *f = &c; b) Give the output produced by the following: [2 marks] int *a, b, *c, d, *e; b = 2; d = 1; a = &d; c = &d; a = &b; e = a; *c = 9; *a = 7; cout << *e << " " << d << endl; c) Give the output produced by the code in part d). [4 marks] d) This code has a memory leak. Clearly indicate exactly how this should be fixed. [2 marks] int *f, i, j; IntBag *g; f = new int; g = new IntBag[4]; *f = 8; for ( i=0; i<4; i++ ) { for ( j=0; j<i; j++ ) { g[i].add(j); } } cout << *f << " " << g[2].size() << endl; (*f)--; g = new IntBag[6]; for ( i=0; i<6; i++ ) { for ( j=0; j<4; j++ ) { (*(g+i)).add(5*i); } } cout << *f << " " << g[0].size() << endl; Page 2 of 10 Question 3 (7 Marks) Below you will find an extract from a template Queue class similar to that studied in the lectures. Here the queue is implemented as a ring array. template <class T> class Queue { private: T *ring; int capacity, count, head, tail; public: Queue (int capacity); // creates empty queue (quits if capacity<1) Queue (const Queue &otherQueue); // copy constructor ~Queue (); // destructor void enqueue (T item); // adds item to queue, quits if full T dequeue (); // removes value from queue; quits if empty T look () const; // returns first value in queue; quits if empty int size() const; // returns number of elements in queue }; // end of extract from template Queue class Queue<String2002> q(5); // start of application program fragment q.enqueue("a string"); q.enqueue("another string"); cout << q.look() << "\n"; cout << q.dequeue() << "\n"; q.enqueue("SYSC 2002"); cout << q.size() << "\n"; cout << q.dequeue() << "\n"; q.enqueue("yet another string"); cout << q.look() << "\n"; a) Give the output of the application program fragment above. [3 marks] b) After all the statements above have been executed, show all the fields (member variables) along with their values. This must include a diagram of the ring array, showing all details, including the index of each array element. [4 marks] Question 4 (7 Marks) On the next page you will find an extract from a template Stack class, similar to that studied in the lectures. Here the stack is implemented as a linked list. template <class T> class Stack { private: class SNode { public: // a very open class Page 3 of 10 T data; SNode *next; SNode (T data, SNode *next) { this -> data = data; this -> next = next; } }; // end of class SNode SNode *head; // head pointer for the linked list bool elastic; // true if stack is elastic (has infinite capacity) int capacity; // stack capacity (only meaningful if not elastic) int count; // number of values on stack public: Stack (); // creates empty elastic stack...
View Full Document

Page1 / 10

2002examq - Page 1 of 10 A Bunch of SYSC 2002 Exam...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online