C++ Programming MCQ – Bitset – 3

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

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

Answer: a
Explanation: <bitset> header provides the flip() function to flip bit(s) in a bitset variable i.e. change the bits in a bitset for example 1100 on flipping becomes 0011.

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

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

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

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

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

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
	bitset<8> b1(95);
	bitset<8> b2(46);
	cout<<(b1^b2);
}

a) 00001101
b) 11111000
c) 01111111
d) 01110001
View Answer

Answer: d
Explanation: ^ operator is used to take xor of two bitset variables i.e. if ith bit in both varaibles are either 1 or 0 then answer is 0 otherwise answer is 1.

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

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
	bitset<8> b1(95);
	bitset<8> b2 = b1 << 3;
	cout<<b2;
}

a) 01110001
b) 11111000
c) 01111111
d) 00001101
View Answer

Answer: b
Explanation: << operator is used to shift bits towards left side so if we have 1111 then on shifting this by 2 i.e. 1111 << 2 will result into 1100.

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

advertisement
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
	bitset<8> b1(95);
	bitset<8> b2 = b1 >> 3;
	cout<<b1<<endl<<b2;
}

a) 00001011
b) 11111000
c) 01111111
d) 00001101
View Answer

Answer: a
Explanation: >> operator is used to shift bits towards right side so if we have 1111 then on shifting this by 2 i.e. 1111 >> 2 will result into 0011.

7. Which operator is used as not operator in bitset?
a) |
b) &
c) ~
d) ^
View Answer

Answer: c
Explanation: ~ operator is used as not operator i.e. the negation of a bit for bitset variables in C++.

8. Which operator is used to take AND of two bitset variables?
a) ~
b) &
c) |
d) ^
View Answer

Answer: b
Explanation: & operator is used as AND operator for bitset variables in C++. ANDing of two bits are 1 only if both are 1.

9. Which operator is used to take OR of two bitset variables?
a) ~
b) &
c) |
d) ^
View Answer

Answer: c
Explanation: | operator is used as OR operator for bitset variables in C++. ORing of two bits are 0 only if both are 0.

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

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
	bitset<8> b1(95);
	bitset<8> b2(45);
	cout<<~b1<<endl;
	cout<<(b1|b2)<<endl;
	cout<<(b1&b2)<<endl;
}

a)

10100000
01111111
00001101

b)

11111111
01111111
00001101

c)

10100000
00001111
00001101

d)

10100000
01111111
00000000
View Answer
Answer: a
Explanation: In this program we are doing not of b1. AND of b1 and b2 and OR of b1 and b2, answer to them are 10100000, 01111111 and 00001101.
 
 

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.