C Questions and Answers – Mathematical functions – 3

«
»

This set of C Interview Questions and Answers focuses on “Mathematical functions – 3”.

1. Which of the following statement is correct?

advertisement
    double x, y, z;
    x = 5.123456;
    z= modf(x, *y);

a) y stores integer part of x, z returns fractional part of x
b) y stores integer part of x, z returns integer part of x
c) y stores fractional part of x, z returns integer part of x
d) y stores fractional part of x, z returns fractional part of x
View Answer

Answer: a
Explanation: double modf(double x, double *y)
This function returns the fractional part of x, with the same sign. modf() function breaks the argument value into integer and fraction parts, each of which has the same sign as the argument. It stores the integer part as a double in the object pointed to by y.

2. A domain error occurs if x is negative and y is not an integral value for the function pow(double x, double y).
a) true
b) false
View Answer

Answer: a
Explanation: The pow() function computes x raised to the power y. A domain error occurs if x is negative and y is not an integral value. A domain error occurs if the result cannot be represented when x is zero and y is less than or equal to zero. A range error may occur.

3. A function is declared as sqrt(-x) under the header file math.h, what will the function return?
a) square root of x
b) complex number
c) domain error
d) range error
View Answer

Answer: c
Explanation: double sqrt (double x);
The sqrt function computes the nonnegative square root of x. A domain error occurs if the argument is negative.

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

advertisement
    double x=1.2
    printf("%.1lf", ceil(x));

a) 1
b) 2
c) 1.0
d) 2.0
View Answer

Answer: d
Explanation: The ceil function returns the smallest integral value not less than x, expressed as a double.

5. What will be the output of the following C code?

    double x=3,y= - 6;
    printf("%lf %lf", fabs(x), fabs(y));

a) 3 6
b) -3 6
c) 3.0 6.0
d) 3.000000 6.000000
View Answer

Answer: d
Explanation: double fabs(double x);
The fabs function computes the absolute value of a floating-point number X.

6. What will be the output of the following C code?

advertisement
double x=1.8;
printf("%.2lf",floor(x));

a) 2.0
b) 2.00
c) 1.0
d) 1.00
View Answer

Answer: d
Explanation: double floor(double x);
The floor function computes the largest integral value not greater than x.

7. double ______ (double x, double y) computes the floating-point remainder of x/y.
a) modf
b) fmod
c) ceil
d) floor
View Answer

Answer: b
Explanation: double fmod(doublr x, double y);
The fmod() function computes the floating-point remainder of x/y.

8. sqrt(x) function is not faster than the apparent equivalent pow(x,0.5).
a) true
b) false
View Answer

Answer: b
Explanation: sqrt(x) function is generally much faster than the apparent equivalent pow (x, 0.5).

9. Which of the given C function can be used instead of the apparent identity pointed to by y?

int x=1; 
double y= 0.5 * (exp (x) + exp (-x));

a) cos(x)
b) cosh(x)
c) fmod(x)
d) modf(x)
View Answer

Answer: b
Explanation: The cosh function returns the hyperbolic cosine value. cosh(x)= 0.5 * (exp (x) + exp (-x))

10. Which function is used to recombine the fraction and exponent parts of a floating-point value after you have worked on them separately?
a) frexp()
b) exp()
c) modf()
d) Idexp()
View Answer

Answer: d
Explanation: ldexp() – Use this function to recombine the fraction and exponent parts of a floating-point value after you have worked on them separately.

Sanfoundry Global Education & Learning Series – C Programming Language.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn