Skip to main content

Introduction to Programming MCQs with Answers

Practice important Introduction to Programming MCQs with answers and explanations.

Chapter: 18
Q261:
How can you read multiple words from a file into different variables in C++?
A. myFile << c1 << c2 << c3;
B. myFile.read(c1, c2, c3);
C. myFile >> c1 >> c2 >> c3;
D. myFile.scanf(c1, c2, c3);
Correct Answer: C
Chapter: 18
Q262:
What is the correct syntax to close a file in C++?
A. myFile.close();
B. close(myFile);
C. fclose(myFile);
D. file.close(myFile);
Correct Answer: A
Chapter: 18
Q263:
Which function is used to check for the end of a file while reading in C++?
A. myFile.end()
B. myFile.eof()
C. myFile.close()
D. myFile.read()
Correct Answer: B
Chapter: 18
Q264:
In the context of file handling, what does the term 'volatile' refer to?
A. File data that is stored permanently
B. File data that is temporarily stored in memory
C. File data that can be executed as a program
D. File data that can be shared over the network
Correct Answer: B
Chapter: 18
Q265:
What is the purpose of the outFile.open(outputFileName, ios::out); statement in the provided code?
A. To read data from the file
B. To write data to the file
C. To close the file
D. To check if the file exists
Correct Answer: B
Chapter: 18
Q266:
What does the outFile << ouputText; statement do in the code?
A. It reads text from the file
B. It writes text to the file
C. It closes the file
D. It opens the file
Correct Answer: B
Chapter: 18
Q267:
What will happen if the file \"myFileOut.txt\" cannot be opened?
A. The program will continue normally
B. The program will display an error and exit
C. The program will create a new file
D. The program will write an error message to the file
Correct Answer: B
Chapter: 18
Q268:
In the program to increase employee salaries, what does the strtok() function do?
A. Reads the entire line as a string
B. Splits the string into tokens based on the delimiter
C. Converts a string to an integer
D. Writes data to the file
Correct Answer: B
Chapter: 18
Q269:
What is the purpose of the atoi(tokenPtr); function in the employee salary program?
A. To convert an integer to a string
B. To convert a string to an integer
C. To read a line from the file
D. To write a string to the file
Correct Answer: C
Chapter: 18
Q270:
Why is it important to close a file after operations are done using close() function?
A. To prevent memory leaks
B. To ensure all data is written and resources are freed
C. To open another file
D. To convert file data to a string
Correct Answer: B
Chapter: 18
Q271:
What is the function getline() used for in the code provided?
A. To write a line to the file
B. To read a line from the file
C. To open a file
D. To split a string into tokens
Correct Answer: B
Chapter: 18
Q272:
What is the purpose of checking if (!inFile) in the provided program?
A. To ensure the file can be opened for reading
B. To write data to the file
C. To check if the file is empty
D. To convert file contents to integers
Correct Answer: A
Chapter: 18
Q273:
In the given code, what will inFile.getline(completeLineText, MAX_CHAR_TO_READ); do?
A. It will write a line to the file
B. It will read a line from the file into the completeLineText array
C. It will close the file
D. It will open the file
Correct Answer: B
Chapter: 18
Q274:
What will the output be when the input file contains Hello World and inFile.getLine(str, 20, ‘W’); is executed?
A. Hello World
B. Hello
C. World
D. ello
Correct Answer: B
Chapter: 19
Q275:
What does the concept of sequential access in files imply?
A. Writing in a random manner
B. Writing and reading in a sequence
C. Reading from the end
D. Skipping unread data
Correct Answer: B
Chapter: 19
Q276:
Which function is used to open a file in C++?
A. read()
B. open()
C. close()
D. write()
Correct Answer: B
Chapter: 19
Q277:
What does the close() function do in file handling?
A. Opens a file
B. Closes a file
C. Reads a file
D. Writes to a file
Correct Answer: B
Chapter: 19
Q278:
Which mode is used by default when opening a file for reading with ifstream?
A. ios::out
B. ios::in
C. ios::app
D. ios::trunc
Correct Answer: B
Chapter: 19
Q279:
What is the default behavior of ofstream when opening a file for writing?
A. Appends data to the file
B. Deletes the previous contents of the file
C. Opens the file in read mode
D. Creates a new file if it doesn’t exist
Correct Answer: B
Chapter: 19
Q280:
Which mode is used to append data to an existing file?
A. ios::trunc
B. ios::in
C. ios::out
D. ios::app
Correct Answer: D

Test Your Knowledge

Take a timed quiz on Introduction to Programming

🚀 Start Quiz Now