0% found this document useful (0 votes)
14 views

C ++ Module 4

This document discusses input/output streams in C++. It covers topics like file streams, file opening modes, reading and writing to files, string streams, and functions like getc(), fseek(), rewind() etc. There are examples and explanations provided for each question.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

C ++ Module 4

This document discusses input/output streams in C++. It covers topics like file streams, file opening modes, reading and writing to files, string streams, and functions like getc(), fseek(), rewind() etc. There are examples and explanations provided for each question.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Module 4

I/O Streams
1. 64. Predict the output:

float x= 3.1496;
cout<<setprecision(2) << x;
A. 3.14
B. 3.15
C. 3
D. 3.1

2. Which stream class is to only write on files ?


A. ofstream
B. ifstream
C. fstream
D. iostream
Explanation: ofstream class is to only write on files.

3. It is not possible to combine two or more file opening mode in open () method.
A. TRUE
B. FALSE
C. May Be
D. Can't Say

4. Which of these is the correct statement about eof() ?


A. Returns true if a file open for reading has reached the next character.
B. Returns true if a file open for reading has reached the next word.
C. Returns true if a file open for reading has reached the end.
D. Returns true if a file open for reading has reached the middle.
Explanation: Returns true if a file open for reading has reached the end is the correct statement
about eof().

5. Which of the following true about FILE *fp


A. FILE is a structure and fp is a pointer to the structure of FILE type
B. FILE is a buffered stream
C. FILE is a keyword in C for representing files and fp is a variable of FILE type
D. FILE is a stream
Explanation: fp is a pointer of FILE type and FILE is a structure that store following information
about opened file
6. Which of the following methods can be used to open a file in file handling?
A. Using Open ( )
B. Constructor method
C. Destructor method
D. Both A and B
Ans : D

7. Which operator is used to insert the data into file?


A. >>
B. <<
C. <
D. None of the above

8. Which is correct syntax ?


A. myfile:open ("example.bin", ios::out);
B. myfile.open ("example.bin", ios::out);
C. myfile::open ("example.bin", ios::out);
D. myfile.open ("example.bin", ios:out);

9. What is the output of this program?


Note:Includes all required header files
using namespace std;
int main ()
{
int l;
char * b;
ifstreami;
i.open ("find.txt", ios :: binary );
i.seekg (0, ios :: end);
l = i.tellg();
i.seekg (0, ios :: beg);
b = new char [l];
i.read (b, l);
i.close();
cout.write (b, l);
delete[] b;
return 0;
}
A. Error
B. find
C. This is find
D. Runtime error
Explanation: In this program, if the file exist, it will read the file. Otherwise it will throw an
exception. A runtime error will occur because the value of the l variable will be ""-1"" if file
doesn't exist and in line 13 we are trying to allocate an array of size "-1".

10. What is the output of this program?


Note:Includes all required header files
using namespace std;
int main ()
{
char fine, course;
cout<< "Enter a word: ";
fine = cin.get();
cin.sync();
course = cin.get();
cout<< fine <<endl;
cout<< course <<endl;
return 0;
}
A. course
B. fine
C. Returns fine 2 letter or number from the entered word
D. None of the mentioned
Explanation: In this program, We are using the sync function to return the fine two letters of
the entered word.

11. ios::trunc is used for ?


A. If the file is opened for output operations and it already existed, no action is taken.
B. If the file is opened for output operations and it already existed, then a new copy is created.
C. If the file is opened for output operations and it already existed, its previous content is
deleted and replaced by the new one.
D. None of the above

12. What will be the output of this program?


Note:Includes all required header files
using namespace std;
int main ()
{
FILE *fp;
char x[1024];
fp = fopen("find.txt", "r"); // "ayushjain and prateek"
x[0] = getc(fp);
fseek(fp, 0, SEEK_END);
fseek(fp, -7L, SEEK_CUR);
fgets(x, 6, fp);
puts(x);
return 0;
}
A. ayushj
B. yushja
C. ayushja
D. prate
Ans : D
Explanation: If we have object from ofstream class, then default mode of opening the file is
ios::out|ios::trunk.

13. When fopen() is not able to open a file, it returns


A. EOF
B. Null
C. Runtime error
D. Compiler dependent
Ans : B
Explanation: fopen() returns NULL if it is not able to open the given file due to any of the
reasons like file not present, inappropriate permissions, etc.

14. By default, all the files are opened in which of the following mode?
A. Binary Mode
B. Text Mode
C. Sequential Mode
D. Both A and B
Ans : B
Explanation: By default, all the files are opened in Text mode

15. How many objects are used for input and output to a string?
A. 1
B. 2
C. 3
D. 4
Ans : C
Explanation: The stringstream, ostringstream, and istringstream objects are used for input and
output to a string.
16. Which member function is used to determine whether the stream object is currently
associated with a file?
A. is_open
B. Buf
C. String
D. None of the above
Ans : A
Explanation: The member function is_open can be used to determine whether the stream
object is currently associated with a file.

17. getc() returns EOF when


A. End of files is reached
B. When getc() fails to read a character
C. Both A & B
D. None of the above
Ans : C
Explanation: None

18. fseek() should be preferred over rewind() mainly because


A. In rewind, there is no way to check if the operations completed successfully
B. rewind() doesn't work for empty files
C. rewind() does work for empty file
D. All of the above

You might also like