Ask Question

A code segment will be used to swap the values of the variables a and b using the temporary variable temp. Which of the following code segments correctly swaps the values of a and b?

a) a to b, temp to a, b to temp

b) temp to a, a to b, b to temp

c) temp to a, a to temp, a to b

d) temp to a, b to temp, a to b

+4
Answers (1)
  1. 28 July, 20:34
    0
    Option (b)

    Explanation:

    These are the right statements but the correct order would be:

    temp=b;

    b=a;

    a=temp;

    Value of b will be stored in temp variables and then value of a will be stored in b and afterwards value of temp variable to a. This is how value of a &b will be swapped. Swapping is needed in various codes many times. For example - while sorting an array, while reversing a string, etc.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “A code segment will be used to swap the values of the variables a and b using the temporary variable temp. Which of the following code ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers