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 technology programming languages
  1. 3,2, 1,

  2. 1, 2, 3,

  3. Compilation fails.

  4. The code runs with no output.

  5. An exception is thrown at runtime.

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

The reverse() method is declared with a raw type 'List' instead of a generic 'List'. The enhanced for-loop expects an Iterable from the raw type, but when used with generics, this creates a type mismatch. The method signature needs generics or explicit casting to compile cleanly.

Multiple choice technology programming languages
  1. A, B, C,

  2. B, C, A,

  3. Compilation fails.

  4. The code runs with no output.

  5. An exception is thrown at runtime.

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

LinkedList maintains insertion order. Elements are added as B, then C, then A. The iterator returns them in this order: B, C, A. The for-each loop prints each with a comma, producing 'B, C, A, '.

Multiple choice technology programming languages
  1. NumberFormat Exception

  2. 100

  3. Compilation fails at line 4

  4. 99

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

The loop variable is of type short, but the expression i minus one is promoted to an int. Consequently, remove is invoked with an Integer object, which never matches any Short objects in the set, leaving the set size at one hundred.

Multiple choice technology programming languages
  1. A compile time error indicating that no run method is defined for the Thread class.

  2. A run time error indicating that no run method is defined for the Thread class.

  3. Clean compile and at run time the values 0 to 9 are printed out.

  4. Clean compile but no output at runtime.

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

To understand what will happen when you attempt to compile and run the given code, the user needs to know the concept of multithreading in Java.

The code defines a class Bground that extends the Thread class and overrides its start() method. In the main method, an instance of Bground is created and its run() method is called.

Now, let's go through each option and explain why it is right or wrong:

A. A compile time error indicating that no run method is defined for the Thread class. This option is incorrect because the Thread class already has a run() method defined, and the Bground class inherits this method from Thread. Therefore, there will be no compile time error.

B. A run time error indicating that no run method is defined for the Thread class. This option is incorrect because the Bground class has a run() method defined that overrides the run() method of the Thread class. Therefore, there will be no run time error.

C. Clean compile and at run time the values 0 to 9 are printed out. This option is incorrect because the run() method of the Bground class is not called with the start() method. Instead, the run() method is called directly in the main method. Therefore, only the start() method of the Bground class is executed, which does not print any output.

D. Clean compile but no output at runtime. This option is correct. The code will compile without any errors and there will be no output at runtime because only the start() method of the Bground class is executed, which does not print any output.

Therefore, the correct answer is: D.

Multiple choice technology programming languages
  1. The program will output 'Hello Thread' and 'My Thread'.

  2. The program will output 'Hello Thread'.

  3. The program will output 'My Thread' and 'Hello Thread'.

  4. The order of the outputs is not will vary upon execution.

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

The HelloThread class overrides start() without calling super.start(), so no new thread is created. It only prints 'Hello thread'. The run() method never executes because start() was overridden instead of the thread actually starting.

Multiple choice technology programming languages
  1. Wrapper Type Call

  2. Primitive Type Call

  3. Compiler Error

  4. Compiles fine, throws runtime exception

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

When a byte is passed to overloaded methods accepting int and Integer, Java chooses primitive widening (byte to int) over autoboxing. The method(int i) version is called.

Multiple choice technology programming languages
  1. true

  2. false

  3. compilation error

  4. exception thrown at runtime

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

When using 'new String()', a new String object is created on the heap each time, even if the character sequence is the same. s1 and s2 are two distinct objects with the same value 'abc'. The '==' operator compares object references, not content, so s1 == s2 returns false.

Multiple choice technology programming languages
  1. true

  2. false

  3. compilation error

  4. exception thrown at runtime

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

The equals method compares the value of the Integer object i1, which is 10, with the value of the argument. The expression i2++ uses the post-increment operator, so it evaluates to 10 for the comparison and then increments i2 to 11. Since 10 equals 10, the equals method returns true.

Multiple choice technology programming languages
  1. Compilation and output of "Not equal! 10".

  2. Compilation and output of "Not equal! 20".

  3. Compilation and output of "We are equal 10".

  4. Compilation error, attempting to perform binary comparison on logical data type.

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

The condition uses the short-circuit logical AND operator &&. Since b1 == true evaluates to false, the second operand ((Output += 10) == 20) is never executed. Consequently, Output remains 10, and the program enters the else block, printing "Not equal! 10".

Multiple choice technology programming languages
  1. 0 0

  2. Compilation error, class A has no start method

  3. 0 1

  4. Compilation succeed but runtime exception

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

To understand the code, the user needs to know the basics of Java threads and anonymous inner classes.

The code defines a class A that extends Thread and overrides its run() method to print the values of i from 0 to 1. It also defines a class Test with a main() method that creates an instance of Test and calls its check() method with an anonymous inner class that extends A. The check() method calls the start() method on the A instance, which starts a new thread that executes the run() method.

Now let's go through each option and explain why it is true or false:

A. 0 0: This option is false. The code creates a new thread that executes the run() method of the A instance, which prints the values of i from 0 to 1. Since the start() method is called only once, the output will be either "0 1" or "1 0", depending on which thread runs first.

B. Compilation error, class A has no start method: This option is false. The A class extends Thread, which provides the start() method. Thus, there is no compilation error.

C. 0 1: This option is true. The code creates a new thread that executes the run() method of the A instance, which prints the values of i from 0 to 1. Since the start() method is called only once, the output will be either "0 1" or "1 0", depending on which thread runs first.

D. Compilation succeed but runtime exception: This option is false. There is no runtime exception in the code.

Therefore, the correct answer is:

The Answer is: C. 0 1

Multiple choice technology programming languages
  1. run-a

  2. run-a run-a

  3. Compilation fails with an error at line 6

  4. Compilation succeed but Runtime Exception

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

Calling start() on a Thread that has already been started is illegal and causes it to throw an IllegalThreadStateException at runtime. The code itself is syntactically valid and compiles without error.

Multiple choice technology programming languages
  1. Value is 67 18 String 13

  2. Value is 13 18 String 13

  3. Value is 13 18 String

  4. Compilation fails

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

First, string concatenation evaluates "Value is " + b + c as "Value is 67". Next, numeric addition of a + b + c yields 18. Finally, the expression "String " + (b+c) evaluates the parenthesis first to get 13, printing String 13.

Multiple choice technology programming languages
  1. Compile fail due to error on line no 2

  2. Compile fail due to error on line no 9

  3. Compile fail due to error on line no 8

  4. 15

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

The code fails to compile because line 9 attempts to call the instance method add() from a static context (main method). Non-static methods must be called on an object instance. You would need to create an instance of class B (or A) with 'new B().add(s, 6)' or declare add() as static to call it directly.