Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

Multiple choice question for engineering

Set 1

1. Which header file is used with input and output operations of C in C++?
a) stdio.h
b) cstdio
c) iostream
d) none of the mentioned

View Answer

Answer: b [Reason:] Input and Output operations of c can be performed in C++ using the C Standard Input and Output Library.

2. Which will be used with physical devices to interact from C++ program?
a) Programs
b) Library
c) Streams
d) None of the mentioned

View Answer

Answer: c [Reason:] C++ library uses streams to operate with physical devices such as Keyboards, Printers, Terminals or with any other type of files supported by the system.

3. How many streams are automatically created when executing a program?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] There are streams that are automatically created when executing a program. They are stdin, stdout and stderr.

4. What is the output of this program by manipulating the text file?

  1.     #include <stdio.h>
  2.     int main () 
  3.     {
  4.         if (remove( "myfile.txt" ) != 0 )
  5.             perror( "Error" );
  6.         else
  7.             puts( "Success" );
  8.         return 0;
  9.     }

a) Error
b) Success
c) Runtime Error
d) Can’t say

View Answer

Answer: d [Reason:] If myfile.txt exists, then it will delete the file. Else it will print an error message. Output: $ g++ out.cpp $ a.out Success

5. What is the output of this program?

  1.     #include <stdio.h>
  2.     int main ()
  3.     {
  4.         FILE * p;
  5.         int c;
  6.         int n = 0;
  7.         p = fopen ("myfile.txt", "r");
  8.         if (p == NULL) 
  9.             perror ("Error opening file");
  10.         else
  11.         {
  12.             do {
  13.                 c = getc (p);
  14.                 if (c == '$') 
  15.                     n++;
  16.             } while (c != EOF);
  17.             fclose (p);
  18.             printf ("%dn", n);
  19.         }
  20.         return 0;
  21.     }

a) Count of ‘$’ symbol
b) Error opening file
c) Any of the mentioned
d) None of the mentioned

View Answer

Answer: c [Reason:] Any one is possible – Either the file doesn’t exist or If exist, it will print the total number of ‘$’ character.

6. What is the output of this program in the text file?

  1.     #include <stdio.h>
  2.     int main ()
  3.     {
  4.         FILE * pFile;
  5.         char c;
  6.         pFile = fopen("sample.txt", "wt");
  7.         for (c = 'A'; c <= 'E'; c++) 
  8.         {    
  9.             putc (c, pFile);
  10.         }
  11.         fclose (pFile);
  12.         return 0;
  13.     }

a) ABCD
b) ABC
c) ABCDE
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are printing from A to E by using the putc function. Output: $ g++ out2.cpp $ a.out ABCDE

7. What is the name of the myfile2 file after executing this program?

  1.     #include <stdio.h>
  2.     int main ()
  3.     {
  4.         int result;
  5.         char oldname[] = "myfile2.txt";
  6.         char newname[] = "newname.txt";
  7.         result = rename(oldname, newname );
  8.         if (result == 0)
  9.             puts ( "success" );
  10.         else
  11.             perror( "Error" );
  12.         return 0;
  13.     }

a) name
b) new
c) newname
d) none of the mentioned

View Answer

Answer: c [Reason:] In this program, We are renaming the myfile2 to newname by using the function rename. Output: myfile2.txt is renamed to newname.txt

8. How many number of characters are available in newname.txt?

  1.     #include <stdio.h>
  2.     int main ()
  3.     {
  4.         FILE * p;
  5.         int n = 0;
  6.         p = fopen ("newname.txt", "rb");
  7.         if (p == NULL) 
  8.             perror ("Error opening file");
  9.         else
  10.         {
  11.             while (fgetc(p) != EOF)
  12.             {
  13.                 ++n;
  14.             }
  15.             if (feof(p)) 
  16.             {
  17.                 printf ("%dn", n);
  18.             }
  19.             else
  20.                 puts ("End-of-File was not reached.");
  21.             fclose (p);
  22.         }
  23.         return 0;
  24.     }

a) 10
b) 15
c) Depends on the text file
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are reading the number of characters in the program by using the function feof. Output: $ g++ out4.cpp $ a.out 162

9. How many indicators are available in c++?
a) 4
b) 3
c) 2
d) 1

View Answer

Answer: b [Reason:] There are three indicators are available in C++. They are Error indicator, End-Of-File indicator and Position indicator.

10. What is the benefit of c++ input and output over c input and output?
a) Type safety
b) Exception
c) Both Type safety & Exception
d) None of the mentioned

View Answer

Answer: a [Reason:] None.

Set 2

1. What kind of library is Standard Template Library?
a) Polymorphic
b) Generic
c) Both Polymorphic & Generic
d) None of the mentioned

View Answer

Answer: b [Reason:] The STL is a generic library, meaning that its components are heavily parameterized.

2. To what type of object does the container can be instantiated?
a) int
b) float
c) double
d) any type of object

View Answer

Answer: d

3. What type of class template is list?
a) Class-based
b) Node-based
c) Method-based
d) None of the mentioned

View Answer

Answer: b [Reason:] It is node-based because it allows efficient insertion anywhere on the program.

4. What type of access does deque and vector provide?
a) Linear access
b) Parallel access
c) Random access
d) None of the mentioned

View Answer

Answer: c [Reason:] Because they can manipulate the values on anywhere in the program, So it is providing randoma access.

5. Where does the vector add the item?
a) End
b) Insert
c) Middle
d) None of the mentioned

View Answer

Answer: a

6. Which are not full container classes in c++?
a) Sequence container
b) Associative container
c) Container adaptor
d) None of the mentioned

View Answer

Answer: c [Reason:] Container adaptors are not full container classes, but classes that provide a specific interface relying on an object of one of the container classes such as deque or list to handle the elements.

7. What is the lifetime of the element in container?
a) Whole program
b) Outside the block
c) Everywhere
d) Only on that container

View Answer

Answer: d [Reason:] A Container “owns” its elements: the lifetime of an element stored in a container cannot exceed that of the Container itself.

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <map> 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         multimap<char, int> mymultimap;
  7.         mymultimap.insert(make_pair('x', 100));
  8.         mymultimap.insert(make_pair('y', 200));
  9.         mymultimap.insert(make_pair('y', 350));
  10.         mymultimap.insert(make_pair('z', 500));
  11.         cout << mymultimap.size() << 'n';
  12.         return 0;
  13.     }

a) 1
b) 2
c) 4
d) 3

View Answer

Answer: c [Reason:] In this program, We are counting the number of elements in the map. Output: $ g++ alc.cpp $ a.out 4

9. What is the output of this program?

  1.     #include <iostream>
  2.     #include <queue>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         priority_queue<int> mypq;
  7.         mypq.push(10);
  8.         mypq.push(20);
  9.         mypq.push(15);
  10.         cout  << mypq.top() << endl;
  11.         return 0;
  12.     }

a) 15
b) 20
c) 10
d) Error

View Answer

Answer: b [Reason:] In this program, We used the queue template and the top method is used to retain the last but before element. Output: $ g++ alc1.cpp $ a.out 20

10. What is the output of this program?

  1.     #include <iostream>
  2.     #include <map>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         multimap<char, int> mymultimap;
  7.         mymultimap.insert(make_pair('y', 202));
  8.         mymultimap.insert(make_pair('y', 252));
  9.         pair<char, int> highest = *mymultimap.rbegin();
  10.         multimap<char, int> :: iterator it = mymultimap.begin();
  11.         do 
  12.         {
  13.             cout << (*it).first << " => " << (*it).second << 'n';
  14.         } while ( mymultimap.value_comp()(*it++, highest) );
  15.         return 0;
  16.     }

a) y => 202
b) y => 252
c) y => 202 & y => 252
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, the method rbegin is used to return the first element in the map. Output: $ g++ alc2.cpp $ a.out y = > 202

Set 3

1. What is the Run-Time Type Information?
a) Information about an object’s datatype at runtime
b) Information about the variables
c) Information about the given block
d) None of the mentioned

View Answer

Answer: a [Reason:] With the help of RTTI, We can get the information about the data type at the runtime.

2. Which operators are part of RTTI?
a) dynamic_cast()
b) typeid
c) both dynamic_cast() & typeid
d) none of the mentioned

View Answer

Answer: c [Reason:] The dynamic_cast<> operation and typeid operator in C++ are part of RTTI.

3. To which type of class, We can apply RTTI?
a) Encapsulation
b) Polymorphic
c) Derived
d) None of the mentioned

View Answer

