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

  2. 3

  3. 1 2

  4. 2 3

  5. 1 2 3

  6. Compilation fails.

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

The first condition (x==4) && !b2 is false because x=5, so '1' is not printed. Print('2') always executes, printing '2'. The second condition (b2 = true) assigns true to b2, then evaluates (true && b1) which is true, so '3' is printed. Output: '2 3'.

Multiple choice technology programming languages
  1. 5

  2. 10

  3. 12

  4. 17

  5. 24

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

The output from line 5 of the Test class would be 10.

Explanation:

In the Test class, there is an instance variable x with a value of 12.

The method takes a parameter x and performs an addition operation x += x, which is equivalent to x = x + x.

When the method is called with an argument of 5 (t.method(5)), the local variable x inside the method is set to 5.

The addition operation x += x is then performed, resulting in x being updated to 10.

Finally, System.out.println(x) prints the value of x, which is 10.

Therefore, the output from line 5 of the Test class would be 10, so the correct answer is C.

Multiple choice technology programming languages
  1. harrier

  2. shepherd

  3. retriever

  4. Compilation fails.

  5. retriever harrier

  6. An exception is thrown at runtime.

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

The switch statement contains a critical syntax error on line 18: 'case default:' is invalid. In Java switch statements, 'default' is a keyword that stands alone and cannot be combined with 'case'. The correct syntax is simply 'default:' without the word 'case' preceding it. This causes compilation to fail regardless of the enum value being tested.

Multiple choice technology programming languages
  1. null

  2. zero

  3. some

  4. Compilation fails.

  5. An exception is thrown at runtime.

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

Line 15 has a syntax error: 'else (str.length() == 0)' is missing the keyword 'if'. In Java, 'else' must be followed immediately by a statement or block, or by 'if' for an else-if clause. The parentheses alone make no sense syntactically. This causes compilation failure regardless of the string value being tested.

Multiple choice technology programming languages
  1. r, t, t,

  2. r, e, o,

  3. Compilation fails.

  4. An exception is thrown at runtime.

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

Line 13 attempts to assign an integer (str.length() returns int) to a boolean variable (check is int, but if condition requires boolean). In Java, check = str.length() is an assignment, not a comparison, and the types are incompatible for the if condition.

Multiple choice technology programming languages
  1. r, t, t,

  2. r, e, o,

  3. Compilation fails.

  4. An exception is thrown at runtime.

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

Line 13 attempts to assign an integer (str.length() returns int) to a boolean variable (check is int, but if condition requires boolean). In Java, check = str.length() is an assignment, not a comparison, and the types are incompatible for the if condition.

Multiple choice technology programming languages
  1. prints "Clove is nice"

  2. prints "Mint is ok"

  3. prints "Sage is average"

  4. Class cast Exception at runtime.

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

The variable c is declared as type Mint. The method c.quality(h) calls quality() on a Mint reference. The argument h is declared as Clove, so the method signature quality(Mint c) in class Mint doesn't match. JVM looks for quality(Clove) in Mint's hierarchy, finds it in Clove class (parent), and executes that due to method resolution rules.

Multiple choice technology programming languages
  1. 10

  2. 20

  3. 30

  4. 40

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

During instantiation of Derived, the Base constructor is called first, which invokes addValue(). Because of dynamic binding, the overridden addValue() in Derived runs, adding 20 to value. Then, the Derived constructor runs and calls addValue() again, adding another 20, resulting in 40.

Multiple choice technology programming languages
  1. Compilation fails.

  2. An exception is thrown at runtime.

  3. The code executes normally and prints ‘foo”.

  4. The code executes normally, but nothing is printed.

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

Calling start() twice on the same Thread object throws IllegalThreadStateException at runtime. Once a thread has been started, subsequent calls to start() cause an exception - you cannot restart a thread.

Multiple choice technology programming languages
  1. n = 100;

  2. i.setX( 100);

  3. o.getY().setX( 100);

  4. i = new Inner(); i.setX( 100);

  5. o.setY( i); i = new Inner(); i.setX( 100);

  6. i = new Inner(); i.setX( 100); o.setY( i);

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

The code passes an Inner object to Outer via setY(). Options B, C, and F all work: B modifies i directly before it was passed to o, C accesses the Inner through o and modifies it, and F creates a new Inner, modifies it, then replaces it in o. Options A, D, and E fail because they modify a different object or n (a primitive).

Multiple choice technology programming languages
  1. A

  2. B

  3. C

  4. D

  5. E

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

Option B: StringBuffer starts with '123456789', delete(0,3) removes '123' leaving '456789', replace(1,3,'24') affects positions 1-2 ('56') replacing with '24' resulting '424789', delete(4,6) removes '89' leaving '4247'. Option E: StringBuilder delete operations transform '123456789' to '4247'. Other options fail due to method chaining issues or incorrect indices.

Multiple choice technology programming languages
  1. c

  2. a

  3. ab

  4. ac

  5. bc

  6. abc

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

When NullPointerException occurs at line 34, it's caught by the first catch block (line 35-36) which prints 'a'. The finally block (line 39-40) always executes, printing 'c'. The output is 'ac'. The second catch block is skipped because the first one matches.

Multiple choice technology programming languages
  1. i=0 j=0

  2. i=0 j=1

  3. i=0 j=2

  4. i=1 j=0

  5. i=1 j=1

  6. i=1 j=2

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

Tracing the loops: When i=0, j prints 2,1 before break at j=0. When i=1, j prints 2 before break at j=1. When i=2, j prints 2,1,0 but i=2 after loop ends. Output: i=0 j=2, i=0 j=1, i=1 j=2.

Multiple choice technology programming languages
  1. 0

  2. 1

  3. 2

  4. 3

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

The post-increment l++ assigns the current value 0 to k, then increments l to 1. Pre-increment ++k increments k to 1 first, then assigns 1 to j. Post-increment j++ assigns 1 to i, then increments j to 2. The output is 1.