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

  2. 7

  3. 8

  4. 14

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

The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.

Multiple choice
  1. ok

  2. dokey

  3. ok dokey

  4. No output is produced

  5. Compilation error

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

The & operator has a higher precedence than the | operator so that on line 8 b1 and b2are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to be true. Hence it prints "dokey".

Multiple choice
  1. f[0] = 0

  2. f[0] = 0.0

  3. Compile Error

  4. Runtime Exception

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

The choices are between Option A and B, what this question is really testing is your knowledge of default values of an initialized array. This is an array type float i.e. it is a type that uses decimal point numbers therefore its initial value will be 0.0 and not 0

Multiple choice
  1. This code will not compile due to line 5.

  2. This code will not compile due to line 6.

  3. 1..2..

  4. 1..2..3..

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

Line 6 calls the run() method, so the run() method executes as a normal method should and it prints 1..2.. Option 1 is incorrect because line 5 is the proper way to create an object. Option 2 is incorrect because it is legal to call the run() method, even though this will not start a true thread of execution. The code after line 6 will not execute until the run() method is complete. Option 4 is incorrect because the for loop only does two iterations.

Multiple choice
  1. An exception occurs at runtime at line 10.

  2. It prints "Zippo".

  3. Compilation fails because of an error on line 7.

  4. Compilation fails because of an error on line 13.

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

The code in the HorseTest class is perfectly legal. Line 13 creates an instance of the method-local inner class Horse, using a reference variable declared as type Object. Line 14 casts the Horse object to a Horse reference variable, which allows line 15 to compile. If line 14 were removed, the HorseTest code would not compile, because class Objectdoes not have a name variable.

Multiple choice
  1. An error at line 11 causes compilation to fail

  2. Errors at lines 8 and 9 cause compilation to fail.

  3. The program prints pairs of values for x and y that might not always be the same on the same line (for example, "x=2, y=1")

  4. The program prints pairs of values for x and y that are always the same on the same line (for example, "x=1, y=1". In addition, each value appears once (for example, "x=1, y=1" followed by "x=2, y=2")

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

The synchronized code is the key to answering this question. Because x and y are both incremented inside the synchronized method they are always incremented together. Also keep in mind that the two threads share the same reference to the Q126 object. Also note that because of the infinite loop at line 13, only one thread ever gets to execute.

Multiple choice
  1. 0 2 4

  2. 0 2 4 6

  3. Compilation fails at line 2

  4. Compilation fails at line 10

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

Nonnested classes cannot be marked protected (or final for that matter), so the compiler will fail at protected class NewTreeSet.

Multiple choice
  1. 18

  2. 117

  3. 567

  4. Compiler error

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

This question is about the + (plus) operator and the overriden + (string cocatanation) operator. The rules that apply when you have a mixed expression of numbers and strings are: If either operand is a String, the + operator concatenates the operands. If both operands are numeric, the + operator adds the operands. The expression on line 6 above can be read as "Add the values i1 and i2 together, then take the sum and convert it to a string and concatenate it with the String from the variable s1". In code, the compiler probably interprets the expression on line 8 above as: System.out.println( new StringBuffer()     .append(new Integer(i1 + i2).toString())     .append(s1)     .toString() );

Multiple choice
  1. abcdefghi

  2. abcdefdef

  3. abcghidef

  4. abcghighi

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

After line 7 executes, both s2 and s3 refer to a String object that contains the value "def". When line 8 executes, a new String object is created with the value "ghi", to which s2 refers. The reference variable s3 still refers to the (immutable) String object with the value "def".

Multiple choice
  1. Base

  2. BaseBase

  3. Compilation fails

  4. The code runs with no output

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

Option B is correct. It would be correct if the code had compiled, and the subclass Alphahad been saved in its own file. In this case Java supplies an implicit call from the sub-class constructor to the no-args constructor of the super-class therefore line 12 causesBase to be output. Line 13 also causes Base to be output. Option A is wrong. It would be correct if either the main class or the subclass had not been instantiated. Option C is wrong. The code compiles. Option D is wrong. There is output.

Multiple choice
  1. result = 1

  2. result = 10

  3. result = 11

  4. result = 11010

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

Line 13 fails because == compares reference values, not object values. Line 15 succeeds because both String and primitive wrapper constructors resolve to the same value (except for the Character wrapper). Lines 17, 19, and 21 fail because the equals()method fails if the object classes being compared are different and not in the same tree hierarchy.

Multiple choice
  1. abcXyZ

  2. abcxyz

  3. xyzabc

  4. XyZabc

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

Line 2 creates a new String object with the value "XYZ", but this new object is immediately lost because there is no reference to it. Line 3 creates a new String object referenced by y. This new String object has the value "xyz" because there was no "Y" in the String object referred to by x. Line 4 creates a new String object, appends "abc" to the value "xyz", and refers y to the result.

Multiple choice
  1. int[ ] scores = {3, 5, 7};

  2. int [ ][ ] scores = {2,7,6}, {9,3,45};

  3. String cats[ ] = {"Fluffy", "Spot", "Zeus"};

  4. boolean results[ ] = new boolean [] {true, false, true};

  5. Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};

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

Option B generates a compiler error: <identifier> expected. The compiler thinks you are trying to create two arrays because there are two array initialisers to the right of the equals, whereas your intention was to create one 3 x 3 two-dimensional array. To correct the problem and make option B compile you need to add an extra pair of curly brackets: int [ ] [ ] scores = { {2,7,6}, {9,3,45} };

Multiple choice
  1. Line 11

  2. Line 12

  3. Line 14

  4. Line 22

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

Assert statements should not cause side effects. Line 22 changes the value of z if the assert statement is false. Option A is fine; a second expression in an assert statement is not required. Option B is fine because it is perfectly acceptable to call a method with the second expression of an assert statement. Option C is fine because it is proper to call an assert statement conditionally.

Multiple choice
  1. 4, 4

  2. 4, 5

  3. 5, 4

  4. Compilation fails.

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

Option D is correct, compilation fails - The return type of getLength( ) in the super class is an object of reference type Integer and the return type in the sub class is an object of reference type Long. In other words, it is not an override because of the change in the return type and it is also not an overload because the argument list has not changed.