Answer: b [Reason:] RTTI is available only for classes which are polymorphic, which means they have at least one virtual method.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     class base { virtual void dummy() {} };
  5.     class derived: public base { int a; };
  6.     int main () 
  7.     {
  8.         try 
  9.         {
  10.             base * pba = new derived;
  11.             base * pbb = new base;
  12.             derived * pd;
  13.             pd = dynamic_cast<derived*>(pba);
  14.             if (pd == 0) 
  15.                 cout << "Null pointer on first type-cast" << endl;
  16.             pd = dynamic_cast<derived*>(pbb);
  17.             if (pd == 0) 
  18.                 cout << "Null pointer on second type-cast" << endl;
  19.         } 
  20.         catch (exception& e) 
  21.         {
  22.             cout << "Exception: " << e.what();
  23.         }
  24.         return 0;
  25.     }

a) Null pointer on first type-cast
b) Null pointer on second type-cast
c) Exception
d) None of the mentioned

View Answer

Answer: b [Reason:] In this program, We apply the dynamic cast to pd. Based on the value in the pd, it produces the output. Output: $ g++ rtti.cpp $ a.out Null pointer on second type-cast

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <typeinfo>
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         int * a;
  7.         int b;
  8.         a = 0; b = 0;
  9.         if (typeid(a) != typeid(b))
  10.         {
  11.             cout << typeid(a).name();
  12.             cout << typeid(b).name();
  13.         }
  14.         return 0;
  15.     }

a) Pi
b) i
c) Both pi & i
d) f

View Answer

Answer: c [Reason:] In this program, We are finding the typeid of the given variables. Output: $ g++ rtti1.cpp $ a.out Pii

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <typeinfo>
  3.     #include <exception>
  4.     using namespace std;
  5.     class base 
  6.     { 
  7.         virtual void f(){} 
  8.     };
  9.     class derived : public base {};
  10.     int main () 
  11.     {
  12.         try 
  13.         {
  14.             base* a = new base;
  15.             base* b = new derived;
  16.             cout << typeid(*a).name() << 't';
  17.             cout << typeid(*b).name();
  18.         } 
  19.         catch (exception& e) 
  20.         { 
  21.             cout << "Exception: " << e.what() << endl; 
  22.         }
  23.         return 0;
  24.     }

a) base*
b) derived*
c) 4base and 7derived
d) none of the mentioned

View Answer

Answer: c [Reason:] In this program, We apply the typeid to the polymorphic class. Output: $ g++ rtti2.cpp $ a.out 4base 7derived

7. What is the output of this program?

  1.     #include <typeinfo>
  2.     #include <iostream>
  3.     using namespace std;
  4.     class A
  5.     {
  6.         public:
  7.         virtual ~A();
  8.     };
  9.     int main() 
  10.     {
  11.         A* a = NULL;
  12.         try 
  13.         {
  14.             cout << typeid(*a).name() << endl; 
  15.         }
  16.         catch (bad_typeid)
  17.         {
  18.             cout << "Object is NULL" << endl;
  19.         }
  20.     }

a) int
b) float
c) double
d) object is NULL

View Answer

Answer: d [Reason:] In this program, We are using the bad typeid() for a. So it is arising an exception. Output: $ g++ rtti3.cpp $ a.out object is NULL

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     struct A 
  4.     {
  5.         virtual void f()  
  6.         { 
  7.             cout << "Class A" << endl; 
  8.         }
  9.     };
  10.     struct B : A 
  11.     {
  12.         virtual void f() 
  13.         { 
  14.             cout << "Class B" << endl;
  15.         }
  16.     };
  17.     struct C : A 
  18.     {
  19.         virtual void f() 
  20.         {
  21.             cout << "Class C" << endl; 
  22.         }
  23.     };
  24.     void f(A* arg) 
  25.     {
  26.         B* bp = dynamic_cast<B*>(arg);
  27.         C* cp = dynamic_cast<C*>(arg);
  28.         if (bp)
  29.             bp -> f();
  30.         else if (cp)
  31.             cp -> f();
  32.         else
  33.             arg -> f();  
  34.     };
  35.     int main() 
  36.     {
  37.         A aobj;
  38.         C cobj;
  39.         A* ap = &cobj;
  40.         A* ap2 = &aobj;
  41.         f(ap);
  42.         f(ap2);
  43.     }

a) Class C
b) Class A
c) Both Class C & A
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We applied the dynamic casting to structure and produced the output. Output: $ g++ rtti4.cpp $ a.out Class C Class A

