0% found this document useful (0 votes)
35 views38 pages

A D V A N C e C Programming Language

The document is a practice questions booklet for the GATE Computer Science & IT exam, focusing on advanced C programming language concepts. It covers various topics including data types, operators, control flow, functions, pointers, structures, and dynamic memory allocation, followed by a series of practice questions with multiple-choice answers. The questions test the understanding of C programming syntax, semantics, and problem-solving skills.

Uploaded by

ajlearner2024
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)
35 views38 pages

A D V A N C e C Programming Language

The document is a practice questions booklet for the GATE Computer Science & IT exam, focusing on advanced C programming language concepts. It covers various topics including data types, operators, control flow, functions, pointers, structures, and dynamic memory allocation, followed by a series of practice questions with multiple-choice answers. The questions test the understanding of C programming syntax, semantics, and problem-solving skills.

Uploaded by

ajlearner2024
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/ 38

GATE Computer Science & IT

Advance
C Programming Language

Practice Questions
Booklet
C Programming Language Syllabus
 Overview Of C
 Basic Structure of C Programming
 Keywords in C
 Identifiers in C
 Format Specifiers
 Format SpecifiersExamplesetc
 Data Types in C Language
 Introduction to Data Types in C
 int Data Type in C
 float Data Type in C
 double Data Type in C
 char Data Type in Cetc
 Variable in C Language
 Variable Introduction in C
 Variable Declaration and Initialization
 Variable types and Scope in C
 Local Variable in C
 Static Variable in C
 Global variables in C
 Storage Class in Cetc
 Constant in C Language
 Constants in C ,etc
 Operators and Enums in C Language
 Introduction to Operator
 Arithmetic Operators in C
 Relational Operators in C
 Bit-wise Operators in C
 Logical Operators in C
 Assignment Operators in C
 Conditional Operator in C
 sizeof() Operator in C
 Operator Precedence. etc
 Decision Making of C Language
 Decision Making in C Introduction
 if Statement
 if-else Statement
 Nested if Statement
 if else if Ladder
 switch case. etc
 Loop control in C Language
 Loop Introduction in C
 while loop in C
 do while Loop In C
 for Loop in C ,etc
 Control Flow in C Programming
 break Statement in C
 continue Statement in C
 go to Statement in C .etc
 Array in C Language
 Single Dimensional Array
 Multi-Dimensional Array in C .etc
 String & String functions in C
 All String Functions
 strcat() function
 strncat() function
 strcpy() function
 strncpy() function
 strlen() function
 strcmp() function.etc
 Function in C Language
 Function in C
 Function Calling in C
 return type in Function
 Call by Value in C
 User Define Function
 Predefined Functions. etc
 Recursion in c
 Introduction to Recursion
 Direct and Indirect Recursion, etc
Pointer in C Language
 Pointer in C
 types of pointer
 NULL pointer
 Pointer and Array
 Strings as pointers
 Pointer to Function.etc
 Structure in C Language
 Structure in C
 Nested Structure in C
 Array of Structures in C
 Pointer to Structure
 Structure to Function in C
 typedef in C, etc
 Union in C Language
 Union in C
 Dynamic Memory Allocation
C Precedence Table

Description Operator Associativity


Function expression () Left to right
Array expression [] Left to Right
Structure operator -> Left to Right
Structure operator . Left to Right
Unary minus - Right to Left
Increments Decrement ++ -- Right to Left
One’s complement  Right to Left
Negation ! Right to Left
Address of & Right to Left
Value of address * Right to left
Type cast (type) Right to Left
Size in byte: size of Right to left
Multiplication * Left to right
Division / Left to Right
Modulus % Left to Right
Addition + Left to Right
Subtractions - Left to Right
Left shift << Left to Right
Right shift >> Left to Right
Less than < Left to right
Less than or equal to <= Left to Right
Greater than > Left to right
Greater than or equal to >= Left to Right
Equal to == Left to right
Not equal to != Left to Right
Description Operator Assciativity
Bitwise AND & Left to Right
Bitwise exclusive OR  Left to right
Bitwise inclusive OR | Left to Right
Logical AND && Left to Right
Logical OR || Left to Right
Conditional ?: Right to Left
= <<= >>= Right to Left

