CS215 Final Exam

¡Supera tus tareas y exámenes ahora con Quizwiz!

Given the queue myData 12, 24, 48 (front is 12), where will the new item 72 be enqueued?

After 48

Which of these is a dynamically allocated array?

An array whose size can change during runtime

Given the following class definition in the file ClassRoom.h, which XXX and YYY complete the corresponding .cpp file? #ifndef CLASSROOM_H #define CLASSROOM_H class ClassRoom { public: void SetNumber(int n); void Print() const; private: int num; }; #endif #include <iostream> using namespace std; XXX void ClassRoom::SetNumber(int n) { num = n; } YYY { cout << "Number of Class Rooms: " << num << endl; }

#include "ClassRoom.h" void ClassRoom::Print() const

What is the output of the following code snippet? list<int> numberlist; const int SIZE = 10; int i; for (i = 0; i< SIZE; i++) numberlist.push_back(i); list<int>::iterator p; p = numberlist.begin(); while (p != numberlist.end()) { p++; cout << *p << " "; p++; }

1 3 5 7 9

What is output? #include <iostream> using namespace std; void NumSeries(int num) { if (num == 0) cout << 1 << " "; else { NumSeries(num - 1 ); num = num + 1; cout << num * 2 << " "; } } int main() { NumSeries(2); return 0; }

1 4 6

What is output for PrintNum(25)? void PrintNum(int num) { if(num == 0) return; else{ int n = num % 2; cout << n; PrintNum(num / 2); } }

10011

Suppose the array primes, defined as double primes[] = {2, 3, 5, 7, 11, 13, 17, 19}; it starts at memory location 10300, and a double value occupies 8 bytes of memory. What are the values of the followings: *primes *primes+5 *(primes+5) &primes[ 2 ]

2 7 13 10316

Given the queue myData 12, 24, 48 (front is 12), what will be the queue contents after the following operations? Enqueue(myData, 72) Dequeue(myData)

24, 48, 12

What is the ending value of the integer variable myInt? #include <iostream> using namespace std; int main() { int myInt; int* myScore; int myVar; myInt = 10; myScore = &myInt; myVar = 20; myVar = *myScore; *myScore = 30; myVar = 40; myVar = *myScore; }

30

Which of the following statements is true about public member functions of a class?

A class user can call the functions to operate on the object

Which of the following statements is true about a class' member function definition?

A function definition provides a class name, return type, arguments, and the function's statements.

Which of the following statements is true about private data members of a class?

A member function can access the private data members, but class users cannot.

Which of the following statements is true about a memory leak? You Answered

A memory leak occurs when a program allocates memory but loses the ability to access the allocated memory.

A linked list's head node stores _____ in the list.

A pointer to the first item node

A stack is a First-in-First-Out data structure

False

When you return a dynamic array to the heap, you must include the number of elements in the array

False

You can directly access the nth node of a linked list.

False

Show the output of the following code: #include <iostream> using namespace std; 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[] = {10, 20}; swap(a,2); cout << "Line 1: " << a[ 0 ] << endl; cout << "Line 2: " << a[ 1 ] << endl; cout << "Line 3: " << *(a+1) << endl; cout << "Line 4: " << *a+1 << endl; return 0; }

Line 1: 20 Line 2: 10 Line 3: 10 Line 4: 21

Which of the following statements is true with reference to searching?

Linear search will compare all elements if the search key is not present.

What is output? #include <iostream> using namespace std; class Line { public: Line () { cout << "No Such line"; ,} }; int main() { Line L1, *L2; return 0; }

No such Line

What is the return type for constructors?

None

Given the list Students: Tom, Harry, Sam, Kim, Tina, Hal, Sally. What will be the value of the next pointer of the node Hal if the node Sally is removed from the list?

Null

What is output? #include <iostream> using namespace std; class Shapes { public: void Print(); Shapes(int x); private: int sides = 4; }; Shapes::Shapes(int x) { this->sides = x; } void Shapes::Print() { if(this->sides == 4) { cout << "It's a Square!" << endl; } else { cout << "Oops!"; } } i nt main() { Shapes s(7); s.Print(); return 0; }

Oops!

Given a stack myData: Tom, Sam (top is Tom), what is the output after the following operations? Push(myData, Hal) Pop(myData) Pop(myData) print(Peek(myData))

