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 C standard says that dereferencing a null pointer causes undefined behavior. This may explain why printf() apparently fails.

  2. If the standard output stream is buffered, the library buffers may not be flushed before the crash occurs.

  3. printf() always buffers output until a new line character appears in the buffer. Since no newline was present in the format string, nothing is printed.

  4. There is insufficient information to determine why the output fails to appear. A broader context is required.

  5. printf() expects more than a single argument. Since only one argument is given, the crash may actually occur inside printf(), which explains why the string is not printed. puts() should be used instead.

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

Standard output is typically line-buffered or fully buffered. If the program crashes before a newline is encountered or the buffer is flushed, the output may not appear.

Multiple choice
  1. 4

  2. 5

  3. 6

  4. 7

  5. 8

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

The array initialization contains 6 strings separated by commas: 'Sleepy', 'Dopey Doc', 'Happy', 'Grumpy Sneezy', 'Bashful'. Wait, the list is: Sleepy, Dopey Doc, Happy, Grumpy Sneezy, Bashful. That is 5 elements.

Multiple choice
  1. The ellipsis is a throwback from K&R C. In accordance with Standard C, the declaration of args should be moved into the parameter list, and the K&R C macro va_arg() should be deleted from the code.

  2. vfprintf() does not conform to ISO 9899: 1990, and may not be portable.

  3. Library routines that accept argument lists cause a fault on receipt of an empty list. The argument list must be validated with va_null() before invoking vfprintf().

  4. The argument list args has been improperly initialized.

  5. Variadic functions are discontinued by Standard C; they are legacy constructs from K&R C, and no longer compile under modern compilers.

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

In the provided function, args is initialized using va_arg(fmt, va_list), which is incorrect because va_arg requires a type and advances the list, whereas va_start should be used to initialize a va_list from a parameter.

Multiple choice
  1. hypotenuse of a triangle with sides, a+2 and b+3.

  2. the square root of 3*a + 4*b + 5

  3. the square root of (a+2) 2 + (b+3) 2

  4. none of these

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

Macros perform simple text substitution. Replacing a and b with (a+2) and (b+3) in sqrt(a*a + b*b) yields sqrt((a+2)(a+2) + (b+3)(b+3)). The provided option B is a literal expansion of the macro without the intended mathematical grouping.

Multiple choice
  1. x = x-y+1

  2. x = -x-y-1

  3. x = -x + y +1

  4. x = x-y-1

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

The expression x = x-y+1; is a straightforward assignment statement in C language. It evaluates the right-hand side using current values and assigns it back to x, meaning the literal algebraic representation is identical to the option.

Multiple choice
  1. Two

  2. One

  3. Infinite

  4. None

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

In C++, destructors are special member functions used to clean up objects. They are defined without parameters and cannot be overloaded, meaning they always take zero arguments.

Multiple choice
  1. integer values

  2. address of another variable

  3. its own address

  4. none of the above

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

A pointer is a variable that holds the memory address of another variable, rather than holding a direct data value like an integer or float.

Multiple choice
  1. int sum(int x,int y,int z) {}

  2. float sum(int x,int y) {}

  3. int sum(int a,int b) {}

  4. all of the above

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

Function overloading occurs when multiple functions have the same name but different parameter lists (different number or types of arguments). Option A changes the number of arguments, which is a valid overload.

Multiple choice
  1. Const

  2. Main

  3. Sizeof

  4. Void

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

In C, 'main' is the name of the function where program execution begins; it is an identifier, not a reserved keyword.

Multiple choice
  1. Constant

  2. Variable

  3. Identifier

  4. Keyword

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

In C, an object is a region of data storage in the execution environment, the contents of which can represent values. Variables are the most common examples of objects.

Multiple choice
  1. Data typing

  2. Data hiding

  3. Data typing

  4. None of these

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

Data typing is essential because it specifies the range of values a variable can hold and the operations that can be performed on those values. The question contains a duplicate option (A and C), but the concept is correct.