C Quiz - 107
Suppose a, b, c and d are int variables. For ternary operator in C ( ? : ), pick the best statement.
a>b ? : ; is valid statement i.e. 2nd and 3rd operands can be empty and they are implicitly replaced with non-zero value at run-time.
a>b ? c=10 : d=10; is valid statement. Based on the value of a and b, either c or d gets assigned the value of 10.
a>b ? (c=10,d=20) : (c=20,d=10); is valid statement. Based on the value of a and b, either c=10,d=20 gets executed or c=20,d=10 gets executed.
All of the above are valid statements for ternary operator.
Question 1 Explanation:
For ternary operator, both 2nd and 3rd operands are necessary. So A) isn’t correct. As per operator precedence, ternary operator has higher precedence over assignment operator. So B) isn’t correct.
Which of the followings is correct for a function definition along with storage-class specifier in C language?
int fun(auto int arg)
int fun(static int arg)
int fun(register int arg)
int fun(extern int arg)
All of the above are correct.
Question 2 Explanation:
As per C standard, “The only storage-class specifier that shall occur in a parameter declaration is register.” That’s why correct answer is C.
In a C program snippet, followings are used for definition of Integer variables?
signed s; unsigned u; long l; long long ll;Pick the best statement for these.
All of the above variable definitions are incorrect because basic data type int is missing.
All of the above variable definitions are correct because int is implicitly assumed in all of these.
Only “long l;” and “long long ll;” are valid definitions of variables.
Only “unsigned u;” is valid definition of variable.
Question 3 Explanation:
Please note that signed, unsigned and long all three are Type specifiers. And int is implicitly assumed in all of three. As per C standard, “int, signed, or signed int” are equivalent. Similarly, “unsigned, or unsigned int” are equivalent. Besides, “long, signed long, long int, or signed long int” are all equivalent. And “long long, signed long long, long long int, or signed long long int“ are equivalent.
Pick the correct statement for const and volatile.
const is the opposite of volatile and vice versa.
const and volatile can’t be used for struct and union.
const and volatile can’t be used for enum.
const and volatile can’t be used for typedef.
const and volatile are independent i.e. it’s possible that a variable is defined as both const and volatile.
Question 4 Explanation:
In C, const and volatile are type qualifiers and these two are independent. Basically, const means that the value isn’t modifiable by the program. And volatile means that the value is subject to sudden change (possibly from outside the program). In fact, C standard mentions an example of valid declaration which is both const and volatile. The example is “extern const volatile int real_time_clock;” where real_time_clock may be modifiable by hardware, but cannot be assigned to, incremented, or decremented. So we should already treat const and volatile separately. Besides, these type qualifier applies for struct, union, enum and typedef as well.
For the following declaration of a function in C, pick the best statement
int  fun(void (*fptr)(int *));
It will result in compile error.
No compile error. fun is a function which takes a function pointer fptr as argument and return an array of int.
No compile error. fun is a function which takes a function pointer fptr as argument and returns an array of int. Also, fptr is a function pointer which takes int pointer as argument and returns void.
No compile error. fun is a function which takes a function pointer fptr as argument and returns an array of int. The array of int depends on the body of fun i.e. what size array is returned. Also, fptr is a function pointer which takes int pointer as argument and returns void.
Question 5 Explanation:
As per C standard, a function can’t have an explicit array as return type. That’s why the above would result in compile error. There’re indirect ways if we need an array as an output of a function call. For example, a pointer can be returned by function by return statement while providing the size of array via other means. Alternatively, function argument can be used for this.
There are 5 questions to complete.