Unit 3 - Practice Quiz

CSE202 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. <fstream>
B. <iostream>
C. <stdio.h>
D. <conio.h>

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

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

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

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

4 What is the return type of a constructor?

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

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

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

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

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

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

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

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

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

A. A(const A &obj)
B. A(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 binary data blocks
C. To write a string
D. To write a single character

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

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

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. tellp()
B. tellg()
C. seekp()
D. getpos()

15 When is a destructor called?

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

16 Can a constructor be overloaded?

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

17 What is an Initializer List used for?

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

18 Which member must be initialized using an initializer list?

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

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

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

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

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

22 How many destructors can a class have?

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

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

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

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

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

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

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

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

A. getline()
B. get()
C. cin >>
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::tail
D. ios::term

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. Creating random data
D. Opening files with random names

29 Can a destructor accept arguments?

A. Yes, if they are default arguments
B. Only integers
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::beg
B. ios::start
C. ios::end
D. ios::cur

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

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

33 Which of the following constitutes a default constructor?

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

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

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

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

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

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

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

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

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

38 The order of destruction of objects is:

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

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 in main
D. Only for temporary objects

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

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

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

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

43 What is a deep copy?

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

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

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

A. It is completely removed
B. It is saved
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. Only in structs
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. Compilation error
C. Everything works fine
D. Derived class destructor is not called (memory leak)

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

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

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

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