Unit 3: Constructors, Destructors and Managing File Operations - Practice Quiz

CAP455 — Object Oriented Programming Using C++ 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which statement correctly describes the name of a constructor in C++?

Features of constructor function Easy
A. It is the same as the class name.
B. It can have any valid identifier.
C. It is the same as the object name.
D. It is always named constructor.

2 What return type is specified for a constructor?

Features of constructor function Easy
A. The class type
B. void
C. int
D. No return type

3 What is a default constructor?

Default constructor Easy
A. A constructor declared outside the class
B. A constructor returning a default value
C. A constructor used only for arrays
D. A constructor callable without arguments

4 Which feature distinguishes a constructor from a normal member function?

Constructor vs normal function Easy
A. It must always return an integer value.
B. It can only be called using a pointer.
C. It is called automatically during object creation.
D. It must always be declared as const.

5 What is the main purpose of a parameterized constructor?

Parameterized constructor Easy
A. To initialize objects with supplied values
B. To copy files with supplied values
C. To delete objects with supplied values
D. To open files with supplied values

6 Which is a common declaration of a copy constructor for a class named Sample?

Copy constructor Easy
A. Sample(int other);
B. Sample copy(Sample other);
C. Sample(const Sample& other);
D. void Sample(Sample other);

7 When is a copy constructor used?

Copy constructor Easy
A. When opening a binary file
B. When declaring a static function
C. When destroying an existing object
D. When creating an object from another object

8 Which symbol introduces a constructor initializer list?

Initializer lists Easy
A. A semicolon ;
B. A period .
C. A colon :
D. A comma ,

9 Which data member must be initialized using a constructor initializer list?

Initializer lists Easy
A. A pointer data member
B. A public data member
C. A const data member
D. A static data member

10 What is the benefit of default arguments in a constructor?

Constructor with default arguments Easy
A. They prevent the creation of objects.
B. They allow omitted arguments to use predefined values.
C. They change the constructor's return type.
D. They force every argument to be supplied.

11 Which is the correct destructor declaration for a class named Book?

Destructor Easy
A. Book~();
B. ~Book();
C. delete Book();
D. void ~Book();

12 When is a destructor normally called for a local object?

Destructor Easy
A. When the object is first declared
B. When a file is first opened
C. When the object goes out of scope
D. When a constructor is overloaded

13 Which member function closes an open C++ file stream?

Opening and closing of files Easy
A. close()
B. clear()
C. stop()
D. end()

14 Which file mode opens a file for appending data at the end?

Modes of file Easy
A. ios::binary
B. ios::app
C. ios::out
D. ios::in

15 Which file stream function checks whether a file was successfully opened?

File stream functions Easy
A. is_closed()
B. is_ready()
C. is_open()
D. is_empty()

16 Which stream class is primarily used to write data to a file?

Reading and writing of files Easy
A. istream
B. ifstream
C. iostream
D. ofstream

17 What does sequential file access mean?

Sequential access and random access for file processing Easy
A. Jumping directly to any selected file position
B. Processing several files at exactly the same time
C. Processing data in order from one position to the next
D. Storing every record at a random memory address

18 Which functions are commonly used for unformatted binary file input and output?

Binary file operations Easy
A. read() and write()
B. open() and close()
C. seekg() and seekp()
D. get() and put()

19 How can a class object safely store its data in a text file?

Classes and file operations Easy
A. Call the destructor before opening the output stream.
B. Write each required data member through an output stream.
C. Convert the class declaration into a file mode.
D. Rename the object to match the output file.

20 How can values read from a file be stored in a structure variable?

Structures and file operations Easy
A. Read the values into the structure's members.
B. Close the file before reading any values.
C. Replace the structure with a file mode.
D. Read the values into the structure's name.

21 Which declaration correctly defines a constructor for a class named Account?

Features of constructor function Medium
A. void Account();
B. int Account();
C. Account();
D. Account create();

22 Consider class Counter { int value; public: Counter() : value(0) {} };. What is the value of value immediately after Counter c; is executed?

Default constructor Medium
A. 1
B. 0
C. A compilation error
D. An indeterminate value

23 A class Device has a constructor Device() and a member function void reset(). Which statement correctly describes their invocation?

