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

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

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

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

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

4 What is the return type of a constructor?

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

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

A. ios::ate
B. ios::trunc
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. 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. The file contents are truncated (deleted)
B. Data is appended
C. An error occurs
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. Copy Constructor
B. Dynamic Constructor
C. Parameterized Constructor
D. Default Constructor

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

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

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

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

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

15 When is a destructor called?

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

16 Can a constructor be overloaded?

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

17 What is an Initializer List used for?

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

18 Which member must be initialized using an initializer list?

A. Pointer members
B. Public members
C. Constant members
D. Static 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.read(obj, sizeof(obj));
D. fin.get(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. end()
B. is_open()
C. eof()
D. finish()

22 How many destructors can a class have?

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

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

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

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

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

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

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

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::tail
B. ios::term
C. ios::ate
D. ios::end

28 What implies 'Random Access' in file handling?

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

29 Can a destructor accept arguments?

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

30 What is a parameterized constructor?

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

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

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

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

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

33 Which of the following constitutes a default constructor?

A. A(int x)
B. A()
C. Both A(int x = 0) and 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. Shallow copy leading to dangling pointers
B. Deep copy is performed
C. Compiler error
D. Memory leak immediately

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

A. write()
B. get()
C. put()
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::app
B. ios::in | ios::out
C. ios::out
D. ios::in

38 The order of destruction of objects is:

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

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

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

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

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

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

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

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

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

43 What is a deep copy?

A. Allocating new memory and copying the actual data
B. Copying the object to a file
C. Copying the address of pointers only
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.start()
B. file.open()
C. file.init()
D. file.create()

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

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

47 Can constructors be declared as private?

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

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

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

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

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

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

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