0% found this document useful (0 votes)
3 views

C programming Advanced sheet _2022

The document contains a series of C programming questions and code snippets, each followed by multiple-choice answers. The questions cover various topics such as pointer manipulation, function behavior, memory management, and output predictions. The document serves as a test or practice material for advanced C programming concepts.

Uploaded by

sourabhyadav2624
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C programming Advanced sheet _2022

The document contains a series of C programming questions and code snippets, each followed by multiple-choice answers. The questions cover various topics such as pointer manipulation, function behavior, memory management, and output predictions. The document serves as a test or practice material for advanced C programming concepts.

Uploaded by

sourabhyadav2624
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

ADVANCE C PROGRAMMING

Q1. What will be the value printed by this program?


#include <stdio.h>
int main()
{
int arri[] = {'1', '2' ,'a'};
int *ptri = arri;
char arrc[] = {11, 21 ,31};
char *ptrc = arrc;
printf("%d ", *arri);
printf("%d ", *ptri);
printf(" %d ", *arrc);
printf("%d ", *ptrc);
return 0;
}
(a) 49,49,11,11
(b) 50,50,12,12
(c) four garbage value
(d) None of these
Q2. What will be the output of following C Program?
(Assume Little Endian Machine and Int is 2 byte for this question)
#include<stdio.h>
int main(){
int a = 320;
char *ptr;
ptr =( char *)&a;
printf("%d ",*ptr);
return 0;
}
(a)320 (b)1
(c)64 (d)Compilation Error

ADVANCED C -PROGRAMMING Page 1


Q3. Consider the following C program:

#include <stdio.h>
int r(){
static int num=9;
return num--;
}
int main(){
for (;r();r())
printf("%d ",r());
return 0;
}
(a)8 5 2 (b)6 3
(c)8 4 2 (d)6 3 0
Q4. Assume that an integer variable takes 4 bytes and a character variable takes 1
byte. Consider the following C program:
#include <stdio.h>
int myFunction(void);
int speed = 12;
int main(void)
{
int index;
int speed = 20;
for (index = 0; index < 2; index++)
myFunction( );
printf("%d\n", speed);
}
int myFunction(void)
{
static int speed = 21;
printf("%d ", speed *= 2);
return speed;
}
What is the output of the above code?
(a) 42 84 84 (b) 42 42 20
(c) 12 12 20 (d) 42 84 20

ADVANCED C -PROGRAMMING Page 2


Q5. Consider the following code:
void foo(int n, int sum){
int k = 0, j = 0;
if (n == 0) return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf ("%d,", k);
}
int main () {
int a = 2048, sum = 0;
foo (a, sum);
printf ("%d\n", sum);
getchar();
}
(a) 2, 0, 4, 8, 0 (b) 8, 4, 0, 2, 0
(c) 2, 0, 4, 8, 14 (d) 8, 4, 0, 2, 14
Q6. Consider the following C Code:
#include<stdio.h>
int main()
{
int array[10] = {4,3,5,6,1,2,9,8,5,4}, n=10, c, d, swap;
for (c = 0 ; c < ( n - 1 ); c+=2)
{
for (d = 0 ; d < n - c - 2; d++)
{
if( ( (d%2 == 0) && (array[d] > array[d+2]) )||( (d%2 == 1)&& (array[d] <
array[d+2])))
{
swap = array[d];

ADVANCED C -PROGRAMMING Page 3


array[d] = array[d+2];
array[d+2] = swap;
}
}
}
for ( c = 0 ; c < n ; c++ )
printf("%d ", array[c]);
printf("\n" );
return 0;
}
What is the output of the above C code?
(a) 1 2 3 4 4 5 5 6 8 9
(b) 1 8 4 6 5 4 5 3 9 2
(c) 8 1 6 4 4 5 3 5 2 9
(d) None of these

Q7. Integer value printed by the program given below is? _________
#include <stdio.h>
int main()
{
int xa = 6, xb=7;
printf("%d", compare(&xa,&xb)*compare(&xb,&xa));
}
int compare(void *m, void *n)
{
int *m1, *n1;
m1 = (int *)m;
n1 = (int *)n;
return (*m1 > *n1?*m1:*n1);
}

ADVANCED C -PROGRAMMING Page 4


Q8. What will be the output of following c code?__________
#include <stdio.h>
int main()
{
int a[] = {1, 3, 5, 7,9};
int i, total = 0, *b = a + 3 ;
for (i = 0; i < 4; i++)
total = total + (*b - i) - *(b - i) ;
printf ("%d\n", total);
return 0 ;
}
Q9. What will be the output of following C program?
#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[]="Hello", s2[]="Hello";
int i=10, j=20, k=30, m=40;
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;
}

ADVANCED C -PROGRAMMING Page 5


