Computer Knowledge

Programming Fundamentals

2,611 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 __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
B Correct answer
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
B Correct answer
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. 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
Multiple choice
  1. c = getc();

  2. getc( &c );

  3. c = getchar( stdin );

  4. getchar( &c )

  5. c = getchar();

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

getchar() reads one character from stdin and returns it as an int (or EOF). Correct usage: c = getchar(). Option A uses getc() without stream argument. Options B and D pass address to getchar (wrong - it takes no arguments). Option C passes stdin to getchar (wrong signature). Only option E is correct.

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

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

The auto specifier in C indicates that a variable's memory storage duration is automatic - it's allocated when the block is entered and automatically deallocated when the block exits. This is the default storage class for local variables.

Multiple choice
  1. TRUE

  2. FALSE

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

In standard LPP formulation, all variables must be non-negative (x ≥ 0). Unrestricted variables (which can be negative) must be transformed using substitution x = x' - x'' where x', x'' ≥ 0 before solving.

Multiple choice
  1. ptr = ptr + sizeof(myStruct);

  2. ++(int*)ptr;

  3. ptr = ptr + sizeof(myArray);

  4. increment(ptr);

  5. ptr = ptr + sizeof(ptr);

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

When working with void pointers, you must manually increment by the size of the data type. Option A ptr = ptr + sizeof(myStruct) correctly advances the pointer by one struct size. Options B, C, and E are incorrect syntax or logic, and option D is not a standard function.

Multiple choice
  1. It will work correctly since the for loop covers the entire list.

  2. It may fail since each node nPtr is freed before its next address can be accessed.

  3. In the for loop, the assignment nPtr=nPtr->next should be changed to nPtr=nPtr.next.

  4. This is invalid syntax for freeing memory.

  5. The loop will never end.

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

The code frees nPtr before accessing nPtr->next, creating a use-after-free bug. Once free() is called, accessing nPtr->next is undefined behavior. The correct approach is to save nPtr->next before freeing nPtr.

Multiple choice
  1. MAX_NUM is an integer variable.

  2. MAX_NUM is a linker constant.

  3. MAX_NUM is a precompiler constant.

  4. MAX_NUM is a preprocessor macro.

  5. MAX_NUM is an integer constant.

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

#define creates a preprocessor macro, not a variable or constant. The preprocessor replaces MAX_NUM with 15 before compilation. It's not stored as a variable in memory - it's textual substitution. Preprocessor macros are different from const variables (which are actually stored) and linker constants.

Multiple choice
  1. setbuf(stdout, FALSE);

  2. setvbuf(stdout, NULL);

  3. setbuf(stdout, NULL);

  4. setvbuf(stdout, _IONBF);

  5. setbuf(stdout, _IONBF);

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

setbuf(stdout, NULL) turns off buffering by setting the buffer to NULL. This causes unbuffered output. setvbuf requires specifying buffer size and mode. _IONBF is a mode constant for setvbuf, not a buffer pointer. FALSE is not valid for setbuf.