WorksheetsHacker Rank 2
Total questions: 30
Worksheet time: 30mins
What is the output?
int x = 5, y = 9;
printf("%d", x ^ y ^ x);
5
9
10
14
Result after stack operations:
push(10), push(20), pop(), push(30), push(40), pop(), pop()
Stack contains: 10
Stack contains: 10, 20
Stack contains: 10, 30
Stack is empty
What is the output?
class Base {
int x;
public:
Base(int a = 0) : x(a) { cout << x << " "; }
};
class Derived : public Base {
public:
Derived(int b) : Base(b * 2) { cout << b << " "; }
};
int main() { Derived d(5); }
5 10
10 5
5 5
10 10
What is the output?
int fun(int n){
if(n==0) return 0;
if(n==1) return 1;
return fun(n-1)+fun(n-2)+1;
}
fun(4);
5
11
15
7
Circular queue (size 6), front = 4, rear = 2.
How many elements can be inserted?**
1
2
3
4
What is the output?
int arr[] = {5,10,15,20,25};
int *ptr = arr + 2;
printf("%d", (ptr++) + (++ptr));
30
35
40
45
What will be printed?
int x = 10, y = 20;
int p = &x, q = &y;
p = q;
q = p;
*q = 30;
cout << x << " " << y;
10 20
20 20
30 20
30 30
In postfix expression 6 2 3 + - 3 8 2 / + * 2 $ 3 +,
what is $ if result = 52?**
Addition
Multiplication
Power
Division
Time complexity of:
void fun(int n){
if(n <= 1) return;
for(int i = 0; i < n; i++)
printf("%d ", i);
fun(n/2);
fun(n/2);
}
O(n)
O(n log n)
O(n²)
O(2^n)
What is the output?
int x = 40;
int result = (x & (x - 1)) == 0 ? 1 : 0;
printf("%d", result);
0
1
40
39
Inorder traversal after inserting:
50, 30, 70, 20, 40, 60, 80
50 30 70 20 40 60 80
20 30 40 50 60 70 80
20 40 30 60 80 70 50
50 30 20 40 70 60 80
What is the output?
class Test{
static int count;
public:
Test(){ count++; }
static int getCount(){ return count; }
};
int Test::count = 0;
int main(){ Test t1, t2, t3; cout << Test::getCount(); }
0
1
3
Compilation Error
Output of reverse("CODE")
void reverse(char *str){
if(*str == '\0') return;
reverse(str+1);
printf("%c", *str);
}
CODE
EDOC
CDOE
Infinite recursion
DLL 10 <-> 20 <-> 30 <-> 40 — pointer changes to delete 30?
2
4
6
8
Count set bits in 29
while(n){
n = n & (n - 1);
count++;
}
3
4
5
29
What is printed?
class A{ public: void show(){ cout<<"A"; }};
class B: public A{ public: void show(){ cout<<"B"; }};
A *ptr = new B();
ptr->show();
A
B
AB
Compilation Error
First pass of selection sort on:
[64, 34, 25, 12, 22, 11, 90]
[11, 34, 25, 12, 22, 64, 90]
[11, 64, 34, 25, 12, 22, 90]
[34, 25, 12, 22, 11, 64, 90]
[11, 12, 25, 34, 22, 64, 90]
What is the output?
int arr[2][3]={{1,2,3},{4,5,6}};
int *ptr = &arr[0][0];
ptr += 4;
printf("%d", *ptr);
4
5
6
Garbage
Destructor output
class Base{ public: virtual ~Base(){ cout<<"~Base "; }};
class Derived: public Base{ public: ~Derived(){ cout<<"~Derived "; }};
Base *ptr = new Derived();
delete ptr;
~Base
~Derived
~Derived ~Base
~Base ~Derived
Output of mystery(5,6)
int mystery(int a,int b){
if(b==0) return 0;
if(b%2==0) return mystery(a+a,b/2);
return mystery(a+a,b/2)+a;
}
11
30
25
15
Quadratic probing (mod 11), collision at 7
Next three positions?
8, 9, 10
8, 0, 3
8, 11,5
7, 7, 7
Output of macro
#define MULTIPLY(a,b) a * b
int result = MULTIPLY(2+3, 4+5);
45
19
23
20
Height of AVL after inserting:
10, 20, 30, 40, 50, 25
2
3
4
5
What is the output?
class Test{
int x;
public:
Test(int a):x(a){}
void operator++(){ x+=1; }
void display(){ cout<<x; }
};
Test t(10);
++t; ++t;
t.display();
10
11
12
Compilation Error
XOR Swap
int a=10,b=20;
p1 = p1 ^ *p2;
p2 = p1 ^ *p2;
p1 = p1 ^ *p2;
10 20
20 10
30 30
0 0
BFS traversal — next after S
Graph: S→A, S→B, A→C, B→C, B→D
A or B
Always A
Always C
Always D
Output of recursive function
fun(5)
5 3 1 1 3 5
5 3 1 3 5
1 3 5 5 3 1
5 3 1
Pure virtual function call
class A{ public: virtual void fun() = 0; };
class B: public A{ public: void fun(){ cout<<"B"; }};
A *ptr = new B();
ptr->fun();
Compilation Error
B
A
Runtime Error
Selection sort partial iteration
arr = {3,1,4,1,5,9,2};
if(i==2) break;
print arr[2];
2
3
4
1
What is the output?
int x = 5;
int y = (x << 2) | (x >> 1);
printf("%d", y);
20
22
21
23
