0% found this document useful (0 votes)
791 views37 pages

Week 5 Assignment Solution

Nothing

Uploaded by

Sruthi Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
791 views37 pages

Week 5 Assignment Solution

Nothing

Uploaded by

Sruthi Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Week 5 Assignment Solution

1. In C three way transfer of control is possible using


a) Ternary operator
b) Unary operator
c) Logical operator
d) None of the above
Solution: (a) Ternary operator
2. The loop in which the statements within the loop are executed at least once is called
a) for
b) do-while
c) while
d) goto
Solution: (b) do-while

3. What will be the output?


#include <stdio.h>
int main()
{
float k = 0;
for (k = 0.5; k < 3; k++)
printf("I love C\n");
return 0;
}

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

4. What is the output of the following code?


#include <stdio.h>
int main()
{
int i=1;
do
{
printf("while vs do-while\n");
}while(i==1);
printf("Out of loop");
return 0;
}

a) ‘while vs do-while’ once


b) ‘Out of loop’ infinite times
c) Both ‘while vs do-while’ and ‘Out of loop’ once
d) ‘while vs do-while’ infinite times
Solution: (d) As the condition inside the while statement is always true, the loop will be executed infinite times and
the statement inside the loop will be printed infinite number of times.
Week 5 Assignment Solution

5. Find the output of the following C program


#include <stdio.h>
int main()
{
int i = 0;
if(i==0)
{
i=i+1;
break;
}
printf("%d", i);
return 0;
}

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.

6. What is the output of the following C program?


#include <stdio.h>
int main()
{
int a = 0, i, b;
for (i = 0; i <= 2; i+= 0.5)
{
a++;
continue;
}
printf("%d", a);
return 0;
}

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.

7. What will be the output?


#include <stdio.h>
int main()
{
int i=0;
for(;;)
{
if(i==10)
break;
printf("%d ", ++i);
Week 5 Assignment Solution

}
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.

8. What is the output of the following C code?


#include <stdio.h>
int main()
{
int a = 1;
if (a--)
printf("True\n");
if (++a)
printf("False\n");
return 0;
}

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.

9. What is the output of the below C program?


#include <stdio.h>
int main()
{
short int k=1, j=1;
while (k <= 4 || j <= 3)
{
k=k+2;
j+=1;
}
printf("%d,%d", k, j);
return 0;
}
a) 5,4
b) 7,4
c) 5,6
d) 6,4
Week 5 Assignment Solution

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

1. Which are true about interpolation?


I. It is a process for finding a value between two points on a line or curve.
II. Interpolation provides a mean for estimating functions at the intermediate points.

a) Only I
b) Only II
c) Both I & II
d) None of the above

Solution: (c) Both

2. A Lagrange polynomial passes through three data points as given below


𝑥 10 15 20
𝑓(𝑥) 19.45 10.63 7.82
The polynomial is determined as 𝑓 (𝑥 ) = 𝐿 (𝑥 ). (19.45) + 𝐿 (𝑥 ). (10.63) +
𝐿 (𝑥 ). (7.82)
The value of 𝑓(𝑥) at 𝑥 = 12 is

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

𝑥−𝑥 (12 − 10)(12 − 20) −16


𝐿 (𝑥 ) = = = = 0.64
𝑥 −𝑥 (15 − 10)(15 − 20) −25

𝑥−𝑥 (12 − 10)(12 − 15) −6


𝐿 (𝑥 ) = = = = −0.12
𝑥 −𝑥 (20 − 10)(20 − 15) 50

So 𝑓(12) = 0.48 ∗ 19.45 + 0.64 ∗ 10.63 − 0.12 ∗ 7.82 = 15.2008

3. The value of ∫ 𝑥 𝑒 𝑑𝑥 by using one segment trapezoidal rule is

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

4. To solve the ordinary differential equation


𝑑𝑦
10 + 𝑥 𝑒 = ycos(𝑥 ) + 𝑥𝑠𝑖𝑛(𝑦) , 𝑦(0) = 5
𝑑𝑥

using Runge-Kutta 4th order method, the equation is re-written as

a) = ycos(𝑥) + 𝑥𝑠𝑖𝑛(𝑦) , 𝑦(0) = 5

