Table of Contents
C Programming GATE Questions and Answers
C programming GATE questions and Data Structure subject weightage in GATE exam is approx. 10 % every year in GATE CSE Exam.
C Programming GATE Questions and Answers consists approx. 6 marks Out of 100 in GATE Exam.
Today in this Tutorial we have explained Previous year C Programming GATE Questions and Answers.
Students who are going to attempt GATE 2024 and UGC Net CS Exam are suggested to practice these C Programming Questions for practice.
Let’s start
Q1. The output of the following C program is_____ [ GATE 2015]
void f1(int a, int b)
{
int c;
c=a; a=b; b=c;
}
void f2(int *a, int *b)
{
int c;
c=*a; *a=*b; *b=c;
}
int main(){
int a=4, b=5, c=6;
f1(a,b);
f2(&b, &c);
printf(“%d”,c-a-b);
}
Answer is -5
Explanation
The first function call f1(a,b) has no effect because here the parameter are passed as call by value. So function signature don’t match.
Second function call f2(&b,&c) signature matched. SO it will work correctly . SO value of variable b and c will be swapped and new value of b will be 6 and c will be 5 so c-a-b will be = 5-6-4= -5
Q2.What does the following fragment of C-program print? [ GATE 2011]
char c[ ] = “GATE2011”;
char *p = c;
printf(“%s”, p + p[3] – p[1]);
(a) GATE 2011
(b) E2011
(c) 2011
(d) 01
Answer : C
Solution:
char c[] = “GATE2011”;
since char *p =c it means p represents to the base address of string “GATE2011”
SO p[3] is ‘E’ and p[1] is ‘A’.
Value of Sub expression p[3] – p[1] = ASCII value of ‘E’ – ASCII value of ‘A’ = 4
// So the expression p + p[3] – p[1] becomes ( p + 4) which is
And (p+4) represent to base address of string “2011”
printf(“%s”, p + p[3] – p[1]) ;
So it will print 2011
Also Read – Disk Scheduling Algorithm in OS
Q3.What does the following program print? [GATE 2010 ]
#include < stdio.h >
void f (int *p, int *q) {
p = q;
*p = 2;
}
int i = 0, j = 1;
int main ( ){
f(&i, &j);
printf (“%d %d \ n”, i, j);
return 0;
}
(a) 2 2
(b) 2 1
(c) 0 1
(d) 0 2
Answer : D
0 2
Explanation
Since i and j are global variables.
Integer Variable | I | J |
Value | 0 | 1 |
Assumed Address | 100 | 200 |
F(&I,&j)
Integer Pointer | p | Q |
Value | 100 | 200 |
Assumed Address | 300 | 400 |
P=q; \p =200
*p=2\ change the value at memory location 200 to 2
*p=2 is equal to j=2;
Integer Variable | I | J |
Value | 0 | 2 |
Assumed Address | 100 | 200 |
printf (“%d %d \ n”, i, j);
it will print
0 2
Q9. What does the following C statement declare? [ GATE 2005]
int (*f)(int *);
(a) A function that take an integer pointer as an argument and returns an integer.
(b) A function that take integer as an argument and return an integer pointer.
(c) A pointer to a function that takes an integer pointer as an argument and return an integer.
(d) A function that take an integer pointer as an argument and return a function pointer.
Answer : C
Explanation
Option C is correct answer because int(*f)(int *); is a statement which represents pointer to function that and that function take an integer argument and return an integer.
Q10. Consider the following C function [GATE 2004]
void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
Which of the following option is correct if we want to exchange the values of x and y variables.
(a) We need to call Swap(x,y)
(b) we need to call call swap(&x,&y)
(c) We can not call swap (x,y) because it does not return any value
d) We can not call swap (x,y) because parameters are passed by value
Answer : (d)
Explanation:
Option a, b and c are incorrect because of following reason.
(a) We need to call Swap(x,y)
(b) we need to call call swap(&x,&y)
(c) We can not call swap (x,y) because it does not return any value
In order to exchange the value of x and y we should call swap(&x, & y) but for it’s working at the time of function definition we should pass parameter as pointer it means we should define void swap( int *x, int*y)
So function call Swap(x,y) can not not be used because parameter are passed as value instead of pointer types arguments.
Also Read – Hard Disk Structure and Disk Concepts
Q11. Consider the following C program: GATE [2019]
#include < stdio.h >
int main()
{
int a[] = {2, 4, 6, 8, 10} ;
int i, sum = 0, *b = a + 4 ;
for (i = 0; i < 5; i++)
sum = sum + (*b – i) – *(b – i) ;
printf (“%d\n”, sum) ;
return 0 ;
}
The output of the above C program is _____.
Answer – 10
Explanation:
In the above program for loop will be run from i=0 to i=4.
When i=0 then
Sum= 0+10-0 =0
When i= 1 then
Sum = 0+9-8
Sum=1
When i=2 then
Sum=1+8-6=3
When i=3 then
Sum=3+7-4=6
When i =4 then
Sum= 6+ 6-2=10
So out put will be 10
Q12. Consider the following C program [ GATE 2018]
#include< stdio.h >
void fun1(char *s1, char *s2){
char *tmp;
tmp = s1;
s1 = s2;
s2 = tmp;
}
void fun2(char **s1, char **s2){
char *tmp;
tmp = *s1;
*s1 = *s2;
*s2 = tmp;
}
int main(){
char *str1 = “Hi”, *str2 = “Bye”;
fun1(str1, str2); printf(“%s %s “, str1, str2);
fun2(&str1, &str2); printf(“%s %s”, str1, str2);
return 0;
}
The output of the program above is
Answer : Option A is correct
Explanation:
fun1(char *s1, char *s2)
This function has a local scope therefore the value changed here won’t affect actual parameter and values will be ‘Hi Bye’.
Let’s understand second function
fun2(char **s1, char **s2)
Since here in this function value is pointer to pointer, so it will change pointer of the actual value. In this case values will be ‘Bye Hi’
So the answer is ‘Hi Bye Bye Hi’
Note- To Learn C Programming Students can click the Link Given Below
Conclusion
I hope the C Programming GATE Questions and Answers Discussed in this tutorial will be helpful to you in understanding the types of C Programming Questions to be asked in GATE Exam.
Good Luck !