*= /= %= Right to Left

Assignment += -= &= Right to Left

= \= Right to Left

<<= >>= Right to left


Comma , Right to Left
ADVANCE C PROGRAMMING
Q1. What will be the output of following C code?
#include <stdio.h>
int main()
{
double x=28;
int r;
r= x%5;
printf ("\n r=%d", r);
return 0;
}
(a)r=3
(b)Run time Error
(c)Compile Time Error
(d)None of These.

Q2. What will be the output of following C code?


#include <stdio.h>
int main()
{
int i = 10,j = 20;
float a, b, c;
a = i / j;
b = 1.0 * i / j ;
c = i / j * 1.0;
printf("%f ,%f, %f", a, b, c);
}
(a)0.000000, 0.500000, 0.500000
(b)0.500000, 0.500000, 0.500000
(c) 0.500000,0.000000,0.000000
(d)0.000000, 0.500000,0.000000

Advance C Programming Language Page 1


Q3. What is the output of following C Code?
#include <stdio.h>
int main()
{
int I ;
I=0x11+ 011+10;
printf("x=%x", I);
}
(a)22 (b)24
(c)36 (d)25

Q4. What will be the size of following structure?( Assume Integer is 32 bit)
struct {
static int x;
int y, z; } ;
(a) 6 bytes (b) 2 bytes
(c) 12 bytes (d) Compiler Error.

Q5. What is the output of following C Code?


#include <stdio.h>
void main()
{
char x[] = " I.I.T.BOMBAY. ", *p;
int i = 0;
p = x;
while ( i != 12)
{
i = i + 2;
p++;
printf("%c",*p);
}
}
(a) .I.T.B (b) I.I.T
(c) I.I.T.B (d)I.T.B

Advance C Programming Language Page 2


Q6. What is the output of the given C Code?______
#include <stdio.h>

int main()

int xray[ ] = {12, 40, 5, 7, 10};

int i, sum = 0, *b = xray;

for (i = 0; i < 5; i++)

sum = sum + (*b-i) - *(b+i);

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

return 0;

Q7. What should be the output of following code?


#include<stdio.h>
struct inode
{
char x, y, z;
};
int main(){
struct inode p = {'1', '0', 'a'+2};
struct inode *q = &p;
printf ("%c,%d", *((char*)q+1), *((char*)q+2));
return 0;
}
(a)0,99
(b)0,c
(c)65,99
(d)99,0

Advance C Programming Language Page 3


Q8. What should be the output of following C code?
#include<stdio.h>
void main()
{
int x , y;
x = 2003;
x++;
y = x++;
y = x;
y++;
x--;
x--;
printf("%d %d",x,y);
}
(a)2003 2004 (b)2003 2005
(c)2003 2006 (d)None of these.

Q9. What will be the output of executing the following C program is _______ .
#include <stdio.h>
int share(int x)
{
static int out = 0;
while(x)
{
out += x&1;
x >>= 1;
}
return out ;
}
void main ()
{
static int x = 0;
int i =6;
for (; i>0;i--)
{
x=x+share(i);
}
printf("%d\n", x);
}

Advance C Programming Language Page 4


Q10. Given the definition of function shown below, what is the value of fun (15, 9)?
#include <stdio.h>
int fun(int x, int y) {
if (x == y)
return x;
else
return x > y ? fun(x-y, y) : fun(x, y-x);
}
int main()
{
printf("%d", fun(15, 9));
return 0;
}
(a) 1 (b) 3 (c) 5 (c) 7

Q11. What will be output of given C code?


#include <stdio.h>
char *c[ ] = {"INDIA", "BEST", "TEST", "QUESTION"};
char **cp[ ] = {c+2, c+1, c+1};
char ***cpp = cp;
int main()
{
printf("%s ", **++cpp);
printf("%s ", *--*++cpp);
printf("%s ", *cpp[-2]);
printf("%s ", cpp[-1][-1]);
return 0;
}
(a) BEST INDIA TEST INDIA
(b) INDIA BEST TEST QUESTION
(c) B I T I
(d)None of these.

