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

Multiple choice technology programming languages
  1. The program has syntax errors because the variable radius is not initialized.

  2. The program has a syntax error because a constant PI is defined inside a method.

  3. The program has no syntax errors but will get a runtime error because radius is not initialized.

  4. The program compiles and runs fine

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

To analyze the given code, we can see that it calculates the area of a circle using a radius and a constant value of PI.

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

A. The program has syntax errors because the variable radius is not initialized.

This option is partially correct. The variable radius is indeed declared but not initialized. Java requires variables to be initialized before they can be used in calculations. Therefore, the program will not compile due to this syntax error.

B. The program has a syntax error because a constant PI is defined inside a method.

This option is also partially correct. The constant PI is indeed defined inside the main method. However, this is not a syntax error. It is a common practice to define constants inside methods in Java. Therefore, this option is incorrect.

C. The program has no syntax errors but will get a runtime error because radius is not initialized.

This option is incorrect. The program has a syntax error because the variable radius is not initialized. Therefore, the program will not run at all, let alone produce a runtime error.

D. The program compiles and runs fine.

This option is incorrect because the program has a syntax error. It will not compile due to the uninitialized variable radius.

Therefore, the correct answer is:

The Answer is: A

Multiple choice technology programming languages
  1. The program has a syntax error because System.out.println method cannot be invoked from the constructor.

  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.

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

The Test class defines a constructor with a String parameter (Test(String t)), so Java does NOT auto-generate a no-argument constructor. The main method tries to call 'new Test()' which requires a no-argument constructor that doesn't exist. This is a compilation error, not a runtime error or issues with printing.

Multiple choice technology programming languages
  1. String i = "abc"

  2. double i = 0.0/0.0

  3. int i = 3/7

  4. int i = 0

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

In IEEE floating-point arithmetic, 0.0/0.0 produces NaN (Not a Number), and NaN != NaN always evaluates to true, creating an infinite loop. Integer division (3/7) equals 0, String comparison compares references, and 0 != 0 is false - only NaN creates this condition.

Multiple choice technology programming languages
  1. Its not possible

  2. print some garbage value

  3. compilation error

  4. Hello world

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

The Unicode escapes decode to valid Java code: a class named 'Ugly' with a main method that concatenates and prints two strings. Java allows flexible spacing, and string concatenation with + is valid. The program compiles and runs successfully.

Multiple choice technology programming languages
  1. Compilation error

  2. Runtime error

  3. 17777

  4. 66666

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

The expression adds an integer (12345) and a long literal (5432l). Java automatically promotes the int to long and performs the addition, resulting in 17777, which is printed to the console. Option A is incorrect because the code is syntactically valid. Option B is incorrect because there are no runtime errors. Option D is incorrect because it represents simple concatenation, not arithmetic addition.

Multiple choice technology programming languages
  1. The class compiles and runs, but does not print anything.

  2. The number 2 gets printed with AssertionError

  3. compile error

  4. The number 3 gets printed with AssertionError

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

The nested loops iterate i=2,3 and j=2,3. The condition if(i < j) filters cases: (2,3) and (2,3) where assert i!=j executes. Since 2!=3 and 2!=3 are both true, the assert never fails. When the assert condition is true (i != j), no AssertionError is thrown. Thus code compiles, runs, and prints nothing.

Multiple choice technology programming languages
  1. 0 false 0

  2. 0 true 0

  3. 0 0 0

  4. Compile error - static variable must be initialized before use.

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

To solve this question, the user needs to know about Java classes, instance variables, static variables, and method invocation.

The given program has two classes: A and Test. The A class defines three instance variables: k (of type int), istrue (of type boolean), and a static variable p (of type int). Class A also defines a method named printValue() that prints the values of k, istrue, and p, in that order.

In the main() method of the Test class, an object of class A is created and its printValue() method is called.

Since int and boolean instance variables are initialized to their default values of 0 and false respectively, and the static variable p is also initialized to 0, the output of the program will be:

A. 0 false 0

Option A is correct because the program will print the values of k (0), istrue (false), and p (0) in that order.

Option B is incorrect because istrue is initialized to false, not true.

Option C is incorrect because k and istrue are both non-static instance variables, and they will be initialized to their default values of 0 and false, respectively. The static variable p will also be initialized to 0.

Option D is incorrect because there is no compile error in the code.

The Answer is: A. 0 false 0