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

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

1. What is meant by permutation in c++?
a) To find all the values in the range
b) To find all the combination of the range
c) To find all the values & combination in the range
d) None of the mentioned

View Answer

Answer: b [Reason:] The permutation is used to find all the combination of numbers in the range.

2. How the different permutations are ordered in c++?
a) Compare lexicographicaly to each other elements
b) By finding the highest element in the range
c) By finding the lowest element in the range
d) None of the mentioned

View Answer

Answer: a

3. Pick out the correct statement about permutation.
a) If the function can determine the next higher permutation, Returns false
b) If the function can determine the next higher permutation, Returns true
c) If the function can’t determine the next higher permutation, Returns true
d) None of the mentioned

View Answer

Answer: b [Reason:] If the function can determine the next higher permutation, it rearranges the elements as such and returns true.

4. What is the output of this program?

  1.     #include <string>
  2.     #include <iostream>
  3.     using namespace std;
  4.     void string_permutation( string& orig, string& perm )
  5.     {
  6.         if (orig.empty())
  7.         {
  8.             cout<<perm<<endl;
  9.             return;
  10.         }
  11.         for (int i = 0; i < orig.size(); ++i)
  12.         {
  13.             string orig2 = orig;
  14.             orig2.erase(i, 1);
  15.             string perm2 = perm;
  16.             perm2 += orig.at(i);
  17.             string_permutation(orig2, perm2);
  18.         }
  19.     }
  20.     int main()
  21.     {
  22.         string orig = "ter";
  23.         string perm;
  24.         string_permutation(orig, perm);
  25.         return 0;
  26.     }

a) ter
b) ert
c) ret
d) returns all the combination of ter

View Answer

Answer: d [Reason:] In the program, We used string permutation to find out all the combination. Output: $ g++ perm.cpp $ a.out ter tre etr ert rte ret

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         int myints[] = {1, 2, 3};
  7.         sort (myints, myints + 3);
  8.         do 
  9.         {
  10.         } while ( next_permutation(myints, myints + 3) );
  11.         cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << 'n';
  12.         return 0;
  13.     }

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

View Answer

Answer: a [Reason:] In this program, We are doing the permutation in the do while loop and then printing last permuted value. Output: $ g++ perm1.cpp $ a.out 1 2 3

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  4.     void swap(int x, int y)
  5.     {
  6.         int temp = array[x];
  7.         array[x] = array[y];
  8.         array[y] = temp;
  9.         return;
  10.     }
  11.     void printArray(int size)
  12.     {
  13.         int i;
  14.         for (i = 0; i < size; i++)
  15.             cout << array[i] << " ";
  16.         cout << endl;
  17.         return;
  18.     }
  19.     void permute(int k, int size)
  20.     {
  21.         int i;
  22.         if (k == 0)
  23.             printArray(size);
  24.         else
  25.         {
  26.             for (i = k - 1;i >= 0;i--)
  27.             {
  28.                 swap(i, k - 1);
  29.                 permute(k - 1, size);
  30.                 swap(i, k - 1);
  31.             }
  32.         }
  33.         return;
  34.     }
  35.     int main()
  36.     {
  37.         permute(3, 3);
  38.         return 0;
  39.     }

a) 0 1 2
1 0 2
b) 0 2 1
2 0 1
c) 2 1 0
1 2 0
d) All of the mentioned

View Answer

Answer: d [Reason:] In this program, We are finding the permutation without using the permutation function. Output: $ g++ perm2.cpp $ a.out 0 1 2 1 0 2 0 2 1 2 0 1 2 1 0 1 2 0

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     #include <algorithm>
  4.     using namespace std;
  5.     void Display(const vector<int>& vi)
  6.     {
  7.         for (size_t i = 0; i < vi.size(); ++i)
  8.             cout << vi[i];
  9.         cout<<endl;
  10.     }
  11.     int main()
  12.     {
  13.         vector<int> vi;
  14.         vi.push_back(3);
  15.         vi.push_back(5);
  16.         sort(vi.begin(), vi.end());
  17.         Display(vi);
  18.         while(next_permutation(vi.begin(), vi.end()))
  19.         Display(vi);
  20.         return 0;
  21.     }

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

