CS 216 Final UKY

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Trace the recursive function name g() and give the return value for function call: g(123). int g(int n) { if (n!= 0) return n % 10 + g(n/10); else return 0; }

3 +2 + 1 = 6

For a directed graph with n vertices, what is the minimum and maximum number of arcs?

A. 0; n*(n-1)

Consider the search function shown below. int search(vector<int> v, int from, int to, int value){ if (from > to) { return -1; } int mid = (from + to) / 2; if (v[mid] == value) { return mid; } else if (v[mid] < value) return search(v, mid + 1, to, value); else return search(v, from, mid - 1, value); } If the vector v contains the values { -5, 5, 8, 18, 19, 24, 45, 77, 104, 114 }, the value of from is 0 and that of to is 9 and the value being searched for is 104, what will this search function do?

Call search(v, 5, 9, 104)

The function dosomething() takes an ordered vector and modify the vector. Assume a vector v initially has the integer elements {2, 2, 2, 2, 3, 3, 5, 5, 7, 11, 11, 11, 13, 13}, fill in the elements in v after calling dosomething(v). void dosomething(vector<int>& v){ int i; stack<int> stk; vector<int> temp; stk.push(v.at(0)); for (i = 1; i < v.size(); i++) if (v.at(i) != stk.top()) stk.push(v.at(i)); while (!stk.empty()) { temp.push_back(stk.top()); stk.pop(); } v = temp; }

v after call to dosomething(v): 13 11 7 5 3 2

Assume MyClass prevObject has already been declared, the statement MyClass object2 = prevObject; will call the definition of assignment operator. (T/F)

False

Class member functions, which are declared with the const key word, may still call the class mutator functions. (T/F)

False

Friend functions of a class may directly access and modify the private data members of the class. (T/F)

False

In an operator overloading, it must be defined as a friend function of the class.

False

The recursive function must return a value.

False

What does this template function do? template <typename T>void f(const vector<T>& vA, vector<T>& vB, T key){ for (int i = 0; i < vA.size(); i++)if (vA[i] != key)vB.push_back(vA[i]);}

Inserts all n elements of vA that do not match key into vB.

What is the action of function doit()? template <typename T> void doit(list<T>& alist, T item) { list<T>::iterator iter; for(iter = alist.begin(); iter != alist.end(); iter++) if (*iter == item) return; alist.push_back(item); }

Inserts an item only if it is not already in the list.Inserts an item only if it is not already in the list.

The code below creates what kind of problem? int main() { string* myword[20]; for (int i = 0; i < 10; i++){ myword[i] = new string("Hello, CS216"); } cout << *myword[10] << endl; for (int i = 0; i < 10; i++) delete myword[i]; return 0; }

Invalid pointer dereference

What does this function do? bool myfunction(string s) { if (s.length() <= 1) { return true; } char first = s[0]; char last = s[s.length() - 1]; if (first == last) { string shorter = s.substr(1, s.length() - 2); return myfunction(shorter); } else return false; }

It returns true if the string is a palindrome.

The code below creates what kind of problem? EXPLAIN int main() { string* myword[20]; for (int i = 0; i < 10; i++){ myword[i] = new string("Hello, CS216"); cout << *myword[i] << endl; } delete[] myword; return 0; }

MEMORY LEAK - creates new memory for string* myword without deleting previously used memory. Do not need for loop.

The code below creates what kind of problem?int main() {string* myword;for (int i = 0; i < 10; i++){myword = new string("Hello, CS216");}cout << *myword << endl; delete myword; return 0;} EXPLAIN

MEMORY LEAK - the code creates 10 new memory addresses with the for loop, and only deletes the original

The code below creates what kind of problem? EXPLAIN int main() { string* myword; for (int i = 0; i < 10; i++){ myword = new string("Hello, CS216"); } cout << *myword << endl; return 0; }

Memory leak - failed to ever delete the allocated memory

