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
-
not same
-
same
-
unknown symbol ‘~’
-
none of the above
B
Correct answer
Explanation
When -1 is assigned to an unsigned int, it wraps around to the maximum value (all bits set to 1). The bitwise NOT of 0 (~0) also produces all bits set to 1. Therefore, both x and y hold the same bit pattern and the comparison 'x == y' evaluates to true, printing 'same'.
B
Correct answer
Explanation
The post-increment operator (a++) passes the current value of 'a' to the function, then increments it afterward. Since 'a' is initialized to 1, the value 1 is passed to f1() and printed. The increment happens after the function call completes.
-
0001 0002 0003
-
compile error
-
0001 0002 0004
-
0001 0002 0006
C
Correct answer
Explanation
Pointer arithmetic advances by the size of the pointed-to type. char* increments by 1 byte, int* by sizeof(int) (2 bytes on typical 16-bit systems), and long* by sizeof(long) (4 bytes on typical 32-bit systems). Starting from 0, the pointers become 1, 2, and 4 respectively.
B
Correct answer
Explanation
The expression ++p++ combines pre-increment (++), dereference (), and post-increment (++). Pre-increment has highest precedence: ++*p means increment the value p points to ('a' becomes 'b'). Then p++ advances the pointer. So c gets 'b'.
-
unknown symbol ‘a1’
-
compile error
-
10
-
20
D
Correct answer
Explanation
In a union, all members share the same memory location. The assignment a1.b=20 overwrites the value stored at a1.a=10. Reading a1.a after a1.b returns 20. The variable name 'a' shadows the union tag, but this doesn't affect execution.
-
int main()
-
int main(int argc, char *argv[])
-
They both work
-
None of the Above
C
Correct answer
Explanation
Both int main() and int main(int argc, char *argv[]) are valid main function declarations in C++. The first is for programs without command-line arguments, the second accepts command-line arguments. Both work correctly.
-
int main()
-
int main(int argc, char *argv[])
-
They both work
-
None of the Above
-
1
-
0
-
Code cannot compile
-
None of the Above
B
Correct answer
Explanation
The post-increment operator m++ returns the current value of m (0) then increments it. Since this return value is used for the function's return, v() returns 0. The increment happens but the incremented value is never used.
-
class aClass{public:int x;};
-
/* A comment */
-
char x=12;
-
None of the Above
A
Correct answer
Explanation
C does not support classes or the 'class' keyword - these are C++ OOP features. Options B and C are valid C syntax (comments and char initialization). Option A uses C++ class syntax which is invalid in C.
-
ab
-
abc
-
c
-
none of the above
A
Correct answer
Explanation
In the loop initialization, printf("a") executes and prints 'a'. Next, the condition printf("b") executes, prints 'b', and returns a non-zero value (true). The loop body then runs, executing break immediately. The loop terminates before the increment step, resulting in 'ab'.
D
Correct answer
Explanation
The output depends on argument evaluation order in printf. Starting i=3, with unspecified evaluation order in C/C++, the output can vary. The claimed answer 443 occurs on specific implementations. Note: this is undefined behavior in C/C++ standards.
-
This code will hang
-
This code will print B.
-
This code will print A.
-
Compilation Error
B
Correct answer
Explanation
Pointer p is assigned address of i. Then p++ increments p by one integer (4 bytes on most systems). The expression (p - &i) calculates the offset between the new pointer address and original address, which is 1 integer - not 1 byte. The difference is measured in the pointer's data type units, so it equals 1.
B
Correct answer
Explanation
A long pointer on most systems is 8 bytes (or 4, depending on architecture), while int is 4 bytes. When you assign &i (int pointer) to long* pointer p, there's a type mismatch. After p++, the difference p - &i is calculated as pointer arithmetic: p advances by sizeof(long) bytes, &i is the original address. The difference is the number of long-sized steps between them, which is 1.