EECE 330 Exam, Spring 2008
American University of Beirut · Data Structures and Algorithms · 7 pages
More EECE 330 files
- Midterm · Fall 2014ExamView
- Midterm With Solutions · Fall 2013Exam · with solutionView
- Midterm Sample Questions · Fall 2011ExamView
- Questions Midterm Sample · Fall 2011ExamView
- Exam 2 · Fall 2009ExamView
- Exam 1 · Fall 2009ExamView
- Exam 1 · Spring 2008ExamView
- Exam 1 with Answers · Spring 2008Exam · with solutionView
Show the text of this exam
1)
Quiz 1:
(Astack) push function is modified to:
bool push(const Elem& item) {
if (top == size) return false; // Stack is full
else { listArray[++top] = item; return true; }
}
What is the second line displayed by this code?
((0),1,2,3,none of the above)
stack s;
for(int i = 0; i < 4 ; i++)
s.push(i);
int j;
for(i=0 ; i < 4 ; i++)
{ s.pop(j);
cout << j << endl ;
}
2)
Final:
(Astack) pop function is modified to:
bool pop(Elem& it) { // Pop top element
if (top == 0) return false;
else { it = listArray[top--]; return true; }
}
What is the second line displayed by this code?
(0,1,2,3,none of the above)
stack s;
for(int i = 0; i < 4 ; i++)
s.push(i);
int j;
for(i=0 ; i < 4 ; i++)
{ s.pop(j);
cout << j << endl ;
}
3)
Quiz 1:
(LStack) push function is modified to:
bool push(const Elem& item) {
if(top==NULL) top = new Link<Elem>(item, NULL);
else top->next = new Link<Elem>(item, top->next);
size++;
return true;
}
What is the second line displayed by this code?
(0,1,2,3,none of the above)
stack s;
for(int i = 0; i < 4 ; i++)
s.push(i);
int j;
for(i=0 ; i < 4 ; i++)
{ s.pop(j);
cout << j << endl ;
}
4)
(LQueue) dequeue is modified to:
bool dequeue(Elem& it) { // Remove Elem from front
if (size == 0) return false; // Empty
it = front->element; // Store dequeued value
Link<Elem>* ltemp = front; // Hold dequeued link
front = front->next; // Advance front
delete ltemp; // Delete link
size --;
return true; // Return element value
}
What is the second line displayed by this code?
(0,1,2,3,none of the above)
Queue q;
for(int i = 0; i < 4 ; i++)
q.enqueue(i);
int j;
for( i = 0; i < 4 ; i++)
q.dequeue(j);
for(i = 0; i < 4 ; i++)
q.enqueue(i);
int j;
for( i = 0; i < 4 ; i++){
q.dequeue(j);
cout << j << endl ;
}
5)
template <class Elem>
void traversal(BinNode<Elem>* subroot)
{
if (subroot == NULL) return;
traversal(subroot->right());
visit(subroot);
traversal(subroot->left());
}
How will the following BST be traversed?
6)
template <class Elem>
int count(BinNode<Elem>* subroot)
{
if (subroot == NULL)
return 1;
return count(subroot->left()) + count(subroot->right());
}
cout << tree.count();
What number will the above code display?
7)
#include <iostream>
#include <string>
#include "BST2.h"
using namespace std;
class Employee
{
public:
int ID;
string name;
Employee(int i=0, string n="undefined"){ID=i;name=n;}
};
ostream& operator<< (ostream& osObject, const Employee& c)
{osObject<<"("<<c.ID<<", " <<c.name<<")";
return osObject;}
class IDCompare
{
public:
// static functions so they can be used without
// having an instantiated object
static bool lt(const Employee& x, const Employee& y)
{ return x.ID < y.ID; }
static bool gt(const Employee& x,const Employee& y)
{ return x.ID > y.ID; }
static bool eq(const Employee& x, const Employee& y)
{ return x.ID == y.ID; }
};
class NameCompare
{
public:
static bool lt(Employee& x, Employee& y)
{ return x.name< y.name; }
static bool gt(Employee& x, Employee& y)
{ return x.name> y.name; }
static bool eq(Employee& x, Employee& y)
{ return x.name== y.name; }
};
class IDEmpCompare
{
public:
// static functions so they can be used without
// having an instantiated object
static bool lt(int i,Employee& x )
{ return i<x.ID ; }
static bool gt(int i,Employee& x )
{ return i> x.ID; }
static bool eq(int i,Employee& x )
{ return i==x.ID ; }
};
void main()
{Employee e;
BST <int,Employee,IDEmpCompare, IDCompare> EmpDict1;
EmpDict1.insert(Employee(4,Ali));
EmpDict1.insert(Employee(3,David));
EmpDict1.insert(Employee(1,George));
EmpDict1.insert(Employee(2,Dany));
EmpDict1.insert(Employee(5,Maria);
EmpDict1.print();
}
If print() prints names, what will be the second line of this code?
8)
void main()
{
BST <int,Employee,IDEmpCompare, NameCompare> EmpDict1;
EmpDict1.insert(Employee(4,Ali));
EmpDict1.insert(Employee(3,David));
EmpDict1.insert(Employee(1,George));
EmpDict1.insert(Employee(2,Dany));
EmpDict1.insert(Employee(5,Maria);
Employee e1(4,Ziad);
Employee e;
EmpDict1.remove(4,e);
EmpDict1.insert(e1);
EmpDict1.print();
}
If print() prints the names, what will be the second line of this code?
9)
The following array is used to build the heap.
[10,100,65,9,28,10,44,14,51,63,62,73,32]
The resulting heap array is
[100,63,73,51,62,65,44,14,9,28,10,10,32]
10)
Important from previous Ali Hajj:
What will be the bits per letter for the following Huffman coding
Letter Freq
A 20
M 30
G 1
D 5
a) 1.26
b) 1.69
c) 1.88
d) 1.57
11)
Also important from previous Ali Hajj:
Suppose that we have numbers between 1 and 1000 in a binary search tree and want to
search for the number 363. Which of the following sequences could not be the sequence
of nodes examined?
a.
2, 252, 401, 398, 330, 344, 397, 363.
b. 924, 220, 911, 244, 898, 258, 362, 363.
c.
925, 202, 911, 240, 912, 245, 363.
d. 2, 399, 387, 219, 266, 382, 381, 278, 363.
e.
935, 278, 347, 621, 299, 392, 358, 363.
Or asked in the following manner:
Which of the following is a binary tree traversal from root downwards?
f.
2, 252, 401, 398, 330, 344, 397, 363.
g. 924, 220, 911, 244, 898, 258, 362, 363.
h. 925, 202, 911, 240, 912, 245, 363.
i.
2, 399, 387, 219, 266, 382, 381, 278, 363.
j.
935, 278, 347, 621, 299, 392, 358, 363.