C Quiz – 109


1
Question 1
In the following program snippet, both s1 and s2 would be variables of structure type defined as below and there won't be any compilation issue.
typedef struct Student
{
 int rollno;
 int total;
} Student;

Student s1;
struct Student s2;
A
TRUE
B
FALSE
Structure & Union    C Quiz - 109    
Discuss it


Question 1 Explanation: 
At first, it may seem that having same ‘struct tag name’ and ‘typedef name’ would cause issue here. But it’s perfectly fine for both of them having same name. s1 is defined using typedef name Student while s2 is defined using struct tag name Student.
Question 2
Pick the best statement for the following program.
#include "stdio.h"

int foo(int a)
{
 printf("%d",a);
 return 0;
}

int main()
{
 foo;
 return 0;
}
A
It’ll result in compile error because foo is used without parentheses.
B
No compile error and some garbage value would be passed to foo function. This would make foo to be executed with output “garbage integer”.
C
No compile error but foo function wouldn’t be executed. The program wouldn't print anything.
D
No compile error and ZERO (i.e. 0) would be passed to foo function. This would make foo to be executed with output 0.
Functions    C Quiz - 109    
Discuss it


Question 2 Explanation: 
In C, if a function name is used without parentheses, the reference to the function name simply generates a pointer to the function, which is then discarded. So the above program would compile but won’t print anything.
Question 3
Find out the correct statement for the following program.
#include "stdio.h"

typedef int (*funPtr)(int);

int inc(int a)
{
 printf("Inside inc() %d\n",a);
 return (a+1);
}

int main()
{

 funPtr incPtr1 = NULL, incPtr2 = NULL;

 incPtr1 = &inc; /* (1) */
 incPtr2 = inc; /* (2) */

 (*incPtr1)(5); /* (3) */
 incPtr2(5); /* (4)*/

 return 0;
}
A
Line with comment (1) will give compile error.
B
Line with comment (2) will give compile error.
C
Lines with (1) & (3) will give compile error.
D
Lines with (2) & (4) will give compile error.
E
No compile error and program will run without any issue.
Advanced Pointer    C Quiz - 109    
Discuss it


Question 3 Explanation: 
While assigning any function to function pointer, & is optional. Same way, while calling a function via function pointer, * is optional.
Question 4
Find out the correct statement for the following program.
#include "stdio.h"

int * gPtr;

int main()
{
 int * lPtr = NULL;

 if(gPtr == lPtr)
 {
   printf("Equal!");
 }
 else
 {
  printf("Not Equal");
 }

 return 0;
}
A
It’ll always print Equal.
B
It’ll always print Not Equal.
C
Since gPtr isn’t initialized in the program, it’ll print sometimes Equal and at other times Not Equal.
Variable Declaration and Scope    C Quiz - 109    
Discuss it


Question 4 Explanation: 
It should be noted that global variables such gPtr (which is a global pointer to int) are initialized to ZERO. That’s why gPtr (which is a global pointer and initialized implicitly) and lPtr (which a is local pointer and initialized explicitly) would have same value i.e. correct answer is a.
Question 5
Find out the correct statement for the following program.
#include "stdio.h"

int * arrPtr[5];

int main()
{
 if(*(arrPtr+2) == *(arrPtr+4))
 {
   printf("Equal!");
 }
 else
 {
  printf("Not Equal");
 }
 return 0;
}
A
Compile Error
B
It’ll always print Equal.
C
It’ll always print Not Equal.
D
Since elements of arrPtr aren’t initialized in the program, it’ll print either Equal or Not Equal.
Arrays    C Quiz - 109    
Discuss it


Question 5 Explanation: 
Here arrPtr is a global array of pointers to int. It should be noted that global variables such arrPtr are initialized to ZERO. That’s why all are elements of arrPtr are initialized implicitly to ZERO i.e. correct answer is b.
There are 5 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice