CBSE - MCQ Question Banks (के. मा. शि. बो . -प्रश्नमाला )

PreviousNext

Q. 190301 When an object of a derived class is created, the program first


A. calls the constructor for derived class, then the constructor for base class.

B. calls the destructor for derived class, then the constructor for base class.

C. calls the constructor for base class, then the destructor for derived class.

D. calls the constructor for base class ,then the constructor for derived class.

Right Answer is: D

SOLUTION

It makes sense, as the constructor for the derived class is build upon the data members of the base class.


Q. 190302 The inheritance, that is most appropriate to well designed object oriented systems is


A. private inheritance.

B. public inheritance.

C. protected inheritance.

D. unprotected inheritance.

Right Answer is: B

SOLUTION

Deriving publicly is a way of saying, “ is a type of “.


Q. 190303 The inheritance that does not reflect the object oriented design of the system is called


A. public inheritance.

B. protected inheritance.

C. private inheritance.

D. constructor inheritance.

Right Answer is: C

SOLUTION

It simply reflects an implementation shortcut.


Q. 190304 With privately derived class, the public and protected members of the base class become


A. public members of the derived class.

B. private members of the derived class.

C. protected members of the derived class.

D. unprotected members of the derived class.

Right Answer is: B

SOLUTION

In privately derived class, data can be accessed only through member functions of the derived class.


Q. 190305 If visibility mode is public, then protected members of base class becomes


A. private in derived class.

B. protected in derived class.

C. unprotected in derived class.

D. public in derived class.

Right Answer is: B

SOLUTION

Visibility mode specifies whether the features of the base class are privately derived, or publicly derived or protected derived.


Q. 190306 The derived class has access privilege to only


A. non private members of the base class.

B. private members of the base class.

C. public members of the derived class.

D. protected members of the base class.

Right Answer is: A

SOLUTION

The derived class cannot access the private members of its super class.


Q. 190307 The transitive nature of inheritance is reflected in


A. single inheritance.

B. multilevel inheritance.

C. multiple inheritance.

D. hierarchical inheritance.

Right Answer is: B

SOLUTION

Multilevel inheritance is, when a subclass inherits a base class that itself inherited from another base class.


Q. 190308 Combining two or more different form of inheritance, is called


A. multiple inheritance.

B. hybrid inheritance.

C. binary inheritance.

D. hierarchical inheritance.

Right Answer is: B

SOLUTION

Hybrid inheritance is the combination of different form of inheritance.


Q. 190309 When many subclasses inherit a single base class, it is known as


A. single inheritance.

B. multilevel inheritance.

C. multiple inheritance.

D. hierarchical inheritance.

Right Answer is: D

SOLUTION

In hierarchical inheritance many subclasses inherited from a single base class.


Q. 190310 The class which inherits the properties of another class is known as the


A. derived class.

B. super class.

C. base class.

D. inheritable class.

Right Answer is: A

SOLUTION

Derived classes (sub-classes) inherit the properties of a base class (super class).


Q. 190311 The most important advantage of inheritance is reusability. Reusing an existing code


A. increases readability.

B. increases time, money and efforts to use the code again.

C. increases reliability of a program.

D. increases writability.

Right Answer is: C

SOLUTION

Reusability is the feature using which we can use the already tested code again and again. It saves development time and easy to maintain.


Q. 190312 The functions that can be applied to objects of more than one type are called


A. double functions.

B. global functions.

C. polymorphic functions.

D. direct functions.

Right Answer is: C

SOLUTION

In C++, Polymorphic functions can be implemented using overloading functions and virtual functions.


Q. 190313 If we add some new features to the derived class, then


A. the base class also changes.

B. changes reflected in the base class only and derived class remains the same.

C. both the base class and derived class changes.

D. the derived class changes whereas base class remains unchanged.

Right Answer is: D

SOLUTION

A derived class inherits all the features of a base class, and can add new features to it. But, by adding new features to the derived class, base class remains the same.


Q. 190314 The stream class to which the file-to-memory link is related, is


A. fstream.

B. instream.

C. ofstream.

D. ifstream.

Right Answer is: D

SOLUTION

The ifstream function provides an interface to read data from files as input streams.


Q. 190315 The functions which are capable of handling single and multiple characters respectively, are


