Strings

Q1: What will be the output of the program ?
#include<stdio.h>

int main()
{
    char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"};
    int i;
    char *t;
    t = names[3];
    names[3] = names[4];
    names[4] = t;
    for(i=0; i<=4; i++)
        printf("%s,", names[i]);
    return 0;
}

A Suresh, Siva, Sona, Baiju, Ritu

B Suresh, Siva, Sona, Ritu, Baiju

C Suresh, Siva, Baiju, Sona, Ritu

D Suresh, Siva, Ritu, Sona, Baiju

ANS:B - Suresh, Siva, Sona, Ritu, Baiju

Step 1: char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"}; The variable names is declared as an pointer to a array of strings. Step 2: int i; The variable i is declared as an integer type. Step 3: char *t; The variable t is declared as pointer to a string. Step 4: t = names[3]; names[3] = names[4]; names[4] = t; These statements the swaps the 4 and 5 element of the array names. Step 5: for(i=0; i<=4; i++) printf("%s,", names[i]); These statement prints the all the value of the array names. Hence the output of the program is "Suresh, Siva, Sona, Ritu, Baiju".



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.