My Report

C++ File and Stream Test – 3


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. Which member function is used to determine whether the stream object is currently associated with a file?

2. What will be the output of the following C++ code?

    #include<iostream>
    #include <fstream>
    using namespace std;
    int main () 
    {
        ofstream outfile ("test.txt");
        for (int n = 0; n < 100; n++)
        {
            outfile << n;
            outfile.flush();
        }
        cout << "Done";
        outfile.close();
        return 0;
    }

3. What will be the output of the following C++ code?

    #include <iostream>
    using namespace std;
    int main ()
    {
        int a = 100;
        double b = 3.14;
        cout << a;
        cout << endl;
        cout << b << endl << a * b;
        endl (cout);
        return 0;
    }

4. What will be the output of the following C++ code?

    #include <iostream>
    #include <fstream>
    using namespace std;
    int main () 
    {
        int length;
        char * buffer;
        ifstream is;
        is.open ("sample.txt", ios :: binary );
        is.seekg (0, ios :: end);
        length = is.tellg();
        is.seekg (0, ios :: beg);
        buffer = new char [length];
        is.read (buffer, length);
        is.close();
        cout.write (buffer, length);
        delete[] buffer;
        return 0;
    }

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

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

7. What will be the output of the following C++ code?

    #include <iostream>
    using namespace std;
    int main ()
    {
        char first, second;
        cout << "Enter a word: ";
        first = cin.get();
        cin.sync();
        second = cin.get();
        cout << first << endl;
        cout << second << endl;
        return 0;
    }

8. Which header file is used for reading and writing to a file?

9. By seeing which operator thus this C++ program stops getting the input?

    #include <iostream>
    #include <fstream>
    using namespace std;
    int main ()
    {
        char ch;
        streambuf * p;
        ofstream os ("test.txt");
        pbuf = os.rdbuf();
        do {
            ch = cin.get();
            p -> sputc(ch);
        } while (ch != '.');
        os.close();
        return 0;
    }

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


 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.