A. get( ) and getline( ).

B. read( ) and write( ).

C. get( ) and put( ).

D. getline( ) and get( ).

Right Answer is: A

SOLUTION

The function get( ) extracts a character from the stream and returns its value and the function getline( ) extracts more than one character from the stream.


Q. 190316 The function that returns the current error state is


A. clear().

B. int eof().

C. int fail().

D. rdstate().

Right Answer is: D

SOLUTION

The function rdstate() returns the current internal error state flags of the stream. The internal error state flags are automatically set by calls to input/output functions to signal certain types of error that happened during their execution.


Q. 190317 The functions which reads and writes data respectively, one byte at a time are


A. get( ) and put( ).

B. get( ) and getline( ).

C. seekg( ) and seekp( ).

D. tellg( ) and tellp( ).

Right Answer is: A

SOLUTION

The get( ) function extracts a character from the stream and returns its value . The put( ) function writes one character to the output stream.


Q. 190318 To append the data in a file, the file is opened in two modes. These modes are


A. output and ios :: app.

B. input and trunc.

C. output and binary.

D. input and ate.

Right Answer is: A

SOLUTION

Once the file gets opened in ios :: app mode, the previous records are retained and new data gets appended to the file.


Q. 190319 The file mode constant, which causes the open() function to fail if the file already exists is


A. ios :: nocreate.

B. ios :: app.

C. ios :: noreplace.

D. ios :: out.

Right Answer is: C

SOLUTION

ios :: noreplace causes the open( ) function to fail if the file already exists. This is used when one wants to create a new file at the same time.


Q. 190320 The base class for fstream, ifstream and ofstream is


A. filebuf.

B. ostreambase.

C. istreambase.

D. fstreambase.

Right Answer is: D

SOLUTION

fstreambase provides operations common to fstream, ifstream and ofstream file streams. It also contains open() and close() functions.


Q. 190321 The function that sets floating-point precision and returns previous precision is


A. int ios::precision( ).

B. int ios::precision(int n).

C. int ios::precision(char n).

D. int ios::previousprecision(n).

Right Answer is: B

SOLUTION

int ios::precision(int n) sets the floating-point precision to 'n'. and returns the previous precision.


Q. 190322 The flag that is used to ignore a whitespace is


A. showbase.

B. showpos.

C. unitbuf.

D. skipws.

Right Answer is: D

SOLUTION

When the skipws format flag is set, as many whitespace characters as necessary are read and discarded from the stream until a non-whitespace character is found before every extraction operation. Tab spaces, carriage returns and blank spaces are all considered whitespaces.


Q. 190323 The file opening mode, which opens the file for appending, is


A. ios::app.

B. ios:: binary.

C. ios::nocreate.

D. ios::noreplace.

Right Answer is: A

SOLUTION

With ios::app, every output is appended at the end of the file.


Q. 190324 The technique that reduces the number of I/O call is known as


A. polymorphism.

B. inheritance.

C. buffering.

D. encapsulation.

Right Answer is: C

SOLUTION

Typically, the data is stored in a buffer as it is retrieved from an input device (such as a keyboard) or just before it is sent to an output device (such as a printer).


Q. 190325 The file opening mode, which seeks to end of file on opening it, is


A. ios::ate.

B. ios::app.

C. ios::nocreate.

D. ios::in.

Right Answer is: A

SOLUTION

The file ios:ate helps to go to the end of the file when opened.


Q. 190326 The file in which the records are written sequentially is known as


A. serial file.

B. parallel file.

C. sequential file.

D. temporary file.

Right Answer is: C

SOLUTION

A sequential file is designed for efficient processing of records in a sorted order on some search key.


Q. 190327 The file-opening mode that discards the content, if the file exists is


A. ios::app.

B. ios::ate.

C. ios::trunc.

D. ios::nocreate.

Right Answer is: C

SOLUTION

In ios::trunc, if the file does not exist, a new empty file is created. If the file exists, its content is deleted and it is treated as a new file.


Q. 190328 A file in which the records are organized in no specific order is known as


A. sequential file.

B. serial file.

C. temporary file.

D. permanent file.

Right Answer is: B

SOLUTION

A serial file is one in which the records are stored in the order in which they occur. They are not sorted into any particular order.


