0
0
Cprogramming~20 mins

Function parameters - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Parameters Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A15
B10
C5
DCompilation error
Attempts:
2 left
💡 Hint
Remember that passing a pointer allows the function to modify the original variable.
Predict Output
intermediate
2: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;
}
A1 2 3
B0 1 2
C2 3 4
DCompilation error
Attempts:
2 left
💡 Hint
Arrays passed to functions can be modified inside the function.
Predict Output
advanced
2: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;
}
ARuntime error
B10
CCompilation error due to assignment to const pointer
D7
Attempts:
2 left
💡 Hint
The function only reads the value pointed to, it does not modify it.
Predict Output
advanced
2: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;
}
A
Hello, Alice!
Hello, Alice!
B
Hello, Alice!
CCompilation error: default parameters not supported
DRuntime error
Attempts:
2 left
💡 Hint
C does not support default parameters, so both arguments must be passed.
🧠 Conceptual
expert
3: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;
}
A25
B30
CUndefined behavior
DCompilation error
Attempts:
2 left
💡 Hint
Passing a pointer to a struct allows modifying its fields inside the function.