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. Compile error at 2,3,4,5

  2. Compile error at 1,2,3,4,5

  3. prints 3

  4. none of the above

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

In Java, local variables inside methods cannot have access modifiers (private, protected, public). These modifiers only apply to class members (fields and methods). Lines 2, 3, and 4 attempt to declare local variables with access modifiers, which is a compile error. Line 5 uses these variables, adding to the errors. The class-level 'int i' is valid shadowed by the local 'int i' at line 1.

Multiple choice technology programming languages
  1. Prints one, two , three

  2. prints one

  3. compile time error

  4. Run time Exception

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

This switch statement lacks 'break' statements, causing fall-through execution. When i1 equals 1, case 1 matches and 'one' is printed, but execution continues through case 2 (printing 'two') and case 3 (printing 'three'). This is the default switch behavior in Java - without explicit breaks, all cases after the matching case execute sequentially.

Multiple choice technology programming languages
  1. Yes will compile

  2. no doesnot compile

  3. changes have to be made in checking the variables

  4. none

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

This line compiles successfully assuming the variables i and j are declared as compatible numeric types. The initialization, condition, and update expressions within the for-loop header follow standard Java syntax. The distractor options incorrectly claim compilation failure or suggest unnecessary changes to the loop structure.

Multiple choice technology programming languages
  1. true

  2. Compile Error

  3. false

  4. NullPointerException

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

To solve this question, the user needs to know the basics of regular expressions and the use of the Pattern and Matcher classes in Java. The given code compiles a regular expression pattern and matches it with the string "aaab". The regular expression pattern "a{3}b?c*" means that the pattern should start with three "a" characters, optionally followed by a single "b" character, and then zero or more "c" characters.

The Matcher.matches() method tries to match the entire input sequence against the pattern. If the entire input sequence matches the pattern, the method returns true; otherwise, it returns false.

In this case, the input sequence "aaab" matches the pattern "a{3}b?c*", because it starts with three "a" characters and ends with zero "c" characters. The optional "b" character is present in the input sequence, but it is not required to match the pattern.

Therefore, the output of the code will be:

The Answer is: A (true)

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.