C++ Programming MCQ – Bitset

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

1. What is bitset in C++?
a) An array of bools consuming one bit per element
b) Vector of bools
c) C-like arrays of bool elements
d) Template class

Explanation: Bitset is a collection of bool variables with each element consuming only one bit. They are introduced for efficient use of memories.

2. Which of the following is correct about bitset and vector of bools?
a) Space consumed by bitset is less than vector<bool>
b) Bitset consume only 1 bit per element
c) Number of elements in bitset should be known at compile time whereas vector can have a dynamic size
d) All of the mentioned

Explanation: Bitset consumes less space compared to bool vector however the size for bitset is static whereas for bool vector size is dynamic.

3. Which of the following is the limitation of bitset over vector bool?
a) Space
b) Size
c) Type
d) Speed

Explanation: Bitset size is static whereas vector size is dynamic therefore the size of a vector can be increased or decreased which is not possible in bitset.

4. Which operator is used to access the nth bit in a bitset?
a) ->
b) []
c) .
d) *

Explanation: [] operator is used to access the nth bit of a bitset from the right side. For example, if my bitset b is 1010 then b[0] represents 0 and b[1] represents 1.

5. How many ways are there for constructing a bitset?
a) 1
b) 2
c) 3
d) 4

Explanation: There are three ways of constructing a bitset. Direct construction, using integer number and using binary string.

6. Which is the correct syntax of constructing a bitset?
a) bitset<size> b;
b) bitset<size> b(12);
c) bitset<size> b(string(“1100”));
d) all of the mentioned

Explanation: All of the above mentioned are correct syntax of constructing a bitset. However each has different way of interpretation.

7. Which of the following is corect way of constructing bitset using integer number?
a) bitset<size> b;
b) bitset<size> b(12);
c) bitset<size> b(string(“1100”));
d) bitset<size> b(float(12));

Explanation: The correct way of constructing bitset using integer number is as follows:
bitset<size> b(integer_number);

8. Which of the following is corect way of constructing bitset using binary string?
a) bitset<size> b;
b) bitset<size> b(12);
c) bitset<size> b(string(“1100”));
d) bitset<size> b(float(12));

Explanation: The correct way of constructing bitset using binary string is as follows:
bitset<size> b(string(“1100”));

9. What is the default value of a bitset?
a) All bits are 0
b) All bits are 1
c) Leftmost bit is 0
d) Rightmost bit is 0

Explanation: By default, all the bits of a bitset variable is set to 0 i.e. the value of bitset variable is 0.

10. Which header file is required for using bitset in your program?
a) <bit>
b) <bitset>
c) <bits>
d) <BitSet>

Explanation: <bitset> header file is required to use the functionality of bitsets in your program i.e. use of binary string in your program.

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

```#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<8> b1(15);
cout<<b1;
}```

a) 00001111
b) 1111
c) 11110000
d) 0000000000001111

Explanation: As the size provided to bitset variable is 8, therefore, the number of bits that this variable will have is equal to 8. Now as 15 is 1111 so the least most significant digits will be 1111 to make it represent 15. Hence the answer is 00001111.

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

```#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<8> b1(string("15"));
cout<<b1;
}```

a) 00001111
b) Compile-time error
c) 11110000
d) Run-time error

Explanation: As we are using binary string way of constructing bitset and as 15 is not a binary string therfore the program gives an error.

13. Indexing of bitset variables starts from ___________
a) leftmost bit
b) rightmost bit
c) same as in an array
d) front

Explanation: The indexing of bitset variable starts from rightmost bit i.e. if you have b = 1100 as your bitset then b[0] = 0, not 1.

14. What is the use of count() function in bitset?
a) To count the number of 0’s
b) To count the number of 1’s
c) To count the number of total bits in a bitset
d) To count the number of low bits

Explanation: <bitset> header provides the count() function to count the number of 1’s or high bits in the bitset variable.

15. What does size() function returns?
a) To count the number of 0’s
b) To count the number of 1’s
c) To count the number of total bits used by bitset variable
d) To count the number of low bits

Explanation: <bitset> header provides the size() function to count the number of bits used by the bitset variable. For example, if bitset variable is 1010 then size() function returns 4.

More Bitset MCQ in C++ Programming:

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]