Problem Solving Through C Programming Assignment Merged
Problem Solving Through C Programming Assignment Merged
Solution: (a) Programming is the process of creating a set of instructions that tell a computer how to perform a
task.
5. Algorithm is-
a) A process or set of rules to be followed in calculations or other problem-solving operations,
especially by a human.
b) A process or set of rules to be followed to solve numerical problems only.
c) A process or set of rules to be followed in calculations or other problem-solving operations,
especially by a computer.
d) A process or set of rules to be followed in to solve logical problems only.
Problem Solving through Programming in C
Solution: (c) A process or set of rules to be followed in calculations or other problem-solving operations,
especially by a computer
Solution: (d) Flowchart is basically a diagrammatic representation of the algorithm. Whereas in pseudo
code normal English language is translated into the programming languages to be worked on.
8. The input N from the user is 6. The output of the following algorithm is
Problem Solving through Programming in C
a) 21
b) 720
c) 1
d) 1024
Solution: (c) The condition “i>=N” fails in the first iteration because i=1 and N=6. Thus, the execution
jumps directly to the print command. The initial assigned value of X will be printed which is 1.
a) 51
b) 52
c) 50
d) Compilation error
Solution: (d) The assignment X=Y is incorrect. “Equals to” is a left to right assignment. The variable Y is
not declared before assignment. This the compiler will throw error at this step.
10. The section of the CPU that selects, interprets and sees to the execution of program instructions
a) Memory
b) Register Unit
c) Control Unit
d) ALU
Solution: (c) Control unit of the computer helps in maintaining sequence of steps and execute the program
Problem Solving through Programming in C
Solution: (c) Only alphanumeric characters and few special characters like ‘_’ are allowed in
variable name is C. The special character @ is not allowed.
2. A function
a) is a block of statements to perform some specific task
b) is a fundamental modular unit to perform some task
c) has a name and can be used multiple times
d) All the above options are true
a) Sequential
b) Parallel
c) Multi-threading
d) None of these
a) -65535
b) 0
𝑐) -32,767
d) -32,768
Solution: (d) The first bit is used to indicate whether it is signed or unsigned integer. So it
will be −215 i.e. -32,768
Problem Solving through Programming in C
a) 9
b) 0
c) 1001
d) Compilation Error
a) 8.00
b) 4.00
c) 0.00
d) 16.00
Solution: (d) The pre-processing replaces fun(2) with (2*2). Thus fun(2)=4, so,
i=64.0/4=16.00
8. The following C program swaps the value of two numbers without using any third
variable. What will be the correct option to fill up the blank?
#include <stdio.h>
Problem Solving through Programming in C
int main()
{
int a=2, b=3;
printf("The values before swapping a = %d, b=%d", a, b);
____________________________________
a) 1 3
b) 3 1
c) 1 1
d) 3 3
Solution: (b) 3 1
Here the program is swapping the values of the variables x and y. A temporary variable t is
used for the swapping purpose.
10. When executed the following code will print _______.
#include <stdio.h>
int main() {
int sum = 3 + 6 / 2 + 6 * 2;
printf("%d", sum);
return 0;
}
Solution: (a) Operator precedence determines which operator is performed first in an expression with
more than one operator with different precedence, whereas associativity is used when two operators of
same precedence appear in an expression
a) 67
b) -36
c) 66
d) -37
Solution: (a) Following the precedence rule, we can conclude that the operation steps are
Result=50+50*- 20/6%3+6*3
Result=50-1000/6%3+6*3
Result=50-166%3+6*3
Result=50-1+6*3
Result=50-1+18
Result=49+18
Result=67
a) 0
b) 3
c) 4
d) Compilation error
Solution: (c) ‘?:’ is Conditional Expression. If Condition is true ? then value X : otherwise value Y.
After simplifying the expression, we get 36<40?4:3. The condition in LHS of ? is true. Thus 4 will be
stored in b.
a) IITKGP
b) IITD and IITM
c) IITKGP and IITM
d) IITM
Solution: (a) Only the first if condition will be executed as the condition inside the if statement is true.
Thus IITKGP will be printed.
a) Condition is true
b) Condition is false
Week 3: Assignment Solution
c) Error
d) No output possible
Solution: (a) Condition is true
Any non-zero value is treated as true for condition. Consider the expressions: if( (-10 &&
10)||(20 && -20) )
=if( (1) || (1) )
=if(1)
a) Programming on C 0
b) NPTEL 0
c) NPTEL 3
d) Compilation error
Solution: (b) At first zero will assign in ‘i’ then comma operator returns the last value which is 3 and
condition becomes true.
#include <stdio.h>
int main()
{
int x = 0;
if (x++)
printf("true\n");
else if (x == 1)
printf("false\n");
return 0;
}
a) true
b) false
c) Compiler dependent
d) Compiler error
Week 3: Assignment Solution
Solution: (b) ++ is a post increment operator. In x++, first 0 will be assigned and then x will be
incremented by 1. Thus the next else if condition will be evaluated and false will be printed.
a) 0
b) 1
c) 10
d) 30
Solution: (b) 1
Here, operators == and != have same precedence. The associativity of both == and != is left to right,
i.e, the expression on the left is executed first and moves towards the right.
#include <stdio.h>
int main()
{
int a = 100, b = 200, c = 300;
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
}
Week 3: Assignment Solution
a) TRUE
b) FALSE
c) Syntax Error
d) Compilation Error
Solution: (b) FALSE :: (c > b > a) is treated as ((c > b) > a), associativity of '>'
is left to right. Therefore the value becomes ((300 > 200) > 100) which becomes (1 > 100) thus FALSE
a)10
b) 11
c)20
d) Compiler error
Solution: (b) ‘==’ is a relational operator. It returns 1 if the condition is true or 0 if the condition is
false. Thus y==10 will return true (which is 1) and 10+1 will be 11. Hence, the value of z will be 11.
Week 4 Assignment Solution
Solution: (c) To test a condition and execute different code based on the result.
Solution: (c) They allow you to test multiple conditions and execute different
blocks of code based on the results.
Solution: (d) If the condition is true, the program executes the code inside the "if"
block; if the condition is false, nothing happens.
a) 2
b) 3
c) 4
d) Compiler error
Solution: (a) i++ is a post-increment operator. It assigns first and then increments the
operator by one. Therefore, i value after the assignment remains 2
7. If multiple conditions are used in a single “if” statement then the testing of those
conditions are done
a) From Right to Left
b) From Left to right
c) Randomly
d) None of the above
Solution: (b) Multiple conditions are tested from Left to the right.
8. What is the purpose of the given program? n is the input number given by the
user.
#include <stdio.h>
int main()
{
int n, x = 0, y;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
y = n % 10;
x = x - y;
n = n/10;
}
printf("Output is = %d", x);
return 0;
}
a) Sum of the digits of a number
b) The negative sum of the digits of a number
c) The reverse of a number
d) The same number is printed
Solution: (b) Negative sum of the digits of a number
Week 4 Assignment Solution
Please take a number and follow the operation step-by-step. You will be able to find the
negative sum number as output.
a) a=5, b=6,c=2;
b) a=6, b=7,c=1;
c) a=6, b=6,c=2;
d) a=5, b=7,c=1;
a) Choice is 1
b) Choice other than 1
c) Both (a) and (b)
d) Syntax error
Solution: (c)
Since the “break;” statement is not used after the print statement, it will execute the default
instruction as well.
Week 5 Assignment Solution
1. The statement that transfers control to the beginning of the loop is called
a) break
b) continue
c) goto
d) None of the above
Solution: (b) continue
a) 5
b) 10
c) No output
d) Compilation error
Week 5 Assignment Solution
Solution: (c) As i is initialized as an integer variable, integer value of i after the operation (i=i+0.5) will be zero.
Thus, the loop will never be ended and the control will not come to the printf statement at all. So, nothing will be
printed.
a) True
b) False
c) Both ‘True’ and ‘False’
d) Compilation error
Solution: (c) ‘a--’ post-increment the value of a. Thus, the if statement is executed as the value of a is
considered as 1 which is true. ‘++a’ pre-increment the value of a. Thus, the decremented value of a (which
is 0) is incremented first and then assigned. So, both the if statements are executed ad correspondingly both
True and False will be printed.
a) Error
b) I love C - will be printed 3 times
c) I love C - will be printed 6 times
d) I love C - will be printed 5 times
Solution: (b) I love C will be printed 3 times
a) 1
b) 2
c) 3
d) Error
Solution: (c) 3
exp1? exp2: exp3
(4 < 8) ? (5!= 1 < 5 == 0)? 1: 2 : 3;
9. The following program is used to find the reverse of a number using C language. Find the missing
condition inside while statement (indicated as ‘xxxx’).
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
while(xxxx)
Week 5 Assignment Solution
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
return 0;
}
a) n!=0
b) n==0
c) n%10==0
d) n/10==0
Solution: (a) The loop should be continued till the value of n becomes zero. Thus, the right option is n!=0.
10. Compute the printed value of i & j of the C program given below
#include <stdio.h>
int main()
{
int i = 0, j = 15;
while (i<8, j >9)
{
i++;
j--;
}
printf("%d, %d\n", i, j);
return 0;
}
a) 8,10
b) 8,9
c) 6, 9
d) 7, 10
Solution: (c) The while condition checks the last condition (i.e. j>9) and till the condition is satisfied the block inside
the loop is executed. Thus the loop is run for 6 times. i will be incremented by 6 and j will be decremented by 6. The
final values of i and j will be i=6 and j=9.
Week 6 Assignment Solution
1. What is an array in C?
a) A collection of similar data elements with the same data type.
b) A built-in function that performs mathematical calculations.
c) A keyword used for declaring variables.
d) A data type used to store characters only.
Solution: a) A collection of similar data elements with the same data type.
Solution: a) 0
Explanation: In C, array indices start from 0. Therefore, the index of the first element in an
array is 0, the second element's index is 1, and so on.
Explanation: The "for" loop is commonly used to iterate through all elements of an array in C.
It allows precise control over the loop variable and is well-suited for iterating over a
range of elements, as required in array traversal.
5. How can you find the sum of all elements in a 1D array "arr" with 5 elements using
loop in C?
a) sum = arr[0] + arr[1] + arr[2] + arr[3] + arr[4];
Week 6 Assignment Solution
b) sum = arr[5];
c) for (int i = 0; i <= 5; i++) { sum += arr[i]; }
d) for (int i = 0; i < 5; i++) { sum += arr[i]; }
Explanation: To find the sum of all elements in a 1D array "arr" with 5 elements, you can use
a "for" loop to iterate through the array and add each element to the "sum" variable. The
code provided correctly calculates the sum of all elements.
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int i = 0;
while (i < 5) {
printf("%d ", arr[i]);
i += 2;}
return 0;
}
a) 135
b) 12345
c) 123
d) 14
Solution: a) 1 3 5
Explanation: The provided code uses a "while" loop to print elements of the array "arr" with
an increment of 2 for the loop counter "i." The loop starts at index 0 and prints elements
at indices 0, 2, and 4. The output will be "1 3 5."
Solution: (a) k=arr[1]++ due to post increment operation, assignment is done first. so it actually
becomes k=arr[1]=2. j=++arr[2]=++3=4. i=arr[j++]=arr[4++]=arr[4]=5 (as its post
increment hence assignment is done first). Due to post increment in i=arr[j++], value of
j is also incremented and finally becomes 5. So, finally i=5, j=5, k=2.
a) 1
b) 2
c) 3
d) 4
Solution: (d) The program finds the maximum element of an array. Hence, the output is 4.
}
printf("%d", sum);
return 0;
}
Week 6 Assignment Solution
a) 5, 4
b) 5, 5
c) 4, 4
d) 3, 4
Solution: (c)
The execution steps are as follows:
1. arr[1] = ++arr[1]; arr[1]=++2=3 so, arr={1, 3, 3, 4, 5}
2. a = arr[1]++; a=arr[1]=3 (due to post increment). arr remains the same as step 1.
3. arr[1] = arr[a++]; arr[1]=arr[a]=arr[3]=4. arr={1, 4, 3, 4, 5}. a is incremented to 3+1=4
after the assignment is done.
4. Finally, a=4 and arr[1]=4 are printed
Week 7 Assignment Solution
a) 1,2
b) 1,2,3
c) 2,4
d) 1,3
Solution: (b) Clearly, we know first three statements are correct, but fourth statement is
wrong because strcat() is used to join two strings in C.
Solution: (c) Address of the first element of the array or the base address of the array.
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
a) gnirts
b) gnirt
c) string
Week 7 Assignment Solution
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′
8. 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
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
10. What will be the value of ‘i’ after the execution of the C code fragment given below?
a) 70
b) Garbage value
c) Compilation error
d) None
a) Infinite times
b) 32767
c) 65535
d) Till stack overflows
5. How many times ‘Hi’ will be printed in the program given below
#include<stdio.h>
int i;
int fun();
int main()
{
while(i)
{
fun();
main();
}
printf("Hello\n");
return 0;
}
int fun()
{
printf("Hi");
}
a) Only once
b) Zero times
c) Infinite times
d) Compilation error
Solution: (b) The default value of i is ‘0’. Thus, the while loop will never be executed and the
control will not come into the function. Thus, ‘Hi’ will never be printed.
Assignment 8 Solution
As n =5, so the function factorial() calls itself 5 times. In addition, factorial() function is
called once from the main(). So, total 6 times the function factorial() will be executed.
int main ()
{
int a = 2048, sum = 0;
func (a, sum);
printf ("%d ", sum);
}
a) 8 ,4, 0, 2, 14
b) 8, 4, 0, 2, 0
c) 2, 0, 4, 8, 14
d) 2, 0, 4, 8, 0
Assignment 8 Solution
Solution: (d) 2, 0, 4, 8, 0
sum has no use in func(), it is there just to confuse. Function func() just prints all digits of a
number. In main, there is one more printf statement after func(), so one more 0 is printed after
all digits of n.
a) 55
b) 45
c) 66
d) 10
Solution: (a) The program finds the sum of the integer numbers till 10. Thus the output is
55.
a) Maximum of a, b
b) Positive difference between a and b
c) Sum of a and b
d) Minimum of a and b
0 if x<y
x-y if x>y
Assume in the first case a>b so find(a,b) will return (a-b). So find(a,find(a,b)) will be
find(a,a-b) which will return (a-(a-b))= b which is the minimum number.
Now if a<b then find(a,b)=0 so find(a,find(a,b))=find(a,0) which will return (a-0)=a
which is the minimum number. So, the code actually returns a minimum of two numbers.
10. What is the output of the C code given below
#include <stdio.h>
float func(float age[ ]);
int main()
{
float result, age[] = { 23.4, 55, 22.6, 3, 40.5, 18 };
result = func(age);
printf("%0.2f", result);
return 0;
}
Ans: 27.08
Week 9: Assignment Solution
2. What is the best case and worst case complexity of ordered linear search?
a) O(nlogn), O(logn)
b) O(logn), O(nlogn)
c) O(n), O(1)
d) O(1), O(n)
Solution: (d)
Although ordered linear search is better than unordered when the element is not present in the array, the best
and worst cases still remain the same, with the key element being found at first position or at last position
respectively.
3. Given an array arr = {12, 34, 47, 62, 85, 92, 95, 99,105} and key = 34; what are the mid values
(corresponding array elements) generated in the first and second iterations?
a) 85 and 12
b) 85 and 34
c) 62 and 34
d) 62 and 47
5. Consider the array A[ ]= {5,4,9,1,3} apply the insertion sort to sort the array. Consider the cost
associated with each sort is 25 rupees, what is the total cost of the insertion sort for sorting the entire
array?
a) 25
b) 50
c) 75
d) 100
Solution: (c)
When the element 1 reaches the first position of the array three comparisons are only required
hence 25 * 3= 75 rupees.
*step 1: 4 5 9 1 3.
Week 9: Assignment Solution
*step 2: 1 4 5 9 3
*step 3: 1 3 4 5 9
6. Select the code snippet which performs unordered linear search iteratively?
Solution: (a)
Unordered term refers to the given array, that is, the elements need not be ordered. To search for an element
in such an array, we need to loop through the elements until the desired element is found.
Week 9: Assignment Solution
a) 35
b) 30
c) 50
d) 53
Solution: (d)
Here, func1(3,5) (inside the if statement ) returns 3 as the condition given in the macro is false and it
returns a=3.
Next, (3+3 >5 ) this condition is true and the program calls func2(3,5) which swaps the values of a and b.
Therefore the output is 5 3.
8. Consider an array of elements arr[5]= {5,4,3,2,1}, what are the steps of insertions done while doing
insertion sort in the array.
a) 4 5 3 2 1
34521
23451
12345
b) 5 4 3 1 2
54123
51234
12345
c) 4 3 2 1 5
32154
21543
15432
d) 4 5 3 2 1
23451
34521
12345
Solution: (a)
Iteration 1: 4 is placed in its appropriate location. arr={4 5 3 2 1}
Week 9: Assignment Solution
a) 0
b) 1
c) 01
d) None of the above
Solution: (b)
Here, A is not initialized, so its default value is 0. The condition in the #if macro is FALSE and #define B 1
is executed. Therefore, B stores 1.
10. What will be the output?
#include <stdio.h>
#define a 10
int main()
{
printf("%d ", a);
int a=50;
printf("%d ", a);
return 0;
}
a) 10 10
b) 10 50
c) 50 50
d) Compilation error
Solution: (d)
If a is defined in macro, its value will be same throughput the program. The value assigned to that macro
cannot be changed inside the program. This is the reason, it will show compilation error at the step
int a=50.
Week 10 Assignment Solution
2. In ……………, the search starts at the beginning of the list and checks every element in
the list.
a) Linear search
b) Binary search
c) Hash search
d) Binary tree search
Solution: (a) Linear search
Answer: c) O(n)
Explanation: In the worst-case scenario, Linear Search may require O(n) time to find an
element in an array of n elements.
Solution: (d)
Explanation: Bubble sort works by starting from the first element and swapping the elements
if required in each iteration. Therefore, in the worst case, the complexity is O(N^2).
5. What maximum number of comparisons can occur when a bubble sort is implemented?
Assume there are n elements in the array.
a) (1/2) (n-1)
b) (1/2) n(n-1)
c) (1/4) n(n-1)
d) None of the above
Solution: (b)(½)n(n-1)
Week 10 Assignment Solution
6. What are the correct intermediate steps of the following data set when it is being sorted
with the bubble sort? 7,4,1,8,2
a) 4,7,1,8,24,1,7,2,84,1,2,7,81,4,2,7,81,2,4,7,8
b) 4,7,1,8,24,1,7,8,24,1,7,2,81,4,7,2,81,4,2,7,81,2,4,7,8
c) 4,7,1,8,21,4,7,8,21,4,2,7,81,2,4,7,8
d) 4,7,1,8,24,7,1,2,81,4,7,2,81,4,2,7,81,2,4,7,8
Solution: (b) Bubble sort works by starting from the first element and swapping the elements
if required in each iteration. Therefore, the order when the changes happen are as shown in
the steps of the answer.
Explanation: The Bisection Method is only applicable for real roots and cannot find complex
roots of a function.
Solution: 30
ptr1 points to the first element (10), and ptr2 points to the fourth element (40). *ptr2 - *ptr1
will be 40−10=40−10=30.
9. What is the solution of the equation given below using the Bisection Method up to four
decimal places? (Consider the root lying on positive quadrant only and compute the root
till five iterations only)
𝑓(𝑥) = 𝑥𝑒 − 3𝑥 − 5
a) 10 12 6 7 2
b) 10 12 6 7
c) 2 7 6 12
d) 2 7 6 12 10
Solution: (d) The pointer p after the operation p = a + 4, essentially points the last element of
the array. Therefore, when the for loop iterates, it keeps printing from the last element to the
beginning.
Week 11 Assignment Solution
Solution: (d) The differential equation, step size and the initial value of y are required to solve differential
equation using Runge-Kutta method.
a) 12.78
b) 13.08
c) 14.12
d) 11.36
Solution: (b)
2
𝑥 − 𝑥𝑗 (7 − 10)(7 − 15) 24
𝐿0 (𝑥) = ∏ = = = 0.48
𝑥0 − 𝑥𝑗 (5 − 10)(5 − 15) 50
𝑗=0
𝑗≠0
2
𝑥 − 𝑥𝑗 (7 − 5)(7 − 15) −16
𝐿1 (𝑥) = ∏ = = = 0.64
𝑥1 − 𝑥𝑗 (10 − 5)(10 − 15) −25
𝑗=0
𝑗≠1
2
𝑥 − 𝑥𝑗 (7 − 5)(7 − 10) −6
𝐿2 (𝑥) = ∏ = = = −0.12
𝑥1 − 𝑥𝑗 (15 − 5)(15 − 10) 50
𝑗=0
𝑗≠2
So 𝑓(7) = 0.48 ∗ 15.35 + 0.64 ∗ 9.63 − 0.12 ∗ 3.74 = 13.08
3.2
4. The value of ∫0 𝑥𝑒 𝑥 𝑑𝑥 by using one segment trapezoidal rule is
a) 172.7
b) 125.6
c) 136.2
d) 142.8
Solution: (b)
𝑏
𝑓(𝑏) − 𝑓(𝑎)
∫ 𝑓(𝑥)𝑑𝑥 = (𝑏 − 𝑎)
𝑎 2
Week 11 Assignment Solution
3.2
Here, 𝑎 = 0, 𝑏 = 3.2, 𝑓(𝑎) = 0 and 𝑓(𝑏) = 78.5. Hence, ∫0 𝑥𝑒 𝑥 𝑑𝑥 = 125.6
Solution: (c)Approximation increases with the increase of the number of segments between the lower and upper
limit.
6. Solve the ordinary differential equation below using Runge-Kutta4th order method.
Step size h=0.2.
𝑑𝑦
5 + 𝑥𝑦 3 = cos(𝑥) , 𝑦(0) = 3
𝑑𝑥
The value of y(0.2) is (upto two decimal points)
a) 2.86
b) 2.93
c) 3.13
d) 3.08
Solution: (b)
Solution: (a)
3
8. The value of ∫1 ex (ln 𝑥) dx calculated using the Trapezoidal rule with five subintervals is (* range
is given in output rather than single value to avoid approximation error)
a) 12.56 to 12.92
b) 13.12 to 13.66
c) 14.24 to 14.58
d) 15.13 to 15.45
a) 9
b) 8
c) 5
d) 2
Solution: (d) 2
func(513, 2) will return 1 + func(256, 2). All subsequent recursive calls (including func(256, 2)) will return
0 + func(n/2, 2) except the last call func(1, 2) . The last call func(1, 2) returns 1. So, the value returned by
func(513, 2) is 1 + 0 + 0…. + 0 + 1=2.
10. What is the output?
#include <stdio.h>
int fun(int n)
{
if (n == 4)
return n;
else return 2*fun(n+1);
}
int main()
{
printf("%d ", fun(2));
return 0;
}
a) 4
b) 8
c) 16
d) Error
Solution: (c) 16
Week 12 Assignment Solution
a) String
b) Structure
c) Char
d) All of the mentioned
Solution: (c) The scope of the member name is confined to the particular structure, within
which it is defined
++p;
while(*p != 'e')
printf("%c", *p++);
return 0;
}
a) abcd
b) bcd
c) cd
d) abcdfgh
Solution: (b)
First, the pointer points to the base of A. Then it's incremented by one to point to the element
b. While p is not pointing e, the loop keeps prints the elements one after another. Therefore,
the final answer is bcd.
7. What is the output of the following C code? Assume that the address of x is 2000 (in
decimal) and an integer requires four bytes of memory.
int main()
{
unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
printf("%u,%u, %u", x+3, *(x+3),*(x+2)+3);
return 0;
}
Solution: (a)
All these points to the same element. Therefore, the same value will be printed. This is an
example of pointer arithmetic in an 2D array.
a) Yes
b) No
c) Only as an array
d) Only if the structure is anonymous
Answer: a) Yes
Explanation: A structure can contain a pointer to its own type, which is often used to create
linked data structures like linked lists.
struct Point {
int x;
int y;
};
struct Point *arr[2];
struct Point p1 = {1, 2}, p2 = {3, 4};
arr[0] = &p1;
arr[1] = &p2;
printf("%d", arr[1]->y);
a) 1
b) 2
c) 3
d) 4
Answer: d) 4
Explanation: arr[1] points to p2, and arr[1]->y accesses the y member of p2, which is 4.
int main()
{
struct p p1[] = {1, 90, 62, 33, 3, 34};
struct p *ptr1 = p1;
int x = (sizeof(p1) / 3);
if (x == sizeof(int) + sizeof(char))
printf("True");
else
printf("False");
return 0;
Week 12 Assignment Solution
a) True
b) False
c) No output
d) Compilation error
Solution: (b) Size of the structure is the maximum size of the variable inside structure. Thus,
the size of each element of structure p is 4 bytes (in gcc compiler, it can vary based on
compiler). Thus, sizeof(p1) is 6*4=24. x will be 24/3=8. In the next step,
sizeof(int)+sizeof(char) is 5 which is not equal to x. Hence, false will be printed.