(a) (b) (c) (d)
10 20 10 20 10 None of these.
10 30 20 40 10 20 30 40 10 30 20 40
20 30 A B 20 30 65 66 20
ABAa 65 66 65 97 ABAa
Aa 65 97 Aa
Q10. Consider the C program below
#include <stdio.h>
int *A, stkTop;
int stkFunc (int opcode, int val)
{
static int size =0, stkTop=0;
switch (opcode) {
case -1 : size = val; break;
case 0 : if (stkTop < size) A [stkTop++] = val; break;
default : if (stkTop) return A [--stkTop];
}
return -1;
}
int main ( )
{
int B[20] ; A = B; stkTop = -1;
stkFunc (-1, 10);
stkFunc (0, 5);
stkFunc (0, 10);
printf("\n%d",stkFunc(1,0)+stkFunc(1,0));
return 1;
}
The value printed by the above program is _____________

ADVANCED C -PROGRAMMING Page 6


Q11. What will be the output of following C code if input to the code is )10?
#include<string.h>
int main()
{
int a=0,i,set=0;
char input[100];
scanf("%s", input);
for(i=0;input[i]!='\0';i++)
{
switch(input[i])
{
case '(':
a++;
break;
case ')':
a--;
break;
default :
printf("%d ",a);
set = 1;
}
}
if(set==0)
printf(" #");
else
printf("##");
return 0;
}
(a)-1 0 #
(b)-1 -1 ##
(c)0 0 #
(d)-1 -1 #

ADVANCED C -PROGRAMMING Page 7


Q12. What will be the output of given code?_______
#include <stdio.h>
int *A, stkTop;
int stkFunc (int opcode, int val)
{
static int size =0, stkTop=0;
switch (opcode) {
case -1 : size = val; break;
case 0 : if (stkTop < size) A [stkTop++] = val; break;
default : if (stkTop) return A [--stkTop];
}
return -1;
}
int main ( )
{
int B[20] ; A = B; stkTop = -1;
stkFunc (-1, 10);
stkFunc (0, 5);
stkFunc (0, 10);
printf("\n%d",stkFunc(1,0)+stkFunc(1,0));
return 1;
}
Q13. What is the output of the following c code:
#include <stdio.h>
char *c[] = {"GATE@ZEAL", "C", "TEST", "QUESTION"};
char **cp[] = {c+3, c+2, c+1, c};
char ***cpp = cp;

int main()
{
printf("%s ", **++cpp);

ADVANCED C -PROGRAMMING Page 8


printf("%s ", *--*++cpp+3);
printf("%s ", *cpp[-2]+3);
printf("%s ", cpp[-1][-1]);
return 0;
}
(a) TEST E@ZEAL STION C (b) TEST E@ZEAL STION
(c) TEST @ZEAL ESTION C (d) Compile time error

Q14. Consider the following code fragment:


struct data
{
struct data *s;
}a, b, c;
Which three lines of code are required to create a circular data structure (a.s
points to b, b.s points to c, and c.s points to a)?
(a) a.s = b; b.s = c; c.s = a;
(b) &a.s = b; &b.s = c; &c.s = a;
(c) a.s = *b; b.s = *c; c.s = *a;
(d) a.s = &b; b.s = &c; c.s = &a;
Q15. What would the following program print?
#include <stdio.h>
struct factor
{ char x, y, z };

int main ()
{ struct factor p = {'l', '0', 'b'+3};
struct factor *q = &p;
printf ("%c, %c", *((char*)q+1),*((char*)q+2)) ;
return 0 ;
}
(a)0,e (b)0,b+3
(c)’0’,’b+3’ (d)’0’,’e’

ADVANCED C -PROGRAMMING Page 9


Q16. What will the following program print?
#include<stdio.h>
#include<math.h>
void main()
{ double pi = 3.1415926535;
int a = 1;
int i;
for(i=0; i < 3; i++)
if(a = cos(pi * i/2) )
printf("%d ",1);
else printf("%d ", 0);
}
(a)100 (b)010
(c)111 (d)101
Q17. What will be the output of following code?
#include<stdio.h>
#include<conio.h>
int show();
int(*array[3])();
int(*(*ptr)[3])();
void main(){
array[0]=show;
array[1]=getch;
ptr=&array;
printf("%d",(**ptr)());
(*(*ptr+1))();
}
int show(){
int x=6;
return x++;
}
(a)6 (b)7
(c)99 (d)Compilation Error.

ADVANCED C -PROGRAMMING Page 10


Q18. What is the output of the program? ________
#include<stdio.h>
int f(int x, int *py, int **ppz)
{
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x*y&z*(*py);
}
int main()
{
int c, *b, **a;
c = 4;
b = &c;
a = &b;
printf("%d ", f(c, b, a));
return 0;
}

Q19. In the following program add a statement in the function fact() such that the
factorial gets stored in j.
#include<stdio.h>
void fact(int*);
int main()
{
int i=5;
fact(&i);
printf("%d ", i);

ADVANCED C -PROGRAMMING Page 11


return 0;
}
void fact(int *j)
{
static int s=1;
if(*j!=0)
{
s = s**j;
*j = *j-1;
fact(j);
/* Add a statement here */
}
}
Which of the following is the correct statement?
(a) j = s; (b) *j =s
(c) *j = &s (d) None of these
Q20. What will be the output of following C program?
#include <stdio.h>

char *c[] = {"Gateatzeal", "MSQ", "TEST", "QUIZ"};


char **cp[] = {c+3, c+2, c+1, c};
char ***cpp = cp;

int main()
{
printf("%s ", **++cpp);
printf("%s ", *--*++cpp+3);
printf("%s ", *cpp[-2]+3);
printf("%s ", cpp[-1][-1]+1);
return 0;
}
(a) TEST eatzeal I MS
(b)TEST eatzeal Z SQ
(c) TEST ezeal Z QU
(d) None of these.

ADVANCED C -PROGRAMMING Page 12


Q21. Consider the following C program:
#include <stdio.h>
int main()
{
printxy(1,1);
return 0;

}
void printxy(int x, int y)
{
int *ptr;
x = 0;
unsigned int z;
ptr = &z;
y = *ptr;
*ptr = 1; // line 13
printf("%d", y);
}
What is the output of the program with and without the line 13 respectively?
(a) 0, garbage value
(b) garbage value, 0
(c) 1, 1
(d) 0,0

Q22. Consider the following C Code:


#include<stdio.h>
int main()
{
/* assume array begins at location 1002
and the size of integer is 2 bytes*/
int a[] = {0,1,2,3,4 };

ADVANCED C -PROGRAMMING Page 13


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);
}
What is the output of above C code?
(a) 1 1 1 2 2 2 3 3 3 4 4 4 (b) 1 1 1 2 2 2 3 3 3 3 4 4
(c) 1 3 3 2 2 2 3 1 1 3 2 2 (d) None of these
Q23. Consider the following C Code:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int main ()
{
char *s = "abc";
char *t = "de";
int n = strlen (s), m = 1 + strlen (t);
int l = n * m + 1;
char *r = ( char *) malloc (l * sizeof ( char ));
char *p, *q, *w;
q = s;
w = r;
while (*q != '\0')
{
*r = *q;

ADVANCED C -PROGRAMMING Page 14


r++;
p = t;
while (*p != '\0')
{
*r = *p;
r++;
p++;
}
q++;
}
*r = '\0';
printf ("%s %s %s", s, t, w);
}
What is the output of the above C code?
(a) abc de abcde (b) abc de
(c) abc de abebc (d) abc de adebdecde
Q24. What is the output value would be printed by the program given below, if given
input is 2 ? ______
#include <stdio.h>
int add(int a, int b)
{
return(a+b);
}
int subtract(int a, int b)
{
return(a-b);
}
int multiply(int a, int b)
{
return(a*b);
}

ADVANCED C -PROGRAMMING Page 15


int main()
{ int out;
int (*fun_ptr_arr[])(int, int) = {add, subtract, multiply};
unsigned int ch, a = 15, b = 10;
printf("Enter Choice: 0 for add, 1 for subtract and 2"
"for multiply\n");
scanf("%d", &ch);

if (ch > 2) return 0;


out= (*fun_ptr_arr[ch])(a, b);
printf("%d", out);
return 0;
}
Q25. What is printed by the following C program?______
#include <stdio.h>
int z(int a, int *by, int **cy)
{
int b,c;
**cy += 1;
c = **cy;
*by += 2;
c = *by;
a += 4;
return a + b + c;
}

void main()
{
int c, *b, **a;
c = 4;
b = &c;
a = &b;
printf( "%d", z(c, b, a));
}

ADVANCED C -PROGRAMMING Page 16


Q26. What will be the output of given code?_______
#include <stdio.h>
#include <math.h>
int main()
{
int div;
int x = 36;
int total = 0;
while(x <= 41)
{
div = 2;
while(x % div != 0 && div <= sqrt(x))
{
div++;
}
if(div > sqrt(x))
{
total += div;
}
x++;
}
printf("Sum: %d\n", total+div);
}
Q27. What will be the out of given C code?
#include<stdio.h>
void XArray(int[], int);
int main()
{
int x[8] = {2, 3, 5, 4, 1, 0, 7, 6};
int i;
for(i = 0; i < 6; i += 2)

ADVANCED C -PROGRAMMING Page 17


{
XArray(x, i);
}
printf("x[0] = %d\n", x[0]);
printf("x[3] = %d\n", x[3]);
printf("x[7] = %d\n", x[7]);
return(0);
}
void XArray(int y[], int i)
{
int j;
for(j = 1; j < 8 - i; j++)
{
y[j] += y[j - 1];
}
}
(a) (b) (c) (d)
x[0] = 2 x[0] = 2 x[0] = 2 None of these.
x[3] = 47 x[3] = 57 x[3] = 67
x[7] = 28 x[7] = 28 x[7] = 28
Q28. Consider the program given below
#include <stdio.h>
#include <string.h>
#include <limits.h>
#define TRUE 1
#define FALSE 0
int DoAdd(int * Result, int , int );
int main()
{
int nFirst = 45300;
int nSecond = 700;
int nSum = 0;
if (!DoAdd(&nSum, nFirst, nSecond))
{

ADVANCED C -PROGRAMMING Page 18


printf("%d",nSum);
}
printf("%4d",nSum);
return (0);
}
int DoAdd(int * nResult, int nFirstValue, int nSecondValue)
{
if ((long)nFirstValue + (long)nSecondValue > (long)INT_MAX)
{
return(FALSE);
}
else
{
*nResult = nFirstValue + nSecondValue;
}
return(TRUE);
}
What is the value printed by the program? _______
Q29. What will be the output of following C program?
#include<stdio.h>
int main(){
static char *s[4]={"CSO", "DBMS", "Digital", "ADA"};
typedef char *( *ppp)[4];
static ppp p1=&s,p2=&s,p3=&s,p4=&s;
char * (*(*array[4]))[4]={&p1,&p2,&p3,&p4};
char * (*(*(*ptr)[4]))[4]=&array;
p2+=1;
p3+=2;
printf("%s",(***ptr[0])[3]);
return 0;
}
(a) Compilation Error
(b) ADA
(c)CSO
(d)Compiler error

