C Language | Set 1 - GeeksforGeeks

07 Oct, 2021
Following questions have been asked in GATE CS exam.
1. Consider the following three C functions :
[PI] int * g (void) {   int x = 10;   return (&x); }   [P2] int * g (void) {   int * px;   *px = 10;   return px; }   [P3] int *g (void) {   int *px;   px = (int *) malloc (sizeof(int));   *px = 10;  return px; } 
Which of the above three functions are likely to cause problems with pointers? (GATE 2001)(a) Only P3(b) Only P1 and P3(c) Only P1 and P2(d) P1, P2 and P3
Answer: (c)Eplaination: In P1, pointer variable x is a local variable to g(), and g() returns pointer to this variable. x may vanish after g() has returned as x exists on stack. So, &x may become invalid.In P2, pointer variable px is being assigned a value without allocating memory to it.P3 works perfectly fine. Memory is allocated to pointer variable px using malloc(). So, px exists on heap, it’s existence will remain in memory even after return of g() as it is on heap.
2. The value of j at the end of the execution of the following C program. (GATE CS 2000)
int incr (int i){   static int count = 0;   count = count + i;   return (count);}main (){   int i,j;   for (i = 0; i <=4; i++)      j = incr(i);}
(a) 10(b) 4(c) 6(d) 7
Answer (a)Eplaination: count is static variable in incr(). Statement static int count = 0 will assign count to 0 only in first call. Other calls to this function will take the old values of count.Count will become 0 after the call incr(0)Count will become 1 after the call incr(1)Count will become 3 after the call incr(2)Count will become 6 after the call incr(3)Count will become 10 after the call incr(4)
3. Consider the following C declaration
struct {     short s [5];    union {          float y;          long z;     }u; } t; 
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignmentconsiderations, is (GATE CS 2000)(a) 22 bytes(b) 14 bytes(c) 18 bytes(d) 10 bytes
Answer: (c)Explanation: Short array s[5] will take 10 bytes as size of short is 2 bytes. Since u is a union, memory allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8).
4. The number of tokens in the following C statement.
printf("i = %d, &i = %x", i, &i);
is (GATE 2000)(a) 3(b) 26(c) 10(d) 21
Answer (c)Explanation:In a C source program, the basic element recognized by the compiler is the “token.” A token is source-program text that the compiler does not break down into component elements.There are 6 types of C tokens : identifiers, keywords, constants, operators, string literals and other separators. There are total 10 tokens in the above printf statement.
5. The following C declarations
struct node {    int i;    float j; }; struct node *s[10] ; 
define s to be (GATE CS 2000)
(a) An array, each element of which is a pointer to a structure of type node(b) A structure of 2 fields, each field being a pointer to an array of 10 elements(c) A structure of 3 fields: an integer, a float, and an array of 10 elements(d) An array, each element of which is a structure of type node.
Answer: (a)
pradipbankar0097
GATE-CS-2000
GATE-CS-2001
GATE-CS-C-Language
GATE CS
MCQ
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Differences between TCP and UDP
Semaphores in Process Synchronization
Caesar Cipher in Cryptography
Difference between Process and Thread
Inter Process Communication (IPC)
Practice questions on Height balanced/AVL Tree
Operating Systems | Set 1
Computer Networks | Set 1
Computer Networks | Set 2
Database Management Systems | Set 1