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. AAACCC

  2. AAADDD

  3. BBBCCC

  4. BBBDDD

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

In Java, String.equals(Object) works correctly because String's equals method overrides Object.equals to handle String comparison. Both s.equals(o) and o.equals(s) return true because they reference the same String object 'foo'. The equals method checks for actual content equality, not reference equality.

Multiple choice
  1. ABBCAD

  2. ABCBCAD

  3. CDADACB

  4. Output determined by the underlying platform.

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

Can you guarantee the order in which threads are going to run? No you can't. So how do you know what the output will be? The output cannot be determined.

Multiple choice
  1. j = 0

  2. j = 4

  3. j = 8

  4. The code will run with no output

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

The lines to watch here are lines 9 & 10. Line 9 features the non-shortcut version of theOR operator so both of its operands will be evaluated and therefore methodB(4) is executed. However line 10 has the shortcut version of the OR operator and if the 1st of its operands evaluates to true (which in this case is true), then the 2nd operand isn't evaluated, so methodB(8) never gets called. The loop is only executed once, b is initialized to false and is assigned true on line 9. Thus j = 4.

Multiple choice
  1. 0 1 2

  2. 0 1 2 1 2 2

  3. Compilation fails at line 11.

  4. Compilation fails at line 12.

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

Case expressions must be constant expressions. Since x is marked final, lines 12 and 13 are legal; however y is not a final so the compiler will fail at line 11.

Multiple choice
  1. finished

  2. Compilation fails.

  3. An AssertionError is thrown.

  4. An AssertionError is thrown and finished is output.

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

An assertion Error is thrown as normal giving the output "assertion failed". The word "finished" is not printed (ensure you run with the -ea option) Assertion failures are generally labeled in the stack trace with the file and line number from which they were thrown, and also in this case with the error's detail message "assertion failed". The detail message is supplied by the assert statement in line 6.

Multiple choice
  1. 0 def 1

  2. 2 1 0 def 1

  3. 2 1 0 def def

  4. 2 1 0 def 1 def 1

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

When z == 0 , case x-2 is matched. When z == 1, case x-1 is matched and then the break occurs. When z == 2, case x, then default, then x-1 are all matched. When z == 3, default, then x-1 are matched. The rules for default are that it will fall through from above like any other case (for instance when z == 2), and that it will match when no other cases match (for instance when z==3).

Multiple choice
  1. 0 1 2

  2. 0 1 2 1 2 2

  3. 2 1 0 1 0 0

  4. 2 1 2 0 1 2

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

The case expressions are all legal because x is marked final, which means the expressions can be evaluated at compile time. In the first iteration of the for loop casex-2 matches, so 2 is printed. In the second iteration, x-1 is matched so 1 and 2 are printed (remember, once a match is found all remaining statements are executed until a break statement is encountered). In the third iteration, x is matched. So 0 1 and 2 are printed.

Multiple choice
  1. main() will finish before starting threads.

  2. main() will finish in the middle of one thread.

  3. main() will finish after one thread.

  4. Cannot be determined.

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

Can you guarantee the order in which threads are going to run? No you can't. So how do you know what the output will be? The output cannot be determined. add this code after line 28: try { Thread.sleep(5000); } catch(InterruptedException e) { } and you have some chance of predicting the outcome.

Multiple choice
  1. i = 6 and j = 5

  2. i = 5 and j = 5

  3. i = 6 and j = 4

  4. i = 5 and j = 6

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

This loop is a do-while loop, which always executes the code block within the block at least once, due to the testing condition being at the end of the loop, rather than at the beginning. This particular loop is exited prematurely if i becomes greater than j. The order is, test i against j, if bigger, it breaks from the loop, decrements j by one, and then tests the loop condition, where a pre-incremented by one i is tested for being lower than 5. The test is at the end of the loop, so i can reach the value of 5 before it fails. So it goes, start: 1, 10 2, 9 3, 8 4, 7 5, 6 loop condition fails.

Multiple choice
  1. 7 7

  2. 7 14

  3. 14 0

  4. 14 14

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

The int x in the twice() method is not the same int x as in the start() method.Start()'s x is not affected by the twice() method. The instance variable s is updated by twice()'s x, which is 14.

Multiple choice
  1. 0

  2. 1

  3. 10

  4. 10010

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

Line 10 fails because b1 and b2 are two different objects. Lines 12 and 18 succeed because the Boolean String constructors are case insensitive. Lines 14 and 16 fail because true is not equal to false.

Multiple choice
  1. I is 1

  2. I is 1 I is 1

  3. No output is produced

  4. Compilation error

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

There are two different looping constructs in this problem. The first is a do-while loop and the second is a while loop, nested inside the do-while. The body of the do-while is only a single statement, brackets are not needed. You are assured that the while expression will be evaluated at least once, followed by an evaluation of the do-while expression. Both expressions are false and no output is produced.