Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

C-Volution (Hard)

Total questions: 10

Worksheet time: 10mins

Name
Class
Date
1.

What is the output of the following code? #include int main() { int i, j; for (i = 1; i <= 5; i++) { for (j = 1; j <= i; j++) { printf("%d ", j); } printf("\n"); } return 0; }

a)

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

b)

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

c)

1 2 3 4 5 1 2 3 4 1 2 3 1 2 1

d)

1 2 3 4 5 2 3 4 5 3 4 5 4 5 5

2.

What is the output of the following code? #include int main() { int i, j; for (i = 5; i >= 1; i--) { for (j = 1; j <= i; j++) { printf("*"); } printf("\n"); } return 0; }

a)

***** **** *** ** *

b)

* ** *** **** *****

c)

***** **** *** ** *

d)

* ** *** **** *****

3.

What is the error in the following code? #include int main() { int *ptr; *ptr = 10; printf("%d\n", *ptr); return 0;}

a)

Missing #include directive.

b)

Dereferencing an uninitialized pointer.

c)

Incorrect format specifier in printf().

d)

No error.

4.

What does the following code snippet do? #define SQUARE(x) x * x

a)

Defines a function named SQUARE.

b)

Defines a macro named SQUARE.

c)

Declares a variable named SQUARE.

d)

Includes a header file named SQUARE.

5.

What is the output of the following code? #include int main() { int i, j; for (i = 1; i <= 4; i++) { for (j = 1; j <= 4; j++) { if (i + j == 5) { printf("*"); } else { printf(" "); } } printf("\n"); } return 0; }

a)

* * * * * * *

b)

**** **** **** ****

c)

* * * *

d)

Nothing will be printed.

6.

What is the correct way to dynamically allocate memory for an array of 10 integers?

a)

int *arr = malloc(10 * sizeof(int));

b)

int *arr = calloc(10, sizeof(int));

c)

int *arr = new int[10];

d)

Both (a) and (b)

7.

What is the output of the following code? #include int main() { int i = 1; do { printf("%d ", i); i++; } while (i <= 5); printf("\n"); return 0; }

a)

1 2 3 4 5

b)

1 2 3 4 5 6

c)

1 2 3 4

d)

Infinite loop

8.

What is the output of the following code? #include int main() { int n = 5; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == 1 || i == n || j == 1 || j == n) { printf("*"); } else { printf(" "); } } printf("\n"); } return 0; }

a)

***** * * * * * * *****

b)

* * * * * * * * * * * * * * * *

c)

***** ***** ***** ***** *****

d)

* ** *** **** *****

9.

What is the output of the following code? #include int main() { int n = 4; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { printf("%c ", 'A' + j -1); } printf("\n"); } return 0; }

a)

A A B A B C A B C D

b)

A B C D E F G H I J

c)

A B B C C C D D D D

d)

A A B A B C A B C D

10.

What is the output of the following code? #include int main() { int i, j; for (i = 1; i <= 5; i++) { for (j = 1; j <= i; j++) { printf("%d ", j); } printf("\n"); } return 0; }

a)

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

b)

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

c)

1 2 3 4 5 1 2 3 4 1 2 3 1 2 1

d)

1 2 3 4 5 2 3 4 5 3 4 5 4 5 5