CMSC 202 Fall 2021 Name TFs are cool_________ Exam 3 Review Worksheet General Concepts Exam 3 will be based on chapters 1-19. You can also be asked questions on any of the topics covered in the lab material including debugging and makefiles. You are responsible for any material in the book and available on the slides. Fill-in-the-Blanks (deleted similar question) 1. Separate compilation generally works except when using ____templates__________. 2. A recursive function should be designed to stop making recursive calls when it reaches its ____base______ ___case_______ (2 words). 3. An _____excetption_____________ is an occurrence of an undesirable situation that can be detected during program compilation. 4. __________A pointer______ to a base class may be assigned the address of a derived class object. 5. A ____recursive____ function is one that calls itself. 6. In a _____shallow_____ ____copy________ , two pointers will point to the same object. (2 words) 7. Two primary queue operations are _____enqueue____________ and _______dequeue________ . 8. When a structure is passed by _________reference______ to a function, its members are not copied. 9. When the ____&____ is placed in front of a variable name, it returns the address of that variable. 10. A ________queue___ is a linked data structure that follows the First In First Out (FIFO) convention. 11. To save the output of a command to a file the operator used is _______ >__________________. 12. A friend function is used to access the _____private______ and __protect_________ of a class. 13. The next pointer of the _________tail______ node in a singly linked list will have a null pointer. 14. Consider the following recursive definition, where n is a positive integer. mult(1) = 1 mult(n) = 2* mult(n - 1) + 1 if n > 1 mult(3) = 2 * mult(2) + 1 -> 2*(3)+1 = 7 mult(2) = 2 * mult(1) + 1 -> 2* (1) + 1 = 3 mult(1) = 1 The value of mult(3) is ____________7______. 15. __copy constructor______ is a special function that is called whenever a new object is created and initialized with data from another object of the same class. 16. Templates can be used for functions as well as for ___classes________. 17. Using virtual functions is also known as _____overriding_________ the functions. 18. The flag ________-g_________ must be used during compilation so we can use gdb on code. g++ -Wall -g project.cpp -c proj Container Matching Match the container with the general description. A. Stores key/value pairs. Keys does not need to 19. Vector E be unique. B. Sorted data structure with duplicates. Cannot 20. List D change the value of an element once added. No random access. C. Stores key/value pairs. Keys must be unique. 21. Set G D. Sequential. Does not support random access. 22. Multiset B E. Dynamic, sequential container, allows random 23. Pair F access using [] or at(). F. Connects two items into a single object. These 24. Map C can be used by other containers. G. Sorted data structure with no duplicates. 25. Multimap A Cannot change the value of an element once added. No random access. True or False 26. True or False? If false, state why (or provide a counterexample). a. _F___ The advantage of a linked list over an array is that any arbitrary element can be inserted or deleted efficiently. b. __F__ The definition fact(1)=1 and fact(n)=fact(n-1)*n is an iterative definition. _ c. _F___ When you overload the << operator you must also overload the >> operator. d. _F___ To pass a two-dimensional array to a function we have to pass the number of rows and the number of columns separately as two additional parameters. fxn(int[][10]) e. _T___ If your program defines a class template, then the compiler will generate a class for each different data type for which it is instantiated. f. _T___ A thrown exception for which there is no matching catch block will cause the execution of the program to abort. g. __F__ In C++, polymorphism can only be implemented by using inheritance. h. __T__ It is possible to declare an entire class as a friend of another class. i. __T__ The private members of a base class cannot be directly accessed by a derived class. j. __F__ The derived class constructor is called first and then the base class constructor is called. k. __F__ To delete a linked list node one must delete the data member separately and the pointer member separately. l. _T___ Function templates allow you to write a single function definition that works with many different data types. Code Evaluation As a reminder, please try and evaluate these without entering them into GL. Check your work after you have identified what the output should be. 27. What is the output of this code? The code does not have any errors or a seg faults. #include <iostream> #include <string> using namespace std; class Flight { public: Flight () { cout << "Default Constructor called" << endl; } Flight (string name){ cout << "Overloaded constructor with name " << name << " called" << endl; } Flight (const Flight &c) { cout << "Copy Constructor called" << endl; } }; int main() { Flight *C1; C1 = new Flight("CS1001"); Flight *C2 = new Flight(*C1); Flight C3 (*C1); return 0; } Overloaded constructor with name “CS1001” called Copy Constructor called Copy Constructor called 28. What is the output of this code? The code does not have any errors or a seg fault. #include <iostream> using namespace std; int myFunction(int p, int q) { if (p == 1) return q; if (p % 2 == 1) return myFunction(p - 3, q + 2); else return myFunction(p + 1, q + 1); } int main () { int x = 3; int y = 4; cout << "The function call with x is = " << myFunction(x, 1) << endl; cout << "The function call with y is = " << myFunction(y, 1) << endl; } myFunction(3, 1) myFunction(0, 3) myFunction(1, 4) return 4 The function call with x is = 4 myFunction(4, 1) myFunction(5, 2) myFunction(2, 4) myFunction(3, 5) myFunction(0, 7) return 8 The function call with y is = 8 29. What is the output of this code? The code does not have any errors or a seg fault. #include <iostream> using namespace std; // function2 throws an exception void function2( ){ cout << "function2" << endl; throw double(42); } void function1(){ cout << "function1" << endl; function2(); } int main( ) { try { function1(); } catch (int i) { cout << "Exception occurred " << i << endl; } catch (double j) { cout << "Different kind of exception occurred " << j << endl; } return 0; } function1 function2 Different kind of exception occurred 42 30. What is the output of this code? Does this code have any errors? #include <iostream> using namespace std; class Fraction { public: Fraction(int n, int d) { m_numerator = n; m_denominator = d; } private: int m_numerator, m_denominator; friend ostream& operator<<(ostream& os, const Fraction& ft) { os << double (ft.m_numerator) / double (ft.m_denominator); return os; } }; int main() { Fraction f(1, 4); cout << f << endl; return 0; } .25 Debugging 31. The code below has five errors. The errors may be syntax errors or logic errors, and there may be more than one per line; examine the code carefully to find them. Indicate each of the errors you find by writing the line number and correction in the space provided below. There should be no memory leaks in this code either. The expected output is: first-12 first-0x18u47598 You must find and correct all five of the errors. If there is a blank line, you may insert a command. 1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 template <class T, V> 6 class MyPair 7 { 8 private: 9 MyPair(T first, V second){m_first=first;m_second=second;} 10 ~MyPair(){} 11 void setFirst(T value){m_first=value;} 12 void setSecond(T value){m_second=value;} 13 T getFirst(){return m_first;} 14 V getSecond(){return m_second;} 15 void print(){cout<<*m_first<<'-'<<m_second<<endl;} 16 private: 17 T m_first; 18 V m_second; 19 }; 20 21 int main() 22 { 23 string *aString = new string("first"); 24 int *anInt = new int(12); 25 MyPair<string*,int*> aPair(aString,anInt); 26 aPair->print(); 27 delete aString; 28 delete anInt; 29 return 0; 30 } Line Number Correction 8 public 15 cout << *m_first << *m_second << endl; setSecond(V value) 12 aPair.print(); 26 5 template<class T, class V> 32. The code below has five errors. The errors may be syntax errors or logic errors, and there may be more than one per line; examine the code carefully to find them. Indicate each of the errors you find by writing the line number and correction in the space provided below. There should be no memory leaks in this code either. The expected output is: Name: Coco Name: Snowy (Wire Fox Terrier) Name: Snowy (Wire Fox Terrier) You must find and correct all five of the errors. If there is a blank line, you may insert a command. 1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class Pet 5 { 6 public: 7 Pet(){}; 8 virtual void setName(string name){m_name=name;} 9 string getName() const {return m_name;} 10 virtual void print() const 11 { 12 cout << "Name: " << m_name << endl; 13 } 14 private: 15 string m_name; 16 }; 17 class Dog : public Pet 18 { 19 public: 20 Dog(){}; 21 Dog(const Dog& aDog) 22 { 23 m_name = aDog.m_name; 24 m_breed = aDog.m_breed; 25 } 26 void setBreed(string breed){m_breed = breed;} 27 string getBreed(){return m_breed;} 28 virtual void print() const 29 { 30 cout << "Name: " << m_name << " (" << m_breed << ") " << endl; 31 } 32 private: 33 string m_breed; 34 }; 35 int main() 36 { 37 Pet aPet; 38 Dog aDog; 39 aPet.setName("Coco"); 40 aDog.setName("Snowy"); 41 aDog.setBreed("Wire Fox Terrier"); 42 Dog* bDog(aDog); 43 aPet.print(); 44 aDog.print(); 45 bDog.print(); 46 47 return 0; 48 } Line Number Correction cout << getName() << endl; 30 setName(aDog.getName()); 23 45 bDog->print(); Dog bDog(aDog); 42 Dog *bDog = new Dog(aDog); delete bDog; 46 33. Define a C++ class to specify a robot. Attributes of a robot are a name, and its technical information. We pass the name as a string and the technical information as an integer number. The technical information is a secret integer code that is stored in an integer pointer in the class. The class requires normal constructors, a destructor, mutators, and accessors. #include <iostream>; … using namespace std; // implement robot here: class Robot{ public: Robot(){ m_name = “Bender”; m_info = nullptr; } ~Robot(){ if(m_info){ delete m_info; m_info = nullptr; } } void setName(string name){ m_name = name; } void setInfo(int* ptr){ m_info = ptr; } string getName(){ return m_name; } int* getInfo(){ return m_info; } private: string m_name; int* m_info; }; 34. Write the C++ code that creates the Rule of Three for the class above. // implemented above but pasted for clarity ~Robot(){ if(m_info){ delete m_info; m_info = nullptr; } } Robot(const Robot& iRobot){ m_name = iRobot.m_name; m_info = new int(*(iRobot.m_info)); } // robot = robot Robot& operator=(const Robot& iRobot){ if(this != &iRobot){ if(m_info){ delete m_info; } m_name = iRobot.name; m_info = new int(*(iRobot.m_info)); } return *this; } 35. Given the following Node class for a linked list, write a recursive destructor that will delete the entire linked list. class Node { public: Node(int data) { m_data = data; } void createChild(int data) { m_next = new Node(data); } ~Node(); //Code this function private: int m_data; Node* m_next; }; ~Node(){ if(m_next != nullptr){ delete m_next; } } 36. Complete the code snippets below by filling in each of the blanks. The length of the blank does not indicate anything about the contents. Each blank may require more than one word or symbol to complete the program correctly. You may assume there are no errors in the code, and that it works as intended. #include <iostream> #include <string> using namespace std; int main() { int c = 0; //string variable declaration string aString; //c-string variable declaration ___char*____ aCString; cout << "Enter a string: "; cin >> aString; aCString = __new_ char[aString.size() + 1]; //dynamically allocates cstring for (c = 0; c < aString.size();++c){ aCString[c] = aString.at(c); } aCString[c] = _’\0’___; //sets the null terminator cout << "You entered: " << aCString << endl; ___delete[]___ aCString; //deallocates aCString return 0; } 37. Complete the code snippets below by filling in each of the blanks. The length of the blank does not indicate anything about the contents. Each blank may require more than one word or symbol to complete the program correctly. You may assume there are no errors in the code, and that it works as intended. // C++ program to demonstrate iterators in vectors #include <iostream> #include <vector> using namespace std; int main() { // Declaring a vector vector<int> v = { 1, 2, 3 }; // Inserting element using iterators for (vector<int>::iterator i = __v.begin()______; i != v.end(); ++i) { if (i == ___v.begin()____) { i = v.insert(i, 5); // inserting 5 at the beginning of v } } // v contains 5 1 2 3 return 0; } 38. Complete the code snippets below by filling in each of the blanks. The length of the blank does not indicate anything about the contents. Each blank may require more than one word or symbol to complete the program correctly. You may assume there are no errors in the code, and that it works as intended. // C++ program to demonstrate iterators in maps #include <iostream> #include <map> using namespace std; int main (){ map<char, int> mymap; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; // Show data from map for (_______map<char, int>::reverse_iterator_____ it=mymap.rbegin(); it!=mymap.rend(); ++it) cout << it->first << " => " << __it->second____ << '\n'; //Expected output: // c => 300 //b => 100 //a => 200 return 0; }
Enter the password to open this PDF file:
-
-
-
-
-
-
-
-
-
-
-
-