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
-
float f = 1.3;
-
byte b = 257;
-
int i = 10;
-
boolean b = null;
-
char c = "a";
C
Correct answer
Explanation
Only 'int i = 10' compiles without error. 'float f = 1.3' fails because 1.3 is a double literal requiring casting or 'f' suffix. 'byte b = 257' fails because 257 exceeds byte's range (-128 to 127). 'boolean b = null' fails because booleans can't be null. 'char c = "a"' fails because "a" is a String, not a char.
C
Correct answer
Explanation
In this Java for loop, i starts at 0 and increments while i < 11. The loop runs for i values 0, 1, 2, ..., 10. When i becomes 11, the condition i < 11 is false, so the loop terminates. At termination, i equals 11.
A,D
Correct answer
Explanation
In Java, primitive data types are the basic built-in types that are not objects. 'int' and 'float' are both primitive types - int is a 32-bit integer and float is a 32-bit floating-point number. Array (B) is not a primitive type - it's a reference type/object. String (C) is also not primitive - it's a class from the java.lang package.
-
The output is “Equal”
-
The output in “Not Equal”
-
An error at " if (x = y)" causes compilation to fall.
-
The program executes but no output is show on console.
C
Correct answer
Explanation
In many programming languages like C, Java, and C#, 'if (x = y)' uses the assignment operator (=) instead of comparison (==). This assigns y's value to x, causing a compilation error because the if condition requires a boolean expression. The answer is correctly identified as the compilation error option.
-
The output is “Equal”
-
The output in “Not Equal”
-
An error at " if (x = y)" causes compilation to fall.
-
The program executes but no output is show on console.
C
Correct answer
Explanation
In many programming languages like C, Java, and C#, 'if (x = y)' uses the assignment operator (=) instead of comparison (==). This assigns y's value to x, causing a compilation error because the if condition requires a boolean expression. The answer is correctly identified as the compilation error option.
-
3
-
4
-
4.75
-
Compiler error declaration not allowed
A
Correct answer
Explanation
The code calculates 23 % 4 (modulo operation). 23 divided by 4 is 5 with remainder 3, so 23 % 4 = 3. The variables x and y are declared as bytes but unused, and z is correctly declared as int to store the result.
-
The output is always NO.
-
The output is always ON.
-
The output varies and is either NO or ON.
-
The code does not compile.
A
Correct answer
Explanation
The code calls worker.run() directly instead of worker.start(). Direct run() calls execute synchronously in the current thread, printing 'N' first, then 'O', resulting in 'NO'. No actual thread spawning occurs, so output is always deterministic.
-
int i; /*above main() */
-
extern int i; /*inside main() */
-
Both 1 & 2
-
option 3 if i is defined elsewhere.
D
Correct answer
Explanation
The code uses variable 'i' without declaring it. Options 1 and 2 alone are insufficient. Option 3 (declaring i as extern inside main) requires i to be defined elsewhere globally - that's what D specifies, making it correct.
-
output i=0 j=20
-
output j=20
-
compiler error starting from "printf("i = %d",op.i);"
-
compiler error starting at the end of struct io
D
Correct answer
Explanation
C structs cannot have member initialization at declaration (int i=0 is invalid). This causes a compiler error at struct definition time, not at the printf statements.
A
Correct answer
Explanation
The program compiles. i=0, j=++i makes j=1 and i=1, so j>i is false. The recursive call never executes, but compilation succeeds regardless. Runtime behavior doesn't affect compilation.
-
Value of float variable f is :12.3
-
Value of float variable f is :12
-
error found on the line "float f =12.3;"
-
none of the above
C
Correct answer
Explanation
In Java, floating-point literals like 12.3 are treated as double by default (64-bit). To assign them to a float variable (32-bit), you must either use the 'f' suffix (12.3f) or cast the value. Without this, the compiler will flag an error due to potential loss of precision from double to float.
-
no compilation error;but no output will be displayed
-
inner=3 outer=1
-
inner=2 outer=1
-
compile error
-
Compiler error at line 1
-
Compiler error at line 3
-
Compiler error at line 4
-
NullPointerException
-
StringIndexOutOfBoundsException
-
Prints “123null”
F
Correct answer
Explanation
When append(null) is called on a StringBuffer, it appends the string 'null'. Then insert(0, '123') inserts '123' at position 0. The result becomes '123null'. There's no NullPointerException because StringBuffer.append() handles null by converting it to the string 'null'. Option F correctly identifies this output.
-
Does not compile
-
Throws IllegalFormatConversionException
-
Prints 222.46
-
Prints 222.45
-
Prints 2.224578
-
Prints .2224578
C
Correct answer
Explanation
The format specifier '%.2f' rounds the double value to 2 decimal places. 222.4578 rounded to 2 decimal places is 222.46 (not 222.45, because the third decimal 7 causes rounding up). The printf method compiles and runs without error, printing the rounded value.