C Programming Exam Questions & Answers
C Programming Exam Questions & Answers
The 'continue' statement in C causes the current iteration of the loop to end immediately, and control jumps to the next iteration of the loop. In the provided code example from Source 1, when 'i' equals 2, the 'continue' statement skips the rest of the loop body and continues with the next cycle, resulting in the output "0,1,3,4,5,6,7,8,9," .
A 'do-while' loop guarantees execution of its block at least once because the condition is checked after the loop block. In contrast, a 'while' loop might not execute if the condition is false initially. For instance, when given an input of '-1 -1', the 'do-while' loop in Source 1 executes once before checking that 'i' equals '-1', counting 1 iteration, unlike a 'while' loop which would not execute at all under similar conditions .
To determine the length of a string using pointer arithmetic in C, iterate the pointer until it reaches the null character '\0' while incrementing the pointer. The difference between the final pointer position and the initial pointer gives the string length. The correct statement completing the code in Source 1 for this logic is 's++;' as it moves the pointer to the next character in the string .
An inversion in an array occurs when there are two elements such that the first element is greater than the second, and their indices follow an increasing order. Efficiently counting inversions involves using nested loops to iterate through the array: starting the outer loop from the first element and the inner from the next element to the current outer loop element. This structure allows a direct comparison and counting of inversions, as seen in Source 2's correct answer C, using initializations 'i=0, count=0' for the outer and 'j=i+1' for the inner loop .
In C, dynamic memory allocation for variable-sized arrays is performed using 'malloc' or 'calloc'. After allocating memory based on the required size, as shown in Source 1 with 'malloc(n*sizeof(int))', inputs are read into the array, and operations like 'print_array' can be performed using a size-based loop, as indicated by using 'count' as the loop boundary . This ensures proper memory access, and the allocated memory should be freed using 'free(a)' after its usage to prevent memory leaks.
When using the comma operator in C, the expression evaluates from left to right, but only the result of the rightmost expression is assigned. In the example from Source 1, 'k = i=2,j;' assigns the value '2' to both 'i' and 'k' because 'i=2' is evaluated before 'j', and 'j' is not assigned to either 'i' or 'k' .
Static variables in C retain their value between function calls because they are initialized only once and their value persists throughout the program's lifetime. For example, the code in Source 1 uses a static variable 'a' within the 'foo' function, which increments each time the function is called. Hence, it outputs '1' on the first call and '2' on the second call .
Mutually recursive functions are those that call each other. In C, such functions leverage call stacks to interleave execution. For instance, in the code from Source 1 with 'foo' and 'bar', 'foo(10)' calls 'bar(9)', which further calls 'foo(4)' and so on. This leads to an interleaved execution flow resulting in the output sequence '10,9,4,3,1,' as each function completes its logic and returns, deepening and unwinding the call stack alternately .
In C, when a 'switch' statement case does not have a 'break' keyword, the execution continues into the next case, regardless of whether the case matches. This is known as 'fall-through'. For example, the code provided in Source 1 prints "10" because, after printing "1" for 'case 1', it falls through to the 'default' case, which prints "0" .
The provided code attempts to swap integers using pointers but fails because it only swaps the local copies of the pointers, not the integers themselves. Swapping the local pointers 'ptra' and 'ptrb' doesn't affect 'a' and 'b', hence they print as '1,2', unchanged. A successful swap should directly modify the address contents using temporary storage for values, not pointers .