Computer Knowledge

Programming Output Evaluation

1,721 Questions

Programming output evaluation tests the ability to trace code execution in languages like C, Java, and SAS. It focuses on arrays, loops, pointers, and data type conversions. These technical questions are standard in computer knowledge sections for IT officer and bank exams.

Java string bufferC language pointersLoop execution outputsData type conversionsMacro variable evaluation

Programming Output Evaluation Questions

Multiple choice
  1. Outputs 12

  2. Outputs 10

  3. Outputs the address of v

  4. No output

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

The function afunction receives a pointer to v but immediately reassigns x to point to newly allocated memory (x=new int). This does NOT change the original pointer in main - it only changes the local copy x. The *x=12 assigns to the new memory, not to v. When printf prints v in main, v still has its original value 10. The new int is leaked (memory not freed).

Multiple choice
  1. Compile-Time Error

  2. Default1Case1

  3. Default2

  4. Default1

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

A switch statement cannot have more than one default case. This code has two default labels (at the beginning and end), which violates C syntax rules. The compiler will report an error for duplicate default cases. The presence of the second default makes this code invalid.

Multiple choice
  1. 25

  2. 11

  3. Error

  4. Garbage Value

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

The macro SQR(x) expands to x*x. When called as SQR(b+2), it becomes b+2*b+2, NOT (b+2)*(b+2). Due to operator precedence, multiplication happens first: 3+2*3+2 = 3+6+2 = 11. The macro lacks parentheses around x and around the entire expression, causing this common pitfall.

Multiple choice
  1. A

  2. B

  3. All the above

  4. None of the above

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

The switch statement has choice=4. There is no case 4, so execution goes to default. The default prints "A" but has NO break statement. Without break, execution falls through to case 1, which prints "B". The output is "AB". Looking at the options, none of A, B, C ("All the above") match "AB". Therefore D (None of the above) is correct.

Multiple choice
  1. An error

  2. Unpredictable

  3. 567

  4. 0

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

The variable k is declared but never initialized. In C++, reading an uninitialized local variable results in undefined behavior - the output will be whatever garbage value happens to be in that memory location. This is unpredictable and can vary between runs. Option B (Unpredictable) correctly identifies this behavior. Note that the header has a typo: 'iosteam.h' should be 'iostream.h', but the conceptual question about uninitialized variables is still valid.

Multiple choice
  1. 6

  2. 5

  3. 4

  4. 0

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

The prefix increment operator ++a increments a BEFORE using its value. Since a is 5, ++a makes it 6 and then returns 6 to cout. The output is 6. Option A is correct. The difference between ++a (prefix) and a++ (postfix) is that prefix increments first, then returns the new value, while postfix returns the old value first, then increments.

Multiple choice
  1. The condition in the for loop is must.

  2. The two semicolons should be dropped.

  3. The for loop should be replaced by a while loop.

  4. No error

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

The for loop syntax 'for(;;)' is valid C++ for creating an infinite loop - all three parts (initialization, condition, increment) are optional. The loop continues until the break statement executes when a exceeds 10. The code will print 1 through 10. Option D correctly states there is 'No error'. The loop works as written, incrementing a and breaking when a > 10. Note: The header has 'iostream.h' which is pre-standard C++; modern C++ uses .

Multiple choice
  1. Use of printf in class A

  2. C++ use cout<< operator to display the text on screen.

  3. Function definition can always be done outside the class.

  4. Function calling can be done by using the name of the function.

  5. Both (1) and (2)

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

Options (1) and (2) correctly explain the reason.

Multiple choice
  1. -330,150

  2. 330, -150

  3. -150, -330

  4. 6, 30

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

Tracing the function: initially a=6, b=30. After a=a*b: a=180. After b=b-a: b=30-180=-150. After a=a-b: a=180-(-150)=330. Since parameters are passed by reference, main's variables are modified. The final output is '330, -150'. The function effectively swaps and scales values through arithmetic operations.

Multiple choice
  1. now = 5, next = 5

  2. now = 40, next = 5

  3. now = 10, next =10

  4. now = 40, next = 10

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

The inner value() function has static int j=40, so it prints 40. The outer value() function has static int j=5. When value() calls value(), the inner function's j (40) is printed first, then the outer's j (5). The global j=10 is shadowed by both function-local static variables.