4th Flashcards

1
Q

Q1: Global vs. Local Variables – Original Question and Full Answer

A

Compare global and local variables in C. Global variables are declared outside any function and have program-wide scope, a lifetime equal to the program’s duration, and are stored in the data segment; local variables are declared within functions/blocks, have limited scope, exist only during execution, and are stored on the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q1: What is a global variable in C?

A

A global variable is declared outside any function, has program-wide scope, remains allocated for the entire program, and is typically stored in the data segment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q1: What is a local variable in C?

A

A local variable is declared inside a function or block, has a scope limited to that function/block, exists only during the execution of that block, and is stored on the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q1: How do memory allocations differ for global and local variables?

A

Global variables are allocated in the data segment and persist for the life of the program, whereas local variables are allocated on the stack and are deallocated when the function/block ends.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q1: What is the scope of a global variable?

A

The scope of a global variable extends throughout the entire program, making it accessible in any function after its declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q1: What is the lifetime of a local variable?

A

The lifetime of a local variable is limited to the execution of the block or function in which it is declared; it is created when the block is entered and destroyed when it is exited.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q1: Provide a C code example that demonstrates both global and local variables.

A

“Example:\n\n#include <stdio.h>\n\n// Global variable\nint globalVar = 10;\n\nvoid display() {\n // Local variable\n int localVar = 20;\n printf(\"Inside display(): Global = %d</stdio.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q2: Factorial Using Recursion – Original Question and Full Answer

A

Write a C program that computes the factorial of a given number using recursion. The recursive function defines factorial(n) as n multiplied by factorial(n-1) with a base case of factorial(0)=1 or factorial(1)=1, reducing the problem until the base condition is met.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q2: What is recursion in the context of computing factorial?

A

Recursion is a method where a function calls itself to solve smaller instances of the problem until reaching a simple base case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q2: What is the base condition in a recursive factorial function?

A

The base condition is when n is less than or equal to 1, at which point the function returns 1 and stops further recursive calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q2: Provide a complete C code example for factorial using recursion.

A

Example:\n\n#include <stdio.h>\n\n// Recursive function to calculate factorial\nlong long factorial(int n) {\n if(n <= 1) { // Base condition\n return 1;\n }\n return n * factorial(n - 1); // Recursive call\n}\n\nint main() {\n int num = 5;\n printf(\"Factorial of %d is %lld\\n\", num, factorial(num));\n return 0;\n}\n// This code recursively computes factorial by reducing n until the base case is reached.</stdio.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q2: How does the recursion work step-by-step for n=5?

A

For n=5, factorial(5) calls factorial(4), which calls factorial(3), then factorial(2), and finally factorial(1) returns 1; then the multiplication unfolds as 21=2, 32=6, 46=24, and 524=120.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q2: Why use recursion for calculating factorial?

A

Recursion simplifies the problem by breaking it into smaller identical subproblems, making the code more intuitive and elegant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q3: Finding GCD Using Functions – Original Question and Full Answer

A

Write a C program to compute the Greatest Common Divisor (GCD) of two numbers using functions. The program uses the Euclidean algorithm recursively, calling gcd(a, b) as gcd(b, a % b) until b becomes 0, at which point a is the GCD.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q3: What is the Euclidean algorithm for computing GCD?

A

The Euclidean algorithm computes the GCD by repeatedly replacing (a, b) with (b, a % b) until the remainder b becomes 0; the last non-zero a is the GCD.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q3: How does recursion simplify the GCD computation?

A

Recursion repeatedly applies the same operation (using modulus) until a base condition (b==0) is met, reducing the problem size at each step.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Q3: Provide a C code example for computing GCD recursively.

A

Example:\n\n#include <stdio.h>\n\n// Recursive function to compute GCD\nint gcd(int a, int b) {\n if(b == 0) // Base condition\n return a;\n return gcd(b, a % b); // Recursive call\n}\n\nint main() {\n int num1 = 56, num2 = 98;\n printf(\"GCD of %d and %d is %d\\n\", num1, num2, gcd(num1, num2));\n return 0;\n}\n// The function continues until b equals zero, then returns a.</stdio.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Q3: What is the significance of the base condition b==0 in the GCD function?

A

