0% found this document useful (0 votes)
29 views49 pages

C Programming - GATE PYQs

The document contains a series of programming questions related to the C language, including array manipulation, pointer usage, and function behavior. Each question presents a code snippet and asks for the expected output or behavior, testing knowledge of C syntax and semantics. The questions are designed for GATE exam preparation, focusing on common pitfalls and concepts in C programming.

Uploaded by

Anbu Alwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views49 pages

C Programming - GATE PYQs

The document contains a series of programming questions related to the C language, including array manipulation, pointer usage, and function behavior. Each question presents a code snippet and asks for the expected output or behavior, testing knowledge of C syntax and semantics. The questions are designed for GATE exam preparation, focusing on common pitfalls and concepts in C programming.

Uploaded by

Anbu Alwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

C Programming

GATE PYQs

1
Consider the following declaration of a ‘two-dimensional array in C:
char a[100][100];
Assuming that the main memory is byte-addressable and that the array is
char a[100][100];
stored starting from memory address 0, the address of a[40][50] is ____

(A) 4040
(B) 4050
(C) 5040
(D) 5050
Assume the following C variable declaration A)

int *A [10], B[10][10];


of the following expressions
I. A[2]
II. A[2][3]
III. B[1]
IV. B[2][3]
which will not give compile-time errors if used as left hand
sides of assignment statements in a C program?
A. I, II, and IV only
B. II, III, and IV only
C. II and IV only
D. IV only
Q) Consider the following C program:

#include <stdio.h>
int main() {
int arr[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 5}, *ip=arr+4;
printf(“%d\n”, ip[1]);
return 0;
}
The number that will be displayed on execution of the program is ______
What is the output printed by the following C code?

#include <stdio.h>
int main ()
{
char a [6] = "world";
int i, j;
for (i = 0, j = 5; i < j; a [i++] = a [j--]);
printf ("%s\n", a);
}

A) A. Dlrow
B) B. Null string
C) C. Dlrld
D) D. worow
Q) What does the following fragment of C program print?

char c[] = "GATE2011";


char *p = c;
printf("%s", p + p[3] - p[1]);

A. GATE2011
B. E2011
C. 2011
D. 011
What is the output of the following C code? Assume that the address of is (in decimal) and
an integer requires four bytes of memory.

int main () {

unsigned int x [4] [3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};

printf ("%u, %u, %u", x + 3, *(x + 3), *(x + 2) + 3);

A) 2036, 2036, 2036


B) 2012, 4, 2204
C) 2036, 10, 10
D) 2012, 4, 6
Consider the following C program.
#include<stdio.h>
#include<string.h>
int main() {
char* c="GATECSIT2017";
char* p=c;
printf("%d", (int)strlen(c+2[p]-6[p]-1));
return 0;
}

The output of the program is _____.


Consider the following C code. Assume that unsigned long
int type length is 64 bits.

unsigned long int fun(unsigned long int n) {


unsigned long int i, j = 0, sum = 0;
for (i = n; i > 1; i = i / 2) j++;
for (; j > 1; j = j / 2) sum++;
return sum;
}

The value returned when we call fun with the input 2^40 is:

A) 4 B) 5 C) 6 D) 40
Consider the following C program :
#include<stdio.h>
int jumble(int x, int y){
x = 2*x+y;
return x;
}
int main(){
int x=2, y=5;
y=jumble(y,x);
x=jumble(y,x);
printf("%d \n",x);
return 0;

The value printed by the program is ______________.


void printxy(int x, int y) {
int *ptr;
x=0;
ptr=&x;
y=*ptr;
*ptr=1;
printf(“%d, %d”, x, y);
}
The output of invoking printxy(1,1) is:

A) A. 0, 0
B) B. 0, 1
C) C. 1, 0
D) D. 1, 1
Let x be an integer which can take a value of 0 or 1 . The statement

if (x == 0) x = 1; else x = 0;

is equivalent to which one of the following ?

A) x=1+x;
B) x=1-x;
C) x=x-1;
D) x=1%x;
GATE 2007
Consider the following C program segment

# include <stdio.h>
int main()
{
char s1[7] = "1234", *p;
p = s1 + 2;
*p = '0';
printf("%s", s1);
}

What will be printed by the program?

A) A. 12
B) B. 120400
C) C. 1204
D) D. 1034
Consider the following C program.

#include <stdio.h>
int main () {
int a[4] [5] = {{1, 2, 3, 4, 5},{6, 7,8, 9, 10},{11, 12, 13, 14, 15},{16,
17,18, 19, 20}};
printf(“%d\n”, *(*(a+**a+2)+3));
return(0);
}

The output of the program is _______.