b) = (ycos(𝑥) + 𝑥𝑠𝑖𝑛(𝑦)) , 𝑦(0) = 5

c) = (ycos(𝑥) + 𝑥𝑠𝑖𝑛(𝑦) − 𝑥 𝑒 ), 𝑦(0) = 5

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

Solution: (b) 2.5546

6. What will be the area under the curve using the Trapezoidal Rule

x 1.4 1.6 1.8 2.0 2.2


y 4.3215 4.7428 5.5205 6.0525 6.8762

a) 4.3829
b) 5.4863
c) 6.3427
d) 3.2857

Solution: (a) 4.3829


Week 11 Assignment Solution

Using Trapezoidal Rule


∫ydx=h2[y0+y4+2(y1+y2+y3)]

∫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

Solution: (d) 0.53 to 0.56


8. Which is/are false?

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.

9. Find the root of 𝑥 − 𝑥 − 10 = 0 approximately upto 5 iterations using Bisection


Method. Let a = 1.5 and b = 2.

a) 1.68
b) 1.92
c) 1.86
d) 1.66

Solution: (c) 1.86


Week 11 Assignment Solution

10. Match the following

A. Newton Method 1. Integration


B. Lagrange Polynomial 2. Root finding
C. Trapezoidal Method 3. Differential Equation
D. RungeKutta Method 4. Interpolation

a) A-2, B-4, C-1, D-3


b) A-3, B-1, C-2, D-4
c) A-1, B-4, C-3, D-2
d) A-2, B-3, C-4, D-1

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)

2. Input device/s of a computer


a) Printer
b) Speaker
c) Joystick
d) Monitor

Solution: (c) Joystick

3. Choose the correct statements from the following


i) In high-level language, testing and debugging a program is difficult than assembly language.
ii) C programs are highly portable on any type of operating system platform.
iii) A flowchart is a visual representation of the sequence of steps for solving a problem.
iv) The role of a compiler is to translate source program statements to decimal codes.

a) (i) and (ii)


b) (ii) and (iii)
c) (i), (ii), and (iii)
d) (ii), (iii), and (iv)

Solution: (b) (ii) and (iii) are correct.

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.

4. How many bits are there in a byte?


a) 2
b) 4
c) 8
d) 16

Solution: (c) 1 Byte=8 bits

5. What will be the output of the flowchart given below?


Week 1: Assignment Solution

a) 4
b) 8
c) 16
d) 20

Solution: (c) b=a+b=5,b=a*b+ b/5=3*5 + 5/5=15 +1= 16

6. The output of the following algorithm is

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.

9. An interpreter reads the source code of a program


a) one line at a time
b) two line at a time
c) complete program in one stroke
d) None of these

Solution: (a) one line at a time.

10. Flowchart and algorithms are used for


a) Easy testing and debugging
b) Better programming
Week 1: Assignment Solution

c) Efficient coding
d) All of these

Solution: (d)
Week 2: Assignment Solution

1. What is the correct way to declare a constant in C?

(a) int const a;


(b) constant int a;
(c) #define a
(d) Both (a) and (c)

Answer: (d) Both (a) and (c)

Explanation: In C, constants can be declared using 'const' keyword or '#define' preprocessor.

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.

3. A syntax error occurs when


a) The rules of grammar of the language is violated
b) The statements in the program have no meaning
c) The program gives wrong or undesired output
d) Some illegal operation (e.g. divide by zero) is done
Solution: (a) The rules of grammar of the language is violated
4. 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.

5. What will happen if you try to store a value beyond the range of the declared data
type of a variable in C?

(a) Compile-time error


(b) Run-time error
(c) Data loss or wrap around
Week 2: Assignment Solution

(d) No error

Answer: (c) Data loss or wrap around

Explanation: Storing a value outside the range of the declared data type can lead to data
loss or wrap around.

6. Which of the following is not a primitive data type in C?

(a) int
(b) float
(c) char
(d) string

Answer: (d) string

Explanation: In C, 'string' is not a primitive data type; it is typically represented using an


array of characters.

7. What is the size of an 'int' data type in C?

(a) 2 bytes
(b) Depends on the compiler
(c) 4 bytes
(d) 1 byte

Answer: (b) Depends on the compiler

Explanation: The size of 'int' in C can vary depending on the compiler and the machine
architecture.

8. Which keyword is used to create a constant variable in C?

(a) const
(b) define
(c) static
(d) fixed

Answer: (a) const

Explanation: The 'const' keyword is used to declare a variable as constant, which means
its value cannot be altered.

9. What is the default return type of a function in C if it is not explicitly specified?

(a) int
(b) float
(c) void
(d) char
Week 2: Assignment Solution

Answer: (a) int

Explanation: In C, if the return type of a function is not explicitly specified, it defaults to


'int'.

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) <, >

Answer: (c) ==, ==

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

1. The precedence of arithmetic operators is (from highest to lowest)


a) %, *, /, +, –
b) %, +, /, *, –
c) +, -, %, *, /
d) %, +, -, *, /

Solution: (a) The precedence order follows the first option (a)

2. What is the output of the following program?


#include <stdio.h>
int main()
{
float i = -3.0;
int k = i % 2;
printf(“%d”, k);
return 0;
}

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

4. What is the output of the following C code?


#include <stdio.h>
int main()
{
Week 3 Assignment Solution

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.

5. What will be the output?


#include <stdio.h>
int main ()
{
int a = 1, b = 2, c = 3;
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
}

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

6. Find the output of the following C code


#include <stdio.h>
int main()
{
int x=1;
if ((3>5) || (2!=3))
printf("IITKGP\n");
else if (x&=0)
printf("IITD\n");
else
printf("IITM\n");
return 0;
}

a) IITKGP
Week 3 Assignment Solution

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 ststement is
true. Thus IITKGP will be printed.

7. What will be the output?


#include <stdio.h>
int main()
{
int x=2;
if(x=1)
printf("TRUE");
else
printf("FALSE");
return 0;
}
a) TRUE
b) FALSE
c) Compilation Error
d) Compiler Dependent
Solution: (a) TRUE
if(x=1)... "=" is an assignment operator, so 1 will be assigned to x and condition will
be true due to if(1).

8. Which of the following statement is correct?


a) Operator precedence determines which operator is performed first in an
expression with more than one operator with different precedence.
Associativity is used when two operators of same precedence appear in an
expression
b) Operator associativity determines which operator is performed first in an
expression with more than one operator with different associativity.
Precedence is used when two operators of same precedence appear in an
expression
c) Operator precedence and associativity are same.
d) None of the above

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

9. Which of the following method are accepted for assignment?


a) 8=x=y=z
b) x=8=y=z
c) x=y=z=8
d) None
Week 3 Assignment Solution

Solution: (c)

10. What will be the output?


#include<stdio.h>
int main()
{
int x;
x= 9<5+3 && 7;
printf("%d", x);
return 0;
}

a) 0
b) 1
c) 7
d) Compilation error
Solution: (a) 0

This expression is equivalent to:


((9< (5 + 3)) && 7)
i.e., (5 + 3) executes first resulting into 8
then, first part of the expression (9< 8) executes resulting into 0 (FALSE)
Then, (0 && 7) executes resulting into 0 (FALSE)
Week 4 Assignment Solution
1. The loop which is executed at least once is
a) while
b) do-while
c) for
d) none of the above
Solution: (b) The do-while loop is executed at least once even though the condition is false.
2. In the C programming language, negative numbers, when used in if-else conditional
checking, are treated as
a) TRUE
b) FALSE
c) Depends on the implementation
d) None

Solution: (a) TRUE, all non-zero values are TRUE in C


3. Choose a statement to use the C If Else statement.
a) else if is compulsory to use with the if statement.
b) else is compulsory to use with an if statement.
c) else or else if is optional with the if statement.
d) None of the above
Solution: (c) else or else if is optional with the if statement

4. What is the output of the following C code snippet?


int a = 1;
if (a--)
printf("True\n");
if (++a)
printf("False\n");

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

Correct Answer: (b) 6

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.

8. What will be the output of the code snippet?


int x = 5, y = 3;
printf("%d", x > y ? x++ : y++);

a) 5
b) 6
c) 3
d) 4

Correct Answer: (a) 5

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.

9. What will the following code segment output?


Week 4 Assignment Solution
int a = 10;
while (a-- > 10) {
printf("%d ", a);
}

a) Prints numbers from 9 to 0


b) Prints 9 only
c) No output
d) Syntax error

Correct Answer: (c) No output

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.

10. What is the output of the code snippet below?


int x = 1, y = 2;
printf("%d", x < y ? x : y, x > y ? y : x);

a) 1 2
b) 2 1
c) 1
d) Compilation error

Correct Answer: (c) 1

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]?

a) The element corresponding to the floor of the index is accessed.


b) The element corresponding to the ceiling of the index is accessed.
c) A syntax error is reported by the compiler.
d) The program executes successfully without errors.

Answer: c) A syntax error is reported by the compiler.


Explanation: C does not allow non-integer index values for arrays. Using a floating-point number as an
index will result in a syntax error during compilation.

4. In a C program, what is the outcome of declaring an array with a negative size, such as int arr[-10];?

a) The compiler will treat it as a positive size.


b) The array will be initialized with zero elements.
c) The compiler will generate an error.
d) A memory allocation error will occur at runtime.

Answer: c) The compiler will generate an error.


Explanation: Declaring an array with a negative size is illegal in C, and it will cause a compile-time error.
The size of an array must be a positive integer.

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.

6. The elements of an array are stored in contiguous memory due to


a) This way, the computer can keep track of only the address of the first element, and the
addresses of other elements can be calculated.
b) The architecture of the computer does not allow arrays to store other than serially
c) Both
d) None
Explanation: (a) This way, the computer can keep track of only the address of the first element, and the
addresses of other elements can be calculated easily.

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.

8. How can you pass an entire array to a function in C?


a) By specifying the size of the array in the function parameter.
b) By passing the address of the first element of the array.
c) By declaring the function parameter as an array size.
d) By passing the array name only.

Answer: D) By passing the array name only.


Explanation: In C, an array can be passed to a function by simply passing the array name. This passes a
pointer to the first element of the array, effectively passing the entire array by reference.

9. Find the output of the following C program


#include<stdio.h>
int main()
{
int a;
int arr[5] = {1, 2, 3, 4, 5};
arr[1] = ++arr[1];
a = arr[1]++;
arr[1] = arr[a++];
printf("%d,%d", a, arr[1]);
return 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.

10. What will be the output?


#include <stdio.h>
int main()
{
int arr[]={1,2,3,4,5,6};
int i,j,k;
j=++arr[2];
k=arr[1]++;
i=arr[j++];
printf("i=%d, j=%d, k=%d", i,j,k);
return 0;
}

a) i=5, j=5, k=2


b) i=6, j=5, k=3
c) i=6, j=4, k=2
d) i=5, j=4, k=2

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

1. In C, the placement of elements of a two dimensional array is


a) Row wise
b) Column wise
c) Diagonal wise
d) Bottom to top wise
Solution: (a) In C the placement of 2D array in memory is row wise.

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.

4. What will be the output?


# include <stdio.h>
int main()
{
char str1[] = "Week-7-Assignment";
char str2[] = {'W', 'e', 'e', 'k', '-', '7', '-', 'A', 's','s','i','g','n','m','e','n','t'};
int n1 = sizeof(str1)/sizeof(str1[0]);
int n2 = sizeof(str2)/sizeof(str2[0]);
printf("n1 = %d, n2 = %d", n1, n2);
return 0;
}
a) n1=18, n2=17
b) n1=18, n2=18
c) n1=17, n1=17
d) n1=17, n2=18

Solution: (a) The size of str1 is 18 and size of str2 17.


When an array is initialized with string in double quotes, compiler adds a ‘\0’ at the end.

5. What is the output of the following C code?


#include <stdio.h>
int main()
{
Week 7: Assignment Solution

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.

6. What will be the output?


#include<stdio.h>
#include<string.h>
int main()
{
char p[] = "assignment";
char t;
int i, j;
for(i=0,j=strlen(p); i<j; i++)
{
t = p[i];
p[i] = p[j-i];
p[j-i] = t;
}
printf("%s", p);
return 0;
}
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[].

7. What will be the output?


# include <stdio.h>
int main()
{
int a[2][3] = {1, 2, 3, 4};
int i = 0, j = 0;
for (i = 0; i< 2; i++)
for (j = 2; j >=0; j--)
printf("%d", a[i][j]);
return 0;
}
Solution: 321004
Week 7: Assignment Solution

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.

8. What will be the output?

#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.

9. What will be the output?


#include<stdio.h>
int main()
{
int i;
char a[] = "";
if(printf("%s", a))
printf("The string is empty");
else
printf("The string is not empty");
return 0;
}
a) The string is empty
b) The string is not empty
c) Error
d) None
Solution: (b) The string is not empty

