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. 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. Synchronize the run method.

  2. Wrap a synchronize(this) around the call to f.increase().

  3. The existing code will cause a runtime exception.

  4. Synchronize the increase() method

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

Option D is correct because synchronizing the code that actually does the increase will protect the code from being accessed by more than one thread at a time. Option A is incorrect because synchronizing the run() method would stop other threads from running the run() method (a bad idea) but still would not prevent other threads with other runnables from accessing the increase() method. Option B is incorrect for virtually the same reason as A—synchronizing the code that calls the increase() method does not prevent other code from calling the increase()method.

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. Compilation fails.

  2. odd will always be the output.

  3. even will always be the output.

  4. odd will be the output for odd values of x, and even for even values.

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

The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.

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. Prints: false,false,false

  2. Prints: false,false,true

  3. Prints: false,true,false

  4. Prints: true,false,false

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

The Vector.elements method returns an Enumeration over the elements of the vector. Vector implements the List interface and extends AbstractList so it is also possible to get an Iterator over a Vector by invoking the iterator or listIteratormethod.

Multiple choice
  1. one two three four

  2. four three two one

  3. four one three two

  4. one two three four one

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

TreeSet assures no duplicate entries; also, when it is accessed it will return elements in natural order, which typically means alphabetical.

Multiple choice
  1. This program will compile successfully.

  2. This program fails to compile due to an error at line 4.

  3. This program fails to compile due to an error at line 6.

  4. This program fails to compile due to an error at line 13.

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

Any method (in this case, the main() method) that throws a checked exception (in this case, out.close() ) must be called within a try clause, or the method must declare that it throws the exception. Either main() must declare that it throws an exception, or the call to out.close() in the finally block must fall inside a (in this case nested) try-catch block.