ADVANCED C -PROGRAMMING Page 19


Q30. What will be the output of following C Program?________
#include <stdio.h>

void main()

{
static int m[][4] = { {10,5,-3}, {9, 0, 0}, {32,20,1}, {0,0,8} };
int row, column, sum;
sum = 0;
for( row = 0; row < 4; row++ )
for( column = 0; column < 3; column++ )
sum = sum + m[row][column];
printf("%d", sum );

}
Q31. What will be the output of following code?
#include <stdio.h>

int main(){

int arr[] = { 9, 8, 98, 88, 87, 1, 2, 4, 101, 102, 103, 105 };


int *x = arr+4;
int *ptr = &arr[7];

arr[*ptr] ++;

printf( "First value: %d\n", *ptr );


printf( "Second value: %d\n", *x );
*x = 7;
printf( "Third value: %d\n", arr[4] + *ptr);

return(0);
}
(a) (b) (c) (d)
First value: 4 First value: 4 First value: 4 First value: 4
Second value: 88 Second value: 87 Second value: 88 Second value: 87
Third value: 12 Third value: 91 Third value: 11 Third value: 11

ADVANCED C -PROGRAMMING Page 20


Q32. What will be the output of given code?
union student
{
char name[10];
char subject[10];
float percentage;
}marksheet;

int main()
{

strcpy(marksheet.name, "RAJ");
strcpy(marksheet.subject, "Maths");
marksheet.percentage = 78.50;

printf(" Name : %s \n", marksheet.name);


printf(" Subject : %s \n", marksheet.subject);
printf(" Percentage : %f \n", marksheet.percentage);
return 0;
}
(a) (b)
Name :RAJ Name :
Subject :MATHS Subject :MATHS
Percentage : 78.500000 Percentage:78.500000

(c) (d)
Name :RAJ Name :
Subject : Subject :
Percentage:78.500000 Percentage: 78.500000

Q33. Void main()


{
static 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);
*ptr++;

ADVANCED C -PROGRAMMING Page 21


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);
}
Write the output in sequence ______
Q34. What is the output of given program
#include<stdio.h>
#define VAR_SIZE 3
int main()
{
char *arr[VAR_SIZE + ~0] = { "brucelee", "bluesky" };
char *pchr = arr[1 + ~0];
printf("%s", ++pchr);
return 0;
}
(a)brucelee (b) luesky
(c) rucelee (d) bluesky
Q35. What will be the output of given program?
int main()
{
int x[100];
int *ai, *cpi = &x[98];
ai = x;
if((cpi - ai) != 98)
printf("Error1");
ai = cpi;
ai++;
if((ai - cpi) != 1)
printf("Error2\n");
}
(a) Error1 (b) Error2
(c) Error1Error2 (d) None of these

ADVANCED C -PROGRAMMING Page 22


Q36. What will be the output of given program?
#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={
{"Zeal",10},{"210 c Block indore",100}
};
ptr=&aa;
printf("%s %s %d %d\n",ptr->bb.name,ptr->cc.address,ptr->bb.age,ptr-
>cc.sal);
return 0;
}
(a)Compilation Error
(b)Zeal c Block indore 100
(c) 210 c Block indore 10 100
(d)Zeal 210 c Block indore 10 100

ADVANCED C -PROGRAMMING Page 23


Q37. 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[]=
{
{"Bhopal",1,a+1},{"India",2,a+2},{"Canada",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)hopal Canada Canada
(b)India Bhopal Canada
(c)Bhopal Canada Canada
(d)hopal India Canada
Q38. What will be the output of following C program?
#include<stdio.h>
#include<string.h>
struct player
{
char pname[20];
}pl;
char* play(struct player *temp_pl)
{
strcpy(temp_pl->pname, "cleanest city");
return temp_pl->pname;
}

ADVANCED C -PROGRAMMING Page 24


int main()
{
strcpy(pl.pname, "Indore");
printf("%s %s", pl.pname, play(&pl));
return 0;
}
(a) Cleanest city cleanest city (b) Cleanest city Indore
(c) Indore cleanest city (d)Compiler Error
Q39. What will be the output of the following program:
#include <stdio.h>
int main()
{
static int a[3][3][3]=
{
{
1,2,3,
4,5,6,
7,8,9
},
{
2,4,6,
8,10,12,
13,14,16
},
{
4,8,12,
16,20,24,
28,32,36
}
};
static int *ptr[]=
{

ADVANCED C -PROGRAMMING Page 25


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],a[1],a[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)1 2 4
1 4 7 2 8
(b) 2 4 6
2 5 8 4 10
(c) 3 6 9
6 12 16 12 24
(d)None of these.
Q40. What will be the output of the given c program_________
#include <stdio.h>
int main()
{
int a[3][3][2]={
{
1,2,
3,4,
5,6
},
{
3,4,
1,2,
5,6
},

ADVANCED C -PROGRAMMING Page 26


{
5,6,
3,4,
1,2
}
};
printf("%d%d%d\n",*(*(a[0]+1)+2),*(*(*(a+2)+1)+1),*(a[1][2]+1));

return 0;
}
Q41. What will be the output of given code?

#include <stdio.h>
void f(char**);
int main()
{
char *arg[] = { "ab", "cd", "ef", "gh", "ij", "kl" };
f(arg);
return 0;
}
void f(char **p)
{
char *t;
t = (p += sizeof(int))[-1];
printf("%s\n", t);
}
(a)ab (b)cd
(c)ef (d)gh
Q42. What will be the output of given C code?
#include<stdio.h>

int main()
{
int arr[3][4] = {
{11,22,33,44},
{55,66,77,88},
{11,66,77,44}
};
int i, j;
for(i = 0; i < 2; i++)

ADVANCED C -PROGRAMMING Page 27


{
for(j = 0; j < 3; j++)
{
printf("arr[%d][%d]=%d\n", i, j, *( *(arr + i) + j) );
}
printf("\n\n");
}
return 0;
}
(a) (b) (c) (d)
arr[0][0]=11 arr[0][0]=11 arr[0][0]=11 None of these.

arr[0][1]=22 arr[0][1]=22 arr[0][1]=22


arr[0][2]=33 arr[0][2]=33 arr[0][2]=33

arr[1][0]=44 arr[1][0]=55 arr[1][0]=11


arr[1][1]=55 arr[1][1]=66 arr[1][1]=22
arr[1][2]=66 arr[1][2]=77 arr[1][2]=33

Q43. Find out the output of given code?


#include <stdio.h>

int main(void) {
int num[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

Int ROWS = 3,COLS = 4,i, j;


int *ptr = &num[0][0];
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%d ", *(ptr + i * COLS + j));
}
printf("\n");
}
return 0;
}

ADVANCED C -PROGRAMMING Page 28


(a) (b) (c) (d)
1234 12345 1592 Compilation
5678 5 6 8 9 10 6 10 3 7 Error
9 10 11 12 9 10 11 12 11 4 8 12

Q44. What is the length of sting printed by the following c program?_______


#include <stdio.h>
#define FALSE 0
#define TRUE 1
int nCount = 0,i=0, nBlank, count = 5;
int main()
{
char *pS = "GATE 21"+1;
while(count = count - nCount)
{

if (*(pS) && *(pS) != ' ')


{
if (++i)
{
++nCount;
if(nCount==5)
break;
}
nBlank = FALSE;
printf("%d",nBlank);
}
else
{
++nCount;
nBlank = TRUE;
printf("%d\n",nBlank);
}
}
return 0;
}

ADVANCED C -PROGRAMMING Page 29


Q45. Consider the C program shown below.
#include <stdio.h>
#define print(x) printf("%d ", x)
int x=1;
void Q(int z) {
z += x; print(z);
}
void P(int *y) {
int x = *y+2;
Q(x); *y = x-2;
print(x);
}
main(void) {
x = 6;
P(&x);
print(x);
}
The output of this program is
(a)12 7 6 (b)14 8 6
(c)16 6 6 (d)18 8 6
Q46. What will be the output of the given c program
#include<stdio.h>
void xray1();
void xray2(void (*ptr)());
int main()
{
xray2(xray1);
return 0;
}
void xray2(void (*ptr)())
{
printf("xray2 is called");

ADVANCED C -PROGRAMMING Page 30


(*ptr)();
}
void xray1()
{
printf("\nxray1 is called");
}
(a) (b) (c) (d)
xray2 is called xray1 is called Compilation Run Time Error
xray1 is called xray2 is called Error

Q47. Consider the following C code:


void abc(char *s)
{
if(s[0]=='\0') return;
abc(s+1);
abc(s+1);
printf("%c ",s[0]);
}
main()
{
abc("123");
}
What will be the output of this code:
(a)3 3 2 3 3 2 1 (b)3 2 1 3 2 1
(c)3 1 2 3 2 1 (d) 3 2 1 1 2 3
Q48. What will be the output of following C Program?
#include <stdio.h>
#include<stdlib.h>
int main()
{
int i;

ADVANCED C -PROGRAMMING Page 31


int *ptr= (int *) malloc(5 * sizeof(int));
for (i=0; i<5; i++)
*(ptr + i) = i;
printf("%d ", *ptr++);
printf ("%d", (*ptr)++);
printf("%d ", *ptr);
printf("%d ", *++ptr);
printf("%d", ++*ptr);
return 0;
}
(a)1 1 2 4 3 (b)0 2 1 2 3
(c)0 1 2 2 3 (d)0 1 2 4 3
Q49. Consider the following C code:
#include<stdio.h>
#include<conio.h>
double getvalue(int *arr, int size);
int main ()
{
int balance[5] = {10, 20, 30, 40, 50};
double val;
val = getvalue( balance, 5 ) ;
printf("%f\t", val );
return 0;
}
double getvalue(int *arr, int size)
{
int i, sum = 0;
double val;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}

ADVANCED C -PROGRAMMING Page 32


val = (double)sum / size;
return val;
What will be the output of above code:
}
(a)10 20 30 40 50
(b)50.000000
(c)30.000000
(d)Compilation error
Q50. What will be the output of following C Program?
#include<stdio.h>
#include<conio.h>
void main()
{
struct India
{
int a;
char b;
struct indore
{
char c;
float d;
}p;
};
struct India st ={1,'A','i',1.8};
printf("%d\t%c\t%c\t%f",st.a,st.b,st.c,st.d);
getch();
}
(a)1 A i 1.800000
(b)1 A
(c)1 A i 1.8
(d)Compilation Error.

