C Programming Fundamentals and Standard Specification

Test your knowledge of C programming concepts including pointers, arrays, memory management, file I/O, and Standard C specifications. Suitable for campus placement preparation and intermediate C programmers.

25 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

int x = 0;
for ( ; ; )
{
if (x++ == 4)
break;
continue;
}
printf(x=%dn, x);

What will be printed when the sample code above is executed?

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

Which of the following variables name is NOT valid?

  1. go_cart
  2. go4it
  3. 4season
  4. run4
  5. what
Question 3 Multiple Choice (Single Answer)

int i = 4;
int x = 6;
double z;
z = x / i;
printf(z=%.2fn, z);

What will print when the sample code above is executed?

  1. z = 0.00
  2. z = 1.00
  3. z = 1.50
  4. z = 2.00
  5. z = NULL
Question 4 Multiple Choice (Single Answer)

int y[4] = {6, 7, 8, 9};
int *ptr = y + 2;
printf(%dn, ptr[ 1 ] ); /ptr+1 == ptr[1]/

What is printed, when the sample code above is executed?

  1. 6
  2. 7
  3. 8
  4. 9
  5. The code will not compile
Question 5 Multiple Choice (Single Answer)

char txt [20] = Hello world!;

How many bytes are allocated by the definition above?

  1. 11
  2. 12
  3. 13
  4. 20
  5. 21
Question 6 Multiple Choice (Single Answer)

What are two predefined FILE pointers in C?

  1. Stdout and stderr
  2. Console and error
  3. Stdout and stdio
  4. Stdio and stderr
  5. Errout and conout
Question 7 Multiple Choice (Single Answer)

Which of the following statements allocates enough space to hold an array of 10 integers that are initialized to 0?

  1. int *ptr = (int *) malloc(10, sizeof(int));
  2. int *ptr = (int *) calloc(10, sizeof(int));
  3. int *ptr = (int ) malloc(10sizeof(int));
  4. int *ptr = (int ) alloc(10sizeof(int));
  5. int *ptr = (int ) calloc(10sizeof(int));
Question 8 Multiple Choice (Single Answer)

penny = one
nickel = five
dime = ten
quarter = twenty-five

How is enum used to define the values of the American coins listed above?

  1. enum coin {(penny,1), (nickel,5), (dime,10), (quarter,25)};
  2. enum coin ({penny,1}, {nickel,5}, {dime,10}, {quarter,25});
  3. enum coin {penny=1,nickel=5,dime=10,quarter=25};
  4. enum coin (penny=1,nickel=5,dime=10,quarter=25);
  5. enum coin {penny, nickel, dime, quarter} (1, 5, 10, 25);
Question 9 Multiple Choice (Single Answer)

int a [8] = { 0, 1, 2, 3 };

The definition of a above explicitly initializes its first four elements. Which one of the following describes how the compiler treats the remaining four elements?

  1. Standard C defines this particular behavior as implementation-dependent. The compiler writer has the freedom to decide how the remaining elements will be handled.
  2. The remaining elements are initialized to zero(0).
  3. It is illegal to initialize only a portion of the array. Either the entire array must be initialized, or no part of it may be initialized.
  4. As with an enum, the compiler assigns values to the remaining elements by counting up from the last explicitly initialized element. The final four elements will acquire the values 4, 5, 6, and 7, respectively.
  5. They are left in an uninitialized state; their values cannot be relied upon.
Question 10 Multiple Choice (Single Answer)

According to the Standard C specification, what are the respective minimum sizes (in bytes) of the following three data types: short; int; and long?

  1. 1, 2, 2
  2. 1, 2, 4
  3. 1, 2, 8
  4. 2, 2, 4
  5. 2, 4, 8
Question 11 Multiple Choice (Single Answer)

Which of the following is a true statement about pointers?

  1. They are always 32-bit values.
  2. For efficiency, pointer values are always stored in machine registers.
  3. With the exception of generic pointers, similarly typed pointers may be subtracted from each other.
  4. A pointer to one type may not be cast to a pointer to any other type.
  5. With the exception of generic pointers, similarly typed pointers may be added to each other.
Question 12 Multiple Choice (Single Answer)

long factorial (long x)
{
????
return x * factorial(x - 1);
}

With what do you replace the ???? to make the function shown above return the correct answer?

  1. if (x == 0) return 0;
  2. return 1;
  3. if (x >= 2) return 2;
  4. if (x == 0) return 1;
  5. if (x <= 1) return 1;
Question 13 Multiple Choice (Single Answer)

When applied to a variable, what does the unary & operator yield?

  1. The variable's value.
  2. The variable's binary form.
  3. The variable's address.
  4. The variable's format.
  5. The variable's right value.
Question 14 Multiple Choice (Single Answer)

How is a variable accessed from another file?

  1. The global variable is referenced via the extern specifier.
  2. The global variable is referenced via the auto specifier.
  3. The global variable is referenced via the global specifier.
  4. The global variable is referenced via the pointer specifier.
  5. The global variable is referenced via the ext specifier.
Question 15 Multiple Choice (Single Answer)

Which one of the following is not a valid identifier?

  1. Ident
  2. Auto
  3. BigNumber
  4. G42277
  5. Peaceful_in_space
Question 16 Multiple Choice (Single Answer)

u32 X (f32 f)
{
union {
f32 f;
u32 n;
} u;
u.f = f;
return u.n;
}

Given the function X() is defined above, assume that u32 is a type-definition indicative of a 32-bit unsigned integer and that f32 is a type-definition indicative of a 32-bit floating-point number.
Which of the following describes the purpose of the function defined above?

  1. X() effectively rounds f to the nearest integer value, which it returns.
  2. X() effectively performs a standard typecast and converts f to a roughly equivalent integer.
  3. X() preserves the bit-pattern of f, which it returns as an unsigned integer of equal size.
  4. Since u.n is never initialized, X() returns an undefined value. This function is therefore a primitive pseudo random number generator.
  5. Since u.n is automatically initialized to zero (0) by the compiler, X() is an obtuse way of always obtaining a zero (0) value.
Question 17 Multiple Choice (Single Answer)

/* Increment each integer in the array 'p' of * size 'n'. /
void increment_ints (int p [/n/], int n)
{
assert(p != NULL); /
Ensure that 'p' isn't a null pointer. /
assert(n >= 0); /
Ensure that 'n' is nonnegative. /
while (n) /
Loop over 'n' elements of 'p'. */
{
p++; / Increment *p. /
p++, n--; /
Increment p, decrement n. */
}
}

Consider the function increment_ints(), defined above. Despite its significant inline commentary, it contains an error. Which one of the following correctly assesses it?

  1. *p++ causes p to be incremented before the reference is performed because both operators have equal precedence and are right associative.
  2. An array is a non-modifiable value, so p cannot be incremented directly. A navigation pointer should be used in conjunction with p.
  3. *p++ causes p to be incremented before the dereference is performed, because the auto-increment operator has higher precedence than the indirection operator.
  4. The condition of a while loop must be a Boolean expression. The condition should be n != 0.
  5. An array cannot be initialized to a variable size. The subscript n should be removed from the definition of the parameter p.
Question 18 Multiple Choice (Single Answer)

FILE *f = fopen( fileName, "r" );
readData( f );
if( ???? )
{
puts( "End of file was reached" );
}

Which one of the following can replace the ???? in the code above to determine if the end of a file has been reached?

  1. f == EOF
  2. feof( f )
  3. eof( f )
  4. f == NULL
  5. !f
Question 19 Multiple Choice (Single Answer)

Global variables that are declared static are ____________.
Which one of the following correctly completes the sentence above?

  1. Deprecated by Standard C.
  2. Internal to the current translation unit.
  3. Visible to all translation units.
  4. Read-only subsequent to initialization.
  5. Allocated on the heap.
Question 20 Multiple Choice (Single Answer)

/* sys/cdef.h /
#if defined(STDC) || defined(__cplusplus)
#define __P(protos) protos
#else
#define __P(protos) ()
#endif
/
stdio.h */
#include <sys/cdefs.h>
div_t div __P((int, int));

The code above comes from header files for the FreeBSD implementation of the C library. What is the primary purpose of the __P() macro?

  1. The __P() macro has no function, and merely obfuscates library function declarations. It should be removed from further releases of the C library.
  2. The __P() macro provides forward compatibility for C++ compilers, which do not recognize Standard C prototypes.
  3. Identifiers that begin with two underscores are reserved for C library implementations. It is impossible to determine the purpose of the macro from the context given.
  4. The __P() macro provides backward compatibility for K&R C compilers, which do not recognize Standard C prototypes.
  5. The __P() macro serves primarily to differentiate library functions from application-specific functions.
Question 21 Multiple Choice (Single Answer)

/* Read a double value from fp. */
double read_double (FILE * fp) {
double d;
assert(fp != NULL);
fscanf(fp, %lf, d);
return d;
}

The code above contains a common error. Which one of the following describes it?

  1. fscanf() will fail to match floating-point numbers not preceded by whitespace.
  2. The format specifier %lf indicates that the corresponding argument should be long double rather than double.
  3. The call to fscanf() requires a pointer as its last argument.
  4. The format specifier %lf is recognized by fprintf() but not by fscanf().
  5. D must be initialized prior to usage.
Question 22 Multiple Choice (Single Answer)

/* Read an arbitrarily long string. */
int read_long_string (const char ** const buf) {
char * p = NULL;
const char * fwd = NULL;
size_t len = 0;
assert(buf);
do
{
p = realloc(p, len += 256);
if (!p)
return 0;
if (!fwd)
fwd = p;
else
fwd = strchr(p, ' ');
} while (fgets(fwd, 256, stdin));
*buf = p;
return 1;
}

The function read_long_string(), defined above, contains an error that may be particularly visible under heavy stress. Which one of the following describes it?

  1. The write to *buf is blocked by the const qualifications applied to its type.
  2. If the null pointer for char is not zero-valued on the host machine, the implicit comparisons to zero (0) may introduce undesired behavior. Moreover, even if successful, it introduces machine-dependent behavior and harms portability.
  3. The symbol stdin may not be defined on some ANCI C compliant systems.
  4. The else causes fwd to contain an errant address.
  5. If the call to realloc() fails during any iteration but the first, all memory previously allocated by the loop is leaked.
Question 23 Multiple Choice (Single Answer)

According to Standard C, what is the type of an unsuffixed floating-point literal, such as 123.45?

  1. Long double
  2. Unspecified
  3. Float
  4. Double
  5. Signed float
Question 24 Multiple Choice (Single Answer)

Which one of the following is true for identifiers that begin with an underscore?

  1. They are generally treated differently by preprocessors and compilers from other identifiers.
  2. They are case-insensitive.
  3. They are reserved for usage by standards committees, system implementers, and compiler engineers.
  4. Applications programmers are encouraged to employ them in their own code in order to mark certain symbols for internal usage.
  5. They are deprecated by Standard C and are permitted only for backward compatibility with older C libraries.
Question 25 Multiple Choice (Single Answer)

Which one of the following is not a valid C identifier?

  1. ___S
  2. 1___
  3. ___1
  4. ___
  5. S___

Practice this chapter

Fill in the Blanks (13781 questions)