C++ Programming MCQ – Bitset – 2

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Bitset – 2”.

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

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
	bitset<8> b1(20);
	cout<<b1.test(1);
	cout<<b1.test(2);
}

a) 01
b) 00
c) 10
d) 11
View Answer

Answer: a
Explanation: The test() function is used to check whether a bit is set to one. So as 20 is equivalent to 10100 which has bit at index 1 as 0 and bit at index 2 as 1 therefore the the output 0 for test(1) and 1 for test(2).
advertisement
advertisement

2. Which of the following is correct about any() function in bitset?
a) Returns true if the number of 1’s equal to the number of 0’s
b) Returns true if any bit is set 0
c) Returns true if any bit is set 1
d) Returns true only if all bits are 1
View Answer

Answer: c
Explanation: <bitset> header provides the any() function which returns true if any of the bit is set to 1 in the bitset variable.

3. Which of the following is correct about none() function in bitset?
a) Returns true if any bits is set 1
b) Returns true if all bits is set 0
c) Returns true if the number of 1’s equal to the number of 0’s
d) Returns true only if all bits are 1
View Answer

Answer: b
Explanation: <bitset> header provides the any() function which returns true if none of the bit is set to 1 in the bitset variable.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
	bitset<8> b1(20);
	cout<<b1.none();
	cout<<b1.any();
}

a) 01
b) 00
c) 10
d) 11
View Answer

Answer: a
Explanation: As 20 is equivalent to 10100 which has two bits which are 1 therefore none() fucntion will return false i.e. 0 and any() function will return true i.e. 1.
advertisement

5. What is the use of the set() function in bitset?
a) Used to make alternate bits zero
b) Used to make a bit 0 in a bitset
c) Used to make all bits zero
d) Used to set bit(s) in a bitset
View Answer

Answer: d
Explanation: <bitset> header provides the set() function to set bit(s) to 1 in a bitset variable.
advertisement

6. What happens when no argument is supplied to set() function?
a) All alternate bits are set to 1 in a bitset
b) All bits are set to 0 in a bitset
c) All bits are set to 1 in a bitset
d) First bit is set to 1
View Answer

Answer: c
Explanation: When no argument is supplied to set() function i.e. function is called with empty parameters then all the bits of the bitset is set to 1.

7. What happens when only one argument is supplied to set() function?
a) All bits are set to 1 in a bitset
b) Bit corresponding to an argument is set to 1
c) All alternate bits are set to 1 in a bitset
d) First bit is set to 1
View Answer

Answer: b
Explanation: When only one argument is supplied to set() function then bit corresponding to that index is set to 1.

8. What is the use of reset function in bitset?
a) Used to make alternate bits zero
b) Used to make a bit 0 in a bitset
c) Used to make all bits 1
d) Used to make a bit(s) 0 in a bitset
View Answer

Answer: d
Explanation: <bitset> header provides the reset() function to set bit(s) to 0 in a bitset variable.

9. What happens when no argument is supplied to reset() function?
a) All bits are set to 1 in a bitset
b) All bits are set to 0 in a bitset
c) All alternate bits are set to 0 in a bitset
d) First bit is set to 0
View Answer

Answer: b
Explanation: When no argument is supplied to reset() function i.e. function is called with empty parameters then all the bits of the bitset is set to 0.

10. What happens when only one argument is supplied to reset() function?
a) Bit corresponding to an argument is set to 0
b) All bits are set to 0 in a bitset
c) All alternate bits are set to 0 in a bitset
d) First bit is set to 0
View Answer

Answer: a
Explanation: When only one argument is supplied to reset() function then bit corresponding to that index is set to 0.

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.