wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Code Carnival

Total questions: 25

Worksheet time: 13mins

Name
Class
Date
1.

What will be the output of the following code?


#include<stdio.h>

int main() {

    int x = 3;

    if (x == 3 || x == 2 * 2 && x != 5) {

        printf("Hello\n");

    } else { printf("World\n");

    }

    return 0;

}

a)
Hello
b)
World
c)
Compilation Error
d)
Hello World
2.

How many times will the loop execute? 


#include<stdio.h>

int main() {

    int i;

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

        if (i % 2 == 0) {

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

        }

    }

   return 0;

}

a)
2 4
b)
2 3 6
c)
2 4 6
d)
Infinite loop
3.

What will be the output of the following code?


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

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

a)
0
b)
3
c)
Garbage value
d)
Compilation error
4.

Given the following declaration in C:

int arr[5];

float arr2[3];

Which of the following statements correctly calculates the total number of bytes required to store these arrays, considering the typical memory sizes for int and float (assuming int is 4 bytes and float is 4 bytes on your system)?

a)
sizeof(arr) + sizeof(arr2)
b)
sizeof(arr) * sizeof(arr2)
c)
sizeof(arr) + sizeof(arr2)) / 2
d)
(sizeof(arr) + sizeof(arr2)) - sizeof(int)
5.

What will be the output of the following code?


#include <stdio.h> 

int main() { 

    char str[5] = "Hello"; 

    printf("%s", str); 

    return 0; 

}

a)
Hello
b)
Compilation error
c)
Undefined behavior
d)
H
6.

What is the output?  

int x = 10;  
switch (x) {  
    case 5: printf("Five");  
    case 10: printf("Ten");  
    case 15: printf("Fifteen");  
}  

a)
Five
b)
Ten
c)
TenFifteen
d)
Compilation error
7.

Which function is used to find the length of a string?

a)
strlen()
b)
sizeof()
c)
strlength()
d)
length()
8.

Which of the following loop constructs will result in an infinite loop if the condition is never updated inside the loop body?

a)
for (int i = 0; i < 10; i--)
b)
while (x < 10) { x += 2; }
c)
do { y--; } while (y > 0);
d)
for (int i = 0; i < 10; i++) { if (i == 5) break; }
9.

What is the output?  


#include <stdio.h>  
int main() {  
    int arr[5] = {1, 3, 5, 7, 9};  

    arr[2] = arr[4] - arr[0];  
    arr[4] = arr[1] + arr[3];  

    for (int i = 0; i < 5; i++) {  
        printf("%d ", arr[i]);  
    }  
    return 0;  
}

a)
1 3 5 7 9
b)
1 3 8 7 11
c)
1 3 8 7 10
d)

1 3 4 6 10

10.

Find the output of the following code:  


int arr[] = {1, 2, 3, 4, 5};  
printf("%d", (arr[2] & arr[3]) | (arr[2] ^ arr[3]));  

a)
5
b)
6
c)
7
d)
8
11.

What is sizeof(arr)/sizeof(arr[0]) for int arr[5]?

a)
4
b)
5
c)
20
d)
Compilation error
12.

What happens if an array index goes out of bounds in C?

a)
Compiler detects it and stops execution
b)
Undefined behavior
c)
Initializes to 0
d)
Returns last element of array
13.

What is the output?  


char str[ ] = "Hello";  
printf("%s", str + 1 + 2);  

a)
Hello
b)
He
c)
lo
d)
Compilation error
14.

What will happen if a function in C returns the address of a local variable?

a)
The function executes successfully and returns the correct value
b)
Compiler detects it and throws an error
c)
Undefined behavior
d)
The returned address always points to 0
15.

What will be printed when the following code is executed?  


char str[ ] = "Programming";  
printf("%s", str + sizeof(char) * 2);  

a)
The entire string is printed.
b)
The first two characters are omitted and the rest are printed.
c)
Only the first two characters are printed.
d)
Compilation error occurs.
16.

Which of the following functions compares two strings lexicographically and ensures that the comparison stops as soon as a difference is found, but also guarantees the correct comparison of binary data (including null bytes) within the strings?

a)
strcmp()
b)
strncmp()
c)
memcmp()
d)
strcoll()
17.

What is wrong with this code?  


for(int i = 0; i < 5; i++);  
printf("%d ", i);  

a)
Nothing, prints 0 1 2 3 4
b)
Prints 5 times
c)
Only prints once
d)
Compilation error
18.

What will be the output of the following code snippet?  


int arr[3] = {'A', 'B', 'C'};  
printf("%d", arr[0] + arr[1] + arr[2]);  

a)
198
b)
199
c)
200
d)
Compilation error
19.

Find the error in the following program:  


#include <stdio.h>  

int sum(int a, int b) {  
    if (a + b > 10)  
        return;  
    return a + b;  
}  

int main() {  
    int result = sum(5, 6);  
    printf("%d", result);  
    return 0;  
}  

a)
No error, prints 11
b)
Compilation error due to missing return type in sum()
c)
Undefined behavior due to missing return statement in sum()
d)
The output will always be 0
20.

What will be the output of the following code?  


for (char ch = 'A'; ch <= 'E'; ch++) {  
    printf("%c ", ch);  
}  

a)
A B C D E
b)
A B C D
c)
A C E
d)
A B C D E F
21.

Find the output of the following code snippet:  


int arr[] = {5, 10, 15, 20};  
int sum = 0, i;  
for(i = 0; i < 4; i += 2)  
    sum += arr[i];  
printf("%d", sum);  

a)
5
b)
20
c)
25
d)
50
22.

What will be the output of the following C program?  


#include <stdio.h>  

int main() {  
    int i = 1;  
   
    while (i <= 5) {  
        if (i % 2 == 0)  
            continue;  
        printf("%d ", i);  
        i++;  
    }  

    return 0;  
}

a)
The program prints only odd numbers between 1 and 5.
b)
The program prints all numbers from 1 to 5.
c)
The program enters an infinite loop due to a missing increment.
d)
The program fails to compile due to incorrect loop syntax.
23.

Find the output of the following:  


char str[] = "Code";  
str[1] = 'A';  
printf("%s", str);  

a)
Code
b)
CAde
c)
Compilation error
d)
Runtime error
24.

Spot the error in this code snippet:  


#include <stdio.h>  
int main() {  
    const int age;  
    age = 30;  
    printf("%d", age);  
    return 0;  
}

a)
Uninitialized constant
b)
Missing return statement
c)
Syntax error in printf statement
d)
No error
25.

How does a compiler differ from an interpreter?

a)
A compiler executes code, while an interpreter does not
b)
A compiler debugs code, while an interpreter does not
c)
A compiler translates the entire program at once, while an interpreter translates it line by line
d)
A compiler is used only in C programming, while an interpreter is not