Q12. What will be the output of given code.(Assume that exchange(&x, &y)

Advance C Programming Language Page 5


exchanges the contents of x and y)?________
#include<stdio.h>
void exchange(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

int main ()
{
int list[] = {12, 7, 1, 5, 8};
int set =0;
int i;
while (set==0)
{
set =1;
for (i=0; i<4; i++)
{
if (list[i] < list[i+1])
{
exchange(&list[i], &list[i+1]);
set=0;
}
}
for (i=4; i>=1; i--)
{
if (list[i] > list[i-1])
{
exchange(&list[i], &list[i-1]);
set =0;
}
}
}
printf("%d", list[4]);
}
Q13. What should be the output of the following program?
#include <stdio.h>
void main()
{
char a[ ]= "Zeal Computers";
char *b= "Zeal Computers";
printf("%d %d", sizeof(a),sizeof(b));
printf("\n%d %d", sizeof(*a),sizeof(*b));
}
(a) (b) (c) (d)
15 8 14 1 15 15 11

Advance C Programming Language Page 6


11 14 14 1 1 88

Q14. What will be output of following code?


#include<stdio.h>
int main()
{
int a[5]={1,2,12,40,15};
int *ptr =a;
printf("%u\n",*++ptr +2 );
printf("%u\n",*(ptr-- +1) +5 );
printf("%u\n",*(ptr +2)- 10 );
printf("%u\n",*(ptr-- +3)- 4 );
printf("%u\n",*(ptr+1) );
return 0;
}
(a) (b) (c) (d)
4 4 15 4
17 10 12 17
2 12 10
36 12 36
1 1 1
Q15. #include <stdio.h>
#define Space(X) sizeof(X)/sizeof(*X);
void fun(int* X, int n)
{
int i;
*X += *(X + n - 1) += 5;
}
void printX(int* X, int n)
{
int i;
for(i = 0; i < n; ++i)
printf("%d ", X[i]);
}
int main()
{
int X[] = {20, 30, 40};
int Y = Space(X);
fun(X, Y);
printX(X, Y);

Advance C Programming Language Page 7


return 0;
}
(a)40 35 30 (b)65 30 40
(c)65 30 45 (d)50 45 40
Q16. #include <stdio.h>
int main()
{
int check(char *str)
{
char *str1 = str;
char *str2 = str + strlen (str) – 1;
while (str1 <=str2)
{
if (*str1 ++! = *str2 --)
return 0;
}
return 1;
}
}
What is the functionality of above code check ()?
(a) Check whether string is odd palindrome.
(b) Check whether the string is even palindrome
(c) Check whether the string is palindrome
(d) None of above

Q17. What will be the output of following C code?


#include<stdio.h>
typedef union bank{
char *a;
long double d;
unsigned int i;
}BANK1;
int main(){
BANK1 *ptr=(BANK1 *)2000;
long double x;
ptr=ptr-5;
printf("%u", ptr);

Advance C Programming Language Page 8


return 0;
}
(a)1920 (b)1920
(c)1880 (d)None of these.
Q18. #include<stdio.h>
int show(char);
int(*show1())(char);
int main()
{
int x;
int(*ptr)(char);
ptr = show1();
x=(*ptr)('G');
printf("%d", x);
return 0;
}
int show(char c)
{
return c;
}
int(*show1())(char)
{
return show;
}
(a)70 (b)71
(c)72 (d)73

Q19. #include<stdio.h>
int function(int q)
{
static int a ;
if(q <= 0) return 1;
if(q>10)
{
a= q;
return function(q- 10)+a;

}
return function(q-5) + a;
}
int main()
{

Advance C Programming Language Page 9


printf("%d", function(30));
return 0;
}

Q20. What will be the output of given code if the input given to code is: abccdd11
#include<stdio.h>
int main()
{
int i,j, ascii[128];
char ip[30];
printf("Enter Input string:");
scanf("%s", ip);
for(i=0;i<128;i++)
{
ascii[i]=0;
}
i=0;
while(ip[i] != '\0')
{
j= (int)ip[i];
ascii[j]++;
if(ascii[j]>1)
{
printf("%c", ip[i]);
return 0;
}
i++;
}
return 0;
}
(a) Print the position of first repeated character in the given string.
(b) Irrespective of given input string it will print ‘a’.
(c) Print the first repeated character in the string.
(d) Compilation error.

Advance C Programming Language Page 10


Q21. What will be the output of given code?
#include <stdio.h>
void main()
{
char s[ ]="Learn";
int i;
for(i=1;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i++),*(i+s),i[s]);
}
(a) aeee (b) eeea (c)Leea (d)Lear
nrrr rrrn rrrn earn