Consider the following program.
#include <stdio.h>
int main()
{
int arr[4][5];
int i, j;
for (i=0; i<4; i++)
{
for (j=0; j<5; j++)
{
arr[i][j] = 10 * i + j;
}
}
printf(“%d”, *(arr[1]+9));
What is the output of the above program? return 0;
}
A) 14
B) B) 20
C) C) 24
D) 30
Consider the C program given below. What does it print?
#include <stdio.h>
int main ()
{
int i, j;
int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
for(i = 0; i < 3; i++) {
a[i] = a[i] + 1;
i++;
}
i--;
for (j = 7; j > 4; j--) {
int i = j/2;
a[i] = a[i] - 1;
}
printf ("%d, %d", i, a[i]);
}
A) A. 2, 3
B) B. 2, 4
C) C. 3, 2
D) D. 3, 3
C program is given below:
#include <stdio.h>
int main ()
{
int i, j;
char a [2] [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}};
char b [3] [2];
char *p = *b;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
*(p + 2*j + i) = a [i] [j];
}
}
}
What should be the contents of the array b at the end of the
program?
Consider the following program. Assume parameters to a function are evaluated from right to
left.
#include <stdio.h>
int g( int p) { printf("%d", p); return p; }
int h(i nt q) { printf("%d", q); return q; }
void f (int x, int y) {
g(x);
h(y);
}
int main() {
f (g(10), h(20));
}
Which one of the following options is the CORRECT output of the above
program?

A) A. 20101020
B) B. 10202010
C) C. 20102010
D) D. 10201020
Consider the following C code

#include <stdio.h>
int r() {
static int num = 7;
return num--;
}
int main() {
for (r(); r(); r())
printf("%d", r());
return 0;
}
Which one of the following values will be
displayed on execution of the programs?

A) 41 B) 52 C) 63 D) 630
GATE CSE 2019
Consider the following C code
#include <stdio.h>

int main() {
float sum = 0.0, j = 1.0, i = 2.0;

// Loop continues while i/j > 0.0625


while (i / j > 0.0625) {
j = j + j; // Double the value of j
sum = sum + i / j; // Update sum
printf("%f\n", sum); // Print sum
}

return 0;
}

The number of times the variable sum will be printed, when the above
program is executed, is _________
Consider the <stdio.h>
#include following C code

int main() {
int a[] = {2, 4, 6, 8, 10};
int i, sum = 0, *b = a + 4;

// Loop to calculate sum


for (i = 0; i < 5; i++) {
sum = sum + (*b - i) - *(b - i);
}

// Print the final value of sum


printf("%d\n", sum);

return 0;
}

What is the output ?


#include <stdio.h>
int main() {
Consider the following ANSI C code
int i, j, count;
count = 0;
i = 0;

// Loop from j = -3 to j = 3
for (j = -3; j <= 3; j++) {
// Check if j is greater than or equal to 0 and increment i
if ((j >= 0) && (i++)) {
count = count + j; // Add j to count
}
}

// Add the value of i to count


count = count + i;

// Print the final value of count


printf("%d", count);

return 0;
}
Which one of the following options is correct?
A. The program will not compile successfully
B. The program will compile successfully and output when executed
C. The program will compile successfully and output when executed
D. The program will compile successfully and output when executed
Consider the following ANSI C code
int f (int x, int y){
for (int i=0 ; i<y ; i++ ) {
x= x + x + y;
}
return x;
}

Which of the following statements is/are TRUE about the above function?

A. If the inputs are x=20, y=10, then the return value is greater than 2^20
B. If the inputs are x=20, y=20, then the return value is greater than 2^20
C. If the inputs are x=20, y=10, then the return value is less than 2^10
D. If the inputs are x=10, y=20, then the return value is greater than 2^20
Consider the following C code
#include <stdio.h>

int funcf(int x);


int funcg(int y);

int main() {
int x = 5, y = 10, count;

// Loop that runs twice


for (count = 1; count <= 2; ++count) {
y += funcf(x) + funcg(x); // Update y based on the return values of funcf and funcg
printf("%d", y); // Print updated y
}

return 0;
}

int funcf(int x) {
int y;
y = funcg(x); // Call funcg from within funcf
return y; // Return the result of funcg
}

int funcg(int x) {
static int y = 10; // static variable to preserve its value across function calls
y += 1; // Increment static variable y
return (y + x); // Return the sum of y and x
}

What is the output ?


Consider the following program:
#include <stdio.h>
int counter = 0;
int calc(int a, int b) {
int c;

counter++;

if (b == 3) return (a * a * a);

else {

c = calc(a, b / 3);

return (c * c * c);

int main() {

calc(4, 81);

printf("%d", counter);

return 0;
What is the Output ?
}
Which of the following statements is FALSE?

A. In statically typed languages, each variable in a program has a fixed type

B. In un-typed languages, values do not have any types

C. In dynamically typed languages, variables have no types

D. In all statically typed languages, each variable in a program is associated with values of
only a single type during the execution of the program
Predict the output of the following program:
void modify(char x[], char y[]){
x[2]=‘y’; x[3]='\0’;
}
void main(){
char x[]="Hello", y[]="Hi";
modify(x,y);
printf("%s,%s",x,y);
}
a) Hello,Hi
b) Hey,Hi
c) Hey\0o,Hello
d) Hey\0o,Hi
e) None of the above

You might also like