When b becomes 0, it means that a is exactly divisible by the previous divisor, and a is therefore the GCD; this stops the recursion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Q3: Explain the key idea behind the recursive GCD algorithm.

A

The key idea is that the GCD of two numbers also divides their difference, so by repeatedly taking the modulus, the problem is simplified until a trivial case is reached.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Q4: Swapping Two Numbers Using Functions – Original Question and Full Answer

A

Write a C program that swaps two numbers using functions. The program demonstrates both call by value and call by reference: call by value passes copies of the variables (ineffective for swapping the originals), while call by reference passes addresses (using pointers) to swap the actual values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Q4: What does call by value mean in C?

A

Call by value means that a copy of the variable is passed to the function, so any changes made inside the function do not affect the original variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Q4: What does call by reference mean in C?

A

Call by reference means that the address of the variable is passed to the function, allowing the function to modify the original variable directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Q4: Provide a C code example demonstrating swap using call by value.

A

“Example:\n\n#include <stdio.h>\n\nvoid swapByValue(int a, int b) {\n int temp = a;\n a = b;\n b = temp;\n printf(\"Inside swapByValue: a = %d</stdio.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Q4: Provide a C code example demonstrating swap using call by reference.

A

“Example:\n\n#include <stdio.h>\n\nvoid swapByReference(int *a, int *b) {\n int temp = *a;\n *a = *b;\n *b = temp;\n}\n\nint main() {\n int x = 5, y = 10;\n printf(\"Before swapByReference: x = %d</stdio.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Q4: Why is call by reference more effective for swapping numbers?

A

Call by reference allows the function to modify the original variables directly by accessing their memory addresses, making the swap effective outside the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Q4: Compare call by value and call by reference approaches for swapping.

A

Call by value passes copies (so changes are local), while call by reference passes addresses (allowing direct modification of the original variables).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Q5: Sum and Average Using Arrays – Original Question and Full Answer

A

Write a C program that takes ‘n’ integers as input and computes their sum and average using arrays. The program stores the numbers in an array, iterates over it to compute the sum, and divides the sum by the number of elements to calculate the average.

28
Q

Q5: How are arrays used in the sum and average program?

A

Arrays are used to store multiple integers, which can then be iterated over to compute the cumulative sum and eventually the average.

29
Q

Q5: How is the sum of array elements computed in the program?

A

The program uses a loop to iterate through the array, adding each element to a cumulative sum variable.

30
Q

Q5: How is the average computed from the sum?

A

The average is computed by dividing the sum by the number of elements; typecasting to float is used to avoid integer division.

31
Q

Q5: Provide a complete C code example for computing sum and average using arrays.

A

“Example:\n\n#include <stdio.h>\n\nint main() {\n int n;\n printf(\"Enter the number of elements: \");\n scanf(\"%d\", &n);\n int arr[n];\n int sum = 0;\n\n printf(\"Enter %d integers:\\n\", n);\n for(int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n sum += arr[i];\n }\n float average = (float)sum / n;\n printf(\"Sum = %d</stdio.h>

32
Q

Q5: Why is typecasting important when computing the average?

A

Typecasting the sum to float before division ensures that the result is a floating-point number and prevents truncation from integer division.

33
Q

Q5: Explain step-by-step how the sum and average are calculated.

A

The program reads n, stores the integers in an array, loops over the array to add each value to a sum variable, and then divides the sum by n (after typecasting) to get the average.

34
Q

Q6: Finding the Largest Element in an Array – Original Question and Full Answer

A

Write a C program to find the largest element in a one-dimensional array. The program assumes the first element is the largest and then iterates through the array, updating the largest value when a larger element is found.

35
Q

Q6: What is the basic approach to finding the largest element?

A

The approach starts by assuming the first element is the largest, then iterates through the remaining elements, updating the largest variable whenever a larger value is encountered.

36
Q

Q6: How does the loop help in finding the largest element?

A

The loop examines each element in the array and uses conditional statements to compare it with the current largest value, ensuring every element is checked.

37
Q

Q6: Provide a complete C code example for finding the largest element.

A