Constructor vs normal function Medium
A. Only the constructor is called automatically on creation
B. Neither can be called during object creation
C. Both are called automatically when an object is created
D. Only 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?

Parameterized constructor Medium
A. 24
B. 10
C. 46
D. 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?

Copy constructor Medium
A. Both objects may delete the same memory
B. The copied object becomes a constant object
C. The pointer is automatically set to null
D. A separate memory block is automatically allocated

26 For Book b1; Book b2 = b1;, which constructor is selected for b2?

Copy constructor Medium
A. The default constructor
B. The parameterized constructor
C. The move assignment operator
D. The copy constructor

27 Why must a reference data member normally be initialized using a constructor initializer list?

Initializer lists Medium
A. A reference must be bound when it is initialized
B. A reference can be assigned only inside a destructor
C. A reference requires dynamic memory allocation
D. A reference always receives a default null value

28 For class Pair { int first; int second; public: Pair() : second(20), first(10) {} };, in what order are the members initialized?

Initializer lists Medium
A. Both members simultaneously
B. The order is implementation-defined
C. first, then second
D. second, then first

29 Consider class Item { public: Item(int n = 0) {} Item() {} };. What happens when Item x; is compiled?

Constructor with default arguments Medium
A. The call is ambiguous
B. The no-argument constructor is selected
C. The integer constructor is selected
D. Both constructors execute in sequence

30 A program deletes a derived object through a base-class pointer. Which base-class declaration ensures that the derived destructor is also invoked?

Destructor Medium
A. static ~Base() {}
B. explicit ~Base() {}
C. friend ~Base() {}
D. 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?

Destructor Medium
A. b, c, a
B. a, b, c
C. a, c, b
D. c, b, a

32 Which code opens data.txt for both input and output using an fstream object named file?

Opening and closing of files Medium
A. file.open("data.txt", std::ios::app | std::ios::trunc);
B. file.open("data.txt", std::ios::binary | std::ios::app);
C. file.open("data.txt", std::ios::in & std::ios::out);
D. 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?

Modes of file Medium
A. app reads binary data, while ate reads formatted text
B. ate truncates the file, while app preserves only the first record
C. app forces every write to the end, while ate initially positions at the end
D. 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?

File stream functions Medium
A. Call flush() and then call seekp()
B. Call eof() and then call write()
C. Call close() and then call tellg()
D. Call 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?

Reading and writing of files Medium
A. std::getline(file >> std::ws, name);
B. file.seekp(std::ws, name);
C. file.write(name, std::ws);
D. std::getline(file << std::ws, name);

36 Which statement moves an input file pointer to byte position 100 from the beginning of the file?

Sequential access and random access for file processing Medium
A. file.tellg(100, std::ios::beg);
B. file.seekg(100, std::ios::end);
C. file.seekp(100, std::ios::cur);
D. 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?

Sequential access and random access for file processing Medium
A. i + sizeof(Record)
B. i * sizeof(Record)
C. sizeof(i) * sizeof(Record)
D. (i + 1) * sizeof(Record)

38 Given a trivially copyable object p, which statement writes its raw bytes to a binary output stream named out?

Binary file operations Medium
A. out << reinterpret_cast<const char*>(&p);
B. out.write(reinterpret_cast<const char*>(&p), sizeof(p));
C. out.read(reinterpret_cast<char*>(&p), sizeof(p));
D. 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?

Classes and file operations Medium
A. Declare the constructor as a file stream object
B. Define a public member function such as save()
C. Make the file stream inherit from the class
D. Convert every private member into a global variable

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?

Structures and file operations Medium
A. Write only the address of the structure object
B. Write the structure through an uninitialized pointer
C. Write each field separately in a defined format
D. Write the entire structure using its memory size

41 Consider class A { public: A(int = 0); };. Which statement about A a; is correct?

Features of constructor function Hard
A. It calls the constructor only after explicit casting
B. It is rejected because no default constructor exists
C. It calls a compiler-generated constructor
D. It calls A(int) using its default argument

42 What happens when struct S { int x; S() = default; }; S value; is declared with automatic storage duration?

Default constructor Hard
A. x is initialized to zero automatically
B. The declaration is rejected by the compiler
C. x receives the value -1
D. x remains indeterminate

