C Quiz – 108


1
Question 1
In the below statement, ptr1 and ptr2 are uninitialized pointers to int i.e. they are pointing to some random address that may or may not be valid address.
int* ptr1, ptr2;
A
TRUE
B
FALSE
Pointer Basics    C Quiz - 108    
Discuss it


Question 1 Explanation: 
Even though * is placed closer to int, * would be associated to ptr1 only but not to ptr2. It means that “int* ptr1” is equal to “int *ptr1”. That’s why only ptr1 is uninitialized pointer to int. Basically, though both ptr1 and ptr2 are uninitialized variables yet ptr1 is pointer to int while ptr2 is variable of type int. If we really want to make both variables as pointers, we need to mention them as “int *ptr1, *ptr2;”
Question 2
Anyone of the followings can be used to declare a node for a singly linked list. If we use the first declaration, “struct node * nodePtr;” would be used to declare pointer to a node. If we use the second declaration, “NODEPTR nodePtr;” can be used to declare pointer to a node.
/* First declaration */
struct node {
int data;
struct node * nextPtr;
};

/* Second declaration */
typedef struct node{
int data;
NODEPTR nextPtr;
} * NODEPTR;
A
TRUE
B
FALSE
Structure & Union    C Quiz - 108    
Discuss it


Question 2 Explanation: 
The typedef usage is incorrect. Basically, we can’t use yet to be typedef-ed data type inside while applying typedef itself. Here, NODEPTR is yet to be defined (i.e. typedef-ed) and we are using NODEPTR inside the struct itself.
Question 3
Anyone of the following can be used to declare a node for a singly linked list and “NODEPTR nodePtr;” can be used to declare pointer to a node using any of the following
/* First declaration */
typedef struct node
{
 int data;
 struct node *nextPtr;
}* NODEPTR;

/* Second declaration */
struct node
{
 int data;
 struct node * nextPtr;
};
typedef struct node * NODEPTR;
A
TRUE
B
FALSE
Structure & Union    C Quiz - 108    
Discuss it


Question 3 Explanation: 
Yes. Both are equivalent. Either of the above declarations can be used for “NODEPTR nodePtr;”. In fact, first one is the compact form of second one.
Question 4
Both of the following declarations for function pointers are equivalent. Second one (i.e. with typedef) looks cleaner.
/* First Declaration */
int (*funPtr1)(int), (*funPtr2)(int);

/* Second Declaration*/
typedef int (*funPtr)(int);
funPtr funPtr1, funPtr2;
A
TRUE
B
FALSE
Functions    C Quiz - 108    
Discuss it


Question 4 Explanation: 
Usually data type of function pointers tends to be cryptic and that’s why it’s used in conjunction with typedef. Think of a function pointer which is pointing to a function that accepts a function pointer and that returns a function pointer. This can be used simplified using typedef otherwise it's going to very difficult to read/understand!
Question 5
In a C file (say sourcefile1.c), an array is defined as follows. Here, we don’t need to mention arrary arr size explicitly in [] because the size would be determined by the number of elements used in the initialization.
int arr[] = {1,2,3,4,5};
In another C file (say sourcefile2.c), the same array is declared for usage as follows:
extern int arr[];
In sourcefile2.c, we can use sizeof() on arr to find out the actual size of arr.
A
TRUE
B
FALSE
Arrays    C Quiz - 108    
Discuss it


Question 5 Explanation: 
First thing first, sizeof() operator works at compile time. So usage of sizeof on arr in sourcefile2.c won’t work because arr in sourcefile2.c is an incomplete type. Please note that arr in sourcefile1.c is a complete type because size of array got determined at compile time due to initialization.
Question 6
Choose the correct option to fill the ?1 and ?2 so that the program prints an input string in reverse order. Assume that the input string is terminated by a new line character.
#include <stdio.h>
void wrt_it (void);
int main (void)
{
    printf("Enter Text"); 
    printf ("\n");
    wrt_ it();
    printf ("\n");
    return 0;
}
void wrt_it (void)
{
    int c;
    if (?1)
        wrt_it();
    ?2
}
A
?1 is  getchar() ! = '\n' ?2 is  getchar(c);
B
?1 is  (c = getchar()); ! = '\n' ?2 is  getchar(c);
C
?1 is  c! = '\n' ?2 is  putchar(c);
D
?1 is (c = getchar()) ! = '\n' ?2 is putchar(c);
C Quiz - 108    GATE-IT-2004    
Discuss it


Question 6 Explanation: 

getchar() : This function is used to read a character from the standard input stream.
https://en.wikibooks.org/wiki/C_Programming/C_Reference/stdio.h/getchar
putchar() : This function is used to write a character to the standard output stream.
https://en.wikibooks.org/wiki/C_Programming/C_Reference/stdio.h/putchar
 
Thus, option (D) is correct.
 
Please comment below if you find anything wrong in the above post.
There are 6 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice