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

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 is declared with a raw type 'List' instead of a generic 'List'. The enhanced for-loop expects an Iterable from the raw type, but when used with generics, this creates a type mismatch. The method signature needs generics or explicit casting to compile cleanly.

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

LinkedList maintains insertion order. Elements are added as B, then C, then A. The iterator returns them in this order: B, C, A. The for-each loop prints each with a comma, producing 'B, C, A, '.

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 loop variable is of type short, but the expression i minus one is promoted to an int. Consequently, remove is invoked with an Integer object, which never matches any Short objects in the set, leaving the set size at one hundred.

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

To understand what will happen when you attempt to compile and run the given code, the user needs to know the concept of multithreading in Java.

The code defines a class Bground that extends the Thread class and overrides its start() method. In the main method, an instance of Bground is created and its run() method is called.

Now, let's go through each option and explain why it is right or wrong:

A. A compile time error indicating that no run method is defined for the Thread class. This option is incorrect because the Thread class already has a run() method defined, and the Bground class inherits this method from Thread. Therefore, there will be no compile time error.

B. A run time error indicating that no run method is defined for the Thread class. This option is incorrect because the Bground class has a run() method defined that overrides the run() method of the Thread class. Therefore, there will be no run time error.

C. Clean compile and at run time the values 0 to 9 are printed out. This option is incorrect because the run() method of the Bground class is not called with the start() method. Instead, the run() method is called directly in the main method. Therefore, only the start() method of the Bground class is executed, which does not print any output.

D. Clean compile but no output at runtime. This option is correct. The code will compile without any errors and there will be no output at runtime because only the start() method of the Bground class is executed, which does not print any output.

Therefore, the correct answer is: D.

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

The HelloThread class overrides start() without calling super.start(), so no new thread is created. It only prints 'Hello thread'. The run() method never executes because start() was overridden instead of the thread actually starting.

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 chooses primitive widening (byte to int) over autoboxing. The method(int i) version is called.

Multiple choice technology programming languages
  1. 0

  2. 5

  3. 20

  4. undefined

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

The global constant x=5 is declared at file scope. Inside main(), 'int x[x]' creates an array of size 5 (using the global x, not a VLA since x is a constant). The local x (the array name) shadows the global x within main. 'sizeof(x)' returns the size of the local array (5 * sizeof(int) = 20 on most systems). Dividing by sizeof(int) gives 5. The local array declaration doesn't affect the global constant.

Multiple choice technology programming languages
  1. True

  2. False

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

This code is valid C++ (not C). 'int x = 5' declares a global variable x. 'class x' declares a class named x. In main(), 'class x y' declares a variable y of type class x. In C++, variable names and class/struct names occupy different namespaces, so 'int x' and 'class x' don't conflict. The code compiles successfully, making the answer True.