Computer Knowledge

Programming Fundamentals

2,381 Questions

Programming fundamentals form the core logic of software development and computer science. This includes variable declarations, pointer assignments, loop iterations, and exception handling. These technical topics are regularly tested in computer knowledge and IT officer competitive examinations.

Variables and arraysPointer assignmentsLoop iterationsException handling blocksCompile time errorsFunction references

Programming Fundamentals Questions

Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The unary & operator in C, when applied to a variable, returns the memory address of that variable. This is used for pointer operations and passing references to functions. Option A is incorrect because the variable's value is obtained by simply using the variable name. Option B describes a nonexistent operation. Option D and E are not valid C concepts.

Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

To access a global variable defined in another file, you must declare it with the extern specifier, which tells the compiler the variable exists elsewhere. The extern declaration doesn't allocate storage, it just references the existing definition. Option B is wrong because auto is for local variables. Option C and D are not valid specifiers. Option E uses an invalid specifier name.

Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The code defines a union containing a 32-bit floating-point number (f) and a 32-bit unsigned integer (n) sharing the same memory location. Assigning the float value to u.f and returning u.n reads the exact raw bit representation of the float as an integer without performing numerical type conversion.

Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
E Correct answer
Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
D Correct answer
Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The function fscanf() requires pointers to variables so it can store the parsed input values into them. Because 'd' is passed by value as a double rather than by reference as a pointer (such as &d), the function cannot update it properly and will cause undefined behavior.

Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The function parameter is declared as const char ** const buf. The outer const means the pointer buf itself cannot be modified. The inner const means the char* pointers that buf points to cannot be modified. However, *buf = p attempts to modify buf[0] (the first char* pointer), which violates the inner const qualification. This would be caught by the compiler. Option B is incorrect - NULL comparisons are fine. Option C is wrong - stdin is always defined in ANSI C. Option D is incorrect - the else logic is valid. Option E describes a real leak but isn't the primary error a compiler would catch.

Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

In the C programming language, identifiers beginning with an underscore followed by an uppercase letter or another underscore, as well as identifiers beginning with an underscore in the global scope, are reserved for use by the implementation, standard libraries, and compiler vendors to prevent naming collisions.

Multiple choice
  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;

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The array is declared as char ** array[12][12][12], which is a 3D array of pointers to char . To point to this array, we need a pointer to the first element, which is char * (*)[12][12]. This matches option A. Options B and C have incorrect indirection levels. D adds const incorrectly. E has wrong pointer type.

Multiple choice
  1. It will not compile because not enough initializers are given.

  2. 6

  3. 7

  4. 12

  5. 24

Reveal answer Fill a bubble to check yourself
E Correct answer
Explanation

testarray[4][3] has 12 elements total (4 rows × 3 columns). Each short is 2 bytes, so sizeof returns 12 × 2 = 24 bytes. The initializer fills only some elements, but sizeof still counts the full array declaration.

Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
E Correct answer
Explanation

A: False - void* pointers don't allow arithmetic. B: False - void* must be cast to dereference. C: False - C standard doesn't mandate minimum indirection levels. D: False - C has no runtime type checking. E: True - passing pointers lets functions modify original data, simulating call-by-reference.

Multiple choice
  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)

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The declaration means: signal is a function returning 'pointer to function taking int returning void'. To simplify, we typedef the return type: typedef void (*sighandler_t)(int). Then: sighandler_t signal(int sig, sighandler_t handler). This matches option A exactly.

Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
D Correct answer