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. Nothing. The program will not compile because no exceptions are specified.

  2. Nothing. The program will not compile because no catch clauses are specified.

  3. Hello world.

  4. Hello world Finally executing

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

Finally clauses are always executed. The program will first execute the try block, printing Hello world, and will then execute the finally block, printing Finally executing. Option A, B, and C are incorrect based on the program logic described above. Remember that either a catch or a finally statement must follow a try. Since the finally is present, the catch is not required.

Multiple choice
  1. 1, 2 and 3

  2. 2, 4 and 5

  3. 3, 4 and 5

  4. 1, 4 and 5

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

(2) is correct because the reference variables f1 and f3 refer to the same array object. (4) is correct because it is legal to compare integer and floating-point types. (5) is correct because it is legal to compare a variable with an array element. (3) is incorrect because f2 is an array object and f1[1] is an array element.

Multiple choice
  1. true true

  2. false true

  3. true false

  4. false false

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

The boolean b1 in the fix() method is a different boolean than the b1 in the start()method. The b1 in the start() method is not updated by the fix() method.

Multiple choice
  1. slip stream

  2. slipstream stream

  3. stream slip stream

  4. slipstream slip stream

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

When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of "slip"). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of "slipstream"). When the program returns to start(), another String object is created, referred to by s2 and with a value of "stream".

Multiple choice
  1. Zero

  2. Twelve

  3. Default

  4. Compilation fails

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

The switch statement can only be supported by integers or variables more "narrow" than an integer i.e. byte, char, short. Here a Float wrapper object is used and so the compilation fails.

Multiple choice
  1. Compilation fails

  2. An exception occurs at runtime.

  3. It prints "Thread one. Thread two."

  4. The output cannot be determined.

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

When the start() method is attempted a second time on a single Thread object, the method will throw an IllegalThreadStateException (you will not need to know this exception name for the exam). Even if the thread has finished running, it is still illegal to call start() again.

Multiple choice
  1. If a is true and b is true then the output is "A && B"

  2. If a is true and b is false then the output is "notB"

  3. If a is false and b is true then the output is "ELSE"

  4. If a is false and b is false then the output is "ELSE"

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

Option C is correct. The output is "ELSE". Only when a is false do the output lines after 11 get some chance of executing. Option A is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. The condition at line 7 will never be evaluated (when a is true it will always be trapped by the line 12 condition) therefore the output will never be "A && B". Option B is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. Option D is wrong. The output is "notB".

Multiple choice
  1. Prints "Inside Thread Inside Thread"

  2. Prints "Inside Thread Inside Runnable"

  3. Does not compile

  4. Throws exception at runtime

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

If a Runnable object is passed to the Thread constructor, then the run method of theThread class will invoke the run method of the Runnable object. In this case, however, the run method in the Thread class is overridden by the run method in MyThread class. Therefore the run() method in MyRunnable is never invoked. Both times, the run() method in MyThread is invoked instead.

Multiple choice
  1. DeadLock

  2. It print 12 12 12 12

  3. Compilation Error

  4. Cannot determine output.

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

The program will execute without any problems and print 12 12 12 12.

Multiple choice
  1. finally

  2. exception finished

  3. finally exception finished

  4. Compilation fails

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

This is what happens: (1) The execution of the try block (line 5) completes abruptly because of the throwstatement (line 7). (2) The exception cannot be assigned to the parameter of any catch clause of the trystatement therefore the finally block is executed (line 9) and "finally" is output (line 11). (3) The finally block completes normally, and then the try statement completes abruptly because of the throw statement (line 7). (4) The exception is propagated up the call stack and is caught by the catch in the main method (line 20). This prints "exception". (5) Lastly program execution continues, because the exception has been caught, and "finished" is output (line 24).

Multiple choice
  1. bar

  2. bar done

  3. foo done

  4. Compilation fails

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

The foo() method returns void. It is a perfectly acceptable method, but because it returns void it cannot be used in an assert statement, so line 18 will not compile.

Multiple choice
  1. Line 5

  2. Line 6

  3. Line 12

  4. Line 14

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

Option D is correct. Compilation fails because of an unreachable statement at line 14. It is a compile-time error if a statement cannot be executed because it is unreachable. The question is now, why is line 20 unreachable? If it is because of the assert then surely line 6 would also be unreachable. The answer must be something other than assert. Examine the following: A while statement can complete normally if and only if at least one of the following is true:

  • The while statement is reachable and the condition expression is not a constant expression with value true. -There is a reachable break statement that exits the while statement. The while statement at line 11 is infinite and there is no break statement therefore line 14 is unreachable. You can test this with the following code: public class Test80 {     public void foo()     {         assert false;         assert false;     }     public void bar()     {         while(true)         {             assert false;             break;         }         assert false;      } }
Multiple choice
  1. Line 11

  2. Line 12

  3. Line 14

  4. Line 22

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

Assert statements should not cause side effects. Line 22 changes the value of z if the assert statement is false. Option A is fine; a second expression in an assert statement is not required. Option B is fine because it is perfectly acceptable to call a method with the second expression of an assert statement. Option C is fine because it is proper to call an assert statement conditionally.

Multiple choice
  1. It prints f2[0] = 0.0

  2. It prints f2[0] = NaN

  3. An error at f2 = f1; causes compile to fail.

  4. It prints the garbage value.

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

Option A is correct. When you create an array (f1 = new float[10];) the elements are initialises to the default values for the primitive data type (float in this case - 0.0), sof1 will contain 10 elements each with a value of 0.0. f2 has been declared but has not been initialised, it has the ability to reference or point to an array but as yet does not point to any array. f2 = f1; copies the reference (pointer/memory address) of f1 intof2 so now f2 points at the array pointed to by f1. This means that the values returned by f2 are the values returned by f1. Changes to f1are also changes to f2 because both f1 and f2 point to the same array.

Multiple choice
  1. z

  2. x z

  3. y z

  4. Compilation fails.

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

b1 is a Boolean object with value false. b2 gets b1's primitive value (false). The !b2 condition is true (NOT false = true), so b2 becomes true and "x" prints. Line 13: b1 & b2 uses Boolean objects. In JDK 1.6+, Boolean objects can use & operator with unboxing. b1 unboxes to false, b2 is true. false & true = false, so the if block doesn't execute. Finally "z" prints. Output: "x z".