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 is meant by heap?
a) Used for fast reterival of elements
b) Used for organising the elements
c) Used for fast reterival & organising the elements
d) None of the mentioned

Answer: c [Reason:] A heap is a way to organize the elements of a range that allows for fast retrieval of the element.

2. Which value is pointed out first in heap?
a) Lowest value
b) Highest value
c) First value
d) None of the mentioned

Answer: b [Reason:] The element with the highest value is always pointed by first.

3. Which operator is used to compare the elements in heap?
a) >>
b) comp
c) <
d) Both comp &<

Answer: d [Reason:] The elements in the heap are compared using operator< (for the first version), or comp (for the second version).

4. 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, 5, 15};`
8. `        vector<int> v(myints, myints + 5);`
9. `        make_heap (v.begin(), v.end());`
10. `        pop_heap (v.begin(), v.end()); v.pop_back();`
11. `        v.push_back(99); push_heap (v.begin(), v.end());`
12. `        sort_heap (v.begin(), v.end());`
13. `        for (unsigned i = 0; i < v.size(); i++)`
14. `            cout << ' ' << v[i];`
15. `        return 0;`
16. `    }`

a) 5 10
b) 5 10 15 20
c) 5 10 15 20 99
d) None of the mentioned

Answer: c [Reason:] In this program, We popped out 30 and pushed 99 and then we are sorting that value, So it is printing it. Output: \$ g++ heap.cpp \$ a.out 5 10 15 20 99

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. `        int myints[] = {1, 2, 3, 4 ,5};`
8. `        vector<int> v(myints, myints + 5);`
9. `        v.push_back(33); `
10. `        push_heap (v.begin(),v.end());`
11. `        cout << v.front() << 'n';`
12. `        sort_heap (v.begin(),v.end());`
13. `        return 0;`
14. `    }`

a) 1
b) 33
c) 3
d) 44

Answer: b [Reason:] In this program, We are pushing a new value into heap and printing it. Output: \$ g++ heap1.cpp \$ a.out 33

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. `        int myints[] = {2, 4, 6, 8, 10};`
8. `        vector<int> v(myints, myints + 5);`
9. `        make_heap (v.begin(),v.end());`
10. `        cout  << v.front() << 'n'; `
11. `        return 0;`
12. `    }`

a) 10
b) 20
c) 4
d) 8

Answer: a [Reason:] In this program, We are printing the maximum value in the heap. Output: \$ g++ heap2.cpp \$ a.out 10

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) { return (i<j); }`
6. `    int main () `
7. `    {`
8. `        int myints[] = {9, 8, 7, 6, 5};`
9. `        vector<int> myvector (myints, myints + 5);`
10. `        partial_sort (myvector.begin(), myvector.begin() + 3, myvector.end());`
11. `        partial_sort (myvector.begin(), myvector.begin() + 2, myvector.end(), `
12. `        myfunction);`
13. `        for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)`
14. `            cout << ' ' << *it;`
15. `        return 0;`
16. `    }`

a) 5 6 7
b) 5 6 7 9 8
c) 9 8 7 6 5
d) None of the mentioned

Answer: b [Reason:] In this program, We are partitioning the value by using the partial_sort method. Output: \$ g++ heap3.cpp \$ a.out 5 6 7 9 8

8. How to protect the heap from affecting the memory?
a) Avoid using pointers for associating two data structures
b) Embed pointed child objects into the parent object
c) Allocate objects in chunks
d) All of the mentioned

9. In what form does the STL provides heap?
a) queue
b) list
c) vector
d) priority_queue

Answer: d [Reason:] STL does provide a heap in the form of a std::priority_queue.

10. How many types are there in binary heaps?
a) 1
b) 2
c) 3
d) 4

Answer: b [Reason:] There are two types of heaps. They are min and max heap.

## Set 2

1. What is the use of middle parameter in rotate method?
a) Marks the begining of a sequence
b) Marks the ending of a sequence
c) Marks the elements in a sequence
d) None of the mentioned

Answer: c [Reason:] Forward iterator pointing to the element within the range and that can be moved to the first position in the range.

2. What kind of object is modifying sequence algorithm?
a) Function template
b) Class template
c) Method
d) None of the mentioned

Answer: a [Reason:] It is a group of functions and implemented under algorithm header file.

3. How the sequence of objects can be accessed?
a) Iterators
b) Pointers
c) Both Iterators & Pointers
d) None of the mentioned

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.

4. 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 (5);`
8. `        fill (myvector.begin(), myvector.begin() + 4, 5);`
9. `        fill (myvector.begin() + 3,myvector.end() - 2, 8);`
10. `        for (vector<int> :: iterator it = myvector.begin();`
11. `            it != myvector.end(); ++it)`
12. `        cout << ' ' << *it;`
13. `        return 0;`
14. `    }`

a) 5 5 5 5 0
b) 8 8 8 8 0
c) 5 8 5 8 0
d) 5 5 5 5 5

Answer: a [Reason:] In this program, We filled up all the vector values by using fill method. Output: \$ g++ msa.cpp \$ a.out 5 5 5 5 0

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. `        int myints[]={ 10, 20, 30, 40, 50 };`
8. `        vector<int> myvector (4, 99);`
9. `        iter_swap(myints, myvector.begin());`
10. `        iter_swap(myints + 3,myvector.begin() + 2);`
11. `        for (vector<int> :: iterator it = myvector.begin(); `
12. `            it != myvector.end(); ++it)`
13. `        cout << ' ' << *it;`
14. `        return 0;`
15. `    }`

a) 10
b) 10 40
c) 10 99 40 99
d) None of the mentioned

Answer: c [Reason:] In this program, We are swapping the certain values in two vectors by using iter_swap. Output: \$ g++ msa1.cpp \$ a.out 10 99 40 99

6. What is the output of this program?

1. `    #include <iostream>`
2. `    #include <algorithm>`
3. `    #include <vector>`
4. `    #include <functional>`
5. `    using namespace std;`
6. `    int op_increase (int i)`
7. `    {`
8. `        return ++i;`
9. `    }`
10. `    int main ()`
11. `    {`
12. `        vector<int> a;`
13. `        vector<int> b;`
14. `        for (int i = 1; i < 4; i++)`
15. `        a.push_back (i * 10);`
16. `        b.resize(a.size());`
17. `        transform (a.begin(), a.end(), b.begin(), op_increase);`
18. `        transform (a.begin(), a.end(), b.begin(), a.begin(), plus<int>());`
19. `        for (vector<int> :: iterator it = a.begin(); it != a.end(); ++it)`
20. `            cout << ' ' << *it;`
21. `        return 0;`
22. `    }`

a) 21
b) 41
c) 61
d) All of the mentioned

Answer: d [Reason:] In this program, We allocated the values to the vector and then by using transform function, We increased the values. Output: \$ g++ msa2.cpp \$ a.out 21 41 61

7. What is the output of this program?

1. `    #include <iostream>`
2. `    #include <algorithm>`
3. `    using namespace std;`
4. `    int main ()`
5. `    {`
6. `        int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };`
7. `        int* pbegin = myints;`
8. `        int* pend = myints + sizeof(myints) / sizeof(int);`
9. `        pend = remove (pbegin, pend, 20);`
10. `        for (int* p = pbegin; p != pend; ++p)`
11. `            cout << ' ' << *p;`
12. `        return 0;`
13. `    }`

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