Q. 190329 C++ statement to go to the 20th byte in a file is


A. fileobject.seekg(20,ios:: end);

B. fileobject.seekg(20,ios:: beg);

C. fileobject.ofstream(20,ios:: end);

D. fileobject.ifstream(20,ios:: end);

Right Answer is: B

SOLUTION

seekg( ) sets the position of the get pointer at the beginning of the file. 20 indicates the position of the byte in a file.


Q. 190330 C++ statement to go to the beginning of the file at anytime will be written as


A. fileobject.seekg(0,ios::beg);

B. ofstream afile(0,ios::beg);

C. ifstream(0,ios::beg);

D. fstream(0,ios::beg);

Right Answer is: A

SOLUTION

seekg( ) sets the position of the get pointer at the beginning of the file.


Q. 190331 C++ statement to open a file named EMP.DAT for output should be


A. ifstream afile(“ EMP.DAT ”);

B. ofstream afile(“ EMP.DAT ”);

C. fstream afile(“ EMP.DAT ”);

D. open afile(“ EMP.DAT ”);

Right Answer is: B

SOLUTION

ofstream provides an interface to write data to files as output streams.


Q. 190332 The function which returns nonzero if no error has occurred is known as


A. int good( ).

B. int fail( ).

C. int eof( ).

D. clear( ).

Right Answer is: A

SOLUTION

The function returns true if none of the stream's error flags are set. This function is not exactly opposite of bad( ), which only checks whether the bad bit error flag is set.


Q. 190333 The function which returns nonzero when an input or output operation has failed is known as


A. int bad( ).

B. int eof( ).

C. int fail( ).

D. int good( ).

Right Answer is: C

SOLUTION

The fail() function returns true if an error has occurred with the current stream, otherwise false . This can be used for checking whether the previous operation has failed.


Q. 190334 Creating pointer of an abstract class is


A. legal in C++ only.

B. illegal in C++.

C. not possible either in C++ or C.

D. valid in C and C++.

Right Answer is: D

SOLUTION

Even though, we cannot create an object of an abstract class, but we can create a pointer to an abstract class.


Q. 190335 When a function call gets resolved at compile time, it is called


A. dynamic binding.

B. static binding.

C. late binding.

D. null binding.

Right Answer is: B

SOLUTION

The binding which is done at the compile time is called static binding or early binding.


Q. 190336 It is possible to derive a class through


A. global derivation.

B. final derivation.

C. initial derivation.

D. public, private and protected derivation.

Right Answer is: D

SOLUTION

A class can be derived publicly, privately or protectedly. All three types of inheritance are possible.


Q. 190337 If the base class source code is not available, then


A. we cannot derive a class from the base class.

B. we can derive a class from the base class.

C. we can partially derive a class from the base class.

D. first we have to find the source code, then only the class can be derived.

Right Answer is: B

SOLUTION

We just want the base class declaration, to derive a class from a base class. The declaration is generally present in the header files.


Q. 190338 A derived class constructor can


A. only accept arguments for itself.

B. only accept arguments for its base class constructor.

C. accept arguments for itself as well as for its base class constructor.

D. not accept any arguments.

Right Answer is: C

SOLUTION

A derived class constructor can invoke base class constructor by passing appropriate arguments that it received for a base class constructor.


Q. 190339 Once we derive a class, we


A. can add new members to it.

B. can not add new members to it.

C. have to add new members to it, otherwise the class can not be accessed.

D. have to redefine the base class variables and members.

Right Answer is: A

SOLUTION

When we derive a class, we may or may not add new members to it. But to construct new members, new constructor (s) must be written for the derived class.


Q. 190340 A derived class can inherit an indirect base class


A. only once.

B. only twice.

C. more than once.

D. which is illegal in C++.

Right Answer is: C

SOLUTION

It is valid when a derived class inherits an indirect base class more than once.


Q. 190341 When a class is derived privately from a base class, all protected base class members become


A. public members of the derived class.

B. private members of the derived class.

C. protected members of the derived class.

D. global members.

Right Answer is: B

SOLUTION

With privatly derived class, the protected members of the base class become private members of the derived class.


Q. 190342 The functions that can be applied to objects of more than one type are called


A. double functions.

B. global functions.

C. polymorphic functions.

D. direct functions.

