Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Week 2 Assignment Solution

1. A function is

a) Block of statements to perform some specific task


b) It is a fundamental modular unit to perform some task
c) It has a name and can be used multiple times
d) All of the above

Solution: (d) All are true

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.

3. Which of the following statements is correct?


I. Keywords are those words whose meaning is already defined by Compiler.
II. Keywords cannot be used as variable names.
III. There are 32 keywords in C
IV. C keywords are also called reserved words.

a) I and II
b) II and III
c) I, II and IV
d) All of the above

Solution: (d) All of the above are correct.

4. What will be the output?


#include <stdio.h>
int main() {
int x = 1, y = 3;
int t = x;
x = y;
y = t;
printf("%d %d", x, y);
return 0;
}
Week 2 Assignment Solution

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.

5. 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: 18 (short answer type)


Apply the BODMAS rule to evaluate the expression.

6. Which of the following are not standard header files in C?


a) stdio.h
b) conio.h
c) string.h
d) All the above are standard header file

Solution: (d) all are valid c headers.

7. What is the output of the following code?


#include<stdio.h>
#define fun(x) (x*x-x)
void main()
{
float i;
i = 37.0/fun(2);
printf("%.2f", i);
}

Solution: 18.50

The pre-processing replaces fun(2) with (2*2-2). Thus fun(2)=2, so, i=37.0/2=18.50

8. Which of the following is not a C variable?


a) Var123
b) Var_123
c) 123Var
Week 2 Assignment Solution

d) X_123_Var

Solution: (c) Variable name must not begin with a digit. So, ‘123Var’ is an invalid variable
declaration in C.

9. What is the output of the following program?


#include <stdio.h>
#define a 6
int main()
{
int a =3;
a = a+1;
printf("%d", a);
return 0;
}

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

a) a=a-b; b=a-b; a=a+b;


b) a=a%b; b=a+b; a=a/b;
c) a=a+b; b=a-b; a=a-b;
d) None of the above
Solution: (c) a=a+b; b=a-b; a=a-b;

You might also like