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.
int fun(int arr[], int n)
{
int x = arr[0];
for (int i = 1; i < n; i++)
x = x ^ arr[i];
return x;
}
Question 4
Consider the following code snippet for checking whether a number is power of 2 or not.
/* Incorrect function to check if x is power of 2*/
bool isPowerOfTwo (unsigned int x)
{
return (!(x&(x-1)));
}
What is wrong with above function?
Question 5
What is the output of the following code snippet?
#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;
}
Question 6
what will do the following code in bit manipulation?
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;
}
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]
Question 9
If we want to invert all the bits of a number, then which bitwise operator should be used?
Question 10
What will the below code snippet do?
#include <iostream>
using namespace std;
int main()
{
int num = 5;
cout << (~num + 1) << endl;
return 0;
}
There are 30 questions to complete.