View Answer

Answer: c [Reason:] In this program, We find out the permutation of two numbers by using sort. Output: $ g++ perm3.cpp $ a.out 5 3 3 5

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     #include <algorithm>
  4.     using namespace std;
  5.     void show(const vector<int>& vi)
  6.     {
  7.         for (size_t i = 0; i < vi.size(); ++i)
  8.             cout << vi[i];
  9.         cout << endl;
  10.     }
  11.     int main()
  12.     {
  13.         vector<int> vi;
  14.         vi.push_back(3);
  15.         vi.push_back(5);
  16.         vi.push_back(5);
  17.         sort(vi.begin(), vi.end());
  18.         show(vi);
  19.         while(next_permutation(vi.begin(), vi.end()))
  20.             show(vi);
  21.         return 0;
  22.     }

a) 355
b) 535
c) 553
d) All of the mentioned

View Answer

Answer: d [Reason:] In this program, We are finding the permutation for the given value. Output: $ g++ perm4.cpp $ a.out 355 535 553

9. What is the header file for vector permutation?
a) vector_permutation.h
b) vector_perm
c) <algorithm>
d) vector_permutation

View Answer

Answer: c [Reason:] To use permutation on a vector we can use the “next_permutation” function defined in the header.

10. How many parameters are required for next_permutation?
a) 1
b) 2
c) 2 or 3
d) 3

View Answer

Answer: c [Reason:] There are 2 or 3 needed for next_permutation. They are first, last and optional comp for comparing the elements.

Set 2

1. To which does the function pointer point to?
a) variable
b) constants
c) function
d) absolute variables

View Answer

Answer: c

2. What we will not do with function pointers?
a) allocation of memory
b) de-allocation of memory
c) both allocation & de-allocation of memory
d) none of the mentioned

View Answer

Answer: c [Reason:] As it is used to execute a block of code, So we will not allocate or deallocate memory.

3. What is the default calling convention for a compiler in c++?
a) __cdecl
b) __stdcall
c) __pascal
d) __fastcall

View Answer

Answer: a

4. What is te output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int add(int first, int second)
  4.     {
  5.         return first + second + 15;
  6.     }
  7.     int operation(int first, int second, int (*functocall)(int, int))
  8.     {
  9.         return (*functocall)(first, second);
  10.     }
  11.     int main()
  12.     {
  13.         int  a;
  14.         int  (*plus)(int, int) = add;
  15.         a = operation(15, 10, plus);
  16.         cout << a;
  17.         return 0;
  18.     }

a) 25
b) 35
c) 40
d) 45

View Answer

Answer: c [Reason:] In this program, we are adding two numbers with 15, So we got the output as 40. Output: $ g++ pfu2.cpp $ a.out 40

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     void func(int x)
  4.     {
  5.         cout << x ;
  6.     }
  7.     int main()
  8.     {
  9.         void (*n)(int);
  10.         n = &func;
  11.         (*n)( 2 );
  12.         n( 2 );
  13.         return 0;
  14.     }

a) 2
b) 20
c) 21
d) 22

View Answer

Answer: d [Reason:] As we are calling the function two times with the same value, So it is printing as 22. Output: $ g++ pfu.cpp $ a.out 22

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int n(char, int);
  4.     int (*p) (char, int) = n;
  5.     int main()
  6.     {
  7.         (*p)('d', 9);
  8.         p(10, 9);
  9.         return 0;
  10.     }
  11.     int n(char c, int i)
  12.     {
  13.         cout << c <<  i;
  14.         return 0;
  15.     }

a) d9
     9
b) d9d9
c) d9
d) compile time error

View Answer

