C Assignment Before 10th April
C Assignment Before 10th April
ROLL-98 SEC - D
-----------------------------------------------------------------------------------------------------------------------------
Group - A
1. A) Answer – fun() is called 6 times in total, once for each value from 6 down to 1.
B) Answer - Each element of the array occupies 2 bytes (since an integer takes 2 bytes). The
memory location of the 8th element would be. Memory location of 8th element=Memory location of
the first byte of the array+(Index of the element−1)×Size of each element Memory location of 8th
element=Memory location of the first byte of the array+(Index of the element−1)×Size of each
element
So, the memory location of the 8th element of the array would be 1014.
C) Answer - The output of the code is unpredictable due to undefined behavior. It might print 4 if the
memory layout happens to contain 4 at the location where the fourth element would be if the array
were properly initialized, but there's no guarantee of this.
int arr[5] = {1, 2, 3, 4, 5}; initializes an integer array arr with 5 elements {1, 2, 3, 4, 5}.
arr[1] = ++arr[1]; increments the value at index 1 of arr by 1. So now arr[1] becomes 3 (2 + 1).
a = arr[1]++; assigns the value of arr[1] (which is 3) to a, and then increments the value of arr[1] by 1.
arr[1] = arr[a++]; assigns the value at index a (which is arr[3], since a is 3) to arr[1]. So now arr[1]
becomes 4.
printf("%d,%d", a, arr[1]); prints the value of a (which is 4) and the value of arr[1] (which is also 4).
char s[] = "UEM"; initializes a character array s with the string "UEM".
The for loop iterates from i = 0 to length - 1, which is from 0 to 2 (inclusive), since length is 3.
Inside the loop, p[i] = s[length - i]; attempts to store the characters of s in reverse order into array p.
However, there's an indexing issue here. It should be s[length - i - 1] to access the correct
characters of s.
However, due to the indexing issue mentioned above, the code tries to access memory beyond the
bounds of array s, leading to undefined behavior. It may result in printing garbage values or crashing
the program.
To fix this issue, the line inside the loop should be corrected to: p[i] = s[length - i - 1];
F) Answer - The while loop is skipped entirely because i is initially set to 0 and remains 0 throughout
the program execution. Therefore, the fun() function is never called. After the loop, the program
continues to execute and prints "Hello".
G) Answer - Due to the truncation behavior, i will never exceed 4, and the loop will keep running
indefinitely, printing "Inside Loop" repeatedly. The output would be an infinite repetition of "Inside
Loop" until the program is manually terminated.
7531
It prints 7 (the last digit of 1357) and then calls itself with n = 135.
It prints 5 (the last digit of 135) and then calls itself with n = 13.
It prints 3 (the last digit of 13) and then calls itself with n = 1.
Group – B
12
12
We modified the swap() function to accept pointers to integers (int *a and int *b). Inside the swap()
function, we dereference these pointers to access and modify the values of the original variables.
We call swap(&a, &b) in main() to pass the addresses of a and b to the swap() function, so it can
modify their values directly.
3. Answer - #include<stdio.h>
if (n <= 1)
return n;
else
int main()
int n, i;
scanf("%d", &n);
return 0;
4. Answer - *(q + 1) accesses the value at the memory location q + 1, which is the second element
of the integer array x. So, *(q + 1) evaluates to 12. *(q + 2) accesses the value at the memory
location q + 2, which is the third element of the integer array x. So, *(q + 2) evaluates to 10.
Therefore, *(q + 1) - *(q + 2) evaluates to 12 - 10 = 2. Now, p + *(q + 1) - *(q + 2) is equivalent to p + 2,
which moves the pointer p two positions ahead. So, *(p + *(q + 1) - *(q + 2)) accesses the character
at the memory location p + 2, which corresponds to the third character of the string "termexam",
i.e., 'r'. Therefore, the output of the code will be:
min = arr[i];
return min;
int main() {
int i;
scanf("%d", &arr[i]);
return 0;
#include <string.h>
int main() {
char password[50];
scanf("%s", password);
if (strlen(password) >= 4) {
printf("Password Accepted\n");
} else {
printf("Incorrect Password\n");
}
return 0;
int main() {
scanf("%d", &val);
scanf("%d", &index);
if (x[index] == val) {
} else {
} else {
return 0;
Group – C
if (n == 0 || n == 1) {
} else {
return result;
return sum;
int main() {
int n;
scanf("%d", &n);
return 0;
arjunaWins++;
karnaWins++;
}
}
printf("Arjuna\n");
printf("Karna\n");
} else {
printf("Tie\n");
int main() {
scanf("%d", &A[i]);
scanf("%d", &K[i]);
determineBestArcher(A, K);
return 0;
#include <string.h>
int main() {
char t, s[10];
int i, j, n;
printf("Enter a string: ");
scanf("%s", s);
n = strlen(s);
t = s[i];
s[i] = s[j];
s[j] = t;
return 0;
int length = 0;
length++;
return length;
int main() {
char t, s[10];
int i, j, n;
scanf("%s", s);
n = findLength(s); // Using the custom function to find the length of the string
t = s[i];
s[i] = s[j];
s[j] = t;
}
printf("Reverse of the string: %s\n", s);
return 0;
int sum = 0;
if (n % i == 0) {
sum += i;
if (sum == n) {
} else {
int remainder;
while (y != 0) {
remainder = x % y;
x = y;
y = remainder;
return x;
int main() {
scanf("%d", &sizeA);
scanf("%d", &A[i]);
scanf("%d", &sizeB);
scanf("%d", &B[i]);
int index = 0;
merged[index++] = A[i];
merged[index++] = B[i];
printf("%d", merged[i]);
if (i < mergedSize - 1) {
printf(", ");
printf("}\n");
return 0;
}
if (matrix[i][j] != matrix[j][i]) {
int main() {
int matrix[3][3];
scanf("%d", &matrix[i][j]);
if (isBalanced(matrix)) {
return 0;
if (n == 0) {
return 1;
else {
int main() {
int x, n;
scanf("%d", &x);
scanf("%d", &n);
return 0;
int index = -1; // Initialize index to -1, indicating k is not found initially
if (A[i] == k) {
index = i; // Update index to the current index if k is found
break; // Break out of the loop after finding the first occurrence
int main() {
int k = 2;
return 0;
-----------------------------------------------------------------------------------------------------------------------------