Example:\n\n#include <stdio.h>\n\nint main() {\n int n;\n printf(\"Enter the number of elements: \");\n scanf(\"%d\", &n);\n int arr[n];\n\n printf(\"Enter %d integers:\\n\", n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n\n int largest = arr[0];\n for (int i = 1; i < n; i++) {\n if (arr[i] > largest) {\n largest = arr[i];\n }\n }\n printf(\"The largest element is: %d\\n\", largest);\n return 0;\n}\n// The code compares each element to find the maximum.</stdio.h>

38
Q

Q6: What initial assumption is made about the largest element?

A

The program initially assumes that the first element of the array is the largest.

39
Q

Q6: How does the loop ensure every element is compared?

A

The loop starts at index 1 and checks each element against the current largest, ensuring that no element is left unexamined.

40
Q

Q7: Printing an Array in Reverse Order – Original Question and Full Answer

A

Write a C program that prints the elements of an array in reverse order. The program uses a loop that starts at the last index and decrements to the first index, printing each element along the way.

41
Q

Q7: What indexing technique is used to print an array in reverse?

A

The program starts at the last index (n-1) and decrements the index in each iteration until it reaches 0.

42
Q

Q7: How does the decrementing loop work in reverse printing?

A

The loop initializes with i = n-1 and decreases i by one on each iteration, accessing elements from the end to the beginning.

43
Q

Q7: Provide a complete C code example for printing an array in reverse.

A

Example:\n\n#include <stdio.h>\n\nint main() {\n int n;\n printf(\"Enter the number of elements: \");\n scanf(\"%d\", &n);\n int arr[n];\n\n printf(\"Enter %d integers:\\n\", n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n\n printf(\"Array in reverse order:\\n\");\n for (int i = n - 1; i >= 0; i--) {\n printf(\"%d \", arr[i]);\n }\n printf(\"\\n\");\n return 0;\n}\n// The loop prints elements from the last index down to the first.</stdio.h>

44
Q

Q7: What is the starting index used in the reverse loop?

A

The starting index is n-1, which corresponds to the last element of the array.

45
Q

Q7: Explain the overall logic behind printing an array in reverse order.

A

The program collects all elements into an array and then uses a loop that decrements from the last element to the first, printing each element to display the array in reverse.

46
Q

Q8: Finding the Greatest Number in a 2D Array – Original Question and Full Answer

A

Write a C program to find the greatest number in a 2D array. The program uses nested loops to traverse each row and column, compares every element, and updates the greatest value accordingly.

47
Q

Q8: How do nested loops facilitate finding the greatest element in a 2D array?

A

Nested loops allow the program to iterate through each row and, within each row, through each column, ensuring that every element is examined.

48
Q

Q8: Provide a complete C code example for finding the greatest number in a 2D array.

A

Example:\n\n#include <stdio.h>\n\nint main() {\n int rows, cols;\n printf(\"Enter the number of rows and columns: \");\n scanf(\"%d %d\", &rows, &cols);\n int arr[rows][cols];\n\n printf(\"Enter the elements of the 2D array:\\n\");\n for (int i = 0; i < rows; i++) {\n for (int j = 0; j < cols; j++) {\n scanf(\"%d\", &arr[i][j]);\n }\n }\n\n int greatest = arr[0][0];\n for (int i = 0; i < rows; i++) {\n for (int j = 0; j < cols; j++) {\n if(arr[i][j] > greatest) {\n greatest = arr[i][j];\n }\n }\n }\n printf(\"The greatest element is: %d\\n\", greatest);\n return 0;\n}\n// The nested loops ensure every element is compared.</stdio.h>

49
Q

Q8: Why initialize the greatest variable to the first element?

A

Initializing to the first element provides a valid starting comparison value from the array, ensuring accurate results.

50
Q

Q8: How are comparisons performed in the nested loop?

A

Each element is compared with the current greatest value, and if it is larger, the greatest value is updated accordingly.

51
Q

Q8: Describe the final output logic in the program.

A

After all comparisons, the program prints the value stored in the greatest variable, which is the largest element in the 2D array.

52
Q

Q9: Matrix Summation (3x3 Order) – Original Question and Full Answer

A

Write a C program to compute the summation of two 3×3 matrices. The program reads two matrices and uses nested loops to add corresponding elements element-wise, producing a resultant matrix.

53
Q

Q9: What is element-wise matrix addition?

A

Element-wise matrix addition is the process of adding two matrices by summing each pair of corresponding elements.

54
Q

Q9: How do nested loops work in matrix summation?

