0

File Handling

Description: File Handling
Number of Questions: 15
Created by:
Tags: File Handling Java/Oracle /C/C++
Attempted 0/15 Correct 0 Score 0

Which of the following is the disadvantage of binary data files?

  1. They can be easily read and written.

  2. They need a special program to read and write.

  3. Processing of binary files is slow.

  4. Binary data files are damaged easily.


Correct Option: B
Explanation:

Binary data files need a special program to read and write. Because binary data files contain only 0 and 1. 

To open the file for reading purpose, we have to create an object of ____________ class.

  1. iostream

  2. ofstream

  3. ifstream

  4. iofstream


Correct Option: C
Explanation:

The ifstream class is used to create an object for opening a file to read.  

Which of the following member functions is used to get a single character from a file?

  1. get()

  2. getc()

  3. cin

  4. put()


Correct Option: A
Explanation:

The member function get() is used to get a single character from a file. And put() is used to write a character to a file. 

Which of the options shows the correct steps to process a file in C++ program?

  1. Name the file, open the file and close the file

  2. Open the existing file, read/write the file and close the file

  3. Open the file, read the file and write a file

  4. Create a file, open the file and close the file


Correct Option: B
Explanation:

The steps to process a file in C++  are

  1. Open the file
  2. Read or write the file
  3. Close the file

Which of the following functions give the current position of the file pointer?

  1. tellme() and giveme()

  2. tellp() and tellg()

  3. put() and get()

  4. seekp() and seekg()


Correct Option: B
Explanation:

The current position of the file pointer is given by the tellp() and tellg() member functions of ostream and istream respectively. 

Which of the following is the invalid open mode of a file in C++?

  1. In

  2. out

  3. app

  4. ale


Correct Option: D
Explanation:

The open mode in, out and ate are the valid modes for reading, writing and appending respectively. There is no any ale mode in C++. 

Normally a file is opened with positioning the cursor at the beginning of the file. But which mode opens the file by positioning the cursor at the end of file?

  1. ate 

  2. app 

  3. end 

  4. aen 


Correct Option: A
Explanation:

The open mode “ate” is used to open the file by position the file pointer at the end of file. 

Which statement takes the file pointer to the beginning of the file?

  1. filepointer.seekg(0L,ios::start);

  2. filepointer.seekg(1L,ios::beg);

  3. filepointer.seekg(0L,ios::beg);

  4. filepointer.seekg(0L,io::beg);


Correct Option: C
Explanation:

The function seekg() is used to position the file pointer at a specific place. So, the statement filepointer.seekg(0L,ios::beg); positions the file pointer at the beginning of the file. 

Which of the following file opening modes shows error, if the file does not already exist?

  1. ios::in

  2. ios::out

  3. ios::noreplace

  4. ios::nocreate


Correct Option: D
Explanation:

The file opening mode ios::nocreate gives the error if the file does not already exist. 

____________ is used when we want to access the streams of bytes from memory.

  1. istream class

  2. ostream class

  3. strstream class

  4. istrstream class


Correct Option: D
Explanation:

The istrstream class is used to create an object for accessing the streams of bytes from memory.

How to check whether a file stream f1 was successfully opened or not?

  1. if(f1.open()) { .......... }

  2. if(f1.isopen) { ........... }

  3. if(f1.is_open()) { ............. }

  4. If(f1.is_open) { ................ }


Correct Option: C
Explanation:

The correct format to check whether a file stream f1 was successfully opened or not is as follows: if(f1.is_open()) { .................. } 

Which method can be used to check the status after read/write operation?

  1. good()

  2. rdstate()

  3. clear()

  4. isstate()


Correct Option: A
Explanation:

The member function good() is used to check whether the state of the stream for input/output operation is good or not. 

Which of the following is not used for reading a text file?

  1. get(ch) function

  2. geline() function

  3. extraction operator

  4. open() function


Correct Option: D
Explanation:

The get(ch), getline() and extraction operators can be used to read a text file. But open() function can be used only to open the file in a particular mode.

Which of the following is the default mode for a open() member function of fstream class?

  1. ios::in

  2. ios::out

  3. ios::app

  4. ios::in | ios::out


Correct Option: D
Explanation:

The default mode for a open() member function of fstream class is ios::in | ios::out. 

How can the integer variable n be displayed in octal format in C++?

  1. cout<<oct(n);

  2. cout<<octal(n);

  3. cout<<n<<oct;

  4. cout<<oct<<n;


Correct Option: D
Explanation:

To display the integer variable in octal format, we have to use the “oct” keyword between the cout and variable using << operator.

- Hide questions