0
0
Cprogramming~20 mins

Defensive programming practices - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Defensive Programming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of buffer overflow prevention code

What is the output of this C code that uses defensive programming to prevent buffer overflow?

C
#include <stdio.h>
#include <string.h>

int main() {
    char buffer[10];
    const char *input = "HelloWorld!";
    if (strlen(input) < sizeof(buffer)) {
        strcpy(buffer, input);
        printf("Buffer content: %s\n", buffer);
    } else {
        printf("Input too large for buffer\n");
    }
    return 0;
}
ABuffer content: HelloWorld!
BBuffer content: HelloWorl
CInput too large for buffer
DSegmentation fault
Attempts:
2 left
💡 Hint

Check the length of the input string compared to the buffer size.

🧠 Conceptual
intermediate
1:30remaining
Purpose of defensive null pointer checks

Why is it important to check if a pointer is NULL before using it in C?

ATo avoid dereferencing a null pointer which causes a crash
BTo improve program speed by skipping pointer usage
CTo automatically allocate memory for the pointer
DTo allow the pointer to point to multiple variables
Attempts:
2 left
💡 Hint

Think about what happens if you try to use a pointer that points to nothing.

🔧 Debug
advanced
2:30remaining
Identify the defensive programming mistake

What is the mistake in this code that tries to safely open a file?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "r");
    if (fp == NULL) {
        printf("File not found\n");
    }
    fprintf(fp, "Hello\n");
    fclose(fp);
    return 0;
}
AThe file is not closed after writing
BThe file is opened in read mode but written to
CThe file pointer is not checked for NULL before fclose
DThe code writes to the file even if fopen failed
Attempts:
2 left
💡 Hint

Look at what happens after the if (fp == NULL) check.

📝 Syntax
advanced
1:30remaining
Which option causes a compilation error?

Which of the following code snippets will cause a compilation error due to defensive programming misuse?

Aif ptr != NULL { *ptr = 10; }
Bif (ptr) *ptr = 10;
Cif (ptr != NULL) { *ptr = 10; }
Dif (ptr == NULL) { return; } *ptr = 10;
Attempts:
2 left
💡 Hint

Check the syntax of the if statement.

🚀 Application
expert
3:00remaining
Result of integer overflow check function

What is the output of this C program that uses defensive programming to check for integer overflow before addition?

C
#include <stdio.h>
#include <limits.h>

int safe_add(int a, int b, int *result) {
    if ((b > 0) && (a > INT_MAX - b)) {
        return 0; // overflow
    }
    if ((b < 0) && (a < INT_MIN - b)) {
        return 0; // underflow
    }
    *result = a + b;
    return 1; // success
}

int main() {
    int x = INT_MAX;
    int y = 1;
    int res;
    if (safe_add(x, y, &res)) {
        printf("Sum: %d\n", res);
    } else {
        printf("Overflow detected\n");
    }
    return 0;
}
ASum: 2147483647
BOverflow detected
CSum: -2147483648
DUndefined behavior
Attempts:
2 left
💡 Hint

Consider what happens when adding 1 to INT_MAX.