Both 12 and 13 please A string array is defined as: char *planets[] = {\"Mercury
ID: 3810804 • Letter: B
Question
Both 12 and 13 please A string array is defined as: char *planets[] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"); What's the value of planets [2] [2]? a) "Venus" b) "Jupiter" c) "Earth" d) 'r' e) 'a' Which one of the following implementation of the swap() function would exchange the variables of the variables successfully? When passed the addresses of two variables i and j, swap (&i;, &j;); will exchange values of i and j. a) void swap (int *p, int q) {int tmp; *p = *q; *q = temp; b) void swap (int *p, int *q) {int *tmp; tmp = p; p = q; q = tmp;} c) void swap (int *p, int *q) {int *tmp; tmp = p; q = p; p = tmp;} d) void swap (int *p, int *q) {int tmp; tmp = *p; *q = *p; *p = tmp;}Explanation / Answer
(12).
Here, planets is an array of string pointers so its value will be strings. The starting index of an array is 0.
planets[0] = "Mercury", planets[1] = "Venus" ... and so on.
So planets[2] = "Earth".
We are supposed to find value of planets[2][2].
Here, planets[2] is also a string which means a character array so we can iterate over it also. planets[2][0] means the first character of "Earth" which means "E". So, planets[2][2] means 3rd character of "Earth" which is 'r'.
So the answer is (d) 'r'.
(13). The answer is option (a).
i and j are passed using address using & so to change their values, we need to change the values pointed by that pointers, not the values of pointers. So the modification is done in *p, not in p as changing in p value means changing its address. So, option (c) and (b) are not valid for this swap operation.
So, we need to find correct option from (a) and (d).
The basic logic of swap is using a temp variable. Storing the first value in temp, storing the second variable in the first, and then that previously stored value in the second variable does the thing.
It is like : temp = a, a = b, b = temp.
Consider here a as *p and b as *q. So option (a) is having the similar sequence.
Option (d) says something like : temp = a, b = a, a = temp. But here, b is stored value of a. Which is right, but the variable a(In this code *p) is assigned its original value again and is not swapped. So it is not valid option.
Please comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.