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
-
c = getc();
-
getc( &c );
-
c = getchar( stdin );
-
getchar( &c )
-
c = getchar();
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.
-
%e always displays an argument of type double in engineering notation; %f always displays an argument of type double in decimal notation.
-
%e expects a corresponding argument of type double; %f expects a corresponding argument of type float.
-
%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.
-
%e displays an argument of type double with trailing zeros; %f never displays trailing zeros.
-
%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.
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.
-
It automatically initializes a variable to 0.
-
It indicates that a variable's memory will automatically be preserved.
-
It automatically increments the variable when used.
-
It automatically initializes a variable to NULL.
-
It indicates that a variable's memory space is allocated upon entry into the block.
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.
-
It will not compile.
-
It will print out: i=9.
-
It will print out: i=10.
-
It will print out: i=11.
-
It will loop indefinitely
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.
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.
-
unalloc()
-
dropmem()
-
dealloc()
-
release()
-
free()
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.
-
ptr = ptr + sizeof(myStruct);
-
++(int*)ptr;
-
ptr = ptr + sizeof(myArray);
-
increment(ptr);
-
ptr = ptr + sizeof(ptr);
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.
-
It will work correctly since the for loop covers the entire list.
-
It may fail since each node nPtr is freed before its next address can be accessed.
-
In the for loop, the assignment nPtr=nPtr->next should be changed to nPtr=nPtr.next.
-
This is invalid syntax for freeing memory.
-
The loop will never end.
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.
-
MAX_NUM is an integer variable.
-
MAX_NUM is a linker constant.
-
MAX_NUM is a precompiler constant.
-
MAX_NUM is a preprocessor macro.
-
MAX_NUM is an integer constant.
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.
-
setbuf(stdout, FALSE);
-
setvbuf(stdout, NULL);
-
setbuf(stdout, NULL);
-
setvbuf(stdout, _IONBF);
-
setbuf(stdout, _IONBF);
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.
-
t = clock();
-
time( &t )
-
t = ctime();
-
t = localtime();
-
None of the above
B
Correct answer
Explanation
The time() function takes a pointer to a time_t variable and sets it to the current calendar time, returning the same value.
-
Yes; it can be referenced through the register specifier.
-
No; it would need to have been initially declared as a static variable.
-
No; it would need to have been initially declared using the global keyword.
-
Yes; it can be referenced through the publish specifier.
-
Yes; it can be referenced through the extern specifier.
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.
-
If the arguments to memcpy() refer to overlapping regions, the destination buffer will be subject to memory corruption.
-
Array_dup() declares its first parameter to be a pointer, when the actual argument will be an array.
-
The memory obtained from alloca() is not valid in the context of the caller. Moreover, alloca() is non-standard.
-
Size_t is not a standard C defined type, and may not be kn.
-
The definition of array_dup() is unusual. Functions cannot be defined using this syntax.
C
Correct answer
Explanation
This is a new data type introduced in C99, standard of C++.
-
Int sprintf ( char * str, const char * format)
-
Void sprintf ( char * str, const char * format, ...)
-
Int sprintf ( int, const char * format, ... )
-
Int sprintf ( char * str, const char * format, ... )
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.