சனி, 20 பிப்ரவரி, 2010

C Questions

************************************************************************
* NOTE : All the programs are tested under Turbo C/C++ compilers.
It is assumed that, *=> Programs run under DOS environment,
*=> The underlying machine is an x86 system,
*=> Necessary header files are included.
*=> Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions.
(For example sizeof(int) = 2 bytes may be assumed). *****************************************************************

[Q001]. Determine which of the following are VALID identifiers. If invalid, state the reason.
(a) sample1 (b) 5sample (c) data_7 (d) return (e) #fine (f) variable (g) 91-080-100 (h) name & age (i) _val (j) name_and_age
Ans. (a) VALID
(b) Invalid, since an identifier must begin with a letter or an underscore
(c) VALID
(d) Invalid, since return is a reserved word
(e) Invalid, since an identifier must begin with a letter or an underscore
(f) VALID
(g) Invalid, since an identifier must begin with a letter or an underscore
(h) Invalid, since blank spaces are not allowed
(i) VALID (j) VALID

[Q002]. Determine which of the following are VALID character constants. If invalid, state the reason.
(a) 'y' (b) '\r' (c) 'Y' (d) '@' (e) '/r' (f) 'word' (g) '\0' (h) '\?' (i) '\065' (j) '\'' (k) ' '
Ans. (a) VALID (b) VALID
(c) VALID (d) VALID
(e) Invalid, since escape sequences must be written with a backward slash (i.e. \)
(f) Invalid, since a character constant cannot consist of multiple characters
(g) VALID (null-character escape sequence) (h) VALID
(i) VALID (Octal escape sequence)
(j) VALID (k) VALID


[Q003]. Determine which of the following are VALID string constants. If invalid, state the reason.
(a) 'Hi Friends' (b)"abc,def,ghi" (c) Qualification (d) "4325.76e-8" (e) "Don\'t sleep" (f) "He said, "You\'re great" (g) "" (h) " " (i) "Rs.100/-"
Ans. (a) Invalid, since a string constant must be enclosed in double quotation marks
(b) VALID
(c) Invalid, since trailing quotation mark is missing (d) VALID
(e) VALID (single-quote escape sequence)
(f) Invalid, since the quotation marks and (optionally) apostrophe within the string
cannot be expressed without the escape sequences.
(g) VALID (h) VALID (i) VALID

[Q004]. Determine which of the following numerical values are valid constants. If a constant is valid, specify whether it is integer or real. Also, specify the base for each valid integer constant.
(a) 10,500 (b) 080 (c) 0.007 (d) 5.6e7 (e) 5.6e-7 (f) 0.2e-0.3 (g) 0.2e 0.3 (h) 0xaf9s82 (i) 0XABCDEFL (j) 0369CF
(k) 87654321l (l) 87654321
Ans. (a) Invalid, since illegal character(,)
(b) VALID (c) VALID (d) VALID
(e) VALID (f) VALID (g) Invalid, since illegal character(blank space) (h) Invalid, since illegal character(s) (i) VALID (j) Invalid, since illegal characters (9, C, F), if intended as an octal constant. (k) VALID (l) VALID

[Q005]. Determine which of the following floating-point constants are VALID for the quantity (5 * 100000).
(a) 500000 (b) 0.5e6 (c) 5E5 (d) 5e5 (e) 5e+5
(f) 500E3 (g) .5E6 (h) 50e4 (i) 50.E+4 (j) 5.0E+5
(k) All of the above (l) None of these
Ans. (k)

[Q006]. What will be the output of the following program :
void main()
{
printf("%f",123.);
}
(a)123 (b)Compile-Time Error (c)123.00 (d)123.000000
Ans. (d)

[Q007]. What will be the output of the following program :
void main()
{
printf("%d",sizeof(integer));
}
(a)2 (b)Com.-Time Error (c)4 (d)6
Ans. (b) since there is no such data type called 'integer'.

[Q008]. What will be the output of the following program :
void main( )
{
char str[]="C For Swimmers";
printf("%d",sizeof str);
}
(a)14 (b)Compile-Time Error (c)15 (d)None of these
Ans. (b)

[Q009]. What will be the output of the following program :
void main()
{
char str[]="C For Swimmers";
printf("%d",++(sizeof(str)));
}
(a)14 (b)Compile-Time Error (c)15 (d)None of these
Ans. (b)

[Q010]. What will be the output of the following program :
void main()
{
char str[]="C For Swimmers";
printf("%d",-sizeof(str));
}
(a)14 (b)Compile-Time Error (c)-15 (d)-14
Ans. (d)

[Q011]. What will be the output of the following program :
void main()
{
printf("%d",!(100==100)+1);
}
(a)100 (b)0 (c)1 (d)2
Ans. (c)

[Q012]. What will be the output of the following program :
void main()
{
int x=5,y=6,z=2;
z/=y/z==3?y/z:x*y;
printf("%d",z);
}
(a)Compile-Time Error(b)2 (c)0(d)1
Ans. (c)

[Q013]. What will be the output of the following program :
void main()
{
printf("%d %d %d",5,!5,25-!25);
}
(a)5 10 22 (b)5 5 25 (c)5 0 25 (d)5 1 24
Ans. (c)

[Q014]. What will be the output of the following program :
int main()
{
int a=500,b=100,c=30,d=40,e=19;
a+=b-=c*=d/=e%=5;
printf("%d %d %d %d %d",a,b,c,d,e);
}
(a)500 100 30 40 4 (b)Run-Time Error
(c)700 200 300 10 4 (d)300 -200 300 10 4
Ans. (d)

[Q015]. What will be the output of the following program :
void main()
{
int a=500,b=100,c=30,d=40,e=19;
if ((((a > b) ? c : d) >= e) && !((e <= d) ? ((a / 5) == b) : (c == d)))
printf("Success");
else
printf("Failure");
}
(a)VALID : Success (b)VALID : Failure (c)INVALID (d)None of these
Ans. (b)

[Q016]. What will be the output of the following program :
void main()
{
int a=1,b=2,c=3,d=4;
printf("%d",!a?b?!c:!d:a);
}
(a)1 (b)2 (c)3 (d)4
Ans. (a)

[Q017]. What will be the output of the following program :
void main()
{
int i=12345,j=-13579,k=-24680;
long ix=123456789;
short sx=-2222;
unsigned ux=5555;
printf("\n%d %d %d %ld %d %u",i,j,k,ix,sx,ux);
printf("\n\n%3d %3d %3d\n%3ld %3d %3u",i,j,k,ix,sx,ux);
printf("\n\n%8d %8d %8d\n%15ld %8d %8u",i,j,k,ix,sx,ux);
printf("\n\n%-8d %-8d\n%-8d %-15ld\n%-8d %-8u",i,j,k,ix,sx,ux);
printf("\n\n%+8d %+8d\n%+8d %+15ld\n%+8d %8u",i,j,k,ix,sx,ux);
printf("\n\n%08d %08d\n%08d %015ld\n%08d %08u",i,j,k,ix,sx,ux);
}
Ans. 12345 -13579 -24680 123456789 -2222 5555

12345 -13579 -24680
123456789 -2222 5555

12345 -13579 -24680
123456789 -2222 5555

12345 -13579
-24680 123456789
-2222 5555

+12345 -13579
-24680 +123456789
-2222 5555

00012345 -0013579
-0024680 000000123456789
-0002222 00005555

[Q018]. What will be the output of the following program :
void main()
{
int i=12345,j=0xabcd9,k=077777;
printf("%d %x %o",i,j,k);
printf("\n%3d %3x %3o",i,j,k);
printf("\n%8d %8x %8o"i,j,k);
printf("\n%-8d %-8x %-8o",i,j,k);
printf("\n%+8d %+8x %+8o",i,j,k);
printf("\n%08d %#8x %#8o",i,j,k);
}
Ans. 12345 abcd9 77777
12345 abcd9 77777
12345 abcd9 77777
12345 abcd9 77777
+12345 abcd9 77777
00012345 0xabcd9 077777

[Q019]. What will be the output of the following program :
void main()
{
char c1='A', c2='B', c3='C';
printf("%c %c %c",c1,c2,c3);
printf("\n%c%c%c",c1,c2,c3);
printf("\n%3c %3c %3c",c1,c2,c3);
printf("\n%3c%3c%3c",c1,c2,c3);
printf("\nc1=%c c2=%c c3=%c",c1,c2,c3);
}
Ans. A B C
ABC
A B C
A B C
c1=A c2=B c3=C

[Q020]. What will be the output of the following program :
void main()
{
float a=2.5, b=0.0005, c=3000.;
printf("%f %f %f",a,b,c);
printf("\n%3f %3f %3f",a,b,c);
printf("\n%8f %8f %8f",a,b,c);
printf("\n%8.4f %8.4f %8.4f",a,b,c);
printf("\n%8.3f %8.3f %8.3f",a,b,c);
printf("\n%e %e %e",a,b,c);
printf("\n%3e %3e %3e",a,b,c);
printf("\n%12e %12e %12e",a,b,c);
printf("\n%8.2e %8.2e %8.2e",a,b,c);
printf("\n%-8f %-8f %-8f",a,b,c);
printf("\n%+8f %+8f %+8f",a,b,c);
printf("\n%08f %08f %08f",a,b,c);
printf("\n%#8f %#8f %#8f",a,b,c);
printf("\n%g %g %g",a,b,c);
printf("\n%#g %#g %#g"a,b,c);
}

Ans. 2.500000 0.000500 3000.000000
2.500000 0.000500 3000.000000
2.500000 0.000500 3000.000000
2.5000 0.0005 3000.0000
2.500 0.001 3000.000
2.500000e+000 5.000000e-004 3.000000e+003
2.500000e+000 5.000000e-004 3.000000e+003
2.500000e+000 5.000000e-004 3.000000e+003
2.5000e+000 5.0000e-004 3.0000e+003
2.50e+000 5.00e-004 3.00e+003
2.500000 0.000500 3000.000000
+2.500000 +0.000500 +3000.000000
2.500000 0.000500 3000.000000
2.500000 0.000500 3000.000000
2.5 0.0005 3000
2.500000 0.000500 3000.000000

[Q021]. What will be the output of the following program :
void main()
{
char str[]="C For Swimmers";
printf("%s",str);
printf("\n%.5s",str);
printf("\n%8.*s",5,str);
printf("\n%-10s %.1s",str+6,str);
}
Ans. C For Swimmers
C For
C For
Swimmers C


[Q022]. What will be the output of the following program :
void main()
{
int a=1,b=2,c=3;
scanf("%d %*d %d",&a,&b,&c);
printf("a=%d b=%d c=%d",a,b,c);
}
[NOTE : 3 values entered by the user are:100 200 300]
(a)1 2 3 (b)100 200 300 (c)100 200 3 (d)100 300 3
Ans. (d)

[Q023]. What will be the output of the following program :
void main()
{
char line[80]; // Max. length=80 Chars
scanf("%[^,]s",line);
printf("\n%s",line);
}
[NOTE : THE USER INPUT IS:Dear Friends, What is the output?]
(a)Compile-Time Error (b)Dear Friends (c)What is the output? (d)None of these
Ans. (b)

[Q024]. What will be the output of the following program :
void main()
{
char a,b,c;
scanf("%c%c%c",&a,&b,&c);
printf("a=%c b=%c c=%c",a,b,c);
}
[NOTE : THE USER INPUT IS :A B C]
(a)a=A b=B c=C (b)a=A b= c=B (c)a=A b= c=C (d)None of these
Ans. (b)

[Q025]. What will be the output of the following program :
void main()
{
int i=1;
float f=2.25;
scanf("%d a %f",&i,&f);
printf("%d %.2f",i,f);
}
[NOTE : THE USER INPUT IS:5 5.75]
(a)1 2.25 (b)5 5.75 (c)5 2.25 (d)None of these
Ans. (c)
[Q026]. What will be the output of the following program :
void main()
{
char a,b,c;
scanf("%c %c %c",&a,&b,&c);
printf("a=%c b=%c c=%c",a,b,c);
}
[NOTE : THE USER INPUT IS :ABC DEF GHI]
(a)a=ABC b=DEF c=GHI (b)a=A b=B c=C (c)a=A b=D c=G (d)None of these
Ans. (b)

[Q027]. What will be the output of the following program :
void main()
{
char a[80],b[80],c[80];
scanf("%1s %5s %3s",a,b,c);
printf("%s %s %s",a,b,c);
}
[NOTE : THE USER INPUT IS:CMeansSea Ocean Vast]
(a)C O V (b)C Means Sea (c)C Ocean Vas (d)None of these
Ans. (b)

[Q028]. What will be the output of the following program :
void main()
{
int a,b,c;
scanf("%1d %2d %3d",&a,&b,&c);
printf("Sum=%d",a+b+c);
}
[NOTE : THE USER INPUT IS :123456 44 544]
(a)Sum=480 (b)Sum=594 (c)Sum=589 (d)None of these
Ans. (a)

[Q029]. What happens when the following program is executed :
void main()
{
char line[80];
scanf("%[^1234567890\n]",line);
}
(a)Accepts the string that contains DIGITS only.
(b)Accepts the string that contains DIGITS and NEWLINE characters.
(c)Accepts the string that contains anything other than the DIGITS and NEWLINE characters.
(d)None of these
Ans. (c)

[Q030]. What happens when the following program is executed :
void main()
{
char line[80];
scanf("%[^*]",line);
}
(a)Accepts the string that contains DIGITS & ALPHABETS only.
(b)Accepts the string that contains * or asterisk characters only.
(c)Accepts the string that contains anything other than the * or asterisk character.
(d)None of these
Ans. (c)

[Q001]. What will be the output of the following program :
void main()
{
printf("Hi!");
if (-1)
printf("Bye");
}
(a)No Output (b)Hi! (c)Bye (d)Hi!Bye
Ans. (d)


[Q002]. What will be the output of the following program :
void main()
{
printf("Hi!");
if (0 || -1)
printf("Bye");
}
(a)No Output(b)Hi! (c)Bye (d)Hi!Bye
Ans. (d)

[Q003]. What will be the output of the following program :
void main()
{
printf("Hi!");
if (!1)
printf("Bye");
}
(a)Compile-Time error (b)Hi! (c)Bye (d)Hi!Bye
Ans. (b)

[Q004]. What will be the output of the following program :
void main()
{
printf("Hi!");
if !(0)
printf("Bye");
}
(a)Compile-Time error (b)Hi! (c)Bye (d)Hi!Bye
Ans. (a)

[Q005]. What will be the output of the following program :
void main()
{
printf("Hi!");
if (-1+1+1+1-1-1-1+(-1)-(-1))
printf("Bye");
}
(a)No Output (b)Hi! (c)Bye (d)Hi!Bye
Ans. (d)

[Q006]. What will be the output of the following program :
void main()
{
if (sizeof(int) && sizeof(float) && sizeof(float)/2-sizeof(int))
printf("Testing");
printf("OK");
}
(a)No Output (b)OK (c)Testing (d)TestingOK
Ans. (b)

[Q007]. What will be the output of the following program :
void main()
{
int a=1,b=2,c=3,d=4,e;
if (e=(a & b | c ^ d))
printf("%d",e);
}
(a)0 (b)7 (c)3 (d)No Output
Ans. (b)

[Q008]. What will be the output of the following program :
void main()
{
unsigned val=0xffff;
if (~val)
printf("%d",val);
printf("%d",~val);
}
(a)Compile-Time error (b)-1 (c)0 (d)-1 0
Ans. (c)

[Q009]. What will be the output of the following program :
void main()
{
unsigned a=0xe75f,b=0x0EF4,c;
c=(a|b);
if ((c > a) && (c > b))
printf("%x",c);
}
(a)No Output (b)0xe75f (c)0xefff (d)None of these
Ans. (c)

[Q010]. What will be the output of the following program :
void main()
{
unsigned val=0xabcd;
if (val>>16 | val<<16)
{
printf("Success");
return;
}
printf("Failure");
}
(a)No Output (b)Success (c)Failure (d)SuccessFailure
Ans. (b)

[Q011]. What will be the output of the following program :
void main()
{
unsigned x=0xf880,y=5,z;
z=x< printf("%#x %#x",z,x>>y-1);
}
(a)1000 f87 (b)8800 0xf88 (c)1000 f88 (d)0x1000 0xf88
Ans. (d)


[Q012]. What will be the output of the following program :
void main()
{
register int a=5;
int *b=&a;
printf("%d %d",a,*b);
}
(a)Compile-Time error (b)Run-Time error (c)5 5 (d)Unpredictable
Ans. (a)

[Q013]. What will be the output of the following program :
auto int a=5;
void main()
{
printf("%d",a);
}
(a)Compile-Time error (b)Run-Time error (c)5 (d)Unpredictable
Ans. (a)

[Q014]. What will be the output of the following program :
void main()
{
auto int a=5;
printf("%d",a);
}
(a)Compile-Time error (b)Run-Time error (c)5 (d)Unpredictable
Ans. (c)

[Q015]. What will be the output of the following program :
void main()
{
int a=1,b=2,c=3,d=4;
if (d > c)
if (c > b)
printf("%d %d",d,c);
else if (c > a)
printf("%d %d",c,d);
if (c > a)
if (b < a)
printf("%d %d",c,a);
else if (b < c)
printf("%d %d",b,c);

}
(a)4 3 3 4 (b)4 3 3 2 (c)4 32 3 (d)4 33 1
Ans. (c)

[Q016]. What will be the output of the following program :
void main()
{
int a=1,b=2,c=3,d=4;
if (d > c)
if (c > b)
printf("%d %d",d,c);
if (c > a)
printf("%d %d",c,d);
if (c > a)
if (b < a)
printf("%d %d",c,a);
if (b < c)
printf("%d %d",b,c);
}
(a)4 32 3 (b)4 33 42 3 (c)4 3 3 4 2 3 (d)None of these
Ans. (b)

[Q017]. What will be the output of the following program :
void main()
{
int a=1;
if (a == 2);
printf("C Program");
}
(a)No Output (b)C Program (c)Compile-Time Error
Ans. (b)

[Q018]. What will be the output of the following program :
void main()
{
int a=1;
if (a)
printf("Test");
else;
printf("Again");
}
(a)Again (b)Test (c)Compile-Time Error (d)TestAgain
Ans. (d)

[Q019]. What will be the output of the following program :
void main()
{
int i=1;
for (; i<4; i++);
printf("%d\n",i);
}
(a)No Output (b)1 (c)4 (d)None of these
2
3
Ans. (c)

[Q020]. What will be the output of the following program :
void main()
{
int a,b;
for (a=0; a<10; a++);
for (b=25; b>9; b-=3);
printf("%d %d",a,b);
}
(a)Compile-Time error (b)10 9 (c)10 7 (d)None of these
Ans. (c)

[Q021]. What will be the output of the following program :
void main()
{
float i;
for (i=0.1; i<0.4; i+=0.1)
printf("%.1f",i);
}
(a)0.10.20.3 (b)Compile-Time Error (c)Run-Time Error (d)No Output
Ans. (a)

[Q022]. What will be the output of the following program :
void main()
{
int i;
for (i=-10; !i; i++);
printf("%d",-i);
}
(a)0 (b)Compile-Time Error (c)10 (d)No Output
Ans. (c)

[Q023]. What will be the output of the following program :
void main()
{
int i=5;
do;
printf("%d",i--);
while (i>0);
}
(a)5 (b)54321 (c)Compile-Time Error (d)None of these
Ans. (c)

[Q024]. What will be the output of the following program :
void main()
{
int i;
for (i=2,i+=2; i<=9; i+=2)
printf("%d",i);
}
(a)Compile-Time error (b)2468 (c)468 (d)None of these
Ans. (c)


[Q025]. What will be the output of the following program :
void main()
{
int i=3;
for (i--; i<7; i=7)
printf("%d",i++);
}
(a)No Output (b)3456 (c)23456 (d)None of these
Ans. (d)

[Q026]. What will be the output of the following program :
void main()
{
int i;
for (i=5; --i;)
printf("%d",i);
}
(a)No Output (b)54321 (c)4321 (d)None of these
Ans. (c)


[Q027]. What will be the output of the following program :
void main()
{
int choice=3;
switch(choice)
{
default:
printf("Default");

case 1:
printf("Choice1");
break;

case 2:
printf("Choice2");
break;
}
}
(a)No Output (b)Default (c)DefaultChoice1 (d)None of these
Ans. (c)

[Q028]. What will be the output of the following program :
void main()
{
static int choice;
switch(--choice,choice-1,choice-1,choice+=2)
{
case 1:
printf("Choice1");
break;

case 2:
printf("Choice2");
break;

default:
printf("Default");
}
}
(a)Choice1 (b)Choice2 (c)Default (d)None of these
Ans. (a)

[Q029]. What will be the output of the following program :
void main()
{
for (;printf(""););
}
(a)Compile-Time error (b)Executes ONLY once (c)Executes INFINITELY (d)None of these
Ans. (b)


[Q030]. What will be the output of the following program :
void main()
{
int i;
for (;(i=4)?(i-4):i++;)
printf("%d",i);
}
(a)Compile-Time error (b)4 (c)Infinite Loop (d)No Output
Ans. (d)

[Q031]. What will be the output of the following program :
void main()
{
static int j;
for (j<5; j<5; j+=j<5)
printf("%d",j++);
}
(a)024 (b)Compile-Time Error (c)01234 (d)No Output
Ans.


[Q032]. What will be the output of the following program :
void main()
{
int i=9;
for (i--; i--; i--)
printf("%d ",i);
}
(a)9 6 3 (b)Compile-Time Error (c)7 5 3 1 (d)Infinite Loop
Ans.

[Q033]. What will be the output of the following program :
void main()
{
int i;
for (i=5; ++i; i-=3)
printf("%d ",i);
}
(a)6 4 2 (b)Compile-Time Error (c)6 3 1 (d)Infinite Loop
[Q034]. Which of the following code causes INFINITE Loop :
(a)do while(1); (b)do;while(1); (c)do; (d)do{}while(1);
while(1);

(i)Only (a) (ii)Only (b), (c) & (d) (iii)None of these (iv)All of these

[Q035]. What will be the output of the following program :
#define Loop(i) for (j=0; j sum += i+j; \
}
void main()
{
int i,j,sum=0;
for (i=0; i<=3; i++)
Loop(i)
printf("%d",sum);
}

(a)Run-Time Error (b)Compile-Time Error (c)18 (d)0

[Q001]. The following code is not well-written. What does the program do ?
void
main(
){int a=1,
b=2,c=3,d=4;printf("%d %d",
a,b);printf(
" %d %d",c,d);
}

(a)Run-Time Error (b)Compile-Time Error (c)1 2 3 4 (d)None of these
Ans. (c)

[Q002]. What will be the output of the following program :
void main()
{
int a=1,b=2,c=3;
c=(--a,b++)-c;
printf("%d %d %d",a,b,c);
}
(a)0 3 -3 (b)Compile-Time Error (c)0 3 -1 (d)0 3 0
Ans. (c)


[Q003]. What will be the output of the following program :
void main()
{
int a=1,b=2,c=3,d=4,e;
e=(a,a)+(b,c)+(c,d)-(d,b);
printf("%d",e);
}
(a)Compile-Time Error (b)10 (c)6 (d)2
Ans. (c)

[Q004]. What will be the output of the following program :
void main()
{
float val=2.;
printf("%.2",val);
}
(a)Compile-Time error (b)2.00 (c)%.2 (d)2.000000
Ans. (c)

[Q005]. What will be the output of the following program :
void main()
{
int a=5;
int b=6;;
int c=a+b;;;
printf("%d",c);;;;
}
(a)Compile-Time Error (b)Run-Time Error (c)11 (d)None of these
Ans. (c)

[Q006]. What will be the output of the following program :
void main()
{
int i,j;
for (i=1; i<=3; i++)
for (j=1; j<3; j++)
{
if (i == j)
continue;
if ((j % 3) > 1)
break;
printf("%d",i);
}
}
[Q007]. What will be the output of the following program :
#define swap(a,b) temp=a; a=b; b=temp;
void main()
{
static int a=5,b=6,temp;
if (a > b)
swap(a,b);
printf("a=%d b=%d",a,b);
}
(a)a=5 b=6 (b)a=6 b=5 (c)a=6 b=0 (d)None of these
Ans. (c)

[Q008]. What will be the output of the following program :
void main()
{
unsigned int val=5;
printf("%u %u",val,val-11);
}
(a)Compile-Time error (b)5 -6 (c)5 65530 (d)None of these
Ans. (c)

[Q009]. What will be the output of the following program :
void main()
{
int x=4,y=3,z=2;
*&z*=*&x**&y;
printf("%d",z);
}
(a)Compile-Time error (b)Run-Time Error (c)24 (d)Unpredictable
Ans. (c)