Answer: a [Reason:] None. Output: $ g++ pfu1.cpp $ a.out d9 9

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int func (int a, int b)
  4.     {
  5.         cout << a;
  6.         cout << b;
  7.         return 0;
  8.     }
  9.     int main(void)
  10.     {
  11.         int(*ptr)(char, int);
  12.         ptr = func;
  13.         func(2, 3);
  14.         ptr(2, 3);
  15.         return 0;
  16.     }

a) 2323
b) 232
c) 23
d) compile time error

View Answer

Answer: d [Reason:] In this program, we can’t do the casting from char to int, So it is raising an error.

8. What are the mandatory part to present in function pointers?
a) &
b) retrun values
c) data types
d) none of the mentioned

View Answer

Answer: c [Reason:] The data types are mandatory for declaring the variables in the function pointers.

9. which of the following can be passed in function pointers?
a) variables
b) data types
c) functions
d) none of the mentioned

View Answer

Answer: c

10. What is meaning of following declaration?
    int(*ptr[5])();
a) ptr is pointer to function
b) ptr is array of pointer to function
c) ptr is pointer to such function which return type is array
d) ptr is pointer to array of function

View Answer

Answer: b [Reason:] In this expression, ptr is array not pointer.

Set 3

1. Which is referred by pointers to member?
a) static members of class objects
b) Non-static members of class objects
c) Refering to whole class
d) None of the mentioned

View Answer

Answer: b [Reason:] We cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object.

2. What should be used to point to a static class member?
a) Smart pointer
b) Dynamic pointer
c) Normal pointer
d) None of the mentioned

View Answer

Answer: c

3. Which operator is used in pointer to member function?
a) .*
b) ->*
c) Both .* & ->*
d) None of the mentioned

View Answer

Answer: c [Reason:] The pointer to member operators .* and ->* are used to bind a pointer to a member of a specific class object.

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class X 
  4.     {
  5.         public:
  6.         int a;
  7. 		void f(int b) 
  8. 		{
  9. 			cout<< b << endl;
  10. 		}
  11.     };
  12.     int main() 
  13.     {
  14.         int X :: *ptiptr = &X :: a;
  15.         void (X :: * ptfptr) (int) = &X :: f;
  16.         X xobject;
  17.         xobject.*ptiptr = 10;
  18.         cout << xobject.*ptiptr << endl;
  19.         (xobject.*ptfptr) (20);
  20.     }

a) 10
20
b) 20
10
c) 20
d) 10

View Answer

Answer: a [Reason:] In this program, We are assigning 10 and printing it in the main function and then for value 20, We are passing the value to class and printing it. Output: $ g++ ptm.cpp $ a.out 10 20

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Testpm 
  4.     {
  5.         public:
  6.         void m_func1() 
  7.         { 
  8.             cout << "func1n";
  9.         }
  10.         int m_num;
  11.     };
  12.     void (Testpm :: *pmfn)() = &Testpm :: m_func1;
  13.     int Testpm :: *pmd = &Testpm :: m_num;
  14.     int main() 
  15.     {
  16.         Testpm ATestpm;
  17.         Testpm *pTestpm = new Testpm;
  18.         (ATestpm.*pmfn)();
  19.         (pTestpm ->* pmfn)();
  20.         ATestpm.*pmd = 1;
  21.         pTestpm ->* pmd = 2;
  22.         cout << ATestpm.*pmd << endl
  23.         << pTestpm ->* pmd << endl;
  24.     }

a) func1
b) func1
func1
c) 1
2
d) func1
func1
1
2

View Answer

Answer: d [Reason:] In this program, As we are passing the value twice to the method in the class, It is printing the func1 twice and then it is printing the given value. Output: $ g++ ptm1.cpp $ a.out func1 func1 1 2

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Car
  4.     {
  5.         public:
  6.         int speed;
  7.     };
  8.     int main()
  9.     {
  10.         int Car :: *pSpeed = &Car :: speed;
  11.         Car c1;
  12.         c1.speed = 1;           
  13.         cout << c1.speed << endl;
  14.         c1.*pSpeed = 2;     
  15.         cout  << c1.speed << endl;
  16.         return 0;
  17.     }

