0% found this document useful (0 votes)
39 views3 pages

Week-2-Jan-2025 Assignment Solution

The document provides solutions to a series of questions related to C programming, covering topics such as variable naming rules, variable types, program execution, keywords, functions, and output of specific code snippets. Key points include that variable names cannot start with a digit, all listed variable types are correct, and the execution of C programs is sequential. Additionally, it discusses the output of various code examples and the minimum value of signed integers in C.

Uploaded by

rakhiroshan475
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)
39 views3 pages

Week-2-Jan-2025 Assignment Solution

The document provides solutions to a series of questions related to C programming, covering topics such as variable naming rules, variable types, program execution, keywords, functions, and output of specific code snippets. Key points include that variable names cannot start with a digit, all listed variable types are correct, and the execution of C programs is sequential. Additionally, it discusses the output of various code examples and the minimum value of signed integers in C.

Uploaded by

rakhiroshan475
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/ 3

ASSIGNMENT 2 SOLUTION

1. Which of the following cannot be used as a variable in C programming?


a) Var123
b) Var_123
c) 123Var
d) X_123_Var

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

2. Which of the following is not a correct variable type in C?


a) int
b) double
c) char
d) All of above are correct variable type

Solution: (d) All of above are correct variable type in C

3. The execution of any C program is

a) Sequential
b) Parallel
c) Multi-threading
d) None of these

Solution: (a) The execution of the C program is sequential.

4. 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 as 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.

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


ASSIGNMENT 2 SOLUTION

6. What will be the output? [N.B: - .2f is used to print up to 2 decimal places of a
floating point number]
#include <stdio.h>
int main()
{
float a = 5.0;
printf ("The output is %.2f", (9/5)*a + 7);
return 0;
}

a) 28.2
b) 21.00
c) 16.00
d) 12.00

Solution: (d) 12.00

Since 9 and 5 are integers, integer arithmetic happens in subexpression (9/5) and
(9/5) will evaluate to 1 (not 1.8). The calculation will be as follows: (9/5)*a+10 =
1*5.0+7 = 12.00

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


#include <stdio.h>
int main()
{
int var = 0110;
var=var+7;
printf("%d", var);
return 0;
}

a) 106
b) 70
c) 79
d) Compiler error

Solution: (c) In 0110, the leading zero indicates that the number is in octal (base-8)
representation of 72. Thus 72 +7 = 79 will be stored in var.

8. If 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
ASSIGNMENT 2 SOLUTION

Solution: To find the minimum value of a signed integer in C, given that it uses 2 bytes
of storage:

 Number of bits: 2 bytes = 16 bits


 Signed representation: One bit is used for the sign (0 for positive, 1 for
negative)
 Effective bits for magnitude: 15 bits
 Minimum value: The minimum value is represented by all bits set to 1, except
the sign bit.

So the solution is option (d) −215

9. What will be the output of the program given below?


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

a) 9
b) 0
c) 1001
d) Compilation Error

Solution: (d) Compilation Error

Before the line a=9;, we need to declare the variable a. Since we are using %d in printf,
it's likely meant to be an integer:variable. therefore a compilation error.

10. What is the output?


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

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

You might also like