Unit 3: Constructors, Destructors and Managing File Operations - Practice Quiz
1 Which statement correctly describes the name of a constructor in C++?
constructor.
2 What return type is specified for a constructor?
void
int
3 What is a default constructor?
4 Which feature distinguishes a constructor from a normal member function?
const.
5 What is the main purpose of a parameterized constructor?
6
Which is a common declaration of a copy constructor for a class named Sample?
Sample(int other);
Sample copy(Sample other);
Sample(const Sample& other);
void Sample(Sample other);
7 When is a copy constructor used?
8 Which symbol introduces a constructor initializer list?
;
.
:
,
9 Which data member must be initialized using a constructor initializer list?
const data member
10 What is the benefit of default arguments in a constructor?
11
Which is the correct destructor declaration for a class named Book?
Book~();
~Book();
delete Book();
void ~Book();
12 When is a destructor normally called for a local object?
13 Which member function closes an open C++ file stream?
close()
clear()
stop()
end()
14 Which file mode opens a file for appending data at the end?
ios::binary
ios::app
ios::out
ios::in
15 Which file stream function checks whether a file was successfully opened?
is_closed()
is_ready()
is_open()
is_empty()
16 Which stream class is primarily used to write data to a file?
istream
ifstream
iostream
ofstream
17 What does sequential file access mean?
18 Which functions are commonly used for unformatted binary file input and output?
read() and write()
open() and close()
seekg() and seekp()
get() and put()
19 How can a class object safely store its data in a text file?
20 How can values read from a file be stored in a structure variable?
21
Which declaration correctly defines a constructor for a class named Account?
void Account();
int Account();
Account();
Account create();
22
Consider class Counter { int value; public: Counter() : value(0) {} };. What is the value of value immediately after Counter c; is executed?
1
0
23
A class Device has a constructor Device() and a member function void reset(). Which statement correctly describes their invocation?
reset() is called automatically on creation
24
Given class Rectangle { int w, h; public: Rectangle(int x, int y) : w(x), h(y) {} int area() { return w * h; } };, what does Rectangle r(4, 6); r.area(); return?
24
10
46
20
25 A class owns memory through a pointer and uses the compiler-generated copy constructor. What is the most likely problem when one object is copied to another?
26
For Book b1; Book b2 = b1;, which constructor is selected for b2?
27 Why must a reference data member normally be initialized using a constructor initializer list?
28
For class Pair { int first; int second; public: Pair() : second(20), first(10) {} };, in what order are the members initialized?
first, then second
second, then first
29
Consider class Item { public: Item(int n = 0) {} Item() {} };. What happens when Item x; is compiled?
30 A program deletes a derived object through a base-class pointer. Which base-class declaration ensures that the derived destructor is also invoked?
static ~Base() {}
explicit ~Base() {}
friend ~Base() {}
virtual ~Base() {}
31
Inside one block, objects are created as Resource a; Resource b; Resource c;. In which order are their destructors called when the block ends normally?
b, c, a
a, b, c
a, c, b
c, b, a
32
Which code opens data.txt for both input and output using an fstream object named file?
file.open("data.txt", std::ios::app | std::ios::trunc);
file.open("data.txt", std::ios::binary | std::ios::app);
file.open("data.txt", std::ios::in & std::ios::out);
file.open("data.txt", std::ios::in | std::ios::out);
33
What is the main difference between std::ios::app and std::ios::ate when opening an output file?
app reads binary data, while ate reads formatted text
ate truncates the file, while app preserves only the first record
app forces every write to the end, while ate initially positions at the end
ate forces every write to the end, while app initially positions at the end
34 After a read operation reaches end-of-file, a program wants to reposition the input pointer and read again. Which sequence should it use?
flush() and then call seekp()
eof() and then call write()
close() and then call tellg()
clear() and then call seekg()
35
After file >> age;, a program uses std::getline(file, name);, but name becomes empty because a newline remains in the stream. Which expression can be used to consume leading whitespace while reading the line?
std::getline(file >> std::ws, name);
file.seekp(std::ws, name);
file.write(name, std::ws);
std::getline(file << std::ws, name);
36
Which statement moves an input file pointer to byte position 100 from the beginning of the file?
file.tellg(100, std::ios::beg);
file.seekg(100, std::ios::end);
file.seekp(100, std::ios::cur);
file.seekg(100, std::ios::beg);
37
A binary file stores fixed-size Record objects. What byte offset should be used to access record index i, where indexing begins at 0?
i + sizeof(Record)
i * sizeof(Record)
sizeof(i) * sizeof(Record)
(i + 1) * sizeof(Record)
38
Given a trivially copyable object p, which statement writes its raw bytes to a binary output stream named out?
out << reinterpret_cast<const char*>(&p);
out.write(reinterpret_cast<const char*>(&p), sizeof(p));
out.read(reinterpret_cast<char*>(&p), sizeof(p));
out.seekp(reinterpret_cast<long>(&p), sizeof(p));
39 A class has private data members that must be saved to a file. Which design allows direct access to those members while preserving their private visibility?
save()
40
A structure contains an int, a double, and a character array. Which approach best avoids dependence on compiler-specific structure padding when storing data for exchange between systems?
41
Consider class A { public: A(int = 0); };. Which statement about A a; is correct?
A(int) using its default argument
42
What happens when struct S { int x; S() = default; }; S value; is declared with automatic storage duration?
x is initialized to zero automatically
x receives the value -1
x remains indeterminate
43 Which declaration is invalid specifically because constructors cannot have a return type?
Widget(const Widget&);
Widget(int);
void Widget();
Widget();
44
Given class Buffer { int n; public: Buffer(int size) : n(size) {} };, which declaration is valid?
Buffer b(8);
Buffer b;
Buffer b = {};
Buffer b = Buffer();
45
For class C { int* p; public: C(int v) : p(new int(v)) {} };, what is the primary problem with the compiler-generated copy constructor?
p into a reference
p
46
Why must const int id and Logger logger be initialized in an initializer list rather than assigned in the constructor body?
47
In class X { int a; int b; public: X() : b(2), a(b) {} };, what value does a receive?
2
0
48
Given class Point { public: Point(int x = 1, int y = 2); };, which statement is true?
Point p(5, 6, 7); ignores the third argument
Point p(5); uses x = 5 and y = 2
Point p(5); initializes both coordinates to 5
Point p; is ill-formed because two arguments are required
49
A base class is used polymorphically through Base* p = new Derived;. Which destructor declaration is necessary for delete p; to destroy the complete object correctly?
50 If a constructor acquires a file resource and then a later member construction throws an exception, which destructor behavior is correct?
51
What is the correct way to distinguish failure of std::ifstream in("data.txt"); from an empty but successfully opened file?
in.peek() returns whitespace
in.eof() immediately
in.fail() or in.is_open()
in.tellg() equals zero
52 Which mode combination preserves existing output-file contents while appending new data at the end?
std::ios::out | std::ios::app
std::ios::in | std::ios::ate
std::ios::out
std::ios::trunc
53
After std::ifstream in("x");, which operation is most appropriate for determining the byte offset of the next input operation?
in.tellg()
in.seekp(0)
in.tellp()
in.flush()
54
Why is while (!in.eof()) { in >> value; process(value); } incorrect for token input?
value after extraction has failed
55
A fixed-size binary record has size R bytes, and record numbering starts at zero. Which expression positions an input stream at record k?
in.seekg(R / k, std::ios::end)
in.seekg(k + R, std::ios::cur)
in.seekg(k * R, std::ios::beg)
in.seekg(k, std::ios::beg)
56
Which type is unsafe to serialize directly with out.write(reinterpret_cast<const char*>(&obj), sizeof obj) without additional design constraints?
57
Why can writing a binary representation of a struct containing bool, int, and double fail to be portable across systems?
write function always reverses byte order
58
A class owns an std::ofstream member. Which design best prevents accidental copying of an object that represents one active file session?
59
A method writes a record and then immediately reads it using the same std::fstream. What is required when switching from output to input?
flush() is ever required
std::ifstream
60 Which structure is most suitable for direct fixed-record binary storage when portability is not required?
std::string
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 →