C Programming -- Sample Questions

Embed Size (px)

Citation preview

  • 8/11/2019 C Programming -- Sample Questions

    1/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 Ccompiler 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;

    andnext++;

    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

  • 8/11/2019 C Programming -- Sample Questions

    2/12

    appear including newlines. The comment is finished with an asteris followed by a slash.

    #xample$/* This is a comment */

    6.

    QCan 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?

    AIf 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++; /* upate */$

    andfor ( next = 0; next < max; next++) /* setup%test *//* an upate */printf("ello"); /* !o# */

    9.

    Q

    pecify a C loop with the test at the bottom.

    A

    next = 0; /* setup */o {printf("ello"); /* !o# */next++; /* upate */$ while ( next < max); /* test */

  • 8/11/2019 C Programming -- Sample Questions

    3/12

    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 maesthe 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?

  • 8/11/2019 C Programming -- Sample Questions

    4/12

    A

    A function prototype tells the compiler to expect a given function to be used in a givenway. 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 theappropriate 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 thevariable-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

    static2ariables are defined in a nonvolatile region of memory such that they retain theircontents though out the program-s execution.

    registerAss 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 contentsand identity when the function it which it is defined terminates.

    externalTells the compiler that the variable is defined in another module.

    volatileTells the compiler that other programs will be modifying this variable in additionto 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 programto function correctly in a larger context.

    19.

    Q

    tate the syntax for theprintf()and scanf()functions. tate their one crucial

    difference with respect to their parameters.

  • 8/11/2019 C Programming -- Sample Questions

    5/12

  • 8/11/2019 C Programming -- Sample Questions

    6/12

    23.

    Q

    What does the t#peefeyword do?

    A

    This eyword provides a short!hand way to write variable declarations. It is not a truedata 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 structureabove. Call it addrtr.

    A

    t#peef struct namer *artr;

    25.

    Q

    6eclare a variable with artrcalled aress.

    A

    artr aress;

    26.

    Q

    Assuming the variable address above, how would one refer to the city portion of the

    record within a C expression?

    A

    aress23cit#

    27.

    Q

    What is the difference between$

    A

    4inclue

  • 8/11/2019 C Programming -- Sample Questions

    7/12

    28.

    Q

    What is 4ifef use for6

    A

    It is used for condition compilation. pecifically the source code between 9ifdef and9endif &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 4efineeyword.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 sometimesnecessary* 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?

  • 8/11/2019 C Programming -- Sample Questions

    8/12

    32.

    Q

    What are the following and how do they differ$ int, lon7, floatand ou!le?

    A

    intAn integer, usually ;3! in magnitude.

    lon7A larger version of int, usually ;3!

  • 8/11/2019 C Programming -- Sample Questions

    9/12

    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 23and 5operators?

    A

    They both provide access to members of a structure or union. They differ in that !D isused 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.7aress23cit#is e0uivalent to (*aress)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++;elsenext = 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>=BCtBc>=B Bc>=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 characterstored in c into the current location pointed by buff. Then increment buff to point to the

  • 8/11/2019 C Programming -- Sample Questions

    10/12

    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( fileame% "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 'oidata type? What is a 'oipointer?

    A

    The void data type is used when no other data type is appropriate. A'oipointer is a

    pointer that may point to any ind of obect at all. It is used when a pointer must be

    specified but its type is unnown.

    44.

    Q

    6eclare a pointer called fncwhich points to a function that returns an unsi7nelon7.

    A

    unsi7ne lon7 (*fnc)();

  • 8/11/2019 C Programming -- Sample Questions

    11/12

    45.

    Q

    6eclare a pointer calledpfncwhich points to a function that returns a pointer to a

    structure of type namer.

    A

    struct namer *(*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 assignmentexpression@ it may have any meaningful combination of variables and constants.

    48.

    QEiven the decimal number

  • 8/11/2019 C Programming -- Sample Questions

    12/12

    A

    Themalloc()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.