Aspire Faculty ID #12784 · Topic: JECA MCA 2023 · Just now
JECA MCA 2023

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.

Previous 10 Questions — JECA MCA 2023

Nearest first

Next 10 Questions — JECA MCA 2023

Ascending by ID
Ask Your Question or Put Your Review.

loading...