a) 1
b) 2
c) Both 1 & 2
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are printing the value by direct access and another one by using pointer to member. Output: $ g++ ptm2.cpp $ a.out 1 2

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class bowl 
  4.     {
  5.         public:
  6.         int apples;
  7.         int oranges;
  8.     };
  9.     int count_fruit(bowl * begin, bowl * end, int bowl :: *fruit)
  10.     {
  11.         int count = 0;
  12.         for (bowl * iterator = begin; iterator != end; ++ iterator)
  13.             count += iterator ->* fruit;
  14.         return count;
  15.     }
  16.     int main()
  17.     {
  18.         bowl bowls[2] = {{ 1, 2 },{ 3, 5 }};
  19.         cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: apples) << " applesn";
  20.         cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: oranges) << " orangesn";
  21.         return 0;
  22.     }

a) I have 4 apples
I have 7 oranges
b) I have 3 apples
I have 5 oranges
c) I have 1 apples
I have 5 oranges
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We are passing the value to the class and adding the values and printing it in the main. Output: $ g++ ptm3.cpp $ a.out I have 4 apples I have 7 oranges

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Foo
  4.     {
  5.         public:
  6.         Foo(int i = 0){ _i = i;}
  7.         void f()
  8.         {
  9.             cout << "Executed"<<endl;
  10.         }
  11.         private:
  12.         int _i;
  13.     };
  14.     int main()
  15.     {
  16.         Foo *p = 0;
  17.         p -> f();
  18.     }

a) Executed
b) Error
c) Runtime error
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We passes the value to the class and printing it. Output: $ g++ ptm4.cpp $ a.out Executed

9. Which is the best design choice for using pointer to member function?
a) Interface
b) Class
c) Structure
d) None of the mentioned

View Answer

Answer: a

10. What is the operation for .*?
a) It combines the first operand and the second operand
b) It seperates the first operand and the second operand
c) It reduces the data size
d) None of the mentioned

View Answer

Answer: a [Reason:] The binary operator .* combines its first operand, which must be an object of class type, with its second operand, which must be a pointer-to-member type.

Set 4

1. With which does the trigonometric functions work with angles in c++?
a) Degrees
b) Radians
c) Both Degrees & Radians
d) None of the mentioned

View Answer

Answer: b [Reason:] The trigonometric functions work with angles in radians rather than degrees.

2. Which header file is required for manipulation of math functions in c++?
a) cmath
b) maths
c) math
d) none of the mentioned

View Answer

Answer: a [Reason:] #include is a header file required for manipulation of math functions.

3. How many macros are used by mathematical functions in the header file
?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] There are three macros used in mathematical functions. They are HUGE_VAL, EDOM, ERANGE.

4. What is the output of this program?

  1.     #include <stdio.h>
  2.     #include <math.h>
  3.     int main ()
  4.     {
  5.         int param, result;
  6.         int n;
  7.         param = 8.0;
  8.         result = frexp (param , &n);
  9.         printf ("%d n", param);
  10.         return 0;
  11.     }

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

View Answer

Answer: c [Reason:] In this program, We are breaking out the number by using the frexp function. Output: $ g++ math.cpp $ a.out 8

5. What is the output of this program?

  1.     #include <stdio.h>
  2.     #include <math.h>
  3.     int main ()
  4.     {
  5.         double param, result;
  6.         param = 5.5;
  7.         result = log (param);
  8.         printf ("%lf", result );
  9.         return 0;
  10.     }

a) 5.5
b) 3.14
c) 1.704
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are finding out the log value of param by using param function. Output: $ g++ math1.cpp $ a.out 1.704748

6. What is the output of this program?

  1.     #include <stdio.h>
  2.     #include <math.h>
  3.     int main ()
  4.     {
  5.         printf ("%lf", pow (7.0,3));
  6.         return 0;
  7.     }

a) 343
b) 343.00
c) 334
d) None of the mentioned