10. What is the output of the following C program?


#include <stdio.h>
#include <ctype.h>
int main ()
{
int i = 0;
Week 7: Assignment Solution

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?

a) To execute the function


b) To define the function body
c) To declare the data types of the function's parameters and return type to the compiler
d) To allocate memory for the function

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.

2. Which of the following statements about recursion in C is true?

a) A recursive function can never call itself.


b) Recursion requires a base case to terminate.
c) Every recursive function needs to have a global variable.
d) Recursion is less memory efficient than iteration.

Answer: b) Recursion requires a base case to terminate.

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

Answer: b) Preserves the variable's value between function calls

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.

5. What is the output of the following C program?


#include <stdio.h>
void foo(), f();
main()
{
f();
}
void foo()
{
printf("2 ");
}
void f()
{
printf("1 ");
foo();
}

a) Compiler error as foo() is not declared in main


b) 12
c) 21
d) Compile-time error due to declaration of functions inside main

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

7. What will be the output?


#include <stdio.h>
int main()
{
{
int a = 70;
}
{
printf("%d", a);
}
return 0;
}

a) 70
b) Garbage value
c) Compilation error
d) None

Solution: (c) Compilation error.


A Block is a set of statements enclosed within left and right braces ({and}, respectively). A
variable declared in a block is accessible in the block and all inner blocks of that block but not
accessible outside the block. Thus, the program produces a compiler error.
8. What will be the output?
#include<stdio.h>
int f(int n, int k)
{
if (n == 0)
return 0;
else if (n % 2)
return f(n/2, 2*k) + k;
else return f(n/2, 2*k) - k;
}
int main ()
{
printf("%d", f(20, 1));
return 0;
}

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

9. Consider the function


int fun(x: integer)
{
If x > 100 then fun = x – 10;
else
fun = fun(fun(x + 11));
}
For the input x = 95, the function will return

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

int func(int num)


{
int count = 0;
while(num)
{
count++;
num >>= 1;
}
return (count) ;
}
If ">>" this operation right shift num (binary representation of num) by one digit. E.g. if
num=60 then num>>1 is 30.
Then func(435) the value returned is
a) 9
b) 8
c) 0
d) 10

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

1. What is the worst case complexity of selection sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

Solution: (d) O(n2)

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).

3. When the Binary search is best applied to an array?


a) For very large size array
b) When the array is sorted
c) When the array elements are mixed data type
d) When the array is unsorted
Solution: (b) Binary search is applied for sorted array.

4. Select the code snippet which performs unordered linear search iteratively?

a) int unorderedLinearSearch(int arr[], int size, int data)


{
int index;
for(int i = 0; i < size; i++)
{
if(arr[i] == data)
{
index = i;
break;
}
}
return index;
}

b) int unorderedLinearSearch(int arr[], int size, int data)


{
int index;
for(int i = 0; i < size; i++)
{
if(arr[i] == data)
{
break;
}
}
return index;
Week 9 Assignment Solution
}
c) int unorderedLinearSearch(int arr[], int size, int data)
{
int index;
for(int i = 0; i <= size; i++)
{
if(arr[i] == data)
{
index = i;
continue;
}
}
return index;
}

d) None of the above

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.

6. Binary Search can be categorized into which of the following?


a) Brute Force technique
b) Divide and conquer
c) Greedy algorithm
d) Dynamic programming

Solution: (b) Divide and conquer


Since ‘mid’ is calculated for every iteration or recursion, we are diving the array into half and then try to
solve the problem.

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

Solution: (a) 91 and 98

8. Select the appropriate pseudo code that performs selection sort


Week 9 Assignment Solution
a) 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;
}

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

10. What will be the output?


#include <stdio.h>
#define a 1
int main()
{
printf("%d ",a);
int a=0;
printf("%d ",a);
return 0;
}
a) 1 0
b) 1 1
c) 0 0
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=0.

You might also like