Problem With Solution
Problem With Solution
#include <iostream>
int main()
return 0;
#include <iostream>
int main()
return 0;
}
Answer: 21 21 21
Explanation: a[1][2] means 1 * (4)+2 = 6th element of an array starting from zero.
Output:
$ g++ point.cpp
$ a.out
21 21 21
#include <iostream>
int main()
int i;
return 0;
Answer: ava
Explanation: In this program we are moving the pointer from first position to second
position and printing the remaining value.
Output:
$ g++ point1.cpp
$ a.out
Ava
int main()
return 0;
Answer: 5
Explanation: In this program, we are making the pointer point to next value and printing
it.
$ g++ point3.cpp
$ a.out
#include <iostream>
int main()
return 0;
}
Answer: c) address of arr
Explanation: As we counted to print only arr, it will print the address of the array.
Output:
$ g++ point2.cpp
$ a.out
0xbfb1cff
#include <iostream>
int main ()
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
return 0;
Answer: 10,20,30,40,50,
Explanation: In this program, we are just assigning a value to the array and printing it
and immediately dereferencing it.
Output:
$ g++ point4.cpp
$ a.out
10,20,30,40,50,
#include <iostream>
int main()
return 0;
Answer: 13
Explanation: In this program, we are adding the value 9 to the initial value of the array,
So it’s printing as 13.
Output:
$ g++ point5.cpp
$ a.out
13
#include <iostream>
using namespace std;
if (p > q)
return p;
else
return q;
main()
int a = 5, b = 10;
int k;
bool x = true;
k =((a * b) + (x + y));
cout << k;
Answer: 52
#include <iostream>
int main()
{
int p;
bool a = true;
bool b = false;
int x = 10;
int y = 5;
p = ((x | y) + (a + b));
cout << p;
return 0;
Answer: 16
#include <iostream>
int main()
char c = 74;
cout << c;
return 0;
Answer: J
Explanation: The literal value for 74 is J. So it will be printing J.
int main()
char a = '\012';
printf("%d", a);
return 0;
Answer: 10
Explanation: The value ‘\012’ means the character with value 12 in octal, which is
decimal 10.
#include <iostream>
int main()
int x = -1;
unsigned int y = 2;
if(x > y)
}
else
Answer: x is greater
Explanation: x is promoted to unsigned int on comparison. On conversion x has all bits
set, making it the bigger one.
#include <iostream>
int main()
if (num1 == num2)
else
return 0;
Answer: harvard
Explanation: Float store floating point numbers with 8 place accuracy and requires 4
bytes of Memory. Double has 16 place accuracy having the size of 8 bytes.
Output:
$ g++ float3.cpp
$ a.out
harvard
#include <iomanip>
#include <iostream>
int main()
double d = 0.1;
return 0;
Answer: b) 0.10000000000000001
Explantion: The double had to truncate the approximatioit’sue to its limited memory,
which resulted in a number that is not exactly 0.1.
Output:
$ g++ float2.out
$ a.out
0.10000000000000001
16. What is the output of the following program?
#include <iostream>
int main()
float i = 123.0f;
return 0;
Answer: 123
Explanation: The value 123 is printed because of its precision.
$ g++ float.cpp
$ a.out
123
#include <iostream>
int main()
float f1 = 0.5;
double f2 = 0.5;
if (f1 == 0.5f)
else
cout << "not equal";
return 0;
Answer: equal
Explanation: 0.5f results in 0.5 to be stored in floating point representations.
Output:
$ g++ float.cpp
$ a.out
equal
#include <iostream>
int main()
return 0;
$ g++ size.cpp
$ a.out
4
#include <iostream>
int main ( )
static double i;
i = 20;
return 0;
Answer: 8
Explanation: The size of the double data type is 8.
$ g++ size1.cpp
$ a.out
#include <iostream>
int main()
Answer: 4
Explanation: In this program, integer is converted into float. Therefore the result of
num1 and num2 is float. And it is returning the size of the float.
Output:
$ g++ size2.cpp
$ a.out
#include <iostream>
int main()
int a = 5;
float b;
cout << a;
return 0;
Answer: 4 5
Explanation: The a as a integer will be converted to float while calculating the size. The
value of any variable doesn’t modify inside sizeof operator. Hence value of variable a
will remain 5.
Output:
$ g++ size3.cpp
$ a.out
45
22. What would be the output of the following program (in 32-bit systems)?
#include <iostream>
int main()
return 0;
Answer: 1 4 4
Explanation: Character is 1 byte, integer 4 bytes and float 4 bytes.
#include <iostream>
enum cat
temp = 7
};
int main()
cout << "If you were cat, you would be " << age << endl;
return 0;
$ g++ enum1.cpp
$ a.out
#include <iostream>
enum test
A = 32, B, C
};
int main()
return 0;
Answer: 323334
Explanation: If we not assigned any value to enum variable means, then the next
number to initialized number will be allocated to the variable.
Output:
$ g++ enum2.cpp
$ a.out
323334
#include <iostream>
enum colour {
};
int main()
return 0;
Answer: 012345
Explanation: The enumerator values start from zero if it is unassigned.
Output:
$ g++ enum3.cpp
$ a.out
012345 <pre>[/expand]
int main()
int i = 0;
return 0;
#include <iostream>
int main()
int i;
enum month {
JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
};
cout << i;
return 0;
a) 01234567891011
b) 123456789101112
c) 34567891011
d) 123456789
View Answer
Answer: c
Explanation: We are getting the values from march to november and printing its
concern number.
#include <iostream>
int g = 100;
int main()
int a;
int b;
b = 20;
a = 35;
g = 65;
cout << b << a << g;
a = 50;
return 0;
Answer: 2035655065
Explanation: The local values of a and g within the block are more dominant than the
global values.
Output:
$ g++ dec1.cpp
$ a.out
2035655065
#include <iostream>
void addprint()
static int s = 1;
s++;
cout << s;
int main()
addprint();
addprint();
addprint();
return 0;
Answer: 234
Explanation: The variable that is declared as static has a file scope.
Output:
advertisement
$ g++ dec2.cpp
$ a.out
234
#include <iostream>
int main()
int a = 10;
if (a < 10) {
cout << i;
else {
cout << i;
return 0;
}
Answer: error
Explanation: We will get compilation error because ‘i’ is an undeclared identifier.
#include <iostream>
int main()
char arr[20];
int i;
*(arr + i) = 65 + i;
*(arr + i) = '\0';
return(0);
Answer: ABCDEFGHIJ
#include <iostream>
int main()
{
char *ptr;
ptr = Str;
ptr += 5;
return 0;
Answer: fg
Explanation: Pointer ptr points to string ‘fg’. So it prints fg.
Output:
$ g++ point.cpp
$ a.out
fg
#include <stdio.h>
int main()
result += array1[temp];
}
result += array2[temp];
return 0;
Answer: 6533
Explanation: In this program we are adding the every element of two arrays. Finally we
got output as 6533.
Output:
$ g++ array.cpp
$ a.out
6533
#include <stdio.h>
int main ()
int n, result = 0;
result += array[n];
}
return 0;
#include <stdio.h>
int main()
return 0;
#include <stdio.h>
return 0;
Answer: ABC
Explanation: We are just printing the values of first 3 values.
$ g++ array.cpp
$ a.out
ABC
#include <stdio.h>
int main()
return 0;
Answer: -30
Explanation: It’s just printing the negative value of the concern element.
$ g++ array.cpp
$ a.out
-30
#include <iostream>
int main()
return 0;
Answer: 21 21 21
Explanation: a[1][2] means 1 * (4)+2 = 6th element of an array starting from zero.
Output:
$ g++ point.cpp
$ a.out
21 21 21
#include <iostream>
int main()
{
int i;
return 0;
Answer: ava
Explanation: In this program we are moving the pointer from first position to second
position and printing the remaining value.
Output:
$ g++ point1.cpp
$ a.out
ava
#include <iostream>
int main()
return 0;
}
Answer: 5
Explanation: In this program, we are making the pointer point to next value and printing
it.
$ g++ point3.cpp
$ a.out
#include <iostream>
int main()
return 0;
advertisement
$ g++ point2.cpp
$ a.out
0xbfb1cff
int main ()
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
return 0;
Answer: 10,20,30,40,50,
Explanation: In this program, we are just assigning a value to the array and printing it
and immediately dereferencing it.
Output:
$ g++ point4.cpp
$ a.out
10,20,30,40,50,
#include <iostream>
using namespace std;
int main()
return 0;
Answer: 13
Explanation: In this program, we are adding the value 9 to the initial value of the array,
So it’s printing as 13.
Output:
$ g++ point5.cpp
$ a.out
13
#include <iostream>
int main()
int const p = 5;
return 0;
}
Answer: Error
Explanation: We cannot modify a constant integer value.
#include <iostream>
#define PI 3.14159
int main ()
float r = 2;
float circle;
circle = 2 * PI * r;
return 0;
Answer: 12.5664
Explanation: In this program, we are finding the area of the circle by using concern
formula.
Output:
$ g++ cons.cpp
$ a.out
12.5664
#include <iostream>
int a = 5, b = 10;
swap(a, b);
return 0;
int temp;
temp = a;
a = b;
b = temp;
$ g++ ref.cpp
$ a.out
#include <iostream>
int main()
int a = 5, b = 10;
swap(a, b);
return 0;
int temp;
temp = a;
a = b;
b = temp;
$ g++ ref.cpp
$ a.out
In swap 105 In main 105
#include <iostream>
int main()
int a = 5, b = 10;
swap(a, b);
return 0;
int temp;
temp = a;
a = b;
b = temp;
$ a.out
#include <iostream>
int main()
func(Str);
return 0;
return 0;
$ g++ b.cpp
$ a.out
0x8048714
50. What is the output of this program?
#include <iostream>
int main()
int *p;
void *vp;
if (vp == p);
return 0;
Answer: equal
Explanation: The void pointer is easily converted to any other type of pointer, so these
are equal.
Output:
$ g++ poi4.cpp
$ a.out
Equal
#include <iostream>
int main()
int i;
char c;
void *data;
i = 2;
c = 'd';
data = &i;
cout << "the data points to the integer value" << data;
data = &c;
cout << "the data now points to the character" << data;
return 0;
$ g++ poi2.cpp
$ a.out
the data points to the integer value0xbfc81824 the data now points to the
character0xbfc8182f
advertisement
#include <iostream>
int main()
int n = 5;
void *p = &n;
return 0;
Answer: 5
Explanation: We just casted this from void to int, so it prints 5
Output:
$ g++ poi1.cpp
$ a.out
#include <iostream>
int main()
int a = 5, c;
void *p = &a;
double b = 3.14;
p = &b;
c = a + b;
return 0;
}
Answer: 8, memory address
Explanation: In this program, we are just adding the two values and printing it.
Output:
$ g++ poi.cpp
$ a.out
0xbfef0378
#include <iostream>
#include <string.h>
int main()
struct student
int num;
char name[25];
};
student stu;
stu.num = 123;
strcpy(stu.name, "John");
return 0;
}
Answer: 123
Explanation: We are copying the value john to the name and then we are printing the
values that are in the program.
Output:
$ g++ stu.cpp
$ a.out
123
John
#include <iostream>
struct Time
int hours;
int minutes;
int seconds;
};
int main()
Time t;
t.hours = 5;
t.minutes = 30;
t.seconds = 45;
return 0;
Answer: 19845
Explanation: In this program, we are just converting the given hours and minutes into
seconds.
Output:
$ g++ stu1.cpp
$ a.out
Total seconds:19845
#include <iostream>
int main()
struct ShoeType
string style;
double price;
};
shoe1.style = "Adidas";
shoe1.price = 9.99;
shoe2 = shoe1;
shoe2.price = shoe2.price / 9;
return 0;
$ g++ stu2.cpp
$ a.out
Adidas $ 9.99
Adidas $ 1.11
#include <iostream>
struct sec
int a;
char b;
};
int main()
return 0;
Answer: 252
Explanation: In this program, We are dividing the values of a and b, printing it.
Output:
$ g++ stu5.cpp
$ a.out
252
#include <iostream>
int main()
int a;
a = 5 + 3 * 5;
cout << a;
return 0;
}
Answer: 20
Explanation: Because the * operator is having highest precedence, So it is executed first
and then the + operator will be executed.
Output:
$ g++ op1.cpp
$ a.out
20
#include <iostream>
int main()
int a = 5, b = 6, c, d;
c = a, b;
d = (a, b);
return 0;
Answer: 5 6
Explanation: It is a separator here. In C, the value a is stored in c and in d the value b is
stored in d because of the bracket.
Output:
$ g++ op3.cpp
$ a.out
5 6
60. What is the output of this program?
#include <iostream>
int main()
int i, j;
j = 10;
cout << i;
return 0;
Answer: 1010
Explanation: j starts with the value 10. j is then incremented to 11. Next, j is added to
100. Finally, j (still containing 11) is added to 999 which yields the result 1010.
Output:
$ g++ op2.cpp
$ a.out
1010
#include <iostream>
int main ()
int x, y;
x = 5;
y = ++x * ++x;
x = 5;
y = x++ * ++x;
return 0;
Answer: 749736
Explanation: Because of the precedence the pre-increment and post increment
operator, we got the output as 749736.
Output:
$ g++ op.cpp
$ a.out
749736
#include <iostream>
int main()
int a = 5, b = 6, c;
c = (a > b) ? a : b;
cout << c;
return 0;
}
Answer: 6
Explanation: Here the condition is false on conditional operator, so the b value is
assigned to c.
Output:
$ g++ op1.cpp
$ a.out
#include <iostream>
main()
double a = 21.09399;
float b = 10.20;
int c ,d;
c = (int) a;
d = (int) b;
return 0;
Answer: 21 10
Explanation: In this program, we are casting the operator to integer, So it is printing as
21 and 10.
Output:
$ g++ op5.cpp
$ a.out
21 10
#include <iostream>
int main ()
int n;
cout << n;
if (n == 3)
break;
return 0;
Answer: 543
Explanation: In this program, We are printing the numbers in reverse order but by using
break statement we stopped printing on 3.
Output:
$ g++ stat.cpp
$ a.out
543
int main()
int a = 10;
if (a < 15)
time:
cout << a;
goto time;
break;
return 0;
#include <iostream>
int main()
int n = 15;
for ( ; ;)
cout << n;
return 0;
a) error
b) 15
c) infinite times of printing n
d) none of the mentioned
View Answer
Answer: c
Explanation: There is not a condition in the for loop, So it will loop continuously.
#include <iostream>
int main()
int i;
cout << i;
return 0;
Answer: 10
Explanation: for loop with a semicolon is called as body less for loop. It is used only for
incrementing the variable values. So in this program the value is incremented and
printed as 10.
Output:
$ g++ stat2.cpp
$ a.out
10
#include <iostream>
int main()
/* this is comment*
return 0;
#include <iostream>
if (a > 1)
return (1);
int main ()
long num = 3;
return 0;
$ g++ arg3.cpp
$ a.out
segmentation fault
#include <iostream>
*x = (*x + 1) * (*x);
int main ( )
{
square(&num);
return 0;
Answer: 110
Explanation: We have increased the x value in operand as x + 1, so it will return as 110.
Output:
$ g++ arg2.cpp
$ a.out
110
#include <iostream>
int main()
int i = 5, j = 6;
return 0;
{
int sum = a + b;
a = 7;
return a + b;
Answer: 13
Explanation: The value of a has been changed to 7, So it returns as 13.
Output:
$ g++ arg1.cpp
$ a.out
13
#include <iostream>
a = b + c;
b = a + c;
c = a + b;
int main()
int x = 2, y =3;
Sum(x, y, y);
return 0;
}
Answer: 2 15
Explanation: We have passed three values and it will manipulate according to the given
condition and yield the result as 2 15.
Output:
$ g++ arg.cpp
$ a.out
2 15
#include <iostream>
void mani()
void mani()
cout<<"hai";
int main()
mani();
return 0;
.
74. What is the output of this program?
#include <iostream>
x = 20;
y = 10;
int main()
int x = 10;
fun(x, x);
cout << x;
return 0;
Answer: 10
Explanation: In this program, we called by value so the value will not be changed, So the
output is 10
Output:
$ g++ fun.cpp
$ a.out
10
#include <iostream>
using namespace std;
a *= 2;
b *= 2;
c *= 2;
int main ()
int x = 1, y = 3, z = 7;
cout << "x =" << x << ", y =" << y << ", z =" << z;
return 0;
Answer: 2 6 14
Explanation: Because we multiplied the values by 2 in the copy function.
Output:
$ g++ arg6.cpp
$ a.out
x = 2,y = 6,z = 14
#include <iostream>
x = 20;
int main()
int x = 10;
fun(x);
return 0;
Answer: 20
Explanation: As we passed by reference, the value is changed and it is returned as 20.
Output:
$ g++ arg5.cpp
$ a.out
20
#include <iostream>
if (a > 1)
else
return (1);
int main ()
long num = 3;
return 0;
$ g++ arg3.cpp
$ a.out
segmentation fault
#include <iostream>
*x = (*x + 1) * (*x);
int main ( )
{
int num = 10;
square(&num);
return 0;
Answer: 110
Explanation: We have increased the x value in operand as x+1, so it will return as 110.
Output:
advertisement
$ g++ arg2.cpp
$ a.out
110
#include <iostream>
int main()
int i = 5, j = 6;
return 0;
{
int sum = a + b;
a = 7;
return a + b;
Answer: 13
Explanation: The value of a has been changed to 7, So it returns as 13.
Output:
$ g++ arg1.cpp
$ a.out
13
#include <iostream>
a = b + c;
b = a + c;
c = a + b;
int main()
int x = 2, y =3;
Sum(x, y, y);
Answer: 2 15
Explanation: We have passed three values and it will manipulate according to the given
condition and yield the result as 2 15
Output:
$ g++ arg.cpp
$ a.out
2 15
#include <iostream>
return ( a > b ? a : b );
int main()
int i = 5;
int j = 7;
return 0;
}
Answer: 7
Explanation: In this program, we are returning the maximum value by using conditional
operator.
Output:
$ g++ ret.cpp
$ a.out
#include <iostream>
double h = 46.50;
double &hours = h;
return hours;
int main()
return 0;
Answer: 46.5
Explanation: We are returning the value what we get as input.
Output:
$ g++ ret1.cpp
$ a.out
46.5
advertisement
#include <iostream>
int result;
result = 0;
while (y != 0)
result = result + x;
y = y - 1;
return(result);
int main ()
int x = 5, y = 5;
return(0);
}
a) 20
b) 25
c) 30
d) 35
View Answer
Answer: b
Explanation: We are multiplying these values by adding every values.
Output:
$ g++ ret.cpp
$ a.out
25
#include <iostream>
int temp;
while (b != 0)
temp = a % b;
a = b;
b = temp;
return(a);
}
int main ()
return(0);
Answer: 5
Explanation: In this program, we are finding the gcd of the number.
Output:
$ g++ ret5.cpp
$ a.out
#include <iostream>
void print(int i)
cout << i;
void print(double f)
cout << f;
int main(void)
{
print(5);
print(500.263);
return 0;
a) 5500.263
b) 500.2635
c) 500.263
d) none of the mentioned
View Answer
Answer: a
Explanation: In this program, we are printing the values and the values will be print(5)
will be printed first because of the order of the execution.
Output:
$ g++ over.cpp
$ a.out
5500.263
#include <iostream>
return X + Y;
{
return X + Y;
int main()
return 0;
#include <iostream>
return (a * b);
return (a / b);
int main()
{
int x = 5, y = 2;
return 0;
Answer: 10 2.5
Explanation: In this program, we are divide and multiply the values.
Output:
advertisement
$ g++ over3.cpp
$ a.out
10 2.5
#include <iostream>
if (flag == true )
else
{
cout << "Flag is false. a = " << a;
int main()
func(200, false);
return 0;
$ g++ def.cpp
$ a.out
#include <iostream>
#include <string>
int main()
string number;
return number;
$ g++ def1.cpp
$ a.out
advertisement
#include <iostream>
int main()
Values(1);
Values(3, 4);
return 0;
a)
b) 1st value: 1
10
3
10
c) compile time error
d) none of the mentioned
View Answer
$ g++ def2.cpp
$ a.out
1st value: 1
2nd value: 10
1st value: 3
2nd value: 4
#include <iostream>
int c;
c = m + n;
return c;
int main()
return 0;
#include <iostream>
#include <stdarg.h>
using namespace std;
va_list Numbers;
va_start(Numbers, Count);
int Sum = 0;
va_end(Numbers);
return (Sum/Count);
int main()
return 0;
Answer: 4
Explanation: We are just calculating the average of these numbers using cstdarg.
Output:
$ g++ uka.cpp
$ a.out
#include <iostream>
#include <stdarg.h>
int sum = 0;
va_list args;
va_start (args,num);
sum += num;
va_end (args);
return sum;
return 0;
}
a) 32
b) 23
c) 48
d) compile time error
View Answer
Answer: a
Explanation: We are adding these numbers by using for statement and stdarg.
Output:
$ g++ uka.cpp
$ a.out
The result is 32
#include <iostream>
#include <stdarg.h>
int main()
dumplist(2, 4, 8);
dumplist(3, 6, 9, 7);
return 0;
va_list p;
int i;
va_start(p, n);
while (n-->0)
i = va_arg(p, int);
cout << i;
va_end(p);
Answer: 48697
Explanation: In this program, we are eradicating the first value
by comparing using while operator.
Output:
$ g++ rka3.cpp
$ a.out
48697
#include <iostream>
#include <stdarg.h>
int main()
int x, y;
x = flue('A', 1, 2, 3);
y = flue('1', 1.0,1, '1', 1.0f, 1l);
return 0;
return c;
Answer: 6549
Explanation: In this program, we are returning the ascii value of the character and
printing it.
Output:
$ g++ rka4.cpp
$ a.out
6549
#include <iostream>
#include <stdarg.h>
int main()
return 0;
}
va_list ptr;
int num;
va_start(ptr, msg);
Answer: 4
Explanation: In this program, we are moving the pointer to the second value and
printing it.
Output:
$ g++ uka6.cpp
$ a.out
#include <iostream>
}
int operation(int first, int second, int (*functocall)(int, int))
int main()
int a;
cout << a;
return 0;
Answer: 45
Explanation: In this program, we are adding two numbers with 15, So we got the output
as 40.
Output:
$ g++ pfu2.cpp
$ a.out
40
#include <iostream>
void func(int x)
{
cout << x ;
int main()
void (*n)(int);
n = &func;
(*n)( 2 );
n( 2 );
return 0;
Answer: 22
Explanation: As we are calling the function two times with the same value, So it is
printing as 22.
Output:
$ g++ pfu.cpp
$ a.out
22
#include <iostream>
int main()
{
(*p)('d', 9);
p(10, 9);
return 0;
return 0;
Answer: d9
Explanation: None.
Output:
$ g++ pfu1.cpp
$ a.out
d9
#include <iostream>
cout << a;
cout << b;
return 0;
}
int main(void)
int(*ptr)(char, int);
ptr = func;
func(2, 3);
ptr(2, 3);
return 0;
#include <iostream>
int main()
{
int a;
cout << a;
return 0;
Answer: 40
Explanation: In this program, we are adding two numbers with 15, So we got the output
as 40.
Output:
$ g++ pfu2.cpp
$ a.out
40
#include <iostream>
void func(int x)
cout << x ;
int main()
void (*n)(int);
n = &func;
(*n)( 2 );
n( 2 );
return 0;
Answer: 22
Explanation: As we are calling the function two times with the same value, So it is
printing as 22.
Output:
$ g++ pfu.cpp
$ a.out
22
#include <iostream>
int main()
(*p)('d', 9);
p(10, 9);
return 0;
return 0;
Answer: d9
Explanation: None.
Output:
$ g++ pfu1.cpp
$ a.out
d9
#include <iostream>
cout << a;
cout << b;
return 0;
int main(void)
int(*ptr)(char, int);
ptr = func;
func(2, 3);
ptr(2, 3);
return 0;
#include <iostream>
namespace first
int var = 5;
namespace second
int main ()
int a;
a = first::var + second::var;
cout << a;
return 0;
}
Answer: 8
Explanation: As we are getting two variables from namespace variable and we are
adding that.
Output:
$ g++ name.cpp
$ a.out
#include <iostream>
namespace first
int x = 5;
int y = 10;
namespace second
double x = 3.1416;
double y = 2.7183;
int main ()
using first::x;
using second::y;
bool a, b;
a = x > y;
return 0;
Answer: 10
Explanation: We are inter mixing the variable and comparing it which is bigger and
smaller and according to that we are printing the output.
Output:
$ g++ name1.cpp
$ a.out
10
#include <iostream>
namespace Box1
int a = 4;
namespace Box2
int a = 13;
}
int main ()
int a = 16;
Box1::a;
Box2::a;
cout << a;
return 0;
Answer: 16
Explanation: In this program, as there is lot of variable a and it is printing the value
inside the block because it got the highest priority.
Output:
$ g++ name2.cpp
$ a.out
16
#include <iostream>
namespace space
int x = 10;
namespace space
{
int y = 15;
Answer: 55
Explanation: We are overriding the value at the main function and so we are getting the
output as 55.
Output:
$ g++ name4.cpp
$ a.out
#include <iostream>
namespace extra
int i;
void i()
{
using namespace extra;
int i;
i = 9;
cout << i;
int main()
class i { letter j; };
::i();
return 0;
Answer: 9
Explanation: A scope resolution operator without a scope qualifier refers to the global
namespace.
#include <iostream>
int main()
int age = 0;
try
if (age < 0)
{
return 0;
Answer: 0
Explanation: As the zero marks the beginning of the positive number, it is printed as
output
Output:
$ g++ excep.cpp
$ a.out
#include <iostream>
int Num;
Num = 1;
while (true)
throw Num;
Num++;
int main(void)
try
PrintSequence(20);
catch(int ExNum)
return 0;
$ a.out
#include <iostream>
if (b == 0)
return (a / b);
int main ()
int x = 50;
int y = 2;
double z = 0;
try
z = division(x, y);
cout << z;
}
return 0;
Answer: 35
Explanation: In this program, we resembling the division by using the exception
handling.
Output:
$ g++ excep2.cpp
$ a.out
25
#include <iostream>
int main()
char* buff;
try
if (buff == 0)
throw "Memory allocation failure!";
else
catch(char *strg)
return 0;
$ g++ excep3.cpp
$ a.out
#include <iostream>
void Funct();
int main()
try
{
Funct();
catch(double)
return 0;
void Funct()
throw 3;
$ g++ excep4.cpp
$ a.out
Aborted
#include <iostream>
#include <exception>
try
catch(bad_alloc&)
return 0;
$ g++ excep5.cpp
$ a.out
Allocated successfully