FSc · Computer Science (Grade 12)

Unit 9 MCQs

50 solved multiple choice questions with answers.

Q1. File handling allows a program to

  1. AOnly print on screen
  2. BOnly perform calculations
  3. CStore and retrieve data permanently
  4. DOnly use RAM
Show answer
Answer: C. Store and retrieve data permanently
Report a mistake

Q2. The header file required for file handling in C++ is

  1. A<fstream>
  2. B<iostream>
  3. C<stdio.h> only
  4. D<file.h>
Show answer
Answer: A. <fstream>
Report a mistake

Q3. ifstream is used for

  1. ABoth reading and writing
  2. BWriting to a file
  3. CReading from a file
  4. DCreating directories
Show answer
Answer: C. Reading from a file
Report a mistake

Q4. ofstream is used for

  1. AWriting to a file
  2. BBoth
  3. CReading from a file
  4. DDeleting files
Show answer
Answer: A. Writing to a file
Report a mistake

Q5. fstream can be used for

  1. AOnly reading
  2. BBoth reading and writing
  3. COnly appending
  4. DOnly writing
Show answer
Answer: B. Both reading and writing
Report a mistake

Q6. To open a file we use

  1. AOnly fopen in C style
  2. BOnly new
  3. COnly create()
  4. Dopen() member function or constructor
Show answer
Answer: D. open() member function or constructor
Report a mistake

Q7. The mode ios::in is used for

  1. AOutput (writing)
  2. BAppend
  3. CInput (reading)
  4. DBinary
Show answer
Answer: C. Input (reading)
Report a mistake

Q8. The mode ios::out is used for

  1. AAppend only
  2. BInput
  3. COutput (writing)
  4. DBinary only
Show answer
Answer: C. Output (writing)
Report a mistake

Q9. The mode ios::app is used for

  1. AOverwrite the file
  2. BRead only
  3. CCreate new file only
  4. DAppend data at the end of file
Show answer
Answer: D. Append data at the end of file
Report a mistake

Q10. The mode ios::binary is used for

  1. AOnly reading text
  2. BText file only
  3. CBinary file operations
  4. DOnly writing text
Show answer
Answer: C. Binary file operations
Report a mistake

Q11. The mode ios::trunc

  1. AAppends data
  2. BCreates a backup
  3. CTruncates (empties) the file if it already exists
  4. DOpens for reading only
Show answer
Answer: C. Truncates (empties) the file if it already exists
Report a mistake

Q12. To check whether a file is opened successfully we use

  1. AOnly bad()
  2. Bis_open() or fail()
  3. COnly good()
  4. DOnly eof()
Show answer
Answer: B. is_open() or fail()
Report a mistake

Q13. eof() returns true when

  1. AFile is closed
  2. BFile is opened
  3. CError occurs
  4. DEnd of file is reached
Show answer
Answer: D. End of file is reached
Report a mistake

Q14. The statement file.close(); is used to

  1. AClose the file
  2. BDelete the file
  3. CClear the file
  4. DOpen the file
Show answer
Answer: A. Close the file
Report a mistake

Q15. Text files store data in

  1. AHuman-readable form (ASCII/Unicode)
  2. BEncrypted form only
  3. CBinary form only
  4. DCompressed form only
Show answer
Answer: A. Human-readable form (ASCII/Unicode)
Report a mistake

Q16. Binary files store data in

  1. ASame format as it is stored in memory
  2. BOnly numbers
  3. CAlways text format
  4. DAlways encrypted
Show answer
Answer: A. Same format as it is stored in memory
Report a mistake

Q17. put() function is used to

  1. ARead a line
  2. BRead a single character
  3. CWrite a single character to a file
  4. DWrite a string
Show answer
Answer: C. Write a single character to a file
Report a mistake

Q18. get() function is used to

  1. AWrite a string
  2. BRead a single character from a file
  3. CRead an integer
  4. DWrite a single character
Show answer
Answer: B. Read a single character from a file
Report a mistake

Q19. getline() is used to

  1. AWrite a line
  2. BWrite a character
  3. CRead a single character
  4. DRead a complete line from a file
Show answer
Answer: D. Read a complete line from a file
Report a mistake

Q20. write() function is used for

  1. AReading text only
  2. BReading data in binary form
  3. CWriting text only
  4. DWriting data in binary form
Show answer
Answer: D. Writing data in binary form
Report a mistake

Q21. read() function is used for

  1. AReading text only
  2. BWriting data in binary form
  3. CWriting text only
  4. DReading data in binary form
Show answer
Answer: D. Reading data in binary form
Report a mistake

Q22. seekg() is used to

  1. AMove the put pointer
  2. BMove the get pointer (for reading)
  3. CClose the file
  4. DOpen the file
Show answer
Answer: B. Move the get pointer (for reading)
Report a mistake

Q23. seekp() is used to

  1. AMove the get pointer
  2. BMove the put pointer (for writing)
  3. CRead data
  4. DWrite data
Show answer
Answer: B. Move the put pointer (for writing)
Report a mistake

Q24. tellg() returns

  1. ASize of file only
  2. BCurrent position of get pointer
  3. CNumber of records
  4. DCurrent position of put pointer
Show answer
Answer: B. Current position of get pointer
Report a mistake

Q25. tellp() returns

  1. ACurrent position of put pointer
  2. BBeginning of file
  3. CCurrent position of get pointer
  4. DEnd of file
Show answer
Answer: A. Current position of put pointer
Report a mistake

Q26. ios::beg means

  1. AEnd of the file
  2. BCurrent position
  3. CBeginning of the file
  4. DMiddle of file
Show answer
Answer: C. Beginning of the file
Report a mistake

