C programming Advanced sheet _2022
C programming Advanced sheet _2022
#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
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);
}
int main()
{
printf("%s ", **++cpp);
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’
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);
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.
}
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
void main()
{
int c, *b, **a;
c = 4;
b = &c;
a = &b;
printf( "%d", z(c, b, a));
}
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(){
arr[*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
int main()
{
strcpy(marksheet.name, "RAJ");
strcpy(marksheet.subject, "Maths");
marksheet.percentage = 78.50;
(c) (d)
Name :RAJ Name :
Subject : Subject :
Percentage:78.500000 Percentage: 78.500000
};
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
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++)
int main(void) {
int num[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
}
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
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.
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.
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; __________
temp = blocks[0];
temp = *(blocks + 2);
temp = *(ptr + 1);
temp = *ptr;
ptr = blocks + 1;
temp = *ptr;
temp = *(ptr + 1);
ptr = blocks;
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;
}