9. Streams (IC) Flashcards

1
Q

What is the purpose of the following code snippet?
ifstream inputFile(inputFileName); if (!inputFile.is_open()) { cout &laquo_space;“Failed to open the input file.” &laquo_space;endl; return 1; }
A) Opens the input file for reading B) Checks if the input file exists C) Outputs an error message if the input file failed to open D) Terminates the program with an error code if the input file failed to open

A

A) Opens the input file for reading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of the size_t data type used in the loops? A) Storing the movie titles B) Storing the ratings C) Storing the showtimes D) Storing the sizes of containers

A

D) Storing the sizes of containers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you open a file for reading using ifstream? a) file.open(“filename.txt”, ios::out) b) file.open(“filename.txt”, ios::in) c) file.open(“filename.txt”, ios::app) d) file.open(“filename.txt”, ios::binary)

A

b) file.open(“filename.txt”, ios::in)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which header file should be included to use ifstream and ofstream classes for file input and output? a) <iostream> b) <fstream> c) <string> d) <sstream></sstream></string></fstream></iostream>

A

b) <fstream></fstream>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the purpose of the input stream object in C++? a) Read input from the console b) Write output to the console c) Perform mathematical calculations d) Manipulate string data

A

a) Read input from the console

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

// Compute the average exam score and assign a letter grade double average = XXXXXXXXXX = assignLetterGrade(average); letterGrades.push_back(grade);
A) (midterm1 + midterm2 + final) / 3.0 B) (midterm1 + midterm2) / 2.0 C) (midterm1 + final) / 2.0 D) (midterm2 + final) / 2.0

A

A) (midterm1 + midterm2 + final) / 3.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

if (!found) { movieTitles.push_back(title.substr(0, 44)); movieRatings.push_back(rating); showtimes.push_back({ title, showtime }); } } inputFile.close();
How are the movie titles truncated to a maximum of 44 characters? A) Using the substr function B) Using the find function C) Using the getline function D) Using the setw manipulator

A

A) Using the substr function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

for (size_t i = 0; i < showtimes.size(); i++) {
cout &laquo_space;left &laquo_space;setw(44) &laquo_space;movieTitles[i] &laquo_space;” | “ &laquo_space;right &laquo_space;setw(5) &laquo_space;movieRatings[i] &laquo_space;” | “;
for (size_t j = 1; XXXXXXX; j++) {
cout &laquo_space;showtimes[i][j];
if (j != showtimes[i].size()-1) {
cout &laquo_space;” “;
}
}
cout &laquo_space;endl;
}
return 0;
}
A) i < showtimes.size() B) j < showtimes[i].size() C) j != showtimes[i].size()-1 D) movieShowtimes[0] == title

A

B) j < showtimes[i].size()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

ostream

A

short for “output stream,” is a class that supports output, available via #include <iostream> and in namespace"std".</iostream>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

insertion operator

A

«

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

istream

A

short for “input stream,” is a class that supports input. Available via#include <iostream></iostream>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

manipulator

A

a function that overloads the insertion operator &laquo_space;or extraction operator&raquo_space; to adjust the way output appears.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

istringstream

A

reads input from an associated string instead of the keyboard

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

eof()

A

returns true or false depending on whether or not the end of the stream has been reached.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

ostringstream

A

insert characters into a string bufferinstead of the screen

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

fail()

A

returns true if the previous stream operation had an error.

17
Q

stream error

A

occurs when insertion or extraction fails, causing the stream to enter an errorstate.

18
Q

ofstream

A

short for “output file stream”, is a class that supports writing to a file.

19
Q

extraction operator

A

> >

20
Q

ifstream inputFile(inputFileName);
if (!inputFile.is_open()) {
cout &laquo_space;“Failed to open the input file.” &laquo_space;endl;
return 1;
}

What does if (!inputFile.is_open()) check? a) If the input file is open b) If the input file failed to open c) If the input file is empty d) If there was an error while reading the input file

A

b) If the input file failed to open

21
Q

Which library is used for basic input and output operations in C++? a) iostream b) string c) fstream d) sstream

A

a) iostream

22
Q

The getline() function is used to read a __________ from an input stream.

A

line

23
Q

The&raquo_space; operator is used for __________ from an input stream.

A

extraction

24
Q

The stringstream class can be used to manipulate __________.

A

strings

25
Q

What does the eof() function in C++ check for? a) Check if the input stream is in a fail state b) Check if the end of the file has been reached c) Check if a specific character is found in the input stream d) Check if the input stream is empty

A

b) Check if the end of the file has been reached

26
Q

What does the following line of code do? int day = stoi(date.substr(date.find(‘ ‘) + 1, date.find(‘,’) - date.find(‘ ‘) - 1));
A) Extracts the day from the input date string B) Extracts the year from the input date string C) Converts the day string to an integer D) Converts the year string to an integer

A

A) Extracts the day from the input date string

27
Q

What is the purpose of the following line of code? string month = date.substr(0, date.find(‘ ‘));
A) Extract the day from the input date B) Extract the month from the input date C) Extract the year from the input date D) Extract the month and day from the input date

A

B) Extract the month from the input date