Output of C programs | Set 66 (Accessing Memory Locations)
Q1. Is the output of this code True or False?
A. True B. False C. Program would crash D. Compilation error
The binary form of 20 is 10100 which is stored in the memory. Since it is of type int, four bytes are allocated and 10100 is stored in the 0th position. We access elements of an array from left to right. But, in the memory, the bits of memory (not at all arrays) are accessed from right to left. The ASCII value is stored in another block of 1 byte having just a 0th position. When we equate y to z, we are overwriting the value of b indirectly. This makes both the pointers equal.
Q2. What is the output of this program?
A. SDKJNSDNSKDJNSKDVNSKJD B. SLKFVMSLFKVSFLALKDJF C. ABCDEFGHIJKLMNOPQRSTUVWXYZ D. NONEOFTHEABOVE
The space allotted for a character is just 1 byte or 8 bits. y is being allocated during run time. It contains the address of x. Now, we can index the memory location of x. And, any change to y reflects in x also. When y+=1 is executed, in each loop, the last bit in that 1 byte of data is being incremented by 1 like:
Initial: 00001010 (Binary form of 'A') In loop 00001010 + 1 = 00001011 = B 00001011 + 1 = 00001100 = C 00001100 + 1 = 00001101 = D and goes on up till Z.
Note: Trying to index y and any other location gives a junk value.
Q3. What gets printed at the end of this program?
A. 20 20 B. 20 10 C. 10 10 D. 10 20
D. 10 20
Here, just the 0th bit of a and b are being changed, leading to the change in the values.
Q4. Is the value of a at the end of the program same as the beginning?
A. Yes B. No C. Program would crash D. Compilation error
The binary value of 100 is 01100100, 132 is 10000100 and 3 is 00000011. Now this is what happens
b b b b 00000000 00000000 00000000 01100100 +00000000 00000000 00000011 10000100 ------------------------------------ 00000000 00000000 00000011 11101000 This gives 1000.
Now again the values are replaced. 232-32 is 200, and this divided by 2 gives 100, which is replaced at b. So, 11101000 becomes 01100100. b*=0 makes all the 8 bits of b=0. This gives the output of a as 100, which was same as in the beginning.
Q5. What happens to this program?
A. Compilation error B. Segmentation Fault C. Output would be returned D. None of the above
B. Segmentation Fault
The program would crash because y points to 0x100, which is the address of some other program running.
Q6. What is the output of this program (Consider address of x as 62fe14)?
A. 0x0010 0x1902 B. Crash C. 0x62fe14 0x62fe15 D. 0x62fe14 0x62fe14
D. 0x62fe14 0x62fe14
y already contains 0x62fe14 (address of x). And *y gives the address of x, i.e., 0x62fe14. Performing y=(char *)0x62fe14 replaces the value but is of no personal use. If you change anything in 0x62fe14 not only effects y but also changes the address of a as y has the address of x, which is stored in the 0th position.
Q7. What is the output of this program?
A. a B. b C. ab D. No output
Initializing x to y changes the address held of x. Basically, murdering the address of B1.a1 and replacing the value in x with the value of that in y which is 11. *z = x is of no use as it already contains the address of x. Printing the hexadecimal value of z gives b (hexadecimal of 11).
Q8. What is printed?
A. Karnataka B. karnataka C. Kerala D. kerala
The integer value of 01001011 is 75, the ASCII value of ‘K’. The integer value of 010110 is 22 and that of 11110110 is -10. Since there is no exceeding of 32 bits (4 bytes), b contains the entire number. Adding each value of n, replaces the value in b, leading to print each character (because of format specifier).
Q9. How does the output look like in this program?
A. decimal value of 1011001100110011 decimal value of 1111000011110011 decimal value of 0111000011010011 decimal value of 0011000000001001 B. Junk value Junk value Junk value decimal value of 0011000000001001 C. Junk value Junk value Junk value Junk value D. Junk value Junk value decimal value of 0111000011010011 decimal value of 0011000000001001
D. Junk value Junk value decimal value of 0111000011010011 decimal value of 0011000000001001
Size of short int is 2 bytes which is equal to 16 bits. a takes only 32 bits out of these 64 bits. And, b stores 2 bytes and b stores 2 bytes. That is the reason you get the output with two junk values and two decimal values.
Q10. What is the output of this program?
A. 0 0 0 0 0 0 0 64 B. 0 0 0 0 0 0 0 0 C. All junk values D. 0 0 0 0 0 0 0 A
A. 0 0 0 0 0 0 0 64
Size of this union is 8 bytes or 64 bits. The latest value put into the union is 100, which replaces the first position that had 10 initially. The hexadecimal value of 100 is 64, leading to this output. 7 bytes as 0 and 1 byte as 64.