Comp Sci Final

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What will be displayed by the following code? #include <iostream> #include "Rational.h" using namespace std; int main() { cout << Rational().toString(); return 0; }

0

What will be displayed by the following code? #include <iostream> #include "Rational.h" using namespace std; int main() { Rational r1(1, 2); Rational r2(2, 4); cout << r1.equals(r2); return 0; }

1

What is the return value for xFunction(4) after calling the following function? int xFunction(int n) { if (n == 1) return 1; else return n + xFunction(n - 1); }

10

How many times the recursive moveDisks function is invoked for 4 disks?

15

Analyze the following code: #include <iostream> using namespace std; void xFunction(int x[], int length) { cout << " " << x[length - 1]; xFunction(x, length - 1); } int main() { int x[] = {1, 2, 3, 4, 5}; xFunction(x, 5); }

The program displays 5 4 3 2 1 and then the index will be out of bounds.

What is wrong in the following code? vector<int> v; v[0] = 2.5;

The program has a runtime error because there are no elements in the vector.

Analyze the following recursive function. long long factorial(int n) { return n * factorial(n - 1); }

The function runs infinitely and runs out of memory.

Which of the following statements is incorrect? class Fruit { public: Fruit(int id) { } }; class Apple: public Fruit { public: Apple() { } };

The program will compile fine.

#include <iostream> using namespace std; class Date { friend void p(); private: int year; int month; int day; }; void p() { Date date; date.year = 2000; cout << date.year; } int main() { p(); return 0; }

The program will have a compile error if the line friend void p() is deleted.

Which of the following statements is false?

The stream.getline() function returns a string.

The += operator is implemented incorrectly in the following code. Which of the following statements is false? Rational Rational::operator+=(const Rational& secondRational) { this->add(secondRational); return this; }

There are multiple errors in the code. One error is on "Rational::", remove it.

What is the output of the following code? #include <iostream> #include <fstream> using namespace std; int main() { // Create a file ofstream output("scores.txt"); // Write two lines output << "John" << " " << "T" << " " << "Smith" << " " << 90 << endl; output << "Eric" << " " << "K" << " " << "Jones" << " " << 85; output.close(); ifstream input; // Open a file input.open("scores.txt"); // Read data char firstName[80]; char mi; char lastName[80]; int score; input >> firstName >> mi >> lastName >> score; double sum = score; input >> firstName >> mi >> lastName >> score; sum += score; cout << "Total score is " << sum << endl; input.close(); return 0; }

Total score is 175

If you are not interested in the contents of an exception object, the catch block parameter may be omitted.

True

To invoke the toString() function defined in GeometricObject from a Circle object c, use __________.

c.GeometricObject::toString()

Suppose r is a Rational object, in order to perform 4 + r to obtain a Rational object, the Rational class header file must contain:

conversion constructor Rational(int numerator) and non-member function Rational operator+(const Rational& r1, const Rational& r2)

Which of the statements is incorrect to invoke the following template function? template<typename T> T maxValue(const T& value1, const T& value2) { if (value1 > value2) return value1; else return value2; }

cout << maxValue(1.5, 2)

The functions seekg and seekp may have two arguments. The first argument is the offset and the second argument may indicate the base for the offset. Which of the following cannot be used as the second argument?

ios::now

Can you open a file for both input and output?

yes

You can combine modes using the _________ operator.

|

