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. 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. %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.

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

%e displays floating-point in exponential notation (1.234e+05). %f displays in fixed-point decimal notation (123400.000000). Both take double arguments. Option A correctly describes this distinction. Option C is wrong - %e always uses exponential form, not conditionally.

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

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

The global variable 'i' is used in the for-loop condition and print statement. However, the increment function takes 'int i' as a parameter, which shadows the global variable. The function increments its local parameter, not the global 'i'. Therefore, the global 'i' remains 0 forever, and the loop condition i < 10 is always true, causing an infinite loop.

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. unalloc()

  2. dropmem()

  3. dealloc()

  4. release()

  5. free()

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

free() is the standard C library function used to release dynamically allocated memory that was obtained using malloc(), calloc(), or realloc(). Every call to a memory allocation function should have a corresponding call to free() to prevent memory leaks.

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.

Multiple choice
  1. Yes; it can be referenced through the register specifier.

  2. No; it would need to have been initially declared as a static variable.

  3. No; it would need to have been initially declared using the global keyword.

  4. Yes; it can be referenced through the publish specifier.

  5. Yes; it can be referenced through the extern specifier.

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

A variable declared at file scope without static has external linkage by default. To reference it from another file, it must be declared with the extern keyword.

Multiple choice
  1. If the arguments to memcpy() refer to overlapping regions, the destination buffer will be subject to memory corruption.

  2. Array_dup() declares its first parameter to be a pointer, when the actual argument will be an array.

  3. The memory obtained from alloca() is not valid in the context of the caller. Moreover, alloca() is non-standard.

  4. Size_t is not a standard C defined type, and may not be kn.

  5. The definition of array_dup() is unusual. Functions cannot be defined using this syntax.

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

Option (1) is correct.

Multiple choice
  1. Int sprintf ( char * str, const char * format)

  2. Void sprintf ( char * str, const char * format, ...)

  3. Int sprintf ( int, const char * format, ... )

  4. Int sprintf ( char * str, const char * format, ... )

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

This is the correct answer as it returns integer which represents the number of characters successfully written on string (first argument). Also the three dots at the end represents that it can have variable number of arguments which is required.