A

An outer loop iterates over the rows and an inner loop iterates over the columns, accessing each element at position (i, j) to perform the addition.

55
Q

Q9: Provide a complete C code example for summing two 3x3 matrices.

A

Example:\n\n#include <stdio.h>\n\nint main() {\n int mat1[3][3], mat2[3][3], sum[3][3];\n\n printf(\"Enter elements of first 3x3 matrix:\\n\");\n for (int i = 0; i < 3; i++) {\n for (int j = 0; j < 3; j++) {\n scanf(\"%d\", &mat1[i][j]);\n }\n }\n\n printf(\"Enter elements of second 3x3 matrix:\\n\");\n for (int i = 0; i < 3; i++) {\n for (int j = 0; j < 3; j++) {\n scanf(\"%d\", &mat2[i][j]);\n }\n }\n\n for (int i = 0; i < 3; i++) {\n for (int j = 0; j < 3; j++) {\n sum[i][j] = mat1[i][j] + mat2[i][j];\n }\n }\n\n printf(\"Resultant Matrix after Summation:\\n\");\n for (int i = 0; i < 3; i++) {\n for (int j = 0; j < 3; j++) {\n printf(\"%d \", sum[i][j]);\n }\n printf(\"\\n\");\n }\n return 0;\n}\n// The code adds corresponding elements using nested loops.</stdio.h>

56
Q

Q9: What inputs are required for the matrix summation program?

A

The program requires two 3x3 matrices, meaning 9 elements per matrix, to perform element-wise addition.

57
Q

Q9: How is the output displayed after summation?

A

The resulting 3x3 matrix is printed row by row, with each element separated by a space.

58
Q

Q9: Explain how element-wise summation is achieved in the code.

A

Each element in the first matrix is added to the corresponding element in the second matrix by using two nested loops that iterate over the row and column indices.

59
Q

Q10: Finding the Second Largest Element in an Array – Original Question and Full Answer

A

Write a C program to find the second largest element in a one-dimensional array. The program initializes two variables (largest and secondLargest) to INT_MIN, iterates through the array to update them appropriately, and handles the edge case when no distinct second largest element exists.

60
Q

Q10: How are ‘largest’ and ‘secondLargest’ initialized in the program?

A

Both variables are initialized to INT_MIN to ensure that any valid number in the array will be greater and can update these variables correctly.

61
Q

Q10: Explain the update logic used in the loop to find the second largest element.

A

For each element, if it is greater than ‘largest’, then ‘secondLargest’ is updated to the old ‘largest’ and ‘largest’ is updated to the new element; otherwise, if it is greater than ‘secondLargest’ and not equal to ‘largest’, then ‘secondLargest’ is updated.

62
Q

Q10: Provide a complete C code example for finding the second largest element.

A

Example:\n\n#include <stdio.h>\n#include <limits.h>\n\nint main() {\n int n = 5;\n int arr[] = {3, 5, 1, 4, 2};\n\n int largest = INT_MIN, secondLargest = INT_MIN;\n for (int i = 0; i < n; i++) {\n if (arr[i] > largest) {\n secondLargest = largest;\n largest = arr[i];\n } else if (arr[i] > secondLargest && arr[i] != largest) {\n secondLargest = arr[i];\n }\n }\n\n if(secondLargest == INT_MIN) {\n printf(\"There is no distinct second largest element.\\n\");\n } else {\n printf(\"The second largest element is: %d\\n\", secondLargest);\n }\n return 0;\n}\n// Inline comments explain each update step in the loop.</limits.h></stdio.h>

63
Q

Q10: Walk through an example using the array [3, 5, 1, 4, 2].

A

Step-by-step: Start with largest = INT_MIN, secondLargest = INT_MIN. For 3, update largest to 3. For 5, update secondLargest to 3 and largest to 5. For 1, no change. For 4, update secondLargest to 4. For 2, no change. Final values: largest = 5, secondLargest = 4.

64
Q

Q10: What edge case is handled in this program?

A

The program handles the case where all elements are identical or no distinct second largest exists by checking if secondLargest remains INT_MIN and then printing an appropriate message.

65
Q

Q10: Why is INT_MIN used for initialization in this context?

A

INT_MIN is used to ensure that any valid number in the array will be larger, making the initial comparisons valid.