What is the output of the following code snippet? void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int a = 215; int b = 216; swap(a, b); cout << "a = " << a << "; b = " << b << endl; return 0; }

a = 215; b = 216 function is not called by reference so the values do not change outside of the scope of the function

What is the output of the following code snippet? void swap(int a[], int size) { if (size < 2) return; int temp = a[0]; a[0] = a[1]; a[1] = temp; } int main() { int a[] = {215, 216}; swap(a, 2); cout << "a[0] = " << a[0] << "; a[1] = " << a[1] << endl; return 0; }

a[0] = 216; a[1] = 215

What is the output of the following code snippet? void swap(int& a, int& b) { int temp = a; a = b; b = temp; } int main() { int a[] = {1, 2}; swap(a[0],a[1]); cout << "a[0] = " << a[0] << "; a[1] = " << a[1] << endl; return 0; }

a[0] = 2; a[1] = 1

Trace the recursive function changeStr() and give the output for the specified function calls string changeStr(string old) { if (old.length() <= 1) return old + old; string mid = changeStr(old.substr(1, old.length()-1)); return old[0] + mid + old [0]; } What is the return value of calling changeStr("abc")?

abccba

An overloaded version of the ^ operator determines whether an element item is a member of set s. The infix form of the operator is "item^s". Complete the implementation of the function.

bool operator^(int item, const set<int>& s) { if (s.find(item) != s.end()) return true ; else return false ; }

Provide the command to make the shell script, named findPattern under your home directory executable:

chmod u+x ~/findPattern

Declaring a member function to be constant implies that the ___________________________

function may not update any data in the object.

Provide the command to compile all source files that is ending in .cpp from your current working directory and generate an executable file called demoSorting, so that you can use gdb tool to debug and run your program:

