C Quiz – 107

1
Question 1
Suppose a, b, c and d are int variables. For ternary operator in C ( ? : ), pick the best statement.
A
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.
B
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.
C
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.
D
All of the above are valid statements for ternary operator.
Operators    C Quiz - 107    
Discuss it


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.
Question 2
Which of the followings is correct for a function definition along with storage-class specifier in C language?
A
int fun(auto int arg)
B
int fun(static int arg)
C
int fun(register int arg)
D
int fun(extern int arg)
E
All of the above are correct.
Storage Classes and Type Qualifiers    C Quiz - 107    
Discuss it


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.
Question 3
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.
A
All of the above variable definitions are incorrect because basic data type int is missing.
B
All of the above variable definitions are correct because int is implicitly assumed in all of these.
C
Only “long l;” and “long long ll;” are valid definitions of variables.
D
Only “unsigned u;” is valid definition of variable.
Data Types    C Quiz - 107    
Discuss it


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.
Question 4
Pick the correct statement for const and volatile.
A
const is the opposite of volatile and vice versa.
B
const and volatile can’t be used for struct and union.
C
const and volatile can’t be used for enum.
D
const and volatile can’t be used for typedef.
E
const and volatile are independent i.e. it’s possible that a variable is defined as both const and volatile.
Storage Classes and Type Qualifiers    C Quiz - 107    
Discuss it


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.
Question 5
For the following declaration of a function in C, pick the best statement
int [] fun(void (*fptr)(int *));
A
It will result in compile error.
B
No compile error. fun is a function which takes a function pointer fptr as argument and return an array of int.
C
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.
D
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.
Arrays    C Quiz - 107    
Discuss it


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

Company Wise Coding Practice    Topic Wise Coding Practice