[Q010]. What will be the output of the following program :
void main()
{
int i=5,j=5;
i=i++*i++*i++*i++;
printf("i=%d ",i);
j=++j*++j*++j*++j;
printf("j=%d",j);
}
(a)Compile-Time Error (b)i=1680 j=1680 (c)i=629 j=6561 (d)i=1681 j=3024
Ans. (c) Compiler dependent


[Q011]. What will be the output of the following program :
void main()
{
int i=5;
printf("%d %d %d %d %d",++i,i++,i++,i++,++i);
}
(a)Compile-Time Error (b)10 9 8 7 6 (c)9 8 7 6 6 (d)10 8 7 6 6
Ans. (d)


[Q012]. What will be the output of the following program :
void main()
{
unsigned ch='Y';
printf("%d",sizeof ch);
}
(a)Compile-Time Error (b)2 (c)4 (d)1
Ans. (b)


[Q013]. What will be the output of the following program :
void main()
{
int a=5;
printf("%d");
}
(a)Compile-Time Error (b)5 (c)Unpredictable (d)No Output
Ans. (b)

[Q014]. What will be the output of the following program :
void main()
{
int a=5,b=6;
printf("%d");
}
(a)Compile-Time Error (b)5 c)6 (d)Unpredictable
Ans. (c)

[Q015]. What will be the output of the following program :
void main()
{
int a=5,b=6,c=7;
printf("%d %d %d");
}
(a)Compile-Time Error (b)5 6 7 (c)7 6 5 (d)Unpredictable
Ans. (c)

[Q016]. What will be the output of the following program :
#define Swap if (a != b) { \
temp=a; \
a = b; \
b = temp; \ //Swapping Done
}
void main()
{
int a=10,b=5,temp;
Swap;
printf("a=%d a=%d",a,b);
}
(a)Compile-Time Error (b)a=5 b=10 (c)a=10 b=5 (d)None of these
Ans. (a)


[Q017]. What will be the output of the following program :
void main()
{
int val=50;
void *ptr;
ptr=&val;
printf("%d %d",val,*(int *)ptr);
}
(a)Compile-Time Error (b)50 50 (c)50 0 (d)None of these
Ans. (b)

[Q018]. What will be the output of the following program :
void main()
{
int a=5,b=6;
Printf("%d %d %d",a,b,--a*++b);
}
(a)Compile-Time Error (b)5 6 30 (c)4 7 28 (d)None of these
Ans. (a)

[Q019]. What will be the output of the following program :
void main()
{
int val=5;
void *ptr;
*(int *)ptr=5;
val=ptr;
printf("%d %d",*(int *)val,*(int *)ptr);
}
(a)Compile-Time Error (b)Unpredictable (c)5 5 (d)None of these
Ans. (c)

[Q020]. What will be the output of the following program :
void main()
{
int val=2;
val = - --val- val--- --val;
printf("%d",val);
}
(a)Compile-Time Error (b)3 (c)-1 (d)0
Ans. (c)


[Q021]. What will be the output of the following program :
void main()
{
int i,n=10;
for (i=1; i printf("%d\n",n-i);
}
(a)84 (b)840 (c)852 (d)864
Ans. (c)

[Q001]. What will be the output of the following program :
#define func(a,b) b-##--a
void main()
{
int a=5,b=3,c;
c=func(a,b);
printf("%d %d %d",a,b,c);
}
(a)Compile-Time Error (b)5 3 2 (c)4 3 -1 (d)5 2 -2
Ans. (d) When using macros with argument lists, you can merge (or paste) two tokens by separating
them with ## (plus optional whitespace on either side), i.e. Token pasting with ##.


[Q002]. What will be the output of the following program :
void main()
{
#define b 100
int a=5,c;
c=a+b;
printf("%d %d %d",a,b,c);
}

(a)Compile-Time Error (b)Run-Time Error (c)5 100 105 (d)None of these
Ans. (c) Preprocessor directives can be defined anywhere in the program but just before its use.

[Q003]. What will be the output of the following program :
void main()
{
int a=25.0;
#include
printf("%.2lf %.2lf",sqrt(a),pow(sqrt(a)*5.0,2.0));
}
(a)Compile-Time Error (b)5 625 (c)5.00 125.00 (d)5.00 625.00
Ans. (d) Preprocessor directives can be defined anywhere in the program but just before its use.


[Q004]. What will be the output of the following program :
void main()
int c;
{
int a=5,b=10;
c=a+=b-=a;
printf("%d %d %d",a,b,c);
}
(a)Compile-Time error (b)5 10 5 (c)10 5 10 (d)10 10 5
Ans. (a) Consider any function definitions (here it is 'main') in which DECLARATION OF VARIABLES
between the () and {} are LEGAL for specifying data types for FORMAL PARAMETERS ONLY. Otherwise
it is ILLEGAL or INVALID. Ex:- int c; in the above program is an ILLEGAL statement in C.


[Q005]. What will be the output of the following program :
#define func(x,y) { func(x,y) }
void main()
{
int a=5,b=6;
c=func(x,y);
printf("%d %d %d",c);
}
(a)Compile-Time Error (b)Linker Error (c)5 6 11 (d)Infinite Loop
Ans. (b) A macro cannot call itself recursively.


[Q006]. What will be the output of the following program :
void main()
{
const val=57;
printf("%d ",val);
*(int *)&val=75;
printf("%d",val);
}
(a)Compile-Time Error (b)75 57 (c)57 75 (d)None of these
Ans. (c) A const variable can be indirectly modified by a pointer as shown in the above program.


[Q007]. What will be the output of the following program :
#define print(msg) printf("Message:" #msg)
void main()
{
print("Hi Friends");
}
(a)Compile-Time Error (b)Message:Hi Friends
(c)Message:"Hi Friends" (d)"Message:Hi Friends"
Ans. (c) The "stringizing" operator # allows a formal argument within a macro definition to be
converted to a string. Any special characters such as '," and \, will be replaced by their
corresponding escape sequences, Ex: \',\" and \\. In addition, the resulting string will
automatically be concatenated (combined) with any adjacent strings.


[Q008]. What will be the output of the following program :
#define print(val) printf("x" #val "=%.2f ",x##val)
void main()
{
float x1=5.74,x2=23.78;
print(1);
print(2);
}
(a)Compile-Time error (b)x1=5.74 x2=23.78 (c)Linker Error (d)None of these
Ans. (b) The "stringizing" operator # allows a formal argument within a macro definition to be
converted to a string. The "token-pasting" operator ## causes individual items within a macro
definition to be concatenated, thus forming a single item.


[Q009]. What will be the output of the following program :
#define print(msg) printf(#msg)
void main()
{
print(C FOR SWIMMERS);
}
(a)Compile-Time error (b)C FOR SWIMMERS (c)C FOR SWIMMERS (d)No Output
Ans. (b) The "stringizing" operator # allows a formal argument within a macro definition to be
converted to a string. Consecutive whitespace characters inside the actual argument will be
replaced by a single blank space character.


[Q010]. What will be the output of the following program :
#define int float
void main()
{
int a=5.75,b=9.63,c;
c=a+b;
printf("%.2f %.2f %.2f",a,b,c);
}

(a)Compile-Time Error (b)5.75 9.63 15.38 (c)Linker Error (d)None of these
Ans. (b) It is legal but ill-advised to use Turbo C++ keywords as macro identifiers.
In the above program : #define int float is legal, but possibly catastrophic.


[Q011]. What will be the output of the following program :
void main()
{
int i=5;
float j=56.78;
char str[20];
scanf("%s %*d %f",str,&i,&j);
printf("%s %d %.2f",str,i,j);
}
[Assume the INPUT values entered by the user are :testing 12345 2.25]
(a)Compile-Time Error (b)testing 12345 2.25
(c)testing 5 2.25 (d)testing 12345 56.78
Ans. (c) To skip over a data item without assigning it to the designated variable or array, the
% sign within the appropriate control group should be followed by an asterisk (*). This feature
is referred to as ASSIGNMENT SUPPRESSION.


[Q012]. What will be the output of the following program :
main()
{
static int val=7;
int data;
if (--val)
{
data=main()+val;
printf("%d ",data);
}
return 0;
}
(a)Compile-Time Error (b)INFINITE LOOP (c)1 2 3 4 5 6 (d)0 0 0 0 0 0
Ans. (d) The variable 'val' is declared as static, hence memory for 'val' will be allocated for
only once, as it encounters the statement. The function main() will be called recursively unless
'val' becomes equal to 0, and since main() is recursively called, so the value of static 'val'
ie., 0 will be printed every time the control is returned.


[Q013]. What will be the output of the following program :
void main()
{
int i=5;
{
int j=10;
printf("%d %d",i++,++j);
}
printf("\n%d %d",i,j);
}
(a)Compile-Time Error (b)5 11 (c)5 11 (d)No Output
6 11 6 Garbage value here
Ans. (a) The variable j is a block level variable and the visibility is inside that block only.


[Q014]. What will be the output of the following program :
#define val 100
void main()
{
printf("%d ",val);
#define val 1000
printf("%d",val*10);
}
(a)Compile-Time Error (b)100 1000 (c)100 10000 (d)None of these
Ans. (c) The preprocessor directives can be redefined anywhere in the program. So the most
recently assigned value will be taken.


[Q015]. What will be the output of the following program :
#define a 50
#define b 60
void main()
{
int sum;
sum=++a + --b;
printf("%d %d %d",a,b,sum);
}
(a)Compile-Time Error (b)50 60 110 (c)51 59 110 (d)None of these
Ans. (a) #define statement defines symbolic constants in a C program. So the assigned value
cannot be altered using any operators instead it can be redefined anywhere in the program.


[Q016]. What will be the output of the following program :
void main()
{
int a[5];
printf("%d",9-*a-3+*a);
}
(a)Compile-Time Error (b)6 (c)Garbage Value (d)None of these
Ans. (b) *a and -*a cancels out. The result is as simple as 9 - 3 = 6


[Q017]. What will be the output of the following program :
void main()
{
static int i;
while (i <= 10)
(i > 2) ? i++ : i--;
printf("%d",i);
}
(a)Compile-Time Error (b)11 (c)32767 (d)None of these
Ans. (c) Since i is static it is initialized to 0. Inside the while loop the conditional operator
evaluates to false, executing i--. Thus the integer value rotates to positive value (32767). Then
while condition becomes false and hence, comes out of the while loop, printing the i value.


[Q018]. What will be the output of the following program :
void main()
{
int a=5,b=6,c;
c=(b++ == 6) || (--a < 2);
printf("%d %d %d",a,b,c);
}
(a)Compile-Time Error (b)4 7 1 (c)4 7 6 (d)5 7 1
Ans. (d) The boolean expression needs to be evaluated only till the truth value of the expression
is not known. Here 's truth value is 1 or true. Because true || where (expr2)
will not be evaluated. So the value of a remains the same. Thus a=5, b=7 & c=1


[Q019]. What will be the output of the following program :
void main()
{
int *ptr;
ptr=0x1fa;
*ptr++=5000;
*ptr=6000;
printf("%d ",++*ptr--);
printf("%d",(*ptr)++);
}
(a)Compile-Time Error (b)6000 6000 (c)6001 5000 (d)6001 5001
Ans. (c) The pointer ptr will point to the integer value in the memory location 0x1fa.
Ex: The value 5000 stored at 0x1fa and 0x1fb since size of integer value is 2 bytes. Similarly,
the value 6000 stored at 0x1fc and 0x1fd.


[Q020]. What will be the output of the following program :
void main()
{
char *str="ABCDEFGHI";
printf("%c %c %c ",str[7]+1,3[++str],++*(str+5));
printf("%c %c %c",++*str,*++str,++*str++);
}
(a)Compile-Time Error (b)J D G E D B (c)J E G E D C (d)J E G D C B
Ans. (c) str[i] is same as i[str], str[i] is same as *(str+i), str[i+1] is same as *++(str+i)
Careful observation is necessary.


[Q001]. What will be the output of the following program :
int func(int *ptr,int a,int b)
{
return (a+b);
*ptr=25;
}
void main()
{
int var=7,sum;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}

(a)Compile-Time Error (b)11 25 (c)11 7 (d)None of these
Ans. (c)


[Q002]. What will be the output of the following program :
int func(int *ptr,int a,int b)
{
return (a+b);
*ptr=25;
return *ptr;
}
void main()
{
int var=7;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}

(a)Compile-Time Error (b)11 25 (c)11 7 (d)25 7
Ans. (c)


[Q003]. What will be the output of the following program :
int func(int *ptr,int a,int b)
{
*ptr=25;
return *ptr;
return (a+b);
}
void main()
{
int var=7;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}

(a)Compile-Time Error (b)11 25 (c)11 7 (d)25 25
Ans. (d)


[Q004]. What will be the output of the following program :
#include
#define ToStr(s) #s
#define Swap(x,y) y##x
void Swap(in,ma)()
{
if (printf(ToStr("Friends"))){}
}

(a)Compile-Time Error (b)"Friends" (c)Friends (d)None of these
Ans. (b)


[Q005]. What will be the output of the following program :
void main()
{
int a=(5,a=50)/a++;
printf("%d",a);
}

(a)51 (b)0 (c)6 (d)1
Ans. (d)


[Q006]. What will be the output of the following program :
void main()
{
int a=(5,a=50)/++a;
printf("%d",a);
}
(a)51 (b)0 (c)6 (d)1
Ans. (b)


[Q007]. What will be the output of the following program :
void main()
{
int a=7;
if ((++a < 7) && (++a < 9) && (++a < 25)
;
printf("%d",a);
}

(a)7 (b)8 (c)9 (d)10
Ans. (b)


[Q008]. What will be the output of the following program :
void main()
{
int a=7;
if ((++a < 7) && (a++ < 9) && (++a < 10);
printf("%d",a);
}

(a)7 (b)8 (c)9 (d)10
Ans. (d)


[Q009]. What will be the output of the following program :
main()
{
for ( ; ; )
main();
}
(a)Compile-Time error (b)Run-Time Error (c)Infinite Loop (d)None of these
Ans. (c)


[Q010]. What will be the output of the following program :
void main()
{
int a=0,b=1,c;
c = a=0 ? (a=1) : (b=2);
printf("%d %d %d",a,b,c);
}

(a)Compile-Time Error (b)2 2 2 (c)1 1 1 (d)None of these
Ans. (b)


[Q011]. What will be the output of the following program :
void main()
{
int a=5,b=1,c;
c = a ? a=1 : b=2;
printf("%d %d %d",a,b,c);
}

(a)Compile-Time Error (b)2 2 2 (c)1 1 1 (d)None of these
Ans. (a)


[Q012]. What will be the output of the following program :
main()
{
unsigned _=5;
_=_--- ++_;
printf("%d",_);
}
(a)Compile-Time Error (b)5 (c)65535 (d)-1
Ans. (d)


[Q013]. What will be the output of the following program :
main()
{
unsigned _=5;
_=_--- ++_;
printf("%u",_);
}
(a)Compile-Time Error (b)5 (c)65535 (d)-1
Ans. (c)


[Q014]. What will be the output of the following program :
int fun(int a,int b)
{
#define NUM 5
return a+b;
}
main()
{
printf("Sum=%d",fun(NUM,NUM));
}

(a)Compile-Time Error (b)Run-Time Error (c)Sum=10 (d)None of these
Ans. (c)


[Q015]. What will be the output of the following program :
void main()
{
int a=5,b=6,c=7;
printf("%d %d",a,b,c);
}
(a)Compile-Time Error (b)5 6 (c)Run-Time Error (d)5 6 7
Ans. (b)


[Q016]. What will be the output of the following program :
int add(int a,int b)
{
return a+b;
}
main()
{
int a=1,b=2;
printf("%d",add(add(add(a,b),add(a,b)),add(add(a,b),add(a,b))));
}

(a)Garbage value (b)Run-Time Error (c)Compile-Time Error (d)12
Ans. (d)


[Q017]. What will be the output of the following program :
#define S(s)char x[]="Welcome to ";s
#define P(s) #s
#define Q(x)char x[]=#x
#define A(x,y)y##x
#define B(x,y)A(y,x)
#define C(x,y)B(y,x)
S(B(A(a,m),A(n,i))(){Q(C(B(A(n,e),d),B(A(r,f),i))s);B(A(t,
s),B(A(c,r),A(y,p)))(C(B(A(n,e),d),B(A(r,f),i))s,P(c4swimmers)
);B(A(r,p),B(A(n,i),A(f,t)))("%s%s",x,C(B(A(n,e),d),B(A(r,f),i))s)
;B(A(e,r),B(A(u,t),A(n,r))) 0;})

(a)Compile-Time Error (b)Welcome to c4swimmers (c)Welcome to (d)c4swimmers
Ans. (c)


[Q018]. What will be the output of the following program :
void main()
{
int a=5,b=6,c=7,d=8,e;
e = a ? b , c : c , d;
printf("%d",e);
}

(a)Compile-Time Error (b)6 (c)7 (d)8
Ans. (c)


[Q019]. What will be the output of the following program :
void main()
{
int a=5,b=6,c;
c++ = b % (b - a);
printf("%d",c);
}

(a)Compile-Time Error (b)6 (c)7 (d)None of these
Ans. (a)


[Q020]. What will be the output of the following program :
void main()
{
int x=5,y=6,z;
z = x++ +++y;
printf("%d %d %d",x,y,z);
}
(a)Compile-Time Error (b)6 7 12 (c)5 6 11 (d)None of these
Ans. (a)


[Q001]. The following code is not well-written. What does the program do ?
void main()
{
int a=1,b=2;
printf("%d",add(a,b));
}
int add(int a,int b)
{
return (a+b);
}
(a)Run-Time Error (b)Compile-Time Error (c)3 (d)None of these
Ans. (b)


[Q002]. What will be the output of the following program :
int add(int a,int b)
{
int c=a+b;
}
void main()
{
int a=10,b=20;
printf("%d %d %d",a,b,add(a,b));
}
(a)10 20 0 (b)Compile-Time Error (c)10 20 30 (d)None of these
Ans. (c)


[Q003]. What will be the output of the following program :
int add(int a,int b)
{
int c=a+b;
return;
}
void main()
{
int a=10,b=20;
printf("%d %d %d",a,b,add(a,b));
}
(a)10 20 0 (b)Compile-Time Error (c)10 20 30 (d)None of these
Ans. (c)


[Q004]. What will be the output of the following program :
void main()
{
int add(int,int);
int a=7,b=13;
printf("%d",add(add(a,b),add(a,b)));
}
int add(a,b)
int a,b;
{
return (a+b);
}
(a)Compile-Time error (b)20 (c)40 (d)None of these
Ans. (c)


[Q005]. What will be the output of the following program :
int add(a,b)
{
int c=a+b;
return c;
}
void main()
{
int a=10,b=20;
printf("%d",add(a,b));
}
(a)30 (b)Compile-Time Error (c)0 (d)None of these
Ans. (a)


[Q006]. What will be the output of the following program :
int funct2(int b)
{
if (b == 0)
return b;
else
funct1(b--);
}
int funct1(int a)
{
if (a == 0)
return a;
else
funct2(a--);
}
void main()
{
int a=7;
printf("%d",funct1(a));
}
(a)0 (b)Compile-Time Error (c)Infinite Loop (d)7
Ans. (c)


[Q007]. What will be the output of the following program :
int funct2(int b)
{
if (b == 0)
return b;
else
funct1(--b);
}
int funct1(int a)
{
if (a == 0)
return a;
else
funct2(--a);
}
void main()
{
int a=7;
printf("%d",funct1(a));
}
(a)0 (b)Compile-Time Error (c)Infinite Loop (d)7
Ans. (a)


[Q008]. What will be the output of the following program :
int funct1(int a)
{{;}{{;}return a;}}
void main()
{
int a=17;
printf("%d",funct1(a));
}
(a)0 (b)Compile-Time Error (c)17 (d)None of these
Ans. (c)


[Q009]. What will be the output of the following program :
int funct1(int a)
{
if (a)
return funct1(--a)+a;
else
return 0;
}
void main()
{
int a=7;
printf("%d",funct1(a));
}
(a)7 (b)21 (c)28 (d)None of these
Ans. (c)


[Q010]. What will be the output of the following program :
int compute(int a,int b)
int c;
{
c=a+b;
return c;
}
void main()
{
int a=7,b=9;
printf("%d",compute(a,b));
}
(a)Compile-Time Error (b)16 (c)None of these
Ans. (a)


[Q011]. What will be the output of the following program :
int a=10;
void compute(int a)
{
a=a;
}
void main()
{
int a=100;
printf("%d ",a);
compute(a);
printf("%d",a);
}
(a)10 10 (b)Compile-Time Error (c)100 100 (d)100 10
Ans. (c)


[Q012]. What will be the output of the following program :
int funct(char ch)
{
ch=ch+1;
return ch;
}
void main()
{
int a=127;
printf("%d %d",a,funct(a));
}
(a)Compile-Time Error (b)127 128 (c)127 -128 (d)None of these
Ans. (c)


[Q013]. What will be the output of the following program :
char funct(int val)
{
char ch=val;
return ch;
}
void main()
{
float a=256.25;
printf("%d",funct(a));
}
(a)0 (b)256.25 (c)256 (d)None of these
Ans. (a)


[Q014]. What will be the output of the following program :
auto int a;
void changeval(int x)
{
a=x;
}
void main()
{
a=15;
printf("%d",a);
changeval(75);
printf("%d",a);
}
(a)Compile-Time Error (b)15 75 (c)15 15 (d)None of these
Ans. (a)


[Q015]. What will be the output of the following program :
int val;
static int funct()
{
return val*val;
}
void main()
{
val=5;
funct();
val++;
printf("%d",funct());
}
(a)Compile-Time Error (b)25 (c)36 (d)None of these
Ans. (c)


[Q016]. What will be the output of the following program :
static int funct(int val)
{
static int sum;
sum+=val;
return sum;
}
void main()
{
int i,n=9;
for (i=1; i funct(i*2);
printf("%d",funct(0));
}
(a)20 (b)0 (c)30 (d)None of these
Ans. (a)


[Q017]. What will be the output of the following program :
void print(int a[],...)
{
while (*a != -1)
printf("%d",*a++);
}
void main()
{
int a[]={1,2,3,4,5,-1};
print(a,5,6,7,8,9,-1);
}
(a)Compile-Time Error (b)Run-Time Error (c)12345 (d)56789
Ans. (c)


[Q018]. What will be the output of the following program :
void print(int *);
void print(int *);
void main()
{
int x=100;
print(&x);
}
void print(int *a)
{
printf("%d",*a);
}
(a)Compile-Time Error (b)Run-Time Error (c)100 (d)None of these
Ans. (c)


[Q019]. What will be the output of the following program :
void main()
{
void funct1(void);
void funct2(void);
clrscr();
funct1();
}
void funct1(void)
{
printf("Ocean of ");
funct2();
}
void funct2(void)
{
printf("Knowledge");
}
(a)Compile-Time Error (b)Run-Time Error (c)Ocean of Knowledge (d)None of these
Ans. (a)


[Q020]. What will be the output of the following program :
static int count=1;
void funct3(void)
{
printf("%d",++count);
}
void funct2(void)
{
printf("%d",count);
funct3();
}
void funct1(void)
{
printf("Counting...%d",count++);
funct2();
}
void Main()
{
funct1();
}
(a)Compile-Time Error (b)Counting...123 (c)Counting...111 (d)Counting...112
Ans. (a) Since main() is different from Main() i.e. linker error

[Q001]. Whether the following program is a LEGAL code or ILLEGAL code. If it is a LEGAL code then
what will be the output :
#include
main(t,_,a)
char *a;
{return!0 main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0 i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

Ans. As an extreme example the above C code snippet (mystery.c) is actually LEGAL C code.
It will compile and run and produce meaningful output. Try this program out. Try to compile and
run it yourself.

Clearly nobody ever writes code like or at least should never. This piece of code actually won an
international Obfuscated C Code Contest http://reality.sgi.com/csp/iocc The standard for
C programs was originally the features set by Brian Kernighan. In order to make the language more
internationally acceptable, an international standard was developed, ANSI C (American National
Standards Institute).

Please excuse for this.


[Q002]. What will be the output of the following program :
void main()
{
int i;
float a[5];
for (i=0; i<5; i++)
a[i] = (printf, ("%d",i/10.0));
for (i=0; i<5; i++)
printf("%.1f ",a[i]);
}

(a)Compile-Time Error (b)0.0 0.0 0.0 0.0 0.0 (c)0.0 0.1 0.2 0.3 0.4 (d)1.0 1.0 1.0 1.0 1.0
Ans. (c) Since Comma operator (,) is used, the expressions are evaluated from left to right and
the entire expression assumes the value of the last one evaluated. Also i/10.0 is the rightmost
expression hence it is evaluated and its value is assigned to a[i]. The first printf in the
program has no effect on the output (See next question [Q003]).


[Q003]. What will be the output of the following program :
void func()
{
printf("Testing...Done\n");
}
void main()
{
func;
func();
}
(a)Compile-Time Error (b)Testing...Done (c)Testing...Done (d)None of these
Testing...Done
Ans. (b) Since first statement 'func;' has no effect on the output and the second statement
'func();' is a normal function call which prints the message(One time) as specified.


[Q004]. A signed int bitfield 1-bit wide can only hold the values

(a)0 and 1 (b)0 and -1 (c)0, 1 and -1 (d)None of these
Ans. (b) Since any non-zero value will be interpreted as -1.


[Q005]. What will be the output of the following program :
void main()
{
int a=19,b=4;
float c;
c=a/b;
printf("%f",c);
}

(a)4.75 (b)4 (c)4.750000 (d)4.000000
Ans. (d) Since a and b are both of type int, so the result of a/b was of type int. That was
converted to type float when you assigned it to c, but the conversion took place after the
division, not before.


[Q006]. What will be the output of the following program :
void main()
{
int _;
_=70;
printf("%d",_);
}
(a)Compile-Time Error (b)Run-Time Error (c)70 (d)None of these
Ans. (c) Since a variable (an identifier) can start with an underscore.


[Q007]. In DOS environment, what is the maximum combined length of the command-line arguments
passed to main (including the space between adjacent arguments and the name of the program
itself).

(a)80 Characters (b)128 Characters (c)Until RETURN KEY (d)None of these
(i.e '\n') is encountered
Ans. (b) Since this is a DOS limit.


[Q008]. What will be the output of the following program :
void main()
{
int (*foo)(char *, ...) = printf;
(*foo)("hello, %s", "world!");
}
(a)Compile-Time error (b)hello, world! (c)Run-Time Error (d)None of these
Ans. (b) This is one of the method used for calling functions through pointers. Here foo is a
pointer to a function (printf) that accepts an argument which is a pointer to a character. Also
it accepts variable number of arguments and returns an integer value.


[Q009]. What will be the output of the following program :
void main()
{
int i=5,(*foo)(char *, ...);
foo=printf;
printf("%d",i=(*foo)("hello, %s\n", "world!"));
}
(a)Compile-Time error (b)hello, world! (c)hello, world! (d)hello, world!
5 13 14
Ans. (d) This is another method used for calling functions through pointers. Here foo is a
pointer to a function (printf) that accepts an argument which is a pointer to a character. Also
it accepts variable number of arguments and returns an integer value. Also 'printf' returns the
no. of bytes output which is stored in the variable 'i' i.e. i=14 (including '\n' character).


[Q010]. What will be the output of the following program :
void main()
{
int choice=2;
switch(choice)
{
default:
printf("Default1");

case 1:
printf("Case1");
break;

default:
printf("Default2");
}
}
(a)Compile-Time Error (b)Default1Case1 (c)Default2 (d)Default1
Ans. (a) Since Too many default cases i.e. the compiler encountered more than one default
statement in a single switch.


[Q011]. What is the MAXIMUM LIMIT for cases in a switch statement ?

(a)32767 cases (b)257 cases (c)127 (d)None of these
Ans. (b) Since a switch statement is limited to 257 cases.


[Q012]. What will be the output of the following program :
#define big(a,b) a > b ? a : b
#define swap(a,b) temp=a; a=b; b=temp;
void main()
{
int a=3,b=5,temp;
if ((3+big(a,b)) > b)
swap(a,b);
printf("%d %d",a,b);
}
(a)3 0 (b)5 3 (c)3 5 (d)5 0
Ans. (d) Since if ((3+3 > 5 ? 3 : 5) > 5) is FALSE. But a macro is expanded during the compilation
process itself. Thus temp=a; will not be executed and the remaining statements a=b; and b=temp;
are executed in the normal way.


[Q013]. What will be the output of the following program :
#define main main()
void main
{
#define END }
printf("First"
"Second"
"Third");
END

(a)Compile-Time Error (b)First (c)FirstSecondThird (d)None of these
Second
Third
Ans. (c) Preprocessor directives can be defined anywhere in the program but just before its use.
Turbo C will automatically do the concatenation for you on very long strings, resulting in nicer
looking programs.
According to K&R (applies to ALL C compilers), a string constant consists of exactly one string
unit, containing double quotes, text, double quotes ("like this"). You must use the backslash(\)
as a continuation character in order to extend a string constant across line boundaries.

Thus the above printf statement could be re-written (K&R Style) as follows :

printf("First" \
"Second" \
"Third");


[Q014]. What will be the output of the following program :
void main()
{
long double val;
printf("%d bytes",sizeof(val));
}
(a)Compile-Time Error (b)4 bytes (c)8 bytes (d)10 bytes
Ans. (d) Since sizeof long double is 80 bits i.e 10 bytes (1 byte = 8 bits)


[Q015]. What will be the output of the following program :
int * func()
{
int temp=50;
return &temp;
}
void main()
{
int *val;
val = func();
printf("%d",*val);
}
(a)Compile-Time Error (b)50 (c)Garbage Value (d)None of these
Ans. (c) Here the address of the variable 'temp' is returned to the variable 'val' in the main
program but the variable 'temp' is local to the function. As we know every function involves
built-in stack and whenever a function is called Stack Pointer (SP, which is a register pseudo-
variable) value changes [See ALT+W+R Register Window Screen]. Therefore it prints the garbage
value.


[Q001]. What will be the output of the following program :
void main()
{
printf();
}
(a)Run-Time Error (b)Compile-Time Error (c)No Output (d)None of these
Ans. (b) Since there must be enough arguments for the format.


[Q002]. What will be the output of the following program :
void main()
{
printf(NULL);
}
(a)Run-Time Error (b)Compile-Time Error (c)No Output (d)None of these
Ans. (c) Since NULL is a constant value or NULL pointer value or a NULL string.


[Q003]. What will be the output of the following program :
void main()
{
printf("%%",7);
}
(a)7 (b)Compile-Time Error (c)% (d)%%
Ans. (c) Since % is a format specifier & excess arguments (more than required by the format) are
merely ignored.


[Q004]. What will be the output of the following program :
void main()
{
printf("//",5);
}
(a)5 (b)Compile-Time Error (c)/ (d)//
Ans. (d) Since / is an escape sequence character & excess arguments (more than required by the format) are merely ignored.


[Q005]. What will be the output of the following program :
void main()
{
printf("d%",8);
}
(a)8 (b)Compile-Time Error (c)d% (d)None of these
Ans. (c) Since excess arguments (more than required by the format) are merely ignored.


[Q006]. What will be the output of the following program :
void main()
{
printf("%d"+0,123);
}
(a)123 (b)Compile-Time Error (c)No Output (d)None of these
Ans. (a) "%d"+0 has no effect on the output operation.


[Q007]. What will be the output of the following program :
void main()
{
printf("%d"+1,123);
}
(a)123 (b)Compile-Time Error (c)d (d)No Output
Ans. (c) "%d"+1 (i.e 1 or > 0) affects the program output by considering "%d" as string and ignores 123
Where 1 refers to the index i.e. 2nd character in the array or string "%d".


[Q008]. What will be the output of the following program :
void main()
{
printf("%d",printf("Hi!")+printf("Bye"));
}
(a)ByeHi!6 (b)Hi!Bye6 (c)Compile-Time Error (d)None of these
Ans. (b) Since L->R priority & the length of the strings 'Hi!' & 'Bye' is 3+3=6


[Q009]. What will be the output of the following program :
void main()
{
printf("%d",printf("Hi!")*printf("Bye"));
}
(a)ByeHi!6 (b)Hi!Bye9 (c)Hi!Bye (d)None of these
Ans. (b) Since L->R priority & the length of the strings 'Hi!' & 'Bye' is 3*3=9


[Q010]. What will be the output of the following program :
void main()
{
printf("%d",printf("")+printf(""));
}
(a)0 (b)No Output (c)Compile-Time Error (d)None of these
Ans. (a) Since L->R priority & the length of the 2 empty strings are : 0+0=0


[Q011]. What will be the output of the following program :
void main()
{
printf("Hi Friends"+3);
}
(a)Hi Friends (b)Friends (c)Hi Friends3 (d)None of these
Ans. (b) Since (base adress)+0 points to the value 'H'. Now the NEW (base address) equals
(base address)+3 that points to the character 'F'. Thus it prints the string from 'F' onwards.


[Q012]. What will be the output of the following program :
void main()
{
printf("C For ") + printf("Swimmers");
}
(a)Compile-Time Error (b)C For Swimmers (c)Run-Time Error (d)None of these
Ans. (b) It is a VALID C statement. Change the operators but the output remains same(NO EFFECT).


[Q013]. What will be the output of the following program :
void main()
{
printf("\/\*\-*\/");
}
(a)Run-Time Error (b)\/*-*\/ (c)/*-*/ (d)None of these
Ans. (c) Since \ is an escape sequence character. Be careful while analyzing such statements.


[Q014]. What will be the output of the following program :
int main()
{
int main=7;
{
printf("%d",main);
return main;
}
printf("Bye");
}
(a)Compile-Time Error (b)Run-Time Error (c)7Bye (d)7
Ans. (d) It is a VALID C statement. Prints 7 and returns the same to the OS. NOTE: Last printf
statement will not be executed.


[Q015]. What will be the output of the following program :
void main()
{
main();
}
(a)Compile-Time Error (b)Run-Time Error (c)Infinite Loop (d)None of these
Ans. (c) It is a VALID C statement. It is like a recursive function & the statements get executed
infinite number of times.


[Q016]. What will be the output of the following program :
void main()
{
printf("Work" "Hard");
}
(a)Work (b)Hard (c)No Output (d)WorkHard
Ans. (d) Since L->R priority. First it prints the word 'Work' & then 'Hard'.


[Q017]. What will be the output of the following program :
void main()
{
char str[]="%d";
int val=25;
printf(str,val);
}
(a)Compile-Time Error (b)Run-Time Error (c)25 (d)None of these
Ans. (c) It is a VALID C statement. First parameter contains the format specifier & the Second
parameter contains the actual value 25.


[Q018]. What will be the output of the following program :
void main()
{
int val=75;
printf("%d",val,.,.);
}
(a)Compile-Time Error (b)Unpredictable (c)75 (d)None of these
Ans. (b) Output is Unpredictable B'coz there are not enough arguments for the format. But it is a
VALID C statement.


[Q019]. What will be the output of the following program :
void main()
{
int val=10;
printf("%d",val+1,"%d",val--);
}
(a)10 (b)11 10 (c)11 9 (d)10 9
Ans. (a) Since R->L priority. The second format specifier '%d' is an excess argument and it is
ignored.


[Q020]. What will be the output of the following program :
void main()
{
int val=5;
printf("%d %d %d %d",val,--val,++val,val--);
}
(a)3 4 6 5 (b)5 5 6 5 (c)4 4 5 5 (d)None of these
Ans. (c) Since R->L priority.


[Q021]. What will be the output of the following program :
void main()
{
int val=5,num;
printf("%d",scanf("%d %d",&val,&num));
}
[NOTE : ASSUME 2 values are entered by the user are stored in the variables 'val' & 'num' respectively.]
(a)1 (b)2 (c)5 (d)None of these
Ans. (b) Since scanf statement returns the number of input fields successfully scanned, converted
& stored.


[Q022]. What will be the output of the following program :
#define Compute(x,y,z) (x+y-z)
void main()
{
int x=2,y=3,z=4;
printf("%d",Compute(y,z,(-x+y)) * Compute(z,x,(-y+z)));
}
(a)40 (b)30 (c)Compile-Time Error (d)None of these
Ans. (b) Since it is macro function. NOTE : Be careful while doing such type of calculations.


[Q023]. What will be the output of the following program :
void main()
{
int m=10,n=20;
printf("%d %d %d",m/* m-value */,/* n-value */n,m*/* Compute m*n */n);
}
(a)Run-Time Error (b)10 20 200 (c)Compile-Time Error (d)None of these
Ans. (b) Since comments /*...*/ are ignored by the compiler.


[Q024]. What will be the output of the following program :
void main()
{
int m=10,n=20;
/* printf("%d",m*n);
}
(a)VALID but No Output (b)VALID : Prints 200 (c)Compile-Time Error (d)None of these
Ans. (c) Since COMMENT statement not ended properly i.e */ is missing in the above program.


[Q025]. What will be the output of the following program :
void main()
{
int val=97;
"Printing..."+printf("%c",val);
}
(a)Printing...97 (b)97 (c)Compile-Time Error (d)a
Ans. (d) Since alphabet 'a' is the ASCII equivalent of 97.


[Q026]. What will be the output of the following program :
void main()
{
int val=5;
val=printf("C") + printf("Skills");
printf("%d",val);
}
(a)Skills5 (b)C1 (c)Compile-Time Error (d)CSkills7
Ans. (d) VALID Since 'printf' function return the no. of bytes output.


[Q027]. What will be the output of the following program :
void main()
{
char str[]="Test";
if ((printf("%s",str)) == 4)
printf("Success");
else
printf("Failure");
}
(a)TestFailure (b)TestSuccess (c)Compile-Time Error (d)Test
Ans. (b) VALID Since 'printf' function return the no. of bytes output.


[Q028]. What will be the output of the following program :
void main()
{
int val=5;
printf("%*d",val);
}
(a) 5 (b)5 (c)Compile-Time Error (d)None of these
Ans. (a) VALID Since '*' specifies the precision (i.e. the next argument in the precision). If no
precision is specified then the value itself will be the precision value. Thus it prints 5 BLANK
SPACES & then the value 5.


[Q029]. What will be the output of the following program :
void main()
{
int val=5;
printf("%d5",val);
}
(a)Compile-Time Error (b)5 (c)55 (d) 5
Ans. (c)


[Q030]. What will be the output of the following program :
void main()
}
int val=5;
printf("%d",5+val++);
{
(a)Compile-Time Error (b)5 (c)10 (d)11
Ans. (a) Since incorrect usage of pair of braces } and {. Correct usage : Each compound statement
should be enclosed within a pair of braces, i.e { and }.



[Q001]. What will be the output of the following program :
void main()
{
int val=1234;
int* ptr=&val;
printf("%d %d",++val,*ptr);
}
(a)1234 1234 (b)1235 1235 (c)1234 1235 (d)1235 1234
Ans. (d)


[Q002]. What will be the output of the following program :
void main()
{
int val=1234;
int* ptr=&val;
printf("%d %d",val,*ptr++);
}
(a)1234 1234 (b)1235 1235 (c)1234 1235 (d)1235 1234
Ans. (a)


[Q003]. What will be the output of the following program :
void main()
{
int val=1234;
int *ptr=&val;
printf("%d %d",val,++*ptr);
}
(a)1234 1234 (b)1235 1235 (c)1234 1235 (d)1235 1234
Ans. (b)


[Q004]. What will be the output of the following program :
void main()
{
int val=1234;
int *ptr=&val;
printf("%d %d",val,(*ptr)++);
}
(a)1234 1234 (b)1235 1235 (c)1234 1235 (d)1235 1234
Ans. (d)


[Q005]. What will be the output of the following program :
void main()
{
int val=1234;
int *ptr=&val;
printf("%d %d",++val,(*(int *)ptr)--);
}
(a)1234 1233 (b)1235 1234 (c)1234 1234 (d)None of these
Ans. (c)


[Q006]. What will be the output of the following program :
void main()
{
int a=555,*ptr=&a,b=*ptr;
printf("%d %d %d",++a,--b,*ptr++);
}
(a)Compile-Time Error (b)555 554 555 (c)556 554 555 (d)557 554 555
Ans. (c)


[Q007]. What will be the output of the following program :
void main()
{
int a=555,b=*ptr,*ptr=&a;
printf("%d %d %d",++a,--b,*ptr++);
}
(a)Compile-Time Error (b)555 554 555 (c)556 554 555 (d)557 554 555
Ans. (a)


[Q008]. What will be the output of the following program :
void main()
{
int a=555,*ptr=&a,b=*ptr;
printf("%d %d %d",a,--*&b,*ptr++);
}
(a)Compile-Time Error (b)555 554 555 (c)556 554 555 (d)557 554 555
Ans. (b)


[Q009]. What will be the output of the following program :
void main()
{
int a=555,*ptr=&a,b=*ptr=777;
printf("%d %d",--*&b,*(int *)&b);
}
(a)Compile-Time Error (b)776 777 (c)554 555 (d)None of these
Ans. (b)


[Q010]. What will be the output of the following program :
void main()
{
int a=5u,*b,**c,***d,****e;
b=&a;
c=&b;
d=&c;
e=&d;
printf("%u %u %u %u",*b-5,**c-11,***d-6,65535+****e);
}
(a)Compile-Time Error (b)0 65530 65535 4 (c)0 65530 65535 65539 (d)0 -6 -1 -2
Ans. (b)


[Q011]. What will be the output of the following program :
void main()
{
float val=5.75;
int *ptr=&val;
printf("%.2f %.2f",*(float *)ptr,val);
}
(a)Compile-Time Error (b)5.75 5.75 (c)5.00 5.75 (d)None of these
Ans. (b)


[Q012]. What will be the output of the following program :
void main()
{
int val=50;
const int *ptr1=&val;
int const *ptr2=ptr1;
printf("%d %d %d",++val,*ptr1,*ptr2);
*(int *)ptr1=98;
printf("\n%d %d %d",++val,*ptr1,*ptr2);
}
(a)Compile-Time Error (b)51 50 50 (c)Run-Time Error (d)None of these
99 98 98
Ans. (d)


[Q013]. What will be the output of the following program :
void main()
{
int val=77;
const int *ptr1=&val;
int const *ptr2=ptr1;
printf("%d %d %d",--val,(*ptr1)++,*ptr2);
}
(a)Compile-Time Error (b)77 78 77 (c)76 77 77 (d)77 77 77
Ans. (a)


[Q014]. What will be the output of the following program :
int main()
{
int a=50,b=60;
int* const ptr1=&a;
printf("%d %d",--a,(*ptr1)++);
ptr1=&b;
printf("\n%d %d",++b,(*ptr1)++);
}
(a)Compile-Time Error (b)49 50 (c)50 50 (d)None of these
61 60 62 60
Ans. (a)


[Q015]. What will be the output of the following program :
void main()
{
int a=50;
const int* const ptr=&a;
printf("%d %d",*ptr++,(*ptr)++);
}
(a)Compile-Time Error (b)51 51 (c)51 50 (d)None of these
Ans. (a)


[Q016]. What will be the output of the following program :
void main()
{
int val=77;
const int const *ptr=&val;
printf("%d",*ptr);
}
(a)Compile-Time Error (b)Run-Time Error (c)77 (d)None of these
Ans. (c)


[Q017]. What will be the output of the following program :
void main()
{
int a[]={1,2,3,4,5,6};
int *ptr=a+2;
printf("%d %d",--*ptr+1,1+*--ptr);
}
(a)Compile-Time Error (b)1 2 (c)2 3 (d)1 3
Ans. (c)


[Q018]. What will be the output of the following program :
void main()
{
int a[]={1,2,3,4,5,6};
int *ptr=a+2;
printf("%d %d",*++a,--*ptr);
}
(a)Compile-Time Error (b)2 2 (c)3 2 (d)4 2
Ans. (b)


[Q019]. What will be the output of the following program :
void main()
{
int matrix[2][3]={{1,2,3},{4,5,6}};
printf("%d %d %d\n",*(*(matrix)),*(*(matrix+1)+2),*(*matrix+1));
printf("%d %d %d",*(matrix[0]+2),*(matrix[1]+1),*(*(matrix+1)));
}
(a)Compile-Time Error (b)1 5 2 (c)1 6 2 (d)1 6 2
6 3 4 3 5 4 3 4 5
Ans. (c)


[Q020]. What will be the output of the following program :
void main()
{
int (*a)[5];
printf("%d %d",sizeof(*a),sizeof(a));
}
(a)Compile-Time Error (b)2 5 (c)5 2 (d)None of these
Ans. (d)


[Q001]. Write a program (W.A.P.) in C to SWAP the contents of 3 variables without using the
temporary (or extra) variables.

Ans.

/* Swapping 3 numbers without using extra variable */
#include
#include
void Swap(int *a,int *b,int *c)
{
*a = *a + *b + *c;
*b = *a - (*b + *c);
*c = *a - (*b + *c);
*a = *a - (*b + *c);
}
void main()
{
int x=1,y=2,z=3;
clrscr();
printf("BEFORE SWAPPING : %d %d %d\n",x,y,z);
Swap(&x,&y,&z);
printf("AFTER SWAPPING : %d %d %d",x,y,z);
} /* End of Main */


[Q002]. W.A.P. in C to find the Fifth root of the sum of the squares of the first 100 ODD numbers
only.

Ans.

/* To find the Fifth root of the sum of the squares of the first 100 ODD numbers ONLY */
#include
#include
#include
void main(void)
{
long i,oddnum=1,sqrnum,sum=0;
clrscr();
for (i=1; i<=100; i++)
{
sqrnum=oddnum * oddnum; // Square the ODD number
sum+=sqrnum; // Add Square value to the sum
oddnum+=2; // Get the next ODD number
}
printf("\nThe result is : %ld,%.2f",sum,pow((double)sum,(1.0/5.0)));
} /* End of Main */


[Q003]. W.A.P. in C to multiply any two numbers without using * (asterisk) and other arithmetic
operators like +, -, / and %. [Hint : Use BITWISE OPERATORS]

Ans.

/* Multiplication of two numbers using BITWISE OPERATORS ONLY */
#include
void main()
{
long int i,n,mul,mul2,count,temp,a,b,sum,carry,res,tot;
clrscr();
printf("\nEnter any 2 numbers : ");
scanf("%ld %ld",&mul,&n);
mul2=temp=mul;
for (i=2; i<=n; i++)
{
temp=mul;
count=32;
res=1;
tot=sum=carry=0;
while (count--)
{
a=temp & 0x1;
b=mul2 & 0x1;
if ((a^b==1) && (carry==1))
{
sum=(a^b)^carry;
carry=(a^b)&carry;
}
else
{
sum=a^b|carry;
carry=a&b;
}
temp=temp>>1;
mul2=mul2>>1;
tot+=res*sum;
res=res*2;
}
mul2=tot;
}
printf("\n%3ld * %3ld = %3ld",mul,i-1,tot);
getch();
} /* End of Main */


[Q004]. W.A.P. in C to check whether given number x is equal to the value 2 POWER i or something,
where i>=0 using BITWISE operators ONLY.
[Hint : Check whether the given number x is equal to the value 2 POWER i or something using
BITWISE operators ONLY]

Ans.

/* Check whether the given number x is equal to the value 2 power i or not using BITWISE
operators ONLY */
#include
#include
void main(void)
{
long x;
clrscr();
printf("Enter a number : ");
scanf("%ld",&x);
if ((x & 0x1) == 0x0)
printf("The given number %ld is EQUAL to the value 2 POWER something",x);
else
printf("The given number %ld is NOT EQUAL to the value 2 POWER something",x);
getch();
} /* End of Main */


[Q005]. W.A.P. in C to maintain 2 STACKS within a SINGLE ARRAY and the values of one stack should
not overwrite the values of another stack.

Ans.

/* Maintaining TWO STACKS within a SINGLE ARRAY */
#include
#include
#define MAX 10
int stack[MAX],top1,top2;
void init()
{
top1=-1;
top2=10;
}
void Push1(int item)
{
stack[++top1]=item;
}
void Push2(int item)
{
stack[--top2]=item;
}
int Pop1()
{
return stack[top1--];
}
int Pop2()
{
return stack[top2++];
}
void Display()
{
int i;
if(top1==-1)
printf("\nStack1 : Empty");
else
{
printf("\nContent of Stack1 :\n");
for(i=0;i<=top1;i++)
printf("%d\t",stack[i]);
}
if(top2==10)
printf("\nStack2 : Empty");
else
{
printf("\nStack2 contains:\n");
for(i=MAX-1;i>=top2;i--)
printf("%d\t",stack[i]);
}
}
void main()
{
int item,ch;
clrscr();
init();
while(1)
{
printf("\n\n\tMenu\n1.Push1\n2.Push2\n3.Pop1\n4.Pop2\n5.Display\n6.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : if ((top1 + 1) < top2)
{
printf("\nEnter item to Push into Stack1:");
scanf("%d",&item);
Push1(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;

case 2 : if ((top2 - 1) > top1)
{
printf("\nEnter item to Push into Stack2:");
scanf("%d",&item);
Push2(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;

case 3 : if(top1 <= -1)
printf("\nError : Underflow on pop1");
else
printf("\nPopped item from stack1 is : %d",Pop1());
break;

case 4 : if(top2 >= 10)
printf("\nError : Underflow on pop2");
else
printf("\nPopped item from stack2 is : %d",Pop2());
break;

case 5 : Display();
break;

case 6 : exit(0);

default: printf("\nInvalid Choice");
}
}
} /* End of Main */


[Q006]. W.A.P. in C that act as a guessing game in which the user has eight tries to guess a
randomly generated number. The program will tell the user each time whether he guessed high or
low. The user WINS the game when the number guessed is same as randomly generated number.

Ans.

/* Guessing Game Solution */
#include
#include
#include
int main(void)
{
int i=8,rval,val,flag=1;
randomize(); // Initialize the random number generator
rval=random (100); // Generates a random number in the range 0 to 99
printf("Welcome to Guessing Game.\n");
printf("RULES:\n1. Only 8 chances to guess the randomly generated number.");
printf("\n2. You can WIN the game when the number guessed is same as the randomly generated number.");
printf("\n3. Hints will be provided during the PLAY.");
printf("\n\n$$$ Good Luck. Start Guessing $$$");
for (i=1; i<=8; i++)
{
printf("\n\nGUESS %d ? ",i);
scanf("%d",&val);
if (val > rval)
printf("Your value is GREATER THAN the randomly generated number");
else if (val < rval)
printf("Your value is LESSER THAN the randomly generated number");
else
{
flag=1;
break;
}
}
if (flag)
printf("\n\n*** You are the WINNER. No. of tries = %d ***",i);
else
printf("\n\n*** You are the LOSER. ***");
} /* End of Main */


[Q007]. W.A.P. to determine how much money is in a piggy bank that contains several 50 paise
coins, 25 paise coins, 20 paise coins, 10 paise coins and 5 paise coins. Use the following values
to test your program : Five 50 paise coins, Three 25 paise coins, Two 20 paise coins,
One 10 paise coin and Fifteen 5 paise coins. (Answer : Rs. 4.50)

Ans.

/* To determine how much money in a piggy bank */
#include
#include
#include
void main(void)
{
float coin1=0.50,coin2=0.25,coin3=0.20,coin4=0.10,coin5=0.05,total=0.0;
int ncoins;
clrscr();
printf("How many 50 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin1);
printf("** %.2f **",total);

printf("\nHow many 25 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin2);
printf("** %.2f **",total);

printf("\nHow many 20 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin3);
printf("** %.2f **",total);

printf("\nHow many 10 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin4);
printf("** %.2f **",total);

printf("\nHow many 5 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin5);

printf("\n\nThe total amount is Rs.%.2f",total);
getch();
} /* End of Main */


[Q008]. Modify the program given in [Q007] to accept total amount (in rupees) and convert them
into paise.(Vice-versa of [Q007])

Ans.

/* Denominations */
#include
#include
#include
void main(void)
{
int nc1,nc2,nc3,nc4,nc5,temp;
float total;
clrscr();
printf("Enter the amount : ");
scanf("%f",&total);
temp = total * 100;
nc1 = temp / 50;
temp = temp % 50;

nc2 = temp / 25;
temp = temp % 25;

nc3 = temp / 20;
temp = temp % 20;

nc4 = temp / 10;
temp = temp % 10;

nc5=temp;

printf("\n\nNo. of 50 paise coins = %d",nc1);
printf("\nNo. of 25 paise coins = %d",nc2);
printf("\nNo. of 20 paise coins = %d",nc3);
printf("\nNo. of 10 paise coins = %d",nc4);
printf("\nNo. of 5 paise coins = %d",nc5);
getch();
} /* End of Main */


[Q009]. W.A.P. in C to determine how many of the characters are vowels and how many are
consonants in a given line of text. Also terminate the string when the input character
encountered is other than the alphabets(a-z or A-Z) and Blank spaces.
[Hint:(a) When the input string is 'C FOR SWIMMERS, TEST YOUR C PROGRAMMING STRENGTHS'. Consider
the string 'C FOR SWIMMERS' only Because ',' is encountered.
(b) When the input string is 'Y2K PROBLEM'. Consider the character 'Y' only Because the
'2' is encountered.]

Ans.

/* Counting vowels and consonants in a given line of text */
#include
#include
#include
void main(void)
{
char *str;
int i,vow=0,cons=0;
clrscr();
printf("Enter a string : ");
scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",str);
for (i = 0; i < strlen(str); i++)
if (str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U' || str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
vow++;
else if (str[i] != ' ') // Ignore BLANK characters
cons++;
printf("\n\nThe given string is %s",str);
printf("\n\nThe total number of VOWELS in a given string is %d",vow);
printf("\nThe total number of CONSONANTS in a given string is %d",cons);
} /* End of Main */


[Q010]. W.A.P. in C to perform 4-letter WORD UNSCRAMBLING i.e. List all possible combinations of
4-letters in a word. Ex: The word 'TEST' can be unscrambled as TEST,TETS,TSET,TSTE,TTSE,TTES,etc.

Ans.

/* 4-letter word unscrambling */
#include
#include
#include
void main()
{
int i,j,k,l,sum=6;
char *str;
clrscr();
printf("Enter a 4-letter word or string : ");
scanf("%s",str);
if (strlen(str) == 4)
{
printf("The possible combinations of the given 4-letter word is shown.");
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (i != j)
{
for (k = 0; k < 4; k++)
if ((k != i) && (k != j))
{
l = sum - (i + j + k);
printf("\n%c%c%c%c",str[i],str[j],str[k],str[l]);
}
}
printf("\nTotal combinations = %d",4*3*2*1);
}
else
printf("\nInvalid string. Length of the string must be 4-letters only ");
getch();
} /* End of Main */


[Q001]. Write a program (W.A.P.) in C to clear the screen and print C FOR SWIMMERS on each line,
forming a diagonal pattern running from upper-left to lower right.[Use suitable delay]

Ans.

/* Print C FOR SWIMMERS on each line, forming a diagonal pattern */
#include
#include
#include
void main()
{
char str[]="C For Swimmers";
int x=1,y;
for (y=1; y<=25; y++)
{
clrscr();
gotoxy(x,y);
printf("%s",str);
delay(250);
if (y < 8)
x+=2;
else
x+=3;
}
} /* End of main */


*[Q002]. W.A.P. in C to SORT the array of strings based on first 3-LETTERS ONLY.


[Q003]. Some C Functions take variable argument list in addition to taking a number of fixed
(known) parameters. Implement using USER-DEFINED C FUNCTION that take VARIABLE ARGUMENT LIST and
COMPUTE THE SUM OF VALUES specified in the list.
Ex: int Sum(int a,...) // Here ... means VARIABLE ARGUMENT LIST
{
// Your Code Goes Here to access the values in the variable argument list
}
void main(void)
{
printf("%d",Sum(5,6,7,8,9,10)); // Prints the RESULT (Sum of these values=45)
}

Ans.

/* Compute the sum of values in the variable argument list */
#include
#include
int Sum(int a,...)
{
int total=a;
va_list ap;
int arg;
va_start(ap,a);
while ((arg = va_arg(ap,int)) != 0)
{
total+=arg;
}
va_end(ap);
return total;
}
void main()
{
clrscr();
printf("%d",Sum(5,6,7,8,9,10));
getch();
} /* End of main */


[Q004]. W.A.P. in C to compute the sum of two values using function that takes two arguments
(INTEGERS) and IT SHOULD RETURN the sum WITHOUT USING the RETURN statement i.e return type of the
function is INTEGER ('int' data type).
[NOTE : DO NOT MAKE USE OF ANY GLOBAL VARIABLES OR POINTER CONCEPT]

Ans.

// Compute the sum of two values using function and return the result w/o using RETURN statement
#include
#include
int Sum(int a,int b)
{
_AX = a + b; // equivalent to : return (a + b);
}
void main(void)
{
clrscr();
printf("Result is = %d",Sum(4,6));
getch();
} /* End of main */


[Q005]. W.A.P. in C to SWAP the contents of TWO VARIABLES WITHOUT using ASSIGNMENT OPERATOR.
[HINT : USE 'asm' statement]

/* To swap the contents of two variables using 'asm' statement */
#include
#include
void main()
{
int val=85,num=77;
clrscr();
printf("Before swapping : %d and %d",val,num);
asm mov ax,val
asm mov cx,num
asm mov num,ax
asm mov val,cx
printf("\n\nAfter swapping : %d and %d",val,num);
} /* End of main */


[Q006]. W.A.P. in C to FIND THE LARGEST of two numbers (integer) WITHOUT using ?: operator, if,
if...else and switch statements.
[HINT : USE 'asm' statement]

/* To find largest of two numbers using 'asm' statement */
#include
#include
void main()
{
int val=85,num=77;
clrscr();
asm mov ax,val
asm cmp ax,num
asm jg FirstNum
asm mov ax,num
FirstNum:printf("Largest of two numbers is %d",_AX);
} /* End of main */


[Q007]. W.A.P. in C to print the following output WITHOUT USING GOTO STATEMENT, CONDITIONAL
STATEMENTS (if, if...else & switch statements) and ANY LOOP STATEMENTS (for, while & do..while) :

(a)
*****
****
***
**
*

/* To print the above pattern or output using asm statement */
#include
#include
void main(void)
{
int i=5,j;
clrscr();
// asm statement doesn't end with semicolon (starts with 'asm' keyword)
asm mov ax,i // Move the value of 'i' to accumulator (ax)
Loopi: j=i;
Loopj: printf("*"); // Print asterisk (*)
j=j-1;
asm mov cx,j // Decrement the counter : Inner Loop
asm cmp cx,0
asm jnz Loopj // Terminate the Inner Loop if counter is 0, otherwise goto Loopj
i=i-1;
printf("\n");
asm mov ax,i // Decrement the counter : Outer Loop
asm cmp ax,0
asm jnz Loopi // Terminate the Outer Loop if counter is 0, otherwise goto Loopi
getch();
} /* End of main */


(b)
*
**
***
****
*****

/* To print the above pattern or output using asm statement */
#include
#include
void main(void)
{
int i=1,j;
clrscr();

// asm statement doesn't end with semicolon (starts with 'asm' keyword)
asm mov ax,i // Move the value of 'i' to accumulator (ax)
Loopi:j=0;
Loopj:printf("*"); // Print asterisk (*)
j=j+1;
asm mov cx,j // Increment the counter : Inner Loop
asm cmp cx,i
asm jnz Loopj // Terminate the Inner Loop if Inner Loop counter = Outer Loop counter
i=i+1;
printf("\n");
asm mov ax,i // Increment the counter : Outer Loop
asm cmp ax,6
asm jnz Loopi // Terminate the Outer Loop if counter = 6

getch();
} /* End of main */

[HINT : USE 'asm' statement]


[Q008]. W.A.P. in C to REVERSE THE WORDS IN A GIVEN LINE OF TEXT.
[HINT : USE Built-in function 'strtok' in 'string.h' header file]

Ans.

/* To reverse the words in a given line of text */
#include
#include
void main()
{
char ch,str[80],*res="",*temp,*temp1;
clrscr();
printf("Enter a sentence :\n");
scanf("%[^\n]",str);
temp=strtok(str," ");
do
{
strcpy(temp1,temp);
strcat(temp1," ");
strcat(temp1,res);
strcpy(res,temp1);
temp=strtok(NULL," ");
}while (temp != NULL);

printf("\n Result : %s",res);
getch();
} /* End of main */


*[Q009]. W.A.P. in C to CONVERT THE TEXT within /* ... */ to UPPER-CASE (all occurrences) from the
SPECIFIED C Source Code File and save the file without altering the remaining text.


[Q010]. W.A.P. in C to READ a line of text and WRITE it out BACKWARDS using RECURSIVE Function.

Ans.

/* To reverse a given line of test using recursive function */
#include
#include
void reverse(void)
{
char c;
if ((c = getchar()) != '\n')
reverse();
putchar(c);
return;
}
void main()
{
clrscr();
printf("Please enter a line of text below\n");
reverse();
} /* End of main */



[Q001]. What will be the output of the following program :
struct {
int i;
float f;
}var;
void main()
{
var.i=5;
var.f=9.76723;
printf("%d %.2f",var.i,var.f);
}
(a)Compile-Time Error (b)5 9.76723 (c)5 9.76 (d)5 9.77
Ans. (d) Though both and are optional, one of the two
must appear. In the above program, i.e. var is used. (2 decimal places or)
2-digit precision of 9.76723 is 9.77


[Q002]. What will be the output of the following program :
struct {
int i;
float f;
};
void main()
{
int i=5;
float f=9.76723;
printf("%d %.2f",i,f);
}
(a)Compile-Time Error (b)5 9.76723 (c)5 9.76 (d)5 9.77
Ans. (d) Both and are optional. Thus the structure
defined in the above program has no use and program executes in the normal way.


[Q003]. What will be the output of the following program :
struct values{
int i;
float f;
};
void main()
{
struct values var={555,67.05501};
printf("%2d %.2f",var.i,var.f);
}
(a)Compile-Time Error (b)55 67.05 (c)555 67.06 (d)555 67.05
Ans. (c) The members of a structure variable can be assigned initial values in much the same
manner as the elements of an array. The initial values must appear in order in which they will be
assigned to their corresponding strucutre members, enclosed in braces and separated by commas.


[Q004]. What will be the output of the following program :
typedef struct {
int i;
float f;
}values;
void main()
{
static values var={555,67.05501};
printf("%2d %.2f",var.i,var.f);
}
(a)Compile-Time Error (b)55 67.05 (c)555 67.06 (d)555 67.05
Ans. (c) In the above program, values is the user-defined structure type or the new user-defined
data type. Structure variables can then be defined in terms of the new data type.


[Q005]. What will be the output of the following program :
struct my_struct{
int i=7;
float f=999.99;
}var;
void main()
{
var.i=5;
printf("%d %.2f",var.i,var.f);
}
(a)Compile-Time Error (b)7 999.99 (c)5 999.99 (d)None of these
Ans. (a) C language does not permit the initialization of individual structure members within the
template. The initialization must be done only in the declaration of the actual variables. The
correct way to initialize the values is shown in [Q003] or [Q004].


[Q006]. What will be the output of the following program :
struct first{
int a;
float b;
}s1={32760,12345.12345};
typedef struct{
char a;
int b;
}second;
struct my_struct{
float a;
unsigned int b;
};
typedef struct my_struct third;
void main()
{
static second s2={'A',- -4};
third s3;
s3.a=~(s1.a-32760);
s3.b=-++s2.b;
printf("%d %.2f\n%c %d\n%.2f %u",(s1.a)--,s1.b+0.005,s2.a+32,s2.b,++(s3.a),--s3.b);
}
(a)Compile-Time Error (b)32760 12345.12 (c)32760 12345.13 (d)32760 12345.13
A 4 a -5 a 5
1 -5 0.00 65531 0.00 65530
Ans. (d) Illustrating 3 different ways of declaring the structres : first, second and third are
the user-defined structure type. s1, s2 and s3 are structure variables. Also an expression of the
form ++variable.member is equivalent to ++(variable.member), i.e. ++ operator will apply to the
structure member, not the entire structure variable.


[Q007]. What will be the output of the following program :
struct {
int i,val[25];
}var={1,2,3,4,5,6,7,8,9},*vptr=&var;
void main()
{
printf("%d %d %d\n",var.i,vptr->i,(*vptr).i);
printf("%d %d %d %d %d %d",var.val[4],*(var.val+4),vptr->val[4],*(vptr->val+4),(*vptr).val[4],*((*vptr).val+4));
}
(a)Compile-Time Error (b)1 1 1 (c)1 1 1 (d)None of these
6 6 6 6 6 6 5 5 5 5 5 5
Ans. (b) Since value of the member 'i' can be accessed using var.i, vptr->i and (*vptr).i
Similarly 5th value of the member 'val' can be accessed using var.val[4], *(var.val+4),
vptr->val[4], *(vptr->val+4), (*vptr).val[4] and *((*vptr).val+4)
________________________________________________________________________________________________

[Q008]. What will be the output of the following program :
typedef struct {
int i;
float f;
}temp;
void alter(temp *ptr,int x,float y)
{
ptr->i=x;
ptr->f=y;
}
void main()
{
temp a={111,777.007};
printf("%d %.2f\n",a.i,a.f);
alter(&a,222,666.006);
printf("%d %.2f",a.i,a.f);
}
(a)Compile-Time error (b)111 777.007 (c)111 777.01 (d)None of these
222 666.006 222 666.01
Ans. (c) This program illustrates the transfer of a structure to a function by passing the
structure's address (a pointer) to the function.


[Q009]. What will be the output of the following program :
typedef struct {
int i;
float f;
}temp;
temp alter(temp tmp,int x,float y)
{
tmp.i=x;
tmp.f=y;
return tmp;
}
void main()
{
temp a={111,777.007};
printf("%d %.3f\n",a.i,a.f);
a=alter(a,222,666.006);
printf("%d %.3f",a.i,a.f);
}
(a)Compile-Time error (b)111 777.007 (c)111 777.01 (d)None of these
222 666.006 222 666.01
Ans. (b) This program illustrates the transfer of a structure to a function by value. Also the
altered structure is now returned directly to the calling portion of the program.


[Q010]. What will be the output of the following program :
typedef struct {
int i;
float f;
}temp;
temp alter(temp *ptr,int x,float y)
{
temp tmp=*ptr;
printf("%d %.2f\n",tmp.i,tmp.f);
tmp.i=x;
tmp.f=y;
return tmp;
}
void main()
{
temp a={65535,777.777};
a=alter(&a,-1,666.666);
printf("%d %.2f",a.i,a.f);
}
(a)Compile-Time error (b)65535 777.777 (c)65535 777.78 (d)-1 777.78
-1 666.666 -1 666.67 -1 666.67
Ans. (d) This program illustrates the transfer of a structure to a function by passing the
structure's address (a pointer) to the function. Also the altered structure is now returned
directly to the calling portion of the program.


[Q011]. What will be the output of the following program :
struct my_struct1{
int arr[2][2];
};
typedef struct my_struct1 record;
struct my_struct2{
record temp;
}list[2]={1,2,3,4,5,6,7,8};
void main()
{
int i,j,k;
for (i=1; i>=0; i--)
for (j=0; j<2; j++)
for (k=1; k>=0; k--)
printf("%d",list[i].temp.arr[j][k]);
}
(a)Compile-Time Error (b)Run-Time Error (c)65872143 (d)56781243
Ans. (c) This program illustrates the implementation of a nested structure i.e. structure inside
another structure.


[Q012]. What will be the output of the following program :
struct my_struct{
int i;
unsigned int j;
};
void main()
{
struct my_struct temp1={-32769,-1},temp2;
temp2=temp1;
printf("%d %u",temp2.i,temp2.j);
}
(a)32767 -1 (b)-32769 -1 (c)-32769 65535 (d)32767 65535
Ans. (d) An entire structure variable can be assigned to another structure variable, provided
both variables have the same composition.


[Q013]. What will be the output of the following program :
struct names {
char str[25];
struct names *next;
};
typedef struct names slist;
void main()
{
slist *list,*temp;
list=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(list->str,"Hai");
list->next=NULL;
temp=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(temp->str,"Friends");
temp->next=list;
list=temp;
while (temp != NULL)
{
printf("%s",temp->str);
temp=temp->next;
}
}
(a)Compile-Time Error (b)HaiFriends (c)FriendsHai (d)None of these
Ans. (c) It is sometimes desirable to include within a structure one member i.e. a pointer to the
parent structure type. Such structures are known as Self-Referencial structures. These structures
are very useful in applications that involve linked data structures, such as lists and trees.
[A linked data structure is not confined to some maximum number of components. Rather, the data
structure can expand or contract in size as required.]


[Q014]. Which of the following declarations is NOT Valid :
(i) struct A{
int a;
struct B {
int b;
struct B *next;
}tempB;
struct A *next;
}tempA;

(ii) struct B{
int b;
struct B *next;
};
struct A{
int a;
struct B tempB;
struct A *next;
};

(iii)struct B{
int b;
}tempB;
struct {
int a;
struct B *nextB;
};


(iv) struct B {
int b;
struct B {
int b;
struct B *nextB;
}tempB;
struct B *nextB;
}tempB;
(a) (iv) Only (b) (iii) Only (c)All of the these (d)None of these
Ans. (d) Since all the above structure declarations are valid in C.


[Q015]. What will be the output of the following program :
union A{
char ch;
int i;
float f;
}tempA;
void main()
{
tempA.ch='A';
tempA.i=777;
tempA.f=12345.12345;
printf("%d",tempA.i);
}
(a)Compile-Time Error (b)12345 (c)Erroneous output (d)777
Ans. (c) The above program produces erroneous output (which is machine dependent). In effect,
a union creates a storage location that can be used by any one of its members at a time. When a
different member is assigned a new value, the new value supercedes the previous member's value.
[NOTE : The compiler allocates a piece of storage that is large enough to hold the largest
variable type in the union i.e. all members share the same address.]


[Q016]. What will be the output of the following program :
struct A{
int i;
float f;
union B{
char ch;
int j;
}temp;
}temp1;
void main()
{
struct A temp2[5];
printf("%d %d",sizeof temp1,sizeof(temp2));
}
(a)6 30 (b)8 40 (c)9 45 (d)None of these
Ans. (b) Since int (2 bytes) + float (4 bytes) = (6 bytes) + Largest among union is int (2 bytes)
is equal to (8 bytes). Also the total number of bytes the array 'temp2' requires :
(8 bytes) * (5 bytes) = (40 bytes).


[Q017]. What will be the output of the following program :
void main()
{
static struct my_struct{
unsigned a:1;
unsigned b:2;
unsigned c:3;
unsigned d:4;
unsigned :6; // Fill out first word
}v={1,2,7,12};
printf("%d %d %d %d",v.a,v.b,v.c,v.d);
printf("\nSize=%d bytes",sizeof v);
}
(a)Compile-Time Error (b)1 2 7 12 (c)1 2 7 12 (d)None of these
Size=2 bytes Size=4 bytes
Ans. (b) The four fields within 'v' require a total of 10 bits and these bits can be accomodated
within the first word(16 bits). Unnamed fields can be used to control the alignment of bit fields
within a word of memory. Such fields provide padding within the word.
[NOTE : Some compilers order bit-fields from righ-to-left (i.e. from lower-order bits to high-
order bits) within a word, whereas other compilers order the fields from left-to-right (high-
order to low-order bits).


[Q018]. What are the largest values that can be assigned to each of the bit fields defined in
[Q017] above.
(a)a=0 b=2 c=3 d=4 (b)a=1 b=2 c=7 d=15 (c)a=1 b=3 c=7 d=15 (d)None of thes
Ans. (c)a=1 (1 bit: 0 or 1)
b=3 (2 bits: 00 or 01 or 10 or 11),
c=7 (3 bits: 000 or 001 or 010 or 011 or 100 or 101 or 110 or 111)
d=15 (4 bits: 0000 or 0001 or 0010 or 0011 or 0100 or 0101 or 0110 or 0111 or 1000 or
1001 or 1010 or 1011 or 1100 or 1101 or 1110 or 1111)


[Q019]. What will be the output of the following program :

void main()
{
struct sample{
unsigned a:1;
unsigned b:4;
}v={0,15};
unsigned *vptr=&v.b;
printf("%d %d",v.b,*vptr);
}
(a)Compile-Time Error (b)0 0 (c)15 15 (d)None of these
Ans. (a) Since we cannot take the address of a bit field variable i.e. Use of pointer to access
the bit fields is prohibited. Also we cannot use 'scanf' function to read values into a bit field
as it requires the address of a bit field variable. Also array of bit-fields are not permitted
and a function cannot return a bit field.


[Q020]. What will be the output of the following program :
void main()
{
static struct my_struct{
unsigned a:1;
int i;
unsigned b:4;
unsigned c:10;
}v={1,10000,15,555};
printf("%d %d %d %d",v.i,v.a,v.b,v.c);
printf("\nSize=%d bytes",sizeof v);
}
(a)Compile-Time Error (b)1 10000 15 555 (c)10000 1 15 555 (d)10000 1 15 555
Size=4 bytes Size=4 bytes Size=5 bytes
Ans. (d) Here the bit field variable 'a' will be in first byte of one word, the variable 'i' will
be in the second word and the bit fields 'b' and 'c' will be in the third word. The variables
'a', 'b' and 'c' would not get packed into the same word. [NOTE: one word=2 bytes]





கருத்துகள் இல்லை:

கருத்துரையிடுக