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. What do associate containers implement?
a) Arrays
b) Associative arrays
c) Functional Arrays
d) None of the mentioned

View Answer

Answer: b [Reason:] Associative containers refer to a group of class templates in the standard library of the C++ programming language that implement ordered associative arrays.

2. By using which of the following the elements in the associate container can
be efficiently accessed?
a) Key
b) Position
c) Both Key & Position
d) None of the mentioned

View Answer

Answer: a [Reason:] Associative containers are designed to be especially efficient in accessing its elements by their key, as opposed to sequence containers which are more efficient in accessing elements by their position.

3. How many items are presented in the associate container?
a) 2
b) 3
c) 4
d) 5

View Answer

Answer: c [Reason:] There are 4 items presented in the associate container. They are set, multiset, map and multimap.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     #include <bitset>
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         string mystring;
  8.         bitset<4> mybits; 
  9.         mybits.set();
  10.         mystring = mybits.to_string<char, char_traits<char>, 
  11.         allocator<char> >();
  12.         cout << mystring << endl;
  13.         return 0;
  14.     }

a) 0000
b) 0001
c) 0011
d) 1111

View Answer

Answer: d [Reason:] In this program, We converted the bitset values to string and printing it. Output: $ g++ asc.cpp $ a.out 1111

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         vector<int> first (5, 10);
  8.         vector<int> second (5, 33);
  9.         vector<int>::iterator it;
  10.         swap_ranges(first.begin() + 1, first.end() - 1, second.begin());
  11.         cout << " first contains:";
  12.         for (it = first.begin(); it != first.end(); ++it)
  13.             cout << " " << *it;
  14.         cout << "nsecond contains:";
  15.         for (it = second.begin(); it != second.end(); ++it)
  16.             cout << " " << *it;
  17.         return 0;
  18.     }

a) first contains: 10 33 33 33 10
second contains: 10 10 10 33 33
b) first contains: 10 33 33 33 10
second contains: 10 10 10 33 10
c) first contains: 10 33 33 33 30
second contains: 10 10 10 33 10
d) none of the mentioned

View Answer

Answer: a [Reason:] In this program, We swapped the values according to their position. Output: $ g++ asc1.cpp $ a.out first contains: 10 33 33 33 10 second contains: 10 10 10 33 33

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <map>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         map<char, int> mymap;
  7.         map<char, int> :: iterator it;
  8.         mymap['b'] = 100;
  9.         mymap['a'] = 200;
  10.         mymap['c'] = 300;
  11.         for (map<char, int> :: iterator it = mymap.begin(); it != mymap.end(); ++it)
  12.             cout << it -> first << " => " << it -> second << 'n';
  13.         return 0;
  14.     }

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

View Answer

Answer: c [Reason:] In this program, We used the map template and the we used the begin operation and then we are printing the elements. Output: $ g++ asc2.cpp $ a.out a => 200 b => 100 c => 300

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <set>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         set<int> myset;
  7.         myset.insert(20);
  8.         myset.insert(30);
  9.         myset.insert(10);
  10.         while (!myset.empty())
  11.         {
  12.             cout << ' ' << *myset.begin();
  13.             myset.erase(myset.begin());
  14.         }
  15.         cout << 'n';
  16.         return 0;
  17.     }

a) 10
b) 20
c) 30
d) All of the mentioned

View Answer

Answer: d [Reason:] In this program, We used the set template and then we are initializing the values and then we are erasing it. Output: $ g++ asc3.cpp $ a.out 10 20 30

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <set>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         multiset<int> mymultiset;
  7.         for (int i = 0; i < 5; i++) mymultiset.insert(i);
  8.         multiset<int> :: key_compare mycomp = mymultiset.key_comp();
  9.         int highest = *mymultiset.rbegin();
  10.         multiset<int> :: iterator it = mymultiset.begin();
  11.         do 
  12.         {
  13.             cout << ' ' << *it;
  14.         } while (mycomp(*it++, highest));
  15.         return 0;
  16.     }

a) 12345
b) 01234
c) 1234
d) 0123

