0% found this document useful (0 votes)
12 views19 pages

Cs 201 Lec 19

The document consists of multiple-choice questions (MCQs) focused on file handling in C++. It covers various functions, modes, and operations related to file manipulation, such as opening, closing, reading, writing, and positioning file pointers. The answers provided clarify the correct usage and behavior of these functions and modes in C++ programming.

Uploaded by

fu947896
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views19 pages

Cs 201 Lec 19

The document consists of multiple-choice questions (MCQs) focused on file handling in C++. It covers various functions, modes, and operations related to file manipulation, such as opening, closing, reading, writing, and positioning file pointers. The answers provided clarify the correct usage and behavior of these functions and modes in C++ programming.

Uploaded by

fu947896
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1. What is the purpose of the open() function in file handling?

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

Answer: B) To open a file for reading or writing

2. What does the close() function do?

A) Opens a file
B) Closes a file
C) Reads a file
D) Writes data to a file

Answer: B) Closes 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);

Answer: B) ifstream file("filename");

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

5. What does ios::trunc mode do when opening a file?

A) Appends data to the file


B) Prevents writing to the file
C) Deletes the existing contents of the file
D) Opens the file for input

Answer: C) Deletes the existing contents of the file

6. What will happen if a file is opened with ios::in mode and the file does
not exist?

A) It will create a new file


B) It will cause a compile-time error
C) It will fail to open and return an error
D) It will open a new file with default contents

Answer: C) It will fail to open and return an error

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';

8. In which mode will ofstream open a file by default?

A) ios::in
B) ios::out
C) ios::app
D) ios::trunc

Answer: B) ios::out

9. Which function is used to read a file one character at a time?

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()

12. What does the getline() function do?


A) Reads a file one character at a time
B) Reads a file line by line
C) Writes data to a file
D) Copies data from one file to another

Answer: B) Reads a file line by line

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

14. What will the tellg() function return?

A) The position of the file pointer while reading


B) The position of the file pointer while writing
C) The length of the file
D) The contents of the file

Answer: A) The position of the file pointer while reading

15. What does the seekg() function do?

A) Positions the file pointer to a specified location for writing


B) Positions the file pointer to a specified location for reading
C) Returns the length of the file
D) Appends data to the file

Answer: B) Positions the file pointer to a specified location for reading

16. Which function is used to determine the length of a file?


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

Answer: C) tellg()

17. What does seekg(0, ios::end) do?

A) Moves the file pointer to the beginning of the file


B) Moves the file pointer to the end of the file
C) Moves the file pointer to 0th byte from the beginning
D) Moves the file pointer 10 bytes from the beginning

Answer: B) Moves the file pointer to the end of the file

18. Which of the following is a correct way to append data to a file in C++?

A) ofstream file("filename", ios::in);


B) ofstream file("filename", ios::out);
C) ofstream file("filename", ios::app);
D) ofstream file("filename", ios::trunc);

Answer: C) ofstream file("filename", ios::app);

19. What does seekp(-10L, ios::cur) do?

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?

A) It moves forward from the end of the file


B) It moves backward from the end of the file
C) It causes a runtime error
D) It moves to the beginning of the file

Answer: B) It moves backward from the end of the file

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()

22. What does seekp() do?

A) Moves the file pointer to a specified location for reading


B) Moves the file pointer to a specified location for writing
C) Moves the file pointer to the beginning of the file
D) Determines the file size

Answer: B) Moves the file pointer to a specified location for writing

23. What is the role of ios::app when opening a file?

A) Opens the file for reading


B) Appends data to the end of the file without deleting its contents
C) Deletes the contents of the file
D) Prevents reading from the file

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()

25. How do you copy a file in C++?

A) By using ifstream for reading and ofstream for writing


B) By using ofstream for reading and ifstream for writing
C) By using read() and write() functions
D) By using get() and put() in the same function

Answer: A) By using ifstream for reading and ofstream for writing

26. What will the following code snippet do?


cpp
Copy
aFile.seekg(10L, ios::beg);

A) Moves the file pointer to the beginning of the file


B) Moves the file pointer 10 bytes forward from the beginning of the file
C) Moves the file pointer 10 bytes backward from the beginning of the file
D) Reads 10 bytes from the file

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

Answer: A) It causes an error

28. What will the function tellp() return?

A) The position of the file pointer while reading


B) The position of the file pointer while writing
C) The total number of bytes in the file
D) The contents of the file

Answer: B) The position of the file pointer while writing

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

MCQs on File Handling in C++

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

File Handling Concepts and Functions

1. Which function is used to open a file in C++ for input?


