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

  2. hi hi

  3. hi hi hi

  4. Compilation fails

  5. hi, followed by an exception

  6. hi hi, followed by an exception

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

m2.go() prints hi because m2's m1 is null by default. m3.go() prints hi because m3's m1 is set to m2. m4.go() prints hi because m4 is same as m3's m1. When m5.go() is called, m2.m1 is null (default value), so calling go() on null throws NullPointerException after printing hi twice.

Multiple choice technology programming languages
  1. true true

  2. false true

  3. true false

  4. false false

  5. Compilation fails.

  6. An exception is thrown at runtime

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

Explanation: Let's go through the code step by step to understand the result.

  1. We have a class called "Fizz" with an instance variable "x" initialized to 5.

  2. In the main method, we create two instances of the "Fizz" class - f1 and f2.

  3. We then call the static method "FizzSwitch" passing f1 and f2 as arguments. In this method, we declare a final variable "z" and assign it the value of "x" (which is f1).

  4. Next, we modify the value of "x" using the "z" reference. Therefore, the value of "x" in f1 is changed to 6.

  5. Finally, we return the reference "z" (which is f1) and assign it to the variable "f3".

  6. In the println statement, we compare if f1 and f3 are the same object reference (f1 == f3) and if their instance variable "x" is the same (f1.x == f3.x).

Now let's analyze the options:

Option A) true true - This option is correct because f1 and f3 refer to the same object, and the value of "x" in both objects is 6.

Option B) false true - This option is incorrect because f1 and f3 refer to the same object, so f1 == f3 should be true.

Option C) true false - This option is incorrect because f1 and f3 refer to the same object, so f1 == f3 should be true.

Option D) false false - This option is incorrect because f1 and f3 refer to the same object, so f1 == f3 should be true.

Option E) Compilation fails - The code does not have any compilation errors, so this option is incorrect.

Option F) An exception is thrown at runtime - The code does not have any exceptional behavior, so this option is incorrect.

The correct answer is option A) true true. This option is correct because f1 and f3 refer to the same object, and the value of "x" in both objects is 6.

Therefore, the result of the code is "true true".

The correct answer is option A.

Multiple choice technology programming languages
  1. 343 340 340

  2. 343 340 342

  3. 343 341 342

  4. 343 341 340

  5. Compilation fails.

  6. An exception is thrown at runtime

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

Static field tooth (343) prints first. The final local tooth (340) shadows the static field. doIt receives 340, pre-increments to 341 (prints), pre-increments to 342 (returns, discarded). main's final tooth remains 340 (passed by value).

Multiple choice technology programming languages
  1. One of them will compile, only one will be true.

  2. Two of them will compile, only one will be true.

  3. Two of them will compile, two will be true.

  4. Three of them will compile, only one will be true

  5. Three of them will compile, exactly two will be true.

  6. Three of them will compile, exactly three will be true.

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

Analyzing each fragment: F1 (f1 == f2) won't compile - comparing primitive to 2D array. F2 (f1 == f2[2][1]) compiles and compares 2.3f == 2.7f, which is false. F3 (x == f2[0][0]) compiles with unboxing, 42L == 42.0f is true. F4 (f1 == f2[1,1]) won't compile - invalid comma syntax. F5 (f3 == f2[2]) compiles but compares array references, which is false. Thus F2, F3, F5 compile (3 total), but only F3 is true (1 true).

Multiple choice technology programming languages
  1. I am in for loop

  2. Infinite Loop

  3. Compiler Error

  4. Exception

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

This code contains an infinite loop because the for loop has no initialization, condition, or increment/decrement parts - it is written as 'for(;;)' which creates an endless loop. The statement 'I am in for loop' will print continuously without termination. Options A, C, and D are incorrect because the code compiles successfully and runs indefinitely.

Multiple choice technology programming languages
  1. I am in do-while

  2. I am in while I am in do-while

  3. Compiler Error

  4. No output

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

