Computer Programming Exam Paper 2022
Computer Programming Exam Paper 2022
Procedural programming languages focus on procedures or routines to operate on data, using a sequence of steps or procedures such as in C. Object-oriented programming languages, like Java, focus on objects that combine data and behavior. Procedural languages follow a top-down approach whereas object-oriented languages use a bottom-up approach with encapsulation, inheritance, and polymorphism as core concepts.
High-level programming languages are preferred because they are easier to read, write, and maintain. They provide abstraction from the hardware, allowing developers to focus more on algorithm development rather than the intricacies of machine code, leading to faster development cycles and easier debugging. High-level languages are portable across different hardware, which isn't the case with low-level languages.
The primary data types in C are int for integers, float for floating-point numbers, char for single characters, and double for double-precision floating-point numbers. These data types define the type and size of data associated with variables, crucial for memory allocation and computation precision.
The switch-case statement allows multi-way branching in C, improving code clarity when dealing with numerous conditions based on a single variable. Its syntax involves the keyword 'switch' followed by an expression in parentheses, with multiple 'case' labels offering different execution paths and an optional 'default' label for unmatched cases.
printf() is used to output data to the standard output device, typically the screen, using formatted strings, while scanf() reads formatted input from standard input, usually the keyboard. For example, 'printf("Enter a number: "); scanf("%d", &num);' displays a prompt for the user to input a number, then reads and stores it.
The 'while' loop evaluates its condition before executing the loop body, meaning the loop body may not be executed if the condition is false initially. In contrast, the 'do-while' loop executes the loop body once before evaluating the condition, ensuring the loop body is executed at least once regardless of the condition.
An array in C is a collection of elements of the same data type, stored at contiguous memory locations. A one-dimensional array is a linear list whereas a two-dimensional array can be thought of as a grid or table, which is a collection of rows and columns, facilitating matrix operations and complex data management.
An assembler is a tool that translates assembly language, a low-level language, into machine code, whereas a compiler translates a high-level programming language into machine code. Assemblers work with one-to-one mapping with machine instructions, whereas compilers generate several machine instructions for a single high-level language statement.
<math.h> includes mathematical functions like pow(), sqrt(), that assist in performing regular mathematical operations. <stdio.h> provides functions required for input and output operations, such as printf() and scanf(), essential for data handling and user interaction in programs.
Machine code is the set of instructions executed directly by a computer's CPU and is written in binary. Unlike high-level languages that are human-readable, machine code is hardware-specific and consists of binary instructions that the computer directly understands, providing the lowest level of control over the system resources.