Top MCQs on Bitwise Algorithms and Bit Manipulations with Answers

Last Updated :
Discuss
Comments

Question 1

Which bitwise operator is used to swap two numbers?

  • Left Shift(<<)

  • Right shift(>>)

  • Bitwise OR |

  • Bitwise XOR ^

Question 2

What is the output of the below code for input num = 15?

C++
unsigned int fun(unsigned int n)
{
    unsigned int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
C
unsigned int fun(unsigned int n) {
    unsigned int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
Java
public class Main {
    public static int fun(int n) {
        int count = 0;
        while (n != 0) {
            count += n & 1;
            n >>= 1;
        }
        return count;
    }
}
Python
def fun(n):
    count = 0
    while n:
        count += n & 1
        n >>= 1
    return count
JavaScript
function fun(n) {
    let count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
  • 15

  • 5

  • 4

  • 3

Question 3

Which is the format specifier used to prefix 0x and print a number in hexadecimal notation.?

  • %O

  • %#

  • %x

  • %#x

Question 4

Which is the Bit Toggling operator below.?

  • Bitwise OR operator( | )

  • Bitwise XOR Operator (^)

  • Bitwise AND Operator(&)

  • TILDE operator(~)

Question 5

What is the output of the below expression? 

000111 | 01010
  • 0000000

  • 00

  • 010111

  • None

Question 6

Bitwise & can be used in conjunction with ~ operator to turn off 1 or more bits in a number.

  • True

  • False

Question 7

If we have to add two numbers without having any carry, which bitwise operator should be used?

  • Bitwise OR |

  • Bitwise AND &

  • Bitwise NOT ~

  • None

Question 8

Predict the output:

C++
#include <iostream>
using namespace std;

int main()
{
    int x = -5;
    x = x >> 1;
    cout << x << endl;
    return 0;
}
C
#include <stdio.h>

int main() {
    int x = -5;
    x = x >> 1;
    printf("%d\n", x);
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        int x = -5;
        x = x >> 1;
        System.out.println(x);
    }
}
Python
# x is initialized to -5
x = -5
# Right shift operation
x = x >> 1
# Print the result
print(x)
JavaScript
// x is initialized to -5
let x = -5;
// Right shift operation
x = x >> 1;
// Print the result
console.log(x);
  • -2

  • -3

  • Error

  • -1

Question 9

What is the output of the below code?

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
    if (~0 == 1)
        cout << "Yes";
    else
        cout << "No";
}
C
#include <stdio.h>
int main() {
    if (~0 == 1)
        printf("Yes");
    else
        printf("No");
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        if (~0 == 1)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
Python
if ~0 == 1:
    print("Yes")
else:
    print("No")
JavaScript
if (~0 === 1)
    console.log("Yes");
else
    console.log("No");
  • Yes 

  • No

  • Error

  • None

Question 10

What will the below code do?

C++
void function(int& num, int pos) 
{
   num |= (1 << pos); 
}
C
void setBit(int* num, int pos) {
   *num |= (1 << pos); 
}
Java
void setBit(int[] num, int pos) {
   num[0] |= (1 << pos); 
}
Python
def set_bit(num, pos):
    return num | (1 << pos)
JavaScript
function setBit(num, pos) {
   return num | (1 << pos);
}
  • divide num by pos.

  • Multiply num by pos.

  • Add num with pos.

  • set the given position (pos) of a number (num).

There are 30 questions to complete.

Take a part in the ongoing discussion