• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
Switch to Dark Mode

Top MCQs on Bitwise Algorithms and Bit Manipulations with Answers

Last Updated : Mar 22, 2024
Discuss
Comments

Question 1

What is the return value of following function for arr[] = {9, 12, 2, 11, 2, 2, 10, 9, 12, 10, 9, 11, 2} and n is size of this array. 

C++
int fun(int arr[], int n) {
    int x = arr[0];
    for (int i = 1; i < n; i++)
        x = x ^ arr[i];
    return x;
}
C
int fun(int arr[], int n)
{
    int x = arr[0];
    for (int i = 1; i < n; i++)
        x = x ^ arr[i];
    return x;
}
Java
public int fun(int[] arr) {
    int x = arr[0];
    for (int i = 1; i < arr.length; i++)
        x = x ^ arr[i];
    return x;
}
Python
def fun(arr):
    x = arr[0]
    for i in range(1, len(arr)):
        x ^= arr[i]
    return x
JavaScript
function fun(arr) {
    let x = arr[0];
    for (let i = 1; i < arr.length; i++)
        x ^= arr[i];
    return x;
}
  • A

    0

  • B

    9

  • C

    12

  • D

    2

Question 2

What does the following C expression do? x = (x<<1) + x + (x>>1);

  • A

    Multiplies an integer with 7

  • B

    Multiplies an integer with 3.5

  • C

    Multiplies an integer with 3

  • D

    Multiplies an integer with 8

Question 3

What does the following expression do?

 x = x & (x-1) 
  • A

    Sets all bits as 1

  • B

    Makes x equals to 0

  • C

    Turns of the rightmost set bit

  • D

    Turns of the leftmost set bit

Question 4

Consider the following code snippet for checking whether a number is power of 2 or not. 

C++
/* Incorrect function to check if x is power of 2*/
bool isPowerOfTwo (unsigned int x) 
{ 
  return (!(x&(x-1))); 
}
C
/* Incorrect function to check if x is power of 2*/
bool isPowerOfTwo (unsigned int x) 
{ 
  return (!(x&(x-1))); 
} 
Java
// Incorrect function to check if x is power of 2
public static boolean isPowerOfTwo(unsigned int x) {
  return (x & (x - 1)) == 0;
}
Python
# Incorrect function to check if x is power of 2
def is_power_of_two(x):
    return (x & (x - 1)) == 0
JavaScript
// Incorrect function to check if x is power of 2
function isPowerOfTwo(x) {
  return (x & (x - 1)) === 0;
}

What is wrong with above function?

  • A

    It does reverse of what is required

  • B

    It works perfectly fine for all values of x.

  • C

    It does not work for x = 0

  • D

    It does not work for x = 1

Question 5

What is the output of the following code snippet?

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

void fun(int& num, int k) { num &= (~(1 << k)); }
int main()
{
    int num = 7;
    int k = 1;
    fun(num, k);
    cout << num << endl;
    return 0;
}
C
#include <stdio.h>

void fun(int* num, int k) { *num &= (~(1 << k)); }
int main() {
    int num = 7;
    int k = 1;
    fun(&num, k);
    printf("%d\n", num);
    return 0;
}
Java
public class Main {
    public static void fun(int[] num, int k) { num[0] &= (~(1 << k)); }
    public static void main(String[] args) {
        int[] num = {7};
        int k = 1;
        fun(num, k);
        System.out.println(num[0]);
    }
}
Python
def fun(num, k):
    num[0] &= ~(1 << k)

num = [7]
k = 1
fun(num, k)
print(num[0])
JavaScript
function fun(num, k) {
    num[0] &= ~(1 << k);
}

let num = [7];
let k = 1;
fun(num, k);
console.log(num[0]);
  • A

    It will unset the all bits of num

  • B

    It will clear all the bits of bits

  • C

    It will unset the kth bit of num

  • D

    None

Question 6

what will do the following code in bit manipulation?

C++
int function(int n)
{
    if (n % 4 == 0)
        return n;
    if (n % 4 == 1)
        return 1;
    if (n % 4 == 2)
        return n + 1;
    else
        return 0;
}
C
int function(int n) {
    if (n % 4 == 0)
        return n;
    if (n % 4 == 1)
        return 1;
    if (n % 4 == 2)
        return n + 1;
    else
        return 0;
}
Java
public int function(int n) {
    if (n % 4 == 0)
        return n;
    if (n % 4 == 1)
        return 1;
    if (n % 4 == 2)
        return n + 1;
    else
        return 0;
}
Python
def function(n):
    if n % 4 == 0:
        return n
    if n % 4 == 1:
        return 1
    if n % 4 == 2:
        return n + 1
    else:
        return 0
JavaScript
function function(n) {
    if (n % 4 === 0)
        return n;
    if (n % 4 === 1)
        return 1;
    if (n % 4 === 2)
        return n + 1;
    else
        return 0;
}
  • A

    It will return the last set bit in a number.

  • B

    It will return the first set bit in a number.

  • C

    It will xor of two numbers.

  • D

    It will give the xor of numbers from 1 to N.

Question 7

Right shift(>>) and Left shift(<<) are equivalent to _____ by 2.

  • A

    Multiply and divide

  • B

    Divide and multiply

  • C

    Addition and subtraction

  • D

    Subtraction and addition

Question 8

You are given a list of 5 integers and these integers are in the range from 1 to 6. There are no duplicates in list. One of the integers is missing in the list. Which of the following expression would give the missing number. ^ is bitwise XOR operator. ~ is bitwise NOT operator. Let elements of list can be accessed as list[0], list[1], list[2], list[3], list[4]

  • A

    list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4]

  • B

    |list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4] ^ 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6

  • C

    list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4] ^ 1 ^ 2 ^ 3 ^ 4 ^ 5

  • D

    ~(list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4])

Question 9

If we want to invert all the bits of a number, then which bitwise operator should be used?

  • A

    ~

  • B

    &

  • C

    ^

  • D

    |

Question 10

What will the below code snippet do?

C++
#include <iostream>
using namespace std;
int main()
{
    int num = 5;
    cout << (~num + 1) << endl;
    return 0;
}
C
#include <stdio.h>
int main() {
    int num = 5;
    printf("%d\n", (~num + 1));
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        int num = 5;
        System.out.println(~num + 1);
    }
}
Python
num = 5
print(~num + 1)
JavaScript
let num = 5;
console.log(~num + 1);
  • A

    It will print 2's complement of a number.

  • B

    It will print 101.

  • C

    It will print 1.s complement of a number.

  • D

    None

Master DSA and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

Step into the Three 90 Challenge – 90 days to push limits, break barriers, and become the best version of yourself! Don't miss your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!

There are 30 questions to complete.

Take a part in the ongoing discussion