ADVANCED C -PROGRAMMING Page 33


Q51. What will be the output of following C Program?
#include <stdio.h>
#include <string.h>
struct tag
{
char lname[20];
char fname[20];
int age;
float rate;
};
struct tag my_struct;
void show_name(struct tag *p);
int main(void)
{
struct tag *st_ptr;
st_ptr = &my_struct;
strcpy(my_struct.lname,"johny");
strcpy(my_struct.fname,"johny");
printf("%s ",my_struct.fname);
printf("%s\n",my_struct.lname);
my_struct.age = 63;
show_name(st_ptr);
return 0;
}
void show_name(struct tag *p)
{
printf("\n%s ", p->fname);
printf("%s ", p->lname);
printf("%d\n", p->age);
}
(a)Compilation error (b)Runtime error
(c) johny johny (d)johny johny 63

ADVANCED C -PROGRAMMING Page 34


Q52. What will be the output of following C Program?
#include<stdio.h>
int main()
{
static char*a[]={
"so","how","are","you"
};
static char **ptr[]={a+3,a+2,a+1,a};
char ***p=ptr;
printf("%s\n", **++p);
printf("%s\n", *--*++p+3);
printf("%s\n", *p[-2]+3);
printf("%s\n", p[-1][-1]+1);
return 0;
}
(a) (b) (c) (d)
are are how are
how how are how
you you you
how how how

Q53. What will be the output of following C Program?________


#include <stdio.h>
int counter = 0;
int calc (int a, int b)
{
int c;
counter= counter+1;
if (b == 3)
return (a*a*a);
else

ADVANCED C -PROGRAMMING Page 35


{
c=calc(a, b/3);
return(c*c*c);
}
}
void main()
{ calc(4,81);
printf("%d", counter);
}

Q54. What will be the output of following C Program?


#include<stdio.h>
#include<string.h>
int main()
{
char a[]="hello how are era woh olleh";
char *t,*s,*b;
s=a;
b=a + strlen(a)-1;
t=b;
while(s!=t)
{
printf("%c",*s);
s++;
printf("%c",*t);
t--;
}
return 0;
}
(a) (b)
hueolyl oe rhao Hyleol oe ryou
(c) (d)
hheelllloo hhooww aarree Hello how are you

ADVANCED C -PROGRAMMING Page 36


Q55. Consider the following C program.
#include<stdio.h>
int main()
{
char arr[5][7][6];
char (*p)[5][7][6] = &arr;
printf("%d\n", (&arr + 1) - &arr);
printf("%d\n", (char *)(&arr + 1) - (char *)&arr);
printf("%d\n", (unsigned)(arr + 1) - (unsigned)arr);
printf("%d\n", (unsigned)(p + 1) - (unsigned)p);
return 0;
}
The sum of all the values printed by above program is___________
Q56. What the will the following C program print
(Assume size of int, float & double are 4,4,8 byte respectively )
#include <stdio.h>
int main()
{
int array[10] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int *iptr = (int *)&array;
void *vptr = &array;
iptr = iptr + 8;
vptr = ((char *)vptr) + 8;
printf("%d ", *iptr);
printf("%d ", *(int *)vptr);
iptr = (int *) (((double *)iptr) - 2);
vptr = ((int *)vptr) - 2;
printf("%d ", *iptr);
printf("%d", *(int *)vptr);

}
The output of the program is
(a) 18 12 15 11 (b) 17 12 14 10
(c) 18 12 14 10 (d) 18 13 16 11

ADVANCED C -PROGRAMMING Page 37