9. What is meant by type_info?
a) Used to hold the type information returned by the typeid operator
b) Used to hold the type information returned by the dynamic_cast
c) Used to hold the type information returned by the static cast
d) None of the mentioned

View Answer

Answer: a

10. At which time does the static_cast can be applied?
a) Compile-time construct
b) Runtime construct
c) Both Compile-time & Runtime construct
d) None of the mentioned

View Answer

Answer: a [Reason:] Static_cast can be applied to only compile-time construct and not during run time construct.

Set 4

1. What do vectors represent?
a) Static arrays
b) Dynamic arrays
c) Stack
d) Queue

View Answer

Answer: b [Reason:] Vectors are sequence containers representing arrays that can change in size.

2. In which type of storage location are the vector members stored?
a) Contiguous storage locations
b) Non-contiguous storage locations
c) Contiguous & Non-contiguous storage locations
d) None of the mentioned

View Answer

Answer: a [Reason:] Vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements

3. How many vector container properties are there in c++?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] There are three container properties in c++. They are sequence, Dynamic array and allocator-aware.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         unsigned int i;
  7.         vector<int> first;
  8.         vector<int> second (4, 100);
  9.         vector<int> third (second.begin(), second.end());
  10.         vector<int> fourth (third);
  11.         int myints[] = {16, 2, 77, 29};
  12.         vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
  13.         for (vector<int> :: iterator it = fifth.begin(); it != fifth.end(); ++it)
  14.             cout << ' ' << *it;
  15.         return 0;
  16.     }

a) 16
b) 16 2
c) 16 2 77
d) 16 2 77 29

View Answer

Answer: d [Reason:] In this program, We got the values and printing it by using the vector and we are contructing vectors. Output: $ g++ vect.cpp $ a.out 16 2 77 29

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         vector<int> myvector;
  7.         int sum (0);
  8.         myvector.push_back (100);
  9.         myvector.push_back (200);
  10.         myvector.push_back (300);
  11.         while (!myvector.empty())
  12.         {
  13.             sum += myvector.back();
  14.             myvector.pop_back();
  15.         }
  16.         cout << sum << 'n';
  17.         return 0;
  18.     }

a) 500
b) 600
c) 700
d) Error

View Answer

Answer: b [Reason:] In this program, We are forming a stack and adding the elements and We are finding the total number of elements that are in stack. Output: $ g++ vect1.cpp $ a.out 600

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         vector<int> a (3, 0);
  7.         vector<int> b (5, 0);
  8.         b = a;
  9.         a = vector<int>();
  10.         cout << "Size of a " << int(a.size()) << 'n';
  11.         cout << "Size of b " << int(b.size()) << 'n';
  12.         return 0;
  13.     }

a) Size of a 0
Size of b 3
b) Size of a 3
Size of b 5
c) Error
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We are finding the size of the vector elements. Output: $ g++ vect2.cpp $ a.out Size of a 0 Size of b 3

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         vector<int> first;
  7.         first.assign (7,100);
  8.         vector<int>::iterator it;
  9.         it=first.begin()+1;
  10.         int myints[] = {1776,7,4};
  11.         cout << int (first.size()) << 'n';
  12.         return 0;
  13.     }

a) 10
b) 9
c) 8
d) 7

View Answer

Answer: d [Reason:] In this program, We are finding the size of the vector elements and resizing it. Output: $ g++ vect3.cpp $ a.out 7

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         vector<int> myvector (5);
  7.         int* p = myvector.data();
  8.         *p = 10;
  9.         ++p;
  10.         *p = 20;
  11.         p[2] = 100;
  12.         for (unsigned i = 0; i < myvector.size(); ++i)
  13.             cout << ' ' << myvector[i];
  14.         return 0;
  15.     }

a) 10 20 0 100 0
b) 10 20 0 100
c) 10 20 0
d) 10 20

View Answer

Answer: a [Reason:] In this program, We are allocating the values to the vector and unallocated values are left as zero. Output: $ g++ vect4.cpp $ a.out 10 20 0 100 0

9. Pick out the correct statement about vector.
a) vector<int> values (5)
b) vector values (5)
c) vector<int> (5)
d) none of the mentioned

View Answer

Answer: a [Reason:] The syntax for declaring the vector element is vector<type> variable_name (number_of_elements);

10. Which is optional in the declaration of vector?
a) Type
b) Name
c) Vector
d) Number_of_elements

