You are on page 1of 16

http://TechPreparation.

com
2008
C Interview Questions
And Answers
V i s i t T e c h P r e p a r a t i o n . c o m f o r mo r e i n t e r v i e w u e s t i o n s a n d
a n s w e r s
C Interview Questions and Answers
What is C language?
The C pro!rammin! "an!ua!e is a standardi#ed pro!rammin! "an!ua!e
deve"oped in the ear"$ %&'0s ($ )en Thompson and *ennis +itchie for
use on the ,-I. operatin! s$stem. It has since spread to man$ other
operatin! s$stems/ and is one of the most wide"$ used pro!rammin!
"an!ua!es. C is pri#ed for its efficienc$/ and is the most popu"ar
pro!rammin! "an!ua!e for writin! s$stem software/ thou!h it is a"so
used for writin! app"ications.
printf() Function
What is the output of printf("%d")?
%. 0hen we write printf123d2/456 this means compi"er wi"" print the
va"ue of 4. 7ut as here/ there is nothin! after 3d so compi"er wi"" show
in output window !ar(a!e va"ue.
2. 0hen we use 3d the compi"er interna""$ uses it to access the
ar!ument in the stac8 1ar!ument stac85. Idea""$ compi"er determines
the offset of the data varia("e dependin! on the format specification
strin!. -ow when we write printf123d2/a5 then compi"er first accesses
the top most e"ement in the ar!ument stac8 of the printf which is 3d
and dependin! on the format strin! it ca"cu"ated to offset to the actua"
data varia("e in the memor$ which is to (e printed. -ow when on"$ 3d
wi"" (e present in the printf then compi"er wi"" ca"cu"ate the correct
offset 1which wi"" (e the offset to access the inte!er varia("e5 (ut as
the actua" data o(9ect is to (e printed is not present at that memor$
"ocation so it wi"" print what ever wi"" (e the contents of that memor$
"ocation.
:. ;ome compi"ers chec8 the format strin! and wi"" !enerate an error
without the proper num(er and t$pe of ar!uments for thin!s "i8e
printf1...5 and scanf1...5.
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e 2
malloc() Function- What is the difference between "calloc(...)" and
"malloc(...)"?
%. ca""oc1...5 a""ocates a ("oc8 of memor$ for an arra$ of e"ements of a
certain si#e. 7$ defau"t the ("oc8 is initia"i#ed to 0. The tota" num(er of
memor$ a""ocated wi"" (e 1num(er<of<e"ements = si#e5.
ma""oc1...5 ta8es in on"$ a sin!"e ar!ument which is the memor$
reuired in ($tes. ma""oc1...5 a""ocated ($tes of memor$ and not ("oc8s
of memor$ "i8e ca""oc1...5.
2. ma""oc1...5 a""ocates memor$ ("oc8s and returns a void pointer to the
a""ocated space/ or -,>> if there is insufficient memor$ avai"a("e.
ca""oc1...5 a""ocates an arra$ in memor$ with e"ements initia"i#ed to 0
and returns a pointer to the a""ocated space. ca""oc1...5 ca""s ma""oc1...5
in order to use the C?? <set<new<mode function to set the new
hand"er mode.
printf() Function- What is the difference between "printf(...)" and
"sprintf(...)"?
sprintf(...) writes data to the character array whereas printf(...) writes data to the
standard output device.
Compilation How to reduce a final sie of e!ecutable?
;i#e of the fina" e4ecuta("e can (e reduced usin! d$namic "in8in! for
"i(raries.
"in#ed "ists -- Can $ou tell me how to chec# whether a lin#ed list is circular?
Create two pointers/ and set (oth to the start of the "ist. ,pdate each
as fo""ows:
whi"e 1pointer%5 @
pointer% A pointer%BCne4t6
pointer2 A pointer2BCne4t6
if 1pointer25 pointer2Apointer2BCne4t6
if 1pointer% AA pointer25 @
print 12circu"ar256
D
D
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e :
If a "ist is circu"ar/ at some point pointer2 wi"" wrap around and (e
either at the item 9ust (efore pointer%/ or the item (efore that. Either
wa$/ its either % or 2 9umps unti" the$ meet.
"union" %ata &$pe What is the output of the following program? Wh$?
Finc"ude
main15 @
t$pedef union @
int a6
char (G%0H6
f"oat c6
D
,nion6
,nion 4/$ A @%00D6
4.a A I06
strcp$14.(/2he""o256
4.c A 2%.I06
printf12,nion 4 : 3d 3s 3f n2/4.a/4.(/4.c56
printf12,nion $ : 3d 3s 3f n2/$.a/$.(/$.c56
D
What does static 'ariable mean?

there are : main uses for the static.
%. If $ou dec"are within a function:
It retains the va"ue (etween function ca""s
2.If it is dec"ared for a function name:
7$ defau"t function is e4tern..so it wi"" (e visi("e from other fi"es if the
function dec"aration is as static..it is invisi("e for the outer fi"es
:. ;tatic for !"o(a" varia("es:
7$ defau"t we can use the !"o(a" varia("es from outside fi"es If it is
static !"o(a"..that varia("e is "imited to with in the fi"e
(d'antages of a macro o'er a function?
Jacro !ets to see the Compi"ation environment/ so it can e4pand <<
<<TIJE<< <<KI>E<< Fdefines. It is e4panded ($ the preprocessor.
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e L
Kor e4amp"e/ $ou canMt do this without macros
Fdefine P+I-T1E.P+5 printf1 FE.P+ NA3dOnP/ E.P+5
P+I-T1 I?Q=' 5 // e4pands into printf1PI?Q='A3dP/ I?Q=' 56
Rou can define $our mini "an!ua!e with macros:
Fdefine streua"1A/75 1Sstrcmp1A/755
Jacros are a necessar$ evi"s of "ife. The purists donMt "i8e them/ (ut
without it no rea" wor8 !ets done.
What are the differences between malloc() and calloc()?
There are 2 differences.
Kirst/ is in the num(er of ar!uments. ma""oc15 ta8es a sin!"e
ar!ument1memor$ reuired in ($tes5/ whi"e ca""oc15 needs 2
ar!uments1num(er of varia("es to a""ocate memor$/ si#e in ($tes of a
sin!"e varia("e5.
;econd"$/ ma""oc15 does not initia"i#e the memor$ a""ocated/ whi"e
ca""oc15 initia"i#es the a""ocated memor$ to TE+U.
What are the different storage classes in C?
C has three t$pes of stora!e: automatic/ static and a""ocated.
Varia("e havin! ("oc8 scope and without static specifier have
automatic stora!e duration.
Varia("es with ("oc8 scope/ and with static specifier have static scope.
V"o(a" varia("es 1i.e/ fi"e scope5 with or without the static specifier a"so
have static scope.
Jemor$ o(tained from ca""s to ma""oc15/ a""oc15 or rea""oc15 (e"on!s to
a""ocated stora!e c"ass.
What is the difference between strings and character arra$s?
A ma9or difference is: strin! wi"" have static stora!e duration/ whereas
as a character arra$ wi"" not/ un"ess it is e4p"icit$ specified ($ usin! the
static 8e$word.
Actua""$/ a strin! is a character arra$ with fo""owin! properties:
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e I
= the mu"ti($te character seuence/ to which we !enera""$ ca"" strin!/
is used to initia"i#e an arra$ of static stora!e duration. The si#e of this
arra$ is 9ust sufficient to contain these characters p"us the terminatin!
-,> character.
= it not specified what happens if this arra$/ i.e./ strin!/ is modified.
= Two strin!s of same va"ueG%H ma$ share same memor$ area. Kor
e4amp"e/ in the fo""owin! dec"arations:
char =s% A NCa"vin and Wo((esP6
char =s2 A NCa"vin and Wo((esP6
the strin!s pointed ($ s% and s2 ma$ reside in the same memor$
"ocation. 7ut/ it is not true for the fo""owin!:
char ca%GH A NCa"vin and Wo((esP6
char ca2GH A NCa"vin and Wo((esP6
G%H The va"ue of a strin! is the seuence of the va"ues of the contained
characters/ in order.
%ifference between const char) p and char const) p
In const char= p/ the character pointed ($ XpM is constant/ so u cant
chan!e the va"ue of character pointed ($ p (ut u can ma8e XpM refer to
some other "ocation.
in char const= p/ the ptr XpM is constant not the character referenced ($
it/ so u cant ma8e XpM to reference to an$ other "ocation (ut u can
chan!e the va"ue of the char pointed ($ XpM.
What is hashing?
To hash means to !rind up/ and thatMs essentia""$ what hashin! is a""
a(out. The heart of a hashin! a"!orithm is a hash function that ta8es
$our nice/ neat data and !rinds it into some randomB"oo8in! inte!er.
The idea (ehind hashin! is that some data either has no inherent
orderin! 1such as ima!es5 or is e4pensive to compare 1such as
ima!es5. If the data has no inherent orderin!/ $ou canMt perform
comparison searches.
If the data is e4pensive to compare/ the num(er of comparisons used
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e Q
even ($ a (inar$ search mi!ht (e too man$. ;o instead of "oo8in! at
the data themse"ves/ $ouM"" condense 1hash5 the data to an inte!er 1its
hash va"ue5 and 8eep a"" the data with the same hash va"ue in the
same p"ace. This tas8 is carried out ($ usin! the hash va"ue as an
inde4 into an arra$.
To search for an item/ $ou simp"$ hash it and "oo8 at a"" the data whose
hash va"ues match that of the data $ouMre "oo8in! for. This techniue
!reat"$ "essens the num(er of items $ou have to "oo8 at. If the
parameters are set up with care and enou!h stora!e is avai"a("e for
the hash ta("e/ the num(er of comparisons needed to find an item can
(e made ar(itrari"$ c"ose to one.
Une aspect that affects the efficienc$ of a hashin! imp"ementation is
the hash function itse"f. It shou"d idea""$ distri(ute data random"$
throu!hout the entire hash ta("e/ to reduce the "i8e"ihood of co""isions.
Co""isions occur when two different 8e$s have the same hash va"ue.
There are two wa$s to reso"ve this pro("em. In open addressin!/ the
co""ision is reso"ved ($ the choosin! of another position in the hash
ta("e for the e"ement inserted "ater. 0hen the hash ta("e is searched/ if
the entr$ is not found at its hashed position in the ta("e/ the search
continues chec8in! unti" either the e"ement is found or an empt$
position in the ta("e is found.
The second method of reso"vin! a hash co""ision is ca""ed chainin!. In
this method/ a (uc8et or "in8ed "ist ho"ds a"" the e"ements whose 8e$s
hash to the same va"ue. 0hen the hash ta("e is searched/ the "ist must
(e searched "inear"$.
How can $ou determine the sie of an allocated portion of memor$?
Rou canMt/ rea""$. free15 can / (ut thereMs no wa$ for $our pro!ram to
8now the tric8 free15 uses. Even if $ou disassem("e the "i(rar$ and
discover the tric8/ thereMs no !uarantee the tric8 wonMt chan!e with the
ne4t re"ease of the compi"er.
Can static 'ariables be declared in a header file?
Rou canMt dec"are a static varia("e without definin! it as we"" 1this is
(ecause the stora!e c"ass modifiers static and e4tern are mutua""$
e4c"usive5. A static varia("e can (e defined in a header fi"e/ (ut this
wou"d cause each source fi"e that inc"uded the header fi"e to have its
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e '
own private cop$ of the varia("e/ which is pro(a("$ not what was
intended.
Can a 'ariable be both const and 'olatile?
Res. The const modifier means that this code cannot chan!e the va"ue
of the varia("e/ (ut that does not mean that the va"ue cannot (e
chan!ed ($ means outside this code. Kor instance/ in the e4amp"e in
KAQ 8/ the timer structure was accessed throu!h a vo"ati"e const
pointer. The function itse"f did not chan!e the va"ue of the timer/ so it
was dec"ared const. Wowever/ the va"ue was chan!ed ($ hardware on
the computer/ so it was dec"ared vo"ati"e. If a varia("e is (oth const and
vo"ati"e/ the two modifiers can appear in either order.
Can include files be nested?
Res. Inc"ude fi"es can (e nested an$ num(er of times. As "on! as $ou
use precautionar$ measures / $ou can avoid inc"udin! the same fi"e
twice. In the past/ nestin! header fi"es was seen as (ad pro!rammin!
practice/ (ecause it comp"icates the dependenc$ trac8in! function of
the JA)E pro!ram and thus s"ows down compi"ation. Jan$ of toda$Ms
popu"ar compi"ers ma8e up for this difficu"t$ ($ imp"ementin! a
concept ca""ed precompi"ed headers/ in which a"" headers and
associated dependencies are stored in a precompi"ed state.
Jan$ pro!rammers "i8e to create a custom header fi"e that has
Finc"ude statements for ever$ header needed for each modu"e. This is
perfect"$ accepta("e and can he"p avoid potentia" pro("ems re"atin! to
Finc"ude fi"es/ such as accidenta""$ omittin! an Finc"ude fi"e in a
modu"e.
When does the compiler not implicitl$ generate the address of the first
element of an arra$?
0henever an arra$ name appears in an e4pression such as
B arra$ as an operand of the si#eof operator
B arra$ as an operand of Y operator
B arra$ as a strin! "itera" initia"i#er for a character arra$
Then the compi"er does not imp"icit"$ !enerate the address of the
address of the first e"ement of an arra$.
What is a null pointer?
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e 8
There are times when itMs necessar$ to have a pointer that doesnMt
point to an$thin!. The macro -,>>/ defined in / has a va"ue thatMs
!uaranteed to (e different from an$ va"id pointer. -,>> is a "itera" #ero/
possi("$ cast to void= or char=. ;ome peop"e/ nota("$ C??
pro!rammers/ prefer to use 0 rather than -,>>.
The nu"" pointer is used in three wa$s:
%5 To stop indirection in a recursive data structure
25 As an error va"ue
:5 As a sentine" va"ue
What is the difference between te!t and binar$ modes?
;treams can (e c"assified into two t$pes: te4t streams and (inar$
streams. Te4t streams are interpreted/ with a ma4imum "en!th of 2II
characters. 0ith te4t streams/ carria!e return/"ine feed com(inations
are trans"ated to the new"ine n character and vice versa. 7inar$
streams are uninterrupted and are treated one ($te at a time with no
trans"ation of characters. T$pica""$/ a te4t stream wou"d (e used for
readin! and writin! standard te4t fi"es/ printin! output to the screen or
printer/ or receivin! input from the 8e$(oard.
A (inar$ te4t stream wou"d t$pica""$ (e used for readin! and writin!
(inar$ fi"es such as !raphics or word processin! documents/ readin!
mouse input/ or readin! and writin! to the modem.
What is static memor$ allocation and d$namic memor$ allocation?
;tatic memor$ a""ocation: The compi"er a""ocates the reuired memor$
space for a dec"ared varia("e.7$ usin! the address of operator/the
reserved address is o(tained and this address ma$ (e assi!ned to a
pointer varia("e.;ince most of the dec"ared varia("e have static
memor$/this wa$ of assi!nin! pointer va"ue to a pointer varia("e is
8nown as static memor$ a""ocation. memor$ is assi!ned durin!
compi"ation time.
*$namic memor$ a""ocation: It uses functions such as ma""oc1 5 or
ca""oc1 5 to !et memor$ d$namica""$.If these functions are used to !et
memor$ d$namica""$ and the va"ues returned ($ these functions are
assin!ned to pointer varia("es/ such assi!nments are 8nown as
d$namic memor$ a""ocation.memor$ is assined durin! run time.
When should a far pointer be used?
;ometimes $ou can !et awa$ with usin! a sma"" memor$ mode" in
most of a !iven pro!ram. There mi!ht (e 9ust a few thin!s that donMt fit
in $our sma"" data and code se!ments. 0hen that happens/ $ou can
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e &
use e4p"icit far pointers and function dec"arations to !et at the rest of
memor$. A far function can (e outside the QL)7 se!ment most
functions are shoehorned into for a sma""Bcode mode". 1Uften/ "i(raries
are dec"ared e4p"icit"$ far/ so the$M"" wor8 no matter what code mode"
the pro!ram uses.5 A far pointer can refer to information outside the
QL)7 data se!ment. T$pica""$/ such pointers are used with farma""oc15
and such/ to mana!e a heap separate from where a"" the rest of the
data "ives. If $ou use a sma""Bdata/ "ar!eBcode mode"/ $ou shou"d
e4p"icit"$ ma8e $our function pointers far.
How are pointer 'ariables initialied?
Pointer varia("e are initia"i#ed ($ one of the fo""owin! two wa$s
B ;tatic memor$ a""ocation
B *$namic memor$ a""ocation
%ifference between arra$s and pointers?
B Pointers are used to manipu"ate data usin! the address. Pointers use
= operator to access the data pointed to ($ them
B Arra$s use su(scripted varia("es to access and manipu"ate data.
Arra$ varia("es can (e euiva"ent"$ written usin! pointer e4pression.
*s using e!it() the same as using return?
-o. The e4it15 function is used to e4it $our pro!ram and return contro"
to the operatin! s$stem. The return statement is used to return from a
function and return contro" to the ca""in! function. If $ou issue a return
from the main15 function/ $ou are essentia""$ returnin! contro" to the
ca""in! function/ which is the operatin! s$stem. In this case/ the return
statement and e4it15 function are simi"ar.
What is a method?
Jethod is a wa$ of doin! somethin!/ especia""$ a s$stematic wa$6
imp"ies an order"$ "o!ica" arran!ement 1usua""$ in steps5.
What is indirection?
If $ou dec"are a varia("e/ its name is a direct reference to its va"ue. If
$ou have a pointer to a varia("e/ or an$ other o(9ect in memor$/ $ou
have an indirect reference to its va"ue.
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e %0
What is modular programming?
If a pro!ram is "ar!e/ it is su(divided into a num(er of sma""er
pro!rams that are ca""ed modu"es or su(pro!rams. If a comp"e4
pro("em is so"ved usin! more modu"es/ this approach is 8nown as
modu"ar pro!rammin!.
How man$ le'els deep can include files be nested?
Even thou!h there is no "imit to the num(er of "eve"s of nested inc"ude
fi"es $ou can have/ $our compi"er mi!ht run out of stac8 space whi"e
tr$in! to inc"ude an inordinate"$ hi!h num(er of fi"es. This num(er
varies accordin! to $our hardware confi!uration and possi("$ $our
compi"er.
What is the difference between declaring a 'ariable and defining a 'ariable?

*ec"arin! a varia("e means descri(in! its t$pe to the compi"er (ut not
a""ocatin! an$ space for it. *efinin! a varia("e means dec"arin! it and
a"so a""ocatin! space to ho"d the varia("e. Rou can a"so initia"i#e a
varia("e at the time it is defined.
What is an l'alue?
An "va"ue is an e4pression to which a va"ue can (e assi!ned. The "va"ue
e4pression is "ocated on the "eft side of an assi!nment statement/
whereas an rva"ue is "ocated on the ri!ht side of an assi!nment
statement. Each assi!nment statement must have an "va"ue and an
rva"ue. The "va"ue e4pression must reference a stora("e varia("e in
memor$. It cannot (e a constant.
%ifferentiate between an internal static and e!ternal static 'ariable?
An interna" static varia("e is dec"ared inside a ("oc8 with static stora!e
c"ass whereas an e4terna" static varia("e is dec"ared outside a"" the
("oc8s in a fi"e.An interna" static varia("e has persistent stora!e/("oc8
scope and no "in8a!e.An e4terna" static varia("e has permanent
stora!e/fi"e scope and interna" "in8a!e.
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e %%
What is the difference between a string and an arra$?
An arra$ is an arra$ of an$thin!. A strin! is a specific 8ind of an arra$
with a we""B8nown convention to determine its "en!th.
There are two 8inds of pro!rammin! "an!ua!es: those in which a strin!
is 9ust an arra$ of characters/ and those in which itMs a specia" t$pe. In
C/ a strin! is 9ust an arra$ of characters 1t$pe char5/ with one wrin8"e: a
C strin! a"wa$s ends with a -,> character.
The Nva"ueP of an arra$ is the same as the address of 1or a pointer to5
the first e"ement6 so/ freuent"$/ a C strin! and a pointer to char are
used to mean the same thin!.
An arra$ can (e an$ "en!th. If itMs passed to a function/ thereMs no wa$
the function can te"" how "on! the arra$ is supposed to (e/ un"ess some
convention is used. The convention for strin!s is -,> termination6 the
"ast character is an A;CII -,> 1XM5 character.
What is an argument? %ifferentiate between formal arguments and actual
arguments?
An ar!ument is an entit$ used to pass the data from ca""in! function to
the ca""ed function. Korma" ar!uments are the ar!uments avai"a("e in
the function definition. The$ are preceded ($ their own data t$pes.
Actua" ar!uments are avai"a("e in the function ca"".
What are ad'antages and disad'antages of e!ternal storage class?
Advanta!es of e4terna" stora!e c"ass
%5Persistent stora!e of a varia("e retains the "atest va"ue
25The va"ue is !"o(a""$ avai"a("e
*isadvanta!es of e4terna" stora!e c"ass
%5The stora!e for an e4terna" varia("e e4ists even when the varia("e is
not needed
25The side effect ma$ produce surprisin! output
:5Jodification of the pro!ram is difficu"t
L5Venera"it$ of a pro!ram is affected
What is a 'oid pointer?
A void pointer is a C convention for a raw address. The compi"er has no
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e %2
idea what t$pe of o(9ect a void Pointer rea""$ points to. If $ou write
int =ip6
ip points to an int. If $ou write
void =p6
p doesnMt point to a voidS
In C and C??/ an$ time $ou need a void pointer/ $ou can use another
pointer t$pe. Kor e4amp"e/ if $ou have a char=/ $ou can pass it to a
function that e4pects a void=. Rou donMt even need to cast it. In C 1(ut
not in C??5/ $ou can use a void= an$ time $ou need an$ 8ind of
pointer/ without castin!. 1In C??/ $ou need to cast it5.
A void pointer is used for wor8in! with raw memor$ or for passin! a
pointer to an unspecified t$pe.
;ome C code operates on raw memor$. 0hen C was first invented/
character pointers 1char =5 were used for that. Then peop"e started
!ettin! confused a(out when a character pointer was a strin!/ when it
was a character arra$/ and when it was raw memor$.
When should a t$pe cast not be used?
A t$pe cast shou"d not (e used to override a const or vo"ati"e
dec"aration. Uverridin! these t$pe modifiers can cause the pro!ram to
fai" to run correct"$.
A t$pe cast shou"d not (e used to turn a pointer to one t$pe of
structure or data t$pe into another. In the rare events in which this
action is (eneficia"/ usin! a union to ho"d the va"ues ma8es the
pro!rammerMs intentions c"earer.
When is a switch statement better than multiple if statements?
A switch statement is !enera""$ (est to use when $ou have more than
two conditiona" e4pressions (ased on a sin!"e varia("e of numeric t$pe.
What is a static function?
A static function is a function whose scope is "imited to the current
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e %:
source fi"e. ;cope refers to the visi(i"it$ of a function or varia("e. If the
function or varia("e is visi("e outside of the current source fi"e/ it is said
to have !"o(a"/ or e4terna"/ scope. If the function or varia("e is not
visi("e outside of the current source fi"e/ it is said to have "oca"/ or
static/ scope.
What is a pointer 'ariable?
A pointer varia("e is a varia("e that ma$ contain the address of another
varia("e or an$ va"id address in the memor$.
What is a pointer 'alue and address?
A pointer va"ue is a data o(9ect that refers to a memor$ "ocation. Each
memor$ "ocation is num(ered in the memor$. The num(er attached to
a memor$ "ocation is ca""ed the address of the "ocation.
What is a modulus operator? What are the restrictions of a modulus
operator?
A Jodu"us operator !ives the remainder va"ue. The resu"t of 43$ is
o(tained ($ 14B14/$5=$5. This operator is app"ied on"$ to inte!ra"
operands and cannot (e app"ied to f"oat or dou("e.
%ifferentiate between a lin#er and lin#age?
A "in8er converts an o(9ect code into an e4ecuta("e code ($ "in8in!
to!ether the necessar$ (ui"d in functions. The form and p"ace of
dec"aration where the varia("e is dec"ared in a pro!ram determine the
"in8a!e of varia("e.
What is a function and built-in function?
A "ar!e pro!ram is su(divided into a num(er of sma""er pro!rams or
su(pro!rams. Each su(pro!ram specifies one or more actions to (e
performed for a "ar!e pro!ram. such su(pro!rams are functions.
The function supports on"$ static and e4tern stora!e c"asses. 7$
defau"t/ function assumes e4tern stora!e c"ass. functions have !"o(a"
scope. Un"$ re!ister or auto stora!e c"ass is a""owed in the function
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e %L
parameters. 7ui"tBin functions that predefined and supp"ied a"on! with
the compi"er are 8nown as (ui"tBin functions. The$ are a"so 8nown as
"i(rar$ functions.
Wh$ should * protot$pe a function?
A function protot$pe te""s the compi"er what 8ind of ar!uments a
function is "oo8in! to receive and what 8ind of return va"ue a function
is !oin! to !ive (ac8. This approach he"ps the compi"er ensure that
ca""s to a function are made correct"$ and that no erroneous t$pe
conversions are ta8in! p"ace.
What is +ol$morphism ?
ZPo"$morphismZ is an o(9ect oriented term. Po"$morphism ma$ (e
defined as the a(i"it$ of re"ated o(9ects to respond to the same
messa!e with different/ (ut appropriate actions. In other words/
po"$morphism means ta8in! more than one form. Po"$morphism "eads
to two important aspects in U(9ect Uriented termino"o!$ B Kunction
Uver"oadin! and Kunction Uverridin!. Uver"oadin! is the practice of
supp"$in! more than one definition for a !iven function name in the
same scope. The compi"er is "eft to pic8 the appropriate version of the
function or operator (ased on the ar!uments with which it is ca""ed.
Uverridin! refers to the modifications made in the su( c"ass to the
inherited methods from the (ase c"ass to chan!e their (ehavior.
What is ,perator o'erloading ?
0hen an operator is over"oaded/ it ta8es on an additiona" meanin!
re"ative to a certain c"ass. 7ut it can sti"" retain a"" of its o"d meanin!s.
E4amp"es:
%5 The operators CC and [[ ma$ (e used for I/U operations (ecause
in the header/ the$ are over"oaded.
25 In a stac8 c"ass it is possi("e to over"oad the ? operator so that it
appends the contents of one stac8 to the contents of another. 7ut the
? operator sti"" retains its ori!ina" meanin! re"ative to other t$pes of
data.
What is the difference between goto and long-mp() and set-mp()?
A !oto statement imp"ements a "oca" 9ump of pro!ram e4ecution/ and
the "on!9mp15 and set9mp15 functions imp"ement a non"oca"/ or far/
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e %I
9ump of pro!ram e4ecution.
Venera""$/ a 9ump in e4ecution of an$ 8ind shou"d (e avoided (ecause
it is not considered !ood pro!rammin! practice to use such statements
as !oto and "on!9mp in $our pro!ram.
A !oto statement simp"$ ($passes code in $our pro!ram and 9umps to
a predefined position. To use the !oto statement/ $ou !ive it a "a(e"ed
position to 9ump to. This predefined position must (e within the same
function. Rou cannot imp"ement !otos (etween functions.
0hen $our pro!ram ca""s set9mp15/ the current state of $our pro!ram is
saved in a structure of t$pe 9mp<(uf. >ater/ $our pro!ram can ca"" the
"on!9mp15 function to restore the pro!ramMs state as it was when $ou
ca""ed set9mp15.,n"i8e the !oto statement/ the "on!9mp15 and set9mp15
functions do not need to (e imp"emented in the same function.
Wowever/ there is a ma9or draw(ac8 to usin! these functions: $our
pro!ram/ when restored to its previous"$ saved state/ wi"" "ose its
references to an$ d$namica""$ a""ocated memor$ (etween the
"on!9mp15 and the set9mp15. This means $ou wi"" waste memor$ for
ever$ ma""oc15 or ca""oc15 $ou have imp"emented (etween $our
"on!9mp15 and set9mp15/ and $our pro!ram wi"" (e horri("$ inefficient.
It is hi!h"$ recommended that $ou avoid usin! functions such as
"on!9mp15 and set9mp15 (ecause the$/ "i8e the !oto statement/ are
uite often an indication of poor pro!rammin! practice.
Visit http://TechPreparation.com for more Interview Questions with Answers Pa!e %Q

You might also like