View Answer

Answer: b [Reason:] In this program, We used the set template and then we compared the keys and printing the result. Output: $ g++ asc4.cpp $ a.out 0 1 2 3 4

9. How many instance are allowed by map and set while inserting a element into container?
a) 1
b) 2
c) 3
d) Multiple

View Answer

Answer: a [Reason:] Both map and set only allow one instance of a key or element to be inserted into the container.

10. What do maps and sets support?
a) Single directional iterators
b) Bi-directional iterators
c) Single & Bi-directional directional iterators
d) None of the mentioned

View Answer

Answer: b

Set 2

Here is a listing of advanced C++ programming questions on “Container Design” along with answers, explanations and/or solutions:

1. How many sets of requirements are need in designing a container?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] There are three sets of requirements. They are container interface requirements, Allocator interface requirements and iterator requirements.

2. Which interface in the container is required for storage management?
a) Memory management
b) Allocater interface
c) Memory interface
d) None of the mentioned

View Answer

Answer: b

3. Which is present in the basic interface of the allocator interface?
a) Set of typedefs
b) A pair of allocation functions
c) allocate()
d) All of the mentioned

View Answer

Answer: d [Reason:] The basic interface of an allocator class consists of a set of typedefs, a pair of allocation functions, allocate() and deallocate() and a pair of construction/destruction members, construct() and destroy().

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     class Component
  5.     { 
  6.         public:
  7.         virtual void traverse() = 0;
  8.     };
  9.     class Leaf: public Component
  10.     {
  11.         int value;
  12.         public:
  13.         Leaf(int val)
  14.         {
  15.             value = val;
  16.         }
  17.         void traverse()
  18.         {
  19.             cout << value << ' ';
  20.         }
  21.     };
  22.     class Composite: public Component
  23.     {
  24.         vector < Component * > children;
  25.         public:
  26.         void add(Component *ele)
  27.         {
  28.             children.push_back(ele);
  29.         }
  30.         void traverse()
  31.         {
  32.             for (int i = 0; i < children.size(); i++)
  33.                 children[i]->traverse();
  34.         }
  35.     };
  36.     int main()
  37.     {
  38.         Composite containers[4];
  39.         for (int i = 0; i < 4; i++)
  40.             for (int j = 0; j < 3; j++)
  41.                 containers[i].add(new Leaf(i *3+j));
  42.             for (int k = 1; k < 4; k++)
  43.                 containers[0].add(&(containers[k]));
  44.             for (int p = 0; p < 4; p++)
  45.             {
  46.                 containers[p].traverse();
  47.             }
  48.     }

a) 345
b) 678
c) 901
d) None of the mentioned

View Answer

Answer: d [Reason:] In this program, We are choosing and printing the numbers based on the certain limit and this is a composite design pattern. Output: $ g++ cont.cpp $ a.out 0 1 2 3 4 5 6 7 8 9 10 11 3 4 5 6 7 8 9 10 11

5. Which container provides random access iterators?
a) vector
b) deque
c) sort
d) both vector & deque

View Answer

