C Quiz – 102

Question 1
In the context of C data types, which of the followings is correct?
Tick
“unsigned long long int” is a valid data type.
Cross
“long long double” is a valid data type.
Cross
“unsigned long double” is a valid data type.
Cross
A), B) and C) all are valid data types.
Cross
A), B) and C) all are invalid data types.


Question 1-Explanation: 

In C, \"float\" is single precision floating type. \"double\" is double precision floating type. \"long double\"is often more precise than double precision floating type.  So the maximum floating type is \"long double\". There\'s nothing called \"long long double\". If someone wants to use bigger range than \"long double\", we need to define our own data type i.e. user defined data type. Besides, Type Specifiers \"signed\" and \"unsigned\" aren\'t applicable for floating types (float, double, long double). Basically, floating types are always signed only.

But integer types i.e. \"int\", \"long int\" and \"long long int\" are valid combinations. As per C standard, \"long long int\" would be at least 64 bits i.e. 8 bytes. By default integer types would be signed. If we need to make these integer types as unsigned, one can use Type Specifier \"unsigned\". That\'s why A) is correct answer.

Question 2
In the context of the following printf() in C, pick the best statement.
i) printf("%d",8);
ii) printf("%d",090);
iii) printf("%d",00200);
iv) printf("%d",0007000);
Cross
Only i) would compile. And it will print 8.
Cross
Both i) and ii) would compile. i) will print 8 while ii) will print 90
Cross
All i), ii), iii) and iv) would compile successfully and they will print 8, 90, 200 & 7000 respectively.
Tick
Only i), iii) and iv) would compile successfully. They will print 8, 128 and 3584 respectively.


Question 2-Explanation: 

As per C standard, \"An octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 through 7 only.\"

So 090 isn\'t valid because 0 prefix is used for octal but 9 isn\'t valid octal-digit.

Question 3
Suppose a C program has floating constant 1.414, what's the best way to convert this as "float" data type?
Cross
(float)1.414
Cross
float(1.414)
Tick
1.414f or 1.414F
Cross
1.414 itself of \"float\" data type i.e. nothing else required.


Question 3-Explanation: 
By default floating constant is of double data type. By suffixing it with f or F, it can be converted to float data type. For more details, this post can be referred here.
Question 4
In the context of modulo operation (i.e. remainder on division) for floating point (say 2.1 and 1.1), pick the best statement.
Cross
For floating point, modulo operation isn\'t defined and that\'s why modulo can\'t be found.
Cross
(2.1 % 1.1) is the result of modulo operation.
Tick
fmod(2.1,1.1) is the result of module operation.
Cross
((int)2.1) % ((int)1.1) is the result of modulo operation.


Question 4-Explanation: 
% works on integer types only not for floating types. Typecasting to integer type might approximate the intended result but it won\'t produce the correct result. Basically the fmod(x,y) function returns the value x − ny, for some integer n such that, if y is nonzero, the result has the same sign as x and magnitude less than the magnitude of y. fmod() is declared in \"math.h\" and its prototype is \"double fmod(double x, double y)\". For float and long double also, modulo has been implemented in math.h library through fmodf() and fmodl().
Question 5
In the context of the below program snippet, pick the best answer.
#include "stdio.h"
int arr[10][10][10];
int main()
{
 arr[5][5][5] = 123;
 return 0;
}
Which of the given printf statement(s) would be able to print arr[5][5][5]
(i) printf("%d",arr[5][5][5]);
(ii) printf("%d",*(*(*(arr+5)+5)+5));
(iii) printf("%d",(*(*(arr+5)+5))[5]);
(iv) printf("%d",*((*(arr+5))[5]+5));
Cross
only (i) would compile and print 123.
Cross
both (i) and (ii) would compile and both would print 123.
Cross
only (i), (ii) and (iii) would compile but only (i) and (ii) would print 123.
Cross
only (i), (ii) and (iii) would compile and all three would print 123.
Cross
all (i), (ii), (iii) and (iv) would compile but only (i) and (ii) would print 123.
Tick
all (i), (ii), (iii) and (iv) would compile and all would print 123.


Question 5-Explanation: 
For arrays, we can convert array subscript operator [] to pointer deference operator * with proper offset. It means that arr[x] is equal to *(arr+x). Basically, these two are interchangeable. The same concept can be applied in multi-dimensional arrays as well. That\'s why all of the above 4 printf are referring to the same element i.e. arr[5][5][5]
There are 5 questions to complete.


  • Last Updated : 26 Sep, 2023

Share your thoughts in the comments
Similar Reads