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

PreviousNext

Q. 190401 Write a program that opens a file named CLASS.DAT and displays the number of characters in it.
Right Answer is:

SOLUTION

#include < fstream.h >

void main()
{

  int num=0;

  char c;

  ifstream ifl;

  ifl.open("CLASS.DAT",ios::in);

  while(ifl)
  {

    ifl.get(c);

    cout<< c;

    num++;

    cout<< num;

  }

  ifl.close();

  cout<<"Total characters = "<< num;

         }


Q. 190402 Given a structure Member, as below,  open the file MEMLIST.DAT that already stores data of Member,  and add a record at the end. 
struct Member
{    int code;
      char name[20];
      int age;
      char sex;
};  
Right Answer is:

SOLUTION

#include < fstream.h >

struct Member
{

    int code;

    char name[20];

    int age;

    char sex;

};

void main()
{

  Member m;

  char c;

  ofstream ofl;

  ofl.open("MEMLIST.DAT", ios::app|ios::binary);

  cout<<"nMember Code : ";

  cin>>m.code;

  c=cin.get();

  cout<<"nMember Name : ";

  cin.getline(m.name,20);

  cout<<"nAge : ";

  cin>>m.age;

  c=cin.get();

  cout<<"nMember Sex (M/F) : ";

  cin>>m.sex;

  ofl.write((char*)&m,sizeof(Member));

  ofl.close();

         }       


Q. 190403 Describe the different file opening modes, in brief.
Right Answer is:

SOLUTION

File mode describes how the file is to be used.  When we open a file using function open(), along with the filename we can also give the file mode.  The general form of the function open() with two arguments is –

           stream_object. open(“filename”, filemode);

The different file modes are –

ios :: in               opens file for reading, i.e., in input mode

ios :: out             opens file for writing, i.e., in output mode

ios :: app             causes all output to that file to be appended to the end

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

ios :: trunk           deletes the contents of the file

ios :: binary          opens a file in binary mode. Default is text mode.

ios :: nocreate      causes the open() function to fail if file does not already exist.

ios :: noreplace    causes the open() function to fail if already exists.

 

If the file mode is not given, the defaults are assumed.  If ifstream is used, the default mode is ios :: in.  If  ofsream is used,  the default mode is  ios :: out. The fstream does not provide a mode by default and so we have to specify the mode when we use the function open ().  File modes can be combined using the bitwise Or operator (the | symbol).     


Q. 190404 Describe the streams used for input and output with a file.
Right Answer is:

SOLUTION

There are 3 kinds of streams – input, output and input/output. 

If we want to use the file for input i.e.  reading, we must create an input stream, using class ifstream.  
    For example,  ifstream ifl;

If we want to use the file for output i.e. writing, we must create an output stream, using class ofstream. 

    For example,  ofstream ofl;

If we want to use the file for input as well as output, we must create a stream   performing both input and output, using class fstream. 

    For example,  fstream iofl;


Q. 190405 Consider class Employee with data members empcode, empname and depart.  Define necessary functions to accept and display data.  Write a program to accept data for different employees and write to the file whose name is provided by the user.
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;

  ofstream ofl;

  ofl.open(fname, ios::app|ios::binary);

  if(!ofl)
  {

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

      return;

  }

  cout<<"How many employees? ";

  cin>>num;

  for(int i=1;i<=num;i++)

  {

    e.getdat();

    ofl.write((char*)&e,sizeof(Employee));

  }

  ofl.close();

              }                                              


Q. 190406 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. Open the data file Books.Dat and delete the record where book code matches the entered code.
Right Answer is:

SOLUTION

          #include < fstream.h >
          #include < stdio.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;

 int cd;

 int found=0;

 ofstream ofl;

 ofl.open("temp.tmp", ios::out|ios::binary);

 ifstream ifl;

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

 cout<<"nEnter Book Code ";

 cin>>cd;

 while(ifl)
 {

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

  found=b.match(cd);

  if(found==0)

    ofl.write((char *)&b,sizeof(Book));

 }

 ifl.close();

 ofl.close();

 remove("Books.Dat");

 rename("temp.tmp", "Books.Dat");

                }                                    


Q. 190407 Write a program to use the structure Member, as below, open the file MEMLIST.DAT that already stores data of Member and display the details of a member whose code is given.        struct Member
{     int code;     char name[20];     int age;     char sex; };  
Right Answer is:

SOLUTION

#include < fstream.h >
#include < string.h >

struct Member
{    int code;
      char name[20];

      int age;

      char sex;

};

void main()
{

  Member m;

  char c;

  int cd;

  ifstream ifl;

  ifl.open("MEMLIST.DAT", ios::in|ios::binary);

  cout<<"Enter code to find ";

  cin>>cd;

  c=cin.get();

  while(ifl)
  {

    ifl.read((char *)&m,sizeof(Member));

    if(cd==m.code)
    {

     cout<<"nMember Code : "<< m.code;

     cout<<"nMember Name : "<< m.name;

     cout<<"nAge : "<< m.age;

     cout<<"nMember Sex : "<< m.sex;

     break;

     }

 }

 ifl.close();

             }                                              


Q. 190408 Name the two file pointers and how can we manipulate them?
Right Answer is:

SOLUTION

The two file pointers are the get pointer and the put pointer.  The get pointer is used with a file that is open for input and put pointer is used with a file open for output.  A file is read from the position marked by the get pointer. In the same way, when a file is written, it is at the position marked by the put pointer.

These two pointers can be manipulated to move directly to a location in the file to read or write.  The functions used for this are seekg() and seekp().  Two more functions may be useful here, tellg() and tellp().  Function tellg() gives the position of the get pointer in the file and tellp() gives the position of the put pointer in the file.

 

 Suppose the file ‘TEST.DAT’ is open for input, linked with the input stream ifl.

 

ifl.seekg(50);              //will move the get pointer to byte number 50 in the file.

ifl.seekg(25, ios::beg); //will move the get pointer to the 25th byte from the beginning of the file

ifl.seekg(-5, ios::end); //will move the get pointer back 5 bytes from the end of the file

ifl.seekg(12, ios::cur);  //will move the get pointer to the 12th  byte from the current position 

long p=ifl.tellg();         // store position of get pointer in p

ifl.seekg(p,ios::beg);   //move get pointer p bytes from the beginning

 

Suppose the file ‘TEST.DAT’ is now open for output, linked with the output stream ofl.

 

ofl.seekp(50);                 //will move the put pointer to byte number 50 in the file.

ofl.seekp(25, ios::beg);    //will move the put pointer to the 25th  byte from the beginning of the file

ofl.seekp(-5, ios::end);    //will move the put pointer back 5 bytes from the end of file

ofl.seekp(12, ios::cur);    //will move the put pointer to the 12th  byte from the current position 

 long p=ofl.tellp();          //store position of put pointer in p

ofl.seekp(p,ios::beg);     //move put pointer p bytes from the beginning


Q. 190409 The arrow (->) operator is used to access


A. the private members of the class with a pointer to an object.

B. the protected members of the class with a pointer to an object..

C. the public members of the class with a pointer to an object.

D. both private and public members of the class with a pointer to an object.

Right Answer is: C

SOLUTION

A pointer can point to only public members of the class. The -> (arrow) operator is used to access class, structure or union members using a pointer. A postfix expression, followed by an -> (arrow) operator, followed by a possibly qualified identifier or a pseudo-destructor name, designates a member of the object to which the pointer points.


Q. 190410 When accessing members of a class using an object pointer, the


A. (.) operator is used.

B. (->) operator is used.

C. (*) operator is used.

D. (&) operator is used.

Right Answer is: B

SOLUTION

C++ allows us to have pointers to objects. The arrow operator (->) is used to access members of class using object pointer.


Q. 190411 A structure having a member element that refers to the structure itself, is called


A. self structure

B. base structure.

C. self-referential structure.

D. recursive structure.

Right Answer is: C

SOLUTION

Self-reference occurs when a system loops back on itself and describes some aspect of its own form or structure.


Q. 190412 When parameters are passed to the functions by reference, then the


A. formal parameters become aliases to the actual parameters.

B. actual parameters become aliases to the formal parameters.

C. formal parameters and actual parameters are same.

D. formal parameters and actual parameters are different.

Right Answer is: A

SOLUTION

The called function does not create its own copy of original values.


Q. 190413 The alias name for a variable is called


A. pointer.

B. typedef.

C. reference.

D. identification.

Right Answer is: C

SOLUTION

No separate memory is allocated for references.


Q. 190414 The two member functions of ofstream class are


A. open and rdbuf.

B. put and get.

C. set and reset.

D. seekg and seekp.

Right Answer is: A

SOLUTION

ofstream class is derived from ostream class. It is used to write data onto a file. The member functions of ofstream class are open and rdbuf.


Q. 190415 In text files,


A. there is one character conversion format.

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

Text files are a sequence of bytes broken into sub-sequences with a special EOL character.It stores information in ASCII characters.


Q. 190416 While using file mode ios::out


A. existing contents of the file will be discarded.

B. error message displayed if file does not exist.

C. error message displayed if the file has data.

D. file pointer is at the end of the file.