Answer: d

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     #include <iterator>
  4.     #include <stddef.h>
  5.     using namespace std;
  6.     template<class myType>
  7.     class SimpleContainer
  8.     {
  9.         public:
  10.         SimpleContainer(size_t xDim, size_t yDim, myType const& defaultValue)
  11.         : objectData(xDim * yDim, defaultValue)
  12.         , xSize(xDim)
  13.         , ySize(yDim)
  14.         {
  15.         }
  16.         myType& operator()(size_t x, size_t y)
  17.         {
  18.             return objectData[y * xSize + x];
  19.         }
  20.         myType const& operator()(size_t x, size_t y) const 
  21.         {
  22.             return objectData[y * xSize + x];
  23.         }
  24.         int getSize()
  25.         {
  26.             return objectData.size();
  27.         }
  28.         void inputEntireVector(vector<myType> inputVector)
  29.         {
  30.             objectData.swap(inputVector);
  31.         }
  32.         void printContainer(ostream& stream)
  33.         {
  34.             copy(objectData.begin(), objectData.end(),
  35.             ostream_iterator<myType>(stream, ""/*No Space*/));
  36.         }
  37.         private:
  38.         vector<myType> objectData;
  39.         size_t  xSize;
  40.         size_t  ySize;
  41.     };
  42.     template<class myType>
  43.     inline ostream& operator<<(ostream& stream, SimpleContainer<myType>& object)
  44.     {
  45.         object.printContainer(stream);
  46.         return stream;
  47.     }
  48.     void sampleContainerInterfacing();
  49.     int main()
  50.     {
  51.         sampleContainerInterfacing();
  52.         return 0;
  53.     }
  54.     void sampleContainerInterfacing()
  55.     {
  56.         static int const ConsoleWidth  = 80;
  57.         static int const ConsoleHeight = 25;
  58.         size_t width  = ConsoleWidth;
  59.         size_t height = ConsoleHeight;
  60.         SimpleContainer<int> mySimpleContainer(width, height, 0);
  61.         cout << mySimpleContainer.getSize() << endl;
  62.         mySimpleContainer(0, 0) = 5;
  63.     }

a) 2000
b) No Space
c) Error
d) Depends on the compiler

View Answer

Answer: d [Reason:] In this program, We formed a simple container and got the size of it and printing it. Output: $ g++ cont1.cpp $ a.out 200

7. Which is used for manually writing lookup table?
a) std:map
b) std:lookup
c) std:find
d) none of the mentioned

View Answer

Answer: a

8. How the member functions in the container can be accessed?
a) Iterator
b) Indirect
c) Both Iterator & Indirect
d) None of the mentioned

View Answer

Answer: a [Reason:] The container manages the storage space for its elements and provides member functions to access them, either directly or through iterators which reference objects with similar properties to pointers.

9. Which of the following type does the container should define?
a) Iterator type
b) Vector type
c) Storage type
d) None of the mentioned

View Answer

Answer: a [Reason:] Every container must define an iterator type. Iterators allow algorithms to iterate over the container’s contents.

10. Which are the parameters for the content of the buffer?
a) start
b) finish
c) both start & finish
d) none of the mentioned

View Answer

Answer: c [Reason:] The contents of the buffer are initialized using the values from the iterator range supplied to the constructor by the start and finish parameters.

Set 3

1. Which is used to allocate and deallocate storage for objects during the execution?
a) Stack
b) Heap
c) Freestore
d) None of the mentioned

View Answer

Answer: c [Reason:] Free store is a pool of memory available for you to allocate and deallocate storage for objects during the execution of your program.

2. Which operators are used in the free store?
a) new
b) delete
c) both new & delete
d) none of the mentioned

View Answer

Answer: c [Reason:] new and delete operators is used to allocate and deallocate the memory for the program.

3. What type of class member is operator new?
a) static
b) dynamic
c) const
d) smart

View Answer

Answer: a

4. What is the output of this program?

  1.     #include <new>
  2.     #include <iostream>
  3.     using namespace std;
  4.     struct A 
  5.     {
  6.         virtual ~A() {  };
  7.         void operator delete(void* p) 
  8.         {
  9.             cout << "A :: operator delete" << endl;
  10.         }
  11.     };
  12.     struct B : A 
  13.     {
  14.         void operator delete(void* p) 
  15.         {
  16.             cout << "B :: operator delete" << endl;
  17.         }
  18.     };
  19.     int main() 
  20.     {
  21.         A* ap = new B;
  22.         delete ap;
  23.     }

a) A::operator delete
b) B::operator delete
c) Both A::operator delete & B::operator delete
d) None of the mentioned

View Answer

Answer: b [Reason:] In this program, We are passing the value to the B, So we are printing B::operator delete. Output: $ g++ free.cpp $ a.out B::operator delete

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     struct A
  4.     {
  5.         virtual ~A()
  6.         { 
  7.             cout << "~A()" << endl; 
  8.         }
  9.         void operator delete[](void* p, size_t)
  10.         {
  11.             cout << "A :: operator delete[]" << endl;
  12.             delete [] p;
  13.         }
  14.     };
  15.     struct B : A 
  16.     {
  17.         void operator delete[](void* p, size_t) 
  18.         {
  19.             cout << "B :: operator delete[]" << endl;
  20.             delete [] p;
  21.         }
  22.     };
  23.     int main() 
  24.     {
  25.         A* bp = new B[3];
  26.         delete[] bp;
  27.     };

