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

  2. b

  3. c

  4. d

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

Look closely at line 2, is this an equality check (==) or an assignment (=). The condition at line 2 evaluates to false and also assigns false to bool. bool is now false so the condition at line 6 is not true. The condition at line 10 checks to see if bool is not true (if !(bool == true) ), it isn't so line 12 is executed.

Multiple choice
  1. done

  2. one two done

  3. one two three done

  4. one two three two three done

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

The variable i will have the values 0, 1 and 2. When i is 0, nothing will be printed because of the break in case 0. When i is 1, one two three will be the output because case 1, case 2 and case 3 will be executed (they don't have break statements). When i is 2, two three will be the output because case 2 and case 3 will be executed (again no break statements). Finally, when the for loop finishes done will be output.

Multiple choice
  1. It prints "true".

  2. It prints "Fred".

  3. An exception occurs at runtime.

  4. Compilation fails

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

(2) is an incorrect statement because there is no such requirement. (3) is an incorrect statement and therefore a correct answer because the hashcode for a string is computed from the characters in the string.

Multiple choice
  1. i = 0

  2. i = 1

  3. value of i is undetermined

  4. Statement causes a compile error

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

Math.random() returns a double value greater than or equal to 0 and less than 1. Its value is stored to an int but as this is a narrowing conversion, a cast is needed to tell the compiler that you are aware that there may be a loss of precision. The value after the decimal point is lost when you cast a double to int and you are left with 0.

Multiple choice
  1. java Myfile 222

  2. java Myfile 1 2 2 3 4

  3. java Myfile 1 3 2 2

  4. java Myfile 0 1 2 3

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

Arguments start at array element 0 so the fourth arguement must be 2 to produce the correct output.

Multiple choice
  1. It compiles and runs printing nothing

  2. Compiles but fails at runtime

  3. Compile Error

  4. Prints "complete"

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

No constructor has been defined for class B therefore it will make a call to the default constructor but since class B extends class A it will also call the Super() default constructor. Since a constructor has been defined in class A java will no longer supply a default constructor for class A therefore when class B calls class A's default constructor it will result in a compile error.

Multiple choice
  1. baz =

  2. baz = null

  3. baz = blue

  4. Runtime Exception

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

When running the program you entered 3 arguments "red", "green" and "blue". When dealing with arrays in java you must remember ALL ARRAYS IN JAVA ARE ZERO BASED therefore args[0] becomes "red", args[1] becomes "green" and args[2] becomes "blue". When the program entcounters line 8 above at runtime it looks for args[3] which has never been created therefore you get an ArrayIndexOutOfBoundsException at runtime.

Multiple choice
  1. Compilation fails.

  2. 1..2..3..

  3. 0..1..2..3..

  4. 0..1..2..

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

The thread MyThread will start and loop three times (from 0 to 2). Option A is incorrect because the Thread class implements the Runnable interface; therefore, in line 7, Thread can take an object of type Thread as an argument in the constructor. Option B and C are incorrect because the variable i in the for loop starts with a value of 0 and ends with a value of 2.

Multiple choice
  1. Assertion expressions should not contain side effects.

  2. Assertion expression values can be any primitive type.

  3. Assertions should be used for enforcing preconditions on public methods.

  4. An AssertionError thrown as a result of a failed assertion should always be handled by the enclosing method.

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

Option A is correct. Because assertions may be disabled, programs must not assume that the boolean expressions contained in assertions will be evaluated. Thus these expressions should be free of side effects. That is, evaluating such an expression should not affect any state that is visible after the evaluation is complete. Although it is not illegal for a boolean expression contained in an assertion to have a side effect, it is generally inappropriate, as it could cause program behaviour to vary depending on whether assertions are enabled or disabled. Assertion checking may be disabled for increased performance. Typically, assertion checking is enabled during program development and testing and disabled for deployment. Option B is wrong. Because you assert that something is "true". True is Boolean. So, an expression must evaluate to Boolean, not int or byte or anything else. Use the same rules for an assertion expression that you would use for a while condition. Option C is wrong. Usually, enforcing a precondition on a public method is done by condition-checking code that you write yourself, to give you specific exceptions. Option D is wrong. "You're never supposed to handle an assertion failure" Not all legal uses of assertions are considered appropriate. As with so much of Java, you can abuse the intended use for assertions, despite the best efforts of Sun's Java engineers to discourage you. For example, you're never supposed to handle an assertion failure. That means don't catch it with a catch clause and attempt to recover. Legally, however, AssertionError is a subclass of Throwable, so it can be caught. But just don't do it! If you're going to try to recover from something, it should be an exception. To discourage you from trying to substitute an assertion for an exception, theAssertionError doesn't provide access to the object that generated it. All you get is the String message.

Multiple choice
  1. i = 6 and j = 5

  2. i = 5 and j = 5

  3. i = 6 and j = 6

  4. i = 5 and j = 6

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

The prefix and postfix unary operators have a higher order of evaluation than the relational operators. So on line 4 the variable i is incremented and the variable j is decremented before the greater than comparison is made. As the loop executes the comparison on line 4 will be: if(i > j) if(2 > 9) if(3 > 8) if(4 > 7) if(5 > 6) at this point i is not less than 5, therefore the loop terminates and line 9 outputs the values of i and j as 5 and 6 respectively. The continue statement never gets to execute because i never reaches a value that is greater than j.

Multiple choice
  1. 1 and 3

  2. 2 and 4

  3. 3 and 5

  4. 4 and 6

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

Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to an integer, these can also be used. Also short data types can be used. Short and Long are wrapper classes and reference types can not be used as variables.

Multiple choice
  1. 1

  2. 10

  3. 101

  4. 1101

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

Even though o and oc are reference variables of different types, they are both referring to the same object. This means that == will resolve to true and that the defaultequals() method will also resolve to true.

Multiple choice
  1. AAACCC

  2. AAADDD

  3. BBBCCC

  4. BBBDDD

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

In Java, String.equals(Object) works correctly because String's equals method overrides Object.equals to handle String comparison. Both s.equals(o) and o.equals(s) return true because they reference the same String object 'foo'. The equals method checks for actual content equality, not reference equality.

Multiple choice
  1. ABBCAD

  2. ABCBCAD

  3. CDADACB

  4. Output determined by the underlying platform.

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

Can you guarantee the order in which threads are going to run? No you can't. So how do you know what the output will be? The output cannot be determined.

Multiple choice
  1. j = 0

  2. j = 4

  3. j = 8

  4. The code will run with no output

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

The lines to watch here are lines 9 & 10. Line 9 features the non-shortcut version of theOR operator so both of its operands will be evaluated and therefore methodB(4) is executed. However line 10 has the shortcut version of the OR operator and if the 1st of its operands evaluates to true (which in this case is true), then the 2nd operand isn't evaluated, so methodB(8) never gets called. The loop is only executed once, b is initialized to false and is assigned true on line 9. Thus j = 4.