Q57. In the context of the below program snippet, pick the best answer.
#include<stdio.h>
int arr[10][10][10];
int main()
{
arr[5][5][5] = 123;
printf("%d",arr[5][5][5]); // print 1
printf("%d",*(*(*(arr+5)+5)+5)); // print 2
printf("%d",(*(*(arr+5)+5))[5]); // print 3
printf("%d",*((*(arr+5))[5]+5)); // print 4
return 0;
}
Which of the given printf statement(s) would be able to print arr[5][5][5]?
(a) both (1) and (2) would compile and both would print 123.
(b) only (1), (2) and (3) would compile and all three would print 123.
(c) all (1), (2), (3) and (4) would compile but only (1) and (2) would print 123.
(d) all (1), (2), (3) and (4) would compile and all would print 123.

Q58. #include <stdio.h>


void traverseArr(int* arr, int N, int M)
{
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
printf("%d ", *((arr + i * M) + j));
}
printf("\n");
}
}
int main()
{
int N = 3, M = 2;
int arr[][2] = { { 2, 4 },{ 5, 6 },{ 7, 9 } };
traverseArr((int*)arr, N, M);
return 0;
}

ADVANCED C -PROGRAMMING Page 38


(a) (b) (c) (d)
24 25 24 None of these.
56 74 65
79 69 97

Q59. Consider the following c program:


#include <stdio.h>
#include <string.h>
#include <limits.h>
#define TRUE 1
#define FALSE 0
int sum(int * Result, int , int );
int main()
{
int nFirst = 44500;
int nSecond = 600;
int nSum = 0;
if (!sum(&nSum, nFirst, nSecond))
{
printf("%d ",nSum);
}
printf("%4d",nSum);
return (0);
}
int sum(int * nResult, int nFirstValue, int nSecondValue)
{
if ((long)nFirstValue + (long)nSecondValue > (long)INT_MAX)
{
return(FALSE);
}
else
{
*nResult = nFirstValue + nSecondValue;
}
return(TRUE);
}
//What is the value printed by the program? ______

ADVANCED C -PROGRAMMING Page 39


Q60. What is the length of sting printed by the following c program?___ _____
#include <stdio.h>
#define FALSE 0
#define TRUE 1
int nCount = 1,i=0, nBlank, count = 4;
int main()
{
char *pS = "GATE Glory"+1;
while(count = count - nCount)
{

if (*(pS) && *(pS) != ' ')


{
if (++i)
{
++nCount;

if(nCount==5)
break;
}
nBlank = FALSE;
printf("%d", nCount);
}
else
{
++nCount;
nBlank = TRUE;
printf("%d\n", nCount);
//printf("%d\n", nCount);
}
}
return 0;
}
Q61. Find the output of given code?
#include<stdio.h>
void main ()
{
int x, y, z, p, q, r;
x=5,y=6,z=3,p=2,q=1,r=4;
p=(x<y)?(y<9)?(z>>2):(p>q)?r<<2:z:r;
r=(x<y)?(y>6)?(z<<2):(p>q)?r<<2:y^r:z;
printf("%d %d", r, p);

}
(a)0 2 (b)2 0
(c)2 4 (d)None of these.

ADVANCED C -PROGRAMMING Page 40


Q62. Consider the following C code:
#include <stdio.h>
int main( void )
{
int i=3;
printf( "%d\n", (--i + 3) );
printf( "%d\n", (i++ + 10) );
printf( "%d\n", ++i );
i += i;
printf( "%d\n", i-- );
printf( "%d\n", i );
return 0;
}
What will be the sum of value printed by above printf___________

Q63. Write the output from the following program:


#include <stdio.h>
int main() {
int w = 0, x = 2.5, y = 5, z = 3, r, s = 4, t = 5, u = -3;
double a = 2.36, b = 3.19, c = 3.0, d = 2.91726;
printf("Expr_1 = %d\n", (int)(c * y / z + y / z * c));
printf("Expr_2 = %lf\n", x - s * t * - c - u);
printf("Expr_3 = %f\n",(float)(x + y < z + w && a > b - 17 * x || ! x < 5));
return 0;
}
(a) (b) (c) (d)
Expr_1 = 8 Expr_1 = 16 Expr_1 = 8 None of these
Expr_2 = 75.000000 Expr_2 = 65.120000 Expr_2 = 65.000000
Expr_3 = 1.124000 Expr_3 = 6.000000 Expr_3 = 1.000000
Q64. What will be the output of following C program?
#include <stdio.h>
void f (int x, int * p)
{
*p = x;
x = 30;
}
int main(void)
{
int a = 4;
int b = 2;
int * p = &a;
ADVANCED C -PROGRAMMING Page 41
*p = 50;
printf("1: a = %d, b=%d\n", a, b);
f(a,&b);
printf("2: a = %d, b=%d\n", a, b);
int ** q = & p;
*q = & b;
*p = 42;
printf("3: a = %d, b=%d\n", a, b);
return 0;
}
(a) (b) (c) (d)
1: a = 50, b=2 1: a = 50, b=2 1: a = 50, b=2 None of these.
2: a = 50, b=2 2: a = 50, b=50 2: a = 50, b=50
3: a = 50, b=42 3: a = 50, b=42 3: a = 50, b=2

Q65. What will be output of following program?(Assume size of int=2 byte )