a) ~A()
b) A :: operator delete[].
c) B :: operator delete[].
d) Warning

View Answer

Answer: d [Reason:] In this program, the behavior of the statement delete[] bp is undefined. $ g++ a.cpp a.cpp: In static member function ‘static void A::operator delete [](void*, size_t)’: a.cpp:12: warning: deleting ‘void*’ is undefined a.cpp: In static member function ‘static void B::operator delete [](void*, size_t)’: a.cpp:20: warning: deleting ‘void*’ is undefined

$ a.out ~A() ~A() ~A() A :: operator delete[].

6. What is the output of this program?

  1.     #include <cstdlib>
  2.     #include <iostream>
  3.     using namespace std;
  4.     class X 
  5.     {
  6.         public:
  7.         void* operator new(size_t sz) throw (const char*)
  8.         {
  9.             void* p = malloc(sz);
  10.             if (p == 0) 
  11.                 throw "malloc() failed";
  12.             return p;
  13.         }
  14.         void operator delete(void* p) 
  15.         {
  16.             cout << "X :: operator delete(void*)" << endl;
  17.             free(p);
  18.         } 
  19.     };
  20.     class Y 
  21.     {
  22.         int filler[100];
  23.         public:
  24.         void operator delete(void* p, size_t sz) throw (const char*)
  25.         {
  26.             cout << "Freeing " << sz << " bytes" << endl;
  27.             free(p);
  28.         };
  29.     };
  30.     int main() 
  31.     {
  32.         X* ptr = new X;
  33.         delete ptr;
  34.         Y* yptr = new Y;
  35.         delete yptr;
  36.     }

a) X::operator delete(void*)
b) Freeing 400 bytes
c) Depends on the compiler
d) Both X::operator delete(void*) & Depends on the compiler

View Answer

Answer: d [Reason:] The memory value allocated for the program depends on compiler ony. $ g++ free2.cpp $ a.out X :: operator delete(void*) Freeing 400 bytes

7. What is the output of this program?

  1.     #include <new>
  2.     #include<cstdlib>
  3.     #include <iostream>
  4.     using namespace std;
  5.     class X;
  6.     struct Node 
  7.     {
  8.         X* data;
  9.         bool filled;
  10.         Node() : filled(false) { }
  11.     };
  12.     class X 
  13.     {
  14.         static Node buffer[];
  15.         public:
  16.         int number;
  17.         enum { size = 3};
  18.         void* operator new(size_t sz) throw (const char*)
  19.         {
  20.             void* p = malloc(sz);
  21.             if (sz == 0)
  22.                 throw "Error: malloc() failed";
  23.             cout << "X :: operator new(size_t)" << endl;
  24.             return p;
  25.         }
  26.         void *operator new(size_t sz, int location) throw (const char*) 
  27.         {
  28.             cout << "X :: operator new(size_t, " << location << ")" << endl;
  29.             void* p = 0;
  30.             if (location < 0 || location >= size || buffer[location].filled == true)
  31.             {
  32.                 throw "Error: buffer location occupied";
  33.             }
  34.             else 
  35.             {
  36.                 p = malloc(sizeof(X));
  37.                 if (p == 0) 
  38.                     throw "Error: Creating X object failed";
  39.                 buffer[location].filled = true;
  40.                 buffer[location].data = (X*) p;
  41.             }
  42.             return p;
  43.         }
  44.         static void printbuffer() 
  45.         {
  46.             for (int i = 0; i < size; i++) 
  47.             {
  48.                 cout << buffer[i].data->number << endl;
  49.             }
  50.         } 
  51.     };
  52.     Node X::buffer[size];
  53.     int main()
  54.     {
  55.         try 
  56.         {
  57.             X* ptr1 = new X;
  58.             X* ptr2 = new(0) X;
  59.             X* ptr3 = new(1) X;
  60.             X* ptr4 = new(2) X;
  61.             ptr2->number = 10000;
  62.             ptr3->number = 10001;
  63.             ptr4->number = 10002;
  64.             X :: printbuffer();
  65.             X* ptr5 = new(0) X;
  66.         }
  67.         catch (const char* message) 
  68.         {
  69.             cout << message << endl;
  70.         }
  71.     }

