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

  2. Compiliation fails.

  3. An AssertionError is thrown and finished is output.

  4. An AssertionError is thrown with the message "assertion failed."

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

Compilation Fails. You can't use the Assert statement in a similar way to the ternary operator. Don't confuse.

Multiple choice
  1. hello throwit caught

  2. Compilation fails

  3. hello throwit RuntimeException caught after

  4. hello throwit caught finally after

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

The main() method properly catches and handles the RuntimeException in the catch block, finally runs (as it always does), and then the code returns to normal. A, B and C are incorrect based on the program logic described above. Remember that properly handled exceptions do not cause the program to stop executing.

Multiple choice
  1. finished

  2. Exception

  3. Compilation fails

  4. Arithmetic Exception

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

Compilation fails because ArithmeticException has already been caught.ArithmeticException is a subclass of java.lang.Exception, by time theArithmeticException has been specified it has already been caught by theException class. If ArithmeticException appears before Exception, then the file will compile. When catching exceptions the more specific exceptions must be listed before the more general (the subclasses must be caught before the superclasses).

Multiple choice
  1. Compilation fails.

  2. An error occurs at runtime.

  3. It prints "foobarhi"

  4. It prints "barhi"

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

Option C is correct because first the Foo instance is created, which means the Fooconstructor runs and prints "foo". Next, the makeBar() method is invoked which creates a Bar, which means the Bar constructor runs and prints "bar", and finally the go()method is invoked on the new Bar instance, which means the go() method prints "hi".

Multiple choice
  1. i = 3

  2. Compilation fails.

  3. i = 5

  4. A ClassCastException will occur.

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

The code compiles and runs successfully. A local class Foo is defined inside main(), instantiated, cast to Object (upcasting, always allowed), then cast back to Foo (downcasting, valid here since the actual object is a Foo). The instance variable i is initialized to 3, so the output is "i = 3". Local classes can be used within their scope.

Multiple choice
  1. Class A

  2. Compilation fails.

  3. An exception is thrown at line 3.

  4. The code executes with no output.

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

Option D is correct. The specification at line 3 is for a method and not a constructor and this method is never called therefore there is no output. The constructor that is called is the default constructor.

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. 5 3

  2. 8 2

  3. 8 3

  4. 8 5

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

The first two iterations of the for loop both x and y are incremented. On the third iteration x is incremented, and for the first time becomes greater than 2. The short circuit or operator || keeps y from ever being incremented again and x is incremented twice on each of the last three iterations.

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.