g++ -g ./*.cpp -o demoSorting

Provide the command to start an executable program, named CS216PA3 with two command line arguments (assume we need pass in two arguments: file1.txt and file2.txt as command line arguments) in GDB environment:

gdb --args ./CS216PA3 file1.txt file2.txt

Provide the Unix command to find all text lines, which contain the word starting with "com" and ending with "ter" in a file named words.txt , under your home directory (note that it contains one word at each line in the file, named words.txt).

grep "^com.*ter$" ~/words.txt

A static member variable can be used when there are no objects of the class in existence. (T/F)

True

Assume MyClass prevObject has already been declared, the following variable statements: MyClass object2; object2 = prevObject; will call the definition of assignment operator. (T/F)

True

Declaring a member function to be constant implies that the function may not modify any private data members in the class.

True

Perform a breadth-first search of the graph below. Assume the starting vertex is E. Which vertex is visited first? Which vertex is visited second? Which vertex is visited third? What is D's distance?

Which vertex is visited first? E Which vertex is visited second? C Which vertex is visited third? A or B What is D's distance? 3

Provide the missing action line (command line) to complete this makefile which will use g++ to compile source files main.cpp and functions.cpp then generate an executable file named CS216PA1: HEADERS = functions.h magicString.h OBJS = main.o functions.o magicString.o CC = /usr/bin/g++ EXEC = CS216PA1 $(EXEC) : $(OBJS) $(CC)__________________ %.o : %.cpp $(HEADERS) $(CC)__________________ clean: __________________________

1. $(CC) $(OBJS) -o $@ 2. $(CC) -c $< -o $@ 3. rm -i $(EXEC) *.o

:What is the output when the following shell script (named myCourse) is executed from the command line like this: $ ./myCourse CS216 CS275 CS221The content of the shell script (named myCourse): #!/bin/bash # test command line arguments coursename=$1 echo 'I am taking $# courses. ' echo "I am taking $# courses. " echo "It's fun to take \"$coursename\"."I am taking $# courses. I am taking 4 courses. It's fun to take "CS216"

1. I am taking $# courses. 2. I am taking 3 courses. 3. It's fun to take "CS216"

What is the output of this program? int main() { ifstream collectionData; string name; int dollars; map<string,int> canvassers; map<string,int>::iterator iter; collectionData.open("canvas.dat"); while (true) { collectionData >> name >> dollars; if (!collectionData) break; canvassers[name] += dollars; } for (iter=canvassers.begin(); iter!=canvassers.end();iter++) cout << (*iter).first << " " << (*iter).second << endl; return 0; } The follow shows the content of the file named canvas.dat Ted 10 Alice 20 Bob 50 Ted 20 Joan 60 Alice 20 Bob 30 Alice 70 Ted 80 Joan 40 The output is: Alice 110 Bob 80 Joan 100 Ted 110

Alice 110 Bob 80 Joan 100 Ted 110

Which of the following are valid template prefixes? template <class T> template <class Me> template <class T, class Me> template <typename myType> All of the above.

All of the above.

If you want to be able to compile the following: codeRational r1; int x; std::cout << r1+x << std::endl; Which overloaded operator(s) do you need? A.friend Rational& operator+(const Rational& left, int right); B.friend void operator+(const Rational& left, int right); C.friend ostream& operator<<(ostream& out, const Rational& object); D.friend ostream& operator>>(ostream& out, const Rational& object);

B and C

For a undirected graph with n vertices, what is the minimum and maximum number of edges?

B. 0; n*(n-1)/2

What is wrong with the following overloaded extraction operator declaration? istream& operator>>(istream& in, const myClass &object);

Object should not be a const parameter because then you wouldn't be able to change the value of the object

Which of the following are needed to change the function into a function template?int smallest( int array[], int size) {int small=0, i; for(i=0;i<size;i++) { if(array[i] < array[small]) small=i; } return small; }

Precede the function definition with template <class T>

A class template may not use dynamic memory allocation. (T/F)

True

The class storeMax is a container that holds a single integer value maxVal. The constructor initializes maxVal with default value 0. The member function update() takes an integer argument, which becomes the new stored value only when the argument is greater than maxVal. The current stored value is accessible through the function getValue(). The following is the declaration of storeMax, which you will modify to become a template-based class? class storeMax { public: storeMax() {maxVal = 0;} storeMax(int value); void update(int value); int getValue() const; private: int maxVal; }; The prototype of the function getValue() is:

T getValue() const;

Which of the following statements is true about sets?

The data items are traversed by an iterator in a sorted order that can be different from the order in which they were inserted.

What is the output from the following code snippet? #include <iostream> using namespace std; void func1(int &parm1, int &parm2, int &parm3); int* func2(int parm1, int parm2, int parm3); int main( ) { int num1 = 15; int num2 = 25; int num3 = 50; int* ptr; func1(num1, num2, num3); cout << "answer 1: "<<num1 << endl; cout << "answer 2: "<<num2 << endl; cout << "answer 3: "<<num3 << endl; ptr = func2(num1, num2, num3); cout << "answer 4: "<< ptr[0] << endl; cout << "answer 5: "<< ptr[1] << endl; delete[] ptr; return 0; } void func1(int &parm1,int &parm2, int &parm3) { static int calling_count = 0; calling_count++; cout << "Calling function: "<< calling_count << " time(s)" <<endl; if (parm1 < parm2){ int k = parm1; parm1 = parm2; parm2 = k; } if (parm2 < parm3){ int k = parm2; parm2 = parm3; parm3 = k; } if (parm1 < parm2){ int k = parm1; parm1 = parm2; parm2 = k; } } int* func2(int parm1, int parm2, int parm3){ func1(parm1, parm2, parm3); int* result = new int[2]; result[0] = parm2; result[1] = (parm1 + parm2 + parm3) / 3; return result; }

The output is: Calling function: 1 time(s) answer 1: 50 answer 2: 25 answer 3: 15 Calling function: 2 time(s) answer 4: 25 answer 5: 30

What is the output when the following program (named myCourses) is executed from the command line like this: $ ./myCourses CS216 CS275 MA213 PHY231 PHY241 The myCourses source code: int main(int argc, char* argv[]) { cout << "I am taking " << argc-1 << " courses" << endl; cout << "I spend most time on " << argv[3] << endl; return 0; }

The output is: I am taking 5 courses I spend most time on MA213

Provide the Unix command to copy files from your home directory to your current directory that have the pattern: the characters CS followed by exactly three characters (that can be any characters) that is ending in .cpp (for example, CS216.cpp but not CS16.cpp):

cp ~/CS???.cpp ./

Consider an undirected unweighted graph G. Let a breadth-first traversal of G be done starting from a node r. Let d(r, u) and d(r, v) be the lengths of the shortest paths from r to u and v respectively, in G. lf u is visited before v during the breadth-first traversal, which of the following statements is correct?

d(r, u) <= d(r, v)

Given the following definition for a map, which code fragment will correctly iterate through the map and output each item? map<int, string> mymap;

for (auto itr = mymap.begin(); itr != mymap.end(); itr++) { cout << itr->first << " " << itr->second << endl; }

In the following code snippet, what is the return value of calling myfunction(3, 3)? int myfunction(int x, int n) { if (n == 0) return 1; return x * myfunction(x, n-1); }

return (3*(3*(3*1))) = 27

Provide the Unix command to delete all files under your current working directory, which start with Lab and followed by exactly two characters and end with .zip (for example, Lab12.zip but not Lab2.zip):

rm ./Lab??.zip

A reason to overload the ________ is to write classes that have array-like behaviors.

square brackets [ ] operator

This type of function is not a member of a class, but it has access to the private members of the class.

static

class storeMax { public: storeMax() {maxVal = 0;} storeMax(int value); void update(int value); int getValue() const; private: int maxVal; }; The header for the implementation of the function update() is...

template <class T> void storeMax::update<T> (int<T> value)

The class storeMax is a container that holds a single integer value maxVal. The constructor initializes maxVal with default value 0. The member function update() takes an integer argument, which becomes the new stored value only when the argument is greater than maxVal. The current stored value is accessible through the function getValue(). The following is the declaration of storeMax, which you will modify to become a template-based class? class storeMax { public: storeMax() {maxVal = 0;} storeMax(int value); void update(int value); int getValue() const; private: int maxVal; }; The function header for the implementation of the function update() is:

template <class T> void storeMax<T>::update(T value)

The class storeMax is a container that holds a single integer value maxVal. The constructor initializes maxVal with default value 0. The member function update() takes an integer argument, which becomes the new stored value only when the argument is greater than maxVal. The current stored value is accessible through the function getValue(). The following is the declaration of storeMax, which you will modify to become a template-based class? class storeMax { public: storeMax() {maxVal = 0;} storeMax(int value); void update(int value); int getValue() const; private: int maxVal; }; The definition of storeMax as a template class is...

template <typename T> class storeMax

Coding Questions: Write a small fragment of code (not a complete program); Provide the definition of a class. 1) Implement a template function named complement() that takes a set s and a universal set u as parameters and returns the complement of s relative to u. The complement of s is the set of all elements in set u that are not in set s.

template <typename T> set<T> complement(const set<T>& s, const set<T>& u); set<T> complement(const set<T>& s, const set<T>& u) { set<T> complement; for (auto it = s.begin(); it != s.end(); it++) { if (u.find(*it) == u.end()) complement.insert(*it); } }

Which of the following functions header is correct for the given code snippet? void copy(... infile, ... outfile) { char ch; while (infile >> ch) outfile << ch; }

void copy(ifstream& infile, ofstream& outfile);


Kaugnay na mga set ng pag-aaral

PHYSICAL GEOLOGY 1403 FINAL EXAM

View Set

Patho Test 1 - Cardiovascular System

View Set

Deep Dive into Project Costs and Estimates

View Set

Econometrics lecture note 6- Multiple linear regression estimation

View Set

NCLEX Practice Test questions for exam 3

View Set