1Which header file is required to perform file input/output operations in C++?
A.<iostream>
B.<fstream>
C.<stdio.h>
D.<conio.h>
Correct Answer: <fstream>
Explanation:The <fstream> header file provides definitions for the stream classes ifstream, ofstream, and fstream used for file handling.
Incorrect! Try again.
2Which class is used specifically for writing data to a file?
A.ifstream
B.ostream
C.ofstream
D.filebuf
Correct Answer: ofstream
Explanation:The 'ofstream' (output file stream) class is used to create files and write information to them.
Incorrect! Try again.
3Which class is used specifically for reading data from a file?
A.ifstream
B.ofstream
C.fstream
D.iostream
Correct Answer: ifstream
Explanation:The 'ifstream' (input file stream) class is used to read information from files.
Incorrect! Try again.
4What is the return type of a constructor?
A.int
B.void
C.class type
D.No return type
Correct Answer: No return type
Explanation:Constructors do not have a return type, not even void.
Incorrect! Try again.
5Which file mode is used to append content to the end of an existing file?
A.ios::app
B.ios::ate
C.ios::trunc
D.ios::out
Correct Answer: ios::app
Explanation:ios::app (append) causes all output operations to be performed at the end of the file.
Incorrect! Try again.
6Which symbol is used to prefix the destructor name?
A.!
B.#
C.~
D.@
Correct Answer: ~
Explanation:A destructor is declared with the tilde (~) symbol followed by the class name.
Incorrect! Try again.
7Which function is used to close a file explicitly in C++?
A.finish()
B.end()
C.stop()
D.close()
Correct Answer: close()
Explanation:The close() member function is used to close the file associated with the stream.
Incorrect! Try again.
8What happens if a file is opened in 'ios::out' mode and the file already exists?
A.Data is appended
B.The file is opened in read-only mode
C.The file contents are truncated (deleted)
D.An error occurs
Correct Answer: The file contents are truncated (deleted)
Explanation: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.
Incorrect! Try again.
9Which constructor is called when an object is initialized with another object of the same class?
A.Default Constructor
B.Parameterized Constructor
C.Copy Constructor
D.Dynamic Constructor
Correct Answer: Copy Constructor
Explanation:A copy constructor is a member function that initializes an object using another object of the same class.
Incorrect! Try again.
10Which syntax correctly defines a copy constructor for class 'A'?
A.A(A obj)
B.A(const A &obj)
C.A(A *obj)
D.void A(A obj)
Correct Answer: A(const A &obj)
Explanation:The copy constructor takes a reference to an object of the same class as an argument (often const).
Incorrect! Try again.
11What is the purpose of the 'write()' function in file handling?
A.To write formatted text
B.To write a single character
C.To write binary data blocks
D.To write a string
Correct Answer: To write binary data blocks
Explanation:The write() function is used to write a block of binary data to a file stream.
Incorrect! Try again.
12Which function is used to move the get pointer (input pointer) in a file?
A.seekp()
B.seekg()
C.tellp()
D.tellg()
Correct Answer: seekg()
Explanation:seekg() stands for 'seek get' and is used to move the file pointer for reading operations.
Incorrect! Try again.
13What does 'ios::binary' mode do?
A.Opens file for text operations
B.Opens file in binary mode preventing character translation
C.Compresses the file
D.Encrypts the file
Correct Answer: Opens file in binary mode preventing character translation
Explanation:ios::binary opens the file in binary mode, preventing the automatic translation of newline characters (e.g.,
to
).
Incorrect! Try again.
14Which function returns the current position of the output pointer?
A.getpos()
B.tellg()
C.tellp()
D.seekp()
Correct Answer: tellp()
Explanation:tellp() returns the current position of the put (output) pointer.
Incorrect! Try again.
15When is a destructor called?
A.When the program starts
B.When an object is created
C.When an object goes out of scope
D.Manually by the programmer only
Correct Answer: When an object goes out of scope
Explanation:Destructors are automatically invoked when an object goes out of scope or is explicitly deleted.
Incorrect! Try again.
16Can a constructor be overloaded?
A.Yes
B.No
C.Only in derived classes
D.Only if virtual
Correct Answer: Yes
Explanation:Constructors can be overloaded like any other function, allowing objects to be initialized in different ways.
Incorrect! Try again.
17What is an Initializer List used for?
A.Listing all classes
B.Initializing file modes
C.Initializing data members before the constructor body executes
D.Listing friend functions
Correct Answer: Initializing data members before the constructor body executes
Explanation:An initializer list is used to initialize data members directly, which is more efficient and sometimes necessary (e.g., for const or reference members).
Incorrect! Try again.
18Which member must be initialized using an initializer list?
A.Static members
B.Constant members
C.Public members
D.Pointer members
Correct Answer: Constant members
Explanation: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.
Incorrect! Try again.
19What is the correct syntax to read an object 'obj' of class 'Student' from a binary file using ifstream object 'fin'?
Explanation:The read function expects a char pointer and the size of the data. The object address must be cast to (char*).
Incorrect! Try again.
20What is the default access mode for an 'ofstream' object?
A.ios::in
B.ios::out
C.ios::app
D.ios::ate
Correct Answer: ios::out
Explanation:The default mode for ofstream is ios::out (write only).
Incorrect! Try again.
21Which function checks if the End-Of-File has been reached?
A.finish()
B.end()
C.eof()
D.is_open()
Correct Answer: eof()
Explanation:The eof() function returns true if the end of the file has been reached during a read operation.
Incorrect! Try again.
22How many destructors can a class have?
A.Zero
B.Only one
C.Two
D.Unlimited
Correct Answer: Only one
Explanation:A class can have only one destructor. It takes no arguments and cannot be overloaded.
Incorrect! Try again.
23If a programmer does not define any constructor, what does the compiler do?
A.Throws an error
B.Supplies a default constructor
C.Supplies a parameterized constructor
D.Does nothing
Correct Answer: Supplies a default constructor
Explanation:If no constructor is explicitly defined, the compiler generates a default constructor that takes no parameters.
Incorrect! Try again.
24What is the output of sizeof(fstream) roughly dependent on?
A.The file size
B.The buffer size and internal state variables
C.Always 0
D.The number of files open
Correct Answer: The buffer size and internal state variables
Explanation: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.
Incorrect! Try again.
25Which combination of modes allows reading and writing to a file while preserving original content?
A.ios::out
B.ios::in | ios::out
C.ios::out | ios::trunc
D.ios::in | ios::trunc
Correct Answer: ios::in | ios::out
Explanation:Opening with both ios::in and ios::out allows update operations without implicitly truncating the file.
Incorrect! Try again.
26Which stream function reads a line of text, including spaces?
A.cin >>
B.get()
C.getline()
D.read()
Correct Answer: getline()
Explanation:getline() reads a stream of characters into a string or buffer until a delimiter (usually newline) is found, preserving spaces.
Incorrect! Try again.
27Which flag sets the file pointer to the end of the file immediately after opening?
A.ios::end
B.ios::ate
C.ios::term
D.ios::tail
Correct Answer: ios::ate
Explanation:ios::ate (at end) seeks to the end of the stream immediately upon opening.
Incorrect! Try again.
28What implies 'Random Access' in file handling?
A.Reading files randomly from disk
B.Accessing data at any position directly without reading sequentially
C.Opening files with random names
D.Creating random data
Correct Answer: Accessing data at any position directly without reading sequentially
Explanation:Random access allows moving the file pointer to any specific location using seekg/seekp to read/write directly.
Incorrect! Try again.
29Can a destructor accept arguments?
A.Yes, always
B.No, never
C.Yes, if they are default arguments
D.Only integers
Correct Answer: No, never
Explanation:Destructors take no arguments and have no return type.
Incorrect! Try again.
30What is a parameterized constructor?
A.A constructor that takes arguments
B.A constructor with no code
C.A constructor that is private
D.A constructor that returns a pointer
Correct Answer: A constructor that takes arguments
Explanation:A parameterized constructor accepts arguments to initialize an object with specific values.
Incorrect! Try again.
31Which seek direction moves the pointer relative to the beginning of the file?
A.ios::cur
B.ios::beg
C.ios::end
D.ios::start
Correct Answer: ios::beg
Explanation:ios::beg is the offset reference for the beginning of the stream.
Incorrect! Try again.
32When working with Structures and Files, why is reinterpret_cast or C-style cast used in write?
A.To encrypt data
B.To convert the structure pointer to a char pointer for byte-wise writing
C.To increase writing speed
D.To format the text
Correct Answer: To convert the structure pointer to a char pointer for byte-wise writing
Explanation:The write function expects a const char*. Pointers to structures must be cast to treat the memory as a sequence of bytes.
Incorrect! Try again.
33Which of the following constitutes a default constructor?
A.A(int x)
B.A(int x = 0)
C.A()
D.Both A(int x = 0) and A()
Correct Answer: Both A(int x = 0) and A()
Explanation: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).
Incorrect! Try again.
34If a class has a pointer member, what is the risk of using the default copy constructor?
A.Compiler error
B.Deep copy is performed
C.Shallow copy leading to dangling pointers
D.Memory leak immediately
Correct Answer: Shallow copy leading to dangling pointers
Explanation:The default copy constructor performs a shallow copy (copying address). If one object deletes the memory, the other holds a dangling pointer.
Incorrect! Try again.
35Which function puts a single character into the file stream?
A.get()
B.peek()
C.put()
D.write()
Correct Answer: put()
Explanation:The put() function writes a single character to the output stream.
Incorrect! Try again.
36In file.seekg(n, ios::cur);, what does n represent?
A.The absolute position
B.The number of bytes to move offset
C.The line number
D.The file mode
Correct Answer: The number of bytes to move offset
Explanation:The first argument in seekg is the offset (number of bytes) to move relative to the position defined by the second argument.
Incorrect! Try again.
37Which mode is required to perform input and output operations on the same file using fstream?
A.ios::in
B.ios::out
C.ios::in | ios::out
D.ios::app
Correct Answer: ios::in | ios::out
Explanation:fstream requires both ios::in and ios::out flags to allow both reading and writing.
Incorrect! Try again.
38The order of destruction of objects is:
A.Same as construction
B.Reverse of construction
C.Random
D.Dependent on size
Correct Answer: Reverse of construction
Explanation:Objects are destroyed in the reverse order of their creation (LIFO - Last In, First Out for stack objects).
Incorrect! Try again.
39Which error handling function returns true if a non-fatal error (like format mismatch) occurs?
A.good()
B.fail()
C.bad()
D.eof()
Correct Answer: fail()
Explanation:fail() returns true if a logical error on I/O operation occurs (e.g., reading a letter when an int is expected).
Incorrect! Try again.
40Can a constructor accept a reference to its own class type as a value parameter (e.g. A(A a))?
A.Yes
B.No
C.Only for temporary objects
D.Only in main
Correct Answer: No
Explanation: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.
Incorrect! Try again.
41What is the main advantage of using binary files over text files for storing objects?
A.Human readability
B.Portability across different OS
C.Efficiency and direct memory mapping
D.Easier debugging
Correct Answer: Efficiency and direct memory mapping
Explanation:Binary files store data exactly as it appears in memory, making read/write operations faster (no conversion) and more compact.
Incorrect! Try again.
42The open() function takes how many arguments generally?
A.1 (Filename only)
B.2 (Filename and Mode)
C.3 (Filename, Mode, Buffer)
D.
Correct Answer: 2 (Filename and Mode)
Explanation:Standard open() takes the filename and the open mode (though the mode often has a default).
Incorrect! Try again.
43What is a deep copy?
A.Copying the address of pointers only
B.Allocating new memory and copying the actual data
C.Copying the object to a file
D.Copying private members only
Correct Answer: Allocating new memory and copying the actual data
Explanation:Deep copy ensures that if an object has pointers, the data pointed to is also duplicated, ensuring independence between objects.
Incorrect! Try again.
44Which symbol is used for the Initializer List syntax?
A.:
B.::
C.->
D..
Correct Answer: :
Explanation:A colon (:) is placed after the constructor's parameter list to start the initializer list.
Incorrect! Try again.
45If fstream file; is declared, which function opens the file?
A.file.create()
B.file.start()
C.file.open()
D.file.init()
Correct Answer: file.open()
Explanation:The open() member function is used to associate the file stream object with a physical file.
Incorrect! Try again.
46When using ios::trunc, what happens to the existing file content?
A.It is saved
B.It is completely removed
C.It is moved to end
D.It is encrypted
Correct Answer: It is completely removed
Explanation:ios::trunc discards the current contents of the file, reducing its size to zero.
Incorrect! Try again.
47Can constructors be declared as private?
A.No, never
B.Yes, to prevent object creation from outside the class
C.Yes, but it causes a syntax error
D.Only in structs
Correct Answer: Yes, to prevent object creation from outside the class
Explanation:Private constructors are valid and are often used in the Singleton design pattern to control object instantiation.
Incorrect! Try again.
48What happens if a destructor is not virtual in a base class when deleting a derived class object via a base pointer?
A.Everything works fine
B.Derived class destructor is not called (memory leak)
C.Compilation error
D.Runtime crash immediately
Correct Answer: Derived class destructor is not called (memory leak)
Explanation: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.
Incorrect! Try again.
49Which function returns the current get position in an input stream?
A.tellp()
B.seekp()
C.seekg()
D.tellg()
Correct Answer: tellg()
Explanation:tellg() returns the position of the current character in the input stream.
Incorrect! Try again.
50In the context of file modes, what does ios::ate stand for?
A.At The End
B.Append To End
C.After The EOF
D.Allocate To Entry
Correct Answer: At The End
Explanation: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).
Incorrect! Try again.
Give Feedback
Help us improve by sharing your thoughts or reporting issues.