Starting from:

$25

COEN79 Quiz Practice Solved

Practice questions for Quiz 4 


1.    Write the pseudo-code of an algorithm which evaluates a fully parenthesized mathematical expression using stack data structure. (calculator). 


 

2.    For the queue class given in Appendix 1 (cf. end of this assignment), implement the copy constructor.  

Note that the class uses a dynamic array. Also please note that you should not use the copy function (copy only the valid entries of one array to the new array).  

 

1.   template <class Item>  

2.    queue<Item>::queue (const queue <Item>& source)  
     

3.   queue class given in Appendix 1 (cf. end of this assignment), implement the following function, which increases the size of the dynamic array used to store items. Please note that you should not use the copy function (copy only the valid entries of one array to the new array). 

 

4.   deque class given in Appendix 2 (cf. end of this assignment), implement the following constructor. The constructor allocates an array of block pointers and initializes all of its entries with NULL. The initial size of the array is init_bp_array_size. 

 

1.             template < class Item >  

2.              deque<Item>::deque( int init_bp_array_size, int init_block_size )  

3.             {   

4.             bp_array_size = init_bp_array_size;   

5.             block_size = init_block_size;  

       


5. deque class given in Appendix 2 (cf. end of this assignment), write the full implementation of the following function. 

 

     

1.        template <class Item>   

2.        void deque <Item>::pop_front()   

3.        // Precondition: There is at least one entry in the deque  

4.        // Postcondition: Removes an item from the front of the deque     

5.        {         

6.         assert(!isEmpty());            

6. What are the outputs of the following programs? 

 

 

 

 

 

 

 

 

1.   #include < iostream >  

2.   using namespace std;   

3.   
4. class Base {   
5.             private:       

6.             int i, j;   
7.             public:      

8.             Base (int _i = 0, int _j = 0): i(_i), j(_j) {}   
9. };   10.  
11. class Derived: public Base {   
12.            public:       

13.            void show() { cout << " i = " << i << "  j = " << j;   }   
14. };   15.  
16. int main(void) {     
17.     Derived d;     
18.     d.show();     
19.     return 0;   
20. }   

       

1.       #include < iostream > 

2.       using namespace std;   3.  
4. class P {   
5.             public:     

6.             void print()  {   
7.         cout << " Inside P";   
8.     }   
9. };     10.  
11. class Q: public P {   
12.            public:     

13.            void print() {   
14.         cout << " Inside Q";   
15.     }   
16. };     17.  
18. class R: public Q {};     19.  
20. int main(void) {     
21.     R r;     
22.     r.print();     
23.     return 0;   
24. }   

  

1.       #include < iostream >  

2.       using namespace std;    3.  
4. class Base {};    5.  
6. class Derived: public Base {};    7.  
8. int main() {       
9.     Base * bp = new Derived;       
10.     Derived * dp = new Base;   
11. }   

 

Appendix 1: queue class declaration: 

 

1. template < class Item >  
 

Appendix 2: deque class declaration: 

 

1. template < class Item >  2. class deque {   

3.        public:  

4.        // TYPEDEF  

5.        static const size_t BLOCK_SIZE = 5; // Number of data items per block   6.  

 7.     // Number of entries in the block of array pointers. The minimum acceptable value is 2 8.     static const size_t BLOCKPOINTER_ARRAY_SIZE = 5;  9.  

10.     typedef std::size_t size_type;   11.     typedef Item value_type;   

12.  

13.                                        deque(int init_bp_array_size = BLOCKPOINTER_ARRAY_SIZE,  

14.                                        int initi_block_size = BLOCK_SIZE);   

15.  

16.     deque(const deque & source);   17.     ~deque();  

18.  

19.       // CONST MEMBER FUNCTIONS 

20.       bool isEmpty();   

21.       value_type front();   22.     value_type back();  

23.  

24.       // MODIFICATION MEMBER FUNCTIONS   

25.       void operator = (const deque & source);   

26.       void clear();   

27.       void reserve();   

28.       void push_front(const value_type & data);   

29.       void push_back(const value_type & data);   

30.       void pop_back();   31.     void pop_front();   

32.  

33. private:  

34. // A pointer to the dynamic array of block pointers   35.     value_type** block_pointers;  

36.  

37.     // A pointer to the final entry in the array of block pointers   38.     value_type** block_pointers_end;  

39.  

40.     // A pointer to the first block pointer that’s now being used   41.     value_type** first_bp;  

42.  

43.     // A pointer to the last block pointer that’s now being used   44.     value_type** last_bp;   

45.  

46.     value_type* front_ptr; // A pointer to the front element of the whole deque   47.     value_type* back_ptr; // A pointer to the back element of the whole deque   

48.  

49.     size_type bp_array_size; // Number of entries in the array of block pointers   50.     size_type block_size; // Number of entries in each block of items   

51. };   

     

 

More products