Right Answer is: C

SOLUTION

Polymorphic functions can be implemented using overloading functions and virtual functions.


Q. 190343 A class “computer science” is derived from class "subjects", which is derived from class "studies", which is derived from class "school". This is known as


A. multiple levels of inheritance.

B. single inheritance.

C. hierarchical inheritance.

D. multiple inheritance.

Right Answer is: A

SOLUTION

When a subclass inherited from a class which, itself inherited from another class, this is known as multilevel inheritance.


Q. 190344 When a derived class and its base class have common ancestor, then ambiguity may arise as the derived class contains multiple copies of common ancestor. This can be resolved


A. only by using scope resolution operator.

B. only using virtual.

C. by either using virtual or scope resolution operator.

D. by nesting classes.

Right Answer is: C

SOLUTION

We can declare common ancestor as virtual, or use ::,i.e.,scope resolution operator.


Q. 190345 In C++ programs, when an object is first created the special member function which is automatically called is


A. destructor.

B. constructor.

C. malloc().

D. calloc().

Right Answer is: B

SOLUTION

Constructor is a special member function, that allows us to set up values while defining the object, without the need to make a separate call to a member function.


Q. 190346 The correct statement is


A. The private members of the base class are visible in the derived class but they are not directly accessible.

B. The private members of the base class are not visible in the derived class and not accessible.

C. The private members of the base class are not visible in the derived class but they are directly accessible.

D. The private members of the base class are visible and directly accessible in the derived class.

Right Answer is: A

SOLUTION

Private members of a base class are not directly accessible in the base class but this does not affects their visibility.


Q. 190347 Similar relation between two classes is known as


A. IS-
A.

B. HOLDS-
A.

C. HAS-
A.

D. HAVE-
A.

Right Answer is: B

SOLUTION

A similar relation between two classes is known as HOLDS-A relation. It is similar to HAS-A relationship but ownership is missing in HOLDS-A relation.


Q. 190348 When the derived class wants to use some attributes of the base class and these inherited features can’t be inherited further, then we should use


A. private derivation.

B. public derivation.

C. protected derivation.

D. unprotected derivation.

Right Answer is: A

SOLUTION

Private inheritance should be used when the derived classes cannot be viewed as a type of their base class.


Q. 190349 When the features are required to be hidden from the outside world and at the same time, required to be inheritable then we should


A. derive publicly.

B. derive protectedly.

C. derive  privately.

D. not use inheritance.

Right Answer is: B

SOLUTION

Protected inheritance should be used, when the situation requires the attributes of base class to be available only to the derived class members but not to the outside world.


Q. 190350 Which object of class will be constructed first in nesting of objects ?


A. contained class object.

B. enclosed class object.

C. Super class object.

D. Derived class object.

Right Answer is: A

SOLUTION

A class may contain objects of another class. This situation is called nesting of objects and in such situation, the contained objects are constructed first before constructing the objects of the enclosing class.


Q. 190351 The visibility mode, which means that derived class can access the public and protected members of base class but not the private members of the base class is


A. private visibility mode.

B. protected visibility mode.

C. public visibility mode.

D. unprotected visibility mode.

Right Answer is: C

SOLUTION

With publicly derived class, public members becomes the public members of derived class and protected becomes protected members of derived class.


Q. 190352 The derivation that does not changes the access specifiers for inherited members in the derived class is


A. public derivation.

B. protected derivation.

C. private derivation.

D. unprotected derivation.

Right Answer is: A

SOLUTION

In public derivation, the inherited members need not be re-defined in the derived class.


Q. 190353 The mode that controls the availability of inherited base class members in the derived class is called as


A. on mode.

B. off mode.

C. visibility mode.

D. access specifiers.

Right Answer is: C

SOLUTION

There are three visibility modes: private, protected, and public.


Q. 190354 The building block of a file is


A. field.

B. record.

C. attribute.

D. token.

Right Answer is: A

SOLUTION

Field is the smallest unit of a file. Field is characterized by its size, length and type.


Q. 190355 The error in the following line cin.get().get(); is


A. the syntax of cin is wrong.

B. undefined symbol infile.

C. integer array cannot read getl() function.

D. get( ) cannot return the reference twice.

Right Answer is: D

SOLUTION

