Data Structures & Algorithms Multiple Choice Questions on “Generating Subsets”.
1. What is meant by the power set of a set? Answer: b 2. Number of elements in the power set of set S={1,2,3} will be? Answer: d 3. Number of elements in the power set of set S={1,2,2} will be? Answer: c 4. Choose the correct statement for the following code segment? a) function returns true if N is odd Answer: c 5. What will be the output for the following code? a) a,b,ab,c,ac,bc,abc, Answer: c 6. What will be the time complexity of the following code? a) O(n 2n) Answer: a 7. What will be the auxiliary space requirement of the following code? a) O(n) Answer: b 8. Which of the following code prints the power set of a given set? b) c) d) Answer: c 9. The number of elements in the power set increases when there are duplicates present in the set. Answer: b 10. What of the following code prints the power set of a given set? b) c) d) Answer: a
a) subset of all sets
b) set of all subsets
c) set of particular subsets
d) empty set
Clarification: Power set of a set is defined as the set of all subsets. Ex- S={1,2} then P={{},{1},{2}{1,2}}.
a) 2
b) 4
c) 6
d) 8
Clarification: Power set of a set is defined as the set of all subsets. Number of elements in the power set of a set having n elements is given as 2n. Thus, here number of elements will be 23=8.
a) 2
b) 4
c) 6
d) 8
Clarification: For finding the number of elements in the power set of the given set we need to remove duplicates. So we will be left with 6 unique elements which will be P={{},{1},{2},{1,2},{2,2},{1,2,2}}.bool check (int N)
{
if( N & (1 << i) )
return true;
else
return false;
}
b) function returns true if N is even
c) function returns true if ith bit of N is set
d) function returns false if ith bit of N is set
Clarification: As the value of 1 << i is 2i so the given function checks whether the ith bit of N is set or not. If it is set then the function returns true.#include
b) a,b,ab,c,ac,bc,abc
c) ,a,b,ab,c,ac,bc,abc,
d) ,abc,bc,ac,c,ab,b,a,
View Answer
Clarification: The given code prints the elements of power set of the given set strset[]. It uses binary counter of appropriate length in order to print corresponding subsets of the given set.#include
b) O(n2)
c) O(n log n)
d) O(2n) (n is the size of set)
Clarification: In the given code we have a nested for loop. In this loop the outer loop runs 2n times and the inner loop runs n times. So the overall time complexity becomes n2n.#include
b) O(1)
c) O(n log n)
d) O(2n) (n is the size of set)
View Answer
Clarification: The given code prints the elements of power set of the given set strset[]. As this code does not require any extra space for generating the output so its auxiliary space requirement will be O(1).
a)#include
#include
#include
#include
Clarification: In the correct version we are taking two cases for each element. In the first case the element is included in the subset and in the second case it is not included. So in this manner, we find all the subsets of the given set.
a) True
b) False
Clarification: In case when duplicates are present in the set then the number of elements in the power set decreases. It is because we remove subsets with identical elements.
a)#include
#include
#include
#include
Clarification: In the correct version of the code we are fixing a prefix and generate all corresponding subsets. Then after all subsets with a prefix are generated, we replace last character with one of the remaining character.