View Answer

Answer: d [Reason:] The number of elements is optional. An empty vector means, A vector that contains zero elements.

Set 5

1. What kind of iteration does forward_list provide in C++?
a) Uni-directional
b) Bi-directional
c) Multi-directional
d) None of the mentioned

View Answer

Answer: a [Reason:] In the forward_list, the container provides insertion and removal at anywhere in the program.

2. What does the size of the vector refers to in c++?
a) Size of vector
b) Type of vector
c) Number of elements
d) None of the mentioned

View Answer

Answer: c

3. Subsequent elements are moved in terms of _____ when an element in inserted in vector?
a) Assignment Operator
b) Copy constructor
c) Both assignment operator and copy constructor
d) None of the mentioned

View Answer

Answer: c [Reason:] The vector maintains a certain order of its elements, so that when a new element is inserted at the beginning or in the middle of the vector, Subsequent elements are moved backwards in terms of their assignment operator or copy constructor.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         vector<int> myvector (3);
  7.         for (unsigned i = 0; i < myvector.size(); i++)
  8.         myvector.at(i) = i;
  9.         for (unsigned i = 0; i < myvector.size(); i++)
  10.         cout << ' ' << myvector.at(i);
  11.         return 0;
  12.     }

a) 1 2 3
b) 0 1 2
c) 1 2 3 4
d) None of the mentioned

View Answer

Answer: b [Reason:] In this program, We are pushing the values into the vector from 0 to 3 by using for loop. Output: $ g++ seqc.cpp $ a.out 0 1 2

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         vector<int> myvector;
  7.         myvector.push_back(78);
  8.         myvector.push_back(16);
  9.         myvector.front() += myvector.back();
  10.         cout << myvector.front() << 'n';
  11.         return 0;
  12.     }

a) 78
b) 16
c) 94
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We added all the values in the vector by using front and back operation. Output: $ g++ seqc1.cpp $ a.out 94

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <list>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         list<int> mylist;
  7.         list<int> :: iterator it1, it2;
  8.         for (int i = 1; i < 10; ++i) mylist.push_back(i * 1);
  9.             it1 = it2 = mylist.begin();
  10.         advance (it2, 6);
  11.         ++it1;
  12.         it1 = mylist.erase (it1);
  13.         it2 = mylist.erase (it2);
  14.         ++it1;
  15.         --it2;
  16.         mylist.erase (it1, it2);
  17.         for (it1 = mylist.begin(); it1 != mylist.end(); ++it1)
  18.             cout << ' ' << *it1;
  19.         return 0;
  20.     }

a) 1 3 6
b) 8 9
c) 1 3 6 8 9
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are comparing the values in both lists and erasing it according to certain condition. Output: $ g++ seqc2.cpp $ a.out 1 3 6 8 9

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <deque> 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         unsigned int i;
  7.         deque<int> mydeque;
  8.         deque<int> :: iterator it;
  9.         mydeque.push_back ( 100 );
  10.         mydeque.push_back ( 200 );
  11.         mydeque.push_back ( 300 );
  12.         for (it = mydeque.begin(); it != mydeque.end(); ++it)
  13.             mydeque.clear();
  14.         cout << ' ' << *it;
  15.     }

a) 100
b) 200
c) 300
d) None of the mentioned

View Answer

Answer: d [Reason:] Errors will arise because we are clearing all the queue values before printing it.

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <deque>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         deque<int> mydeque;
  7.         int sum (0);
  8.         mydeque.push_back ( 10 );
  9.         mydeque.push_back ( 20 );
  10.         mydeque.push_back ( 30 );
  11.         while (!mydeque.empty())
  12.         {
  13.             sum += mydeque.back();
  14.             mydeque.pop_back();
  15.         }
  16.         cout << sum << 'n';
  17.         return 0;
  18.     }

a) 10
b) 20
c) 30
d) 60

View Answer

Answer: d [Reason:] In this program, We are adding all the values in the queue. Output: $ g++ seqc3.cpp $ a.out 60

9. What is the use of adapter in STL in c++?
a) To provide interface
b) To manipulate the data
c) To extract the data
d) None of the mentioned

View Answer

Answer: a [Reason:] Adapters are data types from STL that adapt a container to provide specific interface.

10. Which is used to iterate over container?
a) Associated iterator type
b) Data type of objects
c) Return type of variables
d) None of the mentioned

View Answer

Answer: a