a) X::operator new(size_t)
b) Error
c) Runtime error
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are giving a location to two variables in the program, So it is arising an exception. Output: $ g++ free3.cpp $ a.out X::operator new(size_t) X::operator new(size_t, 0) X::operator new(size_t, 1) X::operator new(size_t, 2) 10000 10001 10002 X::operator new(size_t, 0) Error: buffer location occupied

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <new>
  3.     #include <cstdlib>
  4.     using namespace std;
  5.     const int bsize = 512;
  6.     int *pa;
  7.     bool allocate = true;
  8.     void get_memory() 
  9.     {
  10.         cerr << "free store exhausted" << endl;
  11.         delete [] pa;
  12.         allocate = false;
  13.     }
  14.     void eat_memory(int size) 
  15.     {
  16.         int *p = new int[size];
  17.         if (allocate)
  18.             eat_memory(size);
  19.         else
  20.             cerr << "free store addr = " << p << endl;
  21.     }
  22.     int main()
  23.     {
  24.         set_new_handler(get_memory);
  25.         pa = new int[bsize];
  26.         cerr << "free store addr = " << pa << endl;
  27.         eat_memory(bsize);
  28.         return 0;
  29.     }

a) Free store addr
b) Error
c) Segmentation fault
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, The memory will go beyond the limit, So there will be exhaustion in memory. Output: $ g++ free4.cpp $ a.out free store addr = 0x80a8008 Segmentation fault

9. What must be an operand of operator delete?
a) Pointer
b) Array
c) Stack
d) None of the mentioned

View Answer

Answer: a [Reason:] The operand of delete must be a pointer returned by new.

10. How can object be allocated outside the object lifetime?
a) int
b) float
c) void*
d) none of the mentioned

View Answer

Answer: c

Set 4

1. Which header file is used to operate on numeric sequences?
a) number
b) numeric
c) algorithm
d) none of the mentioned

View Answer

Answer: b [Reason:] header file is used to operate on numeric sequences that support certain operations.

2. Which mathematics library is used for vector manipulation in c++?
a) cli++
b) vec++
c) blitz++
d) none of the mentioned

View Answer

Answer: c [Reason:] Blitz++ is a high-performance vector mathematics library written in C++.

3. What is the use of accumulate function in numeric library?
a) Returns the number
b) Returns the result of accumulating all the values in the range
c) Returns the number & result
d) None of the mentioned

View Answer

Answer: b [Reason:] Returns the result of accumulating all the values in the range from first to last.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <numeric>
  4.     using namespace std;
  5.     int myop (int x, int y)
  6.     {
  7.         return x + y;
  8.     }
  9.     int main ()
  10.     {
  11.         int val[] = {1, 2, 3, 5};
  12.         int result[7];
  13.         adjacent_difference (val, val + 7, result);
  14.         for (int i = 0; i < 4; i++)
  15.             cout << result[i] <<' ';
  16.         return 0;
  17.     }

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

View Answer

Answer: a [Reason:] In this program, We are calculating the adjacent difference of the given range by using function adjacent_difference. Output: $ g++ gnl.cpp $ a.out 1 1 1 2

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <numeric>
  4.     using namespace std;
  5.     int myfunction (int x, int y) 
  6.     {
  7.         return x + 2 * y;
  8.     }
  9.     struct myclass 
  10.     {
  11.         int operator()(int x, int y) 
  12.         {
  13.             return x + 3 * y;
  14.         }
  15.     } myobject;
  16.     int main () 
  17.     {
  18.         int init = 100;
  19.         int numbers[] = {10, 20, 30};
  20.         cout << accumulate(numbers, numbers + 3, init);
  21.         cout << endl;
  22.     }

