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. The man behind C

  2. Experience the legacy of C

  3. Error : == operator cannot be applied to reference data types

  4. boolean cannot be converted to int

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

In C, comparing two arrays using the == operator compares their memory addresses, not their contents. Even though s1 and s2 contain the same string "Dennis", they are separate arrays stored at different memory locations. Therefore s1 == s2 evaluates to false, and the else branch executes, printing "Experience the legacy of C".

Multiple choice technology programming languages
  1. 0

  2. 1

  3. Infinite loop

  4. -1

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

The for loop has an empty body and uses i-- as its condition. The condition is checked before each iteration. Starting with i=0, the condition i-- evaluates to 0 (false) but decrements i to -1 as a side effect. Since the condition is false, the loop body never executes, and the loop terminates. The printf then outputs -1.

Multiple choice technology programming languages
  1. 777777.........

  2. Compilation Error

  3. 7

  4. None of the above

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

In C, it is legal to declare a local variable with the name main inside the main function. This local variable shadows the outer function name. Thus, printf prints the integer value 7. It compile successfully and does not cause infinite recursion or compiler errors.

Multiple choice technology programming languages
  1. 6

  2. Errror

  3. 1

  4. No output

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

The factorial function has incorrect ternary operator syntax. The code (n == 0)? return 1 : return n* factorial(n-1) is invalid because return statements cannot appear inside a ternary expression in this manner. The correct syntax would be 'return (n == 0) ? 1 : n * factorial(n-1);'. This syntax error causes compilation failure.

Multiple choice technology programming languages
  1. I am in 2

  2. Default I am in 2

  3. Default

  4. Compiler Error

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

The output is I am in 2. The switch expression evaluates x (which is 2). The compiler matches this value directly with case 2, executing its block and printing I am in 2. The default block is only executed if no matching case is found, regardless of its position in the switch.

Multiple choice technology programming languages
  1. in m Default

  2. Compiler Error

  3. in m

  4. default

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

Compilation fails because the case label expression m is not a constant expression. In Java, switch case labels must be constant expressions (like literals or variables declared as static final). Because m is a standard local variable, it cannot be used as a case label.

Multiple choice technology programming languages
  1. in 10 Default

  2. Compiler error

  3. in 10

  4. Exception

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

The switch statement in Java does not support the long type as the expression variable. Switch expressions can only be int, short, char, byte (and their wrapper classes), String (Java 7+), or enum types. Since p is a long, this code will not compile regardless of the case labels used.

Multiple choice technology programming languages
  1. Default I am in 3 I am in 1

  2. Default

  3. No output

  4. Exception

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

The switch expression x has value 2, which does not match either case 3 or case 1. Java executes the default case when there is no match. Since there is no break statement after the default case, execution falls through to case 3 and then to case 1, printing all three outputs.

Multiple choice technology programming languages
  1. I am in a

  2. I am in default

  3. Exception

  4. Compiler Error

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

The switch expression is char a with value 'a'. The code has a default case first followed by case 'a'. Since the value 'a' matches case 'a' exactly, Java executes that case and prints "I am in a", then breaks out of the switch statement.

Multiple choice technology programming languages
  1. x = 42

  2. x = 43

  3. x = 44

  4. Compilation fails.

  5. The code runs with no output

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

Variable x is declared inside the try block (scope limited to try), but the finally block attempts to access and increment it with ++x. This is a compilation error because x is not in scope in the finally block. The code does not compile regardless of the exception handling.

Multiple choice technology programming languages
  1. many

  2. a few

  3. Compilation fails

  4. The output is not predictable

  5. An exception is thrown at runtime

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

Compilation fails because 7 is an integer literal of type int. Java does not allow implicit narrowing conversions (from int to short) during method invocation resolution. Since there is no invade(int) or invade(int...) method, the compiler cannot find a matching method signature for invade(7).

Multiple choice technology programming languages
  1. 2

  2. 4

  3. An exception is thrown at runtime

  4. Compilation fails due to an error on line 4

  5. Compilation fails due to an error on line 5

  6. Compilation fails due to an error on line 6

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

Line 7 casts o1 (which is a 2D int array) to a 1D int array. At runtime, this fails because o1 actually references int[][], not int[]. The cast compiles but throws ClassCastException at runtime. Line 4 compiles correctly because a[1] is a 1D array being cast to int[].

Multiple choice technology programming languages
  1. 1 1

  2. 2 1

  3. 3 1

  4. 4 1

  5. 3 3

  6. 4 3

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

The call doX(s,s) passes short values. Java's most specific matching overload is doX(Integer, Integer) because short widens to int, then autoboxes to Integer (Java 5 feature). The call doX(7,7) passes int literals which autobox to Integer, matching the same overload. Output is "3 1" - wait, 7 as int matches Integer exactly, not Long.

Multiple choice technology programming languages
  1. Line 4

  2. Line 5

  3. Line 6

  4. Line 7

  5. Line 8

  6. Line 9

Reveal answer Fill a bubble to check yourself
A,D,E Correct answer
Explanation

Java 5 introduced autoboxing. Line 4: Long.valueOf returns Long but assigns to long - unboxing works in Java 5, not 1.4. Line 5: Long to Long assignment works in both. Line 7: longValue() returns long but assigns to Long - boxing works in Java 5, not 1.4. Line 8: parseLong returns long, assigning to Long requires boxing in Java 5 only.

Multiple choice technology programming languages
  1. Only one compilation will succeed.

  2. Exactly two compilations will succeed.

  3. Exactly three compilations will succeed.

  4. All four compilations will succeed.

  5. No compiler warnings will be produced.

  6. At least one compiler warning will be produced.

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

Java 1.3 allows assert as an identifier, so One.java compiles with -source 1.3. Java 1.4 made assert a keyword, so One.java fails with -source 1.4. Two.java uses assert as a keyword, so it compiles with -source 1.4 but fails with -source 1.3. Exactly two compilations succeed. Using -source 1.4 in Java 5+ produces a warning about obsolete source option.