Right Answer is: A

SOLUTION

By default, this opens file in ios::trunc mode. This means an existing file is truncated when opened.


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


A. ios::app

B. ios::out

C. ios::ate

D. ios::binary

Right Answer is: B

SOLUTION

It opens file for writing, i.e., in output mode. This also opens the file in ios::trunc mode by default. This means an existing file is truncated when opened, i.e., its previous contents are discarded.


Q. 190418 The header file needed, if the file I/O is involved is


A. iostream.h.

B. fileio.h.

C. fstream.h.

D. ifstream.h.

Right Answer is: C

SOLUTION

One must provide the mode explicitly when using an object of fstream class.


Q. 190419 Out of the following, 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

Files are stored in two ways: Binary files and text files. A binary file is just a file that contains information in the same format in which the information is held in memory.


Q. 190420 To read blocks of binary data, we use the command


A. get( ).

B. getline( ).

C. read( ).

D. input( ).

Right Answer is: C

SOLUTION

The read() command can handle the entire structure of an object as a single unit, using the computer's internal representation of data.


Q. 190421 Classes ifstream, ofstream and fstream inherit from


A. fstreambase.

B. iostream.

C. filebuf.

D. streambase.

Right Answer is: C

SOLUTION

A stream is an abstraction that represents a device on which input and output operations are performed.


Q. 190422 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

Three streams exist: cout (terminal output), cin (terminal input), and cerr (error output, which also goes to the terminal).


Q. 190423 The function which closes the stream is


A. closestr().

B. closefile().

C. closef().

D. close().

Right Answer is: D

SOLUTION

The close() function closes the stream and also closes all the opened stream files.


Q. 190424 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

Input stream is a sequence of control statements and data submitted to a system from an input unit.


Q. 190425 Why should we check for End-Of-File?
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:

SOLUTION

We check for End-Of-File to prevent an attempt to read, in case the file pointer has reached the end of the file.


Q. 190426 Name any two functions used in file input-output error handling.  
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:

SOLUTION

 

Input-Output error handling functions are -

(a) int bad()  
(b) int fail() 


Q. 190427 Which classes are used for file input and output operations?
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:

SOLUTION

The classes used are ifstream, ofstream and fstream.


Q. 190428 What is meant by 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:

SOLUTION

A stream is a sequence of bytes or flow of data. It acts as an interface between the file and program.


Q. 190429 What is the difference between sequential and random access?
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:

SOLUTION

In sequential files, records are stored one after the other in sequence.  To read any record, all the preceding records must be read.  In case of random access, the read operation takes place exactly where the record is to be accessed.  To calculate the exact address in memory, a key field is transformed into the relative address.


Q. 190430 What is the difference between ifstream and ofstream classes?
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:

SOLUTION


fstream.h provides simultaneous input and output through ifstream and ofstream respectively.


ifstream - Open the file for input


ofstream - Open the file for output


Q. 190431 Explain using an example how we check for end of  file.
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:

SOLUTION

The function eof() is used to detect an end of file.  It returns a non zero value when end of file is reached.  Another way to check for end of file is whether the stream object returns zero or not.  It returns zero when the end of file is reached. 

   ifstream fin;

   fin.open(“Items.Dat”, ios::in|ios::binary);

   while(fin)                                           //as long as it is non zero

   {   fin.read((char *)& I, sizeof(ITEM));

                ………..

   }

The value in fin becomes zero when the end of file is reached, and the loop ends. Since reading occurs inside the loop, there will be no more attempts to read the file. 


Q. 190432 What are the two ways of opening a file?
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:

SOLUTION

A file can be opened in two ways :-

a)  Using the constructor of the stream class – This method is useful when only one file is used in the stream. Constructors of the stream classes ifstream, ofstream and fstream are used to initialize the file stream object with the file name. 

For example, to open a file Names.Dat for reading i.e. input, we create a stream object of class ifstream and pass on the filename to it, as follows –

         ifstream read_file(“Names.Dat”);

 

b)  Using the function open() - This method is useful when we want to use different files in the stream.  If two or more files are to be processed simultaneously, separate streams must be declared for each. 

ifstream ifl;                                 //input stream ifl created

ifl.open(“Names.Dat”);                   // file Names.Dat linked with ifl

Now using ifl, the file can be read.

 


Q. 190433 When should we use the file mode ios::nocreate?
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:

SOLUTION

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


Q. 190434 The variable that holds the address of another variable, function or data is called


A. null pointer.

B. address variable.

C. pointer variable.

D. function pointer.

Right Answer is: C

SOLUTION

It is the variable that stores the reference to another variable.


