C++ Midterm 2
What is the output of the following? string s = "12345"; int i = 1; while (i < 5){ cout << s.substr (i, 1); i++;}
2345
What prints here? auto a = '1';switch (a){ case 1: cout << "1"; break; case 2: cout << "2"; break; default: cout << "3";}cout << endl;
3
The file expenses.txt contains the line: Hotel, 3 nights. $ 1,750.25. What prints?ifstream in("expenses.txt");char c;while (in.get(c)){ if (isdigit(c)) { in.unget(); int n; in >> n; cout << n << 'x'; }}
3x1x750x25x
Assume that the input is 5 5 4 3 5. What will print? int i = 1;int n;do{ cin >> n; i++;}while (n % 2);cout << i << endl;
4
Which line represents the intentional bounds in this loop?1. string s("Hello CS 150");2. while (s.size())3. {4. if (s.at(0) == 'C') break;5. s = s.substr(1);6. }7. cout << s << endl;
4
What prints here? int i = 5;while (i) cout << --i;cout << endl;
4321
What prints here? int i = 5;while (i--) cout << i;cout << endl;
43210
What is the output of the following? int i = 0, j = 0; while (i < 125){ i = i + 2; j++;} cout << j << endl;
63
Match each item with the correct statement below. End a block of source code Meaning of value returned from a function Required to document functions, global variables and constants. Begin a block of source code Your name Information about the library When was it created? Name and meaning for a parameter
@endcode @return @file @code @author @version @date @param
What prints here? auto a = 'A';switch (a){ case 64: cout << "?"; case 65: cout << "A"; case 66: cout << "B";}cout << endl;
A
What prints? void fn(int, double, double&) { cout << "A" << endl; }void fn(int, int, double&) { cout << "B" << endl; }void fn(int, int, double) { cout << "C" << endl; }void fn(int, int, int) { cout << "D" << endl; }int main(){ fn(2.5, 1.5, 2.5);}
C
Which of these program organization schemes does not work?
Call your functions and define them afterwards.
What prints here? auto a = 3, b = 3;cout << a == b ? "panda" : "tiger" << endl;
Does not compile
Calling cout.put(65) is illegal. Your code will not compile.
False
Calling cout.put(65) prints the number 65 on output.
False
Fatal error messages should be printed to cout.
False
Formatted I/O means that you read and write data line-by-line.
False
Stream parameters should always be passed to functions by const reference.
False
The cin object is an instance of the ifstream class.
False
The cout object is an instance of the ofstream class.
False
The heading of a try block can contain ellipses in place of a parameter.
False
The return value of the getline() function is a string object.
False
To use a disk file as a data stream source or sink, use the <ofstream> header
False
When using cin >> ch; to read a character, leading whitespace is not skipped.
False
How many lines of output are printed? int count = 0;while (count != 9){ cout << "Monster Mash" << endl; if ((count % 2) == 0) { count++; } else { count--; }}
Infinite
What is the purpose of the throw statement?
It is used to pass control to an error handler when an error situation is detected.
What kind of error is this? ex1.cpp:7: undefined reference to `f()'
Linker error (something is missing when linking)
Assume you have a char variable named ch. How do you look ahead before reading a character?
None of these
What kind of error is this? ~/workspace/ $ ./ex1The Patriots won the 2018 Super Bowl
None of these
Which prototypes in the following header file contain errors? #ifndef EXAMPLE_H#define EXAMPLE_H#include <string>std::string f1(int a);int f2(double);void f3(std::string& s, int n);double f4();#endif
None of these
The file temp.txt contains "Orange Coast College". What prints?ifstream in("temp.txt");char c;while (in.get(c)){ if (isupper(c)) cout << toupper(c);}
OCC
What kind of error is this?Segmentation fault
Operating system signal or trap
What kind of error is this? terminate called after throwing an instance of 'std::out_of_range'
Runtime error (throws exception when running)
What prints? void fn(int, double, double&) { cout << "A" << endl; }void fn(int, int, double&) { cout << "B" << endl; }void fn(int, int, double) { cout << "C" << endl; }void fn(int, int, int) { cout << "D" << endl; }int main(){ fn(1, 2, 3, 4);}
Syntax error: no candidates
Below are terms connected with function overloading resolution.Match each item with the correct statement below. A function where an argument is converted to match a parameter When more than one match is found for the proffered arguments. A function where each argument is the same type as the corresponding parameter. A group of functions with the same name. A group of functions that have the same name and the correct number of parameters. When no match is found for the proffered arguments
best match ambiguity exact matches candidate set viable set empty set
Stream arguments to a function should always be passed:
by reference
The try block is followed by one or more ____ blocks.
catch
In a sequence of try/catch blocks, the last catch block of that sequence should be ____.
catch(...){ }
Which of the following blocks is designed to catch any type of exception?
catch(...){ }
Assume the user types "brown cow" when this code runs. What is stored in ch2? char ch1;auto ch2 = cin.get(ch1);
cin
Complete the following code in the echo filter program. char ch;while (______________) cout.put(ch);
cin.get(ch)
Assume you have a char variable named ch. How do you look ahead before reading a character?
cin.peek();
Assume you have a char variable named ch. How do you "unread" a character already read?
cin.putback(ch);
The C++11 standard library provides the function stoi() to convert a string to an integer. Which library is it found in?
cnvt
In a library, the client or test program:
consists of function calls
In a library, the makefile:
consists of instructions that produce the executable
To allow f() to accept the argument passed here, the parameter str should be declared as: void f( . . . str);int main(){ f("hello");}
const string&
Complete the following code in the echo filter program. char ch;while (cin.get(ch)) _______________;
cout.put(ch)
What Java and other OO languages call a subclass, C++ calls a ____________.
derived class
Which of these are dependencies? EXE=digit-testerOBJS=client.o digits.o$(EXE): $(OBJS) $(CXX) $(CXXFLAGS) $(OBJS) -o $(EXE)
digits.o client.o
Arguments passed to a function that has a constant reference parameter must be:
either lvalues or rvalues are fine
The class ____ is the base of the classes designed to handle exceptions.
exception
Given the overloaded functions prototypes and the variable definition below, which of the function calls will fail to compile? int f(int&);int f(int);int f(int, int);int a = 7; Group of answer choices
f(a);
Programs that process streams of characters are called text ______________.
filters
What prints?string s("hello");try { if (s.size() > 20) throw 42; if (islower(s.back())) throw "goodbye"; if (s == "hello") throw string("hello"); s.at(s.size()) = 'x'; cout << "one\n";}catch (const int& e) { cout << "two\n"; }catch (const string& e) { cout << "three\n"; }catch (exception& e) { cout << "four\n"; }catch (...) { cout << "five\n"; }
five
Which of these may go into a header file?
function prototypes constant definitions
The input stream member function for reading a character at a time is named:
get()
Match each item with the correct statement below. Has a single char& parameter Returns the last character read to the input stream Examines, but does not read the next character in an input stream Replaces the last character read with any character Called implicitly when an input statement is used as a test condition. A predicate function Converts its value argument to a character and sends it to output.
get() unget() peek() putback() fail() isalpha() put()
After opening the input stream in, which of these cannot be used to see if the file was successfully opened?
if (in.opened()) {/* opened ok */}
Which line opens the file in.txt for reading?
ifstream in("in.txt");
This loop:string str;while (in >> str){ cout << str << endl;}
illustrates token-based stream processing
The file grades.txt contains lines of text that look like this:Smith 94Jones 75. . . Each line of text contains the student's name (a single word) and an integer score. What is the legal way of reading one student's information, given the following code?string name;int score;ifstream in("grades.txt");
in >> name >> score;
Establish an association between the input stream object named in, and the text file on disk named "pets.txt".
in.open("pets.txt");
This loop:char c;while (c = in.get()){ cout << c << endl;}
is an endless loop
Which of the following loop patterns are used here?string s{"hello CS 150"};for (auto e : s){ if (toupper(e)) break;}
iterator or range loop loop-and-a-half
To deal with logical errors in a program, such as a string subscript out of range or an invalid argument to a function call, several classes are derived from the class ____.
logic_error
Arguments passed to a function that has a non-constant reference parameter must be:
lvalues
What happens when this code fragment runs?istringstream in("12");int n;in >> n;
n is set to 12
Create an output file stream object named out.
ofstream out;
After writing data to an ostringstream object named os, you can retrieve the string it contains by using:
os.str()
Different functions that have the same name, but take different arguments, are said to be:
overloaded
Which of the following loop patterns are used here?size_t pos = 0;char ch;in.get(ch);while (ch != 'Q'){ pos++; in.get(ch);}
sentinel loop primed loop
To allow f() to change the argument passed here, the parameter str should be declared as: void f( . . . str);int main(){ string s = "hello"; f(s);}
string&
What prints here? auto a = 3, b = 3;cout << (a != b ? "panda": "tiger") << endl;
tiger
Complete the following code in the upper filter program.char ch;while (cin.get(ch)) cout.put(____________);
toupper(ch)
Which of these prototypes is the best one to use in this circumstance? int main(){ string str{"TO BE OR NOT TO BE"}; properCase(str); cout << str << endl;}
void properCase(string&);
Match each item with the correct loop form below. Indefinite limit loop that reduces its input Indefinite limit loop that uses successive approximations Counter-controlled symmetric loop for producing a sequence of data Indefinite data loop that uses raw input Counter-controlled asymmetric loop for processing characters Iterator loop that may change its container Iterator loop that cannot change its container Counter-controlled loop for processing substrings Indefinite data loop that uses formatted input
while (n != 0) { n /= 2; } while(abs(g1 - g2) >= EPSILON) {. . .} for (int i=12; i <= 19; i++) {. . .} while(cin.get(ch)) {. . .} for (size_t i=0, len=s.size(); i < len; i++) {. . .} for (auto& e : col) {. . .} for (auto e : col) {. . .} for (size_t i=4, slen=4, len=s.size(); i < len; i++) {. . .} while(cin >> n) {. . .}
Which line represents the necessary bounds in this loop?1. string s("Hello CS 150");2. while (s.size())3. {4. if (s.at(0) == 'C') break;5. s = s.substr(1);6. }7. cout << s << endl;
2
What prints here? auto a = 2;switch (a){ case 1: cout << "1"; break; case 2: cout << "2"; break; default: cout << "3";}cout << endl;
2
Which line runs a.out getting its input from in.txt and sending its output to the file out.txt, and its errors to the file err.txt?
./a.out < in.txt > out.txt 2> err.txt
Which line runs a.out getting its input from in.txt and appending its output to the file out.txt?
./a.out > in.txt >> out.txt
Which line runs a.out getting its input from in.txt and sending its output to the new file out.txt?
./a.out > out.txt < in.txt
Which line runs the dwk program and gets its input from a file named y.data?
./dwk < y.data
Which line in the function "skeleton" below contains an error?#include "digits.h" // 1.int firstDigit(int n); // 2.{ // 3. return 0; // 4.} // 5.
// 2.
Which line in the function "skeleton" below contains an error? #include "borgia.h" // 1. void primoTiara(int n) // 2. { // 3. return 0; // 4. } // 5.
// 4.
What is the output of the following? int i = 0;while (i != 11){ cout << i << " "; i = i + 2;}
0 2 4 6 8 10 12 14 .... (infinite loop)
What is the output of the following? int i = 1;while (i < 10){ cout << i << " "; i = i + 2; if (i == 5) { i = 9; }}
1 3 9
What prints here? auto a = 1;switch (a){ case 1: cout << "1"; case 2: cout << "2";}cout << endl;
12
How many lines of output are printed? int i = 0;int j = 0;while (i < 25){ i = i + 2; j++;}cout << j << endl;
13
How many times will this display "So far so good"? int i = 0; while (i != 15) { cout << "So far so good" << endl; i++; }
15 times
A process filter does something to the characters it encounters.
True
Calling exit() terminates a program immediately and passes an error code back to the operating system.
True
Counting the number of words in input by counting word transitions is an example of a state filter.
True
Default arguments appear only in the function prototype.
True
Default arguments may only be used with value parameters.
True
To read a line of text, you include the header file <string>.
True
Unformatted I/O means that you read and write data character-by-character.
True
When using cin >> ch; to read a character, leading whitespace is skipped.
True
Assume the user types "brown cow" when this code runs. What prints?int n;if (cin >> n) cout << "X\n";else cout << "Y\n";
Y