Computer Programming Exam Questions
Computer Programming Exam Questions
In a nested block structure with shadow declarations, each block can have its own separate scope for variables with the same name. The output of such a program segment depends on which variable (shadowed or original) is accessed in each block. For instance, if a variable 'a' is declared in both the main block and a nested block, the inner block accesses its own 'a', while the outer block accesses the original 'a'. Therefore, the output would show values defined by the scope from which each 'a' is accessed .
To determine if a year is a leap year, the algorithm checks divisibility conditions: a year is a leap year if divisible by 4, except for end-of-century years, which must also be divisible by 400. The algorithm involves: 1) Checking if the year is divisible by 400 (leap year), 2) Else, checking if divisible by 100 (not a leap year), 3) Else, checking if divisible by 4 (leap year), and 4) Not satisfying any is not a leap year. This logic effectively captures leap year rules based on the Gregorian calendar .
Understanding the difference between a function's declaration (prototype) and its definition is crucial because it distinguishes between specifying what a function does and actually implementing it. A declaration introduces the function's signature to the compiler, typically outside main, enabling usage in different files, while the definition contains the executable code inside the function body. Example: `int add(int, int);` is a prototype, and `int add(int a, int b){ return a + b; }` is its definition, ensuring modular code and separate compilation units .
A good algorithm is defined by the properties of correctness, efficiency, finiteness, and clarity. Correctness ensures that the algorithm produces the correct output for all valid inputs. Efficiency refers to the appropriate use of computational resources, like time and memory. Finiteness means that the algorithm must terminate after a finite number of steps. Clarity implies that the algorithm should be easy to understand and follow, making it maintainable and modifiable .
In C programming, a variable is a storage location identified by a memory address and a symbolic name, allowing programmers to store and manipulate data. The comma ',' is commonly used to separate multiple variable names declared in a single statement .
Pointer arithmetic in C involves operations relative to the size of the data type being pointed to. Unlike regular arithmetic that increments values by 1, pointer arithmetic increases a pointer's address by the size of its base type. For instance, incrementing an int* moves 4 bytes forward on a 32-bit system. This feature is particularly useful for iterating through array elements elegantly with pointer incrementation instead of index-based access .
Syntax errors occur when code deviates from the grammatical rules of the programming language, typically detected during compilation, preventing the program from running. Semantic errors occur when code executes but does not perform as intended, usually due to incorrect logic or operation flow. Syntax errors stop the program from running, while semantic errors lead to unexpected results during execution .
An array of pointers is a collection, each element of which is a pointer to a variable, used for storing addresses, for instance, of strings. In contrast, a pointer to an array points to an entire array of variables. For example, `int *ptrArr[5];` can store addresses of int variables, whereas `int (*arrPtr)[5];` points to an int array. This distinction provides flexibility in data management strategies, allowing access to elements or entire collections efficiently .
Logical operators in C are used to perform logical operations on expressions, enabling decision-making in code. Three primary logical operators are the AND operator '&&', which returns true if both operands are true; the OR operator '||', which returns true if at least one operand is true; and the NOT operator '!', which inverts the boolean value of its operand. These operators help in controlling the flow of execution based on multiple conditions .
The if-else statement in programming facilitates decision-making by evaluating a condition; if the condition is true, a specific block of code is executed, otherwise, the alternative block (else) is executed. The execution flow begins by evaluating the expression in the if condition. If true, it immediately executes the code within the if block. If false, it skips the if block and executes the code inside the else block, effectively dictating alternate execution paths based on conditions .