Académique Documents
Professionnel Documents
Culture Documents
Topic: Functions in C
Date: 30-01-2017
Instructions:
Forthequestionsconsistingcodesegments,assumenecessarylibrarieshavebeenincluded.
Incase,ifyoufeelanydiscrepancy,kindlystaterelevantassumptionwithproperreason.
Everyquestionhasonlyonecorrectoptionamonggivenfouroptions.Ifyoufeelnoneof
theoptioniscorrectthenstateyouranswerwithproperreason.
1. ConsiderthefollowingCfunction,forlargevaluesofy,thereturnvalueofthefunctionf
bestapproximates
float f(float x, int y)
{
float p, s; int i;
for (s=1, p=1, i=1; i < y; i ++)
{
p*= x/i;
s+=p;
}
return s;
}
(a) x^y
(b) e^x
(c) ln(1+x)
(d) x^x
2. ConsiderthefollowingCprogram:
void foo(int n, int sum)
{
int k = 0, j = 0;
if (n == 0) return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf ("%d,", k);
}
int main (){
int a = 2048, sum = 0;
foo (a, sum);
printf ("%d\n", sum);
getchar();
}
Page|1
Whatdoestheaboveprogramprint?
(a) 8,4,0,2,14
(b) 8,4,0,2,0
(c) 2,0,4,8,14
(d) 2,0,4,8,0
3. ConsiderthefollowingCprogram:
double foo (double); /* Line 1 */
int main()
{
double da, db;
db = foo(da);
}
double foo(double a)
{
return a;
}
Theabovecodecompiledwithoutanyerrororwarning.IfLine1isdeleted,theabovecode
willshow:
(a) nocompilewarningorerror
(b) somecompilerwarningsnotleadingtounintendedresults
(c) somecompilerwarningsduetotypemismatcheventuallyleadingtounintended
results
(d) compileerror
4. Figure out the output of the below code. State relevant assumption if any during
computation.AssumeNecessarylibrarieshavebeenincluded.
#include<stdio.h>
voidswap(char*str1, char*str2)
{
char*temp = str1;
str1 = str2;
str2 = temp;
}
intmain()
{
char*str1 = "John Forbes";
char*str2 = "Nash Jr";
swap(str1, str2);
printf("%s %s", str1, str2);
getchar();
return0;
}
(a) NashJrJohnForbes
(b) JohnForbesNashJr
(c) ForbesJohnJrNash
(d) JohnJrForbesNash
Page|2
5. Figure out the output of the below code. State relevant assumption if any during
computation.AssumeNecessarylibrarieshavebeenincluded.
#include<stdio.h>
charfun();
intmain(void)
{
printf("%d%c\n",fun(),fun());
return0;
}
charfun()
{
return'G';
}
(a) 71G
(b) G71
(c) Erroratcompiletimeatline4
(d) GG
6. Choosethecorrectoptiontofill?1and?2sothattheprogrambelowprintsaninputstring
inreverseorder.Assumethattheinputstringisterminatedbyanewlinecharacter.
voidreverse(void)
{
intc;
if(?1)reverse();
?2
}
intmain()
{
printf("EnterText");
printf("\n");
reverse();
printf("\n");
}
(a) ?1is(getchar()!=\n)
?2isgetchar(c);
(b) ?1is(c=getchar())!=\n)
?2isgetchar(c);
(c) ?1is(c!=\n)
?2isputchar(c);
(d) ?1is((c=getchar())!=\n)
?2isputchar(c);
7. ConsiderthefollowingCprogramthatattemptstolocateanelementxinanarrayY[]using
binarysearch.Theprogramiserroneous.
1.f(intY[10],intx){
2.inti,j,k;
Page|3
3.i=0;j=9;
4.do{
5.k=(i+j)/2;
6.if(Y[k]<x)i=k;elsej=k;
7.}while(Y[k]!=x&&i<j);
8.if(Y[k]==x)printf("xisinthearray");
9.elseprintf("xisnotinthearray");
10.}
OnwhichofthefollowingcontentsofYandxdoestheprogramfail?
(a) Yis[12345678910]andx<10
(b) Yis[135791113151719]andx<1
(c) Yis[2222222222]andx>2
(d) Yis[2468101214161820]and2<x<20andxiseven
8. ConsiderthedatagiveninabovequestionQ.7,thecorrectionneededintheprogramto
makeitworkproperlyis
(a) Changeline6to:if(Y[k]<x)i=k+1;elsej=k1;
(b) Changeline6to:if(Y[k]<x)i=k1;elsej=k+1;
(c) Changeline6to:if(Y[k]<=x)i=k;elsej=k;
(d) Changeline7to:}while((Y[k]==x)&&(i<j));
9. ConsiderthefollowingCprogram
inta,b,c=0;
voidprtFun(void);
intmain()
{
staticinta=1;/*line1*/
prtFun();
a+=1;
prtFun();
printf("\n%d%d",a,b);
}
voidprtFun(void)
{
staticinta=2;/*line2*/
intb=1;
a+=++b;
printf("\n%d%d",a,b);
}
Whatoutputwillbegeneratedbythegivencodesegment?
(a) 31
41
42
(b) 42
61
Page|4
61
(c) 42
62
20
(d) 31
52
52
10. ConsidertheabovequestionQ.9.Whatoutputwillbegeneratedbythegivencode
\segmentif:Line1isreplacedbyautointa=1;Line2isreplacedbyregisterinta=2;
(a) 31
41
42
(b) 42
61
61
(c) 42
62
20
(d) 42
42
20
11. ConsiderthefollowingCfunctioninwhichsizeisthenumberofelementsinthearrayE:
int MyX(int *E, unsigned int size)
{
int Y = 0;
int Z;
int i, j, k;
Page|5
Page|6
(d) D.(a,nleft_end1,kleft_end1)and(a,left_end,k)
14. ConsiderthefollowingCprogram.Theoutputoftheprogramis__________.
#include <stdio.h>
int f1(void);
int f2(void);
int f3(void);
int x = 10;
int main()
{
int x = 1;
x += f1() + f2() + f3() + f2();
printf("%d", x);
return 0;
}
int f1()
{
int x = 25;
x++;
return x;
}
int f2( )
{
static int x = 50;
x++;
return x;
}
int f3( )
{
x *= 10;
return x;
}
(a) 230
(b) 131
(c) 231
(d) 330
--*--
Page|7