Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 3 MCQsData File operations, Constructors, Destructors and File Handling
Which header file is required to perform file input/output operations in C++?
Not quite. Try another option.
Answer: <fstream>
The <fstream> header file provides definitions for the stream classes ifstream, ofstream, and fstream used for file handling.
Question 2 of 50
Which class is used specifically for writing data to a file?
Not quite. Try another option.
Answer: ofstream
The 'ofstream' (output file stream) class is used to create files and write information to them.
Question 3 of 50
Which class is used specifically for reading data from a file?
Not quite. Try another option.
Answer: ifstream
The 'ifstream' (input file stream) class is used to read information from files.
Question 4 of 50
What is the return type of a constructor?
Not quite. Try another option.
Answer: No return type
Constructors do not have a return type, not even void.
Question 5 of 50
Which file mode is used to append content to the end of an existing file?
Not quite. Try another option.
Answer: ios::app
ios::app (append) causes all output operations to be performed at the end of the file.
Question 6 of 50
Which symbol is used to prefix the destructor name?
Not quite. Try another option.
Answer: ~
A destructor is declared with the tilde (~) symbol followed by the class name.
Question 7 of 50
Which function is used to close a file explicitly in C++?
Not quite. Try another option.
Answer: close()
The close() member function is used to close the file associated with the stream.
Question 8 of 50
What happens if a file is opened in 'ios::out' mode and the file already exists?
Not quite. Try another option.
Answer: The file contents are truncated (deleted)
By default, opening an existing file in ios::out mode truncates the file to zero length unless ios::app or ios::in is also specified.
Question 9 of 50
Which constructor is called when an object is initialized with another object of the same class?
Not quite. Try another option.
Answer: Copy Constructor
A copy constructor is a member function that initializes an object using another object of the same class.
Question 10 of 50
Which syntax correctly defines a copy constructor for class 'A'?
Not quite. Try another option.
Answer: A(const A &obj)
The copy constructor takes a reference to an object of the same class as an argument (often const).
Question 11 of 50
What is the purpose of the 'write()' function in file handling?
Not quite. Try another option.
Answer: To write binary data blocks
The write() function is used to write a block of binary data to a file stream.
Question 12 of 50
Which function is used to move the get pointer (input pointer) in a file?
Not quite. Try another option.
Answer: seekg()
seekg() stands for 'seek get' and is used to move the file pointer for reading operations.
Question 13 of 50
What does 'ios::binary' mode do?
Not quite. Try another option.
Answer: Opens file in binary mode preventing character translation
ios::binary opens the file in binary mode, preventing the automatic translation of newline characters (e.g.,
to
).
Question 14 of 50
Which function returns the current position of the output pointer?
Not quite. Try another option.
Answer: tellp()
tellp() returns the current position of the put (output) pointer.
Question 15 of 50
When is a destructor called?
Not quite. Try another option.
Answer: When an object goes out of scope
Destructors are automatically invoked when an object goes out of scope or is explicitly deleted.
Question 16 of 50
Can a constructor be overloaded?
Not quite. Try another option.
Answer: Yes
Constructors can be overloaded like any other function, allowing objects to be initialized in different ways.
Question 17 of 50
What is an Initializer List used for?
Not quite. Try another option.
Answer: Initializing data members before the constructor body executes
An initializer list is used to initialize data members directly, which is more efficient and sometimes necessary (e.g., for const or reference members).
Question 18 of 50
Which member must be initialized using an initializer list?
Not quite. Try another option.
Answer: Constant members
Non-static const data members and reference data members must be initialized using an initializer list because they cannot be assigned values in the constructor body.
Question 19 of 50
What is the correct syntax to read an object 'obj' of class 'Student' from a binary file using ifstream object 'fin'?
Not quite. Try another option.
Answer: fin.read((char*)&obj, sizeof(obj));
The read function expects a char pointer and the size of the data. The object address must be cast to (char*).
Question 20 of 50
What is the default access mode for an 'ofstream' object?
Not quite. Try another option.
Answer: ios::out
The default mode for ofstream is ios::out (write only).
Question 21 of 50
Which function checks if the End-Of-File has been reached?
Not quite. Try another option.
Answer: eof()
The eof() function returns true if the end of the file has been reached during a read operation.
Question 22 of 50
How many destructors can a class have?
Not quite. Try another option.
Answer: Only one
A class can have only one destructor. It takes no arguments and cannot be overloaded.
Question 23 of 50
If a programmer does not define any constructor, what does the compiler do?
Not quite. Try another option.
Answer: Supplies a default constructor
If no constructor is explicitly defined, the compiler generates a default constructor that takes no parameters.
Question 24 of 50
What is the output of sizeof(fstream) roughly dependent on?
Not quite. Try another option.
Answer: The buffer size and internal state variables
The size of an fstream object depends on its internal data structures (buffers, state flags, pointers) defined by the implementation, not the file size on disk.
Question 25 of 50
Which combination of modes allows reading and writing to a file while preserving original content?
Not quite. Try another option.
Answer: ios::in | ios::out
Opening with both ios::in and ios::out allows update operations without implicitly truncating the file.
Question 26 of 50
Which stream function reads a line of text, including spaces?
Not quite. Try another option.
Answer: getline()
getline() reads a stream of characters into a string or buffer until a delimiter (usually newline) is found, preserving spaces.
Question 27 of 50
Which flag sets the file pointer to the end of the file immediately after opening?
Not quite. Try another option.
Answer: ios::ate
ios::ate (at end) seeks to the end of the stream immediately upon opening.
Question 28 of 50
What implies 'Random Access' in file handling?
Not quite. Try another option.
Answer: Accessing data at any position directly without reading sequentially
Random access allows moving the file pointer to any specific location using seekg/seekp to read/write directly.
Question 29 of 50
Can a destructor accept arguments?
Not quite. Try another option.
Answer: No, never
Destructors take no arguments and have no return type.
Question 30 of 50
What is a parameterized constructor?
Not quite. Try another option.
Answer: A constructor that takes arguments
A parameterized constructor accepts arguments to initialize an object with specific values.
Question 31 of 50
Which seek direction moves the pointer relative to the beginning of the file?
Not quite. Try another option.
Answer: ios::beg
ios::beg is the offset reference for the beginning of the stream.
Question 32 of 50
When working with Structures and Files, why is reinterpret_cast or C-style cast used in write?
Not quite. Try another option.
Answer: To convert the structure pointer to a char pointer for byte-wise writing
The write function expects a const char*. Pointers to structures must be cast to treat the memory as a sequence of bytes.
Question 33 of 50
Which of the following constitutes a default constructor?
Not quite. Try another option.
Answer: Both A(int x = 0) and A()
A constructor that can be called with no arguments is a default constructor. This includes an empty parameter list A() or one where all parameters have default values A(int x = 0).
Question 34 of 50
If a class has a pointer member, what is the risk of using the default copy constructor?
Not quite. Try another option.
Answer: Shallow copy leading to dangling pointers
The default copy constructor performs a shallow copy (copying address). If one object deletes the memory, the other holds a dangling pointer.
Question 35 of 50
Which function puts a single character into the file stream?
Not quite. Try another option.
Answer: put()
The put() function writes a single character to the output stream.
Question 36 of 50
In file.seekg(n, ios::cur);, what does n represent?
Not quite. Try another option.
Answer: The number of bytes to move offset
The first argument in seekg is the offset (number of bytes) to move relative to the position defined by the second argument.
Question 37 of 50
Which mode is required to perform input and output operations on the same file using fstream?
Not quite. Try another option.
Answer: ios::in | ios::out
fstream requires both ios::in and ios::out flags to allow both reading and writing.
Question 38 of 50
The order of destruction of objects is:
Not quite. Try another option.
Answer: Reverse of construction
Objects are destroyed in the reverse order of their creation (LIFO - Last In, First Out for stack objects).
Question 39 of 50
Which error handling function returns true if a non-fatal error (like format mismatch) occurs?
Not quite. Try another option.
Answer: fail()
fail() returns true if a logical error on I/O operation occurs (e.g., reading a letter when an int is expected).
Question 40 of 50
Can a constructor accept a reference to its own class type as a value parameter (e.g. A(A a))?
Not quite. Try another option.
Answer: No
This would cause infinite recursion because passing by value invokes the copy constructor, which would invoke it again, and so on. It must be passed by reference.
Question 41 of 50
What is the main advantage of using binary files over text files for storing objects?
Not quite. Try another option.
Answer: Efficiency and direct memory mapping
Binary files store data exactly as it appears in memory, making read/write operations faster (no conversion) and more compact.
Question 42 of 50
The open() function takes how many arguments generally?
Not quite. Try another option.
Answer: 2 (Filename and Mode)
Standard open() takes the filename and the open mode (though the mode often has a default).
Question 43 of 50
What is a deep copy?
Not quite. Try another option.
Answer: Allocating new memory and copying the actual data
Deep copy ensures that if an object has pointers, the data pointed to is also duplicated, ensuring independence between objects.
Question 44 of 50
Which symbol is used for the Initializer List syntax?
Not quite. Try another option.
Answer: :
A colon (:) is placed after the constructor's parameter list to start the initializer list.
Question 45 of 50
If fstream file; is declared, which function opens the file?
Not quite. Try another option.
Answer: file.open()
The open() member function is used to associate the file stream object with a physical file.
Question 46 of 50
When using ios::trunc, what happens to the existing file content?
Not quite. Try another option.
Answer: It is completely removed
ios::trunc discards the current contents of the file, reducing its size to zero.
Question 47 of 50
Can constructors be declared as private?
Not quite. Try another option.
Answer: Yes, to prevent object creation from outside the class
Private constructors are valid and are often used in the Singleton design pattern to control object instantiation.
Question 48 of 50
What happens if a destructor is not virtual in a base class when deleting a derived class object via a base pointer?
Not quite. Try another option.
Answer: Derived class destructor is not called (memory leak)
If the base destructor is not virtual, deleting a derived object through a base pointer results in undefined behavior, typically meaning the derived destructor is never executed.
Question 49 of 50
Which function returns the current get position in an input stream?
Not quite. Try another option.
Answer: tellg()
tellg() returns the position of the current character in the input stream.
Question 50 of 50
In the context of file modes, what does ios::ate stand for?
Not quite. Try another option.
Answer: At The End
ios::ate stands for 'At The End'. It moves the pointer to the end upon opening but allows seeking to other positions (unlike app which forces writing at the end).
That's unit 3
Answer a few questions and your score shows up here.
Nearly through. Did unit 3 of CSE202 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 3 of CSE202 save you a night before the exam?
Then you can help keep it free for the next batch. 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.