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

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

A. ifstream
B. ofstream
C. filebuf
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. No return type
B. int
C. void
D. class type

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

A. ios::ate
B. ios::app
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. end()
B. stop()
C. close()
D. finish()

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. An error occurs
D. The file contents are truncated (deleted)

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

A. Default Constructor
B. Dynamic Constructor
C. Parameterized Constructor
D. Copy 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 a string
B. To write formatted text
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. seekg()
B. seekp()
C. tellp()
D. tellg()

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

A. Encrypts the file
B. Compresses the file
C. Opens file for text operations
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. When an object goes out of scope
B. When an object is created
C. Manually by the programmer only
D. When the program starts

16 Can a constructor be overloaded?

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

17 What is an Initializer List used for?

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

18 Which member must be initialized using an initializer list?

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

22 How many destructors can a class have?

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

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

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

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

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

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

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

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

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

28 What implies 'Random Access' in file handling?

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

29 Can a destructor accept arguments?

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

30 What is a parameterized constructor?

A. A constructor that returns a pointer
B. A constructor with no code
C. A constructor that takes arguments
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 format the text
B. To increase writing speed
C. To encrypt data
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()
B. A(int x)
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. 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. put()
B. get()
C. write()
D. peek()

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::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. Random
D. Same as construction

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

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

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 in main
C. Only for temporary objects
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. Efficiency and direct memory mapping
D. Easier debugging

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

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

43 What is a deep copy?

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

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

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

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

47 Can constructors be declared as private?

A. No, never
B. Only in structs
C. Yes, but it causes a syntax error
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. Derived class destructor is not called (memory leak)
B. Runtime crash immediately
C. Compilation error
D. Everything works fine

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

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

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

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