Unit 3: Data Files, Constructors and Destructors - Practice Quiz
1 Which member function opens a file for an existing C++ file stream object?
write()
open()
close()
read()
2 Which member function should be used to disconnect a file from a file stream?
clear()
flush()
ignore()
close()
3 Which C++ file mode opens a file for appending data at the end?
ios::out
ios::in
ios::trunc
ios::app
4 Which C++ file mode is primarily used to open a file for reading?
ios::in
ios::ate
ios::app
ios::out
5 Which file stream function returns the current position of the input pointer?
tellg()
tellp()
seekp()
seekg()
6
Which operator is commonly used to write formatted data to an ofstream object?
<<
&&
==
>>
7 Which function reads an entire line of text from a file stream?
getline()
write()
get()
put()
8 In sequential file access, how are records normally processed?
9 Which function moves the input pointer to a specified location in a file?
tellg()
seekg()
seekp()
eof()
10 Which mode flag opens a file for binary operations in C++?
ios::binary
ios::text
ios::format
ios::input
11 Which member function writes a block of binary data to a file stream?
read()
write()
getline()
ignore()
12 Which stream class can both read from and write to files?
ifstream
fstream
ofstream
istream
13 Which C++ keyword is used to define a structure whose records may be stored in a file?
struct
class
union
enum
14 When is a constructor normally called for an object?
15 What is a default constructor?
16 What does a default argument in a constructor provide?
17 Which symbol appears before the class name in a destructor declaration?
#
:
~
&
18 What distinguishes a parameterized constructor from a default constructor?
19 What is the main purpose of a copy constructor?
20 Which symbol introduces a constructor initializer list in C++?
;
,
.
:
21 A program has finished using an input file but will continue running for a long time. Which statement releases the file resource immediately and safely?
in.flush(); closes the input file after transferring all buffered input to the program.
if (in.is_open()) in.close();
if (in.good()) in.open();
if (in.eof()) in.clear();
22
Which mode should be used with an ofstream to preserve existing content and force every new write to the end of the file?
ios::out | ios::trunc
ios::in | ios::out
ios::out | ios::app
ios::out | ios::ate
23 An input stream has read part of a binary file. Which pair of functions can obtain the current input position and then move it back to the beginning?
tellg() and seekg(0, ios::beg)
tellp() and seekp(0, ios::beg)
eof() and clear() because clearing the status flags also returns the input pointer to the start.
get() and putback(0)
24
After fin >> age;, the statement getline(fin, name); reads an empty string because a newline remains in the stream. Which replacement correctly reads the next nonblank line?
fin << name;
getline(fin, age);
fin.get(name);
getline(fin >> ws, name);
25 A file contains 1,000 fixed-size records, and the program frequently needs record 700 without processing earlier records. Which approach is most appropriate?
seekg() with the record's byte offset.
getline() until record 700 is reached.
26
Given int value = 25; and an open binary ofstream out, which statement writes the object's raw bytes?
out.write(value, sizeof(char));
out.write(reinterpret_cast<const char*>(&value), sizeof(value));
out << reinterpret_cast<const char*>(&value);
out.put(static_cast<char*>(value));
27
A Student class has private data, but objects must support syntax such as fin >> student. Which declaration best provides this operation while retaining private data?
istream& operator>>(istream& in);
friend Student operator>>(Student s, istream in);
friend istream& operator>>(istream& in, Student& s);
28
A binary file stores raw Record structures. Which loop processes only structures that were read successfully?
while (in.read(reinterpret_cast<char*>(&r), sizeof(r))) process(r);
do { process(r); } while (in.read(reinterpret_cast<char*>(&r), sizeof(r)));
while (!in.eof()) { in.read(reinterpret_cast<char*>(&r), sizeof(r)); process(r); }
while (in.good()) { process(r); in.read(reinterpret_cast<char*>(&r), sizeof(r)); }
29 When an object of a derived class is created, which order is used for initialization?
30
Consider class Box { public: Box(int size = 1); };. What happens when Box b; is declared?
size equal to 1.
31
What is the result of declaring both Item() and Item(int quantity = 1) and then writing Item x;?
Item() is always selected.
Item(int) is always selected.
32
A Derived object is allocated dynamically and stored in a Base*. What must Base provide so that delete ptr; correctly invokes both destructors?
33
Given class Meter { public: explicit Meter(int value); };, which object declaration is valid?
Meter m(5);
Meter m = 5;
Meter m;
Meter m = { };
34 A class owns a dynamically allocated array through a pointer member. What should its copy constructor generally do to avoid two objects deleting the same array?
nullptr.
35
Why must the members in class Entry { const int id; int& value; /* ... */ }; be initialized using a constructor initializer list?
36
What is the effect of opening an existing file using ofstream out("report.txt", ios::out | ios::trunc);?
37 An input stream has reached end-of-file. The program now needs to reread from the beginning. Which sequence is appropriate?
in.seekg(0, ios::beg); in.close();
in.flush(); in.tellg();
in.eof(); in.open();
in.clear(); in.seekg(0, ios::beg);
38
A binary file contains fixed-size Record objects indexed from zero. Which statement positions an output stream at record index for an update?
file.seekg(k + sizeof(Record), ios::beg);
file.seekp(k * sizeof(Record), ios::beg);
file.seekp(k, ios::end);
file.tellp(k * sizeof(Record));
39
What is the main purpose of including ios::binary when opening a file?
40
For Widget items[3];, in what order are the destructors called when the array leaves scope?
items[0] is destroyed automatically.
items[1], items[2], items[0]
items[2], items[1], items[0]
items[0], items[1], items[2]
41
Assume A.bin and B.bin initially exist and are empty. What happens in the following C++ code? std::ofstream f("A.bin", std::ios::binary); f.open("B.bin", std::ios::binary); bool failed = f.fail(); f.clear(); f << "X"; f.close();
failed is false; A.bin contains X, while B.bin remains empty.
failed is true; B.bin contains X, while A.bin remains empty.
failed is true; A.bin contains X, while B.bin remains empty.
failed is false; both files contain X after f.close().
42
An existing file data.bin contains 10 bytes. What is its size after executing std::ofstream out("data.bin", std::ios::binary | std::ios::ate); out.write("XY", 2); out.close();?
binary implicitly adds app when combined with ate.
ate preserves the file and every write is forced to its end.
ofstream adds out, which truncates the file before the initial end seek.
43
A file contains only 7. Consider int x = 0, y = 99; std::ifstream in("n.txt"); in >> x; in >> y; in.seekg(0); in >> y;. Which statement is correct?
y remains 99 because the failed extraction leaves failbit set, preventing the seek and later extraction.
y remains 99 because reaching EOF permanently closes the stream's underlying file buffer.
y becomes 7 because seekg(0) automatically clears both failbit and eofbit.
y becomes 0 because the failed extraction value-initializes the destination before seeking.
44
A text file contains 10 20x 30. What are the final values after int a=-1, b=-1, c=-1; in >> a >> b >> c; in.clear(); in.ignore(1); in >> c;?
a = 10, b = 20, and c = 30
a = 10, b = 20, and c = 0
a = 10, b = -1, and c = 30
a = 10, b = 20, and c = -1
45 A binary file has a header of bytes followed by fixed-size records of bytes. Which expression gives the starting offset of the record with zero-based index ?
static_cast<std::streamoff>(H) + static_cast<std::streamoff>(k) * S
static_cast<std::streamoff>(H - 1) + static_cast<std::streamoff>(k) * S
static_cast<std::streamoff>(H) * static_cast<std::streamoff>(k + S)
static_cast<std::streamoff>(H) + static_cast<std::streamoff>(k + 1) * S
46
Why is writing an object containing std::string name; using out.write(reinterpret_cast<const char*>(&obj), sizeof obj) unsuitable for later reconstruction?
ostream::write.
std::string reconstruction.
47 A class has private fields, validates all constructor arguments, and must be loaded from a portable binary file. Which design best preserves its invariants?
sizeof(Class) bytes directly into an object created with its ordinary default constructor.
48
A trivially copyable structure contains std::uint32_t id; char flag;. Why can raw write() and read() of the complete structure still fail as a portable file format?
49
Class D derives from B and declares members M m1; M m2; in that order. Which lifecycle order is guaranteed for a complete D object?
B, m2, m1, D, then ~D, ~m1, ~m2, ~B
D, B, m1, m2, then ~m2, ~m1, ~B, ~D
m1, m2, B, D, then ~D, ~B, ~m2, ~m1
B, m1, m2, D, then ~D, ~m2, ~m1, ~B
50
Given struct A { A(int value); };, which statement about A a; is correct?
A(int) is not marked explicit in the class definition.
A() and then assigns zero to the unnamed constructor parameter.
A(int) suppresses generation of an implicit default constructor.
51
What is the result of struct C { C(); C(int x = 0); }; C value;?
C(int) because its default argument supplies a better conversion sequence.
C() because a zero-parameter constructor always has higher priority.
52
Given struct Base { ~Base() {} }; struct Derived : Base { ~Derived() {} };, what is the effect of Base* p = new Derived; delete p;?
Base lacks a virtual destructor.
Derived contains no non-static data members.
Derived.
Base::~Base() runs, and the behavior is otherwise well-defined.
53
For struct P { explicit P(int); }; void consume(P);, which pair of statements is well-formed?
P a = 3; and consume(3);
P a = {3}; and consume({3});
P a(3); and P b{3};
P a; and consume(P());
54
A class Buffer owns memory through int* data, deletes it in its destructor, and defines no copy operations. What is the main risk of Buffer second = first;?
second, leaving first in an automatically empty state.
55
Under C++17, consider struct C { int y; int x; C() : x(5), y(x + 1) {} };. What is the key issue?
y is initialized before x, so evaluating x + 1 reads an indeterminate value.
y is default-initialized and then assigned after x, so both members are valid.
x is initialized first because it appears first in the initializer list, so y becomes 6.
56
An existing file contains ABCDE. What does std::fstream f("x.txt", std::ios::in | std::ios::out | std::ios::app); f.seekp(0); f << "Z"; produce after the stream is closed?
ZBCDE, because seekp(0) overrides the initial append position.
ABCDEZ, because app forces each output operation to the end.
ZABCDE, because app inserts output at the requested position.
ABCDE, because combining in, out, and app disables output.
57
A binary std::fstream io is open with in | out. After writing a value, which sequence most robustly prepares the stream to read that value from the beginning?
io.clear(); io.read(buffer, size); io.seekg(0, std::ios::beg);
io.flush(); io.seekg(0, std::ios::beg); io.read(buffer, size);
io.seekp(0, std::ios::beg); io.read(buffer, size); io.flush();
io.read(buffer, size); io.clear(); io.seekp(0, std::ios::end);
58
A file contains fixed-size records of S bytes with no header. To read, modify, and overwrite record i without changing file size, which operation sequence is correct?
(i+1)*S, read the record, and write it at the same position.
i*S, read, then seek the get position to i*S and write.
i*S, read, then seek the put position to i*S and write.
i, modify it, and write immediately without repositioning.
59
A binary file contains two complete records plus half of a third record. What happens with while (in.read(reinterpret_cast<char*>(&r), sizeof r)) { ++count; } when count initially equals zero?
count becomes 2; the partial read fails, and gcount() reports half a record.
count becomes 3; the loop body runs whenever at least one byte was extracted.
count becomes 2; the partial record is discarded and gcount() becomes zero.
count becomes 3; read() zero-fills the missing half of the final record.
60
While constructing a derived object, its base subobject and member m1 are constructed successfully, but member m2's constructor throws. Which destruction behavior is guaranteed?
m1 and the base are destroyed in that order; neither m2 nor the derived object is destroyed.
m2, m1, and the base are destroyed, but the derived destructor body is not executed.
m2, m1, and the base are all destroyed in reverse declaration order.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →