Ad

Chapter - 3: Graduating to C++

What will be the output of the following programs:


Ad
B
Sections
1
Exercises

(a)

#include<isotream>
using namespace std;
int main()
{
	int i = 5;
	int &j = i;
	int p = 10;
	
	j = p;
	cout << endl << i << endl << j;
	p = 20;
	cout << endl << i << endl << j;
	
	return 0;	
}

Output:
10
10
10
10


(b)

#include<isotream>

using namespace std;

int main()
{
	char *p = "hello";
	char *q = p;

	cout << p << endl << q;
	q = "Good Bye";
	cout << p << endl << q;

	return 0;
}

Output: hello
hello hello
Good Bye


(c)

#include<isotream>

using namespace std;

int i = 20;

int main()
{
	int i = 5;
	
	cout << i << endl << ::i;
	
	return 0;	
}

Output: 5
20


(d)

#include<isotream>

using namespace std;

int i = 20;

int main()
{
	int i = 5;
	
	cout << i << endl << ::i;
	{
		int i = 10;
		cout << i << endl << ::i;
	}
	
	return 0;	
}

Output: 5
20 10 
20


(e)

#include<isotream>

using namespace std;

const int i = 10;

int main()
{
	const int i = 20;
	
	cout << i << endl << ::i;
	cout << &i << endl << &::i;
	
	return 0;	
}

Output: 20
10 [Address of local i]
[Address of global i]


(f)

#include<isotream>

using namespace std;

int main()
{
	int i;
	
	cout << sizeof(i) << endl << sizeof('i');
	
	return 0;	
}

(g)

#include<isotream>

using namespace std;

int main()
{
	for(int i = 1; i <=10; i++)
		cout << i << endl;
		
	cout << i;
	
	return 0;	
}

Error: i is not defined. the scope of i is only in for loop. Outside it is unknown until it is not defined.

Ad

© 2024 Garbage Valuegarbage value logo