C Programming

C Programming Questions Answers GATE 2022

C Programming GATE Question Answers with Explanation.

Q1 – What is printed by the following ANSI C program? (GATE 2022)

 

#include<stdio.h>
int main(int argc, char *argv[])
{
int x = 1, z[2] = {10, 11};
int *p = NULL;
p = &x;
*p = 10;
p = &z[1];
*(&z[0] + 1) += 3;
printf(“%d, %d, %d\n”, x, z[0], z[1]);
return 0;
}

 

  1. 1, 10, 11
  2. 1, 10, 14
  3. 10, 14, 11
  4. 10, 10, 14

Ans – (4)

Explanation –

Let’s break down the code step by step:

1. `int x = 1, z[2] = {10, 11};` initializes an integer variable `x` with the value 1 and an array `z` with values {10, 11}.

2. `int *p = NULL;` declares a pointer `p` and sets it to `NULL`.

3. `p = &x;` assigns the address of `x` to the pointer `p`.

4. `*p = 10;` modifies the value at the memory location pointed to by `p` (which is `x`) to 10. So, `x` becomes 10.

5. `p = &z[1];` now `p` points to the second element of array `z`.

6. `*(&z[0] + 1) += 3;` accesses the first element of array `z` through pointer arithmetic (`&z[0] + 1` moves to the second element), adds 3 to it. So, `z[1]` becomes 14.

7. `printf(“%d, %d, %d\n”, x, z[0], z[1]);` prints the values of `x`, `z[0]`, and `z[1]`.

Let’s analyze the final values:

– `x` has been modified indirectly through the pointer `p` earlier, so it is 10.
– `z[0]` remains unchanged, so it is 10.
– `z[1]` has been modified to 14.

Therefore, the correct output will be:
(4) 10, 10, 14

Q2 – What is printed by the following ANSI C program? (GATE 2022)
#include<stdio.h>
int main(int argc, char *argv[])
{
   int a[3][3][3] = {{ 1,  2,  3,  4,  5,  6,  7,  8,  9},
                         {10, 11, 12, 13, 14, 15, 16, 17, 18},
                         {19, 20, 21, 22, 23, 24, 25, 26, 27}};
   int i = 0, j = 0, k = 0;
   for( i = 0; i < 3; i++ ){
          for(k = 0; k < 3; k++ )
                 printf(“%d “, a[i][j][k]);
          printf(“\n”);
   }
return 0;
}
1.   1 2 3
     10 11 12
     19 20 21
2.   1 4 7
    10 13 16
    19 22 25
3.  1 2 3
     4 5 6
     7 8 9
 
4.  1 2 3
    13 14 15
    25 26 27

Ans – (1)

Explanation –

The program prints the elements of the 3D array a in a specific format. Let’s evaluate the output:

i is used to iterate over the first dimension (2D array).
j is fixed at 0.
k is used to iterate over the third dimension (individual elements in each row).
The correct output is (1)

1 2 3
10 11 12
19 20 21

Q3 – What is printed by the following ANSI C program? (GATE 2022)

 

#include<stdio.h>
int main(int argc, char *argv[]){
      char a = ‘P’;
      char b = ‘x’;
      char c = (a & b) + ‘*’;
      char d = (a | b) – ‘-‘;
      char e = (a ^ b) + ‘+’;
      printf(“%c %c %c\n”, c, d, e);
return 0;
}
ASCII encoding for relevant characters is given below

C prog Q44 GATE 2022

  1.  z K S
  2.  122 75 83
  3.  * – +
  4.  P x +

Ans – (1)

Explanation –

Let’s analyze the given C program

1. `c` is calculated as the bitwise AND of ‘P’ and ‘x’ (`(a & b)`) and then adds the ASCII value of ‘*’.
2. `d` is calculated as the bitwise OR of ‘P’ and ‘x’ (`(a | b)`) and then subtracts the ASCII value of ‘-‘.
3. `e` is calculated as the bitwise XOR of ‘P’ and ‘x’ (`(a ^ b)`) and then adds the ASCII value of ‘+’.

Now, let’s find the ASCII values:

– ‘P’ has an ASCII value of 80.
– ‘x’ has an ASCII value of 120.

Let’s calculate the values:

– c = (80 & 120) + ‘*’ => c = 80 + 42 => `c` is the character with ASCII value 122.
– d = (80 | 120) – ‘-‘   => d = 120 – 45 => `d` is the character with ASCII value 75.
– e = (80 ^ 120) + ‘+’ => e = 40 + 43 => `e` is the character with ASCII value 83.

Now, when you print these characters

printf(“%c %c %c\n”, c, d, e);

The output will be
z K S