Q27. ios::end means

  1. ARandom position
  2. BBeginning of the file
  3. CCurrent position
  4. DEnd of the file
Show answer
Answer: D. End of the file
Report a mistake

Q28. ios::cur means

  1. AEnd
  2. BBeginning
  3. CCurrent position of the pointer
  4. DFixed position
Show answer
Answer: C. Current position of the pointer
Report a mistake

Q29. Sequential access means

  1. AReading/writing data in order from start to end
  2. BJumping to any position directly
  3. COnly writing
  4. DOnly reading
Show answer
Answer: A. Reading/writing data in order from start to end
Report a mistake

Q30. Random access means

  1. AOnly sequential reading
  2. BOnly appending
  3. COnly creating new file
  4. DAbility to move to any position in the file
Show answer
Answer: D. Ability to move to any position in the file
Report a mistake

Q31. File pointer is

  1. AAn indicator of current position in the file
  2. BA function
  3. CA normal C++ pointer
  4. DA class
Show answer
Answer: A. An indicator of current position in the file
Report a mistake

Q32. When a file is opened in ios::out mode and file already exists

  1. AIts contents are discarded (truncated)
  2. BData is appended
  3. CError occurs always
  4. DFile is deleted permanently
Show answer
Answer: A. Its contents are discarded (truncated)
Report a mistake

Q33. To open a file for both reading and writing we use

  1. AOnly ios::app
  2. Bfstream with ios::in | ios::out
  3. COnly ofstream
  4. DOnly ifstream
Show answer
Answer: B. fstream with ios::in | ios::out
Report a mistake

Q34. The default open mode for ofstream is

  1. Aios::out
  2. Bios::in
  3. Cios::app
  4. Dios::binary
Show answer
Answer: A. ios::out
Report a mistake

Q35. The default open mode for ifstream is

  1. Aios::in
  2. Bios::trunc
  3. Cios::app
  4. Dios::out
Show answer
Answer: A. ios::in
Report a mistake

Q36. fail() returns true when

  1. AEnd of file is reached
  2. BData is written
  3. CAn operation fails (e.g., file not found)
  4. DFile is successfully opened
Show answer
Answer: C. An operation fails (e.g., file not found)
Report a mistake

Q37. bad() returns true when

  1. ASuccessful operation
  2. BA serious error occurs (e.g., hardware failure)
  3. CEnd of file
  4. DFile is closed
Show answer
Answer: B. A serious error occurs (e.g., hardware failure)
Report a mistake

Q38. good() returns true when

  1. ANo error has occurred
  2. BFile cannot be opened
  3. CEnd of file is reached
  4. DSerious error
Show answer
Answer: A. No error has occurred
Report a mistake

Q39. clear() function is used to

  1. AClose the file
  2. BDelete the file
  3. CClear error flags of the stream
  4. DClear the contents of file
Show answer
Answer: C. Clear error flags of the stream
Report a mistake

Q40. File handling is necessary because

  1. AFiles are slower always
  2. BRAM is permanent
  3. CData in RAM is temporary (lost when program ends)
  4. DNo need for permanent storage
Show answer
Answer: C. Data in RAM is temporary (lost when program ends)
Report a mistake

Q41. A record in a file usually consists of

  1. ARandom data
  2. BOnly one character
  3. CRelated fields of data
  4. DOnly one integer
Show answer
Answer: C. Related fields of data
Report a mistake

Q42. To write an object to a binary file we use

  1. AOnly getline()
  2. BOnly put()
  3. Cwrite((char*)&object, sizeof(object))
  4. DOnly << operator
Show answer
Answer: C. write((char*)&object, sizeof(object))
Report a mistake

Q43. To read an object from a binary file we use

  1. Aread((char*)&object, sizeof(object))
  2. BOnly get()
  3. COnly getline()
  4. DOnly >> operator
Show answer
Answer: A. read((char*)&object, sizeof(object))
Report a mistake

Q44. Opening a non-existent file in ios::in mode

  1. AAppends data
  2. BCreates a new empty file
  3. CDeletes other files
  4. DCauses open to fail
Show answer
Answer: D. Causes open to fail
Report a mistake

Q45. Opening a non-existent file in ios::out mode

  1. AOpens existing file only
  2. BCreates a new empty file
  3. CAppends to another file
  4. DCauses error always
Show answer
Answer: B. Creates a new empty file
Report a mistake

Q46. The stream hierarchy in C++ includes

  1. AOnly fstream
  2. BOnly iostream
  3. COnly FILE*
  4. Dios → istream/ostream → ifstream/ofstream/fstream
Show answer
Answer: D. ios → istream/ostream → ifstream/ofstream/fstream
Report a mistake

Q47. Closing a file is important because

  1. AIt deletes the file
  2. BIt flushes buffers and releases resources
  3. CIt only works for text files
  4. DIt is optional and never needed
Show answer
Answer: B. It flushes buffers and releases resources
Report a mistake

Q48. Multiple files can be opened

  1. AOnly in main()
  2. BNever
  3. CSimultaneously using different stream objects
  4. DOnly one at a time in whole program
Show answer
Answer: C. Simultaneously using different stream objects
Report a mistake

Q49. File handling errors should be

  1. AOnly checked at the end
  2. BIgnored
  3. CAlways checked (is_open, fail, etc.)
  4. DOnly for binary files
Show answer
Answer: C. Always checked (is_open, fail, etc.)
Report a mistake

Q50. The most common use of file handling is

  1. APermanent storage and retrieval of data
  2. BOnly screen output
  3. COnly temporary calculations
  4. DOnly memory allocation
Show answer
Answer: A. Permanent storage and retrieval of data
Report a mistake