Session 6
Arithmetic functions
· C functions which are
used to perform mathematical operations in a program are called Arithmetic
functions.
·
Example program for
abs(), floor(), round(), ceil(), sqrt(), exp(), log(), sin(), cos(), tan(),
pow() and trunc() functions are given below.
1. What will be the output of the following program?
#include <stdio.h>
#include
<stdlib.h>
int main()
{
int m =
abs(200);
int n =
abs(-400);
printf("Absolute value of m = %d\n", m);
printf("Absolute value of n = %d \n",n);
return 0;
}
Ans: Absolute
value of m = 200
Absolute value of n = 400
Absolute value of n = 400
·
Explaination: abs( ) function in C returns the absolute value of an
integer. The absolute value of a number is always positive. Only integer
values are supported in C. “stdlib.h” header
file supports abs( ) function in C language.
2. What will be the output of the following program?
#include
<stdio.h>
#include
<math.h>
int main()
{
float i=5.1, j=5.9, k=-5.4, l=-6.9;
printf("floor of %f is %f\n", i, floor(i));
printf("floor of %f is %f\n", j, floor(j));
printf("floor of %f is %f\n", k, floor(k));
printf("floor of %f is %f\n", l, floor(l));
return 0;
}
|
OUTPUT:
floor of 5.100000 is 5.000000
floor of 5.900000 is 5.000000
floor of -5.400000 is -6.000000
floor of -6.900000 is -7.000000
floor of 5.900000 is 5.000000
floor of -5.400000 is -6.000000
floor of -6.900000 is -7.000000
Explanation: floor( )
function in C returns the nearest integer value which is less than or equal to
the floating point argument passed to this function. ”math.h” header file
supports floor( ) function in C language.
3.
#include <stdio.h>
#include <math.h>
int main()
{
float i=5.4, j=5.6;
printf("round of %f is %f\n", i, round(i));
printf("round of %f is %f\n", j, round(j));
return 0;
}
Output:
round of 5.400000 is 5.000000
round of 5.600000 is 6.000000
round of 5.600000 is 6.000000
Explanation: round( ) function in C returns the nearest integer value of the
float/double/long double argument passed to this function.
·
If decimal value is
from ”.1 to .5″, it returns integer value less than the argument. If
decimal value is from “.6 to .9″, it returns the integer value greater than the
argument.
4. What will be the output of the following program?
#include <stdio.h>
#include <math.h>
int main()
{
float i=5.4, j=5.6;
printf("ceil of %f is %f\n", i, ceil(i));
printf("ceil of %f is %f\n", j, ceil(j));
return 0;
}
Output:
ceil of 5.400000 is
6.000000
ceil of 5.600000 is 6.000000
ceil of 5.600000 is 6.000000
Explaination:
·
ceil( ) function in C
returns nearest integer value which is greater than or equal to the
argument passed to this function. ”math.h” header file supports ceil( )
function in C language.
5.Which of the following
is used the find the 2 power 4?
A.
Pow(4,2) B. Pow(2,4) C. Pow(4.0, 2.0) D.
Pow(2.0,4.0)
Ans: B
Explanation:
·
pow( ) function in
C is used to find the power of the given number.
·
”math.h” header file
supports pow( ) function in C language. Syntax for pow( ) function in
C is given below.
double
pow (double base, double exponent);
6. What will be the output of the following program?
int main()
{
printf (
trunc (16.99) );
printf (
trunc (20.1) );
return 0;
}
A.
17,20 B. 16.9,20.0 C. 16,20 D.
17,21
Ans: C
Explanation:
·
trunc( ) function in
C truncates the decimal value from floating point value and returns
integer value.
C – Type Casting functions
·
Typecasting concept in C language is used to modify a variable
from one date type to another data type. New data type should be mentioned
before the variable name or value in brackets which to be typecast. It is best practice to convert
lower data type to higher data type to avoid data loss.
·
Data will be truncated when
higher data type is converted to lower. For example, if float is converted to
int, data which is present after decimal point will be lost.
7. What will be the output of the following program?
#include <stdio.h>
int main ()
{
float
x;
x
= (float) 7/5;
printf("%f",x);
}
A.
1.400000 B. 1.0 C.
1 D. Error
Answer: A
8. Which one of the
following is used to modify a variable from one datatype to another?
A. Typedef B. Typecasting C. Error handling D.
All of the above
Answer: B
9. What will be the output of the following program?
#include <stdio.h>
main() {
int i = 17;
char c = 'c'; int sum;
sum = i + c;
printf("Value of sum :
%d\n", sum );
}
A.
Compile time error B. Runtime error C.17 c D.116
Answer: D
Explanation:
Here, the value of sum is 116 because the
compiler is doing integer promotion and converting the value of 'c' to ASCII
(99) before performing the actual addition operation.
10. What will be the output of the following program?
int main()
{
char
a[10] = "3.14";
float
pi = atof(a);
printf("Value
of pi = %f\n", pi);
return
0;
}
A.
Compiletime Error B.Garbage value C. 3.140000 D.
None of the above
Answer: C
Explaination: atof()
function in C language converts string data type to float data type.
11.
Which of the following header file supports all type casting functions in c?
A.
Stdio.h B. Stdlib.h C.conio.h D.Math.h
Ans: B
12. Which
of the following statement are correct?
(I) The maximum value a variable can hold depends upon its storage class.
(II) By default all variables enjoy a static storage class.
(I) The maximum value a variable can hold depends upon its storage class.
(II) By default all variables enjoy a static storage class.
A. only I is correct
B. Only II is correct
C. Both I and II are correct
D. Both I and II are Incorrect
Answer: D
13. Which of the following
statement are correct?
(I)
The value stored in the CPU register can always be accessed faster than that
stored in memory.
(II) A register storage class variable will always be stored in a CPU register.
(II) A register storage class variable will always be stored in a CPU register.
A. only I is correct
B. Only II is correct
C. Both I and II are correct
D. Both I and II are Incorrect
Answer:
A
14. #include<stdio.h>
void main()
{
static num = 4;
printf("%d ",--num);
if(num)
main();
}
A.
4 3 2 1 0
B.
3 2 1 0
C.
Infinitine times 4
D.
Compiler error
Answer:
b
15. #include<stdio.h>
int main(){
register int a=10;
int *p;
p=&a;
printf("%u",p);
return 0;
}
A.
Compiler error
B.
10
C.
Address of a
D.
None of these
Answer:
a
Explanation:
we cannot dereference any of the register variable since it doesn’t have any
memory address.
16. What will be the output of the following program?
#include <stdio.h>
static int i=10;
int main(){
i=5;
for(i=0;i<5;i++){
static int a=10;
printf("%d\t",a++);
}
return 0;
}
A.
0 1 2 3 4
B.
10 10 10 10 10
C.
10 11 12 13 14
D.
Static variable
declaration not allowed inside loop
Answer:
c
Explanation:
A
static variable initialization statement executes only once.
So, static int a=10 ; is executed only in the first iteration and is skipped in the following iterations.
Hence,
In the first iteration a=10 ,
In the 2nd iteration a=11, and so on till i becomes 4.
So, static int a=10 ; is executed only in the first iteration and is skipped in the following iterations.
Hence,
In the first iteration a=10 ,
In the 2nd iteration a=11, and so on till i becomes 4.
17.In case of a conflict between the names of a local and global
variable what happens?
a) The global variable is given a priority.
b) The local variable is given a priority.
c) Which one will get a priority depends upon which one is defined first.
d) The compiler reports an error.
a) The global variable is given a priority.
b) The local variable is given a priority.
c) Which one will get a priority depends upon which one is defined first.
d) The compiler reports an error.
Answer: b
18. Where will the
space be allocated for an automatic storage class variable?
a) In CPU register
b) In memory as well as in CPU register
c) In memory
d) On disk.
a) In CPU register
b) In memory as well as in CPU register
c) In memory
d) On disk.
Answer: c
19. What will be the
output of the following code?
#include< stdio.h>
int main()
{
extern int a;
static char j = ‘E’;
printf(“%c %d”, ++j, ++a);
return 0;
}
a) E 2
b) F 1
c) F Garbage
d) F 0
#include< stdio.h>
int main()
{
extern int a;
static char j = ‘E’;
printf(“%c %d”, ++j, ++a);
return 0;
}
a) E 2
b) F 1
c) F Garbage
d) F 0
Answer:b
20. For which of the
following situation should the register storage class be used?
a) For local variable in a function
b) For loop counter
c) For collecting values returned from a function
d) For variables used in a recursive function
a) For local variable in a function
b) For loop counter
c) For collecting values returned from a function
d) For variables used in a recursive function
Answer: b
21. which one of the
following is the default storage class?
A. extern B. Static C. Auto D.
Register
Answer: auto
Explanation: int month;
is same as auto int month; sine auto is the default storage class.
22. Comment on
the output of this C code?
1. #include <stdio.h>
2. void main()
3. {
4. float x = 0.1;
5. printf("%d, ", x);
6. printf("%f", x);
7. }
a) 0.100000, junk
value
b) Junk value, 0.100000
c) 0, 0.100000
d) 0, 0.999999
b) Junk value, 0.100000
c) 0, 0.100000
d) 0, 0.999999
Answer: b
23. What
is the output of this C code?
1. #include <stdio.h>
2. void main()
3. {
4. int x = 97;
5. char y = x;
6. printf("%c\n", y);
7. }
a) a
b) b
c) 97
d) Run time error
b) b
c) 97
d) Run time error
Answer: a
Explanation: ASCII
value of a is 97...so while assigning x to y type conversion takes place.’
24. When double is
converted to float, the value is?
a) Truncated
b) Rounded
c) Depends on the compiler
d) Depends on the standard
a) Truncated
b) Rounded
c) Depends on the compiler
d) Depends on the standard
Answer: c
25. function
tolower(c) defined in library works for
a) Ascii character set
b) Unicode character set
c) Ascii and utf-8 but not EBSIDIC character set
d) Any character set
a) Ascii character set
b) Unicode character set
c) Ascii and utf-8 but not EBSIDIC character set
d) Any character set
Answer: d
26. Which
type conversion is NOT accepted?
a) From char to int
b) From float to char pointer
c) From negative int to char
d) From double to char
a) From char to int
b) From float to char pointer
c) From negative int to char
d) From double to char
Answer: b
Explanation: Conversion of a float to pointer type is not allowed.
Explanation: Conversion of a float to pointer type is not allowed.
27. Which of
the following type-casting have chances for wrap around?
a) From int to float
b) From int to char
c) From char to short
d) From char to int
a) From int to float
b) From int to char
c) From char to short
d) From char to int
Answer: b
28. Which of
the following typecasting is accepted by C?
a) Widening conversions
b) Narrowing conversions
c) Both
d) None of the mentioned
a) Widening conversions
b) Narrowing conversions
c) Both
d) None of the mentioned
Answer: c
29. When do you need
to use type-conversions?
a) The value to be stored is beyond the max limit
b) The value to be stored is in a form not supported by that data type
c) To reduce the memory in use, relevant to the value
d) All of the mentioned
a) The value to be stored is beyond the max limit
b) The value to be stored is in a form not supported by that data type
c) To reduce the memory in use, relevant to the value
d) All of the mentioned
Answer: D
30. What is the output of this C code?
1. #include <stdio.h>
2. void main()
3. {
4. m();
5. m();
6. }
7. void m()
8. {
9. static int x = 5;
10. x++;
11. printf("%d", x);
12. }
a) 6 7
b) 6 6
c) 5 5
d) 5 6
b) 6 6
c) 5 5
d) 5 6
Answer: a
31. What is the output of this C code?
1. #include <stdio.h>
2. void main()
3. {
4. static int x;
5. if (x++ < 2)
6. main();
7. }
a) Infinite calls to
main
b) Run time error
c) Varies
d) main is called twice
b) Run time error
c) Varies
d) main is called twice
Answer: d
32. Which of following
is not accepted in C?
a) static a = 10; //static as
b) static int func (int); //parameter as static
c) static static int a; //a static variable prefixed with static
d) All of the mentioned
a) static a = 10; //static as
b) static int func (int); //parameter as static
c) static static int a; //a static variable prefixed with static
d) All of the mentioned
Answer: c
33. Which of the
following cannot be static in C?
a) Variables
b) Functions
c) Structures
d) None of the mentioned
a) Variables
b) Functions
c) Structures
d) None of the mentioned
Answer: d
34. What is the output of this C code?
1. #include <stdio.h>
2. int x = 5;
3. void main()
4. {
5. int x = 3;
6. printf("%d", x);
7. {
8. x = 4;
9. }
10. printf("%d", x);
11. }
a) Run time error
b) 3 3
c) 3 5
d) 3 4
b) 3 3
c) 3 5
d) 3 4
Answer: d
35. Global
variables are:
a) Internal
b) External
c) Both (a) and (b)
d) None of the mentioned
a) Internal
b) External
c) Both (a) and (b)
d) None of the mentioned
Answer: b
36. Which of the following are an external
variable?
1. #include <stdio.h>
2. int func (int a)
3. {
4. int b;
5. return b;
6. }
7. int main()
8. {
9. int c;
10. func (c);
11. }
12. int d;
a) a
b) b
c) c
d) d
b) b
c) c
d) d
Answer: d
37. What will be the output?
1. #include <stdio.h>
2. int main()
3. {
4. printf("%d", d++);
5. }
6. int d = 10;
a) 9
b) 10
c) 11
d) Compile time error
b) 10
c) 11
d) Compile time error
Answer: d
38. What will be the output?
1. #include <stdio.h>
2. double var = 8;
3. int main()
4. {
5. int var = 5;
6. printf("%d", var);
7. }
a) 5
b) 8
c) Compile time error due to wrong format identifier for double
d) Compile time error due to redeclaration of variable with same name
b) 8
c) Compile time error due to wrong format identifier for double
d) Compile time error due to redeclaration of variable with same name
Answer: a
39. Can variable i be accessed by
functions in another source file?
1. #include <stdio.h>
2. int i;
3. int main()
4. {
5. printf("%d\n", i);
6. }
a) 0
b) false
c) Only if extern keyword is used
d) Depends on the type of the variable
View Answer
b) false
c) Only if extern keyword is used
d) Depends on the type of the variable
View Answer
Answer: c
40. Property
of external variable to be accessed by any source file is called by C90
standard as
a) external linkage
b) external scope
c) global scope
d) global linkage
a) external linkage
b) external scope
c) global scope
d) global linkage
Answer: a
41. The
scope of an automatic variable is:
a) Within the block it appears
b) Within the blocks of the block it appears
c) Until the end of program
d) Both (a) and (b)
a) Within the block it appears
b) Within the blocks of the block it appears
c) Until the end of program
d) Both (a) and (b)
Answer: d
42. Automatic
variables are allocated space in the form of a:
a) stack
b) queue
c) priority queue
d) random
a) stack
b) queue
c) priority queue
d) random
Answer: a
43. What is the output of this C
code?
1. #include <stdio.h>
2. void foo(auto int i);
3. int main()
4. {
5. foo(10);
6. }
7. void foo(auto int i)
8. {
9. printf("%d\n", i );
10. }
a) 10
b) Compile time error
c) Depends on the standard
d) None of the mentioned
b) Compile time error
c) Depends on the standard
d) None of the mentioned
Answer: b
44. What
linkage does automatic variables have?
a) Internal linkage
b) External linkage
c) No linkage
d) None of the mentioned
a) Internal linkage
b) External linkage
c) No linkage
d) None of the mentioned
Answer: c
45. What is the output of this C code?
1. #include <stdio.h>
2. int main()
3. {
4. auto i = 10;
5. const auto int *p = &i;
6. printf("%d\n", i);
7. }
a) 10
b) Compile time error
c) Depends on the standard
d) Depends on the compiler
b) Compile time error
c) Depends on the standard
d) Depends on the compiler
Answer: a
46.
Automatic variables are initialised to
a) Zero
b) Junk value
c) Nothing
d) Both a & b
a) Zero
b) Junk value
c) Nothing
d) Both a & b
Answer: b
47.
Which of the following storage class supports char data type?
a) register
b) static
c) auto
d) All of the mentioned
a) register
b) static
c) auto
d) All of the mentioned
Answer: d
48. The
variable declaration with no storage class specified is by default:
a) auto
b) extern
c) static
d) register
a) auto
b) extern
c) static
d) register
Answer: a
49. What is
the scope of an external variable?
a) Whole source file in which it is defined
b) From the point of declaration to the end of the file in which it is defined
c) Any source file in a program
d) From the point of declaration to the end of the file being compiled
a) Whole source file in which it is defined
b) From the point of declaration to the end of the file in which it is defined
c) Any source file in a program
d) From the point of declaration to the end of the file being compiled
Answer: d
50. Which
variable has the longest scope?
1. #include <stdio.h>
2. int b;
3. int main()
4. {
5. int c;
6. return 0;
7. }
8. int a;
a) a
b) b
c) c
d) Both (a) and (b)
b) b
c) c
d) Both (a) and (b)
Answer: b
Hi
ReplyDelete