C, C++ Questions: Ans. B
C, C++ Questions: Ans. B
com
C,C++Questions
1.Baseclasshassomevirtualmethodandderivedclasshasamethodwiththesamename.
Ifweinitializethebaseclasspointerwithderived
object,.callingofthatvirtualmethodwillresultinwhichmethodbeingcalled?
a.Basemethod
b.Derivedmethod..
Ans.b
2.ForthefollowingCprogram
#defineAREA(x)(3.14*x*x)
main()
{floatr1=6.25,r2=2.5,a;
a=AREA(r1);
printf("\nAreaofthecircleis%f",a);
a=AREA(r2);
printf("\nAreaofthecircleis%f",a);
}
Whatistheoutput?
Ans.Areaofthecircleis122.656250
Areaofthecircleis19.625000
3.Whatdothefollowingstatementsindicate.Explain.
int(*p)[10]
int*f()
int(*pf)()
Freshersworld.comResourceCenter
FirstJob.DreamJob.Freshersworld.com
int*p[10]
Referto:
Kernighan&Ritchiepageno.122
Schaumseriespageno.323
4.
voidmain()
{
intd=5;
printf("%f",d);
}
Ans:Undefined
5.
voidmain()
{
inti;
for(i=1;i<4,i++)
switch(i)
case1:printf("%d",i);break;
{
case2:printf("%d",i);break;
case3:printf("%d",i);break;
}
switch(i)case4:printf("%d",i);
}
Ans:1,2,3,4
6.
Freshersworld.comResourceCenter
FirstJob.DreamJob.Freshersworld.com
voidmain()
{
char*s="\12345s\n";
printf("%d",sizeof(s));
}
Ans:6
7.
voidmain()
{
unsignedi=1;/*unsignedchark=1=>k=255;*/
signedj=1;/*chark=1=>k=65535*/
/*unsignedorsignedintk=1=>k=65535*/
if(i<j)
printf("less");
else
if(i>j)
printf("greater");
else
if(i==j)
printf("equal");
}
Ans:less
8.
voidmain()
{
floatj;
j=1000*1000;
printf("%f",j);
}
1.1000000
Freshersworld.comResourceCenter
FirstJob.DreamJob.Freshersworld.com
2.Overflow
3.Error
4.None
Ans:4
9.HowdoyoudeclareanarrayofNpointerstofunctionsreturning
pointerstofunctionsreturningpointerstocharacters?
Ans:Thefirstpartofthisquestioncanbeansweredinatleast
threeways:
1.char*(*(*a[N])())();
2.Buildthedeclarationupincrementally,usingtypedefs:
typedefchar*pc;/*pointertochar*/
typedefpcfpc();/*functionreturningpointertochar*/
typedeffpc*pfpc;/*pointertoabove*/
typedefpfpcfpfpc();/*functionreturning...*/
typedeffpfpc*pfpfpc;/*pointerto...*/
pfpfpca[N];/*arrayof...*/
3.Usethecdeclprogram,whichturnsEnglishintoCandvice
versa:
cdecl>declareaasarrayofpointertofunctionreturning
pointertofunctionreturningpointertochar
char*(*(*a[])())()
cdeclcanalsoexplaincomplicateddeclarations,helpwith
casts,andindicatewhichsetofparenthesesthearguments
goin(forcomplicatedfunctiondefinitions,liketheone
above).
AnygoodbookonCshouldexplainhowtoreadthesecomplicated
Cdeclarations"insideout"tounderstandthem("declaration
mimicsuse").
Thepointertofunctiondeclarationsintheexamplesabovehave
notincludedparametertypeinformation.Whentheparameters
havecomplicatedtypes,declarationscan*really*getmessy.
(Modernversionsofcdeclcanhelphere,too.)
10.Astructurepointerisdefinedofthetypetime.With3fieldsmin,sechourshavingpointers
tointergers.
Writethewaytoinitializethe2ndelementto10.
Freshersworld.comResourceCenter
FirstJob.DreamJob.Freshersworld.com
11.Intheabovequestionanarrayofpointersisdeclared.
Writethestatementtoinitializethe3rdelementofthe2elementto10;
12.
intf()
voidmain()
{
f(1);
f(1,2);
f(1,2,3);
}
f(inti,intj,intk)
{
printf("%d%d%d",i,j,k);
}
Whatarethenumberofsyntaxerrorsintheabove?
Ans:None.
13.
voidmain()
{
inti=7;
printf("%d",i++*i++);
}
Ans:56
14.
#defineone0
#ifdefone
printf("oneisdefined");
#ifndefone
printf("oneisnotdefined");
Ans:"oneisdefined"
Freshersworld.comResourceCenter
FirstJob.DreamJob.Freshersworld.com
15.
voidmain()
{
intcount=10,*temp,sum=0;
temp=&count;
*temp=20;
temp=∑
*temp=count;
printf("%d%d%d",count,*temp,sum);
}
Ans:202020
16.Therewasquestionincworkingonlyonunixmachinewithpatternmatching.
14.whatisalloca()
Ans:Itallocatesandfreesmemoryafteruse/aftergettingoutofscope
17.
main()
{
statici=3;
printf("%d",i);
returni>0?main():0;
}
Ans:321
18.
char*foo()
{
charresult[100]);
strcpy(result,"anythingisgood");
return(result);
}
voidmain()
{
char*j;
j=foo()
printf("%s",j);
}
Freshersworld.comResourceCenter
FirstJob.DreamJob.Freshersworld.com
Ans:anythingisgood.
19.
voidmain()
{
char*s[]={"dharma","hewlettpackard","siemens","ibm"};
char**p;
p=s;
printf("%s",++*p);
printf("%s",*p++);
printf("%s",++*p);
}
Ans:"harma"(p>add(dharma)&&(*p)>harma)
"harma"(afterprinting,p>add(hewlettpackard)&&(*p)>harma)
"ewlettpackard"
20.Outputofthefollowingprogramis
main()
{inti=0;
for(i=0;i<20;i++)
{switch(i)
case0:i+=5;
case1:i+=2;
case5:i+=5;
defaulti+=4;
break;}
printf("%d,",i);
}
}
a)0,5,9,13,17
b)5,9,13,17
c)12,17,22
d)16,21
e)Syntaxerror
Ans.(d)
Freshersworld.comResourceCenter
FirstJob.DreamJob.Freshersworld.com
21.Whatistheouptutinthefollowingprogram
main()
{charc=64;
inti=32
unsignedintu=16;
if(c>i)
{printf("pass1,");
if(c<u)
printf("pass2");
else
printf("Fail2");
}
else
printf("Fail1);
if(i<u)
printf("pass2");
else
printf("Fail2")
}
a)Pass1,Pass2
b)Pass1,Fail2
c)Fail1,Pass2
d)Fail1,Fail2
e)Noneofthese
Ans.(c)
22.Whatwillthefollowingprogramdo?
voidmain()
{
inti;
chara[]="String";
char*p="NewSring";
char*Temp;
Temp=a;
a=malloc(strlen(p)+1);
strcpy(a,p);//Linenumber:9//
p=malloc(strlen(Temp)+1);
strcpy(p,Temp);
printf("(%s,%s)",a,p);
free(p);
Freshersworld.comResourceCenter
FirstJob.DreamJob.Freshersworld.com
free(a);
}//Linenumber15//
a)Swapcontentsofp&aandprint:(Newstring,string)
b)Generatecompilationerrorinlinenumber8
c)Generatecompilationerrorinlinenumber5
d)Generatecompilationerrorinlinenumber7
e)Generatecompilationerrorinlinenumber1
Ans.(b)
23.Inthefollowingcodesegmentwhatwillbetheresultofthefunction,
valueofx,valueofy
{unsignedintx=1;
inty;
y=~0;
if(x==y)
printf("same");
else
printf("notsame");
}
a)same,MAXINT,1
b)notsame,MAXINT,MAXINT
c)same,MAXUNIT,1
d)same,MAXUNIT,MAXUNIT
e)notsame,MAXINT,MAXUNIT
Ans.(a)
24.Whatwillbetheresultofthefollowingprogram?
char*gxxx()
{staticcharxxx[1024];
returnxxx;
}
main()
{char*g="string";
Freshersworld.comResourceCenter
FirstJob.DreamJob.Freshersworld.com
strcpy(gxxx(),g);
g=gxxx();
strcpy(g,"oldstring");
printf("Thestringis:%s",gxxx());
}
a)Thestringis:string
b)Thestringis:Oldstring
c)Runtimeerror/Coredump
d)Syntaxerrorduringcompilation
e)Noneofthese
Ans.(b)
25.FindtheoutputforthefollowingCprogram
main()
{
char*p1="Name";
char*p2;
p2=(char*)malloc(20);
while(*p2++=*p1++);
printf("%s\n",p2);
}
Ans.Anemptystring
26.FindtheoutputforthefollowingCprogram
main()
{
intx=20,y=35;
x=y+++x++;
y=++y+++x;
printf("%d%d\n",x,y);
}
Ans.5794
27.FindtheoutputforthefollowingCprogram
Freshersworld.comResourceCenter
10
FirstJob.DreamJob.Freshersworld.com
main()
{
intx=5;
printf("%d%d%d\n",x,x<<2,x>>2);
}
Ans.5201
28FindtheoutputforthefollowingCprogram
#defineswap1(a,b)a=a+b;b=ab;a=ab;
main()
{
intx=5,y=10;
swap1(x,y);
printf("%d%d\n",x,y);
swap2(x,y);
printf("%d%d\n",x,y);
}
intswap2(inta,intb)
{
inttemp;
temp=a;
b=a;
a=temp;
return;
}
Ans.105
29FindtheoutputforthefollowingCprogram
main()
{
char*ptr="RamcoSystems";
(*ptr)++;
printf("%s\n",ptr);
ptr++;
printf("%s\n",ptr);
}
Ans.SamcoSystems
Freshersworld.comResourceCenter
11
FirstJob.DreamJob.Freshersworld.com
30FindtheoutputforthefollowingCprogram
#include<stdio.h>
main()
{
chars1[]="Ramco";
chars2[]="Systems";
s1=s2;
printf("%s",s1);
}
Ans.Compilationerrorgivingitcannotbeanmodifiable'lvalue'
31FindtheoutputforthefollowingCprogram
#include<stdio.h>
main()
{
char*p1;
char*p2;
p1=(char*)malloc(25);
p2=(char*)malloc(25);
strcpy(p1,"Ramco");
strcpy(p2,"Systems");
strcat(p1,p2);
printf("%s",p1);
}
Ans.RamcoSystems
32.FindtheoutputforthefollowingCprogramgiventhat
[1].Thefollowingvariableisavailableinfile1.c
staticintaverage_float;
Ans.Allthefunctionsinthefile1.ccanaccessthevariable
33.FindtheoutputforthefollowingCprogram
Freshersworld.comResourceCenter
12
FirstJob.DreamJob.Freshersworld.com
#defineTRUE0
somecode
while(TRUE)
{
somecode
}
Ans.Thiswon'tgointotheloopasTRUEisdefinedas0
34.structlist{
intx;
structlist*next;
}*head;
thestructhead.x=100
Istheaboveassignmenttopointeriscorrectorwrong?
Ans.Wrong
35.Whatistheoutputofthefollowing?
inti;
i=1;
i=i+2*i++;
printf(%d,i);
Ans.4
36.FILE*fp1,*fp2;
fp1=fopen("one","w")
fp2=fopen("one","w")
fputc('A',fp1)
fputc('B',fp2)
fclose(fp1)
fclose(fp2)
}
FindtheError,IfAny?
Ans.noerror.ButItwilloverwritesonsamefile.
Freshersworld.comResourceCenter
13
FirstJob.DreamJob.Freshersworld.com
37.Whataretheoutput(s)forthefollowing?
38.#include<malloc.h>
char*f()
{char*s=malloc(8);
strcpy(s,"goodbye");
}
main()
{
char*f();
printf("%c",*f()='A');}
39.#defineMAN(x,y)(x)>(y)?(x):(y)
{inti=10;
j=5;
k=0;
k=MAX(i++,++j);
printf(%d%d%d%d,i,j,k);
}
Ans.1050
40.
voidmain()
{
inti=7;
printf("%d",i++*i++);
}
Ans:56
Freshersworld.comResourceCenter
14