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
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
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
Explanation: When only one argument is supplied to flip() function then bit corresponding to that index only is flipped.
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
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?
#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
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?
#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
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
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
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
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 00000000View Answer
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]
- Practice Programming MCQs
- Apply for C++ Internship
- Practice Computer Science MCQs
- Check Programming Books
- Check Computer Science Books