C++ Programming Questions and Answers – Permutations

This section on tough C++ programming questions focuses on “Permutations”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ programming questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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) To delete all the values
View Answer

Answer: b
Explanation: 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 lexicographically to each other elements
b) By finding the highest element in the range
c) By finding the lowest element in the range
d) By deleting the lowest element in the range
View Answer

Answer: a
Explanation: In c++ permutations can be ordered by comparing lexicographically to each other elements.

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) If the function can determine the lower higher permutation, Returns true
View Answer

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

4. What will be the output of the following C++ code?

  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
Explanation: In the program, We used string permutation to find out all the combination.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ perm.cpp
$ a.out
ter
tre
etr
ert
rte
ret

5. What will be the output of the following C++ code?

advertisement
  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
Explanation: In this program, We are doing the permutation in the do while loop and then printing last permuted value.
Output:

advertisement
$ g++ perm1.cpp
$ a.out
1 2 3

6. What will be the output of the following C++ code?

  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
Explanation: 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 will be the output of the following C++ code?

  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) 3 3
View Answer

Answer: c
Explanation: 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 will be the output of the following C++ code?

  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)

355
535
553
View Answer
Answer: d
Explanation: 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
Explanation: 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
Explanation: There are 2 or 3 needed for next_permutation. They are first, last and optional comp for comparing the elements.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.