43 Which declaration is invalid specifically because constructors cannot have a return type?

Constructor vs normal function Hard
A. Widget(const Widget&);
B. Widget(int);
C. void Widget();
D. Widget();

44 Given class Buffer { int n; public: Buffer(int size) : n(size) {} };, which declaration is valid?

Parameterized constructor Hard
A. Buffer b(8);
B. Buffer b;
C. Buffer b = {};
D. 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?

Copy constructor Hard
A. It prevents the original object from being destroyed
B. It converts p into a reference
C. It copies the pointed-to integer twice
D. It performs a shallow copy of p

46 Why must const int id and Logger logger be initialized in an initializer list rather than assigned in the constructor body?

Initializer lists Hard
A. They cannot be assigned after construction begins
B. They are invisible inside the constructor body
C. They are initialized before local variables
D. They always require dynamic allocation

47 In class X { int a; int b; public: X() : b(2), a(b) {} };, what value does a receive?

Initializer lists Hard
A. The value 2
B. Compilation always fails
C. The value 0
D. An indeterminate value

48 Given class Point { public: Point(int x = 1, int y = 2); };, which statement is true?

Constructor with default arguments Hard
A. Point p(5, 6, 7); ignores the third argument
B. Point p(5); uses x = 5 and y = 2
C. Point p(5); initializes both coordinates to 5
D. 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?

Destructor Hard
A. A destructor with one parameter
B. A static destructor
C. A protected nonvirtual destructor
D. A public virtual destructor

50 If a constructor acquires a file resource and then a later member construction throws an exception, which destructor behavior is correct?

Destructor Hard
A. The destructor runs twice to release all members
B. The destructor of the incomplete object runs
C. No destructor runs for the incomplete object
D. Only the constructor body is rerun

51 What is the correct way to distinguish failure of std::ifstream in("data.txt"); from an empty but successfully opened file?

Opening and closing of files Hard
A. Test whether in.peek() returns whitespace
B. Test in.eof() immediately
C. Test in.fail() or in.is_open()
D. Test whether in.tellg() equals zero

52 Which mode combination preserves existing output-file contents while appending new data at the end?

Modes of file Hard
A. std::ios::out | std::ios::app
B. std::ios::in | std::ios::ate
C. std::ios::out
D. std::ios::trunc

53 After std::ifstream in("x");, which operation is most appropriate for determining the byte offset of the next input operation?

File stream functions Hard
A. in.tellg()
B. in.seekp(0)
C. in.tellp()
D. in.flush()

54 Why is while (!in.eof()) { in >> value; process(value); } incorrect for token input?

Reading and writing of files Hard
A. The loop can process value after extraction has failed
B. Formatted extraction never changes stream state
C. EOF is set before the final token is read
D. The extraction operator reads only binary data

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?

Sequential access and random access for file processing Hard
A. in.seekg(R / k, std::ios::end)
B. in.seekg(k + R, std::ios::cur)
C. in.seekg(k * R, std::ios::beg)
D. 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?

Binary file operations Hard
A. A trivially copyable record with no pointers
B. An array of bytes
C. A class containing a pointer member
D. A struct containing only fixed-width integers

57 Why can writing a binary representation of a struct containing bool, int, and double fail to be portable across systems?

Binary file operations Hard
A. Binary files cannot store floating-point values
B. The write function always reverses byte order
C. Object layout, padding, and type representation may differ
D. Binary streams convert values to decimal text

58 A class owns an std::ofstream member. Which design best prevents accidental copying of an object that represents one active file session?

Classes and file operations Hard
A. Define a copy constructor that copies the stream bytes
B. Delete copy construction and copy assignment
C. Make the stream member static
D. Convert the stream member to a raw pointer

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?

Classes and file operations Hard
A. Only flush() is ever required
B. A positioning operation or suitable synchronization is required
C. The stream must be converted to std::ifstream
D. The file must be closed and reopened every time

60 Which structure is most suitable for direct fixed-record binary storage when portability is not required?

Structures and file operations Hard
A. A trivially copyable structure with fixed-size fields
B. A structure containing a virtual function
C. A structure containing a raw pointer
D. A structure containing std::string