Home Other Languages C Basic C Questions - 15

Interview Dates

« < September 2010 > »
S M T W T F S
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2
Subscribe to cyberqbank.com

Powered by
in.groups.yahoo.com

Login Form




Basic C Questions - 15 Print E-mail

1.  An expression consisting solely of literals can appear anywhere that literals of the same type can appear.  Which one of the following correctly assesses the statement above?

  •  The statement is true.
  •  The statement is false. A constant expression may contain side effects that prevent it from being evaluated at compile time. Since it cannot be universally permitted, it is universally prohibited.
  •  The statement is false. While a constant expression could logically appear wherever a literal could appear, the Standard C committee chose to allow constant expressions only in runtime contexts.
  •  The statement is false. A constant expression cannot appear as the operand of sizeof or in the size definition of an array.
  •  The statement is false. To simplify the design of the compiler, C compiler implementers are not obliged to evaluate constant expressions at compile time.

 

2. What is static memory allocation and dynamic memory allocation?

Static memory allocation : The compiler allocates the required memory space for a declared variable.By using the address of operator,the reserved address is obtained and this address may be assigned to a pointer variable.Since most of the declared variable have static memory,this way of assigning pointer value to a pointer variable is known as static memory allocation. memory is assigned during compilation time.

Dynamic memory allocation : It uses functions such as malloc( ) or calloc( ) to get memory dynamically.If these functions are used to get memory dynamically and the values returned by these functions are assingned to pointer variables, such assignments are known as dynamic memory allocation.memory is assined during run time.

 

3. What is a pointer variable?

A pointer variable is a variable that may contain the address of another variable or any valid address in the memory.

 

4. How are pointer variables initialized?

Pointer variable are initialized by one of the following two ways

- Static memory allocation
- Dynamic memory allocation

 

5. Sample Code
char buf[ 50 ] = "Hello World";
char *ptr = buf + 5;

From the sample above, what is the proper way to copy 20 bytes from the location pointed to by ptr to the beginning of buf?

  •  memcpy( buf, ptr, 20 );
  •  It cannot be done because the source and destination overlap.
  •  strncpy( buf, ptr, 20 );
  •  That may cause an illegal memory access because it will read memory past the end of the string.
  •  memmove( buf, ptr, 20 );

 

6. Sample Code
char*(*(*x)(void))[];

What does the above statement declare?

  •  An array of pointers to a function with no parameters returning a pointer to a string pointer
  •  An array of pointers to a pointer to a function with no parameters returning a string
  •  A pointer to an array of functions with no parameters returning a string
  •  A pointer to a function with no parameters that returns a pointer to an array of strings
  •  It is a syntactically incorrect statement.

 

7. What is modular programming?

If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming.

 

8. When would you use a pointer to a function?

Pointers to functions are interesting when you pass them to other functions. A function that takes function pointers says, in effect, Part of what I do can be customized. Give me a pointer to a function, and I’ll call it when that part of the job needs to be done. That function can do its part for me. This is known as a callback. It’s used a lot in graphical user interface libraries, in which the style of a display is built into the library but the contents of the display are part of the application.

As a simpler example, say you have an array of character pointers (char*s), and you want to sort it by the value of the strings the character pointers point to. The standard qsort() function uses function pointers to perform that task. qsort() takes four arguments,

- a pointer to the beginning of the array,
- the number of elements in the array,
- the size of each array element, and
- a comparison function, and returns an int.

 

9. In C, why is the void pointer useful? When would you use it?

The void pointer is useful becuase it is a generic pointer that any pointer can be cast into and back again without loss of information.

 

10. Sample Code
void myFunc (int x)
{
if (x > 0) myFunc(--x);
printf("%d, ", x);
}

int main()
{
myFunc(5);
return 0;
}

What will the above sample code produce when executed?

  •  5, 4, 3, 2, 1, 0,
  •  0, 0, 1, 2, 3, 4,
  •  4, 3, 2, 1, 0, 0,
  •  1, 2, 3, 4, 5, 5,
  •  0, 1, 2, 3, 4, 5,




     

 

  No Comments.
Quick Post


Discuss this item on the forums. (0 posts)