Introduction to Programming MCQs with Answers
Practice important Introduction to Programming MCQs with answers and explanations.
Chapter: 19
Q301:
What method was used historically to insert data into the middle of a file?
A. Split Method
B. Merge Method
C. Overwrite Method
D. Append Method
Correct Answer:
B
Chapter: 19
Q302:
Which function is used for reading from a file?
A. seekg()
B. seekp()
C. tellg()
D. tellp()
Correct Answer:
A
Chapter: 19
Q303:
Which function is used for writing to a file?
A. seekg()
B. seekp()
C. tellg()
D. tellp()
Correct Answer:
B
Chapter: 19
Q304:
What is the key element to ensure no data corruption when inserting data into the middle of a file?
A. Variable record length
B. Constant record length
C. Sequential record length
D. Random record length
Correct Answer:
B
Chapter: 19
Q305:
What type of comparison is more efficient for locating a record within a file?
A. String comparison
B. Integer comparison
C. Boolean comparison
D. Float comparison
Correct Answer:
B
Chapter: 19
Q306:
What technique can be used to open a file for both input and output in C++?
A. ANDing ios::in and ios::out
B. ORing ios::in and ios::out
C. XORing ios::in and ios::out
D. NORing ios::in and ios::out
Correct Answer:
B
Chapter: 19
Q307:
How can we efficiently read large amounts of data from a file?
A. Using getc() and putc()
B. Using read() and write()
C. Using seekg() and tellg()
D. Using fstream and ofstream
Correct Answer:
B
Chapter: 19
Q308:
What is the primary advantage of using read() and write() functions for file operations?
A. They are faster for small data
B. They allow character-by-character operations
C. They process data in chunks, reducing the number of function calls
D. They support string operations
Correct Answer:
C
Chapter: 19
Q309:
What is the correct order of operations to copy a file in reverse order?
A. Open file, seekg(), read, close
B. Open file, read, seekg(), close
C. Open file, seekg(), close, read
D. Open file, write, seekp(), close
Correct Answer:
A
Chapter: 19
Q310:
How do you move the file pointer to the second last byte of a file?
A. seekg(-2, ios::end)
B. seekp(-1, ios::end)
C. seekg(-1, ios::beg)
D. seekp(-2, ios::beg)
Correct Answer:
A
Chapter: 19
Q311:
Which stream class can be used for both input and output operations in C++?
A. ifstream
B. ofstream
C. fstream
D. file_stream
Correct Answer:
C
Chapter: 19
Q312:
When replacing a character in a sentence within a file, what function can be used to move the file pointer to a specific position?
A. seekg()
B. seekp()
C. tellg()
D. tellp()
Correct Answer:
B
Chapter: 19
Q313:
How does the tellg() function help in file operations?
A. It opens the file
B. It writes data to the file
C. It returns the current position of the file pointer
D. It moves the file pointer to a specific location
Correct Answer:
C
Chapter: 19
Q314:
What is the impact of using constant record lengths in files?
A. Ensures efficient string comparison
B. Allows updating data without disturbing the file structure
C. It increases file size
D. It makes the file access slower
Correct Answer:
B
Chapter: 19
Q315:
Why is it not practical to insert data in the middle of a sequential file without additional techniques?
A. It can corrupt the file
B. It requires complex algorithms
C. Disk space needs to be reorganized
D. It requires specialized hardware
Correct Answer:
C
Chapter: 19
Q316:
What is the function of the seekg() function in file handling?
A. It reads data from a file.
B. It writes data to a file.
C. It moves the file pointer to a specific location.
D. It closes the file.
Correct Answer:
C
Chapter: 19
Q317:
In the code aFile.seekg(-1L, ios::end);, what does -1L signify?
A. Move the file pointer forward by one byte.
B. Move the file pointer backward by one byte from the end of the file.
C. Move the file pointer to the beginning of the file.
D. Move the file pointer to the end of the file.
Correct Answer:
B
Chapter: 19
Q318:
What happens when you attempt to move the file pointer before the beginning of a file?
A. The file pointer wraps around to the end of the file.
B. An error occurs.
C. The file pointer remains at the beginning of the file.
D. The file pointer moves to the middle of the file.
Correct Answer:
B
Chapter: 19
Q319:
What is the advantage of using the sizeof() function while writing an integer to a file?
A. It avoids magic numbers and ensures portability across different systems.
B. It speeds up the file writing process.
C. It reduces the file size.
D. It ensures the file is written in text format.
Correct Answer:
A
Chapter: 19
Q320:
In the program that writes integers from 0 to 99 into a file using the write() function, what type of write operation is performed?
A. Text write
B. Binary write
C. ASCII write
D. Hexadecimal write
Correct Answer:
B