a) 100
b) 140
c) 160
d) 180

View Answer

Answer: c [Reason:] In this program, We are calculating the product of every number in the given range by using accumulate function. Output: $ g++ gnl1.cpp $ a.out 160

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <numeric>
  4.     using namespace std;
  5.     int myop (int x, int y) 
  6.     {
  7.         return x + y + 1;
  8.     }
  9.     int main () 
  10.     {
  11.         int val[] = {1, 2, 3, 4, 5};
  12.         int result[5];
  13.         partial_sum (val, val + 5, result);
  14.         for (int i = 0; i < 5; i++)
  15.             cout << result[i] << ' ';
  16.         return 0;
  17.     }

a) 1 3 6
b) 1 3 6 10 15
c) 1 3 6 10 16
d) None of the mentioned

View Answer

Answer: b [Reason:] In this program, We are calculating the sum of the given range by using partial_sum function. Output: $ g++ gnl2.cpp $ a.out 1 3 6 10 15

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <numeric>
  4.     using namespace std;
  5.     int myfunction (int x, int y) 
  6.     {
  7.         return x + 2 * y;
  8.     }
  9.     struct myclass 
  10.     {
  11.         int operator()(int x, int y) 
  12.         {
  13.             return x + 3 * y;
  14.         }
  15.     } myobject;
  16.     int main () 
  17.     {
  18.         int init = 100;
  19.         int numbers[] = {10, 20, 30};
  20.         cout << accumulate (numbers, numbers + 3, init, minus<int>() );
  21.         cout << endl;
  22.         return 0;
  23.     }

a) 40
b) 100
c) 140
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We are finding the difference between the init and the total of numbers range. Output: $ g++ gnl3.cpp $ a.out 40

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <numeric> 
  4.     using namespace std;
  5.     int myaccumulator (int x, int y) 
  6.     {
  7.         return x - y;
  8.     }
  9.     int myproduct (int x, int y) 
  10.     {
  11.         return x + y;
  12.     }
  13.     int main () 
  14.     {
  15.         int a = 100;
  16.         int series1[] = {10, 20, 30};
  17.         int series2[] = {1, 2, 3};
  18.         cout << inner_product(series1, series1 + 3, series2, a ,myaccumulator, 
  19.         myproduct);
  20.         cout << endl;
  21.         return 0;
  22.     }

a) 40
b) 34
c) 32
d) 20

View Answer

Answer: b [Reason:] In this program, We are forming the custom function from two ranges by using inner_product function. Output: $ g++ gnl4.cpp $ a.out 34

9. How many parameters are available in partial_sum function in c++?
a) 2
b) 3
c) 2 or 3
d) 3 or 4

View Answer

Answer: d [Reason:] There are three or four parameters available in partial_sum function in C++. They are first and last element, result and an optional binary operator.

10. What is the default operation of adjacent_difference function in numeric library?
a) Difference
b) Addition
c) Multiplication
d) None of the mentioned

View Answer

Answer: a [Reason:] The default operation is to calculate the difference, but some other operation can be specified as binary operator instead.

Set 5

1. How does a sequence of objects are accessed in c++?
a) Iterators
b) Pointers
c) Both Iterators & Pointers
d) None of the mentioned

View Answer

Answer: c [Reason:] A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers.

2. How many parameters are present in mismatch method in non-sequence modifying algorithm?
a) 1
b) 2
c) 3
d) 3 or 4

View Answer

Answer: d [Reason:] There are two definitions of mismatch with either three or four parameters. They are first1, last1, first2 and optional predicate.

3. What will happen in ‘all_of’ method if the range is empty?
a) Return true
b) Return false
c) Return nothing
d) None of the mentioned

View Answer