void main()
{
int a[2][3][2]={{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
/*Base address of a=65502*/
printf(“%u%u%u%d\n”,a,*a,**a,***a);
printf(“%u%u%u%d\n”,a+1,*a+1,**a+1,***a+1);
}
(A)65502 65502 65502 2 (B) 65502 65502 65502 2
65514 65506 65504 3 65512 65506 65504 3
(C)65502 65502 65502 2 (D) 65502 65502 65502 2
65514 65514 65514 2 65512 65512 65512 3

Q66. What will be the output of following program?


#include<stdio.h>
#include<stdlib.h>

Void populate(int*a)
{ int*parray=malloc(2*sizeof(int));
parray[0]=37;
parray[1]=73;
a=parray;
}
int main()
{ int*a=NULL;
populate(a);
ADVANCED C -PROGRAMMING Page 42
printf("a[0]=%d and a[1]=%d\n", a[0],a[1]);
return0;
}
(a) a[0]=37 and a[1]=73 (b)0 0
(c) Segmentation fault. (d)None of these.

Q67. What will be the output of following code?


#include<stdio.h>
int main(){
char string[]="goldrun";
char*ptr=string;
*ptr=*ptr+2;
ptr=ptr+2;
printf("%c",*ptr);
ptr--;
printf("%c",*ptr);
ptr=string;
printf("%c",*ptr);
return 0;
}
(a)dlo (b)loi
(c)lor (d)ldr

Q68. What is the size (in bytes) for the following structures on a 32bit machine?
(Assume) sizeof (int) = 4 bytes, sizeof (short) = 2 bytes, sizeof (char) = 1 byte.
Structure Size in byte
Structfoo1
{ intd1; __________
charc1;
intd2; };
Structfoo2
{ intd1;
charc1; __________
intd2;
charc2;
shorts;
};
Structfoo3
{ intd1;
intd2;
charc1; __________

ADVANCED C -PROGRAMMING Page 43


charc2;
shorts; };
Structfoo4
{ charc1;
intd1;
shorts; __________
intd2;
charc2; };
Write the size of structures.
Size of Structfoo1?______, Size of Structfoo2?______,
Size of Structfoo3?______,Size of Structfoo4?_______.

Q69. What will be the output of following C program?


#include <stdio.h>
void f(int* a, int b)
{
int k;
for (k=3;k>=1;k--)
{
a[k]--;
b=a[k];
}
}
int main()
{
int x[6]={1,2,3,4,5,6};
int* y, z, i;
y=x+2;
z=*y-2;
f(x,z);
*y=*y+5;
for (i=0;i<=5;i++)
printf("%d ",x[i]);
printf("%d %d\n",*y, z);
return 0;
}
(a)1 1 2 3 4 5 6 1 (b)1 2 3 4 5 6 7 8
(c)1 1 7 3 5 6 3 1 (d)1 1 7 3 5 6 7 1

ADVANCED C -PROGRAMMING Page 44


Q70. Consider following C code below:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *t = (int *) malloc(sizeof(int) + sizeof(int));
t++;
*t = 8;
t[-1] = *t / 2;
t--;
t[1] = *t / 2;
printf("%d\n",*t);
free(t);
return 0;
}
What will be the output of above code?_________
Q71. What will be the output of following code given below?_________
#include <stdio.h>
struct S {
int Var;
struct S *Str;
};
int main(void)
{
struct S S[] = { { 8, NULL }, { 4, &S[0] }, { 2, &S[1] } };
printf("%d", S[2].Str->Str->Var);
return 0;
}
Q72. What will be the out of given C code:
#include <stdio.h>
char *f(char *p) {
return p++;
}
char *g(char *p) {
return p += 2;
}
int main(void) {
char *s = "ABCDEFGHIJ";
char p = *f(g(f(s + 6)));
printf("%d", p - 'A');
return 0;
}
(a)6 (b)7 (c)8 (d)9

ADVANCED C -PROGRAMMING Page 45


Q73. What will be the output of following code?
#include <stdio.h>
typedef int *IntPtrType;
int main()
{
IntPtrType ptr_a, ptr_b;
int num_c = 4, num_d = 7;
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)
44 44 44 44
47 47 44 77
74 77 74 77
77 77 77 77

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


#include<stdio.h>
int main()
{
char blocks[3] = {'A','B','C'};
char *ptr = &blocks[0];
char temp;

temp = blocks[0];
temp = *(blocks + 2);
temp = *(ptr + 1);
temp = *ptr;

ptr = blocks + 1;
temp = *ptr;
temp = *(ptr + 1);

ptr = blocks;

ADVANCED C -PROGRAMMING Page 46


temp = *++ptr;

temp = ++*ptr;
temp = *ptr++;
temp = *ptr;
printf("%c %d", temp,*ptr);
return 0;
}
(a)D 68 (b)C 66
(c)C 67 (d)Garbage value.
Q75. What will be output of following C code?____________
#include <stdio.h>
int main()
{
int arr_size=5,i,ans=0,sum=0,leftsum=0 ;
int arr[7]= {-7, 1, 5, 2, -4,3,0};
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;
}

ADVANCED C -PROGRAMMING Page 47

You might also like