0% found this document useful (0 votes)
36 views

Assignment2 July2024

NPTEL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Assignment2 July2024

NPTEL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Problem Solving through Programming in C

Week 02 Assignment Solution

1. Which of the following correctly defines a function in C?

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 are true

Solution: (d) All of the above are true

Explanation: A function in C is indeed a block of statements designed to perform a


specific task. It acts as a fundamental modular unit, has a name, and can be used
repeatedly.

2. If an integer requires two bytes of storage, what is the maximum value of an unsigned
integer in C?

a) 215 – 1
b) 216 – 1
c) 215
d) 216

Solution: (b) 216 - 1

Explanation: An unsigned integer uses all bits for the value. With 16 bits, the maximum
value is 216 - 1 (65535).

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 are correct

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


Problem Solving through Programming in C
Week 02 Assignment Solution

4. What will be the output of the following code?

#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("%d %d", a, b);
return 0;
}

a) 10 5
b) 5 10
c) 0 15
d) Compilation error

Solution: (a) 10 5

Explanation: This code swaps the values of ‘a’ and ‘b’ without using a temporary
variable.

5. The following code will print _______.

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. What will be the output of the following code?

#include <stdio.h>
#define SQUARE(x) x*x
int main() {
int result = SQUARE(2+3);
printf("%d", result);
return 0;
}
Problem Solving through Programming in C
Week 02 Assignment Solution

a) 25
b) 13
c) 11
d) Compilation error

Solution: (c) 11

Explanation: Explanation: The macro expands to 2+3*2+3, which is evaluated as 2 + 6 +


3.

7. Which of the following header files is not a standard C library?

a) stdlib.h
b) math.h
c) iostream.h
d) stdio.h

Solution: (c) iostream.h

Explanation: ‘iostream.h’ is a C++ header file, not a C standard library header.

8. What is the primary use of the ‘printf’ function in C?

a) To read input from the user


b) To perform mathematical calculations
c) To display output on the screen
d) To allocate memory dynamically

Solution: (c) To display output on the screen


Explanation: The ‘printf’ function is used to display output on the screen.

9. What is the purpose of the ‘return’ statement in C?

a) To terminate a loop
b) To end a program
c) To exit a function and return a value
d) To declare a variable

Solution: (c) To exit a function and return a value


Explanation: The ‘return’ statement is used to exit a function and optionally return a
value to the caller.

10. What is typecasting in C?


Problem Solving through Programming in C
Week 02 Assignment Solution

a) Assigning a value to a variable


b) Converting a variable from one data type to another
c) Defining a new data type
d) Initializing a variable with a constant value

Solution: (b) Converting a variable from one data type to another


Explanation: Typecasting is the process of converting a variable from one data type to
another.

You might also like