Q22. What will be output of give C code?


#include <stdio.h>
void main(){
int a=-2,b=-1,c=0,d=1,e;
int *p,*q;
e=a++&&b++&&c++||d++;
p= &e;
q=&b;
printf("%d %d %d %d %d",a,b,c,d,e);
printf("\n%u",++*p + ++*q);
}
(a) (b) (c) (d)
-1 0 1 2 2 -1 0 1 2 1 -1 0 1 2 1 -2 0 1 2 1
5 2 3 2

Advance C Programming Language Page 11


Q23. What will be the output of given C code?_____
#include <stdio.h>
void main()
{
static int array[][3] = { {10,5,-3}, {9, 0, 0}, {32,20,1}, {0,0,8}, {1,2,1} };
int i, j, k;
int sum = 0;
for( i = 0; i <= 4; i++ )
for( j = 0; j <= 4; j++ )
sum = sum + array[i][j];
printf("%d", sum );
}
Q24. What the above code is doing on given data set.
#include <stdio.h>
void main()
{ int temp;
int a[ ]={1,2,3,4};
int n =4;
for (int i = 0; i < n/2; i++)
{
temp = a[n-i-1];
a[n-i-1] = a[i];
a[i] = temp;
}
for(int j=0;j<n; j++)
{
printf("%d", a[j]);
}

}
(a) It will half the values of array and then print itin reverse order.

Advance C Programming Language Page 12


(b)It will half the values of array and then print it.
(c) It will print the array in given order.
(d) Reverses the order of values in a one-dimensional string array
Q25. What will be output of given code
#include<stdio.h>
void f(int i, int j, int k)
{
printf("%d %d %d", i, j, k);
}
void main()
{
int i=10;
f(i++,i++,i++);
printf(" %d\n", i);
i=12;
f(i++,i++,i++);
printf(" %d", i);
}
(a) (b) (c) (d)
13 10 11 12 12 11 10 13 10 10 10 10 Both (a) & (b) are
14 13 12 15 14 13 12 15 12 12 12 12 true.

Q26. What will be the output of given program?


#include <stdio.h>
int main()
{
struct s1
{
char *z;
int i;
struct s1 *p;
};
static struct s1 a[]=
{
{"Monday",1,a+2},{"Tuesday",2,a+1},{"Wednesday",3,a}
};
struct s1*ptr=a;
printf("%s\t",(ptr++->z));
printf("%s\t",a[(ptr)->i].z);
printf("%s\t",a[--(ptr->p->i)].z);
return 0;
}
(a) Monday Tuesday Wednesday

Advance C Programming Language Page 13


(b) Wednesday Tuesday Monday
(c) Monday Wednesday Tuesday
(d)Compilation Error.
Q27. What will be the output of the following program:
#include <stdio.h>
int main()
{
static int a[3][3][3]=
{
{
3,1,5,
5,8,7,
9,10,11
},
{
12,14,16,
17,18,19,
20,21,22
},
{
23,24,25,
26,27,28,
29,30,31
}
};
static int *ptr[]=
{
a[0][0],a[0][1],a[0][2],
a[1][0],a[1][1],a[1][2],
a[2][0],a[2][1],a[2][2]
};
int *ptr1[]={a[0][0],a[1][1],a[2][2]};
int **ptr2=ptr, i;
printf("\n");
for(i=0;i<=2;i++)
printf("%d\t",*(ptr1[i]));
printf("\n");
for(i=0;i<=4;i++)
printf("%d\t",*ptr[i]);
return 0;
}
(a)

Advance C Programming Language Page 14


(a) (b) (c) (d)
3 17 29 3 12 23 315 3 17 29
3 5 9 12 17 3 5 9 12 17 12 14 16 3 5 9 29 30 31
23 24 25

Q28. What will be output of given code?


