C Quiz – 102

1
Question 1
In the context of C data types, which of the followings is correct?
A
“unsigned long long int” is a valid data type.
B
“long long double” is a valid data type.
C
“unsigned long double” is a valid data type.
D
A), B) and C) all are valid data types.
E
A), B) and C) all are invalid data types.
Storage Classes and Type Qualifiers    C Quiz - 102    
Discuss it


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);
A
Only i) would compile. And it will print 8.
B
Both i) and ii) would compile. i) will print 8 while ii) will print 90
C
All i), ii), iii) and iv) would compile successfully and they will print 8, 90, 200 & 7000 respectively.
D
Only i), iii) and iv) would compile successfully. They will print 8, 128 and 3584 respectively.
Misc    C Quiz - 102    
Discuss it


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?
A
(float)1.414
B
float(1.414)
C
1.414f or 1.414F
D
1.414 itself of "float" data type i.e. nothing else required.
Data Types    C Quiz - 102    
Discuss it


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.
A
For floating point, modulo operation isn't defined and that's why modulo can't be found.
B
(2.1 % 1.1) is the result of modulo operation.
C
fmod(2.1,1.1) is the result of module operation.
D
((int)2.1) % ((int)1.1) is the result of modulo operation.
Operators    C Quiz - 102    
Discuss it


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));
A
only (i) would compile and print 123.
B
both (i) and (ii) would compile and both would print 123.
C
only (i), (ii) and (iii) would compile but only (i) and (ii) would print 123.
D
only (i), (ii) and (iii) would compile and all three would print 123.
E
all (i), (ii), (iii) and (iv) would compile but only (i) and (ii) would print 123.
F
all (i), (ii), (iii) and (iv) would compile and all would print 123.
Advanced Pointer    C Quiz - 102    
Discuss it


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

Company Wise Coding Practice    Topic Wise Coding Practice