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

  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.

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.