We cannot use two get() simultaneously in cin. get() function returns a reference to istream. It cannot return the reference twice.


Q. 190356 An appropriate C++ statement to open a file named EMP.DAT is


A. fileobject.seekg(“EMP.DAT”);

B. ofstream afile(“EMP.DAT”);

C. instream seekg(“EMP.DAT”);

D. instream afile(“EMP.DAT”);

Right Answer is: B

SOLUTION

ofstream is used for output to a file. For an ofstream object, the file open mode can be either ios::out to output data to a file or ios::app to append data to the end of file.


Q. 190357 An appropriate statement, in C++, to access the beginning of the file is


A. fileobject.seekg(ios::beg);

B. fileobject.seekg(1,ios::beg);

C. fileobject.seekg(0,ios::beg);

D. fileobject.seekg(ios::begin);

Right Answer is: C

SOLUTION

seekg() function sets the position of the get pointer. The get pointer determines the next location to be read in the source associated to the stream.


Q. 190358 An appropriate statement, in C++, to access the 20th byte in a file is


A. fileobject.seekg(ios::beg);

B. fileobject.seekg(20,ios::beg);

C. fileobject.seekg(21,ios::beg);

D. fileobject.seekg(ios::begin);

Right Answer is: B

SOLUTION

seekg( ) function sets the position of the get pointer. The get pointer determines the next location to be read in the source associated to the stream.


Q. 190359 A sequence of bytes is known as


A. data.

B. waterfall.

C. stream.

D. class.

Right Answer is: C

SOLUTION

A stream is a general name given to a flow of data. Different streams are used to represent different kinds of data flow.


Q. 190360 A key field is used to


A. verify the record.

B. check if the record is present or not.

C. identify the record.

D. check the number of attributes in a field.

Right Answer is: C

SOLUTION

A key field is a field or set of fields of a database (typically a relational database) table, which together form a unique identifier for a database record (a table entry).


Q. 190361 The number of stream objects required to merge two sorted files into a third file is


A. three.

B. four.

C. five.

D. two.

Right Answer is: A

SOLUTION

When two sorted files are merged into a third file, three stream objects are required.


Q. 190362 The function which opens a file and attaches it to an existing ofstream object is


A. openfil().

B. openfile().

C. openfl().

D. open().

Right Answer is: D

SOLUTION

open() is ofstream member function which opens the file and attaches the file to the ofstream object.


Q. 190363 The two methods in which the file can be opened are


A. constructor and destructor.

B. constructor and read() function.

C. constructor and write() function.

D. constructor and open() function.

Right Answer is: D

SOLUTION

A file can be opened in C++ by two methods: (a) By using the constructor of the stream class (b) By using the open() function of the stream class


Q. 190364 The function of ios::ate is


A. open the file for appending.

B. seek to end of file on opening it.

C. discard contents if file exists.

D. if file doesn’t exist, open fails.

Right Answer is: B

SOLUTION

ios::ate seeks to end-of-file upon opening of the file. I/O operations can still occur anywhere within the file.


Q. 190365 The number of ways in which a file can be opened in C++ is


A. two.

B. three.

C. four.

D. five.

Right Answer is: A

SOLUTION

A file can be opened in C++ in two ways: (a)By using the constructor of the stream class (b)By using the open() function of the stream class


Q. 190366 The error in the following code is in
         int buf[100], n = 10;           cin.getline)( buf, n, ‘t’);


A. int.

B. buf.

C. cin.

D. getline.

Right Answer is: D

SOLUTION

The above statement is invalid as integer array cannot be read using getline() function.
The prototype of getline() function is - istream & getline(char * buf, int num, char delim ='n'); The getline() function reads characters from input stream and puts them in the array pointed to by buf until either num characters have been read, or the character specified by delim is encountered.


Q. 190367 The error in the following code is in
           char  ch;            cin.get(ch).get(ch);


A. char.

B. cin.

C. get.

D. ch.

Right Answer is: C

SOLUTION

We cannot use two get() simultaneously in cin. The get() returns a reference to istream. It cannot return the reference twice.


Q. 190368 The ostream member function that outputs a fixed number of bytes beginning with a specific location in memory, is


A. open().

B. read().

C. write().

D. tellp().

Right Answer is: C

SOLUTION

The prototype of write function is - ostream& write ( const char* s , streamsize n ); It writes the block of data pointed by s, with a size of n characters, into the output buffer. The characters are written sequentially until n have been written.

This is an unformatted output function and what is written is not necessarily a c-string, therefore any null-character found in the array s is copied to the destination and does not end the writing process. 


Q. 190369 The ostream member function which inputs a fixed number of bytes from the specified stream to an area in memory beginning at a specified address, is


A. open().

B. read().

C. write().

D. tellp().

Right Answer is: B

SOLUTION

The prototype of read function is - istream& read ( char* s, streamsize n ); It reads a block of data of n characters and stores it in the array pointed by s.


Q. 190370 To write class objects, we use


A. put()

B. read()

C. write()

D. getline()

Right Answer is: C

SOLUTION

The functions read() and write() are used for reading and writing class objects. These functions handle the entire structure of an object as a single unit, using the computer's internal representation of data.


Q. 190371 The base class of ofstream and ifstream is


A. fstream.

B. streambuf.

C. filebase.

D. fstreambase.

Right Answer is: D

SOLUTION

fstreambase class provides a set of common functions.


Q. 190372 How many file stream objects would be needed to merge two files into a third one?


A. one

B. two

C. three

D. none

Right Answer is: C

SOLUTION

Two for reading and the third for writing.


Q. 190373 To modify the data in a file, open it in


A. ios::in file mode

B. ios::binary file mode

C. ios::noreplace file mode

D. ios::ate file mode

Right Answer is: D

SOLUTION

The ios::ate file mode seeks to end-of-file upon opening of the file. I/O operations can still occur anywhere within the file.


Q. 190374 Out of the following statements, the incorrect statement is


A. By default, in C++, files are considered to be binary files.

B. A file can be opened using the constructor of the stream class.

C. To close a file, we disconnect it from the stream.

D. After disconnecting the file, the stream still exists.

Right Answer is: A

SOLUTION

The data files are the files that store data pertaining to a specific application for later use. The data files can be stored in two ways: text files and binary files. When we work with files in C++, unless we specify, files are by default considered and treated as text files.


Q. 190375 The function eof() 


A. is used with the filename.

B. is generally used with output mode.  

C. returns value zero at end of file.

D. returns non zero value at end of file.

Right Answer is: D

SOLUTION

The function eof() is used to detect when the end of file is reached. The prototype is : int eof(); It returns nonzero when the end of file has been reached, otherwise it returns zero.


Q. 190376 The function get()


A. removes the delimiting character from the stream.

B. does not remove the delimiting character from the stream.

C. can have only 'n' as delimiting character.

D. is used to read binary files.

Right Answer is: B

SOLUTION

The get ()does not extract the delimiter character from the input stream.


Q. 190377 To read blocks of binary data, use


A. get().

B. getline().

C. read().

D. input().

Right Answer is: C

SOLUTION

The function read() is used to read blocks of binary data.This function handles the entire structure of an object as a single unit, using the computer's internal representation of data.


Q. 190378 With file mode ios::app


A. we can write anywhere in the file.

B. previous data is discarded.

C. the file can only be read.

D. we can write only at the end of the file.

Right Answer is: D

SOLUTION

The ios::app file mode causes all output to that file to be appended to the end. This value can be used only with files capable of output.


Q. 190379 File mode ios::nocreate causes


A. a new file,not to be created, if the file exists.

B. the file, not to be created as binary file.

C. open() function to fail.

D. all output to that file to be appended to the end.

Right Answer is: D

SOLUTION

ios::nocreate constant causes the open() function to fail if the file does not already exist. It will not create a new file with that name.


Q. 190380 In text files,


A. there is no character conversion.

B. a character can have one of 256 possible values.

C. data is stored as it is in the memory.

D. characters are stored in ASCII format.

Right Answer is: D

SOLUTION

A text file stores information in ASCII characters. In text files, each line of text is terminated(delimited) with a special character known as EOL(End Of Line) character. In text files, some internal translations take place when this EOL character is read or written.


Q. 190381 The link Memory-to-File is used for


A. output purposes.

B. input purposes .

C. input/output purposes.

D. close purposes.

Right Answer is: A

SOLUTION

If the data(after some processing) is to be sent from memory to file, then the link can be said to be Memory-to-File link. So it is used for output purposes. 


