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

  2. 42.5

  3. 43

  4. bad number

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

All of this code is legal, and line 8 creates a new String with a value of "42.5". Lines 9 and 10 convert the String to a double and then back again. Line 11 is fun—Math.ceil()'s argument expression is evaluated first. We invoke the valueOf()method that returns an anonymous Double object (with a value of 42.5). Then thedoubleValue() method is called (invoked on the newly created Double object), and returns a double primitive (there and back again), with a value of (you guessed it) 42.5. The ceil() method converts this to 43.0, which is cast to an int and assigned to x.

Multiple choice
  1. null null 42

  2. 0 0 42

  3. 0 42 42

  4. 0 0 0

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

In the fix() method, the reference variable tt refers to the same object (class Two) as the t reference variable. Updating tt.x in the fix() method updates t.x (they are one in the same object). Remember also that the instance variable x in the Two class is initialized to 0.

Multiple choice
  1. java

  2. javac

  3. javajavac

  4. Compile error

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

A string is immutable, it cannot be changed, that's the reason for the StringBufferclass. The stringReplace method does not change the string declared on line 14, so this remains set to "java". Method parameters are always passed by value - a copy is passed into the method - if the copy changes, the original remains intact, line 5 changes the reference i.e. text points to a new String object, however this is lost when the method completes. ThetextBuffer is a StringBuffer so it can be changed. This change is carried out on line 9, so "java" becomes "javac", the text reference on line 9 remains unchanged. This gives us the output of "javajavac"

Multiple choice
  1. 41

  2. 42

  3. 50

  4. 51

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

In Java, boolean instance variables are initialized to false, so the if test on line 7 is true and hand is incremented. Line 9 is legal syntax, a do nothing statement. The else-if is true so hand has 7 added to it and is then incremented.

Multiple choice
  1. 0

  2. 1

  3. 101

  4. 111

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

As instance variables, b1 and b2 are initialized to false. The if tests on lines 7 and 9 are successful so b1 is set to true and x is incremented. The next if test to succeed is on line 19 (note that the code is not testing to see if b2 is true, it is setting b2 to be true). Since line 19 was successful, subsequent else-if's (line 21) will be skipped.

Multiple choice
  1. 0

  2. 2

  3. -1

  4. 4

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

The indexOf() method in Java is case-sensitive. The string str is 'Foolish boy.' with capital 'F' in 'Fool'. When we search for the lowercase substring 'fool', it doesn't match because Java considers character case. Since the substring is not found, indexOf() returns -1, which is the standard behavior indicating 'substring not present.'

Multiple choice
  1. 5

  2. 12

  3. 21

  4. 13

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

The while loop continues as long as number < number2 (12). Starting from 0, number increments by 1 each iteration. After 11 iterations, number becomes 11, which is still < 12, so one more iteration occurs, setting number to 12. Now 12 < 12 is false, so the loop exits. Final value is 12.

Multiple choice
  1. 17, byte

  2. 25, byte

  3. 17, int

  4. 25, int

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

Java follows standard mathematical precedence: multiplication before addition. So 2 + 3 * 5 = 2 + (3 * 5) = 2 + 15 = 17. Since all operands are integers, the result is also an integer (int), not a byte. The '17, int' option correctly captures both the calculated value and the data type.

Multiple choice
  1. 40000

  2. 50000

  3. 15000

  4. 10000

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

The while loop iterates 4 times (index 0, 1, 2, 3). Each iteration assigns 10000 to salaries[index]. After index=3 iteration, salaries[3] = 10000. The array doesn't accumulate values - each position gets exactly 10000. This is assignment, not addition. Option D is correct.

Multiple choice
  1. a

  2. v

  3. throws StringIndexOutofBoundsException

  4. null characater

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

Strings are zero-indexed. 'Java' has indices 0-3 (J=0, a=1, v=2, a=3). charAt(4) attempts to access index 4 which doesn't exist, throwing StringIndexOutOfBoundsException.

Multiple choice
  1. x = 0

  2. x = 1

  3. Compilation fails.

  4. An exception is thrown at runtime.

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

The code doesn't compile because the method GetResult() in class A is final and so cannot be overridden.

Multiple choice
  1. 9 7 7 foo 7 7foo

  2. 72 34 34 foo34 34foo

  3. 9 7 7 foo34 34foo

  4. 72 7 34 foo34 7foo

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

Because all of these expressions use the + operator, there is no precedence to worry about and all of the expressions will be evaluated from left to right. If either operand being evaluated is a String, the + operator will concatenate the two operands; if both operands are numeric, the + operator will add the two operands.

Multiple choice
  1. count = 0

  2. count = 2

  3. count = 3

  4. count = 4

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

The reference variables b and x both refer to the same boolean array. count is incremented for each call to the set() method, and once again when the first if test istrue. Because of the && short circuit operator, count is not incremented during the second if test.