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
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
A
Correct answer
Explanation
The function signature void abc(int a[]) decays the parameter to a pointer, so a++ increments the local pointer. However, in main(), a is declared as char a[5], and passing it to abc expects an int pointer (int*), causing a type mismatch error or compilation issue in strict C compilation contexts. Additionally, type mismatch between char array and int function parameter triggers a compilation error.
-
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).
B
Correct answer
Explanation
In C, -1 is represented as all 1s in two's complement (0xFFFFFFFF for 32-bit or 0xFFFF for 16-bit). Left shifting by 4 positions multiplies by 16, giving 0xFFFFFFF0 (32-bit) or 0xFFF0 (16-bit). On systems where the output is 4 hex digits, this prints as fff0. Option A (ffff) would be the result without shifting, while options C and D are incorrect bit patterns.
-
5 6 6 5 5
-
6 5 6 5 5
-
5 5 6 5 5
-
4 5 5 4 5
D
Correct answer
Explanation
In C, the order of evaluation of printf arguments is unspecified. However, the values are computed right-to-left before printing. Starting from i=5: i++ makes 6, i-- makes 4, ++i makes 5, --i makes 4, and i is 4. But compilers may vary. On systems producing 4 5 5 4 5, this matches option D. This tests understanding of increment/decrement operators and evaluation order.
-
6
-
junk value
-
some address
-
error
D
Correct answer
Explanation
The code declares 'p' as a pointer to a const int (int const *p), meaning the value pointed to cannot be modified. The line printf("%d",++(*p)) attempts to increment the value through the pointer, which violates the const qualifier. This results in a compilation error. Option D is correct.