Answer: b [Reason:] In this program, We removed the values in the vector by using the remove method. Output: \$ g++ msa3.cpp \$ a.out 10 30 30 10 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 ,40 };`
8. `        int * p;`
9. `        p = find (myints, myints + 4, 30);`
10. `        --p;`
11. `        cout << *p << 'n';`
12. `        return 0;`
13. `    }`

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

Answer: b [Reason:] In this program, We used the find method to find the value before 20. Output: \$ g++ msa4.cpp \$ a.out 20

9. How many kind of operation can be applied to transform method in c++?
a) 1
b) 2
c) 3
d) 4

Answer: b [Reason:] There are two kinds of operations. They are unary and binary operation.

10. What operator is used to remove the dupplicates in the range?
a) )
b) ^
c) %
d) ==

Answer: d [Reason:] The function uses operator== to compare the pairs of elements.

## Set 3

1. What does the function objects implement?
a) operator
b) operator()
c) operand
d) none of the mentioned

Answer: b [Reason:] Function objects are objects specifically designed to be used with a syntax similar to that of functions.

2. What are the two advantage of function objects than the function call?
a) It contains a state
b) It is a type
c) It contains a state & It is a type
d) None of the mentioned

Answer: c [Reason:] A function object can contain state. The second is that a function object is a type and therefore can be used as a template parameter.

3. Which header is need to be used with function objects?

```a) <function>
b) <functional>
c) <funct>
d) <functionstream>```

4. What is the output of this program?

1. `    #include <iostream>`
2. `    #include <functional>`
3. `    #include <algorithm>`
4. `    using namespace std;`
5. `    int main ()`
6. `    {`
7. `        int first[] = {10, 40, 90};`
8. `        int second[] = {1, 2, 3};`
9. `        int results;`
10. `        transform ( first, first + 5, second, results, divides<int>());`
11. `        for (int i = 0; i < 3; i++)`
12. `            cout << results[i] << " ";`
13. `        return 0;`
14. `    }`

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

Answer: c [Reason:] In this program, We are dividing the first with the second by using function objects. Output: \$ g++ funo.cpp \$ a.out 10 20 30

5. What is the output of this program?

1. `    #include <iostream> `
2. `    #include <functional>`
3. `    #include <algorithm>`
4. `    using namespace std;`
5. `    int main () `
6. `    {`
7. `        int numbers[] = {3, -4, -5};`
8. `        transform ( numbers, numbers + 5, numbers, negate<int>() );`
9. `        for (int i = 0; i < 3; i++)`
10. `            cout << numbers[i] << " ";`
11. `    }`

a) -3
b) 3 4 5
c) 3 -4 5
d) -3 4 5

Answer: d [Reason:] In this program, We negated the given value by using the functional objects, So it is producing this output. Output: \$ g++ funo1.cpp \$ a.out -3 4 5

6. What is the output of this program?

1. `    #include <iostream>`
2. `    #include <functional>`
3. `    #include <vector>`
4. `    #include <algorithm>`
5. `    #include <string>`
6. `    using namespace std;`
7. `    int main () `
8. `    {`
9. `        vector <string*> numbers;`
10. `        numbers.push_back ( new string ("one") );`
11. `        numbers.push_back ( new string ("two") );`
12. `        numbers.push_back ( new string ("three") );`
13. `        vector <int> lengths ( numbers.size() );`
14. `        transform (numbers.begin(), numbers.end(), lengths.begin(), `
15. `        mem_fun(&string :: length));`
16. `        for (int i = 0; i < 3; i++) `
17. `        {`
18. `            cout << lengths[i];`
19. `        }`
20. `        return 0;`
21. `    }`

a) 335
b) 225
c) 334
d) 224

Answer: a [Reason:] In this program, We calculated the number of letters in every string by using function objects. Output: \$ g++ funo2.cpp \$ a.out 335

7. What is the output of this program?

1. `    #include <iostream>`
2. `    #include <functional>`
3. `    #include <algorithm>`
4. `    using namespace std;`
5. `    int main () `
6. `    {`
7. `        int numbers[] = {1, 2, 3};`
8. `        int remainders;`
9. `        transform ( numbers, numbers + 5, remainders, `
10. `        bind2nd(modulus<int>(), 2) );`
11. `        for (int i = 0; i < 5; i++)`
12. `            cout << (remainders[i] == 1 ? "odd" : "even") << "n";`
13. `        return 0;`
14. `    }`

a) odd
even
b) even
c) odd
odd
d) odd
even
odd
odd
even

Answer: d [Reason:] Running the program will show above behaviour because we have given the value in for loop as 5 instead of 3.

8. What is the output of this program?

1. `    #include <iostream>`
2. `    #include <functional>`
3. `    #include <algorithm>`
4. `    using namespace std;`
5. `    int main ()`
6. `    {`
7. `        int numbers[] = {10, -20, -30, 40, -50};`
8. `        int cx;`
9. `        cx = count_if ( numbers, numbers + 5, bind2nd(less<int>(), 0) );`
10. `        cout << cx;`
11. `        return 0;`
12. `    }`

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

