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
The loop continues while (x-1) is non-zero. x starts at 3: (3-1)=2 true → counter=1, x=2. Next: (2-1)=1 true → counter=2, x=1. Next: (1-1)=0 false → exit. Final counter=2. This is correct.
A
Correct answer
Explanation
ceil(-3.5) rounds UP to -3 (less negative), ceil(3.5) rounds up to 4. floor(-3.5) rounds DOWN to -4 (more negative), floor(3.5) rounds down to 3. The printf statements output: -3 : 4 (from ceils) then -4 : 3 (from floors).
C
Correct answer
Explanation
x starts at 5. Since op='*' doesn't match any case, switch goes to default: x becomes 6. Without break, execution falls through to case '+': x += 2 makes x=8, then case '-': x -= 2 makes x=6. Fallthrough continues through all cases after the matching one.
-
There will be a memory overwrite.
-
There will be a memory leak.
-
There will be a segmentation fault.
-
Not enough space is allocated by the malloc.
-
It will not compile.
B
Correct answer
Explanation
Initially, ptr2 points to dynamically allocated memory from malloc(5). The next statement reassigns ptr2 to point to ptr1, causing the reference to the original heap allocation to be lost without being freed, resulting in a classic memory leak.
C
Correct answer
Explanation
This is the correct answer. The operator ( >>) is a right shift operator. The general formula to solve (N >> s) is: N/2^s. Here, 128>>4 will be solved first which is 128/(2^4) = 8. So, the statement now becomes 8>>3>>2. Now 8/(2^3) will be solved which will return (8/8) = 1. Now again the statement becomes 1>>2. So, 1/(2^2) will be solved resulting in 0. The correct output is zero.
-
65536
-
1024
-
0
-
512
-
Error at line 1
B
Correct answer
Explanation
Two kinds of operators are used in the statement: left shift (<<) and right shift (>>). The general formula to solve (N << s) is: N * 2^s, and the general formula to solve (N >> s) is: N/2^s. So, firstly 128<<4<<3<<2 will be solved which will return 128 * (2^4) * (2^3) * (2^2) = 65536. Now the statement becomes 65536 >> 6. So, it will return 65536/2^6 = 1024. The output will be 1024. So, this is the correct answer.
C
Correct answer
Explanation
This is the correct answer. The compiler will first reverse the bits of x and y as we have used ~ (tilde) operator on them. So, the expression becomes: -24 | -43. Now, the compiler will convert -25 and -43 into binary and perform OR operation on them. So, the answer will be -9.
B
Correct answer
Explanation
This is the correct answer. The operators used here are all bitwise operators which operate on binary bits. So, the value of x in binary is 00011000, the value of Y in binary is 00101010 and the value of Z in binary is 00100101. The value of variable "a" in binary is 00010110. The expression becomes [ 000011000 & 00101010 | 00100101 ^ 00010110 ]. So, we will evaluate 24 and 42 | 37 ^ 22, which results in 59.
D
Correct answer
Explanation
The compiler will first convert all of the given values in binary form.
So, value of x in binary is 00011000, value of Y in binary is 00101010, value of Z in binary is 00100101 and value of "a" in binary is 00010110. Now, OR operation will be performed on all of them to obtain a single result: (00011000) | (00101010) | (00100101) | (00010110). It will result in 63. So, this is the correct answer.
C
Correct answer
Explanation
This is the correct answer because we have used %d access specifier in printf() function that is used to print the integer values, but pow() function returns a double value. So, we have to use %lf to print the original value. Zero will get printed on the screen.
E
Correct answer
Explanation
This is the correct answer because ~ (tilde) operator is unary operator. So, it can be applied only on a single operand. But here we are using it on two operands (x~a). So, it will generate an error.
-
HelloByeGoodBad
-
ByeGoodBad
-
GoodBad
-
Bad
-
Compiler error
E
Correct answer
Explanation
There is an error in the code because we have put a semicolon after the switch() statement which is invalid. The compiler will raise an error “case label not with a switch statement”. So, this answer is correct.
-
0 GoodBad
-
8ByeGoodBad
-
14HelloByeGoodBad
-
16
-
Compiler error
E
Correct answer
Explanation
This is the correct choice because the last two cases: “case 0/9” and “case 0/1” both will evaluate to 0. But we cannot have two duplicate case values, so the compiler will raise an error.
-
32
-
20
-
16
-
Blank Output Screen
-
Compiler error
D
Correct answer
Explanation
This is the correct choice because the correct answer is 20, but it will not be printed on the screen because we have used specifier %h in the printf() function which is not recognised by the compiler. To successfully print the output in hexadecimal form on the screen we must use %hx. So, blank output will be there on the screen.
-
The result comes out to be: 0111.
-
The result comes out to be: 1111.
-
The result comes out to be is: 1000.
-
The result comes out to be is: 0110.
-
Compiler error
B
Correct answer
Explanation
This program will just OR the binary values of 7 and 15. So, after ORing them, the result comes out to be 15 in binary which is 1111. So, this option is correct.