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) Both a & b
d) None of the mentioned
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 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
Explanation:None.

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
Explanation: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
Explanation: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
Explanation:In this program, We are finding the permutation in the loop and then printing the last permutated 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
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 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) Both a & b
d) None of the mentioned
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 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
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) vector_perm.h
d) vector_permutation
View Answer

Answer:c
Explanation:It is the header file, Where the vector permutation is defined.

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

Answer:c
Explanation:There are three header files needed for next_permutation. They are first, last and comp for comparing the elements.

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

Here’s the list of Best Reference Books in C++ Programming Language.

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

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


1000 C++ MCQs | Quiz

Types
Booleans
Character Types
Integer Types
Floating Point Types
Sizes
Void
Enumerations
Declaration
Pointers
Arrays
Pointers into Arrays
Constants
References
Pointer to Void
Structures
Operators
Statements
Comments and Indentation
Function Declarations
Argument Passing
Value Return
Overloaded Function Names
Default Arguments
Unspecified Number of Arguments
Pointer to Function
Macros
Modularization and Interfaces
Namespaces
Exceptions
Linkage
Header Files Usage
Classes
User Defined Types
Objects
Operator Functions
Complex Number Type
Conversion Operators
Friends
Large Objects
Essential Operators
Subscripting
Function Call
Dereferencing
Increment and Decrement
String Class
Derived Classes
Abstract Classes
Design of Class Hierarchies
Class Hierarchies & Abstract Classes
Simple String Template
Function Templates
Template Arguments for Policy Usage
Specialization
Derivation and Templates
Error Handling
Grouping of Exceptions
Catching Exceptions
Resource Management
Exceptions That Are Not Errors
Exception Specifications
Uncaught Exceptions
Exceptions and Efficiency
Error Handling Alternatives
Standard Exceptions
Class Hierarchies Introduction
Multiple Inheritance
Access Control
Run Time Type Information
Pointers to Members
Free Store
Standard Library Design
Container Design
Vector
Sequences
Sequence Adapters
Associative Containers
Almost Containers
Defining a New Container
Standard Library Algorithms
Sequences and Containers
Function Objects
Nonmodifying Sequence Algorithms
Modifying Sequence Algorithms
Stored Sequences
Heaps
Min and Max
Permutations
C Style Algorithms
Iterators and Sequences
Checked Iterators
Allocators
String Characters
Basic String
C Standard Library
Output Stream
Input Stream
Formatting
File Streams and String Streams
Buffering
Locale
C Input Output
Numeric Limits
Standard Mathematical Functions
Vector Arithmetic
Generalized Numeric Algorithms
Random Numbers
Follow Manish & Sanfoundry