Q. 190435 The two operators for dynamic memory allocation in C++ are


A. malloc( ) and calloc( ).

B. new( ) and delete( ).

C. malloc( ) and delete( ).

D. new( ) and calloc( ).

Right Answer is: B

SOLUTION

The operator new( ) is used for memory allocation, whereas delete( ) is used for memory deallocation.


Q. 190436 When the memory allocation takes place at run time, it is called


A. dynamic memory allocation.

B. static memory allocation.

C. free store.

D. dereferencing memory.

Right Answer is: A

SOLUTION

The amount of memory required is not known beforehand. When the program executes, i.e., during run time only memory is allocated, then it is dynamic memory allocation.


Q. 190437 When we declare a variable, int c; in a C++ program, the allocation of memory here is


A. dynamic memory allocation.

B. free store.

C. automatic memory allocation.

D. static memory allocation.

Right Answer is: D

SOLUTION

Here, the computer knows the length of an integer variable, which is 2 bytes. So, 2 bytes internal memory will be allocated to the variable c during compilation itself.


Q. 190438 When the memory is allocated during compilation, it is


A. dynamic memory allocation.

B. free store.

C. static memory allocation.

D. automatic memory allocation.

Right Answer is: C

SOLUTION

The amount of memory to be allocated is known beforehand, and allocated during compile time, then it is static memory allocation.


Q. 190439 The area used for dynamic allocation of memory is called


A. stack.

B. heap.

C. reserved area.

D. free store.

Right Answer is: B

SOLUTION

The heap memory area is a region of free memory, from which chunks of memory are allocated via C++’s dynamic memory allocation functions.


Q. 190440 The data type of the pointer


A. must be the same as that of the data member it points to.

B. should be different than that of the data member it points to.

C. may or may not be same as that of the data member it points to.

D. is always float type.

Right Answer is: A

SOLUTION

A pointer points to a data member of an object and its data type is always same as that of the data member.


Q. 190441 In the declaration: int * p [10]; for 10 pointer that point to integers, the


A. array elements are always allocated first 10 memory locations.

B. array elements are always allocated last 10 memory locations.

C. array elements are always allocated randomly 10 memory locations.

D. array elements are always allocated contiguous 10 memory locations.

Right Answer is: D

SOLUTION

This declaration is for an array of 10 int pointers; so contiguous memory would be allocated for 10 int type pointers.


Q. 190442 Whenever the member functions of a class are invoked, this pointer is


A. explicitly passed to the member functions.

B. implicitly passed to the member functions.

C. referred to the member functions.

D. called by the class data types.

Right Answer is: B

SOLUTION

When a member function is called, this pointer is automatically passed as an implicit (built -in) argument that is pointer to the object that invoked the function. This pointer is called 'this'.


Q. 190443 The pointers that can cause a system to crash are


A. initialized pointers.

B. wild or uninitialized pointers.

C. virus pointers.

D. referenced pointers.

Right Answer is: B

SOLUTION

It is easy to use pointers incorrectly, causing bugs that are difficult to find, and which can crash a computer.


Q. 190444 The 'this' pointer cannot be passed to


A. static member functions only.

B. friend functions only.

C. static member functions and friend functions.

D. virtual functions and friend functions only.

Right Answer is: C

SOLUTION

The friend functions are not the members of a class, and therefore, are not passed as this pointer. Similarly, the static member functions do not have a this pointer.


Q. 190445 A special pointer, which stores the address of the object that is currently invoking a member function is


A. object pointer.

B. current pointer.

C. function pointer.

D. this pointer.

Right Answer is: D

SOLUTION

this pointer refers to the current object we are working on.


Q. 190446 The method, in which the value of each argument in the calling function is copied on to the corresponding formal arguments of the called-function is called


A. call by value.

B. call by reference.

C. call by address.

D. call by name.

Right Answer is: A

SOLUTION

In this method, the called function works with, and manipulates its own copy of arguments.


Q. 190447 Error in the code below is main() { int i, j; int *ip; i=j=ip=0; }


A. Pointer cannot be used in assignment statement.

B. ip is a pointer and cannot be used as integer.

C. Pointer should not be initialized.

D. Pointer cannot be equal to zero.

Right Answer is: B

SOLUTION

A pointer points to the address of a variable. It cannot be used as an integer.


Q. 190448 The correct output of the following program is #include < iostream.h > void main() { int i=5; int &j=i; int p=10; j=p; cout << " " << i << " " << j; p=20; cout << endl << i << " " << j; }


A. 10 10 10 10.

B. 10 5 20 20.

C. 5 10 10 10.

D. 10 10 20 20.

Right Answer is: A

SOLUTION