○ A) ofstream
○ B) fstream
○ C) ifstream
○ D) open()
2. Answer: C) ifstream
3. What does the seekg() function do?
○ A) It moves the write pointer to a specified position.
○ B) It moves the read pointer to a specified position.
○ C) It opens a file.
○ D) It writes data to a file.
4. Answer: B) It moves the read pointer to a specified position.
5. What is the main difference between seekg() and seekp()?
○ A) seekg() is for output, and seekp() is for input.
○ B) seekg() is for input, and seekp() is for output.
○ C) They are the same function.
○ D) seekg() is used to write data, and seekp() is used to read data.
6. Answer: B) seekg() is for input, and seekp() is for output.
7. Which of the following opens a file for both reading and writing in C++?
○ A) ifstream
○ B) ofstream
○ C) fstream
○ D) ios::in
8. Answer: C) fstream
9. What is the purpose of tellg() in file handling?
○ A) It checks whether the file is open.
○ B) It moves the file pointer to a specified position.
○ C) It returns the current position of the read pointer.
○ D) It writes data to the file.
10. Answer: C) It returns the current position of the read pointer.
11. What does the read() function do in C++ file handling?
○ A) It reads a single character from the file.
○ B) It reads a specified number of bytes from a file into an array.
○ C) It writes data to a file.
○ D) It moves the file pointer to a specific position.
12. Answer: B) It reads a specified number of bytes from a file into an array.
13. Which of the following file operations is more efficient when handling large files?
○ A) getc() and putc()
○ B) read() and write()
○ C) fscanf() and fprintf()
○ D) seekg() and seekp()
14. Answer: B) read() and write()
15. How do you append data to an existing file in C++?
○ A) Open the file in ios::app mode.
○ B) Open the file in ios::in mode.
○ C) Use seekp() to move to the end of the file.
○ D) Open the file in ios::out mode.
16. Answer: A) Open the file in ios::app mode.
17. Which function would you use to read a file in reverse order in C++?
○ A) seekg(-1, ios::end)
○ B) seekp(-1, ios::end)
○ C) seekg(1, ios::begin)
○ D) seekg(0, ios::end)
18. Answer: A) seekg(-1, ios::end)
19. Which mode is used to open a file for both reading and writing in C++?
○ A) ios::in | ios::out
○ B) ios::app | ios::out
○ C) ios::in | ios::app
○ D) ios::out
20. Answer: A) ios::in | ios::out

Data Structures and Record Handling

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.

File Pointer Movement and Manipulation

17. What is the purpose of seekg(-1, ios::end)?


○ A) Move the read pointer to the beginning of the file.
○ B) Move the read pointer to the last byte of the file.
○ C) Move the read pointer to one byte before the end of the file.
○ D) Move the write pointer to the end of the file.
18. Answer: C) Move the read pointer to one byte before the end of the file.
19. What happens when you try to move the file pointer before the beginning of a file?
○ A) It results in an error.
○ B) It moves to the beginning of the file automatically.
○ C) It moves to the end of the file.
○ D) It causes undefined behavior.
20. Answer: A) It results in an error.
21. What does tellp() do in C++?
○ A) It returns the current position of the read pointer.
○ B) It returns the current position of the write pointer.
○ C) It moves the read pointer.
○ D) It closes the file.
22. Answer: B) It returns the current position of the write pointer.
23. How do you use seekp() to write at a specific position in a file?
○ A) Use seekp() before writing to set the file pointer to a position.
○ B) seekp() cannot be used for writing.
○ C) Use seekp() after writing to move to the next byte.
○ D) Use seekg() to move the write pointer.
24. Answer: A) Use seekp() before writing to set the file pointer to a position.
File Copying and Performance

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.

Binary Files and Data Types

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

28. How can you reverse a file using seekg()?


○ A) Read the file normally and store it in reverse order.
○ B) Start from the end and move backward by using seekg(-1, ios::end)
and seekg(-1, ios::cur).
○ C) Use seekg(1, ios::end) to reverse the file.
○ D) Read the entire file in reverse order using read().
29. Answer: B) Start from the end and move backward by using seekg(-1,
ios::end) and seekg(-1, ios::cur).
30. What would happen if you try to access a position beyond the end of the file using
seekg()?
○ A) The program will automatically append data.
○ B) An error will be thrown or undefined behavior will occur.
○ C) The file pointer will move to the end of the file.
○ D) The data will be written to the file.
31. Answer: B) An error will be thrown or undefined behavior will occur.
32. What does seekg(-2, ios::cur) do?
○ A) Moves the read pointer to two positions backward from the current position.
○ B) Moves the write pointer to two positions backward from the current position.
○ C) Moves the pointer to the end of the file.
○ D) Moves the pointer to the beginning of the file.
33. Answer: A) Moves the read pointer to two positions backward from the current
position.
34. What function is used to move the write pointer in a file?
○ A) seekg()
○ B) seekp()
○ C) tellg()
○ D) tellp()
35. Answer: B) seekp()
Miscellaneous

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.

You might also like