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

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

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

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

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

4 What is the return type of a constructor?

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

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

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

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

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. An error occurs
C. The file is opened in read-only mode
D. Data is appended

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

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

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

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

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

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

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

A. Compresses the file
B. Opens file for text operations
C. Encrypts 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. tellg()
C. tellp()
D. seekp()

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 in derived classes
B. Yes
C. No
D. Only if virtual

17 What is an Initializer List used for?

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

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 >> obj;
B. fin.read(obj, sizeof(obj));
C. fin.read((char*)&obj, sizeof(obj));
D. fin.get(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. is_open()
C. end()
D. eof()

22 How many destructors can a class have?

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

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

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

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

A. The file size
B. The number of files open
C. Always 0
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::out | ios::trunc
B. ios::out
C. ios::in | ios::trunc
D. ios::in | ios::out

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

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

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

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

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. Yes, if they are default arguments
B. Yes, always
C. No, never
D. Only integers

30 What is a parameterized constructor?

A. A constructor that takes arguments
B. A constructor that returns a pointer
C. A constructor that is private
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::end
D. ios::cur

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 format the text
D. To increase writing speed

33 Which of the following constitutes a default constructor?

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

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. Memory leak immediately
C. Compiler error
D. Deep copy is performed

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

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

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 absolute position
D. The file mode

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

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

38 The order of destruction of objects is:

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

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

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

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

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

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. Human readability
D. Easier debugging

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

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

43 What is a deep copy?

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

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

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

47 Can constructors be declared as private?

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

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. Derived class destructor is not called (memory leak)
D. Everything works fine

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

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

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

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