1.
Which header file is required to use file output operations?
Correct Answer
A. <ofstream>
Explanation
The header file is required to use file output operations. This header file provides the necessary functions and classes for writing data to files. It includes the ofstream class, which is used to create output file streams and perform output operations on them. By including this header file, we can access the necessary functionality for writing data to files in our program.
2.
Which of the following is used to create a stream that performs both input and output operations?
Correct Answer
C. <fstream>
Explanation
The correct answer is . This is because is a C++ library that allows for both input and output operations on files. It provides the functionality to create a stream that can read from and write to a file simultaneously. The other options, and , are used for either output or input operations respectively. is a library used for manipulating input/output formatting.
3.
Which of the following is not used as a file opening mode?
Correct Answer
A. Ios:trunc
Explanation
The option "ios:trunc" is not used as a file opening mode. This is because "ios:trunc" is not a valid file opening mode in C++ or any programming language. The correct file opening modes in C++ are "ios::in", "ios::out", "ios::app", "ios::ate", and "ios::binary".
4.
By default, all the files in C++ are opened in _________ mode.
Correct Answer
A. Text
Explanation
In C++, by default, all files are opened in "Text" mode. This means that the file is treated as a text file, where characters are represented using their ASCII values. This mode allows for easy reading and writing of text data to and from the file. Other modes, such as "Binary" or "ASCII", may be used when dealing with non-textual data or specific encoding formats. However, in the absence of any specified mode, the default is "Text" mode.
5.
What is the use of ios::trunc mode?
Correct Answer
D. To truncate an existing file to zero
Explanation
The ios::trunc mode is used to truncate an existing file to zero. This means that when a file is opened in this mode, it will delete all the contents of the file and make it empty. This can be useful in situations where we want to start writing new data to a file from scratch, without any previous content.
6.
What is the return type open() method?
Correct Answer
D. Bool
Explanation
The return type of the open() method is bool, which stands for boolean. This means that the open() method will return either true or false. A boolean value is commonly used to indicate the success or failure of an operation. In the context of the open() method, it is likely that a true value would indicate that the file or resource was successfully opened, while a false value would indicate that the operation was unsuccessful.
7.
Which of the following is not used to seek file pointer?
Correct Answer
A. Ios:set
Explanation
The "ios:set" option is not used to seek the file pointer. The other options, "ios::cur", "ios::beg", and "ios::end" are used to seek the file pointer to the current position, the beginning of the file, and the end of the file, respectively. However, "ios:set" is not a valid option for seeking the file pointer.
8.
Which of the following is the default mode of the opening using the fstream class?
Correct Answer
C. Ios::in|ios::out
Explanation
The default mode of opening using the fstream class is ios::in|ios::out. This mode allows both input and output operations on the file.
9.
Which function is used in C++ to get the current position of file pointer in a file?
Correct Answer
A. Tellp()
Explanation
The tellp() function is used in C++ to get the current position of the file pointer in a file. It returns the current position as a streampos object, which represents the position in the stream. This function is typically used with file streams to determine the current position before performing any read or write operations.
10.
Which function is used to reposition the file pointer?
Correct Answer
C. Seekg()
Explanation
The seekg() function is used to reposition the file pointer. It allows us to move the file pointer to a specific position within the file, which can be useful when we want to read or write data at a specific location. The tellg() function is used to get the current position of the file pointer. The change() and getch() functions are not related to repositioning the file pointer.
11.
Which of the following is used to move the file pointer to start of a file?
Correct Answer
B. Ios::beg
Explanation
The option "ios::beg" is used to move the file pointer to the start of a file. This is a flag in the ios class that represents the beginning of a file. By using this flag, the file pointer is set to the beginning of the file, allowing operations to be performed from the start.
12.
Which function allows you to set minimum width for the next input?
Correct Answer
B. Setw()
Explanation
The function setw() allows you to set the minimum width for the next input. This function is commonly used in C++ programming to specify the minimum number of characters to be written in the output. It is often used in combination with the
13.
What is a function template?
Correct Answer
A. Creating a function without having to specify the exact type
Explanation
A function template allows the creation of a function without specifying the exact type that it will operate on. This means that the function can be written in a generic way, allowing it to work with different types of data. It provides flexibility and reusability in code, as the same function template can be used with different types of arguments. The exact type is determined when the function is called, based on the argument provided.
14.
Which is used to describe the function using placeholder types?
Correct Answer
B. Template type parameters
Explanation
Template type parameters are used to describe the function using placeholder types. These parameters allow the function to accept different data types as arguments, without the need to specify the exact type beforehand. By using template type parameters, the function becomes more flexible and can be used with various data types, providing a generic solution.
15.
Pick out the correct statement.
Correct Answer
A. You only need to write one function, and it will work with many different types
Explanation
The correct statement is "you only need to write one function, and it will work with many different types." This means that by writing just one function, you can handle multiple types of inputs, making the code more efficient and reusable. It eliminates the need to write separate functions for each type, reducing code duplication and making the program easier to maintain.