NEW
Font size
WorksheetsHacker Rank
Total questions: 40
Worksheet time: 40mins
What is the output of the following code?
cint x = 5, y = 2;
printf("%d", x / y);
2.5
2
3
2.0
What will be printed?
cint a = 10;
printf("%d %d %d", a, ++a, a++);
10 11 11
12 11 10
11 11 10
Output is undefined/compiler dependent
What is the output?
cint i = 5;
if(i = 10)
printf("Equal");
else
printf("Not Equal");
Equal
Not Equal
Compilation Error
No output
What will this print?
cint x = 1;
while(x <= 3) {
printf("%d ", x);
x++;
}
1 2 3
1 2 3 4
0 1 2 3
Infinite loop
What is the output?
cfor(int i = 0; i < 3; i++)
printf("%d ", i * i);
0 1 4
1 4 9
0 1 2
0 2 4
What will be printed?
cint a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("%d %d", a, b);
5 10
10 5
15 5
15 10
What is the output?
cint x = 10;
if(x > 5)
if(x < 15)
printf("A");
else
printf("B");
A
B
AB
Compilation Error
What will this code print?
cint n = 5;
do {
printf("%d ", n);
n--;
} while(n > 5);
5 4 3 2 1
5
No output
Infinite loop
What is the output?
cint arr[] = {1, 2, 3, 4, 5};
printf("%d", arr[3]);
3
4
5
Compilation Error
What will be printed?
cint x = 5;
printf("%d", x++ + ++x);
11
12
13
Undefined behavior
What is the output?
cchar c = 'A';
printf("%d", c);
A
65
97
Compilation Error
What will this print?
cint i;
for(i = 1; i <= 5; i++) {
if(i == 3)
continue;
printf("%d ", i);
}
1 2 3 4 5
1 2 4 5
1 2
3 4 5
What is the output?
cint x = 0;
if(x)
printf("True");
else
printf("False");
True
False
0
Compilation Error
How many times will "Hello" be printed?
cfor(int i = 0; i < 5; i++) {
if(i == 2)
break;
printf("Hello ");
}
2
3
4
5
What is the output?
cint n = 4;
printf("%d", n << 1);
2
4
8
16
What will be printed?
cint a = 5;
int b = (a > 3) ? 10 : 20;
printf("%d", b);
5
10
20
3
What is the output?
cint x = 10, y = 20;
if(x > 5 && y++ > 15)
printf("%d", y);
20
21
15
No output
What will this print?
cint num = 15;
switch(num % 3) {
case 0: printf("A"); break;
case 1: printf("B"); break;
default: printf("C");
}
A
B
C
ABC
What is the output?
cint a = 10, b = 20, c;
c = (a > b) ? a : b;
printf("%d", c);
10
20
0
30
How many times will the loop execute?
cint i = 5;
while(i--) {
printf("%d ", i);
}
4 times
5 times
6 times
Infinite
What is the output?
cint x = 5;
x += x++ + ++x;
printf("%d", x);
15
17
18
Undefined
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);
}
}
11 12 21 22
12 21 31 32
12 21 23 31 32
12 23 31 32
What is the output?
cint arr[5] = {1, 2, 3};
printf("%d", arr[4]);
0
Garbage value
3
Compilation Error
What will this print?
cint x = 10;
printf("%d %d %d", x, x << 1, x >> 1);
10 20 5
10 5 20
20 10 5
5 10 20
What is the output?
cint arr[] = {10, 20, 30, 40, 50};
printf("%d", *(arr + 2));
20
30
40
Compilation Error
What will this function return?
cint fun(int n) {
if(n == 1)
return 1;
return n + fun(n - 1);
}
// Called as: fun(5)
5
10
15
120
What is the output?
cvoid change(int x) {
x = 20;
}
int main() {
int a = 10;
change(a);
printf("%d", a);
}
10
20
30
Compilation Error
What will be printed?
cint arr[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
printf("%d", arr[1][2]);
5
6
8
9
What is the output?
cchar str[] = "Hello";
printf("%d", sizeof(str));
5
6
4
10
What will this function return?
cint mystery(int n) {
if(n <= 1)
return 1;
return n * mystery(n - 1);
}
// Called as: mystery(4)
4
10
24
16
What is the output?
cint arr[] = {5, 10, 15, 20};
int *ptr = arr;
printf("%d", *(ptr + 3));
5
15
20
Garbage value
What will be printed?
cchar str[10] = "ABC";
printf("%c", str[2]);
A
B
C
\0
What is the output?
cint x = 5, y = 10, z;
z = x > y ? x++ : y++;
printf("%d %d %d", x, y, z);
5 11 10
6 10 5
5 10 10
6 11 11
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);
153
154
9
15
What is the output?
cint a = 5, b = 10;
printf("%d", a & b);
0
5
10
15
What will be printed?
cfor(int i = 0; i < 5; i++) {
if(i % 2 == 0)
continue;
printf("%d ", i);
}
0 2 4
1 3
0 1 2 3 4
2 4
What is the output?
cint x = 1;
switch(x) {
case 1: printf("One ");
case 2: printf("Two ");
default: printf("Default");
}
One
One Two
One Two Default
Default
What will this print?
cint num = 7;
if(num % 2 == 0)
printf("Even");
else if(num % 2 == 1)
printf("Odd");
else
printf("Neither");
Even
Odd
Neither
No output
What is the output?
cint a = 10, b = 5;
printf("%d", a | b);
5
10
15
50
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);
3
4
5
10
