All Exams Test series for 1 year @ ₹349 only
Question

Consider the following code :

#include<stdio.h>

void f1(char *x, char *y) {

    char *t1; t1=x; x=y; y=t1;}

void f2(char **x, char **y) {

    char *t1; t1=*x; *x=*y; *y=t1;}

int main()

{

    char *a="ONE", *b="TWO";

    f1(a, b); printf("%s %s", a, b);

    f2(&a, &b); printf("%s %s", a, b);

    return 0;

}

What will be the output of the above code?

This question was previously asked in
UGC NET 2024 Computer Science and Applications Question Paper (23-Aug-2024) (Shift 1)
The correct answer is

ONE TWO TWO ONE

C passes every argument by value. Passing a char * copies the address, so swapping the copies inside a function does not affect the caller; passing the address of the pointer (char **) lets the function change the caller's pointers themselves.

Step 1 — f1(a, b): x and y are local copies of a and b. Swapping x and y leaves a = "ONE" and b = "TWO", so the first printf prints ONE TWO.

Step 2 — f2(&a, &b): x points to a and y points to b. *x = *y and *y = t1 exchange the actual pointers, so now a = "TWO" and b = "ONE"; the second printf prints TWO ONE.

Step 3 — The combined output is ONE TWO followed by TWO ONE (no separator is printed between the two calls).

TWO ONE ONE TWO and TWO ONE TWO ONE wrongly assume f1 swaps the caller's strings; ONE TWO ONE TWO wrongly assumes f2 also has no effect.

ONE TWO TWO ONE option is correct.

Was this answer helpful?

Similar Questions

  1. What does the following expression means ?

    char *(*(* a[N]) ( )) ( );

  2. What is the output of the following program ?

    (Assume that the appropriate preprocessor directives are included and there is no syntax error)

    main ( )

        {    char S[ ]="ABCDEFGH";

                printf ("%C", * (& S[3]));

                printf ("%s", S+4);

                printf ("%u", S);

        /* Base address of S is 1000 */

        }

  3. #include<stdio.h>

    void main()

    {

        int arr[]={1, 2, 3, 4, 5};

        int *p=arr;

        printf("%d", *p++);

        printf("%d", *(p+1));

    }

    Find the output of the above code?

  4. Consider following C program :

    #include<stdio.h>

    int main()

    {

        int x[]={2, 4, 6, 8, 10};

        int a, b=0, *y=x+4;

        for(a=0; a<5; a++)

        {

            b=b+(*y-a)-*(y-a);

        }

            printf("%d\n", b);

            return 0;

    }

    What will be the output of the above C program ?

  5. Which of the following is correct way to declare a functional pointer in C ?

  6. What will be the output of the following C code?

    #include<stdio.h>

    void main()

    {

        int arr[5] = {10, 20, 30, 40, 50};

        int *p = (int*) (&arr + 1);

        printf("%d%d", *(arr + 1), *(p - 1));

    }

  7. Which of the following statements about pointers in C are TRUE.

    (A) Pointers can be used to access array elements

    (B) Pointers can store the address of another pointer

    (C) Pointers are automatically deferenced in expression

    (D) Pointers cannot be used to access structure members

    Choose the correct answer from the options given below :


Important Questions from Pointer

  1. What is the output of the following C programming language code? 

    #include<stdio.h>

     int main()

    { 

    int x = 10;

     int *p = &x;

     *p = *p + 5;

     printf("%d %d", x, *p); 

    return 0; 

    }

  2. The following statement in ‘C’
    int (*f())[ ];
    declares
  3. Consider the following ANSI-C program.

    #include <stdio.h>
    int main(){
     int *ptr, a, b, c;
     a=5; b=11; c=20;
     ptr=&a; *ptr=c; ptr=&c;
     a=*(&b); c=*ptr-a;
     printf("%d",c);
     return(0);
    }

    The output of this program is ____________. (answer in integer)

    Note: Assume that the program compiles and runs successfully.

  4. Consider the following C program:
    #include <stdio.h>
    void stringcopy(char *, char *);
    int main(){
    char a[30] = "@#Hello World!";
    stringcopy(a, a + 2);
    printf("%s\n", a);
    return 0;
    }
    void stringcopy(char *s, char *t) {
    while(*t)
    *s++ = *t++;
    }
    Which ONE of the following will be the output of the program?

  5. Consider the following C program:
    #include <stdio.h>
    int main(){
    int a;
    int arr[5] = {30,50,10};
    int *ptr;
    ptr = & arr[0] + 1;
    a = *ptr;
    (*ptr)++;
    ptr++;
    printf("%d", a + (*ptr) + arr[1]);
    return 0;
    }
    The output of the above program is ____________ (Answer in integer)

Need Expert Advice?
Upcoming Exams
CTET
December 12, 2026
WB SET
December 13, 2026
CBSE UGC NET
December 14, 2026
CSIR UGC NET
December 19, 2026
Test Series
UGC NET img
Teaching
UGC NET (Paper 2) December 2026 Mock Test Series
799 Tests 2 Tests Free
300 Attempts
4.4(45)
English, Hindi
More Questions from UGC NET

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App