C Quiz – 106

1
Question 1
Assuming int size is 4 bytes, what is going to happen when we compile and run the following program?
#include “stdio.h”
int main()
{
  printf(“GeeksQuiz\n”);
  main();
  return 0;
}
A
We can’t use main() inside main() and compiler will catch it by showing compiler error.
B
GeeksQuiz would be printed in 2147483647 times i.e. (2 to the power 31) - 1.
C
It’ll print GeeksQuiz infinite times i.e. the program will continue to run forever until it’s terminated by other means such as CTRL+C or CTRL+Z etc.
D
GeeksQuiz would be printed only once. Because when main() is used inside main(), it’s ignored by compiler at run time. This is to make sure that main() is called only once.
E
GeeksQuiz would be printed until stack overflow happens for this program.
Functions    C Quiz - 106    
Discuss it


Question 1 Explanation: 
First of all, there’s no restriction of main() calling main() i.e. recursion can happen for main() as well. But there’s no explicit termination condition mentioned here for this recursion. So main() would be calling main() after printing GeeksQuiz. This will go on until Stack of the program would be filled completely. Please note that stack (internal to a running program) stores the calling function sequence i.e. which function has called which function so that the control can be returned when called function returns. That’s why here in program, main() would continue to call main() until complete stack is over i.e. stack-overflow occurs.
Question 2
What’s the meaning of following declaration in C language?
int (*p)[5];
A
It will result in compile error because there shouldn't be any parenthesis i.e. “int *p[5]” is valid.
B
p is a pointer to 5 integers.
C
p is a pointer to integer array.
D
p is an array of 5 pointers to integers.
E
p is a pointer to an array of 5 integers
Arrays    C Quiz - 106    
Discuss it


Question 2 Explanation: 
Here p is basically a pointer to integer array of 5 integers. In case of “int *p[5]”, p is array of 5 pointers to integers.
Question 3
In a C program, following variables are defined:
 float      x = 2.17;
 double   y = 2.17;
 long double z = 2.17;
Which of the following is correct way for printing these variables via printf.
A
printf("%f %lf %Lf",x,y,z);
B
printf(“%f %f %f”,x,y,z);
C
printf("%f %ff %fff",x,y,z);
D
printf("%f %lf %llf",x,y,z);
Data Types    C Quiz - 106    
Discuss it


Question 3 Explanation: 
In C language, float, double and long double are called real data types. For “float”, “double” and “long double”, the right format specifiers are %f, %lf and %Lf from the above options. It should be noted that C standard has specified other format specifiers as well for real types which are %g, %e etc.
Question 4
“typedef” in C basically works as an alias. Which of the following is correct for “typedef”?
A
typedef can be used to alias compound data types such as struct and union.
B
typedef can be used to alias both compound data types and pointer to these compound types.
C
typedef can be used to alias a function pointer.
D
typedef can be used to alias an array.
E
All of the above.
Data Types    C Quiz - 106    
Discuss it


Question 4 Explanation: 
In C, typedef can be used to alias or make synonyms of other types. Since function pointer is also a type, typedef can be used here. Since, array is also a type of symmetric data types, typedef can be used to alias array as well.
Question 5
For the following “typedef” in C, pick the best statement
typedef int INT, *INTPTR, ONEDARR[10], TWODARR[10][10];
A
It will cause compile error because typedef is being used to define multiple aliases of incompatible types in the same statement.
B
“INT x” would define x of type int. Remaining part of the statement would be ignored.
C
“INT x” would define x of type int and “INTPTR y” would define pointer y of type int *. Remaining part of the statement would be ignored.
D
“INT x” would define x of type int. “INTPTR y” would define pointer y of type int *. ONEDARR is an array of 10 int. TWODARR is a 2D array of 10 by 10 int.
E
“INT x” would define x of type int. “INTPTR *y” would define pointer y of type int **. “ONEDARR z” would define z as array of 10 int. “TWODARR t” would define t as array of 10 by 10 int.
Storage Classes and Type Qualifiers    C Quiz - 106    
Discuss it


Question 5 Explanation: 
Here, INT is alias of int. INTPTR is alias of int *. That’s why INTPTR * would be alias of int **. Similarly, ONEDARR is defining the alias not array itself. ONEDARR would be alias to int [10]. That’s why “ONEDARR z” would define array z of int [10]. Similarly, TWODARR would be alias to int [10][10]. Hence “TWODARR t” would define array t of int [10][10]. We can see that typedef can be used to create alias or synonym of other types.
There are 5 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice