Unit 3: Data File operations, Constructors, Destructors and File Handling - Practice Quiz

CSE202 — Object Oriented Programming 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which header file is required to perform file input/output operations in C++?

A. <stdio.h>
B. <conio.h>
C. <iostream>
D. <fstream>

2 Which class is used specifically for writing data to a file?

A. filebuf
B. ofstream
C. ifstream
D. ostream

3 Which class is used specifically for reading data from a file?

A. ifstream
B. fstream
C. ofstream
D. iostream

4 What is the return type of a constructor?

A. int
B. void
C. No return type
D. class type

5 Which file mode is used to append content to the end of an existing file?

A. ios::out
B. ios::app
C. ios::trunc
D. ios::ate

6 Which symbol is used to prefix the destructor name?

A. @
B. ~
C. #
D. !

7 Which function is used to close a file explicitly in C++?

A. close()
B. stop()
C. end()
D. finish()

8 What happens if a file is opened in 'ios::out' mode and the file already exists?

A. An error occurs
B. The file is opened in read-only mode
C. The file contents are truncated (deleted)
D. Data is appended

9 Which constructor is called when an object is initialized with another object of the same class?

A. Dynamic Constructor
B. Default Constructor
C. Parameterized Constructor
D. Copy Constructor

10 Which syntax correctly defines a copy constructor for class 'A'?

A. A(A *obj)
B. void A(A obj)
C. A(const A &obj)
D. A(A obj)

11 What is the purpose of the 'write()' function in file handling?

A. To write a single character
B. To write binary data blocks
C. To write formatted text
D. To write a string

12 Which function is used to move the get pointer (input pointer) in a file?

A. seekg()
B. tellg()
C. tellp()
D. seekp()

13 What does 'ios::binary' mode do?

A. Encrypts the file
B. Opens file for text operations
C. Compresses the file
D. Opens file in binary mode preventing character translation

14 Which function returns the current position of the output pointer?

A. getpos()
B. seekp()
C. tellg()
D. tellp()

15 When is a destructor called?

A. When the program starts
B. Manually by the programmer only
C. When an object is created
D. When an object goes out of scope

16 Can a constructor be overloaded?

A. Only in derived classes
B. No
C. Only if virtual
D. Yes

17 What is an Initializer List used for?

A. Listing all classes
B. Initializing file modes
C. Listing friend functions
D. Initializing data members before the constructor body executes

18 Which member must be initialized using an initializer list?

A. Pointer members
B. Constant members
C. Static members
D. Public members

19 What is the correct syntax to read an object 'obj' of class 'Student' from a binary file using ifstream object 'fin'?

A. fin >> obj;
B. fin.read((char*)&obj, sizeof(obj));
C. fin.get(obj);
D. fin.read(obj, sizeof(obj));

20 What is the default access mode for an 'ofstream' object?

A. ios::ate
B. ios::in
C. ios::out
D. ios::app

21 Which function checks if the End-Of-File has been reached?

A. eof()
B. finish()
C. end()
D. is_open()

22 How many destructors can a class have?

A. Two
B. Unlimited
C. Zero
D. Only one

23 If a programmer does not define any constructor, what does the compiler do?

A. Throws an error
B. Does nothing
C. Supplies a default constructor
D. Supplies a parameterized constructor

24 What is the output of sizeof(fstream) roughly dependent on?

A. The file size
B. Always 0
C. The buffer size and internal state variables
D. The number of files open

25 Which combination of modes allows reading and writing to a file while preserving original content?

A. ios::in | ios::out
B. ios::out | ios::trunc
C. ios::in | ios::trunc
D. ios::out

26 Which stream function reads a line of text, including spaces?

A. getline()
B. read()
C. cin >>
D. get()

27 Which flag sets the file pointer to the end of the file immediately after opening?

A. ios::ate
B. ios::end
C. ios::tail
D. ios::term

28 What implies 'Random Access' in file handling?

A. Creating random data
B. Opening files with random names
C. Accessing data at any position directly without reading sequentially
D. Reading files randomly from disk

29 Can a destructor accept arguments?

A. Only integers
B. Yes, if they are default arguments
C. Yes, always
D. No, never

30 What is a parameterized constructor?

A. A constructor that returns a pointer
B. A constructor that takes arguments
C. A constructor with no code
D. A constructor that is private

31 Which seek direction moves the pointer relative to the beginning of the file?

A. ios::end
B. ios::beg
C. ios::cur
D. ios::start

32 When working with Structures and Files, why is reinterpret_cast or C-style cast used in write?

A. To convert the structure pointer to a char pointer for byte-wise writing
B. To format the text
C. To increase writing speed
D. To encrypt data

33 Which of the following constitutes a default constructor?

A. A()
B. A(int x)
C. A(int x = 0)
D. Both A(int x = 0) and A()

34 If a class has a pointer member, what is the risk of using the default copy constructor?

A. Shallow copy leading to dangling pointers
B. Deep copy is performed
C. Memory leak immediately
D. Compiler error

35 Which function puts a single character into the file stream?

A. peek()
B. write()
C. get()
D. put()

36 In file.seekg(n, ios::cur);, what does n represent?

A. The line number
B. The number of bytes to move offset
C. The file mode
D. The absolute position

37 Which mode is required to perform input and output operations on the same file using fstream?

A. ios::in
B. ios::in | ios::out
C. ios::out
D. ios::app

38 The order of destruction of objects is:

A. Same as construction
B. Dependent on size
C. Random
D. Reverse of construction

39 Which error handling function returns true if a non-fatal error (like format mismatch) occurs?

A. good()
B. bad()
C. fail()
D. eof()

40 Can a constructor accept a reference to its own class type as a value parameter (e.g. A(A a))?

A. Only in main
B. No
C. Yes
D. Only for temporary objects

41 What is the main advantage of using binary files over text files for storing objects?

A. Portability across different OS
B. Efficiency and direct memory mapping
C. Easier debugging
D. Human readability

42 The open() function takes how many arguments generally?

A. 1 (Filename only)
B. 3 (Filename, Mode, Buffer)
C. 2 (Filename and Mode)
D. 0

43 What is a deep copy?

A. Copying the object to a file
B. Allocating new memory and copying the actual data
C. Copying private members only
D. Copying the address of pointers only

44 Which symbol is used for the Initializer List syntax?

A. ->
B. .
C. :
D. ::

45 If fstream file; is declared, which function opens the file?

A. file.open()
B. file.create()
C. file.init()
D. file.start()

46 When using ios::trunc, what happens to the existing file content?

A. It is encrypted
B. It is saved
C. It is moved to end
D. It is completely removed

47 Can constructors be declared as private?

A. Only in structs
B. Yes, to prevent object creation from outside the class
C. No, never
D. Yes, but it causes a syntax error

48 What happens if a destructor is not virtual in a base class when deleting a derived class object via a base pointer?

A. Runtime crash immediately
B. Derived class destructor is not called (memory leak)
C. Compilation error
D. Everything works fine

49 Which function returns the current get position in an input stream?

A. tellg()
B. tellp()
C. seekg()
D. seekp()

50 In the context of file modes, what does ios::ate stand for?

A. After The EOF
B. Append To End
C. Allocate To Entry
D. At The End