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

  2. good idea

  3. good idea good idea

  4. good 0 good 0 1

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

Java passes parameters by value. The nameTest method receives a copy of the reference 'sName', so reassigning it locally doesn't affect the static variable. The thread's run() method modifies sName but main() prints before thread execution. Output is 'good'.

Multiple choice technology programming languages
  1. Compilation Fail

  2. java.io.NotSerializableException: Because class A is not Serializable

  3. Run properly

  4. Compilation Fail : Because class A is not Serializable.

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

Class B implements Serializable, but it contains an instance variable of class A which is not Serializable. Since A does not implement Serializable and is not marked transient, serialization of B at runtime fails with java.io.NotSerializableException. It compiles fine.

Multiple choice technology programming languages
  1. 9 0 9

  2. 9 7 9

  3. 0 0 0

  4. 0 7 0

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

When deserializing, transient fields reset to default values (int becomes 0). Static fields belong to the class, not instances, so they maintain their value (9). First print: a.b = 9. After deserialization: s2.a = 0, s2.b = 9. Output: '9 0 9'.

Multiple choice technology programming languages
  1. 7:30

  2. Compile Error - an enum cannot be instantiated using the new operator

  3. 12:50

  4. 19:45

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

Enum constants cannot be instantiated with the 'new' keyword - they are predefined instances. The line 'Test t = new BREAKFAST;' attempts to use 'new' which is invalid for enums, causing a compilation error.

Multiple choice technology programming languages
  1. Compile error

  2. One One Two Two

  3. One Two One Two

  4. One Two

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

The code calls run() directly, not start(), so no actual threading occurs. Each Test1 runs its run() method sequentially in the main thread. Each loop prints twice, so 'One One Two Two' is the output.

Multiple choice technology programming languages
  1. Compilation clean and run but no output

  2. Compilation and run with the output "Running"

  3. Compile time error with complaint of no Thread import

  4. Compile time error with complaint of no access to Thread package

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

When start() is called on a Thread, it invokes the run() method in a new thread of execution. The Test class extends Thread and overrides run() to print "Running". When main() calls b.start(), the JVM spawns a new thread and executes run(), which outputs "Running" to the console. Options A and D are incorrect because Thread is in java.lang which is auto-imported. Option C is wrong - no explicit import is needed for Thread.

Multiple choice technology programming languages
  1. anonymous tech

  2. Compile Error

  3. Tech

  4. anonymous tech Tech

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

The code creates an anonymous inner class that extends Tech and overrides the tech() method. When a.tech() is called in dothis(), it invokes the overridden version which prints "anonymous tech". Option B is wrong - anonymous inner classes extending a class are valid syntax. Option C would only occur if the original Tech.tech() was called, but it's overridden. Option D is incorrect - only one method call happens, printing one line.

Multiple choice technology programming languages
  1. Outer x is Outer variable.

  2. Compile Error

  3. Local variable z is local variable

  4. Outer x is Outer variable Local variable z is local variable

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

This code attempts to access a local variable z from within a method-local inner class. Local variables accessed by inner classes must be final or effectively final (never reassigned). Since z is a local variable in doStuff() that the Inner class tries to print, this causes a compilation error. The Outer private variable x is accessible, but z violates the final-variable rule for inner classes.

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. The number 3 gets printed with AssertionError

  4. compile error

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

When assertions are enabled, the loop executes. When i = 2 and j = 2, the assert condition i != j fails. The detail message is the value of i (2). Thus, an AssertionError is thrown printing 2. If assertions are disabled, it prints nothing, but the standard test behavior assumes -ea is enabled.

Multiple choice technology programming languages
  1. das abcdef abcdef

  2. das das abcdef abcdef

  3. das abcdef

  4. abcdef abcdef

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

