Computer Programming Exam Paper 2017
Computer Programming Exam Paper 2017
Dynamic memory management with pointers for 2D arrays involves a pointer to an array of pointers, where each sub-array can be dynamically allocated. This contrasts with single arrays which use a simple contiguous memory allocation. A 2D array utilizes pointers to manage rows, allowing non-contiguous allocation and more flexible memory utilization. This approach, unlike single arrays, supports variable-length rows and optimized memory usage for systems with fragmented memory .
'for' loops are preferred when the number of iterations is known prior to entry, initializing, testing, and updating in a compact format. 'while' loops evaluate condition before entering the loop body, making them suitable for repeated execution where the number of iterations depends on runtime conditions. 'do-while' loops ensure the loop executes at least once, useful when the loop body must run regardless of conditions. Each serves specific control flow needs, varying by predictability of iteration counts and conditions .
Operator precedence and associativity dictate the order in which parts of an expression are evaluated. Precedence determines the sequence in which operators are processed, where higher precedence operators are evaluated before lower precedence ones. Associativity determines the order operators of the same precedence level are processed, typically left-to-right or right-to-left. Misunderstanding these can lead to different execution results, impacting program correctness and efficiency .
In C programming, data types are crucial for defining the kind of value a variable can hold and how memory is allocated for it. Homogeneous data types, such as arrays, store data items of the same kind, which helps in performing statistical operations and data processing efficiently. Heterogeneous data types, like structures, allow storing different data types under one name, facilitating complex data manipulation and organization in applications such as databases or file systems .
Functions in C promote modularity by encapsulating code into reusable blocks with specific functionality, allowing cleaner code and improved maintenance. They enable developers to compartmentalize tasks, leading to reusable and testable units. This approach minimizes redundancy, eases debugging, and enhances collaboration by abstracting complexity. Parameters allow flexibility in function behavior, further enhancing code reusability and modular software development .
Text files are human-readable and useful for data that needs frequent human inspection but can introduce issues such as misinterpretation and require more storage space due to encoding. Binary files store raw data, offering higher storage efficiency and faster read/write operations, preserving exact data integrity, but lack readability without specific programs. Choosing between them depends on application needs regarding human interaction, storage constraints, and performance demands .
Structures in C face challenges in direct comparison due to potential differences in how C compilers align memory for structure members, which might include padding bytes to meet alignment requirements. This inconsistency makes direct binary comparison unreliable. Additionally, structures can contain complex data types such as pointers, which require deep comparisons that differ from shallow, byte-wise equivalent tests .
File handling in C is essential for data storage, retrieval, and manipulation, facilitating persistent data organization in text or binary form. The purpose of standard I/O operations like fopen(), fread(), and fwrite() is to simplify these interactions under a unified API. However, challenges like incorrect permissions can lead to runtime errors, failed access, or data corruption. Proper permissions and error handling are needed to ensure data integrity and program robustness .
A leap year occurs if a year is divisible by 400 or if it is divisible by 4 but not by 100. The divisibility by 400 rule addresses century years like 1600 and 2000, maintaining calendar alignment over longer periods while skipping non-century years by the 100 rule unless they fit the divisible by 4 rule. This ensures the calendar remains synchronized with Earth's revolutions around the Sun .
Recursion allows a function to call itself to solve subproblems, offering a straightforward implementation for problems like tree traversals. It simplifies code for complex algorithms but can lead to excessive memory use due to function call overhead and risk of stack overflow. Iteration offers more control over flow and avoids call overhead. Recursion is advantageous for naturally recursive problems, while iteration is preferred for performance-sensitive tasks .