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. 0 Garbage Value 2

  2. 0 1 2

  3. 1 2 3

  4. None of the above

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

The expression i++ uses post-increment, meaning the current value of i is assigned to a[i], then i is incremented. First iteration: a[0]=0, i becomes 1. Second: a[1]=1, i becomes 2. Third: a[2]=2, i becomes 3. But wait - after i=2, we assign a[2]=2, then i becomes 3, so loop exits. However, when i=0, a[0]=0, then i becomes 1. When i=1, a[1]=1, then i becomes 2. When i=2, a[2]=2, then i becomes 3. Actually, the issue is that the loop condition is checked BEFORE the increment in the for statement. Let me reconsider: for(i=0; i<3; i++) - the increment happens AFTER each iteration. So: i=0, assign a[0]=0, then i++ makes i=1; i=1, assign a[1]=1, then i++ makes i=2; i=2, assign a[2]=2, then i++ makes i=3. But there's undefined behavior here because i is modified twice in the same expression (a[i]=i++). In practice, the assignment happens before the increment, so a[0]=0, a[1]=1, a[2]=2. However, option A says '0 Garbage Value 2' which suggests something else is happening. The key is that after i becomes 3, the loop exits, but a[3] is never set. However, the array only has 3 elements (indices 0,1,2). The answer '0 Garbage Value 2' suggests that a[1] contains garbage. This is due to the undefined behavior of i++ where the assignment and increment order is not guaranteed. The most likely outcome is indeed that a[1] ends up with garbage because of the sequence point violation.

Multiple choice
  1. 123 456

  2. 123 123

  3. 456 456

  4. 456 123

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

This XOR swap algorithm exchanges values without a temporary variable. a ^= b means a = a XOR b. The chained XOR operations work because XOR is reversible. Let's trace: Initially a=123, b=456. After all operations, a becomes 456 and b becomes 123. The XOR-swap technique correctly swaps the values.

Multiple choice
  1. Error

  2. Garbage Value 1

  3. 0 1

  4. 1 2

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

The line int *array = &realarray[-1] uses negative array indexing, which is undefined behavior. However, it typically works by pointing before the array start. When the loop runs, *array accesses realarray[-1](garbage/previous memory), then array++ makes it point to realarray[0]=1. So output is garbage value followed by 1.

Multiple choice
  1. 20 20

  2. Garbage Value 20

  3. 0 20

  4. Linker Error

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

The variable i is declared as extern but never defined anywhere in the program. This causes a linker error because the linker cannot find the definition of i to satisfy the external declaration. The inner block's int i=20 is a separate local variable and doesn't resolve the extern.

Multiple choice
  1. Program Hangs

  2. No Output

  3. 6 4 2

  4. 5 2

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

The for loop initializes i=5, then checks ++i which makes i=6 (true, so loop runs), prints 6. Then i-=3 makes i=3. Check ++i: i=4 (true), prints 4. Then i-=3 makes i=1. Check ++i: i=2 (true), prints 2. Then i-=3 makes i=-1. Check ++i: i=0 (false), loop exits. Output: 6 4 2.

Multiple choice
  1. 1234 1234

  2. 1235 1235

  3. 1234 1235

  4. 1235 1234

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

The printf statement has *ptr++ as the second argument. The post-increment operator means the current value (*ptr=1234) is used for printf, then ptr is incremented. So printf receives val=1234 and *ptr=1234, printing "1234 1234". The pointer increment happens after the value is fetched.

Multiple choice
  1. Compile-Time Error

  2. Testing...Done

  3. Testing...Done

  4. None of these

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

The line func; is a statement that evaluates the function name but doesn't call it - it's essentially a no-op that gets discarded. Then func(); is called normally and prints "Testing...Done". The first func; is valid but useless, not an error.

Multiple choice
  1. Compile-Time Error

  2. 0.0 0.0 0.0 0.0 0.0

  3. 0.0 0.1 0.2 0.3 0.4

  4. 1.0 1.0 1.0 1.0 1.0

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

The code contains a comma operator quirk in the assignment expression, but more importantly, integer division i/10.0 promotes the result to float. For i from 0 to 4, i/10.0 evaluates to 0.0, 0.1, 0.2, 0.3, and 0.4 respectively. Printing these floats with format %.1f produces the expected sequence.

Multiple choice
  1. 100

  2. Linker Error

  3. Run Time Error

  4. Compiler Error

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

The underscore _ is a valid variable name in C. The code declares int _, assigns 100 to it, then prints it. This is perfectly valid C code and outputs 100. Underscore is a legal identifier character.