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

  2. bad number

  3. compilation fails on line 13

  4. compilation fails on line 14

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

The xxxValue() methods convert any numeric wrapper object's value to any primitive type. When narrowing is necessary, significant bits are dropped and the results are difficult to calculate.

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.

Multiple choice
  1. Ex0 caught

  2. exception caught

  3. Compilation fails because of an error it line 2

  4. Compilation fails because of an error in line 9

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

An exception Exc1 is thrown and is caught by the catch statement in line 11. 

Multiple choice
  1. 1

  2. 2

  3. 3

  4. Compilation Fails

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

Initially this looks like a question about the logical and logical shortcut operators "|" and "||" but on closer inspection it should be noticed that the name of the boolean method in this code is "catch". "catch" is a reserved keyword in the Java language and cannot be used as a method name. Hence Compilation will fail.

Multiple choice
  1. The program will fail to compile.

  2. The program will compile without any errors and will print 'Hello' and 'World' everytime the program is run.

  3. The program will compile without errors and will print 'Hello' and 'World' but the order is unpredictable.

  4. The program will compile without any errors and will terminate without any output when run.

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

The program will compile without errors and will simply terminate without any output. Two threads are created but they will never be started. The start() method must be called on the thread objects to make the threads execute the run() method asynchronously.

Multiple choice
  1. AB

  2. BC

  3. ABC

  4. BCD

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

(1) A RuntimeException is thrown, this is a subclass of exception. (2) The exception causes the try to complete abruptly (line 5) therefore line 6 is never executed. (3) The exception is caught (line 7) and "B" is output (line 9) (4) The finally block (line 10) is always executed and "C" is output (line 12). (5) The exception was caught, so the program continues with line 15 and outputs "D".

Multiple choice
  1. ABCD.

  2. Compilation fails.

  3. C is printed before exiting with an error message.

  4. BC is printed before exiting with an error message.

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

Error is thrown but not recognised in line(18) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore, only the code in the finally statement can be run before exiting with a runtime error (Exception in thread main java.lang.Error).

Multiple choice
  1. catch (ArrayIndexOutOfBoundsException aie) {
     aie.printStackTrace();
     System.out.println("array size is less than what you are trying to access")
    }
    
  2. catch (ArrayIndexOutOfBoundsException aie) {
     System.out.println("array size is less than what you are trying to access")
    }
    
  3. catch (aie) {
     aie.printStackTrace();
     System.out.println("array size is less than what you are trying to access")
     }
    
  4. throw (ArrayIndexOutOfBoundsException aie) {
     aie.printStackTrace();
     System.out.println(array size is less than what you are trying to access ')
     }
    
  5. throw (aie) {
     aie.printStackTrace();
     System.out.println(array size is less than what you are trying to access)
    }
    
Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Catch routines are defined in this form. 

Multiple choice
  1. This code will not compile due to line 5.

  2. This code will not compile due to line 6.

  3. 1..2..

  4. 1..2..3..

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

Line 6 calls the run() method, so the run() method executes as a normal method should and it prints 1..2.. 1 is incorrect because line 5 is the proper way to create an object. 2 is incorrect because it is legal to call the run() method, even though this will not start a true thread of execution. The code after line 6 will not execute until the run() method is complete. 4 is incorrect because the for loop only does two iterations.

Multiple choice
  1. The code fails to compile in the main() method

  2. The code fails to compile in the run() method

  3. Only the text "In run" will be displayed

  4. The text "In run" followed by "Leaving run" will be displayed

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

The Thread.yield() method suggests to the thread scheduler that the current thread is willing to yield its current use of processor, but the scheduler is free to ignore this suggestion. In this simple single-threaded program, yield() has no visible effect, and both statements execute normally, printing 'In run' followed by 'Leaving run'.

Multiple choice
  1. no code is necessary

  2. throws Exception

  3. catch ( Exception e )

  4. throws RuntimeException

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

Option 2 is correct. This works because it DOES throw an exception if an error occurs. Option 1 is wrong. If you compile the code as given the compiler will complain: unreported exception must be caught or declared to be thrown The class extendsException so we are forced to test for exceptions. Option 3 is wrong. The catch statement belongs in a method body not a method specification. Option 4 is wrong. TestException is a subclass of Exception therefore the test method, in this example, must throw TestException or some other class further up theException tree. Throwing RuntimeException is just not on as this belongs in thejava.lang.RuntimeException branch (it is not a superclass of TestException). The compiler complains with the same error as in A above.

Multiple choice
  1. BD

  2. BCD

  3. BDE

  4. BCDE

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

A Runtime exception is thrown and caught in the catch statement in line 10. The code after the finally statement is run because the exception has been caught.

Multiple choice
  1. ACCBAD

  2. ABBCAD

  3. CDDACB

  4. Indeterminate output

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

It gives different output while executing the same compiled code at different times. C:>javac Test.java C:>java Test ABBCAD C:>java Test ACADCB C:>java Test ACBCBAD C:>java Test ABBCAD C:>java Test ACBCBAD C:>java Test ACBCBAD C:>java Test ABBCAD

Multiple choice
  1. Compilation error.

  2. Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously.

  3. Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code.

  4. Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...

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

Both threads are operating on the same instance variables. Because the code is synchronized, the first thread will complete before the second thread begins. Modify line 17 in the following way to print the thread name. System.out.println(Thread.currentThread().getName() + x = + x + , y = + y);

Multiple choice
  1. It will print the numbers 0 to 19 sequentially.

  2. It will print the numbers 1 to 20 sequentially.

  3. It will print the numbers 1 to 20, but the order cannot be determined.

  4. The code will not compile.

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

You have two different threads that share one reference to a common object. The updating and output takes place inside the synchronized code. One thread will run to complete printing the numbers 1-10. The second thread will then run to complete printing the numbers 11-20.