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
  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. compile and display “Equal”

  2. compile and display “Not Equal”

  3. cause a compiler error

  4. compile and display NULL

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

The trim method removes leading and trailing whitespace, returning "Welcome". When compared using the equality operator ==, Java compares object references. Since string literals of the same value are interned and pooled together in Java, both references point to the exact same object in the string pool, evaluating to true and printing Equal.

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.

Multiple choice
  1. True

  2. False

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

Java supports implicit type widening or promotion from lower precision to higher precision types. For example, byte (8 bits) can be assigned to int (32 bits), or int can be assigned to long. This is safe because the destination type can accommodate all possible values of the source type without data loss. The reverse (higher to lower precision) requires explicit casting.