wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Hacker Rank

Total questions: 40

Worksheet time: 40mins

Name
Class
Date
1.

⁠ ⁠What is the output of the following code?

cint x = 5, y = 2;

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

a)

2.5

b)

2

c)

3

d)

2.0

2.

What will be printed?

cint a = 10;

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

a)

10 11 11

b)

12 11 10

c)

11 11 10

d)

Output is undefined/compiler dependent

3.

What is the output?

cint i = 5;

if(i = 10)

    printf("Equal");

else

    printf("Not Equal");

a)

Equal

b)

Not Equal

c)

Compilation Error

d)

No output

4.

What will this print?

cint x = 1;

while(x <= 3) {

    printf("%d ", x);

    x++;

}

a)

1 2 3

b)

1 2 3 4

c)

0 1 2 3

d)

Infinite loop

5.

What is the output?

cfor(int i = 0; i < 3; i++)

    printf("%d ", i * i);

a)

0 1 4

b)

1 4 9

c)

0 1 2

d)

0 2 4

6.

What will be printed?

cint a = 5, b = 10;

a = a + b;

b = a - b;

a = a - b;

printf("%d %d", a, b);

a)

5 10

b)

10 5

c)

15 5

d)

15 10

7.

What is the output?

cint x = 10;

if(x > 5)

    if(x < 15)

        printf("A");

else

    printf("B");

a)

A

b)

B

c)

AB

d)

Compilation Error

8.

What will this code print?

cint n = 5;

do {

    printf("%d ", n);

    n--;

} while(n > 5);

a)

5 4 3 2 1

b)

5

c)

No output

d)

Infinite loop

9.

What is the output?

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

printf("%d", arr[3]);

a)

3

b)

4

c)

5

d)

Compilation Error

10.

What will be printed?

cint x = 5;

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

a)

11

b)

12

c)

13

d)

Undefined behavior

11.

What is the output?

cchar c = 'A';

printf("%d", c);

a)

A

b)

65

c)

97

d)

Compilation Error

12.

What will this print?

cint i;

for(i = 1; i <= 5; i++) {

    if(i == 3)

        continue;

    printf("%d ", i);

}

a)

1 2 3 4 5

b)

1 2 4 5

c)

1 2

d)

3 4 5

13.

What is the output?

cint x = 0;

if(x)

    printf("True");

else

    printf("False");

a)

True

b)

False

c)

0

d)

Compilation Error

14.

How many times will "Hello" be printed?

cfor(int i = 0; i < 5; i++) {

    if(i == 2)

        break;

    printf("Hello ");

}

a)

2

b)

3

c)

4

d)

5

15.

What is the output?

cint n = 4;

printf("%d", n << 1);

a)

2

b)

4

c)

8

d)

16

16.

What will be printed?

cint a = 5;

int b = (a > 3) ? 10 : 20;

printf("%d", b);

a)

5

b)

10

c)

20

d)

3

17.

What is the output?

cint x = 10, y = 20;

if(x > 5 && y++ > 15)

    printf("%d", y);

a)

20

b)

21

c)

15

d)

No output

18.

What will this print?

cint num = 15;

switch(num % 3) {

    case 0: printf("A"); break;

    case 1: printf("B"); break;

    default: printf("C");

}

a)

A

b)

B

c)

C

d)

ABC

19.

What is the output?

cint a = 10, b = 20, c;

c = (a > b) ? a : b;

printf("%d", c);

a)

10

b)

20

c)

0

d)

30

20.

How many times will the loop execute?

cint i = 5;

while(i--) {

    printf("%d ", i);

}

a)

4 times

b)

5 times

c)

6 times

d)

Infinite

21.

What is the output?

cint x = 5;

x += x++ + ++x;

printf("%d", x);

a)

15

b)

17

c)

18

d)

Undefined

22.

What will be printed?

cfor(int i = 1; i <= 3; i++) {

    for(int j = 1; j <= 2; j++) {

        if(i == j)

            break;

        printf("%d%d ", i, j);

    }

}

a)

11 12 21 22

b)

12 21 31 32

c)

12 21 23 31 32

d)

12 23 31 32

23.

What is the output?

cint arr[5] = {1, 2, 3};

printf("%d", arr[4]);

a)

0

b)

Garbage value

c)

3

d)

Compilation Error

24.

What will this print?

cint x = 10;

printf("%d %d %d", x, x << 1, x >> 1);

a)

10 20 5

b)

10 5 20

c)

20 10 5

d)

5 10 20

25.

What is the output?

cint arr[] = {10, 20, 30, 40, 50};

printf("%d", *(arr + 2));

a)

20

b)

30

c)

40

d)

Compilation Error

26.

What will this function return?

cint fun(int n) {

    if(n == 1)

        return 1;

    return n + fun(n - 1);

}

// Called as: fun(5)

a)

5

b)

10

c)

15

d)

120

27.

What is the output?

cvoid change(int x) {

    x = 20;

}

int main() {

    int a = 10;

    change(a);

    printf("%d", a);

}

a)

10

b)

20

c)

30

d)

Compilation Error

28.

What will be printed?

cint arr[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

printf("%d", arr[1][2]);

a)

5

b)

6

c)

8

d)

9

29.

What is the output?

cchar str[] = "Hello";

printf("%d", sizeof(str));

a)

5

b)

6

c)

4

d)

10

30.

What will this function return?

cint mystery(int n) {

    if(n <= 1)

        return 1;

    return n * mystery(n - 1);

}

// Called as: mystery(4)

a)

4

b)

10

c)

24

d)

16

31.

What is the output?

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

int *ptr = arr;

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

a)

5

b)

15

c)

20

d)

Garbage value

32.

What will be printed?

cchar str[10] = "ABC";

printf("%c", str[2]);

a)

A

b)

B

c)

C

d)

\0

33.

What is the output?

cint x = 5, y = 10, z;

z = x > y ? x++ : y++;

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

a)

5 11 10

b)

6 10 5

c)

5 10 10

d)

6 11 11

34.

What will this print?

cint n = 153;

int sum = 0;

while(n > 0) {

    int digit = n % 10;

    sum += digit digit digit;

    n /= 10;

}

printf("%d", sum);

a)

153

b)

154

c)

9

d)

15

35.

What is the output?

cint a = 5, b = 10;

printf("%d", a & b);

a)

0

b)

5

c)

10

d)

15

36.

What will be printed?

cfor(int i = 0; i < 5; i++) {

    if(i % 2 == 0)

        continue;

    printf("%d ", i);

}

a)

0 2 4

b)

1 3

c)

0 1 2 3 4

d)

2 4

37.

What is the output?

cint x = 1;

switch(x) {

    case 1: printf("One ");

    case 2: printf("Two ");

    default: printf("Default");

}

a)

One

b)

One Two

c)

One Two Default

d)

Default

38.

What will this print?

cint num = 7;

if(num % 2 == 0)

    printf("Even");

else if(num % 2 == 1)

    printf("Odd");

else

    printf("Neither");

a)

Even

b)

Odd

c)

Neither

d)

No output

39.

What is the output?

cint a = 10, b = 5;

printf("%d", a | b);

a)

5

b)

10

c)

15

d)

50

40.

What will be the value of count?

cint count = 0;

for(int i = 1; i <= 10; i++) {

    if(i % 3 == 0)

        count++;

}

printf("%d", count);

a)

3

b)

4

c)

5

d)

10