Fill in the code to complete the following function for checking whether a string is a palindrome. bool isPalindrome(const char * const s) { if (strlen(s) <= 1) // Base case return true; else if _____________________________ // Base case return false; else return isPalindrome(substring(s, 1, strlen(s) - 2)); }

(s[0] != s[strlen(s) - 1])

Which of the following statements returns the smallest element in vector v?

*min_element(v, v.begin(), v.end())

How many times the recursive moveDisks function is invoked for 3 disks?

7

Which of the following operators cannot be overloaded?

?:

Which of the following statements is true? CH16

A class is abstract if it contains a pure virtual function.

Which of the following statements is false?

A constructor can be redefined.

Which of the following statements are true?

A custom exception class is just like a regular class.

What is the output of the following code? #include <iostream> using namespace std; class B { public: ~B() { cout << "B"; } }; class A: public B { public: ~A() { cout << "A"; } }; int main() { A a; return 0; }

AB

What will be displayed by the following code? #include <iostream> #include <string> using namespace std; class A { public: string toString() { return "A"; } }; class B: public A { public: string toString() { return "B"; } }; int main() { B b; cout << static_cast<A>(b).toString() << b.toString() << endl; return 0; }

AB

What will be displayed by the following code? #include <iostream> #include <string> using namespace std; class C { public: virtual string toString() { return "C"; } }; class B: public C { public: string toString() { return "B"; } }; class A: public B { public: string toString() { return "A"; } }; void displayObject(C* p) { cout << p->toString(); } int main() { displayObject(&A()); displayObject(&B()); displayObject(&C()); return 0; }

ABC

True Statements CH17

Assigning j to queens[i] to denote that a queen is placed in row i and column j. Invoking search(0) searches for a queen placement solution for the entire board. Invoking search(i) searches for a queen placement solution in row i and in the process it finds the solution for all rows after i. Invoking isValid(i, j) tests if there is a queen at the upper-left diagonal from (i, j), upper-right diagonal from (i, j), and at the j's column.

True Statements CH16

C++ allows you to throw a primitive type value or any object-type value. In general, common exceptions that may occur in multiple classes in a project are candidates for exception classes. Simple errors that may occur in individual functions are best handled locally without throwing exceptions. Exception handling is for dealing with unexpected error conditions. Do not use a try-catch block to deal with simple, expected situations.

What will be displayed by the following code? #include <iostream> #include <string> using namespace std; class C { public: string toString() { return "C"; } }; class B: public C { public: string toString() { return "B"; } }; class A: public B { public: string toString() { return "A"; } }; void displayObject(C* p) { cout << p->toString(); } int main() { displayObject(&A()); displayObject(&B()); displayObject(&C()); return 0; }

CCC

Suppose you declared GeometricObject* p = &object. To cast p to Circle, use ___________.

Circle* p1 = dynamic_cast<Circle*>(p);

True Statements CH14

Computers do not differentiate binary files and text files. All files are stored in binary format, and thus all files are essentially binary files. Text I/O is built upon binary I/O to provide a level of abstraction for character encoding and decoding. The C++ source programs are stored in text files and can be read by a text editor. The C++ executable files are stored in binary files and are read by the operating system.

What is the correct signature for the = operator function in the Course class?

Course& operator=(const Course& course);

Suppose Exception2 is derived from Exception1. Analyze the following code. try { statement1; statement2; statement3; } catch (Exception1& ex1) { } catch (Exception2& ex2) { }

If an exception of the Exeception2 type occurs, this exception is caught by the first catch block.

What is the purpose of invoking the close function?

If this function is not invoked, the data may not be saved properly in the file.

Are the constructors inherited by the derived class?

No

If the << operator does not access the private data fields in Rational, do you still have to declare it friend?

No

Is there a special character stored in a file to denote the end of file?

No

What does the following statement do? ifstream stream("scores.txt");

Open a file for input.

What does the following statement do? ofstream stream("scores.txt");

Open a file for output, the contents of the file is destroyed if the file already exists.

Analyze the following two programs: A: void xFunction(int length) { if (length > 1) { cout << (length - 1) << " "; xFunction(length - 1); } } int main() { xFunction(5); } B: public static void xFunction(int length) { while (length > 1) { cout << (length - 1) << " "; xFunction(length - 1); } } int main() { xFunction(5); }

Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 .... 1 infinitely.

What is the correct signature for the overloaded unary operator +?

Rational Rational : : operator+()

What is the correct signature for the overloaded postfix ++ operator?

Rational operator++(int dummy)

Which of the following statements creates a default Rational object?

Rational r1;

Which of the following statements is false? CH 17

Recursive functions run faster than non-recursive functions.

Which of the statements is incorrect for using the following template class? template<typename T = int> class Stack { Stack(); ... };

Stack s;

Which of the statements is incorrect for using the following template class? template<typename T, int capacity> class Stack { Stack(); ... private: T elements[capacity]; int size; };

Stack<int, double> s;

Which of the following statements is incorrect? CH 12

Templates improve performance.

Which of the following statements is true? CH17

The Fibonacci series begins with 0 and 1, and each subsequent number is the sum of the preceding two numbers in the series.

Which of the following statements is incorrect?

The code in the catch block is always executed.

Analyze the following code: #include <iostream> using namespace std; class A { public: A() { t(); // cout << "i from A is " << i << endl; } void t() { setI(20); } virtual void setI(int i) { this->i = 2 * i; } int i; }; class B: public A { public: B() { cout << "i from B is " << i << endl; } void setI(int i) override { this->i = 3 * i; } }; int main() { A* p = new B(); return 0; }

The constructor of class A is called and it displays "i from B is 40".

Which of the following statements is false?

The dynamic_cast operator can cast a primitive type variable to another primitive type.

tail-recursive functions

The factorial functon in LiveExample 17.9. The isPalindrome function in LiveExample 17.3. The sort function in LiveExample 17.5. The binarySearch function in LiveExample 17.6.

Which of the following statements is false?

You cannot have a protected constructor.

This book uses .dat to denote _______ file.

a binary

The following code write text _______ to the file Text.txt. ofstream output("Test.txt"); output << "ba" << setw(6) << left << "bc";

babc⌴⌴⌴⌴

To read a character string from a binary file, use the function _______.

biStream.read(char* address, int size)

To write a character string to a binary file, use the function _______.

biStream.write(char* address, int size)

int binarySearch(const int list[], int key, int low, int high) { if (low > high) // The list has been exhausted without a match return -low - 1; // Return -insertion point - 1 int mid = (low + high) / 2; if (key < list[mid]) return binarySearch(list, key, low, mid - 1); else if (key == list[mid]) return mid; else return binarySearch(list, key, mid + 1, high); } int binarySearch(const int list[], int key, int size) { int low = 0; int high = size - 1; return __________________________; }

binarySearch(list, key, low, high)

Suppose you declare double array[SIZE] and array is written to the file using binary I/O. To read the array to result (double result[SIZE]), use _____.

binaryio.read(reinterpret_cast<char*>(&result), sizeof(result));

Suppose you declare Student student1 . To read a Student object from a binary file, use _____.

binaryio.read(reinterpret_cast<char*>(&studentNew), sizeof(Student));

Suppose you want to read an int to the variable value from a binary file, use ______.

binaryio.read(reinterpret_cast<char*>(&value), sizeof(value)); biStream.read(&value);

Suppose you declare Student student1 . To write student1 to a binary file, use _____.

binaryio.write(reinterpret_cast<char*>(&student1), sizeof(Student));

Suppose you declare int value = 99, to write it to a binary file, use ________.

binaryio.write(reinterpret_cast<char*>(&value), sizeof(value));

The signature for the nonmember < operator function for comparing two Rational objects is __________.

bool operator<(const Rational& r1, const Rational& r2)

The signature for the < operator function for comparing two Rational objects is __________.

bool operator<(const Rational& secondRational)

Suppose Circle and Rectangle classes are derived from GeometricObject and the displayGeometricObject function is defined as shown below. Which of the following function calls is incorrect? void displayGeometricObject(GeometricObject shape) { cout << shape.toString() << endl; }

displayGeometricObject(string());

The function what() is defined in ______________.

exception

Which of the following statements is false?

friend functions and classes of a class must be declared inside the class

What is the correct signature for the overloaded >> operator?

friend istream& operator>>(istream& stream, Rational& rational);

To read data from a file, you create an instance of _________.

ifstream

Which of the following classes is not predefined in C++?

index_out_range_error

Given the printArray template function, which of the statements are correct to invoke it? template<typename T> void printArray(T list[], int arraySize) { for (int i = 0; i < arraySize; i++) { cout << list[i] << " "; } cout << endl; }

int list[] = {1, 2, 3, 4}; printArray(list, 4); int list[] = {1, 2.5, 3, 4}; printArray(list, 4); double list[] = {1, 2, 3, 4}; printArray(list, 4); string list[] = {"Atlanta", "Dallas", "Houston", "Chicago"}; printArray(list, 4);

You can invoke the following swap function using ______. template<typename T> void swap(T& var1, T& var2) { T temp = var1; var1 = var2; var2 = temp; }

int v1 = 1; int v2 = 2; swap(v1, v2);

What is the correct signature for overloading the subscript operator []?

int& operator[](const int& index);

Which of the following modes is for input?

ios::in

To open a file for binary input, use the mode _______.

ios::in | ios::binary

Fill in the code to complete the following function for checking whether a string is a palindrome. bool isPalindrome(const char * const s, int low, int high) { if (high <= low) // Base case return true; else if (s[low] != s[high]) // Base case return false; else return ____________________________; } bool isPalindrome(const char * const s) { return isPalindrome(s, 0, strlen(s) - 1); }

isPalindrome(s, low + 1, high - 1)

What are the base cases in the following recursive function? void f(int n) { if (n > 0) { cout << n % 10; f(n / 10); } }

n <= 0

To write data to a file, you create an instance of _________.

ofstream

What is the correct signature for a function that converts a Rational to double?

operator double()

Which of the following is wrong to add Rational objects r1 to r2?

r1 += r2;

In the implementation of ImprovedStack.h, which of the following is true?

size may increase or decrease. capacity never reduces. Inside Stack, a regular array is used to store elements. If the current capacity equals to size, capacity is doubled when a new element is added to Stack.

Fill in the code to complete the following function for sorting a list. void sort(char list[], int high) { if (high > 1) { // Find the largest element and its index int indexOfMax = 0; char max = list[0]; for (int i = 1; i <= high; i++) { if (list[i] > max) { max = list[i]; indexOfMax = i; } } // Swap the largest with the last element in the list list[indexOfMax] = list[high]; list[high] = max; // Sort the remaining list sort(list, high - 1); } } void sort(char list[]) { ____________________________; }

sort(list, strlen(list) - 1)

Suppose that statement3 throws an exception of type Exception3 in the following statement: try { statement1; statement2; statement3; } catch (Exception1& ex1) { } catch (Exception2& ex2) { } catch (Exception3& ex3) { statement4; throw; } statement5;

statement4

Assume function3 throws an exception of the Exception2 type in Figure 16.3 in this section, which statements are executed?

statement4, statement1, statement2

Suppose that statement2 throws an exception of type Exception2 in the following statement: try { statement1; statement2; statement3; } catch (Exception1& ex1) { } catch (Exception2& ex2) { } catch (Exception3& ex3) { statement4; throw; } statement5;

statement5

To know whether it is the end of a file, you use the function ________.

stream.eof()

To know whether the I/O operation succeeded, you use the function ________.

stream.fail()

You can use the _________ function to move the file pointer for input.

stream.seekg(length);

You can use the _________ function to move the file pointer for output.

stream.seekp(length);

Which of the following statements deletes the first element from vector v?

v.erase(v.begin())

Which of the following statements inserts number 5 to the beginning of vector v?

v.insert(v.begin(), 5)

To add an int value 5 to a vector v of integers, use _________.

v.push_back(5);

To obtain the size of the vector v, use _______.

v.size();

Which of the following statements is correct?

vector<int> v = { 1, 2 }; int a[] = {1, 2}; v[0] = 3; a[0] = 4;

To declare a vector for holding int values, use __________.

vector<int> v;

Which of the following is an abstract function?

virtual double getArea() = 0;


संबंधित स्टडी सेट्स

Ankle and Foot: Part 4 (Insufficient Dorsiflexion)

View Set

MAN 4701 Study Plan Chapters 9, 11, 14

View Set

The Internet and the World Wide Web ( webtech )

View Set

Chapter 6 Cardiovascular system;blood

View Set