0
0
Cprogramming~20 mins

Use cases - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
C Use Cases Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pointer arithmetic in array traversal
What is the output of this C program that uses pointer arithmetic to traverse an array?
C
#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40};
    int *p = arr;
    printf("%d\n", *(p + 2));
    return 0;
}
A10
B20
C30
D40
Attempts:
2 left
💡 Hint
Remember that pointer arithmetic moves by the size of the data type.
🧠 Conceptual
intermediate
1:30remaining
Use case of static variables inside functions
What is the main use case of a static variable declared inside a function in C?
ATo keep the variable value persistent between function calls
BTo make the variable accessible globally
CTo allocate memory dynamically
DTo declare a constant variable
Attempts:
2 left
💡 Hint
Think about what happens to a normal local variable after the function ends.
🔧 Debug
advanced
2:30remaining
Identify the error in this memory allocation use case
What error will this C code produce when run?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = malloc(5 * sizeof(int));
    ptr[5] = 10;
    printf("%d\n", ptr[5]);
    free(ptr);
    return 0;
}
AMemory leak error
BSegmentation fault due to out-of-bounds access
CCompilation error due to missing header
DNo error, prints 10
Attempts:
2 left
💡 Hint
Check the valid index range for the allocated memory.
📝 Syntax
advanced
1:30remaining
Which option correctly declares a function pointer?
Which of the following is the correct syntax to declare a pointer to a function that takes an int and returns void?
Avoid (*funcPtr)(int);
Bvoid *funcPtr(int);
Cint (*funcPtr)(void);
Dvoid funcPtr(*int);
Attempts:
2 left
💡 Hint
Function pointers use parentheses around *name.
🚀 Application
expert
2:00remaining
Determine the output of this recursive use case
What is the output of this C program that uses recursion to calculate factorial?
C
#include <stdio.h>

int factorial(int n) {
    if (n <= 1) return 1;
    else return n * factorial(n - 1);
}

int main() {
    printf("%d\n", factorial(4));
    return 0;
}
A1
B10
C120
D24
Attempts:
2 left
💡 Hint
Factorial of 4 is 4*3*2*1.