Week 7 Assignment Solution Jan 2023
Week 7 Assignment Solution Jan 2023
a) fellows
b) h
c) fello
d) Compiler error
Solution: (a) a[2] indicates the 3rd string of the 2D array. Thus fellows will be printed.
a) n1=18, n2=17
b) n1=18, n2=18
c) n1=17, n1=17
d) n1=17, n2=18
char p[20];
char s[] = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length - i];
printf("%s", p);
return 0;
}
The output would be-
a) gnirts
b) gnirt
c) string
d) no output is printed
Solution: (d)
Let us consider below line inside the for loop p[i] = s[length - i];
For i = 0, p[i] will be s[6 - 0] and s[6] is ‘\0′
So p[0] becomes ‘\0’. It doesn’t matter what comes in p[1], p[2]….. as p[0] will not change
for i >0. Nothing is printed if we print a string with first character ‘\0′
5. What will be the value of ‘i’ after the execution of the C code given below?
a) 0
b) 1
c) -1
d) None
Solution: (a) 0
strcat(str3, strcpy(str2, str1)) makes it “daffodills”, hence strcmp(“daffodills”, “daffodills”)=0
6. If the starting address of an float array Arr[10][10] is 2000, what would be the
memory address of the element Arr[5][6]? (float takes 4 bytes of memory)
a) 2268
b) 2120
c) 2224
d) 2144
Week 7 Assignment Solution 2023
Solution: (c) If ‘a’, ‘b’ and ‘c’ denotes the starting address, number of columns and size in
bytes for each element respectively of array Arr[][], then the location of Arr[i][j] can be
calculated as
𝐴𝑑𝑑𝑟𝑒𝑠𝑠 = 𝑎 + (𝑖 ∗ 𝑏 + 𝑗) ∗ 𝑐
Thus the address of Arr[5][6] is 2000+(5*10+6)*4=2224
Solution: 321004
In a[2][3] = {1, 2, 3, 4}; only 4 values are given. The rest will be taken as 0. So, finally
a[2][3] = {{1, 2, 3}, {4,0,0}}; So, 321004 will be printed as per the given for loop.
c) Error
d) None
Solution: (b) The string is not empty
a) Compilation error
b) 7
c) 1
d) 2
Solution: (a) The initialization method of the array is not valid in C. The second dimension
must be specified.