HashSet uses equals() and hashCode() to determine uniqueness. For String objects, s1 and s2 have the same content ("das"), so they're equal - only one is stored. NameBean uses the default Object equals()/hashCode(), which check object reference, not content. s3 and s4 are different objects, so both are added. Output shows 3 elements: one String ("das") and two NameBeans (both print "abcdef").

Multiple choice technology programming languages
  1. Hello

  2. Compile error

  3. java.lang.ClassCastException

  4. null

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

The raw ArrayList (no type parameter) can hold any object. When we add a String to it, it compiles due to backward compatibility. However, when we retrieve it through listBuf (typed as ArrayList), the runtime cannot enforce the type - it returns the actual object (String) and attempts to cast it to StringBuffer, causing ClassCastException. This demonstrates why mixing raw and generic types is unsafe.

Multiple choice technology programming languages
  1. Compile error : Integer can't add

  2. newyork ca texas 11

  3. newyork ca texas

  4. newyork ca

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

To understand the output of the given code, it is important to know the concept of Queue in Java.

A Queue is a data structure that follows the First-In-First-Out (FIFO) principle. In a Queue, elements are added from the rear and removed from the front. The methods used to add elements to the Queue are 'add' and 'offer', whereas 'poll' and 'remove' are the methods used to remove elements from the Queue.

Now let's go through the code and analyze the output.

The code creates a Queue of Strings named 'q' and adds three Strings to it: "newyork", "ca", and "texas". Then the method 'show(q)' is called, and a new Integer object 11 is added to the Queue 'q' inside the 'show' method. Finally, the elements of the Queue are printed using the 'poll' method.

Since the Queue 'q' has been defined as a Queue of Strings, adding an Integer object to the Queue will result in a compile-time error. Therefore, option A is incorrect.

The 'poll' method removes and returns the head of the Queue. So, the output will be the elements of the Queue in the order they were added. Thus, option C is incorrect because it does not include the added Integer object.

Therefore, the correct answer is:

The Answer is: B. newyork ca texas 11

Multiple choice technology
  1. The program does not compile as j is not initialized

  2. The program compiles but does not run

  3. The program compiles and runs but does not print anything

  4. prints some value

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

Local variable 'j' is used without being initialized - in Java, local variables must be explicitly initialized before use. The 'j++' operation in the do-while tries to increment an uninitialized variable, causing a compile-time error. This is different from instance variables which get default values.

Multiple choice technology
  1. MyProg

  2. "I"

  3. "like"

  4. 3

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

When running a Java application from the command line, args[0] is the first command-line argument following the class name. For the command 'java MyProg I like tests', args[0] is 'I', args[1] is 'like', and args[2] is 'tests'.

Multiple choice technology java
  1. 50

  2. ‘\u0000'

  3. cannot be determined

  4. always null until a value is

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

To answer this question, you need to understand how arrays work in Java. Specifically, the behavior of character arrays when they are initialized.

In Java, when you create an array, all elements in the array are automatically initialized to their default values. For the char data type, the default value is the null character, represented as '\u0000'.

Let's go through each option to understand why it is correct or incorrect:

Option A) 50 - This option is incorrect. When an array of char type is created in Java, its elements are not automatically initialized to their indices. Therefore, c[50] is not equal to 50.

Option B) ‘\u0000' - This option is correct. As mentioned above, when a char array is created in Java, its elements are automatically initialized to the null character, '\u0000'. Therefore, c[50] is equal to '\u0000'.

Option C) cannot be determined - This option is incorrect. In Java, the behavior of array initialization is well-defined. For a char array, all elements are automatically initialized to '\u0000'. Therefore, the value of c[50] can be determined.

Option D) always null until a value is - This option is incorrect. In the context of Java, "null" usually refers to a reference that does not point to any object. But for primitive types, such as char, the concept of "null" does not apply. The default value for a char is '\u0000', not "null".

The correct answer is B) ‘\u0000'. This option is correct because in Java, when a char array is initialized, all of its elements are automatically set to the null character, '\u0000'.