Computer Knowledge

Programming Output Evaluation

1,953 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. Hello

  2. lo

  3. llo

  4. Error

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

The function fun() increments the pointer by 3 (ptr+=3), advancing it from 'Hello' to point at 'lo'. This pointer is returned to main() where it's printed. String 'Hello' has indices 0-4 for H,e,l,l,o, so +3 points to index 3 which is 'l', making the string 'lo'.

Multiple choice
  1. 22 10 11 13

  2. 22 11 11 11

  3. 22 10 11 13

  4. 22 13 13 13

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

In printf, arguments are evaluated right-to-left. Starting with a=10: a++ makes a=11 (but evaluates to 10 in first operation), then ++a makes a=12 (evaluates to 12). So b=10+12=22. For printf: ++a makes a=13, then a is 13, then a++ evaluates to 13 (a becomes 14), and b is 22. Output: 22 13 13 13.

Multiple choice
  1. System Hangs

  2. 100 100

  3. 0 100

  4. 1 100

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

The outer for loop has no condition (empty middle clause), so it's infinite: for(i=0;;i++). The inner loop is also infinite: for(j=0;;j++). The break only exits the inner loop when j>100, but then the inner loop restarts with j=0. This continues forever, causing the system to hang.

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.