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. float, int pointer

  2. float pointer, int pointer

  3. float, int

  4. float pointer, int

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

The macro floatptr expands to 'float *', so 'floatptr f1,f2' becomes 'float *f1,f2'. This declares f1 as a pointer to float, but f2 as a regular float (the * only binds to f1). Similarly, 'intptr p1,p2' becomes 'int *p1,p2', making p2 a regular int. This is a classic macro pitfall.

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. This is invalid syntax for freeing memory.

  4. The loop will never end.

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

The loop frees nptr BEFORE reading nptr->next in the next iteration. After free(nptr), accessing nptr->next is undefined behavior - the memory has been deallocated. The correct approach is to save nptr->next BEFORE freeing nptr: next = nptr->next; free(nptr); nptr = next;

Multiple choice
  1. 3 0

  2. 5 3

  3. 3 5

  4. 5 0

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

The macro big(a,b) returns the larger value, so big(3,5) returns 5. The condition (3+5)>5 evaluates to true (8>5). When swap(a,b) expands, it executes temp=a; a=b; b=temp; making a=5 and b=3. The printf outputs "5 3". The key point is that the swap macro lacks braces, but it still executes sequentially.

Multiple choice
  1. Breaks out of the if statement

  2. Exits the function

  3. Compiler error

  4. Nothing

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

The break statement can only be used inside loops (for, while, do-while) or switch statements. Using break inside an if statement that is not inside a loop or switch causes a compilation error. The compiler will reject this code because break has no valid target to break out of.

Multiple choice
  1. float, int pointer

  2. float pointer, int pointer

  3. float, int

  4. float pointer, int

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

The macro floatptr expands to float , so floatptr f1,f2 becomes float *f1,f2. Due to precedence, this declares f1 as float but f2 as float (only f1 gets the asterisk). For intptr (defined as int ), intptr p1,p2 becomes int *p1,p2, declaring p1 as int and p2 as int. Therefore f2 is float and p2 is int.

Multiple choice
  1. only from the innermost loop

  2. only from the innermost switch

  3. from all loops and switches

  4. from the innermost loop or switch

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

The break statement in C++ exits from the innermost loop or switch statement that contains it. It does not exit from all nested loops or switches simultaneously - only the immediately enclosing one. To exit from multiple levels, you would need multiple break statements or a different control structure like goto.

Multiple choice
  1. stdio.h

  2. math.h

  3. ctype.h

  4. string.h

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

The toascii() function is a character-handling function used to convert integer values into standard ASCII character range. It is declared in the standard library header file ctype.h in C and C++. Other headers like stdio.h or math.h handle input/output or mathematical operations respectively.

Multiple choice
  1. cout

  2. cin

  3. cerr

  4. get

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

cin is the standard input stream object in C++ that is used to input data from the keyboard. It is part of the iostream library and is used with the extraction operator (>>) to read values into variables. cout is for output, cerr is for error output, and get is a specific method.

Multiple choice
  1. sequential

  2. count

  3. repetitive

  4. incremental

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

A while loop in C++ is a control structure that repeatedly executes a block of code as long as a specified condition remains true. Because it involves repeating instructions, it is classified as a repetitive control structure.

Multiple choice
  1. Missing parentheses in return statement.

  2. The function should be defined as int f(int a, int b).

  3. Redeclaration of 'a'.

  4. None of the above

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

The function has a parameter named 'a', and then inside the function body, 'a' is redeclared as a local variable with 'int a;'. This creates a redeclaration error because you cannot declare a variable with the same name as a parameter in the same scope. Option C correctly identifies this as 'Redeclaration of a'. The parameter 'a' is already declared in the function signature, so declaring it again inside is a compilation error.