Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Hacker Rank 2

Total questions: 30

Worksheet time: 30mins

Name
Class
Date
1.

What is the output?

int x = 5, y = 9;

printf("%d", x ^ y ^ x);

a)

5

b)

9

c)

10

d)

14

2.

Result after stack operations:

push(10), push(20), pop(), push(30), push(40), pop(), pop()

a)

Stack contains: 10

b)

Stack contains: 10, 20

c)

Stack contains: 10, 30

d)

Stack is empty

3.

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); }

a)

5 10

b)

10 5

c)

5 5

d)

10 10

4.

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);

a)

5

b)

11

c)

15

d)

7

5.

Circular queue (size 6), front = 4, rear = 2.

How many elements can be inserted?**

a)

1

b)

2

c)

3

d)

4

6.

What is the output?

int arr[] = {5,10,15,20,25};

int *ptr = arr + 2;

printf("%d", (ptr++) + (++ptr));

a)

30

b)

35

c)

40

d)

45

7.

What will be printed?

int x = 10, y = 20;

int p = &x, q = &y;

p = q;

q = p;

*q = 30;

cout << x << " " << y;

a)

10 20

b)

20 20

c)

30 20

d)

30 30

8.

In postfix expression 6 2 3 + - 3 8 2 / + * 2 $ 3 +,

what is $ if result = 52?**

a)

Addition

b)

Multiplication

c)

Power

d)

Division

9.

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);

}

a)

O(n)

b)

O(n log n)

c)

O(n²)

d)

O(2^n)

10.

What is the output?

int x = 40;

int result = (x & (x - 1)) == 0 ? 1 : 0;

printf("%d", result);

a)

0

b)

1

c)

40

d)

39

11.

Inorder traversal after inserting:

50, 30, 70, 20, 40, 60, 80

a)

50 30 70 20 40 60 80

b)

20 30 40 50 60 70 80

c)

20 40 30 60 80 70 50

d)

50 30 20 40 70 60 80

12.

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(); }

a)

0

b)

1

c)

3

d)

Compilation Error

13.

Output of reverse("CODE")

void reverse(char *str){

    if(*str == '\0') return;

    reverse(str+1);

    printf("%c", *str);

}

a)

CODE

b)

EDOC

c)

CDOE

d)

Infinite recursion

14.

DLL 10 <-> 20 <-> 30 <-> 40 — pointer changes to delete 30?

a)

2

b)

4

c)

6

d)

8

15.

Count set bits in 29

while(n){

    n = n & (n - 1);

    count++;

}

a)

3

b)

4

c)

5

d)

29

16.

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)

A

b)

B

c)

AB

d)

Compilation Error

17.

First pass of selection sort on:

[64, 34, 25, 12, 22, 11, 90]

a)

[11, 34, 25, 12, 22, 64, 90]

b)

[11, 64, 34, 25, 12, 22, 90]

c)

[34, 25, 12, 22, 11, 64, 90]

d)

[11, 12, 25, 34, 22, 64, 90]

18.

What is the output?

int arr[2][3]={{1,2,3},{4,5,6}};

int *ptr = &arr[0][0];

ptr += 4;

printf("%d", *ptr);

a)

4

b)

5

c)

6

d)

Garbage

19.

Destructor output

class Base{ public: virtual ~Base(){ cout<<"~Base "; }};

class Derived: public Base{ public: ~Derived(){ cout<<"~Derived "; }};

Base *ptr = new Derived();

delete ptr;

a)

~Base

b)

~Derived

c)

~Derived ~Base

d)

~Base ~Derived

20.

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;

}

a)

11

b)

30

c)

25

d)

15

21.

Quadratic probing (mod 11), collision at 7

Next three positions?

a)

8, 9, 10

b)

8, 0, 3

c)

8, 11,5

d)

7, 7, 7

22.

Output of macro

#define MULTIPLY(a,b) a * b

int result = MULTIPLY(2+3, 4+5);

a)

45

b)

19

c)

23

d)

20

23.

Height of AVL after inserting:

10, 20, 30, 40, 50, 25

a)

2

b)

3

c)

4

d)

5

24.

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();

a)

10

b)

11

c)

12

d)

Compilation Error

25.

XOR Swap

int a=10,b=20;

p1 = p1 ^ *p2;

p2 = p1 ^ *p2;

p1 = p1 ^ *p2;

a)

10 20

b)

20 10

c)

30 30

d)

0 0

26.

BFS traversal — next after S

Graph: S→A, S→B, A→C, B→C, B→D

a)

A or B

b)

Always A

c)

Always C

d)

Always D

27.

Output of recursive function

fun(5)

a)

5 3 1 1 3 5

b)

5 3 1 3 5

c)

1 3 5 5 3 1

d)

5 3 1

28.

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();

a)

Compilation Error

b)

B

c)

A

d)

Runtime Error

29.

Selection sort partial iteration

arr = {3,1,4,1,5,9,2};

if(i==2) break;

print arr[2];

a)

2

b)

3

c)

4

d)

1

30.

What is the output?

int x = 5;

int y = (x << 2) | (x >> 1);

printf("%d", y);

a)

20

b)

22

c)

21

d)

23