Assignment Solutions-Problem Solving Through Programming in C
Assignment Solutions-Problem Solving Through Programming in C
1. The input given from keyboard is converted to computer understandable unit (bit) by the standard
a) ISO
b) ANSI
c) ASCII
d) EBCDIC
Solution: (c) ASCII is the standard for character to equivalent number (in bits) conversion.
In high-level language, testing and debugging a program is easier than assembly language.
The role of a compiler is to translate source program statements to object codes.
Solution: (a) X and Y both have same values but different locations.
a) 4
b) 8
c) 16
d) 20
a) 21
b) 28
c) 30
Week 1 Assignment Solution
d) 40
Solution: (b) The flowchart finds the sum of first 7 natural numbers. Hence, the right answer is 28.
7. The print values of ‘a’ and ‘b’ of the flowchart below are
a) a=4,b=6
b) a=6,b=4
c) a=10,b=2
d) a=2,b=10
Solution: (b) The algorithm finds the swap of two numbers. Hence, the output is a=6,b=4
8. The program which translates high level program into its equivalent machine language program is
called
a) a translator
b) a language processor
c) a converter
d) None of the above
Solution: (a) translator. Generally, there are three types of translator-compilers, interpreters, assemblers.
1. A function is
2. If an integer needs two bytes of storage, then the minimum value of a signed integer in C
would be
a) −(216 − 1)
b) 0
c) −(215 − 1)
d) −215
Solution: (d) The first bit is used to indicate whether it is a signed or unsigned integer.
a) I and II
b) II and III
c) I, II and IV
d) All of the above
a)13
b)31
c)11
d)33
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.
Solution: 18.50
The pre-processing replaces fun(2) with (2*2-2). Thus fun(2)=2, so, i=37.0/2=18.50
d) X_123_Var
Solution: (c) Variable name must not begin with a digit. So, ‘123Var’ is an invalid variable
declaration in C.
a) 6
b) 3
c) 4
d) Compilation error
Solution: (d) #define is a pre-processor and 6 is stored in a. Thus ‘a’ cannot be declared as a
variable. Thus, the compiler will return a compilation error.
10. The following C program swaps the value of two numbers without using any third
variable. What are the correct operations that need to be inserted inside the blanks?
#include <stdio.h>
int main()
{
int a=2, b=3;
printf("The values before swapping a = %d, b=%d", a, b);
_____; ______; _____;
printf("The values after swapping a = %d, b=%d", a, b);
return 0;
}
#include<stdio.h>
int main()
{
int a, z, x=10, y=12;
z=x*y++;
a= x*y;
printf("%d, %d", z, a);
return 0;
}
a) 120, 120
b) 120, 130
c) 130, 120
d) 130, 130
Solution:(b) The eqation z= x*y++ gives the result 120 because ‘y’ does not get incremented. After
multiplication ‘y’ incremented to 13. So the second equation gives the result 130.
#include<stdio.h>
int main()
{
int p=2;
int m=10;
int k;
k= !((p<2)&&(m>2));
printf("\n%d", k);
return 0;
}
a) 1
b) 0
c) -1
d) 2
Solution: (a) As (2<2) and (10>2) both condition are false, so the AND (&& ) operation will be zero. Hence
k=!0=1.
}
else
printf("\n Roots are imaginary");
return 0;
}
a) x1=4, x2=3
b) x1=-5, x2=-4
c) x1=2.5, x2=4.2
d) Roots are imaginary
Solution: (d) Roots are imaginary. b*b=4, 4*a*c=4*5*6=120 So, b*b<4*a*c. Hence ‘else’ part will be
executed.
#include<stdio.h>
int main()
{
int p=6, q=4, r=10;
if(p>q)
{
if(p>r)
printf("%d", p);
else
printf("%d", r);
}
else
{
if(r>q)
printf("%d", r+q);
else
printf("%d", q);
}
return 0;
}
a) 6
b) 4
c) 10
d) 14
Solution: (c) 10. Here p>q as p=6, q=4 so the first if condition is true. But the condition p>r is not true. So
the else condition is satisfied of the first if block.
a) Only I
b) Only II
c) Both I and II
Week 3: Assignment Solution
Solution: (a) The ‘else’ block is executed when condition is false. One ‘if’ statement have only one ‘else’
statement.
Solution: (b) modulo division operator ‘ %’ can be applied only on int variables.
#include<stdio.h>
int main()
{
if(0)
printf(" C programming\n");
if(0.5)
printf("Java \n");
if(-0.5)
printf("Python \n");
return 0;
}
a) C programming
b) Java
Python
c) C programming
Java
d) Compilation error
Solution: (b) Any non-zero number inside the if condition is always true. So if(0.5) and if(-0.5) are true. Thus the
printf corresponding to 0.5 and -0.5 will be printed. But if(0) is false.
#include<stdio.h>
int main()
{
int p=2;
int k;
k= p>10?2:10;
printf("%d",k);
return 0;
}
a) 2
b) 8
c) 10
d) 12
Week 3: Assignment Solution
Solution: (c)10. It is a ternary operator. Here condition is false. So the second expression will execute.
#include<stdio.h>
int main()
{
int s=20;
if(s=19)
printf("Right\n");
else
printf("Wrong\n");
return 0;
}
a) Right
b) Wrong
c) 0
d) No output
Solution: (a) s=19 is an assignment not comparison. If (non zero) is always true.
#include<stdio.h>
int main()
{
int a=2, b=3, c=5, d=8, ans;
ans=a-b+c*a/d%b;
printf(" The answer will be %d", ans);
return 0;
}
Solution: (b)
Following the precedence rule, we can conclude that the operation steps are
ans=2-3+5*2/8%3
ans=-1+10/8%3
ans=-1+1%3
ans=-1+1
ans=0
Week 4 Assignment Solution
a TRUE
b FALSE
c Depends on the implementation
d None of the above
a True
b False
c Both ‘True’ and ‘False’ are printed
d Compilation error
Solution: (c) ‘a--’ post-increments 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
Week 4 Assignment Solution
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.
Solution: (c) Condition1 will be evaluated first; condition2 will be evaluated only if the
condition1 is TRUE. This is called the Short-circuited evaluation of the operators
6. Which one of the following is the correct syntax for C Ternary Operator?
a) condition ? expression1 : expression2
b) condition : expression1 ? expression2
c) condition ? expression1 < expression2
d) condition < expression1 ? expression2
Solution: (a) If the condition is true, expression 1 is evaluated. If the condition is false, expression
2 is evaluated.
This is a well-known method to swap the two variables without using a third variable.
8. What will be the output?
#include <stdio.h>
int main()
{
int x=0;
x = printf("3");
printf("%d",x);
return 0;
}
a 11
b 33
c 31
d 13
Solution: (c ) 31
x = printf("3");
First printf prints 3. printf() returns 1. Now the variable x=1; So 1 is printed next.
a 0
12
b 1
11
c 0
00
d 0
11
Solution: (d)
Inside the first printf statement, i++ results in 0 (due to post increment operation). As the left
operand of && operator is zero, the compiler will not evaluate the right operand (i.e., ++j). So, j
is not incremented after the first printf statement. Finally, the result is i=1 and j=1.
10. What will be the value of a, b, and c after the execution of the following
int a = 5, b = 7, c = 111;
c /= ++a * b--;
Week 4 Assignment Solution
a)10 9 8 7 6 5 4 3 2 1
b) 1 2 3 4 5 6 7 8 9 10
c) No output
d) Error
Solution: (c)The test condition is not proper at the beginning. So, loop will not
execute.
Solution: (d) ‘do-while’ loop is not always an infinite loop. It depends on the
condition mentioned inside the while statement.
‘do-while’ loop is an exit-controlled loop.
4. Compute the printed value of ‘m’ and ‘n’ of the C program given below
Week 5 Assignment Solution
#include <stdio.h>
int main()
{
int m= 0, n = 0;
while (m<5, n <7)
{
m++;
n++;
}
printf("%d, %d\n", m, n);
return 0;
}
a) 5,7
b) 5,5
c) 7,7
d) 0,0
Solution: (c) The while condition checks the last condition (i.e. n<7) and till the
condition is satisfied the block inside the loop is executed. Thus the loop is run for
7 times and both the values of m and n are incremented by 7.
5. What should be in the place of ****** so that except i=8, rest of the values of i
(as defined in the ‘for’ loop: i=0 to i=19) will be printed?
#include <stdio.h>
int main()
{
int i = 0;
for (i = 0;i< 20; i++)
{
if(i==8)
{
**********;
}
printf("i=%d\n",i);
}
return 0;
}
a) break
b) continue
c) switch
d) exit
Week 5 Assignment Solution
Solution: (b) continue forces the next iteration of the loop to take place skipping
any code in between.
a) NPTEL
b) IIT/IISc
c) NPTELSWAWAMIIT/IISc
d) Compilation error
Solution: (c) Here no ‘break’ statement is given. When a match is found, the
program executes the statements following that case, and all subsequent case and
default statements as well.
7. What will be the output?
#include <stdio.h>
int main()
{
int k,j;
for (k=1,j=3;k<=3,j>=1;k++,j--)
{
printf("%d,%d\n",k,j);
}
return 0;
}
a) 1,3
b) 1,3
3,1
c) 1,3
2,2
3,1
d) 0,0
Week 5 Assignment Solution
Solution: (c) Multiple variables can be initialized as well as multiple condition ,and
propagation can be made in a single ‘for’ loop just by using a comma ‘,’.So k value
will be increased upto 3 and j value will be decreased to 1.
Solution: (b) for loop will be continue for 3 times(p=0,1,2) and 4 will be print three
times. Here, the variable ‘p’ inside the for loop is a local variable. So, this local
variable does not change the condition of the for loop.
9.
For the C program given below, if the input given by the user is 7. What will be
shown on the output window?
#include <stdio.h>
int main()
{
int n,i=2;
scanf("%d",&n);
do
{
if(n%i==0)
{
printf("The number is odd");
}
i++;
}
while(i<n);
printf("The number is prime");
return 0;
}
Week 5 Assignment Solution
a) The number is odd
b) The number is prime
c) The number is odd The number is prime
d) Syntax Error
10. What will be the output? (Answer according to the output you get on the output window and the
options provided, though it prints some values previously.)
#include <stdio.h>
int main()
{
int i=0;
for(;;)
{
if(i==10)
continue;
printf("%d ",++i);
}
return 0;
}
a) 0 1 2 3 4 5 6 7 8 9 11 12…..infinite times
b) 1 2 3 4 5 6 7 8 9 11 12….infinite times
c) Won’t print anything
d) Error
Solution: (b)
2. An integer array of dimension 10 is declared in a C program. The memory location
of the first byte of the array is 1000. What will be the location of the 9th element of
the array? (Assume integer takes 4 bytes of memory and the element stored at 1000
is identified as 1st element)
a) 1028
b) 1032
c) 1024
d) 1036
Solution: (b) Integer takes four bytes of memory. As the memory assignment to the
elements are consecutive and the index starts from 0, the 9th element will be located at
1000+(8×4)
a) 1
b) 2
c) 3
d) 4
Solution: (a) The program finds the minimum element of an array. Hence, the output
is 1.
5. What actually gets passed when you pass an array as an argument to a function
a) 5, 4
b) 5, 5
c) 4, 4
d) 3, 4
Solution: (c)
a) 5
b) 6
c) 9
d) 10
Solution: (c)
x[i] is equivalent to *(x + i),
so (buf + 1)[5] is *(buf + 1 + 5), i.e. buf[6]=9.
10. How many ‘a’ will be printed when the following code is executed?
#include <stdio.h>
int main()
{
int i = 0;
char c = 'a';
while (i < 5)
{
i++;
Week 6 Assignment Solution
switch (c)
{
case 'a':
printf("%c ", c);
break;
}
}
printf("a\n");
return 0;
}
a) I
b) II
c) Both I and II
d) None
Solution: (b)
3. Which of the following function is more appropriate for reading in a multi word
string?
a) scanf()
b) gets()
c) printf()
d) puts()
Solution: (b)
Solution: 40
3[a] denotes the 4th element of the array.
a) Array
b) Array String
c) Array\0String
d) Compilation error
Solution:(a)
a) Programming
b) Language
c) ProgrammingLanguage
d) LanguageProgramming
Solution: (d)
Assignment 7 July 2022 Solution
a) assignment
b) tnemngissa
c) nothing will be printed
d) tttttttttt
Solution: (c) nothing will be printed as the string termination character ‘\0’ is assigned to first
element of array p[].
if(p==q)
{
printf("Two strings are equal");
}
return 0;
}
Solution: (c) No output, as we are comparing both base addresses and they are not same.
Assignment 8 July 2022 Solution
a) 70
b) Garbage value
c) Compilation error
d) None
Solution: (b) The function f() is called from the main. After printing ‘1’, foo() will be
called, and ‘2’ will be printed. This is an example of a nested function.
a) Choice1
b) CChoice1
Assignment 8 July 2022 Solution
c) DefaultChoice1
d) CChoice1Choice2
Solution: (b)
Solution: The first printf statement inside the switch prints ‘C’ and returns 1 (i.e. the
number of elements/characters inside printf) to the switch i.e. switch(1), therefore the
case 1 is invoked and Choice1 is printed. So, the whole answer is CChoice1.
a) Compilation error
b) 0, 1, 2 …….., 127
c) 0, 1, 2, …….., 127, -128, -127,…infinite loop
d) 1, 2, 3…….,127
Solution: (c)
Because the character data type range varies from -128 to 127, after reaching the value of
127, the value of x changes to -128 by incrementing and therefore the loop runs all over
again, and the process continues.
return 0;
}
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.
return 1;
}
The function above has a flaw that may result in a serious error during some
invocations.
Which one of the following describes the deficiency illustrated above?
(a) For some values of n, the environment will almost certainly exhaust its stack
space before the calculation completes.
(b) An error in the algorithm causes unbounded recursion for all values of n.
(c) A break statement should be inserted after each case. Fall-through is not
desirable here.
(d) The fibonacci() function includes calls to itself. This is not directly supported by
Standard C due to its unreliability.
Solution (a) For some values of n, the environment will almost certainly exhaust its stack
space before the calculation completes.
The function calls itself. So it's an example of recursion. As there is no proper terminating
condition for the function, for a large value of n, it will exhaust the storage space (stack),
which will stop the execution without a final result.
int main()
{
float result, age[] = { 23.4, 55, 22.6, 3, 40.5, 18 };
result = func(age);
printf("Result is=%0.2f", result);
return 0;
}
Ans: 27.08
Assignment 8 July 2022 Solution
Solution: The code essentially calculates the average of the provided numbers.
Assignment 9 July 2022 Solution
1. What is the best case complexity of ordered linear search and worst case complexity of
selection sort respectively?
a) O(1), O(n2)
b) O(logn), O(1),
c) O(n), O(logn)
d) O(n2), O(nlogn)
a) I & II
b) Only I
c) I and III
d) I, II & III
3. What is the recurrence relation for the linear search recursive algorithm?
a) T(n-2)+c
b) 2T(n-1)+c
c) T(n-1)+c
d) T(n+1)+c
4. Given an array arr = {20, 45, 77, 89, 91, 94, 98,100} and key = 45; what are the mid values
(corresponding array elements) generated in the first and second iterations?
a) 91 and 98
b) 89 and 45
c) 89 and 77
d) 91 and 94
After 1st pass: 4, 10, 7, 23, 67, 12, 5 – 4 and 10 got swapped or 4 got inserted at its appropriate
position.
After 2nd pass: 4, 7, 10, 23, 67, 12, 5 – 7 got inserted at its appropriate position.
After 3rd pass: 4, 7, 10, 23, 67, 12, 5 – No change as the value 10 is placed at its appropriate
position already.
6. Select the code snippet which performs unordered linear search iteratively?
continue;
}
}
return index;
}
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.
7. Which of the following input will give worst case time complexity for selection sort to sort
an array in ascending order?
I. 1, 2, 3, 4, 5, 6, 7, 8
II. 8, 7, 6, 5, 4, 3, 2, 1,
III. 8, 7, 5, 6, 3, 2, 1, 4
a) I
b) II
c) II and III
d) I,II and III
Ans : (d)
Selection sort has the same time complexity for best, average and worst cases. Therefore, all
the three arrays will be sorted at the same time complexity of O(n2).
8. 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.
*step 2: 1 4 5 9 3
*step 3: 1 3 4 5 9
Solution: (b) A sorting algorithm is called stable if it keeps elements with equal keys in the
same relative order in the output as they were in the input.
10. The average case occurs in the Linear Search Algorithm when
a) The item to be searched is in some where middle of the Array
b) The item to be searched is not in the array
c) The item to be searched is in the last of the array
d) The item to be searched is either in the last or not in the array
Solution: (a)
The average case occurs in the Linear Search Algorithm when the item to be searched
is in some where middle of the Array.
The best case occurs in the Linear Search Algorithm when the item to be searched is
in starting of the Array.
The worst case occurs in the Linear Search Algorithm when the item to be searched is
in end of the Array.
So, option (A) is correct.
Week 10 Assignment Solutions
a) n1 = 9, n2 = 10
b) n1 = 6, n2 = 11
c) n1 = 7, n2 = 12
d) n1 = 6, n2 = 10
Solution: (b) NPTEL when taken as a string, ends with a null character (\0). The Null
character in the C programming language is used to terminate the character strings. Therefore
the size of the str1 is 6 (NPTEL + Null). Whereas when PROGRAMMING is stored in str2,
they are taken as individual characters, as a result there is no null character at the end. Hence,
size of str2 is 11 (PROGRAMMING).
3. 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: (d)
Week 10 Assignment Solutions
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).
Solution: (b)(½)n(n-1)
The total number of comparisons, therefore, is (n - 1) + (n - 2) ... (2) + (1) = n (n - 1)/2
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.
7. Which of the following statement is correct for the 2 arrays with respect to A and B.
int *x[5];
int *(y[5]);
A. Array of pointers
B. Pointer to an array
a) x is A, y is B
b) x is A, y is A
c) x is B, y is A
d) y is B, y is B
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)
𝑓(𝑥) = 𝑥𝑒 2𝑥 − 3𝑥 2 − 5
Solution: 1.0312
The root lies between 1 and 2. Using bisection method, we can find the root as 1.0312 (up to
three decimal accuracy)
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
a) Only I
b) Only II
c) Both I & II
d) None of the above
a) 12.78
b) 13.08
c) 15.20
d) 11.36
Solution: (c)
𝑥−𝑥 (12 − 15)(12 − 20) 24
𝐿 (𝑥 ) = = = = 0.48
𝑥 −𝑥 (10 − 15)(10 − 20) 50
a) 5446.3
b) 5336.2
c) 4986.5
d) 5278.4
Solution: (a)
Week 11 Assignment Solution
𝑓 (𝑏) − 𝑓(𝑎)
𝑓(𝑥)𝑑𝑥 = (𝑏 − 𝑎)
2
Here, 𝑎 = 0, 𝑏 = 3, 𝑓(𝑎) = 0 and 𝑓(𝑏) = 3630.859. Hence, ∫ 𝑥 𝑒 𝑑𝑥 = 5446.3
d) = 𝑥𝑠𝑖𝑛(𝑦) − 𝑥 𝑒 , 𝑦(0) = 5
Solution: (c) The LHS contains the derivative part. All other terms are shifted to right
side.
5. Given 4 + 𝑥 = 𝑦 , y(0.5)=2, and using a step size of h 0.2 , Find the value of
𝑦(0.7) using Runge-Kutta 4th order method is
a) 2.8634
b) 2.5546
c) 2.1865
d) 1.9856
6. What will be the area under the curve using the Trapezoidal Rule
a) 4.3829
b) 5.4863
c) 6.3427
d) 3.2857
∫ydx=0.22[4.3215+6.8762+2×(4.7428+5.5205+6.0525)]
∫ydx=0.22[4.3215+6.8762+2×(16.3158)]
∫ydx=4.3829
Solution by Trapezoidal Rule is 4.3829
7. The real root of the equation 5x − 2cosx −1 = 0 (up to two decimal accuracy) is
[You can use any method known to you. A range is given in output rather than single
value to avoid approximation error]
a) 0.45 to 0.47
b) 0.35 to 0.37
c) 0.41 to 0.43
d) 0.53 to 0.56
I. The bisection method is guaranteed to work for finding roots of all continuous
functions.
II. Trapezoidal rule is a technique for approximating the indefinite integral.
III. Lagrange polynomial is used for Polynomial Interpolation.
a) Only I
b) Only II
c) II and III
d) None
Solution: (b) Trapezoidal rule is a technique for approximating the definite integral.
a) 1.68
b) 1.92
c) 1.86
d) 1.66
Solution: (a)
Week 12 Assignment Solution
Solution: (c) The scope of the member name is confined to the particular structure, within
which it is defined
int main()
{
int *ptr;
ptr = (int*)malloc(sizeof(int)*4);
ptr = realloc(ptr,sizeof(int)*2);
return 0;
}
Solution: 8
First, using malloc 16 bytes were allocated. Thereafter, applying calloc the size is adjusted and
made 8.
Week 12 Assignment Solution
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.
a) 11100
b) 11
c) 11001
d) 11000
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);
}
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.
int main()
{
struct p p1[] = {1, 90, 62, 33, 3, 34};
struct p *ptr1 = p1;
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.