main()
{
static int a[2][2]={1,2,3,4};
int i,j;
static *p[]={a,a+1,a+2};
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j),*(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i));
}
(a) (b) (c) (d)
1111 1234 12 3 4 11 1 1
2323 2222 12 3 4 23 2 3
3232 3333 32 2 3 32 3 2
4343 4444 43 4 3 44 4 4
Q29. What will be output of given C code?
#include<stdio.h>
int main()
{
int i;
for(i =0; i<35; i++)
{
switch(i)
{
//default: break;
case 10:
case 20:
case 5:
case 40:
break;
printf("C programming");
}
}
return 0;
}
Advance C Programming Language Page 15
(a)It will print C programming 35 times
(b)It will print C programming 3 times
(c) It will print C programming 2 times
(d) It will not print C programming.
Q30. #include<stdio.h>
void main()
{
int x = 5;
int y= 2;
char op = '*';
switch (op)
{
default : x += 1;
case '+' : x += y;
case '-': x -= y;
}
printf("%d", x);
}
What will be the value of x after the execution of above code?_______

Q31. What value will be printed by given C code?_______


#include <stdio.h>
void area(int *);
int volume(int *);
int volume(int *p)
{
static int a=2;
a=a + *p ;
if(a>20)
return a;
else
{ area(&a);
volume(&a);
}
}
void area(int *k)
{
*k =*k+3;
}
int main()
{ int h;
int r=6;
h=volume(&r);

Advance C Programming Language Page 16


printf("%d", h);

Q32. What will be output of given C code?


#include<stdio.h>
int main()
{
int array[5]={10,20,30,40,50};
int *ptr =array;
printf("%u\n",*++ptr +3 - *ptr );
printf("%u\n",*(ptr-- +2) - 5 );
printf("%u\n",*(ptr +3)- 10 + *(ptr+1) );
printf("%d",(ptr = ptr += sizeof(int))[-2]);
return 0;
}
(a) (b) (c) (d)
3 30 3 Compilation
35 35 35 Error.
50 50 50
30 3 40
Q33. What will be output of following code?
#include <stdio.h>
void function1(int*, int);
void function2(int*, int);
void (*X[2])(int*, int);
int main()
{
int a = 13;
int b = 6;
X[0] = function1;
X[1] = function2;
X[0](&a, b);
printf("%d %d ", a, b);
X[1](&b, a);

Advance C Programming Language Page 17


printf("%d %d", a, b);
return 0;
}
void function1(int *p, int q)
{
int tmp = *p;
*p = *p+q;
q = tmp;
}
void function2(int *p, int q)
{
int tmp=*p;
*p=tmp;
q=tmp;
}
(a)13 6 13 6 (b)6 6 6 6 (c)19 13 19 13 (d)19 6 19 6

Q34. What will be output of given code?


#include <stdio.h>
int main()
{
struct s1
{
char *z;
int i;
struct s1 *p;
};
struct s1 a[ ] ={{"Nagpur", 1, a + 1 },{"Raipur", 2, a + 2 },
{"Kanpur", 3, a } };
struct s1 *p = a ;
int j ;
for ( j =0;j<=2;j++)
{
printf ( "%d",--a[j].i);
printf ( "%s\n", ++a[j].z) ;
}
return 0;
}

Advance C Programming Language Page 18


(a) (b) (c) (d)
0Nagpur Nagpur 0agpur Compilation
1Raipur Raipur 1aipur Error.
2Kanpur Kanpur 2anpur
Q35. What will be output of given C code?
#include<stdio.h>
int main()
{
struct a
{
struct b
{
char name[10];
int age ;
}bb;
struct C
{ char address[50];
int sal ;
}cc ;
};
struct a *ptr;
struct a aa = {
{ "George", 30 }, { "86, Vermalayout, Nagpur", 4000 }};
ptr = &aa ;
printf ("%s %s %d %d",ptr -> bb.name, ptr ->cc.address, ptr ->bb.age, ptr -
>cc.sal);
return 0;
}
(a) (b)
George 30, Vermalayout, Nagpur George 86, Vermalayout, Nagpur 30
86 4000 4000
(c) (d)
George 86, Vermalayout, Nagpur George 86, Vermalayout, Nagpur 30
4000 4000 30

Advance C Programming Language Page 19


Q36. Which of the following is correct option for given code?
#define max 5
#define int arr1[max]
main()
{
typedef char arr2[max];
arr1 list={0,1,2,3,4};
arr2 name="name";
printf("%d %s", list[0],name);
}
(a)0 name (b)0 n
(c)Compilation error (d)Garbage Output

Q37. What will be output of given C code?(Assume machine to be Big Endian)


#include<stdio.h>
void main()
{
struct a
{
int i;
int j;
};
struct b
{
char x ;
char y ;
};
union c
{
struct a aa;
struct b bb;
};
union c u ;
u.aa.i = 120 ;
u.aa.j = 250;
printf ( "%d %d ", u.aa.i, u.aa.j );
printf ( "%d %d", u.bb.x, u.bb.y ) ;
}
(a) 120 250 0 0

Advance C Programming Language Page 20


(b)120 250 120 0
(c) 120 250 250 250
(d)250 120 120 0
Q38. #include<stdio.h>
struct structure
{
char x, y, z;
};
int main(){
struct structure p = {'2', '3', 'a'+1};
struct structure *q = &p;
printf ("%c, %d", *((char*)q+1), *((char*)q+2));
return 0;
}
(a)1, 90 (b)0, 98
(c)3,98 (d)Compilation Error

Q39. What will be output of given C code?


#include <stdio.h>
int globally (int x, int y);
int main()
{
float k;
k = globally(22, 59);
printf("%f", k);
return 0;
}
int globally (int x, int y)
{ if (x < 0)
{ return globally (x, y);
}
else if (y < 0)
{
return globally (x, y);
}
else if (x == 0 && y == 0)
return 0;

else
{
return 100 * globally (x / 10, y / 10) + 10 * (x % 10) + y%10;
}

Advance C Programming Language Page 21


}
(a)2529.000000 (b)2560.000000 (c)2520.000000 (d)2520

Q40. What will be output of given C code?


#include <stdio.h>
#include <string.h>
struct tag
{
char lname[20];
char fname[20];
int age;
float rate;
};
struct tag my_struct; /* define the structure */
void show_name(struct tag *p); /* function prototype */
int main(void)
{
struct tag *st_ptr; /* a pointer to a structure */
st_ptr = &my_struct; /* point the pointer to my_struct */
strcpy(my_struct.lname,"Gate");
strcpy(my_struct.fname,"Instructor");
printf("\n%s ",my_struct.fname);
printf("%s\n",my_struct.lname);
my_struct.age = 32;
show_name(st_ptr); /* pass the pointer */
return 0;
}
void show_name(struct tag *p)
{
printf("\n%s ", p->fname); /* p points to a structure */
printf("%s ", p->lname);
printf("%d\n", p->age);
}
(a) (b)
Gate Instructor Instructor Gate
Advance C Programming Language Page 22
Instructor Gate 32 Instructor Gate 32
(c) (d)
Gate Instructor 32 Compilation Error
Q41. Identify the error if any in the following code?
#include<stdio.h>
int main()
{
char *p;
*p = (char)calloc(io);
strcpy(p, "HELLO");
printf("%s", p);
free(p);
return 0;
}
(a) No error
(b) Error in the statement: strcpy(p,"HELLO");
(c) Error in the statement: *p=(char)calloc(w);
(d) Error in the statement: free(p);

Q42. What will be the output of given C code?


#include<stdio.h>
int main()
{
int a[] = {1,2,3,4,5 };
int *p[] = {a,a+1, a+2, a+3, a+4};
int **ptr = p;
ptr++;
printf("%d %d %d", ptr - p, *ptr - a, **ptr);
*ptr++;
printf(" %d %d %d", ptr - p, *ptr - a, **ptr);
*++ptr;
printf(" %d %d %d", ptr - p, *ptr - a, **ptr);
++*ptr;
printf(" %d %d %d", ptr - p, *ptr - a, **ptr);
}
(a)1 1 1 2 2 2 3 3 3 3 4 4
(b)1 1 2 2 2 3 3 3 4 3 4 5
(c) 1 2 3 4 5 1 2 3 4 5 1 2

Advance C Programming Language Page 23


(d) Run time error.

Q43. #include <stdio.h>


typedef int *PtrType;
int main()
{
PtrTypeptr_a, ptr_b;
int num_c = 3, num_d = 6;
ptr_a = &num_c;
ptr_b = ptr_a;
printf("%d %d\n",*ptr_a,*ptr_b);
ptr_b = &num_d;
printf("%d %d\n",*ptr_a++,*ptr_b);
*ptr_a = *ptr_b;
printf("%d %d\n",++*ptr_a,*ptr_b);
printf("%d %d",num_c,*&*&*&num_c);
return 0;
}
(a) (b) (c) (d)
33 33 33 33
36 36 36 44
77 76 66 77
33 33 77 88
Q44. Consider the following C Code:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int main ()
{
char *s = "abc";
char *t = "def";
int n = strlen (s), m = 1 + strlen (t);
char *r = ( char *) malloc ( sizeof ( char ));
char *p, *q, *w;

Advance C Programming Language Page 24


q = s;
w = r;
while (*q != '\0')
{
*r = *q;
r++;
p = t;
while (*p != 'e')
{
*r = *p;
r++;
p++;
}
q++;
}
*r = '\0';
printf ("%s %s %s", s, t, w);
}
(a)abcdefadbdcef
(b)abcdefabcdef
(c)abcdefadbdcd
(d) None of these
Q45. #include <stdio.h>
#include <stdlib.h>
int main(void) {
int *t = (int *) malloc(sizeof(int));
t++;
*t = 6;
t[-1] = *t / 3;
t--;
t[1] = *t / 4;
printf("%d\n",*t);
free(t);
return 0;

Advance C Programming Language Page 25


}
What will be the output of given C code? _______
Q46. What will be the output of the given C program?
#include <stdio.h>
#include <stdio.h>
int main()
{
int array[3][3][2]=
{
{
1,2,
3,4,
5,6
},
{
7,8,
9,10,
11,12
},
{
13,14,
15,16,
17,18
}
};
printf("%d %d %d\n",*(*(array[0]+1)+2),*(*(*(array+2)+1)+1),*(array[1][2]+1));
return 0;
}
(a)5 16 12
(b) 5 1 2
(c) 3 15 11
(d)3 16 12

Advance C Programming Language Page 26


Q47. What is the output of the following C code?
#include <stdio.h>
#include <string.h>
int main()
{
char string[10];
char *p = string;
strcpy(string, "98260-");
strcat(p, "Zeal");
p += 6;
*(--p) = '\0';
p+=1;
printf("(%s) (%s) \n", string, p);
return 0;
}
(a)(Zeal)
(b)(Zeal) (98260)
(c)(98260) (Zeal)
(d) Segmentation fault.

Q48. What will be output of following C code?


#include <stdio.h>
void main()
{
void funl(void*);
char a[] = "purpose";
void *temp;
temp = a;
funl(temp);
}
void funl(void *temp1 )
{
int t1 = 0;

Advance C Programming Language Page 27


while(*((char*)temp1+ t1++ )!='\0')
{
printf("%c",*((char*)temp1 + t1));
}
}

(a)purpose
(b) qurpose
(c)ose
(d)urpose.

Q49. What will be output of given C code?


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int i;
char x[]="Unity";
char *y="India";
char *Temp;
Temp=x;
x=malloc(strlen(x) + 1);
strcpy(x, y);
y = malloc(strlen(Temp) + 1);
strcpy(x, Temp);
printf("(%s)",x);
free(x);
free(y);
}
(a)Unity
(b)India
(c) Unity India
(d) Compilation Error

Advance C Programming Language Page 28


Q50. Consider the following C Code:
#include<stdio.h>
void main()
{
int array[10] = {1,2,3,5,6,7,8,9,10,12}, n=10, c, d, swap;
for (c = 0 ; c < ( n - 1 ); c+=2)
{
for (d = 0 ; d < n - c - 2; d++)
{
if(((d%3 == 0)&&(array[d] > array[d+2]) )||( (d%3 == 1)&& (array[d] <
array[d+2])))
{
swap = array[d];
array[d] = array[d+2];
array[d+2] = swap;
}
}
}
for ( c = 0 ; c < n ; c++ )
printf("%d ", array[c]);
}
(a)1 5 3 2 8 7 6 12 10 9
(b)1,2,3,5,6,7,8,9,10,12
(c)1,3,5,7,9,11,12
(d)1,3,6,9,12

Q51. What will be output of given C code?


#include <stdio.h>
int f( int i ) { printf( "%d ", i ); return i; }
int g( char c ) { printf( "%c ", c ); return c; }
int main( void ) {
char s1[]="GATE", s2[]="IISC";
int i=20, j=10, k=30, m=40;

Advance C Programming Language Page 29


char c1='A', c2='a', c3='B';
if ((f(i) < f(j)) || (f(k) >= f(m))) {}
printf( "\n" );
if ((f(i) < f(k)) && (f(j) >= f(m))) {}
printf( "\n" );
if ((s1 == s2) || (f(j) >= f(k)) || (g(c1) == g(c3))) {}
printf( "\n" );
if ((g(c1) != g(c3)) && (g(c1) == g(c2))) {}
printf( "\n" );
if ((g(c1) > g(c2)) && (g(c1) > g(c3))) {}
return 0;
}
(a) (b) (c) (d)
20 10 30 40 20 10 30 20 10 Compilation Error
20 10 30 40 40 10 30 20 40
10 30 A B 20 30 10 20 30 A B
ABAa 40 ABAa
AB 10 30 A B Aa
ABAa
Aa
Q52. What will be output of following C code?
#include<stdio.h>

