Week 5 Assignment Solution
Week 5 Assignment Solution
a) Error
b) I love C -- will be printed 6 times
c) I love C -- will be printed 3 times
d) I love C –will be printed 5 times
Solution: (c) I love C will be printed 3 times
a) 0
b) 1
c) No output
d) Compiler error
Solution: (d) Break statement is applicable in loop and switch statements. It is not allowed inside if
statement. Thus the program will show compiler error.
a) 5
b) 4
c) 1
d) No output
Solution: (d) 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.
}
return 0;
}
a) Syntax error
b) 0 1 2 3 4 5 6 7 8 9 10
c) 1 2 3 4 5 6 7 8 9 10
d) 0123456789
Solution: (c)
for(; ;) is possible in c, there is no need to place condition with in the for(), you can place condition within
the body of the loop. The ++i makes it printing from 1 to 10.
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.
Solution: (b) The loop will be continued till any of the condition k<=4 or j<=3 is satisfied. So, the loop will be
executed 3 times. Thus, the value of k and j would be 7 and 4.
10. What will be the value of ‘i’ after the execution of the program below
#include <stdio.h>
int main()
{
int i=1,j;
for(j=0;j<=10;j+=i)
{
i=i+j;
}
return 0;
}
a) 10
b) 11
c) 12
d) 13
Solution: (d) The value of j will reach to 8 and i will be 13. In the next iteration, j will become 8+13=21 and the
condition inside for loop will be invalid, thus the compilation will come out of the loop. So, the value of i will be 13.
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 1: Assignment Solution
1. CPU comprises of
a) ALU- Arithmetic and Logic Unit
b) Registers
c) Control unit
d) All of the above
Solution: (d)
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.
a) 4
b) 8
c) 16
d) 20
a) 21
b) 28
c) 30
d) 40
Solution: (b) The flowchart finds the sum of first 7 natural numbers. Hence, the right answer is 28.
Week 1: Assignment Solution
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.
c) Efficient coding
d) All of these
Solution: (d)
Week 2: Assignment Solution
2. What will be the output of the following code snippet? 'printf("%d", 5 == 5);'
(a) 5
(b) 0
(c) 1
(d) True
Answer: (c) 1
Explanation: In C, the '==' operator returns 1 for true conditions and 0 for false. Since 5
is equal to 5, it returns 1.
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.
5. What will happen if you try to store a value beyond the range of the declared data
type of a variable in C?
(d) No error
Explanation: Storing a value outside the range of the declared data type can lead to data
loss or wrap around.
(a) int
(b) float
(c) char
(d) string
(a) 2 bytes
(b) Depends on the compiler
(c) 4 bytes
(d) 1 byte
Explanation: The size of 'int' in C can vary depending on the compiler and the machine
architecture.
(a) const
(b) define
(c) static
(d) fixed
Explanation: The 'const' keyword is used to declare a variable as constant, which means
its value cannot be altered.
(a) int
(b) float
(c) void
(d) char
Week 2: Assignment Solution
10. In the following C code, what will be the output after replacing the blanks with the
correct logical operator?
#include <stdio.h>
int main () {
int x = 4, y = 5;
if (x __ 4 && y __ 5)
printf("True");
else
printf("False");
return 0;
}
(a) ==, !=
(b) !=, ==
(c) ==, ==
(d) <, >
Explanation: The correct logical operators to check if 'x' is equal to 4 and 'y' is equal to 5 are
'==' for both variables.
Week 3 Assignment Solution
Solution: (a) The precedence order follows the first option (a)
a) -1
b) 1
c) 0
d) Compilation error
Solution: (d) 'int to binary ' operator ‘%' cannot be operated on floating variable. Thus i%2 is not a
valid operation in C. The compiler will show error at this step.
3. Find the output of the following C code. (% indicates modulo operation, which results the
reminder of a division operation)
#include<stdio.h>
int main()
{
int a=10, b=3, c=2, d=4, result;
result=a+a*-b/c%d+c*d;
printf("%d", result);
return 0;
}
a) -42
b) 24
c) 15
d) -34
Solution: (c) Following the precedence rule, we can conclude that the operation steps are
Result=10+10*- 3/2%4+2*4
Result=10-30/2%4+2*4
Result=10-15%4+2*4
Result=10-3+2*4
Result=10-3+8
Result=7+8
Result=15
int h = 8;
int b = 4 * 6 + 3 * 4 < h*5 ?4 : 3;
printf("%d\n", b);
return 0;
}
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) 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 ((3 > 2) > 1) which becomes (1 > 1), thus FALSE
a) IITKGP
Week 3 Assignment Solution
Solution: (c)
a) 0
b) 1
c) 7
d) Compilation error
Solution: (a) 0
a) True
b) False
c) Both ‘True’ and ‘False’
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-increments 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, and correspondingly, both True and False will be printed.
5. Which of the following statements correctly describes the short-circuit behavior in logical
expressions?
a) The right-hand side of an && operation is evaluated even if the left-hand side is false.
b) The right-hand side of an || operation is evaluated even if the left-hand side is true.
c) The right-hand side of an && operation is not evaluated if the left-hand side is false.
d) The right-hand side of an || operation is not evaluated if the left-hand side is true.
Week 4 Assignment Solution
Solution: (c) The right-hand side of an && operation is not evaluated if the left-hand side is
false.
This principle optimizes logical expressions by not evaluating the second operand if the first
operand is sufficient to determine the outcome. For &&, if the left-hand side is false, the
overall expression cannot be true, so the right-hand side is skipped. For ||, if the left-hand side
is true, the overall expression must be true, so the right-hand side is skipped.
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.
7. Given the code snippet, what will be the value of ‘a’ after execution?
int a = 5, b = 10;
a += a <= b;
a) 5
b) 6
c) 10
d) 11
Explanation: The expression a <= b evaluates to true (1), so 1 is added to a (5 + 1), making
the final value of a equal to 6.
a) 5
b) 6
c) 3
d) 4
Explanation: The condition x > y is true, so the ternary operator selects x++ for execution.
However, because it uses the post-increment operator, the value of x before the increment (5)
is printed. x is then incremented to 6 after being evaluated.
Explanation: The condition a-- > 10 checks the value of a before it is decremented. On the
first check, a is 10, which is not greater than 10, so the loop body is not executed, resulting in
no output.
a) 1 2
b) 2 1
c) 1
d) Compilation error
Explanation: Due to the way the printf function is called, only the first expression after the
format string is evaluated as part of the conditional statement. The second conditional operation
is ignored because the format string does not specify another placeholder for an integer.
Therefore, the output is the result of the first conditional operator, which is 1.
Week 6 Assignment Solution
1. What does the expression sizeof(arr) / sizeof(arr[0]) evaluate to, where arr is an integer array of 100
elements?
a) 100
b) 400
c) 25
d) 4
Answer: a) 100
Explanation: The size of operator gives the total size in bytes of the array. For an integer array arr[100],
assuming an integer is 4 bytes, sizeof(arr) equals 400 bytes. sizeof(arr[0]) equals 4 bytes (size of one
integer). Thus, 400 / 4 evaluates to 100, giving the number of elements in the array.
2. Considering a two-dimensional array declared as int arr[3][4];, what is the correct way to access the
second element of the third row?
a) arr[3][2]
b) arr[2][1]
c) arr[2][3]
d) arr[1][2]
Answer: b) arr[2][1]
Explanation: In C, array indexing starts from 0. So, the third row is indexed as 2 (arr[2]) and the second
element of this row is indexed as 1 (arr[2][1]).
3. How does a C program react when an attempt is made to access an array element using a floating-point
index, such as arr[1.5]?
4. In a C program, what is the outcome of declaring an array with a negative size, such as int arr[-10];?
5. An integer array of size 15 is declared in a C program. The memory location of the first byte of the
array is 2000. What will be the location of the 13th element of the array?
Week 6 Assignment Solution
a) 2013
b) 2024
c) 2026
d) 2030
Explanation: (b) Integer takes two bytes of memory. As the memory assignment to the elements is
consecutive and the index starts from 0, the 13th element will be located at 2000+(12×2) =2024.
7. What is the effect of executing the statement int arr[5] = {1, 2}; in C?
a) The first two elements are set to 1 and 2, rest are uninitialized.
b) The first two elements are set to 1 and 2, rest are set to 0.
c) All elements are set to 1 and 2 alternatively.
d) The compiler generates an error.
Answer: B) The first two elements are set to 1 and 2; the rest are set to 0.
Explanation: In C, when an array is partially initialized, the uninitialized elements are automatically
initialized to 0.
a) 5,4
Week 6 Assignment Solution
b) 5,5
c) 4,4
d) 3,4
Answer: (c)
Explanation: The statement arr[1] = ++arr[1]; increments arr[1] (which is 2) before assignment.
So, arr[1] becomes 3. Then, a = arr[1]++; assigns arr[1] (which is now 3) to a and then
increments arr[1] to 4. So, a is 3. arr[1] = arr[a++]; assigns the value at arr[a] (which is
arr[3], value 4) to arr[1], and then a is incremented to 4. Finally, the program prints a and arr[1],
which are 4 and 4, respectively.
Explanation: (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.
Week 7: Assignment Solution
2. Which of the following function is more appropriate for reading in a multi-word string?
a) printf();
b) scanf();
c) gets();
d) puts();
Solution: (c) gets(); collects a string of characters terminated by a new line from the standard input
stream stdin
3. Applications of multidimensional array are?
a) Matrix-Multiplication
b) Minimum Spanning Tree
c) Finding connectivity between nodes
d) All of the mentioned
Solution: (d) For all of the above cases, multi-dimensional arrays are used.
int ary[2][3];
ary[][] = {{1, 2, 3}, {4, 5, 6}};
printf("%d\n", ary[1][0]);
return 0;
}
a) Compile time error
b) 4
c) 1
d) 2
Solution: (a) The initialization method of the array is not valid in C. The second dimension must be
specified.
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.
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20] = "hello", str2[20] = " world";
printf("%s", strcpy(str2, strcat(str1, str2)));
return 0;
}
a) hello
b) world
c) world hello
d) hello world
Solution: (d) hello world.
str1=hello, str2=world.
After strcat(str1,str2), str1=hello world. And strcpy makes str1=hello world.
char c;
char str[] = "Programming Language";
while(str[i]!=' ')
{
putchar (toupper(str[i]));
i++;
}
return 0;
}
a) Programming
b) PROGRAMMING LANGUAGE
c) PROGRAMMING
d) Syntax error
Solution: (c) While loop is executed till the space between the words. toupper() will convert the lower case
letter to upper case. Thus, PROGRAMMING will be the output.
Week 8: Assignment Solution
1. What is the purpose of a function prototype in C programming?
Answer: c) To declare the data types of the function's parameters and return type to the
compiler
Explanation: A function prototype in C programming serves to inform the compiler about the
return type of the function and the number and types of parameters it accepts. This helps the
compiler in type checking of function calls.
Explanation: Recursion requires a base case to prevent infinite calls and ensure termination. It
does not necessarily need a global variable and can be more memory efficient in some cases.
3. In the context of C programming, what does the static keyword do when used with a
variable within a function?
a) Makes the variable global
b) Preserves the variable's value between function calls
c) Initializes the variable to zero
d) Increases the scope of the variable to the entire program
Explanation: When the static keyword is used with a variable inside a function, it causes
the variable to retain its value between function calls. This variable is initialized only once,
the first time the function is called, and its value is preserved between subsequent calls to
the same function.
4. What will be the output of the following C code snippet if the input is 4?
int func(int n) {
if (n == 1)
return 1;
else
return n * func(n - 1);
}
printf("%d", func(input));
a) 10
b) 24
Week 8: Assignment Solution
c) 12
d) Undefined
Answer: b) 24
Explanation: This code defines a recursive function that calculates the factorial of a
number. For the input 4, the output will be 4 * 3 * 2 * 1 = 24, which is the factorial of 4.
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.
6. How many times the function get() will be invoked if get(6) is called from the main
function
void get(int n)
{
if (n<1) return;
get (n-1);
get (n-3);
}
a) 6 times
b) 25 times
c) 12 times
d) Infinite times
Solution: (b) This is a recursive function. You can easily verify the counting by declaring a global
int variable count and defining count+=1; inside the function.
Week 8: Assignment Solution
a) 70
b) Garbage value
c) Compilation error
d) None
a) 5
b) 8
c) 9
d) 20
Solution: (c)
f(20,1) = 9.
f(10,2) - 1 = 9
f(5,4) - 2 = 10
f(2,8) + 4 = 12
f(1,16) - 8 = 8
Week 8: Assignment Solution
f(0,32) + 16 = 16
a) 89
b) 90
c) 91
d) 92
Solution: (c)
f(95)–>f(f(106)) = f(96)–>f(f(107)) = f(97)–>f(f(108)) = f(98)–>f(f(109)) = f(99)–>f(f(110)) =
f(100)–>f(f(111))=f(101)=91
So the correct option is (c)
10. Consider the function
Solution: (a) 9
The inner while loop of func(435) will be true till num is zero. In every execution of while loop
count increases by 1 and num value is right shifted by 1. This condition will hold true for 9 times,
hence 9 will be the output.
Week 9 Assignment Solution
Selection sort creates a sub-list, LHS of the ‘min’ element is already sorted and RHS is yet to be sorted.
Starting with the first element the ‘min’ element moves towards the final element
2. What is the correct order of insertion sort (in ascending order) of the array arr[5]={8 3 5 9 4}?
a) {3 8 5 9 4} {3 5 8 9 4}{3 5 8 9 4}{3 4 5 8 9}
b) {3 8 5 9 4} {3 5 8 9 4} {3 5 8 4 9} {3 5 4 8 9}{3 4 5 8 9}
c) {3 8 5 9 4}{3 4 8 5 9}{3 4 5 8 9}{3 4 5 8 9}{3 4 5 8 9}
d) {8 3 5 4 9}{8 3 4 5 9}{3 4 5 8 9}
Solution: (a) In insertion sort, we start from the second number onwards and place it in appropriate position before
its current position. Thus, the correct answer is (a).
4. 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.
5. 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.
7. Given an array arr = {45, 77, 89, 91, 94, 98,100} and key = 100; what are the mid values
(corresponding array elements) generated in the first and second iterations?
a) 91 and 98
b) 91 and 100
c) 91 and 94
d) 94 and 98
b) int min;
for(int j=0; j<arr.length-1; j++)
{
min = j;
for(int k=j+1; k<=arr.length; k++)
{
if(arr[k] < arr[min])
min = k;
}
int temp = arr[min];
arr[min] = arr[j];
arr[j] = temp;
}
c) int min;
for(int j=0; j<arr.length-1; j++)
{
min = j;
for(int k=j+1; k<=arr.length-1; k++)
{
if(arr[k] > arr[min])
min = k;
}
int temp = arr[min];
arr[min] = arr[j];
arr[j] = temp;
}
d) int min;
for(int j=0; j<arr.length-1; j++)
{
min = j;
for(int k=j+1; k<=arr.length; k++)
{
if(arr[k] > arr[min])
min = k;
}
int temp = arr[min];
arr[min] = arr[j];
arr[j] = temp;
Week 9 Assignment Solution
}
Solution: (a)
Starting with the first element as ‘min’ element, selection sort loops through the list to select the
least element which is then swapped with the ‘min’ element.
9. Consider the array A[]= {5,4,9,1,3} apply the insertion sort to sort the array . Consider the cost
associated with each sort (swap operation) is 25 rupees, what is the total cost of the insertion sort
when element 1 reaches the first position of the array?
a) 25
b) 50
c) 75
d) 100
Solution: (b)
When the element 1 reaches the first position of the array two comparisons are only required hence
25 * 2= 50 rupees.
*step 1: 4 5 9 1 3.
*step 2: 1 4 5 9 3