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
-
Compilation fails.
-
An exception is thrown at runtime
-
The variable first is set to null.
-
The variable first is set to elements[0].
D
Correct answer
Explanation
The ternary operator checks if elements.length > 0 (true since array has 3 elements), so it returns elements[0] which is 'for'. The colon separates the true and false branches - elements[0] is assigned to first. The array initialization syntax is valid.
-
String value.............null
-
Exception
-
Compiler Error
-
Integer value.............0
C
Correct answer
Explanation
The code creates ambiguity because both overloaded displayVal methods can accept zero arguments. The String... varargs and int... varargs methods are both valid candidates for the call displayVal() with no parameters. Java cannot determine which method to invoke, resulting in a compile-time error.
-
No observations
-
x y {A 1} {A 2} {B 2}
-
x y {A 1} {A 2}
-
x y {A 1} {A 2} {B .}
D
Correct answer
Explanation
The SAS DATA step uses line-hold specifier @ to hold the current record. For 'A' records, it reads the next line/value y. For the 'B' record, it doesn't execute the inner input for y, causing y to remain missing (.) for that observation. The output contains observations for A (1), A (2), and B (.).
C
Correct answer
Explanation
An unsigned char can only hold values from 0 to 255 (8 bits, 2^8 - 1). When you assign 256 (0xFF + 1), it wraps around to 0 due to unsigned integer overflow. This is standard behavior in C for unsigned types - they implement modular arithmetic.
-
10
-
1000
-
error
-
none of this
C
Correct answer
Explanation
The ternary operator attempts to declare variables directly in its branches: (int a = 100) and (int a = 1000). In C, variable declarations are statements, not expressions, and cannot appear inside a ternary operator. The scope issue is also problematic - even if allowed, 'a' would not be visible in the printf statement. This code will not compile and produces an error.
-
error
-
25
-
will not complie
-
some memory address
B
Correct answer
Explanation
In C, a void pointer (void*) can be implicitly assigned to any other pointer type without explicit casting. This is a special property of void pointers. After p1=p2 and p2=p1, both pointers still point to i, which holds value 25. The printf correctly outputs 25. The 'will not compile' distractor is incorrect because void* to other pointer assignment is valid in C.
-
error
-
arbitary value
-
612
-
100
-
I hate you
-
I love your
-
error
-
will not compile
A
Correct answer
Explanation
The line if(me = = you) is problematic. In C, the assignment operator is '=' not ' = '. The 'me = = you' appears to be a typo for 'me == you'. Assuming it means 'me == you': me is float (1.1) and you is double (1.1). Due to floating point representation differences, they may not compare exactly equal. Float and double have different precision. Typically 1.1 as float != 1.1 as double in binary representation. So the condition is false, and 'I hate you' prints. Also note that even if it was 'me == you', the comparison would be false due to precision differences.
-
50
-
10
-
error
-
none of these
A
Correct answer
Explanation
The preprocessor macro is redefined inside the main function scope. The compiler uses the latest definition of the macro 'a' when compiling the printf statement, resulting in the value 50 being printed instead of the initial value of 10.
-
400 300
-
300 400
-
junk value
-
error
A
Correct answer
Explanation
In Turbo C, local variables are allocated on the stack. Calling printf with format specifiers but omitting variables causes it to retrieve the values directly from the stack locations where variables i and j reside, printing 400 300 rather than throwing an error.
-
GOOD
-
BAD
-
ERROR
-
NONE OF THESE
C
Correct answer
Explanation
In a switch statement, case labels must be constant expressions, not variables. The code uses 'case i:' and 'case j:' where i and j are variables (int i=1, j=2). This is invalid syntax - case labels cannot be runtime variable values. The compiler will generate an error. The switch structure also has a semicolon after the closing brace, which is unusual but not an error.
-
junk value
-
100
-
12
-
none of these
B
Correct answer
Explanation
The macro f(g,g2) is defined as g##g2 (token concatenation using ## operator). When called as f(var,12), it concatenates 'var' and '12' to produce 'var12'. The printf becomes printf('%d', var12). Since var12 is declared as int var12=100, it outputs 100. The ## operator performs token pasting during preprocessing, not string concatenation.
-
-1 -1 0 2 0
-
-1 -1 0 2 1
-
0 0 1 3 1
-
0 0 1 3 0
C
Correct answer
Explanation
The expression m=i++&&j++&&k++||l++ uses short-circuit evaluation with post-increment. Starting: i=-1, j=-1, k=0, l=2. First i++ evaluates to -1 (true), then i becomes 0. Since -1 (true) && j++, we evaluate j++: j=-1 evaluates to -1 (true), then j becomes 0. Now we have (-1 && -1) which is 1 (true) && k++. But wait, i++ and j++ both happened. Now: i=0, j=0, k=0. Continuing: true && k++ means evaluate k++: k=0 evaluates to 0 (false), then k becomes 1. Short-circuit! Since k++ was false (0), the entire AND chain becomes false without needing more evaluation. m = (false) || l++, so we evaluate l++: l=2 evaluates to 2 (true), then l becomes 3. m = false || true = 1. Final values: i=0, j=0, k=1, l=3, m=1. This matches option C.
B
Correct answer
Explanation
The for loop is: for( ; i++ ; printf('%d',i)). Initial condition is empty (uses i=0). The condition is i++, which is a post-increment: First iteration: i++ evaluates 0 (current value of i), then i becomes 1. Since 0 is false, the loop condition fails immediately. The printf inside the loop never executes. After loop exit, printf('%d',i) outputs 1 (the value of i after the post-increment in the condition check).