int main()

char S1[]="hello";

char S2[]= {'h','e','l','l','o'};

int i=100;

int *p1,*p2;

p1=&i;

p2=(++p1);

--p1;

Advance C Programming Language Page 30


printf("%u",p2-p1);

int a[] ={0,1,2,3,4};

int *p[]={a,a+1,a+2,a+3,a+4};

int **ptr=p;

ptr++;

printf("%d,%d,%d", ptr-p,*ptr-a,**ptr);

printf("%d,%d,%d,%d",sizeof(S1),sizeof(S1[0]),sizeof(S2),sizeof(S2[0]));

return 0;

(a)11,1,11,16,1,5
(b)1,1,1,1,6,1,5,1
(c)11,1,16,1,5,1
(d)None Of these

Q53. What will be output of given C code?


#include<stdio.h>
int main()
{
static char*a[ ]={
"I", "am" ,"In", "Top","10"
};
static char **ptr[]={a+3,a+2,a+1,a};
char ***p=ptr;
printf("%s\n", **++p);
printf("%s\n", *--*++p+2);
printf("%s\n", p[-1][-1]+1);
printf("%s\n", **++p+2);
return 0;
}

Advance C Programming Language Page 31


(a) (b) (c) (d)
In In In Compilation Error
Top am am
10 m In
am am am
Q54. #include <stdio.h>
int main()
{
int arr_size=5,i,ans=0,sum=0,leftsum=0 ;
int arr[7]= {2, -5, 6};
for(i=0;i<arr_size;i++)
for (i = 0; i <arr_size; i++)
sum += arr[i];
for( i = 0; i <arr_size; i++)
{
sum -= arr[i];
if(leftsum == sum)
{
ans=i;
break;
}
leftsum += arr[i];
}
printf("%d", leftsum);
return 0;
}
What will be output of given C code?_______

Q55. Identify the problem associated with following code?


#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *load1 = "opengenus";
char *load2 = "new_data";
load2 = load1;
free(load1);
free(load2);
return 0;

Advance C Programming Language Page 32


}
(a)Memory Leak
(b)Dangling pointer
(c)Run time Error
(d)No Error.
Q56. Identify whether the problem associated with the following code, if we execute
this program with the following arguments:”hello this is a test”?
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *string, *string_so_far;
int i, length;
length = 0;
for(i=0; i<argc; i++)
{
length += strlen(argv[i])+1;
string = malloc(length+1);
/* * Copy the string built so far. */
if(string_so_far != (char *)0)
strcpy(string, string_so_far);
else *string = '\0';
strcat(string, argv[i]);
if(i < argc-1) strcat(string, " ");
string_so_far = string;
}
printf("You entered: %s\n", string_so_far);
return (0);
}
(a)Memory Leak
(b)Dangling pointer
(c)No error
(d)hello this is a test

Advance C Programming Language Page 33

You might also like