Q. 190382 The default file mode with open() using ofstream is


A. ios::app.

B. ios::out.

C. ios::ate.

D. ios::binary.

Right Answer is: B

SOLUTION

The ifstream open() method and constructor use ios :: in (open for reading) as the default value for the mode argument, while the ofstream open() method and constructor use ios :: out (open for writing) as the default.


Q. 190383 The header file needed if file I/O is involved is


A. iostream.h.

B. fileio.h.

C. fstream.h.

D. ifstream.h.

Right Answer is: C

SOLUTION

The classes defined inside fstream.h derive from classes under iostream.h.


Q. 190384 Classes ifstream, ofstream and fstream inherit from


A. fstreambase.

B. ostream.

C. filebug.

D. streambase.

Right Answer is: A

SOLUTION

The file I/O system of C++ contains a set of classes that define the file handling methods. These classes, designed to manage the disk files, are declared in fstream.h. Therefore, we must include this file in a program that works with files.


Q. 190385 To close a file


A. use a file mode constant.

B. open another stream.

C. disconnect it from the stream.

D. open it in text mode

Right Answer is: C

SOLUTION

A file is closed by disconnecting it with the stream it is associated with. The close() function accomplishes this task and takes the following general form:

stream_object.close();


Q. 190386 The classes used for file I/O are


A. ifstream, ofstream, iofstream.

B. ifstream, ofstream, fstream.

C. istream, ostream, fstream.

D. istream, ostream, iostream.

Right Answer is: B

SOLUTION

The classes I/O system of C++ contains a set of classes that define the file handling methods. These classes, designed to manage the disk files, are declared in fstream.h.  Therefore, we must include this file in a program that works with files. To create an input stream, you must declare the stream to be of class ifstream. To create an output stream, you must declare it as class ofstream. Streams that will perform both input and output operations must be declared as class fstream.


Q. 190387 Function that flushes the buffer before terminating the connection of the file with the stream is


A. buffer().

B. flush()

C. end().

D. close(). 

Right Answer is: D

SOLUTION

A file is closed by disconnecting it with the stream it is associated with. The close() function accomplishes this task and it takes the following general form :     stream_object.close(); close() function flushes the buffer before terminating the connection of the file with the stream.


Q. 190388 An output stream


A. is like an interface between the program and the file.

B. provides data from the file into the program.

C. opens the file, by default, to take data.

D. cannot be used for sequential operations.

Right Answer is: A

SOLUTION

The stream that supplies data to the program is known as input stream. It reads the data from the file and hands it over to the program. The stream that receives data from the program is known as output stream.


Q. 190389 An input stream


A. writes data into the file.

B. reads data from the file.

C. reads bytes randomly.

D. exists only for binary files.

Right Answer is: B

SOLUTION

The stream that supplies data to the program is known as input stream. It reads the data from the file and hands it over to the program.


Q. 190390 The number of file stream objects needed to merge two files into a third one is/are


A. one

B. two

C. three

D. none

Right Answer is: C

SOLUTION

Two objects are needed for reading the two files and third object is needed to write to the third file.


Q. 190391 To modify the data in a file, open it in


A. ios::in file mode.

B. ios::binary file mode.

C. ios::noreplace file mode.

D. ios::ate file mode.

Right Answer is: D

SOLUTION

This function seeks to end-of-file upon opening of the file.


Q. 190392 To write an object to a file, we would use


A. put().

B. read().

C. write().

D. getline().

Right Answer is: C

SOLUTION

This function handles the entire structure of an object as a single unit, using the computer's internal representation of data.


Q. 190393 The classes used for file I/O are


A. ifstream, ofstream, iofstream.

B. ifstream, ofstream, fstream.

C. istream, ostream, fstream.

D. istream, ostream, iostream.

Right Answer is: B

SOLUTION

All C++ compilers come with classes for streaming input from the console and output to the console. These classes are defined by putting the directive #include at the top of the code. The istream class has methods for detecting input errors and the end of input data. The ostream class has methods for formatting output, i.e. specifying scientific notation, fixed decimal notation, or a combination thereof and for specifying the number of decimal digits displayed. Using some of the features of these classes, we add the capability of reading and writing our own custom types. Finally, the ifstream and ofstream classes let us read from and write to named files.


