C++ Files & Streams Aptitude Questions and Answers

C++ Files & Streams Aptitude: This section contains C++ Files & Streams Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 22, 2021

1) Which of the following header file is used for file handling in C++?

  1. <file>
  2. <filestream>
  3. <stream>
  4. <fstream>

2) Which of the following classes are used for file handling in C++?

  1. fstream
  2. ifstream
  3. ofstream
  4. ostream

Options:

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

3) Which of the following statements are correct about the fstream class in C++?

  1. This is used to read data from the file.
  2. This is used to write data into the file
  3. This is used to create a file.
  4. This is used to delete a file.

Options:

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

4) Which of the following modes are used to open a file in C++?

  1. ios::in
  2. ios::out
  3. ios::app
  4. ios::trunc

Options:

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

5) Which of the following mode is used to open a file and move the read and write control to the end of the file?

  1. ios::ate
  2. ios::in
  3. ios::out
  4. ios::trunc

6) Which of the following mode is used to open an existing file and remove its content before opening the file?

  1. ios::ate
  2. ios::in
  3. ios::out
  4. ios::trunc

7) Which of the following function is used to check a file is opened or not?

  1. isopen()
  2. is_open()
  3. isopened()
  4. None of the above

8) What is return type of "is_open()" function?

  1. int
  2. bool
  3. char
  4. short

9) Which of the following mode is used to open a file in binary mode?

  1. ios::bin
  2. ios::binary
  3. ios::bins
  4. None of the above

10) Which of the following function is used to check no space left in the device?

  1. nospace()
  2. isspace()
  3. bad()
  4. ismem()

11) Which of the following are correct functions used to handle file streams?

  1. bad()
  2. good()
  3. fail()
  4. pass()

Options:

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

12) Which of the following function is used to reset the state flags?

  1. readline()
  2. getline()
  3. line()
  4. rline()

13) We can write data into the file using an insertion operator?

  1. Yes
  2. No

14) Which of the following function is used to reset the state flags?

  1. reset()
  2. resetflags()
  3. delete()
  4. clear()

15) What is correct output of given code snippets?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream fs;

    char data[16];

    fs.open("abc.txt");

    fs << "hello world" << endl;

    getline(fs, data);

    fs.close();

    cout << data << endl;

    return 0;
}

Options:

  1. hello world
  2. no output
  3. syntax error
  4. Runtime error

16) What is correct output of given code snippets?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream fs;

    string data;

    fs.open("abc.txt");

    fs << "hello world" << endl;

    getline(fs, data);

    fs.close();

    cout << data << endl;

    return 0;
}

Options:

  1. hello world
  2. no output
  3. syntax error
  4. Runtime error

17) What is correct output of given code snippets?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream ofs;
    ifstream ifs;
    string data;

    ofs.open("abc.txt");

    ofs << "hello world" << endl;

    getline(ifs, data);

    ifs.close();

    cout << data << endl;

    return 0;
}

Options:

  1. hello world
  2. no output
  3. Syntax error
  4. Runtime error

18) What is correct output of given code snippets?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream ofs;
    ifstream ifs;
    string data;

    ofs.open("abc.txt");

    ofs << "hello world" << endl;
    ofs.close();

    ifs.open("abc.txt");
    getline(ifs, data);

    ifs.close();

    cout << data << endl;

    return 0;
}

Options:

  1. hello world
  2. no output
  3. Syntax error
  4. Runtime error

19) Which of the following functions are used for pointer positioning in a file?

  1. seekp()
  2. seekg()
  3. tellp()
  4. tellg()

Options:

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

20) Which of the following flags are used for pointer positioning in a file?

  1. ios::start
  2. ios::current
  3. ios::end
  4. ios::middle

Options:

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

21) Which of the following pointer positioning flags is used to move to get and put pointer at the starting of the file?

  1. ios::str
  2. ios::beg
  3. ios::begninig
  4. None of the above

22) Which of the following function is used to write a single character into a file?

  1. write()
  2. put()
  3. set()
  4. putchr()

23) What is correct output of given code snippets?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream ofs;

    ofs.open("abc.txt");

    ofs << "hello world" << endl;

    cout << ofs.tellg() << endl;
    ofs.close();

    return 0;
}

Options:

  1. 0
  2. 1
  3. Syntax error
  4. Runtime error

24) What is correct output of given code snippets?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream ofs;

    ofs.open("abc.txt");

    ofs << "hello world" << endl;

    cout << ofs.tellp() << endl;
    ofs.close();

    return 0;
}

Options:

  1. 11
  2. 12
  3. 13
  4. Syntax error

25) What is correct output of given code snippets?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream ofs;

    ofs.open("abc.txt");

    ofs << "hello world";

    cout << ofs.tellp() << endl;
    ofs.close();

    return 0;
}

Options:

  1. 11
  2. 12
  3. 13
  4. Syntax error

26) Which of the following function is used to read a single character from a file?

  1. read()
  2. get()
  3. getchr()
  4. readchr()





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.