j is a reference to i. If, we assign value of p to j, then value of i also changes.


Q. 190449 The correct output of the following program is #include< iostream.h > void main() { char *p="abc"; char *q=p; cout << p << " " << q ; q="xyz"; cout << " " << p <<  " " << q ; }


A. abc xyz abc xyz.

B. abc abc abc xyz.

C. abc abc xyz xyz.

D. abc abc abc abc.

Right Answer is: B

SOLUTION

Value of p is assigned to pointer q . So, initially *p and * q will have same value.


Q. 190450 The correct statement according to the code below is   void *a;   char *b;   a=b;   b=a;


A. This is correct.

B. It would give an error as ‘a’ cannot be assigned to ‘b’.

C. It would give an error as ‘b’ cannot be assigned to ‘a’.

D. Error in statement 3 and 4.

Right Answer is: B

SOLUTION

In C++, void pointer cannot be assigned to the pointer of any type. This would give an error of type mismatch. It can be made true when we explicitly typecast it, b=(char*) a;


Q. 190451 The statement that is valid or legal in C++ is


A. A reference can be used without proper initialization.

B. Array of references is legal in C++.

C. char, int pointers can refer to null pointers but null pointers cannot refer to other pointers.

D. Array of pointers is not legal in C++.

Right Answer is: A

SOLUTION

A reference has to be properly initialized, otherwise it will result in an error. In C++, special care is taken for assignment of void pointers to other pointer types. While we can assign a pointer of any type to a void pointer the reverse is not true until we do explicit typecasting.


Q. 190452 If I want to declare a function that accepts three integer arguments and return a pointer to a float, then the appropriate declaration is


A. float  p (int *a, int *b, int *c);

B. float * p (int *a, int *b, int *c);

C. float * p (int a, int b, int c);

D. float * p *(int a, int b, int c);

Right Answer is: B

SOLUTION

It cannot take integer arguments.


Q. 190453 If a pointer to a function is declared, which accepts 3 integer parameters and returns a float value, then the appropriate declaration is


A. double  p (int, int, int );

B. double * p (int, int, int ):

C. int * p (double, double, double);

D. double * p (int, int, int );

Right Answer is: D

SOLUTION

Here, p is a pointer to a function.


Q. 190454 C++ treats a string as


A. an array of integers terminated by a null (‘0’).

B. an array of characters started by a null (‘0’).

C. an array of characters terminated by a null (‘0’).

D. an array of characters and integers.

Right Answer is: C

SOLUTION

A string is a one-dimensional array of characters, and is terminated by null (‘ 0’).


Q. 190455 While scanning arrays, it is faster to use


A. an index rather than an element pointer.

B. an element pointer rather than an index.

C. an index rather than using data structure trees.

D. trees rather than queues.

Right Answer is: B

SOLUTION

Using pointers, for accessing array elements fastens the process.


Q. 190456 To declare an array holding 10 int pointers, the declaration would be as follows:


A. int &ip[10];

B. int *ip[10];

C. int [10];

D. int *ip(10);

Right Answer is: B

SOLUTION

Pointers also, may be arrayed like any other data type. After the declaration, contiguous memory would be allocated for 10 pointers that can point to integers.


Q. 190457 The name of an array is actually a pointer pointing to


A. the first element of the array.

B. the last element of the array.

C. any arbitrary element of the array.

D. the second element of the array.

Right Answer is: A

SOLUTION

C++ treats the name of an array, as if it were a pointer, i.e., memory address of some element.


Q. 190458 Improper use of new() and delete() operators may lead to


A. memory allocatin.

B. memory deallocation.

C. memory use.

D. memory leaks.

Right Answer is: D

SOLUTION

The memory allocated through new() must be properly deleted through delete(), otherwise it leads to leakage of memory. We should make sure the memory allocated through new must be properly deleted through delete.


Q. 190459 The keyword used to allocate memory is


A. new.

B. malloc.

C. create.

D. value.

Right Answer is: A

SOLUTION

The new keyword allocates memory for an object or array of objects of type-name from the free store and returns a suitably typed, nonzero pointer to the object.


Q. 190460 The C++ statement that gives the value stored in pointer a is


A. a;

B. val(a);

C. *a;

D. &a;

Right Answer is: C

SOLUTION

* is a value at an address operator. Thus, it displays the value pointed by pointer a.


Q. 190461 The C++ statement int &a denotes


A. address of a.

B. reference operator.

C. pointer.

D. scanning the value of a.

Right Answer is: B

SOLUTION

The reference operator & is placed before a variable to declare a reference variable. Example: int sum; int &s=sum; Here s is a reference variable of sum, so s is pointing on the reference of sum variable.