Answer: c [Reason:] In this program, We are calculating the number of negative elements present in the program by using function objects. Output: \$ g++ funo3.cpp \$ a.out 3

9. Which are instances of a class with member function operator() when it is
defined?
a) function objects
b) member
c) methods
d) none of the mentioned

Answer: a [Reason:] Function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a regular function call.

10. How many parameters does a operator() in a function object shoud take?
a) 1
b) 2
c) 3
d) 4

Answer: b [Reason:] In the case of binary function objects, this operator() member function will take two parameters.

## Set 4

1. How many groups of output of operation are there in c++?
a) 1
b) 2
c) 3
d) 4

Answer: b [Reason:] There are two groups of output operation in c++. They are formatted output and unformatted output.

2. Pick out the correct objects about the instantiation of output stream.
a) cout
b) cerr
c) clog
d) all of the mentioned

Answer: d [Reason:] cout, cerr and clog are the standard objects for the instantiation of output stream class.

3. What is meant by ofstream in c++?
a) Writes to a file
b) Reads from a file
c) Writes to a file & Reads from a file
d) None of the mentioned

Answer: a [Reason:] ofstream is a stream class to write on files.

4. What is the output of this program?

1. `    #include <iostream>`
2. `    using namespace std;`
3. `    int main () `
4. `    {`
5. `        char str[] = "Steve jobs";`
6. `        int val = 65;`
7. `        char ch = 'A';`
8. `        cout.width (5);`
9. `        cout << right;`
10. `        cout << val << endl;`
11. `        return 0;`
12. `    }`

a) Steve jobs
b) A
c) 65
d)     65

Answer: d [Reason:] In this program, We are printing the five spaces and then we are printing the value of 65. Output: \$ g++ ous .cpp \$ a.out      65

5. What is the output of this program?

1. `    #include <iostream>`
2. `    using namespace std;`
3. `    int main () `
4. `    {`
5. `        int n; `
6. `        n = 43;`
7. `        cout << hex << n << endl;`
8. `        return 0;`
9. `    }`

a) 2c
b) 2b
c) 20
d) 50

Answer: b [Reason:] In this program, We are printing the hexadecimal value of the given decimal number by using hex function. Output: \$ g++ ous1.cpp \$ a.out 2b

6. What is the output of this program in the “test.txt” file?

1. `    #include <fstream>`
2. `    using namespace std;`
3. `    int main ()`
4. `    {`
5. `        long pos;`
6. `        ofstream outfile;`
7. `        outfile.open ("test.txt");`
8. `        outfile.write ("This is an apple",16);`
9. `        pos = outfile.tellp();`
10. `        outfile.seekp (pos - 7);`
11. `        outfile.write (" sam", 4);`
12. `        outfile.close();`
13. `        return 0;`
14. `    }`

a) This is an apple
b) apple
c) sample
d) This is a sample

Answer: d [Reason:] In this program, We are changing the ap to sam by using the pos function. Output: \$ g++ ous2.cpp \$ a.out This is a sample

7. What is the output of this program?

1. `    #include <iostream>`
2. `    using namespace std;`
3. `    int main ()`
4. `    {`
5. `        int n;`
6. `        n = -77;`
7. `        cout.width(4); `
8. `        cout << internal << n << endl;`
9. `        return 0;`
10. `    }`

a) 77
b) -77
c) – 77
d) None of the mentioned

Answer: c [Reason:] In this program, We are using the internal function and moving the 77 to one position. Output: \$ g++ ous3.cpp \$ a.out – 77

8. What is the output of this program?

1. `    #include <iostream>`
2. `    #include <locale>`
3. `    using namespace std;`
4. `    int main()`
5. `    {`
6. `        locale mylocale("");`
7. `        cout.imbue( mylocale );`
8. `        cout << (double) 3.14159 << endl;`
9. `        return 0;`
10. `    }`

a) 3.14
b) 3.14159
c) Error
d) None of the mentioned

