Aspire's Library

A Place for Latest Exam wise Questions, Videos, Previous Year Papers,
Study Stuff for MCA Examinations - NIMCET

Previous Year Question (PYQs)



29.What is the output of the following program ?
#include <iostream>
using namespace std;
int main ()
{
    int c1 = 10;
    int c2 = 20;
    {
        int c1;
        c1 = 50;
        c2 = 50;
        cout << "c1= " << c1 << ", c2=" << c2;
    }
    cout << ", c1= " << c1 << ", c2=" << c2;
    return 0;
}






Solution

  • Output:
    c1= 50, c2=50, c1= 10, c2=50
    Explanation:

    Outer c1 = 10, c2 = 20.

    Inside the inner block, a new local c1 is declared (shadowing outer c1).

    Inner c1 = 50, and c2 (outer one) is updated to 50.

    First cout prints → c1= 50, c2=50.

    After the block ends, inner c1 is destroyed. Outer c1 remains 10, while outer c2 is 50.

    Second cout prints → , c1= 10, c2=50.

    ? Final output: c1= 50, c2=50, c1= 10, c2=50.



Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More


Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More

Ask Your Question or Put Your Review.

loading...