Q. 190462 The C++ statement to display the memory address of a variable is


A. *a;

B. .a;

C. &a;

D. address(a);

Right Answer is: C

SOLUTION

The operator & is used to find the address associated with a variable. The syntax of the reference operator is as follows: &variablename.


Q. 190463 The proper keyword to deallocate memory is


A. free.

B. delete.

C. clear.

D. remove.

Right Answer is: B

SOLUTION

The delete operator deallocates a block of memory. The argument must be a pointer to a block of memory previously allocated for an object created with the new operator. The delete operator has a result of type void and therefore does not return a value.


Q. 190464 A proper declaration of pointer is


A. int x;

B. int &x;

C. ptr x;

D. int *x;

Right Answer is: D

SOLUTION

In order to define pointer variables, the programmer must use the operator denoted as * in C++. The symbol * when placed before a pointer variable means that it as a pointer to.


Q. 190465 The arithmetic operations that may be performed on pointers are


A. multiplication and division.

B. addition and subtraction.

C. multiplication only.

D. addition and division.

Right Answer is: B

SOLUTION

In pointer arithmetic, all pointers increase and decrease by the length of the data.


Q. 190466 The null pointer is defined in the header file


A. < stdio.h >.

B. < iostream.h >.

C. < stddef.h >.

D. < conio.h >.

Right Answer is: C

SOLUTION

A null pointer has a reserved value, often but not necessarily the value 0, indicating that it refers to no object.


Q. 190467 The character which, stands for the value at address operator is called


A. *.

B. ->.

C. &.

D. <<.

Right Answer is: A

SOLUTION

Using a pointer, we can directly access the value stored in the variable, which it points to.


Q. 190468 The character which stands for the address operator is


A. *.

B. ->.

C. &.

D. <<.

Right Answer is: C

SOLUTION

It is also called as the reference operator or address of operator.


Q. 190469 If a pointer to double precision quantity is declared, then the appropriate declaration is


A. double *p;

B. double p;

C. double p*;

D. double p;

Right Answer is: A

SOLUTION

Here p is a pointer, which holds the memory address of a float value.


Q. 190470 The pointer which points to only public members of the class is called


A. reference pointer.

B. function pointer.

C. empty pointer.

D. an object pointer.

Right Answer is: D

SOLUTION

We can increment and decrement an object pointer.


Q. 190471 The instance that lives in the memory as long as it is being used or referenced in an expression.


A. reference pointer.

B. function pointer.

C. an instance.

D. temporary instance.

Right Answer is: D

SOLUTION

A temporary instance is the one which lives in the memory as long as it is being used or referenced in an expression and after it dies. The temporary instances are anonymous.


Q. 190472 If we try to dereference a null pointer, then it will result in


A. compile time error.

B. deallocation of memory from null pointer.

C. run time error.

D. no error.

Right Answer is: C

SOLUTION

If this error is left unhandled, it can cause termination of the program immediately.


Q. 190473 The correct declaration of a pointer to an int is


A. int &p.

B. int*p.

C. int p.

D. int p*.

Right Answer is: B

SOLUTION

Here p is a pointer variable, which holds the address of integer data.


Q. 190474 What is the error in the given code?             
             int line[80];  cin.get(line,80,'&');  


A. the third argument is not given.

B. length of line is 80 but n is 20.

C. line should be a character array.

D. getline() cannot be used without a file.

Right Answer is: C

SOLUTION

The correct statement is char line[80];
It will read characters into line until either 80 characters are read or '&' character is encountered, whichever occurs earlier.  


Q. 190475 Number of file stream objects needed to add record from a file is/ are


A. one.

B. two.

C. three.

D. many.

Right Answer is: B

SOLUTION

One to read from original file and the other to write to a new file.


Q. 190476 The statement that would go to the 50th byte in the file is


A. ifl.tellg(ios::end, -50);

B. ifl.seekg(50, ios::beg);

C. ifl tellp(50);

D. seekp(50,ios::beg);

Right Answer is: B

SOLUTION

seekg() moves the get_pointer to a particular position from the beginning of the file.


Q. 190477 To open a DAT
A.DAT file in output mode, we need to write


A. ofl.open(DAT
A.Dat, ios

Right Answer is: D

SOLUTION

The ios::out file mode opens file for writing, i.e., in output mode. This also opens the file in ios::trunc mode, by default. This means an existing file is truncated when opened i.e., its previous contents are discarded.


Q. 190478 The following statements :

ofstream ofl;
ofl.open(filename, ios::app
A. show error, as file name incorrectly given.
B. open the given file in append mode, if it exists. 
C. open the file if it exists, delete all contents.
D. create the file if it does not exist, open it in append mode. 

Right Answer is: B

SOLUTION

It will open a file in the append mode if the file exists and will abandon the file opening operation if the file does not exist.


Q. 190479 What will the following statement do?
fl.read((char*)&oa, sizeof(oa));  


A. read as many bytes as the size of oa from the file and store in memory at the address of oa.

B. read as many bytes as the size of oa from the address of oa in memory and store in file.

C. read characters at the address of oa and store in memory.

D. read characters at the address of oa in the file and store in memory.

Right Answer is: A

SOLUTION

To read and write blocks of binary data, we use read() and write() functions.
The prototype of read() function is -
istream &read( (char*) & buf, int sizeof(buf));
The read function reads sizeof(buf) (it can be any other integer value also) bytes from the associated stream and put them in the buffer pointed to by buf.
This function takes two arguments. The first is the address of variable buf and the second is the length of that variables in bytes. The address of the variable must be type cast to type char* (i.e., a pointer to character type).


Q. 190480 The correct way to write the given statement is - 
ofl.open(“items.lst”  ios::in
A. ofl open(“items.lst”  ios::in| ios::out);
B. ofl.open(“items.lst” || ios::in|| ios::out);
C. ofl.open(“items.lst”,  ios::in| ios::out);
D. ofl.open(“items.lst” :: ios::in|| ios::out);

Right Answer is: C

SOLUTION

When we associate a stream with a file, either by initializing a file stream object with a file name or by using the open() method, we can provide a second argument specifying the file mode, as mentioned below:
stream_object.open("filename", (filemode));
The second argument of open(), the filemode, is of type int.
We can combine two or more filemode constants using the C++ bitwise OR operator (|).
For example, ofl.open(“items.lst”,  ios::in | ios::out);


Q. 190481 The function of filebuf class is 


A. to set file buffers to read and write.

B. to provide operations common to file streams.

C. to provide output operations.

D. to provide support for simultaneous input and output operations. 

Right Answer is: A

SOLUTION

The file stream class filebuf contains close() and open() member functions in it.


Q. 190482 The function of ofstream class is 


A. to provide support for simultaneous input and output operations.

B. to set file buffers to read and write.

C. to provide operations common to file streams.

D. to provide output operations.

Right Answer is: D

SOLUTION

Being an output file stream class, it provides output operations. It inherits put() and write() functions along with functions supporting random access(seekp() and tellp()) from ostream class defined inside iostream.h file.


Q. 190483 The mistake in the given statement is - 
     close(ifl);             //ifl is input stream


A. ifl close(); 

B. ifl.close(); 

C. ifstream close(ifl);

D. ifstream.ifl close();

Right Answer is: B

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();


Q. 190484 The mistake in the given statement is-        
ifstream.ifl(“OPEN.DAT”);  


A. that function open() is not there.

B. that file name is a reserved word.

C. that there is no file in opening mode.

D. there is a dot between ifstream and ifl.

Right Answer is: D

SOLUTION

In order to create file streams, the header file fstream.h is included in the program. For File-to-Memory (i.e. input) type of link, ifstream class type's stream is declared. 
For example -
ifstream ifl(OPEN.DAT"); 


Q. 190485 The function of fstream class is 


A. to set file buffers to read and write.

B. to provide operations common to file streams.

C. to provide support for simultaneous input and output operations.

D. to provide output operations.

Right Answer is: C

SOLUTION

It is an input-output file stream class. It provides support for simultaneous input and output operations. It inheirts all the functions from istream and ostream classes through iostream class defined inside iostream.h file.


Q. 190486 A data file contains only integers values. The function used to read data from file is


A. read().

B. get().

C. put().

D. write().

Right Answer is: A

SOLUTION

Since, the data in a file of integers stores data in binary format, so the function read() is used. 


Q. 190487 To print the contents of a file


A. declare a stream named PRN.

B. open an output file named PRN.

C. use the class iofprint.

D. cannot be done in C++.

Right Answer is: B

SOLUTION

To print the output of a program on printer, open file"PRN" as an output file and write contents on it.


Q. 190488 Consider the following code
  void main()
  {
      char ch='A';
      fstream ofl("Pur.dat",ios::app);
      ofl<< ch;
      int p = ofl.tellg();
     cout << p;
}
If the file content before the execution of the program is the string "ABC" (Note that " " are not part of the file), the output will be  


A. 3.

B. 4.

C. 2.

D. 1.

Right Answer is: B

SOLUTION

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


Q. 190489 The steps below pertain to insertion of a record in a sorted file, with records sorted on code.  Arrange  them in proper sequence.           
i)  open the data file and a new file ii)  write new record iii)  get new code to insert iv)  delete data file, rename new file v)  if record code less than new code, write record to new file
vi)  write records with code greater than new code  