View Answer

Answer: b [Reason:] In this program, We are calculating the 7 power of 3 by using the powfunction. As we are using 7.0, it is producing the result in 343.00 Output: $ g++ math2.cpp $ a.out 343.000000

7. What is the output of this program?

  1.     #include <stdio.h>
  2.     #include <math.h>
  3.     int main ()
  4.     {
  5.         printf ("%lfn", fmod (5.3, 2) );
  6.         printf ("%lfn", fmod (18.5, 4.2) );
  7.         return 0;
  8.     }

a) 1.300
b) 1.700
c) 1.300
1.700
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are finding the remainder of the given values by using fmod function. Output: $ g++ math3.cpp $ a.out 1.300000 1.700000

8. What is the output of this program?

  1.     #include <stdio.h>
  2.     #include <math.h>
  3.     int main ()
  4.     {
  5.         printf ("%lf", fabs (-10.6) );
  6.         return 0;
  7.     }

a) 10
b) -10
c) 10.600000
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are finding the absolute value of -10.6 by using abs function. Output: $ g++ math4.cpp $ a.out 10.600000

9. Which of the following mathematical function is overloaded in <complex>
and <valarray>?
a) cos
b) tan
c) sin
d) mod

View Answer

Answer: b

10. How many parameters are used in frexp function?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: b [Reason:] There are two parameters that are used in frexp function. They are floating point value to be computed and pointer to an int object.

Set 5

Here is a listing of online C++ test questions on “Stored Sequences” along with answers, explanations and/or solutions:

1. What is meant by sequence point?
a) Represent the point of execution in the program
b) Represent the whole program
c) Represent the beginning of the program
d) None of the mentioned

View Answer

Answer: a [Reason:] A sequence point defines any point in a computer program’s execution at which it is guaranteed that all side effects of previous evaluations have been performed.

2. Pick out the correct statement about sequence point.
a) Sequence point will compile the program
b) Sequence point will resolve all the side effects
c) Sequence point will save the program for execution
d) None of the mentioned

View Answer

Answer: b [Reason:] Sequence point is a point in time at which the dust has settled and all side effects which have been seen so far are guaranteed to be complete.

3. In sequence point, how will the overloaded operators behave like?
a) Function
b) Objects
c) Instance variable
d) None of the mentioned

View Answer

Answer: a

4. What do input and output objects support?
a) Terminated sequences
b) Extracted sequences
c) Null-terminated sequences
d) None of the mentioned

View Answer

Answer: c [Reason:] cin and cout support null-terminated sequences as valid containers for sequences of characters.

5. What kind of execution does sequence point allow?
a) Non-overlap
b) Overlap
c) Concurrent
d) None of the mentioned

View Answer

Answer: a [Reason:] To resolve all the side-effects in the program, the sequence point should not be overlapped.

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string mys;
  7.         char mya[20]= "Hello world";
  8.         mys = mya;
  9.         cout << mys << 'n';
  10.         return 0;
  11.     }

a) Hello world
b) Hello
c) Error
d) Runtime error

View Answer

Answer: a [Reason:] In this program, We converted the char values into the string. Output: $ g++ sts.cpp $ a.out Hello world

7. 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.         for (int i = 1; i < 10; ++i) myvector.push_back(i);
  13.         vector<int> :: iterator bound;
  14.         bound = partition (myvector.begin(), myvector.end(), IsOdd);
  15.         for (vector<int> :: iterator it = myvector.begin(); it != bound; ++it)
  16.             cout << ' ' << *it;
  17.         return 0;
  18.     }

a) 1 3 5
b) 1 3 9
c) 1 9 3 7
d) 1 9 3 7 5

View Answer

Answer: d [Reason:] In this program, We are finding the odd values in the sequence. Output: $ g++ sts1.cpp $ a.out 1 9 3 7 5

8. When does the next sequence point start?
a) At the beginning
b) After a terminating semicolon
c) It is a beginning statement
d) None of the mentioned

View Answer

Answer: b