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

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

  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.