A. i    ii   iii   iv   v   vi

B. vi   v   iv  iii   ii   i

C. iii    i    v   ii   vi   iv 

D. iii    i    iv  ii    v    vi

Right Answer is: C

SOLUTION

To insert a record in a sorted file, firstly its appropriate position is determined and then records in the file prior to this determined position are copied to temporary file, followed by the new record to be inserted and then the rest of the records from the file are also copied.


Q. 190490 The steps below pertain to deletion of a record from a file. The correct sequence of the steps is -                                                                 
i) open the data file and a new file 
ii) delete the data file and rename the new file iii) if code does not match, write record to the new file iv)  get code to delete


A. i    ii   iii   iv

B. iv  iii   ii   i

C. i    iv   ii   iii    

D. iv   i   iii   ii

Right Answer is: D

SOLUTION

To delete a record, following procedure is carried out:
a. Firstly, determine the position of the record to be deleted, by performing a search in the file.
b. Keep copying the records other than the record to be deleted in a temporary file, i.e., temp.dat.
c. Do not copy the record to be deleted to temporary file, temp.dat.
d. Copy rest of the records to temp.dat.
e. Delete original file, i.e., item.dat as : remove("item.dat");
f. Rename temp.dat as item.dat as : rename("temp.dat", "stu.dat");


Q. 190491 To add a record in a sorted file, we must


A. re-enter all records in proper order.

B. add it at the end and sort again.

C. first determine the position where it is to be added.

D. first search the file with the records.

Right Answer is: C

SOLUTION

To insert a record in a sorted file, first, its appropriate position is determined.Then the records prior to determined position are copied to a temporary file. New record is entered in temporary file and then rest of the records are copied.


Q. 190492 Out of the following, the correct statement is


A. tellg() positions the pointer for reading 

B. at end of file, function eof() returns 0  

C. tellp() returns the position of the put pointer

D. to move the put pointer 10 bytes ahead, give fl.seekp(10,ios::beg)

Right Answer is: C

SOLUTION

In C++, random access is achieved by manipulating seekg(), seekp(), tellg() abd tellp() functions. The seekg() and tellg() functions allow us to set and examine the get_pointer and the seekp() and tellp() functions perform these operations on the put_pointer.


Q. 190493 Random access is achieved by manipulating


A. seekg().

B. get().

C. eof().

D. put().

Right Answer is: B

SOLUTION

The seekg() allow to set and examine get_pointer. This function is for input streams.


Q. 190494 The function that resets the error state so that further operations can be attempted is known as


A. eof( ).

B. bad( ).

C. fail( ).

D. clear( ).

Right Answer is: D

SOLUTION

clear() function is one of the error-handling function that resets the error state so that further operations can be attempted.


Q. 190495 The file in which direct access of a record is possible is known as


A. direct file.

B. mapped file.

C. random file.

D. indirect file.

Right Answer is: C

SOLUTION

Random access files consist of records that can be accessed in any sequence.This means the data is stored exactly as it appears in the memory. It saves the processing time when read and write operation performed on the file.


Q. 190496 The base class for ofstream and ifstream is


A. fstream.

B. istream.

C. ostream.

D. ofistream.

Right Answer is: A

SOLUTION

The fstream is an input-output file stream class. It inherits all the functions from istream and ostream classes through iostream class defined inside iostream.h file.


Q. 190497 The function used to read a complete string at a time is known as


A. get( ).

B. getdata( ).

C. getline( ).

D. getch( ).

Right Answer is: C

SOLUTION

The prototype of getline() function is - istream & getline(char *buf, int num, char delim = 'n'); The function getline() 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. 190498 A collection of related fields that can be treated as a single unit is known as


A. field.

B. record.

C. file.

D. table.

Right Answer is: B

SOLUTION

A record is a collection of interrelated data,i.e.,related fields.


Q. 190499 The file in which the data is stored in the form of 0 and 1 is


A. data file.

B. text file.

C. binary file.

D. octal file.

Right Answer is: C

SOLUTION

The data in the text file is stored in the form of readable characters while the data in the binary file is in the form of binary format, i.e., 0 and 1.


Q. 190500 The link that is used to send the processed data from memory to the file is


A. file-to-memory.

B. memory-to-file.

C. memory-to-memory.

D. file-to-file.

Right Answer is: B

SOLUTION

Memory-to-file link is used for output purposes in which the data flows from memory to the file.


PreviousNext