Answer: b [Reason:] In this program, We are using the locale and then assigning the type to the value. Output: \$ g++ ous4.cpp \$ a.out 3.14159

9. How many types of output stream classes are there in c++?
a) 1
b) 2
c) 3
d) 4

Answer: c [Reason:] There are three output stream classes in c++. They are ostream, ofstream and ostrstream.

10. What must be specified when we construct an object of class ostream?
a) stream
b) streambuf
c) memory
d) none of the mentioned

Answer: b [Reason:] If you construct an object of class ostream, you must specify a streambuf object to the constructor.

## Set 5

1. How many categories of iterators are there in c++?
a) 2
b) 4
c) 5
d) 3

Answer: c [Reason:] There are five types of iterators. They are Output, Input, Forward, Random access and Bi-directional.

2. Which of the following can serve as random-access iterator?
a) Memory pointer
b) Object pointer
c) Class pointer
d) None of the mentioned

Answer: b [Reason:] Because of this, It can serve as any category of iterator.

3. What kind of pattern is iterator pattern?
a) Design pattern
b) Sequence pattern
d) All of the mentioned

Answer: a [Reason:] Iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container’s elements.

4. What is the output of this program?

1. `    #include <iostream>`
2. `    #include <set>`
3. `    using namespace std;`
4. `    int main()`
5. `    {`
6. `        set<int> tst;`
7. `        tst.insert(12);`
8. `        tst.insert(21);`
9. `        tst.insert(32);`
10. `        tst.insert(31);`
11. `        set<int> :: const_iterator pos;`
12. `        for(pos = tst.begin(); pos != tst.end(); ++pos)`
13. `        cout << *pos << ' ';`
14. `        return 0;`
15. `    }`

a) 12 21 32 31
b) 12 21 31 32
c) 12 21 32
d) 12 21 31

Answer: b [Reason:] In this program, We are using const_iterator to sort the data in the set. Output: \$ g++ itr.cpp \$ a.out 12 21 31 32

5. What is the output of this program?

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

a) 5 6 7 8 9
b) 5 6 8 9 10
c) 6 7 8 9 10
d) None of the mentioned

Answer: b [Reason:] In this program, We are erasing the values in the vector based on the given condition. Output: \$ g++ itr1.cpp \$ a.out 5 6 8 9 10

6. What is the output of this program?

1. `    #include <iostream>`
2. `    #include <iterator>`
3. `    #include <list>`
4. `    using namespace std;`
5. `    int main () `
6. `    {`
7. `        list<int> mylist;`
8. `        for (int i = 0; i < 10; i++) `
9. `        mylist.push_back (i * 10);`
10. `        list<int> :: iterator it = mylist.begin();`
11. `        advance (it, 5);`
12. `        cout  << *it << endl;`
13. `        return 0;`
14. `    }`

a) 30
b) 40
c) 50
d) 60

Answer: c [Reason:] In this program, We are printing the sixth element in the list. Output: \$ g++ itr2.cpp \$ a.out 50

7. What is the output of this program?

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

a) 10 20 1 2
b) 10 20
c) 1 2
d) 1 10

Answer: a [Reason:] In this iterator, We are copying the first list into second and printing it. Output: \$ g++ itr3.cpp \$ a.out 10 20 1 2

8. What is the output of this program?

1. `    #include <iostream>`
2. `    #include <iterator>`
3. `    #include <list>`
4. `    using namespace std;`
5. `    int main () `
6. `    {`
7. `        list<int> mylist;`
8. `        for (int i = 0; i < 5; i++) `
9. `            mylist.push_back (i * 20);`
10. `        list<int> :: iterator first = mylist.begin();`
11. `        list<int> :: iterator last = mylist.end();`
12. `        cout << distance(first, last) << endl;`
13. `        return 0;`
14. `    }`

a) 20
b) 100
c) 5
d) None of the mentioned

Answer: c [Reason:] In this program, We are printing the number of elements in the list by using distance method. Output: \$ g++ itr4.cpp \$ a.out 5

9. In which type of semantics does c++ implements iterator?
a) Memory
b) Size
c) Pointer
d) None of the mentioned