Object Oriented Programming Interview Questions - Part - 7

1. What is stream?
The I/O system in C++ is designed to work with a wide variety of devices including terminals, disks and tape drives. Although each device is very different, the I/O system supplies an interface to the programmer that is independent of the actual device being accessed. This interface is known as stream. A stream is a sequence of bytes. It acts
either as source from which the input data can be obtained or a destination to which the output data can be set.
2. What is Input Strem and Output Stream ?
A stream is a sequence of bytes. It acts either as source from which the input data can
be obtained or a destination to which the output data can be set. The source stream that
provides data to the program is called the input stream and the destination stream that
receives output from the program is called the output stream.
3. What is stream class?
The C++ I/O system contains a hierarchy of classes that are used to define various
streams to deal with both the console and disk files. These classes are called stream
classes. These classes are declared in the header file iostream.
4. Explain stream class for console operations
Class name Contents
ios (General
stream class)
Contains basic facilities that are used by all other input and output
Also contains a pointer to a buffer object
istream (input
Inherits the properties of ios
Declares input functions such as get(),getline() and read
ostrem (Output
Inherits the properties of ios
Declares output funciotns put() and write()
Streambuf Provides an interface to physical devices through buffers
Acts as a base for filebuf class used ios files.
5. Explain getline() and write() functions.
We can read and display a line of text more efficiently using the line oriented
input/output functions getline() and write(). The getline() function reads a whole line of
text that ends with a newline character. This function can be invoked by using the object
cin as follows.
cin.getline (line,size);
char name[20];
cout.write (line,size);
the first argument line represents the name of the string to be displayed and the second
argument size indicates the number of characters to display.
6. Explain ios format functions?
Function Task
width() To specify the required field size for displaying an output value
precision() To specify the number of digits to be displayed after the decimal point
of a float value
Fill() To specify a character that is used to fill the unused portion of a field.
setf() To specify format flags that can control the form of output display
unsetf() To clear the flags specified.
7. Explain manipulators in C++
Function Task
setw() To specify the required field size for displaying an output value
setprecision() To specify the number of digits to be displayed after the decimal point
of a float value
setfill() To specify a character that is used to fill the unused portion of a field.
setiosflags() To specify format flags that can control the form of output display
Resetiosflags() To clear the flags specified.
8. Explain width, precision ios format functions?
Width() function to define the width of a field necessary for the output of an item. Since,
it is a member function, we have to use an object to invoke it.
the floating numbers are printed with six digits after the decimal point. However, we can
specify the number of digits to be displayed after the decimal point while printing the
floating-point numbers. This can be done by using the preceision() member function
output is : 3.141
5 3 4
9. Write about fill() function with example?
Printing the values using much larger field widths than required by the values.
They unused positions of the field are filled with white spaces, by default. We can use the
fill() function to fill the unused positions by any desired character.
General syntax
10. Explain bit-fields and setf()?
The setf(), a member function of the ios class, can provide answers to these and
many other formatting questions. The setf() – set flags function
The arg1 is one of the formatting flags defined in the class ios. The formatting flag
specifies the format action required for the output. Another ios constant, arg2 known as
bitfield specifies the group to which the formatting flag belongs
11. What is trailing zeros and plus sign in formatted output?
would cause cout to display trailing zeros and trailing decimal point. Under default
precision, the value 3.24 will be displayed as 3.250000.
similarly, a plus sign can be printed before a positive number using the following
* * 5 2 5
5 2 1 * *
+ 2 7 5 . 0 0 0
12. What are flag in bit fields?
Flag Meaning
ios::showbase Use base indicator on output
ios::showpos Print + before positive numbers
ios::showprint Show trailing decimal point and zeroes
ios::uppercase Use uppercase letters for hex output
ios::skipus Skip white space on input
ios::unitbuf Flush all streams after insertion
ios::stdio Flush stdout and stderr after insertion
13. What are the manipulators in C++?
The header file iomanip provides a set of functions called manipulators
which can be used to manipulate the output formats. They provide the same
features as that of the ios member functions flags. Some manipulators are more
convenient to use than their counterparts in the class ios.
Manipulator Meaning Equivalent
setw(int w)
Set the field width to w.
Set the floating point
prevision to d
setfill(int c) Set the fill character to c. fill()
Set the format flag f setf()
Clear the flag specified by f unsetf()
14. Explain the file stream class in C++?
Class Contents
filebuf Its purpose is to set the file buffers to read and write. Contains
openprot constant used in the open() of file stream classes. Also
contain close() and open() as members
fstreambase Provides operations common to the file streams. Serves as a base for
fstream, ifstream and ofstream class. Contains open() and close()
ifstream Provides input operations. Contains open() with default input mode.
Inherits the functions get(), getline(),read(),seekg() and tellg()
functions from istream.
ostream Provides output operations. Contains open() with default output
mode. Inherits put(), seekp(), tellp() and write(), functions from
fstream Provides support for simultaneous input and output operations.
Contains open() with default input mode. Inherits all the functions
15. What are the file open modes?
ios::app Append to end-of-file
ios::ate Go to end-of-file on opening
ios::binary Binary file
ios::in Open file for reading only
ios::nocreate Open fails if the file does not exist
ios::noreplace Open files if the file already exists
ios::out Open file for writing only
ios::trunc Delete the contents of the file if its exits
16. What are the file manipulation function in C++?
seekg() – Moves get pointer (input ) to a specified location
seekp() – Moves put pointer (output) to a specified location
tellg() - Gives the current position of the get pointer
tellp() - Gives the current position of the put pointer
17. What are pointer offset calls in seekg() function?
fout is an ofstream object.
Seek call Action
fout.seekg(0,ios::beg); Go to start
fout.seekg(0,ios::cur); Stay at the current position
fout.seekg(0,ios::end); Go to the end of file
fout.seekg(m,ios::beg); Move to (m+1)the byte in the file
fout.seekg(m,ios::cur); Go forward by m byte form the current position
fout.seekg(-m,ios::cur); Go backward by m bytes from the current position
18. Explain Sequential input and output operations in File?
The file stream classes support a number of member of member functions for
performing the input and output operations on files. One pair functions, put() and get(),
are designed for handling a single character at a time. Another pair of functions, write()
and read() , are designed to write and read blocks of binary data.
19. Explain get() and put() function in Files?
The function put() writes a single character to the associated stream. Similarly, the
function get() reads a single character from the associated stream. On receiving the string,
the program writes it, character by character, to the file using the put() function in a for
loop. The program then displays the contents of the file on the screen. It uses the function
get() to fetch a character from the file and continues to do so until the end-of-file
condition is reached.
20. Explain read and write File functions?
The functions write() and read(), unlike the function put() and get(), handle the
data in binary form, this means that the values are stored in the disk file in the same
format in which they are stored in the internal memory . The binary format is more
accurate for storing the numbers as they are stored in the exact internal representation.
There are no conversions while saving the data and therefore saving is much faster.
infile.read((char *) &v , sizeof(v));
outfile.write ((char *) &v, sizeof(v));
21. What are the errors appear in file operation?
_ A file which we are attempting to open for reading does not exist.
_ The filename used for a new file may already exist
_ We may attempt an invalid operation such as reading past the end-of-file.
_ There may not be any space in the disk for storing more data
_ We may use an invalid file name.
_ We may attempt to perform an operation when the file is not opened for the purpose
22. What are the error handling functions in Files?
Function Return Value and meaning
eof() Returns true. If end-of-file is encountered while reading; otherwise return
fail() Returns true when an input or output operation has failed.
bad() Returns true if an invalid operation is attempted or any unrecoverable error
has occurred. However, if it is false, it may be possible to recover from any
other error reported, and continue operations.
good() Returns true if no error has occurred. This means, all the above functions are
false. For instance, if file. good() is true, all is well with the stream file and we
can proceed to perform I/O operations. When it returns false, no further
operations can be carried out.
23. Explain command line arguments?
The supply of arguments to the main() function. These arguments are supplied at
the time of invoking the program. They are typically used to pass the names of data file.
C: \> exam data results
Exam is the name of the file containing the program to be executed, and data and results
are the filenames passed to the program as command line arguments.
The main() functions which we have been using up to now without any arguments can
take two arguments as show below
main(int argc, char *argv[])
argu[0]- exam , argu[1]- data , argu[2] – results
24. What are the file pointers in C++?
Each file has two associated pointes known as the file pointers. One of them is
called the input pointer (or get pointer) and the other is called the output pointer (or put
pointer). We can use these pointers to move through the files while reading or writing.
The input pointer is used for reading the contents of a given file location and the output
pointer is used for writing to a given file location.
25. What are the types of communication in a program?
_ Data transfer between the console unit and the program.
_ Data transfer between the program and a disk file.