Q. 190394 How is get() different from read()?
Right Answer is:

SOLUTION

Function get() reads one character at a time but read() can read chunks like structures and objects at a time.


Q. 190395 Describe two member functions from fstream class.
Right Answer is:

SOLUTION

Some member functions of fstream class are seekp(), seekg(), tellg() and tellp().  Functions seekg() and tellg() are used with the input stream.  Common forms of these functions are –

 

            a)   ifstream & seekg(long);

            b)   ifstream & seekg(long, pos)  ;

            c)   long tellg();

 

In form a) seekg() positions the get pointer at an absolute position given by the long integer.

In form b) seekg() positions the get pointer at a position given by the long integer and by the value of pos.

Position can be -        ios :: beg                // from beginning of file

                                          ios :: cur                // from current position

                                          ios :: end               // from the end

 

In form c) tellg() gives the byte position of the get pointer in the file as a long integer. 


Q. 190396 How is the file mode ios::ate different from ios::app?
Right Answer is:

SOLUTION

ios::ate - This seeks to end-of-file upon opening of the file. I/O operations can still occur anywhere within the file.
ios::app - This causes all output to that file to be appended to the end. This value can be used only with files capable of output.


Q. 190397 How is get() different from getline() ?
Right Answer is:

SOLUTION

Both get() and getline() can read characters from the input stream into a character array till the specified number of characters are entered or till the delimiting character is encountered( ‘n’ by default). The major difference is that get() does not extract delimiter newline character from the input stream. On the other hand, getline() does extract the delimiter newline character from input stream so that stream type is empty after getline() is over.


Q. 190398 How are text files different from binary files?
Right Answer is:

SOLUTION

Text files are also called ASCII files.  They store information in the form of ASCII characters. There are 128 different ASCII codes.  Also, text files contain lines of text and each of these has an end-of-line (EOL) marker automatically appended whenever you indicate that you have reached the end of a line. When a file is opened in text mode, various character translations take place. In case of binary files, information is stored in the same format in which it is stored in the memory.  No character translation takes place when a binary file is opened,  because of which binary files can be read and written faster by the computer.


Q. 190399 Consider class Employee with data members empcode, empname and depart. Define necessary functions. Accept the filename and display all the data in it.
Right Answer is:

SOLUTION

#include < fstream.h>

class Employee{

int empcode;

char empname[20];

char depart[20];

public:

void getdat(){

cout<<"nEmployee Code : ";

cin>>empcode;

char c=cin.get();

cout<<"nEmployee Name : ";

cin.getline(empname,20);

cout<<"nDepartment : ";

cin.getline(depart,20);

}

void dispdat(){

cout<<"nEmployee Code : "<< empcode;

cout<<"nEmployee Name : "<< empname;

cout<<"nDepartment : "<< depart;

}

};

void main(){

Employee e;

int num=0;

char fname[13];

cout<<"Enter file name ";

cin>>fname;

ifstream ifl;

ifl.open(fname, ios::in|ios::binary);

if(!ifl){

cout<<"Cannot open file "<< fname;

return;

}

while(ifl) {

ifl.read((char*)&e,sizeof(Employee));

e.dispdat();

}

ifl.close();

}


Q. 190400 Write a program to create a class Book that stores the book code and name. Define functions necessary to accept data, display data and check for matching. The file name is Books.Dat. Display all the data in the file.
Right Answer is:

SOLUTION

#include < fstream.h >

#include < string.h >

class Book
{

int bookcode;

char bookname[20];

public:

void getdat()
{

cout<<"nBook Code : ";

cin>>bookcode;

char c=cin.get();

cout<<"nBook Name : ";

cin.getline(bookname,20);

}

void dispdat()
{

cout<<"nBook Code : "<< bookcode;

cout<<"nBook Name : "<< bookname;

}

int match(int cd)
{

if(cd==bookcode)

return 1;

else

return 0;

}

};

void main()
{

Book b;

ifstream ifl;

ifl.open("Books.dat",ios::in|ios::binary);

if(!ifl)
{

cout<<"Unable to open file - Returning";

return;}

while(ifl)
{

ifl.read((char *)&b,sizeof(Book));

b.dispdat();

}

}


PreviousNext