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 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 contains Short objects. In line 3, s.add(i) adds short values 0-99 (autoboxed to Short). In line 4, s.remove(i-1) fails because i-1 is an int expression, and remove(int) treats it as an index (which doesn't apply to Set) or fails to find a matching object. The remove(Object) method would be needed, but the int type prevents that. So all 100 Short objects remain, and size() returns 100.

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

Bground overrides start() instead of run(). When b.run() is called directly in main(), it executes Thread's default run() method which does nothing. There's no compilation error (the class is valid) and no runtime error (empty run() is legal). The overridden start() method is never called because run() is invoked directly.

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() without calling super.start(). When main() calls hello.start(), the overridden method prints 'Hello thread' and returns - no actual thread is created. The run() method (which would create and start a new MyThread) is never invoked because the Thread lifecycle never begins. Output is just 'Hello thread' once.

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 a byte is passed to overloaded methods accepting int and Integer, Java uses primitive widening (byte -> int) rather than autoboxing (byte -> Byte -> Integer). This is because widening takes precedence over boxing in overload resolution. The method(int) overload is selected, so 'Primitive Type call' is printed.

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

After sorting the integer array, intArr becomes {11, 22, 66, 99}. Searching for 67, which is absent from the array, returns -(insertion point) - 1. The insertion point for 67 is index 3, resulting in -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 correct answer is C. Compilation fails.

The code will fail to compile at line 18 because the for loop cannot iterate over an iterator. The reverse() method returns an iterator, but the for loop expects an object.

To fix the code, you can either change the for loop to a while loop, or you can return a list from the reverse() method.

public static List reverse(List list) {
  Collections.reverse(list);
  return list;
}

With this change, the code will compile and run successfully, and the output will be 3, 2, 1.

Here is an explanation of each option:

  • Option A: This option is incorrect because the for loop cannot iterate over an iterator. The reverse() method returns an iterator, but the for loop expects an object.
  • Option B: This option is incorrect because the code will not run. The for loop will fail to compile at line 18.
  • Option C: This option is correct because the code will fail to compile at line 18.
  • Option D: This option is incorrect because the code will not run. The for loop will fail to compile at line 18.
  • Option E: This option is incorrect because no exception will be thrown at runtime. The code will simply fail to compile.
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.