Recall & Review
beginner
What is defensive programming?
Defensive programming is a way to write code that expects and safely handles unexpected problems or errors to prevent crashes or bugs.
Click to reveal answer
beginner
Why should you check function inputs in C?
Checking inputs helps catch wrong or dangerous values early, avoiding crashes or security problems later in the program.
Click to reveal answer
intermediate
How can you handle errors in C defensively?
Use return codes or error flags to detect problems, and always check these before continuing to avoid unexpected behavior.
Click to reveal answer
intermediate
What is the purpose of assertions in defensive programming?
Assertions check if assumptions in code are true during development. If not, they stop the program to help find bugs early.
Click to reveal answer
beginner
Give an example of a defensive programming practice in C.
Before using a pointer, check if it is NULL to avoid crashes. For example: if (ptr == NULL) { handle error; }
Click to reveal answer
What is a key goal of defensive programming?
✗ Incorrect
Defensive programming aims to prevent crashes by safely handling unexpected inputs or errors.
Which of these is a defensive programming practice in C?
✗ Incorrect
Checking pointers for NULL before use helps avoid crashes and is a defensive practice.
What does an assertion do in C?
✗ Incorrect
Assertions check conditions during development and stop the program if the condition is false.
Why should you validate inputs in your functions?
✗ Incorrect
Validating inputs helps catch invalid data early, preventing errors later.
Which is NOT a defensive programming practice?
✗ Incorrect
Ignoring error codes is unsafe and not a defensive programming practice.
Explain what defensive programming is and why it is important in C.
Think about how to keep your program safe from unexpected problems.
You got /3 concepts.
Describe three defensive programming techniques you can use in C code.
Consider how to check data and handle errors before they cause crashes.
You got /3 concepts.