C++ Programming Questions and Answers – 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?

advertisement
#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).

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.
advertisement

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.

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.
advertisement

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.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & 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, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn