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 technology programming languages
  1. A, B, C,

  2. B, C, A,

  3. Compilation fails.

  4. The code runs with no output.

  5. An exception is thrown at runtime.

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

To solve this question, the user must understand the syntax and behavior of the given Java code. In this code, a method named get() is defined that returns a Collection of type LinkedList containing three strings: "B", "C", and "A". The main method then iterates through each element in the Collection returned by get() and prints its value followed by a comma and a space.

Going through each option:

A. A, B, C,: This option is incorrect because the order of the strings in the Collection returned by get() is "B", "C", "A". Therefore, the output of the program will be "B, C, A,".

B. B, C, A,: This option is correct. As explained above, the Collection returned by get() contains the strings "B", "C", and "A", in that order. The main method iterates through each element in the Collection and prints its value, resulting in the output "B, C, A,".

C. Compilation fails.: This option is incorrect because there are no syntax errors or other issues in the code that would cause it to fail to compile.

D. The code runs with no output.: This option is incorrect because the main method prints output to the console, as described above.

E. An exception is thrown at runtime.: This option is incorrect because there are no errors or exceptional conditions in the code that would cause it to throw an exception.

Therefore, the correct answer is:

The Answer is: B. B, C, A,

Multiple choice technology programming languages
  1. Wrapper Type Call

  2. Primitive Type Call

  3. Compiler Error

  4. Compiles fine, throws runtime exception

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

The variable b is of primitive type byte. Widening is preferred over autoboxing, so JVM chooses the primitive type method over the wrapper type method by widening byte to int. Thus, method(int) is executed, printing "Primitive Type call". Autoboxing to Integer is not selected because widening takes precedence.

Multiple choice technology programming languages
  1. -3

  2. 3

  3. -4

  4. 4

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

The array intArr initially contains {22, 11, 66, 99}. After calling Arrays.sort(intArr), it becomes sorted: {11, 22, 66, 99}. Searching for 67 using Arrays.binarySearch yields an insertion point of index 3 (between 66 and 99). The return value is key insertion point index negated minus 1, which calculates to -(3) - 1 = -4.

Multiple choice technology programming languages
  1. A compile time error indicating that no run method is defined for the Thread class.

  2. A run time error indicating that no run method is defined for the Thread class.

  3. Clean compile and at run time the values 0 to 9 are printed out.

  4. Clean compile but no output at runtime.

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

The code compiles cleanly but produces no output because calling run() directly does not start a new thread - it just executes the method in the current thread. The Thread class already has a run() method (from Thread), so there's no error. To start a thread, you must call start() which then calls run(), but this class overrides start() to do something else.

Multiple choice technology programming languages
  1. The program will output 'Hello Thread' and 'My Thread'.

  2. The program will output 'Hello Thread'.

  3. The program will output 'My Thread' and 'Hello Thread'.

  4. The order of the outputs is not will vary upon execution.

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

HelloThread overrides start() to print 'Hello thread' without calling super.start(), so the run() method never executes as part of thread startup. The code in run() creates a new Thread but that start() call would work normally. However, since run() is never called by the overridden start(), only 'Hello thread' prints.

Multiple choice technology programming languages
  1. NumberFormat Exception

  2. 100

  3. Compilation fails at line 4

  4. 99

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

The HashSet stores Short objects from 0 to 99. In each iteration, we add the current Short value, then attempt to remove i-1. However, i-1 is an int (arithmetic promotion), so remove() looks for an Integer object, not a Short. Since the HashSet contains only Shorts, the remove() calls never find matches. Therefore all 100 elements remain in the set.

Multiple choice technology programming languages
  1. 3,2, 1,

  2. 1, 2, 3,

  3. Compilation fails.

  4. The code runs with no output.

  5. An exception is thrown at runtime.

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

The reverse() method returns a raw Iterator (not Iterator). When used in the for-each loop, Java cannot determine the type parameter for the iterator. For-each loops require an array or a generic Iterable. Raw Iterator doesn't implement Iterable properly, so this fails to compile. The fix is to return Iterator.

Multiple choice technology programming languages
  1. -3

  2. 3

  3. -4

  4. 4

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

The array intArr contains {22, 11, 66, 99}. After sorting, the array becomes {11, 22, 66, 99}. When searching for 67, which is not in the array, Arrays.binarySearch returns -(insertion point) - 1. The value 67 would be inserted at index 3 (after 66), so the result is -3 - 1 = -4.

Multiple choice technology programming languages
  1. 3,2, 1,

  2. 1, 2, 3,

  3. Compilation fails.

  4. The code runs with no output.

  5. An exception is thrown at runtime.

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

The reverse() method returns a raw Iterator (not Iterator). When used in the for-each loop, Java cannot determine the type parameter for the iterator. For-each loops require an array or a generic Iterable. Raw Iterator doesn't implement Iterable properly, so this fails to compile. The fix is to return Iterator.

Multiple choice technology programming languages
  1. A, B, C,

  2. B, C, A,

  3. Compilation fails.

  4. The code runs with no output.

  5. An exception is thrown at runtime.

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

The method returns a LinkedList, which maintains insertion order. Elements are added in the order B, C, A, so they iterate in that same order. The method name 'get' and variable name 'sorted' are misleading - there's no actual sorting happening. LinkedList preserves insertion order, not sorted order.

Multiple choice technology programming languages
  1. NumberFormat Exception

  2. 100

  3. Compilation fails at line 4

  4. 99

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

The HashSet stores Short objects from 0 to 99. In each iteration, we add the current Short value, then attempt to remove i-1. However, i-1 is an int (arithmetic promotion), so remove() looks for an Integer object, not a Short. Since the HashSet contains only Shorts, the remove() calls never find matches. Therefore all 100 elements remain in the set.

Multiple choice technology programming languages
  1. A compile time error indicating that no run method is defined for the Thread class.

  2. A run time error indicating that no run method is defined for the Thread class.

  3. Clean compile and at run time the values 0 to 9 are printed out.

  4. Clean compile but no output at runtime.

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

The Bground class extends Thread but overrides start instead of run. Since the main method directly invokes run on the object, the default Thread run method executes, which does nothing since there is no target Runnable, producing no output.

Multiple choice technology programming languages
  1. The program will output 'Hello Thread' and 'My Thread'.

  2. The program will output 'Hello Thread'.

  3. The program will output 'My Thread' and 'Hello Thread'.

  4. The order of the outputs is not will vary upon execution.

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

HelloThread overrides the start() method without calling super.start(). The overridden start() method only prints 'Hello thread' and returns, never spawning a new thread or calling run(). Therefore, the MyThread run() method is never executed.

Multiple choice technology programming languages
  1. Wrapper Type Call

  2. Primitive Type Call

  3. Compiler Error

  4. Compiles fine, throws runtime exception

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

When passing a byte to an overloaded method, Java chooses between widening conversion (byte to int, calling the primitive method) and autoboxing (byte to Byte, which doesn't match Integer). Widening wins over autoboxing, so method(int i) is called, printing 'Primitive Type call'.

Multiple choice technology programming languages
  1. -3

  2. 3

  3. -4

  4. 4

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

Sorting the array results in eleven, twenty-two, sixty-six, ninety-nine. Searching for sixty-seven yields a insertion point of index three. Using the binary search return formula, negative insertion point minus one, returns negative four. Distractors are incorrect calculations.