Anda di halaman 1dari 12

C Programming -- Sample Questions

1.
Q
What does the term cast refer to? Why is it used?
A
Casting is a mechanism built into C that allows the programmer to force the conversion
of data types. This may be needed because most C functions are very particular about the
data types they process. A programmer may wish to override the default way the C
compiler promotes data types.
2.
Q
In arithmetic expressions, to what data type will the C compiler promote a character?
A
It will promote it to an integer unless otherwise directed
3.
Q
What is the difference between a statement and a bloc?
A
A statement is a single C expression terminated with a semicolon. A bloc is a series of
statements, the group of which is enclosed in curly!braces.
4.
Q
Increment the variable next three different ways.
A
next = next + 1;
and
next++;
and
next += 1;
5.
Q
"ow is a comment formed in C.
A
Comments in C begin with a slash followed by an asteris. Any text may then
appear including newlines. The comment is finished with an asteris followed by a slash.
#xample$
/* This is a comment */
6.
Q
Can comments be nested?
A
%ot in standard &'()* C.
7.
Q
+rom the standpoint of programming logic, what is the difference between a loop with the
test at the top, and a loop where the test is at the bottom?
A
If the test is at the bottom, the body of the loop will always be executed at least once.
When the test is at the top, the body of the loop may never be executed.
8.
Q
,pecify the seletons of two C loops with the test at the top.
A
next = 0; /* setup */
while ( next < max) { /* test */
printf("ello "); /* !o"# */
next++; /* up"ate */
$
and
for ( next = 0; next < max; next++) /* setup%test */
/* an" up"ate */
printf("ello"); /* !o"# */
9.
Q
,pecify a C loop with the test at the bottom.
A
next = 0; /* setup */
"o {
printf("ello"); /* !o"# */
next++; /* up"ate */
$ while ( next < max); /* test */
10.
Q
What is the switch statement?
A
It is C-s form of multiway!conditional &a..a case statement in .ascal*.
11.
Q
What does a brea statement do? Which control structures use it?
A
The brea statement unconditionally ends the execution of the smallest enclosing while,
do, for or switch statement.
12.
Q
In a loop, what is the difference between a brea and continue statement?
A
The brea terminates the loop. The continue branches immediately to the test portion of
the loop.
13.
Q
Where may variables be defined in C?
A
/utside a function definition &global scope, from the point of definition downward in the
source code*. Inside a bloc before any statements other than variable declarations &local
scope with respect to the bloc*.
14.
Q
What is the difference between a variable definition and a variable declaration?
A
A definition tells the compiler to set aside storage for the variable. A declaration maes
the variable nown to parts of the program that may wish to use it. A variable might be
defined and declared in the same statement.
15.
Q
What is the purpose of a function prototype?
A
A function prototype tells the compiler to expect a given function to be used in a given
way. That is, it tells the compiler the nature of the parameters passed to the function &the
0uantity, type and order* and the nature of the value returned by the function.
16.
Q
What is type checing?
A
The process by which the C compiler ensures that functions and operators use data of the
appropriate type&s*. This form of chec helps ensure the semantic correctness of the
program.
17.
Q
To what does the term storage class refer?
A
This is a part of a variable declaration that tells the compiler how to interpret the
variable-s symbol. It does not in itself allocate storage, but it usually tells the compiler
how the variable should be stored.
18.
Q
1ist C-s storage classes and what they signify.
A
static 2ariables are defined in a nonvolatile region of memory such that they retain their
contents though out the program-s execution.
register Ass the compiler to devote a processor register to this variable in order to speed
the program-s execution. The compiler may not comply and the variable looses it contents
and identity when the function it which it is defined terminates.
external Tells the compiler that the variable is defined in another module.
volatile Tells the compiler that other programs will be modifying this variable in addition
to the program being compiled. +or example, an I3/ device might need write directly into
a program or data space. 4eanwhile, the program itself may never directly access the
memory area in 0uestion. In such a case, we would not want the compiler to optimi5e!out
this data area that never seems to be used by the program, yet must exist for the program
to function correctly in a larger context.
19.
Q
,tate the syntax for the printf() and scanf() functions. ,tate their one crucial
difference with respect to their parameters.
A
Where fmt&tr tells printf() how to format the variable list that follows. 'ar1
through 'ar( may be variables of any base type.
scanf( fmt&tr% )'ar1% )'ar*% )'ar();
This routine is the input compliment to printf().
scanf() re0uires the address of each variable instead of the variable-s value &as in
printf()*. This is subtle source of serious bugs.
20.
Q
With respect to function parameter passing, what is the difference between call!by!value
and call!by!reference? Which method does C use?
A
In the case of call!by!reference, a pointer reference to a variable is passed into a function
instead of the actual value. The function-s operations will effect the variable in a global as
well as local sense. Call!by!value &C-s method of parameter passing*, by contrast, passes
a copy of the variable-s value into the function. Any changes to the variable made by
function have only a local effect and do not alter the state of the variable passed into the
function.
21.
Q
What is a structure and a union in C?
A
A structure is an aggregate data type. It combines one or more base or aggregate data
types into a pacage that may treated as a whole. A structure is lie a record in other
languages. A union combines two or more data types in the same area of storage. The
contents of a union may be one data type at one time and another type at a different time.
A union is sometimes called a tric!record.
22.
Q
6efine a structure for a simple name3address record.
A
struct name+""r {
char name,-0.;
char a""r,-0.;
char cit#,*0.;
char state,-.;
char /ip,0.;
$;
23.
Q
What does the t#pe"ef eyword do?
A
This eyword provides a short!hand way to write variable declarations. It is not a true
data typing mechanism, rather, it is syntactic 7sugar coating.7
24.
Q
8se typedef to mae a short!cut way to declare a pointer to the nameAddr structure
above. Call it addr.tr.
A
t#pe"ef struct name+""r *a""r1tr;
25.
Q
6eclare a variable with a""r1tr called a""ress.
A
a""r1tr a""ress;
26.
Q
Assuming the variable address above, how would one refer to the city portion of the
record within a C expression?
A
a""ress23cit#
27.
Q
What is the difference between$
A
4inclu"e <st"io5h3
and
4inclu"e "st"io5h"
A They both specify a file for inclusion into the current source file. The difference is
where the file stdio.h is expected to be. In the case of the bracets, the compiler will loo
in all the default locations. In the case of the 0uotes, the compiler will only loo in the
current directory.
28.
Q
What is 4if"ef use" for6
A
It is used for condition compilation. ,pecifically the source code between 9ifdef and
9endif &or 9else* is compiled if the associated symbol is defined to the compiler.
28.
Q
"ow do you define a constant in C?
A
The C language itself has no provision for constants. "owever, its companion program,
the preprocessor, can be used to mae manifest constants. It does this through the use of
the 4"efine eyword.
29.
Q
Why can-t you nest structure definitions?
A
Tric 0uestion$ :ou can nest structure definitions.
30.
Q
Can you nest function definitions?
A
%o. &:ou can in .ascal, a close relative to C.*
31.
Q
What is a forward reference?
A
It is a reference to a variable or function before it is defined to the compiler. The cardinal
rule of structured languages is that everything must be defined before it can be used.
There are rare occasions where this is not possible. It is possible &and sometimes
necessary* to define two functions in terms of each other. /ne will obey the cardinal rule
while the other will need a forward declaration of the former in order to now of the
former-s existence. Confused?
32.
Q
What are the following and how do they differ$ int, lon7, float and "ou!le?
A
int An integer, usually ;3! <=> in magnitude.
lon7 A larger version of int, usually ;3! <?= in magnitude.
float A single precision real &floating point* number. 4agnitude varies.
"ou!le A double precision real number. 4agnitude varies.
33.
Q
6efine a macro called &89 which s0uares a number.
A
4"efine &89(x) (x * x)
&The parenthesis around 7x * x7 are extremely important because the macro may be
expanded into a place where any embedded spaces could cause the compiler to
misinterpret the expression. The conse0uences could range from a pesy syntax error to
wrong answers when the program is run.*. 4oral$ The preprocessor does not now C.
34.
Q
Is it possible to tae the s0uare!root of a number in C. Is there a s0uare!root operator in
C?
A
:es. There is no s0uare!root operator@ such computation is performed though the use of a
function.
35.
Q
8sing fprintf() print a single floating point number right!Austified in a field of <B
spaces, no leading 5eros, and C decimal places. The destination should be stderr and the
variable is called num.
A
fprintf( st"err% ":2*05;f"% num);
36.
Q
What is the difference between the ) and )) operators and the < and << operators?
A
) and < are bitwise A%6 and /) operators respectively. They are usually used to
manipulate the contents of a variable on the bit level. )) and << are logical A%6 and /)
operators respectively. They are usually used in conditionals.
37.
Q
What is the difference between the 23 and 5 operators?
A
They both provide access to members of a structure or union. They differ in that !D is
used when the variable is a pointer to a structure or union. The dot is used when the
variable is itself the structure or union. The !D operator combines the pointer
dereferencing operator with the member access operator@ it is syntactic 7sugar coating.7
a""ress23cit# is e0uivalent to (*a""ress)5cit#.
38.
Q
What is the symbol for the modulus operator?
A
: &the percent symbol*
39.
Q
+rom the standpoint of logic, what is the difference between the fragment$
if (next < max)
next++;
else
next = 0;
and the fragment$
next += (next < max)6 (1)=(2next);
A %othing. They are different ways to express the same logic.
40.
Q
What does the following fragment do?
while(("=c=7etch()%")>=?@A))(c>=BCtB<<c>=B B<<c>=BC!B))
*!uff++ = ++c;
A
6o the following until either the end of standard input or the variable c taes on the value
of a tab, space, or bacspace character$ ,tore the character that succeeds the character
stored in c into the current location pointed by buff. Then increment buff to point to the
next location in memory. 4eanwhile, d is assigned the same value as c and it is the value
of d that is used in the comparison to #/+.
41.
Q
Is C case sensitive &ie$ does C differentiate between upper and lower case letters*?
A
:es.
41.
Q
,pecify how a filestream called in+ile should be opened for random reading and writing.
the file-s name is in file%ame.
A
inAile = fopen( file(ame% "r+");
42.
Q
What does fopen() return if successful. If unsuccessful?
A
8pon success fopen() returns a pointer to a filestream. /therwise it returns the value
of %811.
43.
Q
What is the 'oi" data type? What is a 'oi" pointer?
A
The void data type is used when no other data type is appropriate. A 'oi" pointer is a
pointer that may point to any ind of obAect at all. It is used when a pointer must be
specified but its type is unnown.
44.
Q
6eclare a pointer called fnc which points to a function that returns an unsi7ne"
lon7.
A
unsi7ne" lon7 (*fnc)();
45.
Q
6eclare a pointer called pfnc which points to a function that returns a pointer to a
structure of type name+""r.
A
struct name+""r *(*pfnc)();
46.
Q
It is possible for a function to return a character, an integer, and a floating point number.
Is it possible for a function to return a structure? Another function?
A
%o. "owever, it is possible to return pointers to structures and functions.
47.
Q
What is the difference between an lvalue and an rvalue?
A
The lvalue refers to the left!hand side of an assignment expression. It must always
evaluate to a memory location. The rvalue represents the right!hand side of an assignment
expression@ it may have any meaningful combination of variables and constants.
48.
Q
Eiven the decimal number <F, how would one express it as a hexadecimal number in C?
A
0x1D
49.
Q
What is malloc()?
A
This function allocates heap storage for dynamic data structures.
50.
Q
What is the difference between malloc() and calloc()?
A
The malloc() function allocates raw memory given a si5e in bytes. /n the other hand,
calloc() clears the re0uested memory to 5eros before return a pointer to it. &It can also
compute the re0uest si5e given the si5e of the base data structure and the number of them
desired.*
51.
Q
What ind of problems was C designed to solve?
A
C was designed to be a 7universal7 assembly language. It is used for producing system
software such as operation systems, compilers3interpreters, device drivers, editors,
6G4,-s and similar things. It is not as well suited to application programs.