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.

23 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

char *ptr;
char myString[] = abcdefg;
ptr = myString;
ptr += 5;
What string does ptr point to in the sample code above?

  1. fg
  2. efg
  3. defg
  4. cdefg
  5. None of the above
Question 2 Multiple Choice (Single Answer)

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. 1
  2. 2
  3. 3
  4. 4
  5. 5
Question 3 Multiple Choice (Single Answer)

Which of the following will declare a pointer to an integer at address 0x200 in memory?

  1. int *x; *x = 0x200;
  2. int *x = &0x200;
  3. int *x = *0x200;
  4. int *x = 0x200;
  5. int *x( &0x200 );
Question 4 Multiple Choice (Single Answer)

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?

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
Question 5 Multiple Choice (Single Answer)

char ** array [12][12][12];
Consider array, defined above. Which of the following definitions and initializations of p is valid?

  1. char ** (* p) [12][12] = array;
  2. char ***** p = array;
  3. char * (* p) [12][12][12] = array;
  4. const char ** p [12][12][12] = array;
  5. char (** p) [12][12] = array;
Question 6 Multiple Choice (Single Answer)

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

  1. - 3 : 4 - 4 : 3
  2. - 4 : 4 - 3 : 3
  3. - 4 : 4 - 3 : 3
  4. - 4 : 3 - 3 : 4
  5. - 3 : 3 - 4 : 4
Question 7 Multiple Choice (Single Answer)

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?

  1. 4
  2. 5
  3. 6
  4. 7
  5. 8
Question 8 Multiple Choice (Single Answer)

Which of the following functions returns the string representation from a pointer to a time t value?

  1. Localtime
  2. Gmtime
  3. Strtime
  4. Asctime
  5. Ctime
Question 9 Multiple Choice (Single Answer)

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?

  1. It will not compile because not enough initializers are given.
  2. 6
  3. 7
  4. 12
  5. 24
Question 10 Multiple Choice (Single Answer)

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?

  1. ptr = realloc( ptr, 10 * sizeof( struct customer));
  2. realloc( ptr, 9 * sizeof( struct customer ) );
  3. ptr += malloc( 9 * sizeof( struct customer ) );
  4. ptr = realloc( ptr, 9 * sizeof( struct customer ) );
  5. realloc( ptr, 10 * sizeof( struct customer ) );
Question 11 Multiple Choice (Single Answer)

Which of the following is a true statement about pointers?

  1. Pointer arithmetic is permitted on pointers of any type.
  2. A pointer of type void * can be used to directly examine or modify an object of any type.
  3. Standard C mandates a minimum of four levels of indirection accessible through a pointer.
  4. A C program knows the types of its pointers and indirectly referenced data items at runtime.
  5. Pointers may be used to simulate call-by-reference.
Question 12 Multiple Choice (Single Answer)

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);

  1. typedef void (*sighandler_t) (int);
  2. typedef sighandler_t void (*) (int);
  3. typedef void *sighandler_t (int);
  4. #define sighandler_t(x) void (*x) (int)
  5. #define sighandler_t void (*) (int)
Question 13 Multiple Choice (Single Answer)

In a C expression, how is a logical AND represented?

  1. @@
  2. ||
  3. AND
  4. &&
  5. OR
Question 14 Multiple Choice (Single Answer)

Which of the following Standard C functions can be used to reset end-of-file and error conditions on an open stream?

  1. clearerr()
  2. fseek()
  3. ferror()
  4. feof()
  5. setvbuf()
Question 15 Multiple Choice (Single Answer)

char buf [] = Hello world!;
char * buf = Hello world!;
In terms of code generation, how do the two definitions of buf, both presented above, differ?

  1. The first definition certainly allows the contents of buf to be safely modified at runtime while the second definition does not.
  2. The first definition is not suitable for usage as an argument to a function call but the second definition is.
  3. 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.
  4. They do not differ -- they are functionally equivalent.
  5. The first definition does not allocate enough space for a terminating NUL-character, nor does it append one while the second definition does.
Question 16 Multiple Choice (Single Answer)

Which of the following will read a character from the keyboard and store it in the variable c?

  1. c = getc();
  2. getc( &c );
  3. c = getchar( stdin );
  4. getchar( &c )
  5. c = getchar();
Question 17 Multiple Choice (Single Answer)

How do printf()'s format specifiers %e and %f differ in their treatment of floating-point numbers?

  1. %e always displays an argument of type double in engineering notation; %f always displays an argument of type double in decimal notation.
  2. %e expects a corresponding argument of type double; %f expects a corresponding argument of type float.
  3. %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.
  4. %e displays an argument of type double with trailing zeros; %f never displays trailing zeros.
  5. %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.
Question 18 Multiple Choice (Single Answer)

Which of the following C operators is right associative?

  1. =
  2. ,
  3. []
  4. ^
  5. ->
Question 19 Multiple Choice (Single Answer)

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

  1. There will be a memory overwrite.
  2. There will be a memory leak.
  3. There will be a segmentation fault.
  4. Not enough space is allocated by the malloc.
  5. It will not compile.
Question 20 Multiple Choice (Single Answer)

What does the auto specifier do?

  1. It automatically initializes a variable to 0.
  2. It indicates that a variable's memory will automatically be preserved.
  3. It automatically increments the variable when used.
  4. It automatically initializes a variable to NULL.
  5. It indicates that a variable's memory space is allocated upon entry into the block.
Question 21 Multiple Choice (Single Answer)

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

  1. It will not compile.
  2. It will print out: i=9.
  3. It will print out: i=10.
  4. It will print out: i=11.
  5. It will loop indefinitely
Question 22 Multiple Choice (Single Answer)

How do you include a system header file called sysheader.h in a C source file?

  1. #include <sysheader.h>
  2. #incl "sysheader.h"
  3. #includefile <sysheader>
  4. #include sysheader.h
  5. #incl <sysheader.h>
Question 23 Multiple Choice (Single Answer)

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?

  1. %- 30.4e
  2. %4.30e
  3. %-4.30f
  4. %-30.4f
  5. %#30.4f