Sam

Which of the following statements is true about the member access operator (->)?

The member access operator is always a non-static member function

The following program generates an error. Why? #include <iostream> #include <string> using namespace std; class Arcade { public: Arcade(); Arcade(string name, int r); void Print(); private: string arcName; int rating; }; Arcade:: Arcade() { arcName = "New"; rating = 1; } Arcade:: Arcade(string name, int r) { arcName = name; rating = r; } void Arcade:: Print() { cout << "Name is: " << arcName << endl; cout << "Rating is: " << rating << " stars" << endl; } int main() { Arcade myArc(Games Ablaze, 5); myArc.Print(); }

The object creation should be Arcade myArc("Games Ablaze", 5);

Which of the following is true for overloading a class constructor?

The parameter types of the constructors should be different.

Identify the error in the following algorithm for traversing a linked list. ListTraverse(list) { curNode = list⇢head while (curNode is not null) { Print curNode's data curNode = list⇢head⇢next } }

The statement curNode = list⇢head⇢next should be curNode = curNode⇢next.

A linked list is not fixed in size

True

Putting the keyword const after the class member function declaration guarantees that the member function will not change the calling object

True

The dereference operator * accesses the variable to which a pointer points.

True

Which of the following statements allocates memory in the heap?

a = new int;

A(n) _____ is a sequence of steps for solving a problem.

algorithm

Which statement properly frees the dynamically allocated array below? Musketeer* musketeers = new Musketeer[8];

delete [] musketeers;

Which XXX will avoid a memory leak? #include <iostream> using namespace std; class MyClass { public: MyClass(); int value; private: int * ptr; }; MyClass::MyClass () { ptr = new int; *ptr = 0; delete ptr; } int main() { int value = 100; int *ptr1 = new int; //XXX ptr1 = &value;

delete ptr1;

#include <iostream> #include <vector> using namespace std; int main() { vector<int> identityNums(5); identityNums.at(1) = 1; identityNums.at(2) = 6; identityNums.at(3) = 8; identityNums.at(4) = 3; identityNums.at(5) = 5; cout << "Size_1: " << identityNums.size() << endl; cout << "Size_2: " << identityNums.size() << endl; identityNums.push_back(2); cout << "New size_1: " << identityNums.size() << endl; cout << "New size_2: " << identityNums.size() << endl; return 1; }

identityNums.at(5) = 5;

Which XXX condition generates the following output?Not found #include <iostream> #include <string> #include <vector> using namespace std; int BinarySearch(vector<int> numberList, int element, int lowVal, int highVal) { int midVal; if (XXX) { midVal = (highVal + lowVal) / 2; if (numberList.at(midVal) == element) { return midVal; } else if (numberList.at(midVal) > element) { return BinarySearch(numberList, element, lowVal, midVal - 1); } else { return BinarySearch(numberList, element, midVal + 1, highVal); } } else { return -1; } } int main() { vector<int> numberList(0); int element = 20; int matchPos; for (int i = 0; i <= 10; ++i) { numberList.push_back(i); } matchPos = BinarySearch(numberList, element, 0, numberList.size() - 1); if (matchPos >= 0) { cout << "Found at position " << matchPos << "." << endl; } else { cout << "Not found. " << endl; } return 0; }

lowVal <= highVal

Consider a class Student with public member functions and private data members. Which XXX is valid for the code? class Student { public: void SetName(string studentName); void SetGrade(int studentGrade); void Display(); private: string name; int grade; }; int main() { Student stu1; XXX ... }

stu1.Display();

Heap is a region in program memory where _______

the "new" operator allocates memory

In a queue, a dequeue operation always removes _____ element

the front

A pointer is a(n) _____ that contains a _____.

variable, memory address

Assuming a class Student has been defined, which of the following statements is correct for declaring a vector?

vector<Student> studList;

Which XXX completes the binary search algorithm? BinarySearch(numbers, numbersSize, key) { mid = 0 low = 0 high = numbersSize - 1 XXX { mid = (high + low) / 2 i f (numbers[mid] < key) { low = mid + 1 } else if (numbers[mid] > key) { high = mid - 1 } else { return mid } } return -1 }

while( high >= low)


Conjuntos de estudio relacionados