IPA FE Subject A sample Question 9
A question IPA Japan published in 基本情報技術者試験 科目A サンプル問題 問9, translated into English. ITPEC is based on the same exams.
Which of the following is an appropriate item to be specified in the coding standards of a program?
Explanation
Write the processing for when a memory allocation fails — coding standards set rules that make programs reliable and easy to read, and handling a failed call is one of them.
A memory allocation function (such as malloc in C) returns an error value, typically a null pointer, when it cannot reserve the area. A program that uses the result without checking it crashes or corrupts memory at run time, so coding standards require the result to be checked and the failure to be handled.
Answer (d)
Why not others:
- •(a) — reusing one local variable for different purposes because the type matches makes the code hard to read and invites mistakes; standards ask for one variable per purpose, with a meaningful name
- •(b) — floating-point loop counters accumulate rounding errors, so the loop may run once too often or too rarely, and they are not faster; standards require integer control variables
- •(c) — recursion for a repeated calculation costs a call and stack space each time and can overflow the stack; a loop is usually preferred, and recursion is never a rule a standard imposes for this
Key rule: Coding standards aim at readability, maintainability and reliability: one purpose per variable, integer loop counters, and a check on every call that can fail.
Algorithm and Programming · AI-written, may contain errors
This question comes from the past questions IPA Japan publishes, translated into English by ITPEC Practice. ITPEC Practice is an independent study tool and is not affiliated with IPA or ITPEC. See the original paper (PDF) or Report an issue.