Cs 201 Lec 19
Cs 201 Lec 19
A) To close a file
B) To open a file for reading or writing
C) To read a file
D) To write data to a file
A) Opens a file
B) Closes a file
C) Reads a file
D) Writes data to a file
3. Which of the following is the correct way to open a file for reading in
C++?
A) ofstream file("filename");
B) ifstream file("filename");
C) fstream file("filename");
D) open("filename", ios::in);
4. Which mode will not destroy the contents of a file when opening it for
writing?
A) ios::trunc
B) ios::in
C) ios::out
D) ios::app
Answer: D) ios::app
6. What will happen if a file is opened with ios::in mode and the file does
not exist?
7. What is the correct syntax for writing an integer, float, and character to a
file using ofstream?
A) out << 100 << " " << 123.12 << 'a';
B) out << 100 << 123.12 << 'a';
C) out.write(100, 123.12, 'a');
D) out.put(100, 123.12, 'a');
Answer: A) out << 100 << " " << 123.12 << 'a';
A) ios::in
B) ios::out
C) ios::app
D) ios::trunc
Answer: B) ios::out
A) get()
B) getline()
C) put()
D) read()
Answer: A) get()
10. Which operator is used for extracting data from an input stream in C++?
A) <<
B) >>
C) =
D) <<=
Answer: B) >>
11. What function is used to copy the contents of one file to another?
A) copyFile()
B) write()
C) get()
D) put()
Answer: D) put()
13. Which of the following is NOT a valid file mode when opening a file?
A) ios::in
B) ios::out
C) ios::skipws
D) ios::temp
Answer: D) ios::temp
Answer: C) tellg()
18. Which of the following is a correct way to append data to a file in C++?
A) Moves the file pointer 10 bytes backward from the current position
B) Moves the file pointer 10 bytes forward from the current position
C) Moves the file pointer to the beginning of the file
D) Moves the file pointer to the end of the file
Answer: A) Moves the file pointer 10 bytes backward from the current position
20. What happens when you use seekg() with a negative value and
ios::end?
21. How can you get the file size using seekg() and tellg()?
A) Open the file for reading, use seekg(0, ios::end) and call tellg()
B) Open the file for writing, use seekp(0, ios::end) and call tellp()
C) Open the file for appending and call seekg()
D) Use seekg() and seekp() together
Answer: A) Open the file for reading, use seekg(0, ios::end) and call tellg()
Answer: B) Appends data to the end of the file without deleting its contents
24. Which of the following functions does not require a parameter?
A) open()
B) close()
C) tellg()
D) seekg()
Answer: B) close()
Answer: B) Moves the file pointer 10 bytes forward from the beginning of the file
27. What happens if you move the file pointer beyond the end of a file using
seekg()?
A) It causes an error
B) It moves to the beginning of the file
C) It moves the file pointer to the end of the file
D) It does nothing
29. Which file mode can be used to append data without truncating the file?
A) ios::trunc
B) ios::app
C) ios::in
D) ios::out
Answer: B) ios::app
30. What does the tellg() function return when used with an empty file?
A) 0
B) -1
C) Undefined behavior
D) Null
Answer: A) 0
31. Which mode will destroy the contents of a file when opening it?
A) ios::out
B) ios::in
C) ios::app
D) ios::trunc
Answer: A) ios::out
1. Which function is used to position the file pointer for reading from a specific
location in a file?
○ A) seekp()
○ B) seekg()
○ C) tellp()
○ D) tellg()
○ Answer: B) seekg()
2. What does the seekg() function do in C++ file handling?
○ A) Positions the pointer to write data
○ B) Positions the pointer to read data
○ C) Moves the pointer to the beginning of the file
○ D) Moves the pointer to the end of the file
○ Answer: B) Positions the pointer to read data
3. What is the use of the tellg() function?
○ A) To get the position of the write pointer
○ B) To get the position of the read pointer
○ C) To set the write pointer at a specific location
○ D) To set the read pointer at a specific location
○ Answer: B) To get the position of the read pointer
4. Which of the following file positions can be used in seekg() and seekp()
functions?
○ A) ios::start
○ B) ios::end
○ C) ios::begin
○ D) ios::cur
○ Answer: B) ios::end, D) ios::cur
5. How can you move the file pointer 10 bytes from the beginning of a file using
seekg()?
○ A) seekg(10, ios::cur)
○ B) seekg(10, ios::end)
○ C) seekg(10, ios::beg)
○ D) seekg(-10, ios::end)
○ Answer: C) seekg(10, ios::beg)
6. Which function is used to get the current position of the file pointer for writing?
○ A) seekg()
○ B) tellg()
○ C) tellp()
○ D) seekp()
○ Answer: C) tellp()
7. What does the seekp() function do in file handling?
○ A) It moves the pointer for reading data
○ B) It moves the pointer for writing data
○ C) It returns the position of the read pointer
○ D) It returns the position of the write pointer
○ Answer: B) It moves the pointer for writing data
8. Which of the following is NOT a valid argument for seekg() and seekp()?
○ A) ios::beg
○ B) ios::start
○ C) ios::cur
○ D) ios::end
○ Answer: B) ios::start
9. What happens if you use seekg() with a negative value for ios::cur?
○ A) It moves the file pointer forward
○ B) It moves the file pointer backward
○ C) It causes an error
○ D) It moves the pointer to the beginning
○ Answer: B) It moves the file pointer backward
10. How would you move the file pointer to 100 bytes before the end of the file using
seekg()?
○ A) seekg(100, ios::beg)
○ B) seekg(100, ios::cur)
○ C) seekg(-100, ios::end)
○ D) seekg(-100, ios::cur)
○ Answer: C) seekg(-100, ios::end)
11. In which situation is the seekg() and tellg() combination most useful?
○ A) To check if a file exists
○ B) To determine the size of a file
○ C) To check the end of a file
○ D) To open a file in input mode
○ Answer: B) To determine the size of a file
12. Which of the following C++ stream functions are used to check the position of the
file pointer for reading and writing?
○ A) seekg() and seekp()
○ B) tellg() and tellp()
○ C) get() and put()
○ D) open() and close()
○ Answer: B) tellg() and tellp()
13. What is the main reason why file size can be greater than the actual data length in
a file?
○ A) Because of logical blocks used by disks
○ B) Due to the structure of the file system
○ C) Because of the operating system buffer
○ D) Due to the metadata associated with files
○ Answer: A) Because of logical blocks used by disks
14. How can you determine the actual data length of a file in C++?
○ A) By using the tellp() function
○ B) By using the seekg() and tellg() functions
○ C) By using fseek() function
○ D) By checking file size in the file properties
○ Answer: B) By using the seekg() and tellg() functions
15. What happens if you try to move the file pointer beyond the end of the file using
seekg()?
○ A) The program crashes
○ B) The pointer will remain at the end of the file
○ C) It returns an error code
○ D) The pointer moves to the beginning of the file
○ Answer: B) The pointer will remain at the end of the file
16. Which C++ file mode is used to open a file for writing, but without erasing existing
content?
○ A) ios::in
○ B) ios::out
○ C) ios::app
○ D) ios::trunc
○ Answer: C) ios::app
17. Which file stream object is used for both reading and writing to a file in C++?
○ A) ifstream
○ B) ofstream
○ C) fstream
○ D) iostream
○ Answer: C) fstream
18. What would happen if you try to write data to a file opened in ios::in mode?
○ A) The data is appended to the file
○ B) The file is overwritten with the new data
○ C) A runtime error occurs
○ D) Data is written in binary format
○ Answer: C) A runtime error occurs
19. Which of the following methods would allow you to insert data into the middle of a
sequential file without disturbing the structure?
○ A) Directly modifying the file at the target position
○ B) Using the Merge method to create a new file with updated data
○ C) Using seekp() to overwrite the data
○ D) Using a temporary buffer to store the file data
○ Answer: B) Using the Merge method to create a new file with updated data
20. Which is the most important consideration when designing a file structure to allow
safe insertion and updates in the middle of the file?
○ A) Using random access files with fixed record lengths
○ B) Using dynamic data structures
○ C) Sorting the file alphabetically
○ D) Using separate files for different data types
○ Answer: A) Using random access files with fixed record lengths
21. When inserting data in the middle of a file, why must records be of a fixed size?
○ A) To allow efficient searching and modification
○ B) To maintain the file size consistency
○ C) To avoid overwriting existing data
○ D) To make the program easier to debug
○ Answer: C) To avoid overwriting existing data
22. Which of the following is a disadvantage of using the Merge method to insert data
in the middle of a file?
○ A) It allows for fast searching and updates
○ B) It consumes a lot of time and space, especially for large files
○ C) It doesn’t allow random access to the file
○ D) It is easy to implement with minimal code
○ Answer: B) It consumes a lot of time and space, especially for large files
23. What is the recommended file structure for random access files?
○ A) Fixed-length records
○ B) Variable-length records
○ C) Unsorted data with unique keys
○ D) Files with indexes for each field
○ Answer: A) Fixed-length records
24. In random access file handling, why is it important to keep a unique key for each
record?
○ A) To sort the data
○ B) To quickly locate and modify specific records
○ C) To prevent data corruption
○ D) To maintain file size consistency
○ Answer: B) To quickly locate and modify specific records
25. Which of the following methods is most efficient for finding a record in a large file
based on a unique key?
○ A) Brute-force search through all records
○ B) Indexing and binary search
○ C) Reading the entire file and modifying records as needed
○ D) Writing a custom search algorithm
○ Answer: B) Indexing and binary search
26. Which of the following file operations might result in data corruption?
○ A) Randomly overwriting data with different-sized records
○ B) Appending data to a file in ios::app mode
○ C) Using seekg() to move the pointer backward
○ D) Using seekp() to write to a file in append mode
○ Answer: A) Randomly overwriting data with different-sized records
27. What happens if a program tries to move the read pointer forward from the end of
a file?
○ A) The program will crash
○ B) The pointer will stay at the end of the file
○ C) It will trigger an error message
○ D) The program will read the entire file again
○ Answer: B) The pointer will stay at the end of the file
28. In C++, which file mode allows you to both read and write to a file at the same
time?
○ A) ios::in | ios::out
○ B) ios::app | ios::out
○ C) ios::in | ios::app
○ D) ios::read | ios::write
○ Answer: A) ios::in | ios::out
29. Which function can be used to move the write pointer backward in a file?
○ A) seekg()
○ B) seekp()
○ C) tellg()
○ D) tellp()
○ Answer: B) seekp()
30. What is a common solution to update or insert data in the middle of a large
sequential file?
○ A) Replace the existing data directly using seekg() and seekp()
○ B) Create a new file, copy data up to the point of insertion, insert new data, and
then copy the remaining data
○ C) Append the new data at the end of the file
○ D) Rebuild the entire file from scratch with the new data
○ Answer: B) Create a new file, copy data up to the point of insertion, insert new
data, and then copy the remaining data
11. How would you find the starting byte of the 23rd student’s record, if each record
takes 100 bytes?
○ A) seekg(23 * 100)
○ B) seekg(22 * 100)
○ C) seekg(100)
○ D) seekg(23)
12. Answer: B) seekg(22 * 100)
13. In a fixed-length record structure, if each record takes 100 bytes, how many bytes
are required for the first 10 student records?
○ A) 1000 bytes
○ B) 900 bytes
○ C) 100 bytes
○ D) 1100 bytes
14. Answer: A) 1000 bytes
15. Which of the following is true for a file opened with fstream in ios::in ||
ios::out mode?
○ A) The file is opened only for output.
○ B) The file is opened for both reading and writing.
○ C) The file is opened for reading only.
○ D) The file is opened for writing only.
16. Answer: B) The file is opened for both reading and writing.
17. If a file has 250 records, and each record takes 100 bytes, how would you access
the 23rd record?
○ A) seekg(23 * 100)
○ B) seekg(22 * 100)
○ C) seekg(23)
○ D) seekg(100 * 250)
18. Answer: B) seekg(22 * 100)
19. How many bytes are there in a student record if the following are the lengths: ID
(10 bytes), Name (40 bytes), City (40 bytes), Date of Birth (10 bytes)?
○ A) 100 bytes
○ B) 90 bytes
○ C) 80 bytes
○ D) 120 bytes
20. Answer: A) 100 bytes
21. In the case of random access to a file, what is the advantage of using seekg()
and seekp() over sequential reading?
○ A) They allow for more flexible data manipulation and reduce unnecessary loops.
○ B) They read data faster than sequential methods.
○ C) They are only used for writing.
○ D) They move the file pointer to the end automatically.
22. Answer: A) They allow for more flexible data manipulation and reduce
unnecessary loops.
21. Which of the following methods is the most efficient way to copy large files in
C++?
○ A) Using getc() and putc() in a loop.
○ B) Using read() and write() to handle large chunks of data.
○ C) Using fscanf() and fprintf() for each byte.
○ D) Using seekg() and seekp().
22. Answer: B) Using read() and write() to handle large chunks of data.
23. In the context of file copying, which of the following is the correct approach?
○ A) Open both files, read from the source file, and write to the destination file in
chunks.
○ B) Open the destination file, read it, and append to the source file.
○ C) Write each byte individually from the source to the destination.
○ D) Use getc() and putc() repeatedly for copying files.
24. Answer: A) Open both files, read from the source file, and write to the destination
file in chunks.
25. Which of the following functions is used to get the current position of the file
pointer?
○ A) tellg()
○ B) seekg()
○ C) tellp()
○ D) seekp()
26. Answer: A) tellg()
27. What is the advantage of copying a file in chunks instead of copying byte by byte?
○ A) It reduces the number of system calls, improving performance.
○ B) It allows for random access to data.
○ C) It is more memory-intensive.
○ D) It requires no file pointer manipulation.
28. Answer: A) It reduces the number of system calls, improving performance.
25. What happens when you use write() to store integers in a binary file?
○ A) The integers are stored in human-readable form.
○ B) The integers are stored in their internal binary format.
○ C) The file becomes unreadable.
○ D) The data is compressed automatically.
26. Answer: B) The integers are stored in their internal binary format.
27. When you use read() and write() functions with binary data, which of the
following is true?
○ A) The file pointer cannot be moved.
○ B) The file is always written as text.
○ C) Data is written or read in raw binary form.
○ D) Binary files can only store integers.
28. Answer: C) Data is written or read in raw binary form.
29. What will you see if you open a binary file containing integers in a text editor?
○ A) The integers will be displayed in human-readable format.
○ B) The integers will be represented by symbols or gibberish.
○ C) The file will display as empty.
○ D) The integers will be compressed into a single byte.
30. Answer: B) The integers will be represented by symbols or gibberish.
File Manipulation
32. In C++, what does the ios::binary flag indicate when opening a file?
○ A) The file is opened for both reading and writing.
○ B) The file is opened as a binary file (raw data).
○ C) The file is opened for text processing.
○ D) The file is opened in read-only mode.
33. Answer: B) The file is opened as a binary file (raw data).
34. What is the default mode when opening a file using ofstream in C++?
○ A) ios::in
○ B) ios::out
○ C) ios::app
○ D) ios::binary
35. Answer: B) ios::out
36. How do you ensure that a file is opened successfully in C++?
○ A) Check if ifstream or ofstream is not equal to NULL.
○ B) Use open() and check if it returns true.
○ C) Use open() and check the file pointer.
○ D) Use file.good() to verify success.
37. Answer: D) Use file.good() to verify success.
38. In which scenario would you use ios::trunc mode while opening a file?
○ A) To ensure that the file is read-only.
○ B) To append data to the file.
○ C) To truncate the file to zero length before writing.
○ D) To open a file in binary mode.
39. Answer: C) To truncate the file to zero length before writing.