Class 7 Exam  >  Class 7 Questions  >  What will be the output of the following C++ ... Start Learning for Free
What will be the output of the following C++ code?
    #include <iostream>
    using namespace std;
    void swap(int &a, int &b);
    int main()
    {
        int a = 5, b = 10;
        swap(a, b);
        cout << "In main " << a << b;
        return 0;
    }
    void swap(int &a, int &b)
    {
        int temp;
        temp = a;
        a = b;
        b = temp;
        cout << "In swap " << a << b;
    }
  • a)
    In swap 105 In main 105
  • b)
    In swap 105 In main 510
  • c)
    In swap 510 In main 105
  • d)
    In swap 510 In main 510
Correct answer is option 'A'. Can you explain this answer?
Most Upvoted Answer
What will be the output of the following C++ code? #include <iostr...
Explanation:

Main Function:
- Initialize two integers, a=5 and b=10.
- Call the swap function passing the values of a and b.
- Print "In main" followed by the values of a and b which were swapped in the swap function.

Swap Function:
- Declare a temporary variable temp.
- Assign the value of a to temp.
- Assign the value of b to a.
- Assign the value of temp to b.
- Print "In swap" followed by the values of a and b after swapping.
Therefore, the output will be:
In swap 10 5
In main 10 5
Free Test
Community Answer
What will be the output of the following C++ code? #include <iostr...
 As the function is called by reference i.e. all the changes are done directly into the memories of a and b. Therefore changes made to a and b in swap function is reflected back to main function. Hence the values of a and b in swap as well as in main function is changed.
Output:
$ g++ ref.cpp
$ a.out
In swap 105 In main 105
Explore Courses for Class 7 exam
Question Description
What will be the output of the following C++ code? #include <iostream> using namespace std; void swap(int &a, int &b); int main() { int a = 5, b = 10; swap(a, b); cout << "In main " << a << b; return 0; } void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; cout << "In swap " << a << b; }a)In swap 105 In main 105b)In swap 105 In main 510c)In swap 510 In main 105d)In swap 510 In main 510Correct answer is option 'A'. Can you explain this answer? for Class 7 2025 is part of Class 7 preparation. The Question and answers have been prepared according to the Class 7 exam syllabus. Information about What will be the output of the following C++ code? #include <iostream> using namespace std; void swap(int &a, int &b); int main() { int a = 5, b = 10; swap(a, b); cout << "In main " << a << b; return 0; } void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; cout << "In swap " << a << b; }a)In swap 105 In main 105b)In swap 105 In main 510c)In swap 510 In main 105d)In swap 510 In main 510Correct answer is option 'A'. Can you explain this answer? covers all topics & solutions for Class 7 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What will be the output of the following C++ code? #include <iostream> using namespace std; void swap(int &a, int &b); int main() { int a = 5, b = 10; swap(a, b); cout << "In main " << a << b; return 0; } void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; cout << "In swap " << a << b; }a)In swap 105 In main 105b)In swap 105 In main 510c)In swap 510 In main 105d)In swap 510 In main 510Correct answer is option 'A'. Can you explain this answer?.
Solutions for What will be the output of the following C++ code? #include <iostream> using namespace std; void swap(int &a, int &b); int main() { int a = 5, b = 10; swap(a, b); cout << "In main " << a << b; return 0; } void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; cout << "In swap " << a << b; }a)In swap 105 In main 105b)In swap 105 In main 510c)In swap 510 In main 105d)In swap 510 In main 510Correct answer is option 'A'. Can you explain this answer? in English & in Hindi are available as part of our courses for Class 7. Download more important topics, notes, lectures and mock test series for Class 7 Exam by signing up for free.
Here you can find the meaning of What will be the output of the following C++ code? #include <iostream> using namespace std; void swap(int &a, int &b); int main() { int a = 5, b = 10; swap(a, b); cout << "In main " << a << b; return 0; } void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; cout << "In swap " << a << b; }a)In swap 105 In main 105b)In swap 105 In main 510c)In swap 510 In main 105d)In swap 510 In main 510Correct answer is option 'A'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What will be the output of the following C++ code? #include <iostream> using namespace std; void swap(int &a, int &b); int main() { int a = 5, b = 10; swap(a, b); cout << "In main " << a << b; return 0; } void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; cout << "In swap " << a << b; }a)In swap 105 In main 105b)In swap 105 In main 510c)In swap 510 In main 105d)In swap 510 In main 510Correct answer is option 'A'. Can you explain this answer?, a detailed solution for What will be the output of the following C++ code? #include <iostream> using namespace std; void swap(int &a, int &b); int main() { int a = 5, b = 10; swap(a, b); cout << "In main " << a << b; return 0; } void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; cout << "In swap " << a << b; }a)In swap 105 In main 105b)In swap 105 In main 510c)In swap 510 In main 105d)In swap 510 In main 510Correct answer is option 'A'. Can you explain this answer? has been provided alongside types of What will be the output of the following C++ code? #include <iostream> using namespace std; void swap(int &a, int &b); int main() { int a = 5, b = 10; swap(a, b); cout << "In main " << a << b; return 0; } void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; cout << "In swap " << a << b; }a)In swap 105 In main 105b)In swap 105 In main 510c)In swap 510 In main 105d)In swap 510 In main 510Correct answer is option 'A'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What will be the output of the following C++ code? #include <iostream> using namespace std; void swap(int &a, int &b); int main() { int a = 5, b = 10; swap(a, b); cout << "In main " << a << b; return 0; } void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; cout << "In swap " << a << b; }a)In swap 105 In main 105b)In swap 105 In main 510c)In swap 510 In main 105d)In swap 510 In main 510Correct answer is option 'A'. Can you explain this answer? tests, examples and also practice Class 7 tests.
Explore Courses for Class 7 exam

Top Courses for Class 7

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev