Output of C programs | Set 38
Ques1. What is the output of the following ?
#include <stdio.h> void main() { int a[5] = { 5, 1, 15, 20, 25 }; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf ( "%d, %d, %d" , i, j, m); } |
Options :
A. 3, 2, 15
B. 2, 3, 20
C. 2, 1, 15
D. 1, 2, 5
Answer : A
Explanation : >> int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25.
>> int i, j, m; The variable i, j, m are declared as an integer type.
>> i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
>> j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
>> m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
>> printf(“%d, %d, %d”, i, j, m); It prints the value of the variables i, j, m
Ques2. What is the output of the following ?
void main() { int arr[10] = { 1, 2, 3, 4, 5 }; printf ( "%d" , arr[5]); } |
Options :
A. Garbage Value
B. 5
C. 6
D. 0
Answer : D
Explanation : When an array is partially initialized at the time of declaration then the remaining elements of the array is initialized to 0 by default.
Ques3. What is the output of the following?
Assume that the array begins at 65472 and each integer occupies 2 bytes
void main() { int a[3][4] = { 1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0 }; printf ( "%u, %u" , a + 1, &a + 1); } |
Options :
A. 65474, 65488
B. 65480, 65488
C. 65480, 65496
D. 65474, 65476
Answer : C
Explanation : The base address(also the address of the first element) of array is 65472.
For a two-dimensional array like a reference to array has type “pointer to array of 4 ints”. Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480
Then, &a has type “pointer to array of 3 arrays of 4 ints”, totally 12 ints. Therefore, &a+1 denotes “12 ints * 2 bytes * 1 = 24 bytes”.
Hence, beginning address 65472 + 24 = 65496. So, &a+1 = 65496
Ques4. What is the output of the following ?
#include <stdio.h> void main() { printf (6 + "Geeks for Geeks" ); } |
Options :
A. for Geeks
B. Geeks
C. compile time error
D. no output
Answer : A
Explanation : It skips the 6 characters and prints the given string.
Ques5. What is the output of the following ?
#include <stdio.h> void main() { char * s = "hello" ; char * p = s; printf ( "%p\t%p" , p, s); } |
Options :
A. Different address is printed
B. Same address is printed
C. Run time error
D. Nothing
Answer : B
Explanation : *s will hold the value “hello”and that string is assigned to *p which will start pointing to same physical location.
This article is contributed by Rishabh jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...