Unit 3 - Practice Quiz

CSE202

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

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

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

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

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

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

4 What is the return type of a constructor?

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

5 Which 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

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. finish()
B. end()
C. stop()
D. close()

8 What 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

9 Which 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

10 Which 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)

11 What 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

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

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

13 What 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

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

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

15 When 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

16 Can a constructor be overloaded?

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

17 What 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

18 Which member must be initialized using an initializer list?

A. Static members
B. Constant members
C. Public members
D. Pointer 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.read((char*)&obj, sizeof(obj));
B. fin.read(obj, sizeof(obj));
C. fin.get(obj);
D. fin >> obj;

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

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

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

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

22 How many destructors can a class have?

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

23 If 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

24 What 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

25 Which 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

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

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

27 Which 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

28 What 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

29 Can a destructor accept arguments?

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

30 What 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

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

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

32 When 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

33 Which 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()

34 If 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

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

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

36 In 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

37 Which 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

38 The order of destruction of objects is:

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

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

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

40 Can 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

41 What 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

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

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

43 What 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

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.create()
B. file.start()
C. file.open()
D. file.init()

46 When 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

47 Can 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

48 What 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

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

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

50 In 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