



























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This exam prepares learners to analyze and enhance software maintainability in C programs. Topics include modular design, naming conventions, code documentation, complexity reduction, error handling, debugging techniques, refactoring patterns, and maintainability metrics. The exam is filled with case studies that require identifying weaknesses in legacy C code and proposing improvements.
Typology: Exams
1 / 99
This page cannot be seen from the preview
Don't miss anything!




























































































Question 1. Which ISO/IEC standard defines software maintainability as “the degree of effectiveness and efficiency with which a product can be modified”? A) ISO/IEC 12207 B) ISO/IEC 25010 C) ISO/IEC 15504 D) ISO/IEC 27001 Answer: B Explanation: ISO/IEC 25010 includes maintainability as one of the quality characteristics of software products. Question 2. In the context of software economics, which factor most directly increases the cost of maintenance? A) High initial development effort B) Low defect density after release C) Accumulated technical debt D) Use of open‑source libraries Answer: C Explanation: Technical debt creates hidden work that must be paid back during maintenance, raising overall cost. Question 3. Which type of maintenance is performed to adapt a program to a new operating system version? A) Corrective maintenance B) Adaptive maintenance C) Perfective maintenance
D) Preventive maintenance Answer: B Explanation: Adaptive maintenance deals with changes required by the environment, such as OS upgrades. Question 4. Refactoring code to improve future changeability without adding new functionality is an example of: A) Corrective maintenance B) Adaptive maintenance C) Perfective maintenance D) Preventive maintenance Answer: D Explanation: Preventive maintenance aims to improve the internal quality (e.g., refactoring) to ease future work. Question 5. Which of the following statements best describes the “single responsibility principle” (SRP) for C functions? A) A function should have only one entry point. B) A function should perform exactly one well‑defined task. C) A function should not call any other functions. D) A function must be declared static. Answer: B Explanation: SRP states that a module (or function) should have one reason to change, i.e., a single responsibility.
Answer: A Explanation: Replacing raw literals with named constants makes the code self‑documenting and easier to modify. Question 9. Which of the following is a drawback of excessive global variables? A) They reduce compilation time. B) They increase module cohesion. C) They create hidden dependencies across files. D. They enforce encapsulation. Answer: C Explanation: Globals can be accessed anywhere, leading to hidden coupling and making reasoning about code harder. Question 10. Which control‑flow construct is generally discouraged in C because it reduces structured programming clarity? A) for loop B) while loop C) switch statement D) goto statement Answer: D Explanation: goto can create spaghetti code; structured constructs are preferred for readability. Question 11. The ternary operator ?: should be used primarily when:
A) It replaces a whole function. B) It improves readability of a simple conditional assignment. C) It eliminates the need for any if statements. D) It can be nested arbitrarily. Answer: B Explanation: The ternary operator is concise for simple assignments; overuse or complex nesting harms readability. Question 12. Which of the following is a safe pattern for allocating memory in C? A) ptr = malloc(size); *ptr = 0; without checking the return value. B) ptr = malloc(size); if (!ptr) exit(EXIT_FAILURE); C) ptr = malloc(size); free(ptr); free(ptr); D) ptr = malloc(size); ptr = realloc(ptr, newSize); without storing the result. Answer: B Explanation: Always check the return value of malloc; exiting on failure prevents dereferencing a NULL pointer. Question 13. A memory leak occurs when: A) Memory is freed twice. B) Memory is allocated but never freed before the program terminates. C) A pointer is assigned to NULL. D) malloc returns a non‑NULL pointer. Answer: B
B) Using assert(ptr != NULL); in production code. C) Checking if (ptr == NULL) return ERROR_CODE; before dereferencing. D) Ignoring the pointer and using a global variable instead. Answer: C Explanation: Explicitly validating parameters prevents undefined behavior if the caller passes an invalid pointer. Question 17. Which macro definition can cause unexpected side effects when used with arguments that have side effects? A) #define SQUARE(x) ((x)*(x)) B) #define MAX(a,b) ((a) > (b)? (a) : (b)) C) #define INCREMENT(x) ++x D) #define SET_FLAG(p) ((p)->flag = 1) Answer: A Explanation: SQUARE(x) evaluates x twice; if x has side effects (e.g., i++), the side effect occurs twice. Question 18. When should a const variable be preferred over a #define macro for a constant value? A) When the constant is needed in preprocessor conditionals. B) When type safety and debugging information are desired. C) When the constant must be concatenated with other tokens. D) When the constant is used only inside a single function. Answer: B
Explanation: const provides type checking and appears in the debugger, whereas macros are simple text substitution. Question 19. Cyclomatic complexity of a function that contains two if statements and one for loop is: A) 1 B) 2 C) 3 D) 4 Answer: C Explanation: Complexity = E – N + 2P; each decision point adds one. Two ifs + one for = 3. Question 20. A high cyclomatic complexity value generally indicates: A) Better performance. B) Higher maintainability. C) More independent execution paths, making the code harder to test. D) Fewer lines of code. Answer: C Explanation: More decision points increase possible paths, raising testing effort and reducing maintainability. Question 21. Which metric combines lines of code, cyclomatic complexity, and comment density to produce a single maintainability score? A) Halstead Volume
Question 24. Integrating static analysis into a CI pipeline primarily helps: A) Reduce the need for version control. B) Ensure every build meets quality gates before merging. C. Eliminate all runtime errors. D. Replace unit testing. Answer: B Explanation: CI integration runs analysis automatically, preventing low‑quality code from entering the main branch. Question 25. Which comment style is recommended for explaining “why” a piece of code exists rather than “what” it does? A) Inline comment next to every statement. B) Block comment describing the algorithm in detail. C) Comment that states the intent, not the literal operation. D) No comments at all. Answer: C Explanation: “Why” comments convey design rationale, which is not obvious from the code itself. Question 26. In API documentation for a C function, which element is essential? A) The exact number of CPU cycles the function uses. B) A description of each parameter, return value, and possible error codes. C) The file path where the function is defined.
D) The name of the developer who wrote the function. Answer: B Explanation: Clear parameter and return documentation enables correct usage and easier maintenance. Question 27. Which testing practice directly improves code testability? A) Writing large monolithic functions. B) Increasing coupling between modules. C) Designing functions with low coupling and high cohesion. D) Removing all comments. Answer: C Explanation: Low coupling and high cohesion make units independent, simplifying isolated testing. Question 28. Test‑driven development (TDD) starts with: A) Writing production code first, then tests. B) Writing failing unit tests before implementing the functionality. C. Refactoring existing code without tests. D. Generating documentation automatically. Answer: B Explanation: TDD’s Red‑Green‑Refactor cycle begins with a failing test that defines the required behavior. Question 29. Code coverage of 80 % means:
Question 32. When replacing a magic number with a symbolic constant, which keyword is preferred in modern C for type safety? A) #define B) enum C. const D. static Answer: C Explanation: const variables have type information and are respected by the debugger, unlike simple macros. Question 33. A descriptive commit message should include: A) Only the ticket number. B) A brief summary, the rationale, and any side effects. C. The entire diff of the change. D. The author's full name and phone number. Answer: B Explanation: Good commit messages help future developers understand why a change was made. Question 34. Which practice during code reviews most directly improves maintainability? A. Focusing solely on performance optimizations. B. Enforcing naming conventions and checking for duplicated logic. C. Ignoring style and only looking for syntax errors.
D. Approving all changes without comments. Answer: B Explanation: Reviewing for naming, duplication, and style catches maintainability issues early. Question 35. Which of the following is a benefit of using forward declarations instead of including full header files? A) It eliminates the need for a linker. B) It reduces compilation dependencies, lowering coupling. C. It makes the code run faster at runtime. D. It automatically generates documentation. Answer: B Explanation: Forward declarations limit header inclusion, decreasing compile‑time coupling between modules. Question 36. In C, the inline keyword is primarily used to: A) Force the compiler to inline a function regardless of optimization settings. B) Suggest that a small function may be expanded at call sites, reducing function‑call overhead. C. Declare a variable that lives forever. D. Replace macros entirely. Answer: B Explanation: inline hints the compiler to embed the function body, often improving performance and readability compared to macros. Question 37. Which of the following statements about malloc and calloc is true?
Explanation: Properly checking and handling the return value ensures robust error handling. Question 40. The “DRY” principle stands for: A. “Don’t Repeat Yourself”. B. “Debug Rapidly, Yield”. C. “Define Reusable Yields”. D. “Directly Refactor Your code”. Answer: A Explanation: DRY encourages eliminating duplicated logic to improve maintainability. Question 41. Which of the following is a safe way to share a constant value across multiple C source files? A. Define it with #define in each file separately. B. Declare it as extern const int MAX_SIZE; in a header and define it in one source file. C. Use a static variable in each file. D. Write the literal value directly wherever needed. Answer: B Explanation: Using extern const provides a single definition while allowing other files to reference it. Question 42. A function that returns a pointer to a dynamically allocated array must: A. Free the memory before returning. B. Document that the caller is responsible for free‑ing the returned pointer. C. Return a pointer to a local stack variable.
D. Never be called more than once. Answer: B Explanation: Ownership transfer must be clearly documented to avoid leaks. Question 43. Which of the following is the most appropriate use of a macro in C? A. Implementing complex arithmetic expressions with side effects. B. Defining a compile‑time constant that may be used in preprocessor conditionals. C. Replacing a function that takes pointers. D. Creating a generic container. Answer: B Explanation: Macros are useful for compile‑time constants and conditional compilation; complex logic should be in functions. Question 44. When measuring maintainability, why is Lines of Code (LOC) considered a limited metric? A. It does not account for code complexity or quality. B. It is impossible to count. C. It directly measures performance. D. It is only useful for assembly language. Answer: A Explanation: LOC counts size but ignores complexity, duplication, and readability, so it alone is insufficient. Question 45. Which of the following best describes “low coupling” in a C program?
Explanation: assert is compiled out when NDEBUG is defined, so it is intended for debugging, not production error handling. Question 48. Which practice helps keep function signatures simple and maintainable? A. Passing large structures by value. B. Using many optional parameters with default values. C. Passing a pointer to a struct that groups related data. D. Overloading functions based on parameter count. Answer: C Explanation: Grouping related parameters into a struct reduces the number of arguments and clarifies intent. Question 49. Which of the following is an advantage of using enum instead of a series of #define constants for related integer values? A. Enums are processed by the preprocessor. B. Enums provide scoped, type‑checked constants. C. Enums automatically generate documentation. D. Enums reduce binary size. Answer: B Explanation: Enumerations give the compiler type information and limit values to the defined set. Question 50. When a C function needs to report multiple error conditions, the recommended approach is: A. Return a single integer where each bit encodes a different error.
B. Use errno exclusively. C. Return an enum value describing the specific error. D. Print the error to stderr and exit. Answer: C Explanation: Returning an enum makes the error explicit and type‑safe, improving readability and testability. Question 51. Which of the following statements about static variables inside a function is true? A. They are re‑initialized on each call. B. They retain their value between calls, providing function‑level persistence. C. They are visible to other translation units. D. They must be declared extern. Answer: B Explanation: static inside a function gives the variable internal linkage with static storage duration. Question 52. A “large class/file” smell in C is often addressed by: A. Adding more global variables. B. Splitting the file into multiple modules with focused responsibilities. C. Removing all comments. D. Converting the file to a binary library. Answer: B