The do-while loop executes once because the condition is checked at the end. Inside, the while loop condition p>100 is initially false (p equals 100), so it never executes. Therefore, only the do-while body prints once.

Multiple choice technology programming languages
  1. i in loop = 0 i in loop = 1 i in loop = 2

  2. i in loop = 0 i in loop = 0 i in loop = 1 i in loop = 2

  3. i in loop = 0 i in loop = 2

  4. Compiler Error

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

The loop runs for i values 0, 1, and 2. When i = 0, p = 1, printing 'i in loop = 0'. When i = 1, p = 2, triggering the continue statement and skipping the print. When i = 2, p = 3, printing 'i in loop = 2'.

Multiple choice technology programming languages
  1. Compiler Error

  2. i= 1 j = 1

  3. i= 1 j = 0

  4. Exception

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

In Java, the condition expression of a for loop must evaluate to a single boolean value. Using a comma operator like i<2,j<2 is invalid syntax in Java and causes a compiler error. Java does not support the comma operator in the condition section of loops.

Multiple choice technology programming languages
  1. i in for loop = 1 For loop over Method call over

  2. i in for loop = 1 Method call over

  3. Compiler Error

  4. Exception

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

The for loop starts with i=1 and prints it. The if condition (i==1) is true, so return immediately exits the testFor() method. Therefore, the code after the for loop (For loop over) never prints, and execution continues in main with Method call over.

Multiple choice technology programming languages
  1. i in for loop = 1 i in for loop = 2 For loop over Method call Over

  2. i in for loop = 1 For loop over Method call Over

  3. Compiler Error

  4. i in for loop = 1 Method call Over

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

The for loop prints i=1, then the if (i==1) triggers break. This exits only the for loop, not the method. After the loop, For loop over prints, then back in main, Method call Over prints.

Multiple choice technology programming languages
  1. Compiler Error

  2. I am in Constructor

  3. Message Hello

  4. Exception

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

The code defines two main methods with different signatures: main(String[] args) and main(String arg). Java allows method overloading with different parameter lists. When the class is run, the JVM invokes main(String[] args), creating a TestMain object with the no-arg constructor, which prints 'I am in Constructor'.

Multiple choice technology programming languages
  1. Exception

  2. Compiler Error

  3. p = 0

  4. p=10

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

The parameterized constructor calls this() to invoke the no-arg constructor, but places this() after the assignment 'this.p = p'. In Java, constructor chaining with this() must be the FIRST statement in a constructor. Placing it after other code causes a compilation error.

Multiple choice technology programming languages
  1. I am in Constructor p = 10

  2. I am in Constructor p = 0

  3. Compiler Error

  4. Exception

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

When TestConstructor2(10) is called, it first executes this() which calls the no-arg constructor, printing 'I am in Constructor'. Then execution returns to complete the parameterized constructor, which sets p=10 and prints 'p = 10'. The output combines both print statements in order.

Multiple choice technology programming languages
  1. Compiler Error

  2. Exception

  3. p = 0

  4. p=10

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

The class has two issues: (1) 'public void TestConstructor(int p)' is a regular method, not a constructor, because constructors have no return type - 'void' makes it a method. (2) The class lacks a no-arg constructor, but 'new TestConstructor()' tries to call one. Additionally, 'TestConstructor tp' is declared twice causing a duplicate variable error. Compilation fails.

Multiple choice technology programming languages
  1. The program has a syntax error because test is not initialized.

  2. The program has a syntax error because x has not been initialized.

  3. The program has a syntax error because you cannot create an object from the class that defines the object.

  4. The program has a syntax error because Test does not have a default constructor.

  5. The program has a runtime NullPointerException because test is null while executing test.x.

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

The code compiles without syntax errors. Declaring test as null is valid, and x defaults to 0. However, at runtime, attempting to access test.x when the reference test is null causes a NullPointerException. The other options are incorrect because there are no compile-time or syntax errors present in this program.