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. //1 will create an compilation error

  2. //2 will create an compilation error

  3. //1 and //2 both will create an compilation error

  4. //1 will create an runtime error

  5. //2 will create an runtime error

  6. //1 and //2 both will create an runtime error

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

Both statements are invalid. 'i++=i++' and '++i=++i' both attempt to assign to the result of an increment operation. In Java, increment expressions (i++ or ++i) return values but cannot be assignment targets (not l-values). Both lines cause compilation errors. The error is not at runtime but during compilation.

Multiple choice technology programming languages
  1. for(int y : x) {

  2. for(x : Int y) {

  3. int y = 0; for(y : x) {

  4. for(int y=0, z=0; z<x.length; z++) { y = x[z];

  5. for(int y=0, int z=0, int z=0; z<x.length; z++) { y = x[z];

  6. int y = 0; for(int z=0; z<x.length; z++) { y = x[z];

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

Option A is the enhanced for loop syntax - correct. Option F declares y before the loop and uses traditional for loop to iterate - correct. Options B, C, D, E have syntax errors: wrong order in B, wrong syntax in C, incorrect declaration in E (int z declared twice in for-init). Only A and F compile independently.

Multiple choice technology programming languages
  1. abc

  2. abcde

  3. Compilation fails due only to an error on line 7.

  4. Compilation fails due to errors on multiple lines.

  5. Compilation fails due only to an error on line 10.

  6. Compilation fails due only to an error on line 11.

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

Line 11 fails compilation. In switch statements, case labels must be compile-time constant expressions. x4 is a final Integer (wrapper), not a compile-time constant. Only constant primitive types or String are allowed. x2 works (int primitive), x4 fails (Integer object). The error is specifically on line 11.

Multiple choice technology programming languages
  1. -ic of

  2. -mf of

  3. Compilation failure

  4. -ic mf of

  5. -ic mc mf of

  6. -ic mc of mf

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

The nested try-catch-finally blocks execute in sequence: Inner try throws, caught by 'ex' -> 'ic' appended. Then throws new Exception, caught by outer catch 'x' -> 'mc' appended. Outer finally executes -> 'mf' appended. Outermost finally executes -> 'of' appended. The sequence is ic (inner catch), mc (middle catch), mf (middle finally), of (outer finally) = '-ic mc mf of'.

Multiple choice technology programming languages
  1. null

  2. null null

  3. A ClassCastException is thrown.

  4. A NullPointerException is thrown.

  5. A NoClassDefFoundError is thrown.

  6. An ArrayIndexOutOfBoundsException is thrown.

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

The command 'java Miner.java' makes args[0] = 'Miner.java' (not empty). The array has Mineral and Gem objects. Both are cast to Mineral successfully (Gem extends Mineral). getWeight prints 'null ' (static String s is null) twice. No division by zero occurs (x=7, not 0). The output is 'null null'. args[0] exists, so no ArrayIndexOutOfBoundsException.

Multiple choice technology programming languages
  1. Compilation error.

  2. Runtime error.

  3. Hello Hi Hi_Hello Hi

  4. Hi_Hello

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

The code has two overloaded main methods. When main(String[]) runs, it prints "Hello" then calls main(int[]) which prints "Hi" and returns. In the main class, ob.main(b) prints "Hello" then "Hi", then "Hi_Hello" prints, then main(a) prints "Hi" again. Total output: Hello Hi Hi_Hello Hi. Options A and B are wrong because the code compiles and runs without errors. Option D only captures part of the output.

Multiple choice technology programming languages
  1. Hello....

  2. Hi....

  3. Compilation error

  4. Runtime error

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

Java uses string literal pooling. When s1 and s2 are both assigned "Hello", they reference the same string literal object in the pool. The == operator compares references, not content. Since both variables point to the same pooled string, s1 == s2 returns true, printing "Hello....". Option B is wrong because the references are equal. Options C and D are incorrect as the code compiles and runs normally.

Multiple choice technology programming languages
  1. OF(11) is: 45

  2. OF(11) is: 56

  3. OF(11) is: 89

  4. OF(11) is: 111

  5. Compilation fails.

  6. Runtime Exception

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

The code contains a syntax error: 'else (x <= 4)' is invalid Java syntax. An else clause cannot have a condition - it should be 'else if (x <= 4)'. This compilation error prevents the code from running, so none of the numeric outputs can be produced. Options A-D are all numeric results that would require successful compilation and execution.

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.