Answer: a [Reason:] Returns true if pred returns true for all the elements in the range [first,last) or if the range is empty, and false otherwise.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     #include <utility>
  5.     using namespace std;
  6.     bool mypredicate (int i, int j)
  7.     {
  8.         return (i == j);
  9.     }
  10.     int main () 
  11.     {
  12.         vector<int> myvector;
  13.         for (int i = 1; i < 6; i++) myvector.push_back (i * 10);
  14.         int myints[] = {10, 20, 30, 40, 1024};
  15.         pair<vector<int> :: iterator, int*> mypair;
  16.         mypair = mismatch (myvector.begin(), myvector.end(), myints);
  17.         cout  << *mypair.first<<'n';
  18.         cout  << *mypair.second << 'n';
  19.         ++mypair.first; ++mypair.second;
  20.         return 0;
  21.     }

a) 40
1024
b) 50
1024
c) 20
1024
d) None of the mentioned

View Answer

Answer: b [Reason:] In this program, We are finding the elements which are mismatching in both the variables. Output: $ g++ non.cpp $ a.out 50 1024

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     bool IsOdd (int i)
  6.     {
  7.         return ((i % 2) == 1);
  8.     }
  9.     int main () 
  10.     {
  11.         vector<int> myvector;
  12.         myvector.push_back(10);
  13.         myvector.push_back(25);
  14.         myvector.push_back(40);
  15.         myvector.push_back(55);
  16.         vector<int> :: iterator it = find_if (myvector.begin(), 
  17.         myvector.end(), IsOdd);
  18.         cout  << *it << 'n';
  19.         return 0;
  20.     }

a) 10
b) 25
c) 40
d) 55

View Answer

Answer: b [Reason:] In this program, We used find_if method and returned the first odd value in the vector. Output: $ g++ non1.cpp $ a.out 25

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         vector<int> myvector;
  8.         for (int i = 1; i < 5; ++i)
  9.             myvector.push_back(i);
  10.         rotate(myvector.begin(), myvector.begin() + 3, myvector.end( ));
  11.         for (vector<int> :: iterator it = myvector.begin(); 
  12.             it != myvector.end(); ++it)
  13.         cout << ' ' << *it;
  14.         return 0;
  15.     }

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

View Answer

Answer: d [Reason:] In this program, We are rotating the vector values by 3, So it is printing this option. Output: $ g++ non2.cpp $ a.out 4 1 2 3

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     bool myfunction (int i, int j) 
  6.     {
  7.         return (i==j);
  8.     }
  9.     int main () 
  10.     {
  11.         int myints[] = {10, 20, 20, 20, 30, 30, 20, 20, 10};
  12.         vector<int> myvector (myints, myints + 9);
  13.         vector<int> :: iterator it;
  14.         it = unique (myvector.begin(), myvector.end());                                
  15.         myvector.resize( distance(myvector.begin(), it) );
  16.         unique (myvector.begin(), myvector.end(), myfunction);
  17.         for (it = myvector.begin(); it != myvector.end(); ++it)
  18.             cout << ' ' << *it;
  19.         return 0;
  20.     }

a) 10 20 30 20 10
b) 10 20 30
c) 30 20 10
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We are printing only the unique values by comparing every value. Output: $ g++ non3.cpp $ a.out 10 20 30 20 10

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
  8.         int mycount = count (myints, myints + 8, 10);
  9.         cout << mycount;
  10.         vector<int> myvector (myints, myints + 8);
  11.         mycount = count (myvector.begin(), myvector.end(), 20);
  12.         cout << mycount;
  13.         return 0;
  14.     }

a) 33
b) 44
c) 22
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We are counting the number of 10’s and 20’s in the program. Output: $ g++ non4.cpp $ a.out 33

9. To what kind of elements does non-modifying sequence algorithm can be applied?
a) Range
b) Vector
c) List
d) All of the mentioned

View Answer

Answer: a [Reason:] Non-modifying sequence algorithm can be applied to list and vector for example the “find” function can be applied to list and vector.

10. Pick out the incorrect method in non-modifying sequence algorithm?
a) find-if
b) none-of
c) any-of
d) like

View Answer

Answer: d

.woocommerce-message { background-color: #98C391 !important; }