Challenge - 5 Problems
C Use Cases Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember that pointer arithmetic moves by the size of the data type.
✗ Incorrect
The pointer p points to the first element of arr. Adding 2 moves it to the third element, which is 30.
🧠 Conceptual
intermediate1:30remaining
Use case of static variables inside functions
What is the main use case of a static variable declared inside a function in C?
Attempts:
2 left
💡 Hint
Think about what happens to a normal local variable after the function ends.
✗ Incorrect
Static variables inside functions keep their value between calls, unlike normal local variables which are reinitialized each time.
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the valid index range for the allocated memory.
✗ Incorrect
The array allocated has indices 0 to 4. Accessing ptr[5] is out of bounds and causes a runtime error.
📝 Syntax
advanced1: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?
Attempts:
2 left
💡 Hint
Function pointers use parentheses around *name.
✗ Incorrect
Option A correctly declares funcPtr as a pointer to a function taking int and returning void.
🚀 Application
expert2: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; }
Attempts:
2 left
💡 Hint
Factorial of 4 is 4*3*2*1.
✗ Incorrect
The factorial function multiplies numbers from 4 down to 1, resulting in 24.