GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 62

Consider the following C program:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
#include <string.h>
  
void geek1(char *s1, char *s2) {
    char *temp;
    temp = s1;
    s1 = s2;
    s2 = temp;
}
void geek2(char **s1, char **s2) {
    char *temp;
    temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}
int main() {
    char *str1 = "for geek", *str2 = "by geek";
    geek1(str1, str2);
    printf("%s %s", str1, str2);
    geek2(&str1, &str2);
    printf(" %d", (int) strlen(str1));
    return 0;
}

chevron_right


Last printf will printed _______________ .
(A) 2
(B) 7
(C) 3
(D) None of these


Answer: (B)

Explanation: First function geek1 will not swap strings, so first print is for geek by geek.
Now, function geek2 will swap string, and strlen(str1) will return lenght of string “by geek”, which is 7.

Option (B) is correct.

Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.