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
A
Correct answer
Explanation
The nested ternary evaluates as: !a ? (b ? !c : !d) : a. Since a=1, !a=0 (false), so we take the rightmost branch and return a, which is 1. The nested ternarys on the left side (!c and !d) are never evaluated due to short-circuiting.
-
0 -200 300 10 4
-
300 -200 300 10 4
-
-200 300 10 4 0
-
100 -200 300 4 10
B
Correct answer
Explanation
The compound assignment evaluates right-to-left: e%=5 gives e=4, then d/=e gives d=10, then c*=d gives c=300, then b-=c gives b=-200, finally a+=b gives a=300. The final values are: a=300, b=-200, c=300, d=10, e=4.
-
20 20
-
Garbage Value 20
-
0 20
-
Linker Error
D
Correct answer
Explanation
The variable i is declared as extern but never defined anywhere in the program. This causes a linker error because the linker cannot find the definition of i to satisfy the external declaration. The inner block's int i=20 is a separate local variable and doesn't resolve the extern.
-
Program Hangs
-
No Output
-
6 4 2
-
5 2
C
Correct answer
Explanation
The for loop initializes i=5, then checks ++i which makes i=6 (true, so loop runs), prints 6. Then i-=3 makes i=3. Check ++i: i=4 (true), prints 4. Then i-=3 makes i=1. Check ++i: i=2 (true), prints 2. Then i-=3 makes i=-1. Check ++i: i=0 (false), loop exits. Output: 6 4 2.
-
1234 1234
-
1235 1235
-
1234 1235
-
1235 1234
A
Correct answer
Explanation
The printf statement has *ptr++ as the second argument. The post-increment operator means the current value (*ptr=1234) is used for printf, then ptr is incremented. So printf receives val=1234 and *ptr=1234, printing "1234 1234". The pointer increment happens after the value is fetched.
-
Compile-Time Error
-
Testing...Done
-
Testing...Done
-
None of these
B
Correct answer
Explanation
The line func; is a statement that evaluates the function name but doesn't call it - it's essentially a no-op that gets discarded. Then func(); is called normally and prints "Testing...Done". The first func; is valid but useless, not an error.
-
Compile-Time Error
-
0.0 0.0 0.0 0.0 0.0
-
0.0 0.1 0.2 0.3 0.4
-
1.0 1.0 1.0 1.0 1.0
-
100
-
Linker Error
-
Run Time Error
-
Compiler Error
A
Correct answer
Explanation
The underscore _ is a valid variable name in C. The code declares int _, assigns 100 to it, then prints it. This is perfectly valid C code and outputs 100. Underscore is a legal identifier character.
-
Outputs 12
-
Outputs 10
-
Outputs the address of v
-
No output
B
Correct answer
Explanation
The function afunction receives a pointer to v but immediately reassigns x to point to newly allocated memory (x=new int). This does NOT change the original pointer in main - it only changes the local copy x. The *x=12 assigns to the new memory, not to v. When printf prints v in main, v still has its original value 10. The new int is leaked (memory not freed).
-
Compile-Time Error
-
Default1Case1
-
Default2
-
Default1
A
Correct answer
Explanation
A switch statement cannot have more than one default case. This code has two default labels (at the beginning and end), which violates C syntax rules. The compiler will report an error for duplicate default cases. The presence of the second default makes this code invalid.
-
25
-
11
-
Error
-
Garbage Value
B
Correct answer
Explanation
The macro SQR(x) expands to x*x. When called as SQR(b+2), it becomes b+2*b+2, NOT (b+2)*(b+2). Due to operator precedence, multiplication happens first: 3+2*3+2 = 3+6+2 = 11. The macro lacks parentheses around x and around the entire expression, causing this common pitfall.
-
100
-
200
-
Compiler Error
-
None of the above
-
A
-
B
-
All the above
-
None of the above
D
Correct answer
Explanation
The switch statement has choice=4. There is no case 4, so execution goes to default. The default prints "A" but has NO break statement. Without break, execution falls through to case 1, which prints "B". The output is "AB". Looking at the options, none of A, B, C ("All the above") match "AB". Therefore D (None of the above) is correct.
-
An error
-
Unpredictable
-
567
-
0
B
Correct answer
Explanation
The variable k is declared but never initialized. In C++, reading an uninitialized local variable results in undefined behavior - the output will be whatever garbage value happens to be in that memory location. This is unpredictable and can vary between runs. Option B (Unpredictable) correctly identifies this behavior. Note that the header has a typo: 'iosteam.h' should be 'iostream.h', but the conceptual question about uninitialized variables is still valid.
A
Correct answer
Explanation
The prefix increment operator ++a increments a BEFORE using its value. Since a is 5, ++a makes it 6 and then returns 6 to cout. The output is 6. Option A is correct. The difference between ++a (prefix) and a++ (postfix) is that prefix increments first, then returns the new value, while postfix returns the old value first, then increments.