Challenge - 5 Problems
Function Parameters Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with pointer parameter
What is the output of this C program?
C
#include <stdio.h> void update(int *p) { *p = *p + 5; } int main() { int x = 10; update(&x); printf("%d\n", x); return 0; }
Attempts:
2 left
💡 Hint
Remember that passing a pointer allows the function to modify the original variable.
✗ Incorrect
The function 'update' receives a pointer to 'x' and adds 5 to the value it points to. So 'x' becomes 15.
❓ Predict Output
intermediate2:00remaining
Output of function with array parameter
What will this program print?
C
#include <stdio.h> void modify(int arr[], int size) { for (int i = 0; i < size; i++) { arr[i] += 1; } } int main() { int nums[3] = {1, 2, 3}; modify(nums, 3); for (int i = 0; i < 3; i++) { printf("%d ", nums[i]); } return 0; }
Attempts:
2 left
💡 Hint
Arrays passed to functions can be modified inside the function.
✗ Incorrect
The function adds 1 to each element of the array, so the output is '2 3 4 '.
❓ Predict Output
advanced2:00remaining
Output of function with const pointer parameter
What is the output of this program?
C
#include <stdio.h> void printValue(const int *p) { // *p = 10; // Uncommenting this line causes error printf("%d\n", *p); } int main() { int x = 7; printValue(&x); return 0; }
Attempts:
2 left
💡 Hint
The function only reads the value pointed to, it does not modify it.
✗ Incorrect
The function prints the value pointed to by 'p', which is 7. The commented line would cause a compile error if uncommented.
❓ Predict Output
advanced2:00remaining
Output of function with multiple parameters and default values (simulate)
What is the output of this program?
C
#include <stdio.h> void greet(char *name, int times) { for (int i = 0; i < times; i++) { printf("Hello, %s!\n", name); } } int main() { greet("Alice", 2); return 0; }
Attempts:
2 left
💡 Hint
C does not support default parameters, so both arguments must be passed.
✗ Incorrect
The function prints the greeting twice because 'times' is 2.
🧠 Conceptual
expert3:00remaining
Behavior of function parameters with struct pointers
Consider this code snippet. What will be the value of 'p.age' after calling 'updatePerson(&p);'?
C
typedef struct {
int age;
} Person;
void updatePerson(Person *person) {
person->age = 30;
}
int main() {
Person p = {25};
updatePerson(&p);
// What is p.age here?
return 0;
}Attempts:
2 left
💡 Hint
Passing a pointer to a struct allows modifying its fields inside the function.
✗ Incorrect
The function sets the 'age' field of the struct pointed to by 'person' to 30, so 'p.age' becomes 30.