C++ Programming Multiple Choice Questions - File Handling

This section focuses on the "File Handling" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.

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

A. ofstream
B. ifstream
C. fstream
D. iostream

View Answer


2. 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

View Answer


3. 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.

View Answer


4. 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

View Answer


5. 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

View Answer


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

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

View Answer


7. 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);

View Answer


8. What will be the output of the following program?

         
Note:Includes all required header files
using namespace std;
 
int main() 
{
   ofstream ofile; 
   ofile.open ("find.txt"); 
   ofile << "letsfindcourse" << endl;
   cout << "Data written to file" << endl;
   ofile.close(); 
   return 0;
}  

A. Compile error
B. Runtime error
C. The program prints ""letsfindcourse"" in the file find.txt
D. none of the above

View Answer


9. 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

View Answer


10. What is the output of this program?

        
Note:Includes all required header files 
          
using namespace std;
    int main () 
    {
        ofstream outfile ("find.txt");
        for (int i = 0; i < 70; i++)
        {
            outfile << i;
            outfile.flush();
        }
        cout << "Done";
        outfile.close();
        return 0;
    }       

A. Done
B. Error
C. Runtime error
D. None of the mentioned

View Answer


11. What is the output of this program?

         
Note:Includes all required header files

 using namespace std;
    int main ()
    {
        int p = 1000;
        double q = 3.14;
        cout << p;
        cout << endl;
        cout << q << endl << p * q;
        endl (cout);
        return 0;
    }

A. 1000
B. 3.14
C. 3140
D. All of the above

View Answer


12. Which of the following is true about the following program

         
Note:Includes all required header files
using namespace std;
    int main () 
    {
            char i;
        streambuf * p;
        ofstream of ("find.txt");
        pbuf = of.rdbuf();
        do {
            i = cin.get();
            p -> sputc(i);
        } while (i != '.');
        of.close();
         return 0;
    }

A. insertion operator
B. $ symbol
C. dot operator
D. none of the mentioned

View Answer


13. What will be the output of this program?

         
Note:Includes all required header files
using namespace std;
    int main ()
    {
        int p = 10;
        double q = 1.14;
        
        cout << p + q;
        endl (cout);
        return 0;
    }

A. 10
B. 1.14
C. 11.14
D. All of the above

View Answer


14. 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

View Answer


15. If we have object from ofstream class, then default mode of opening the file is _____

A. ios::in
B. ios::out
C. ios::in|ios::trunc
D. ios::out|ios::trunk

View Answer


16. Which is correct syntax for, position n bytes back from end of fileObject ?

A. FileObject.seekg(ios::end, n);
B. FileObject.seekg(n, ios:end );
C. FileObject.seekg(n, ios::end );
D. FileObject.seekg(ios:end, n);

View Answer


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

A. EOF
B. Null
C. Runtime error
D. Compiler dependent

View Answer


18. 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

View Answer


19. How many objects are used for input and output to a string?

A. 1
B. 2
C. 3
D. 4

View Answer


20. Calling the stream's member function sync() causes an immediate synchronization.

A. Yes
B. NO
C. May Be
D. Can't Say

View Answer


21. Which of the following is not used to seek a file pointer?

A. ios::cur
B. ios::beg
C. ios::end
D. ios::set

View Answer


22. What is the output of this program?

         
Note:Includes all required header files
using namespace std;
    int main () 
    {
        int l;
        char * b;
        ifstream i;
        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

View Answer


23. 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

View Answer


24. 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

View Answer


25. In fopen(), the open mode "wx" is sometimes preferred "w" because. i) Use of wx is more efficient. ii) If w is used, old contents of file are erased and a new empty file is created. When wx is used, fopen() returns NULL if file already exists.

A. Only i
B. Only ii
C. Both i & ii
D. None of the above

View Answer


26. 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

View Answer


27. 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

View Answer


28. 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

View Answer


29. Which function is used to position back from the end of file object?

A. Seekg
B. Seekp
C. Both seekg & seekp
D. None of the above

View Answer





Also check :


Discussion



* You must be logged in to add comment.

Abdul Haseeb
very conceptual questions