C Programming Fundamentals
Test your C programming skills with questions covering pointers, arrays, memory management, operators, control flow, and standard library functions. Ideal for campus placement preparation for BCA, MCA, B.Sc IT, and M.Sc. IT students.
Questions
char *ptr;
char myString[] = abcdefg;
ptr = myString;
ptr += 5;
What string does ptr point to in the sample code above?
- fg
- efg
- defg
- cdefg
- None of the above
int x = 3;
if( x == 2 )
x = 0;
if( x == 3 )
x++;
else x += 2;
What value will x contain when the above sample code is executed?
- 1
- 2
- 3
- 4
- 5
Which of the following will declare a pointer to an integer at address 0x200 in memory?
- int *x; *x = 0x200;
- int *x = &0x200;
- int *x = *0x200;
- int *x = 0x200;
- int *x( &0x200 );
x = 3, counter = 0;
while ((x-1))
{
++counter;
x--;
}
Referring to the sample code above, what will be the value of variable counter after the execution of the code?
- 0
- 1
- 2
- 3
- 4
char ** array [12][12][12];
Consider array, defined above. Which of the following definitions and initializations of p is valid?
- char ** (* p) [12][12] = array;
- char ***** p = array;
- char * (* p) [12][12][12] = array;
- const char ** p [12][12][12] = array;
- char (** p) [12][12] = array;
double x = -3.5, y = 3.5;
printf( "%.0f : %.0f", ceil( x ), ceil( y ) );
printf( "%.0f : %.0f", floor( x ), floor( y ) );
What will the above code print when it is executed?
ceil =>rounds up 3.2=4 floor =>rounds down 3.2=3
- - 3 : 4 - 4 : 3
- - 4 : 4 - 3 : 3
- - 4 : 4 - 3 : 3
- - 4 : 3 - 3 : 4
- - 3 : 3 - 4 : 4
int x = 5;
int y = 2;
char op = '*';
switch (op)
{
default : x += 1;
case '+' : x += y; /It will go to all the cases/
case '-' : x -= y;
}
After the sample code above is executed, what will be the value of variable x?
- 4
- 5
- 6
- 7
- 8
Which of the following functions returns the string representation from a pointer to a time t value?
- Localtime
- Gmtime
- Strtime
- Asctime
- Ctime
short testarray[4][3] = { {1}, {2, 3}, {4, 5, 6} };
printf(%dn, sizeof( testarray));
Assuming a short is two bytes long, what will be printed by the above code?
- It will not compile because not enough initializers are given.
- 6
- 7
- 12
- 24
struct customer *ptr = malloc( sizeof( struct customer ) );
Given the sample allocation for the pointer ptr found above, which of the following statements is used to reallocate ptr to be an array of 10 elements?
- ptr = realloc( ptr, 10 * sizeof( struct customer));
- realloc( ptr, 9 * sizeof( struct customer ) );
- ptr += malloc( 9 * sizeof( struct customer ) );
- ptr = realloc( ptr, 9 * sizeof( struct customer ) );
- realloc( ptr, 10 * sizeof( struct customer ) );
Which of the following is a true statement about pointers?
- Pointer arithmetic is permitted on pointers of any type.
- A pointer of type void * can be used to directly examine or modify an object of any type.
- Standard C mandates a minimum of four levels of indirection accessible through a pointer.
- A C program knows the types of its pointers and indirectly referenced data items at runtime.
- Pointers may be used to simulate call-by-reference.
void (*signal(int sig, void (*handler) (int))) (int);
Which of the following definitions of sighandler_t allows the above declaration to be rewritten as follows?
sighandler_t signal (int sig, sighandler_t handler);
- typedef void (*sighandler_t) (int);
- typedef sighandler_t void (*) (int);
- typedef void *sighandler_t (int);
- #define sighandler_t(x) void (*x) (int)
- #define sighandler_t void (*) (int)
In a C expression, how is a logical AND represented?
- @@
- ||
- AND
- &&
- OR
Which of the following Standard C functions can be used to reset end-of-file and error conditions on an open stream?
- clearerr()
- fseek()
- ferror()
- feof()
- setvbuf()
char buf [] = Hello world!;
char * buf = Hello world!;
In terms of code generation, how do the two definitions of buf, both presented above, differ?
- The first definition certainly allows the contents of buf to be safely modified at runtime while the second definition does not.
- The first definition is not suitable for usage as an argument to a function call but the second definition is.
- The first definition is not legal because it does not indicate the size of the array to be allocated while the second definition is legal.
- They do not differ -- they are functionally equivalent.
- The first definition does not allocate enough space for a terminating NUL-character, nor does it append one while the second definition does.
Which of the following will read a character from the keyboard and store it in the variable c?
- c = getc();
- getc( &c );
- c = getchar( stdin );
- getchar( &c )
- c = getchar();
How do printf()'s format specifiers %e and %f differ in their treatment of floating-point numbers?
- %e always displays an argument of type double in engineering notation; %f always displays an argument of type double in decimal notation.
- %e expects a corresponding argument of type double; %f expects a corresponding argument of type float.
- %e displays a double in engineering notation if the number is very small or very large. Otherwise, it behaves like %f and displays the number in decimal notation.
- %e displays an argument of type double with trailing zeros; %f never displays trailing zeros.
- %e and %f both expect a corresponding argument of type double and format it identically. %e is left over from K&R C; Standard C prefers %f for new code.
Which of the following C operators is right associative?
- =
- ,
- []
- ^
- ->
char ptr1[] = "Hello World";
char *ptr2 = malloc( 5 );
ptr2 = ptr1;
What is wrong with the above code (assuming the call to malloc does not fail)?
- There will be a memory overwrite.
- There will be a memory leak.
- There will be a segmentation fault.
- Not enough space is allocated by the malloc.
- It will not compile.
What does the auto specifier do?
- It automatically initializes a variable to 0.
- It indicates that a variable's memory will automatically be preserved.
- It automatically increments the variable when used.
- It automatically initializes a variable to NULL.
- It indicates that a variable's memory space is allocated upon entry into the block.
#include <stdio.h>
int i;
void increment( int i )
{
i++;
}
int main()
{
for( i = 0; i < 10; increment( i ) )
{
}
printf(i=%dn, i);
return 0;
}
What will happen when the program above is compiled and executed?
- It will not compile.
- It will print out: i=9.
- It will print out: i=10.
- It will print out: i=11.
- It will loop indefinitely
How do you include a system header file called sysheader.h in a C source file?
- #include <sysheader.h>
- #incl "sysheader.h"
- #includefile <sysheader>
- #include sysheader.h
- #incl <sysheader.h>
Which one of the following printf() format specifiers indicates to print a double value in decimal notation, left aligned in a 30-character field, to four (4) digits of precision?
- %- 30.4e
- %4.30e
- %-4.30f
- %-30.4f
- %#30.4f