1. How can object be allocated outside the object lifetime?
2. In nested try catch blocks, if both inner and outer catch blocks are unable to handle the exception thrown, then . . . . . . . .
3. How many number of spaces should be set in default tab?
4. What will be the output of the following C++ code?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
vector<char> v2;
for (int i = 1; i <= 5; i++)
v1.push_back(i);
for (int i = 6; i <= 10; i++)
v2.push_back(i);
v1.swap(v2);
for(int i=0;i<v1.size();i++)
cout<<v1[i]<<" ";
for(int i=0;i<v2.size();i++)
cout<<v2[i]<<" ";
cout<<endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
vector<char> v2;
for (int i = 1; i <= 5; i++)
v1.push_back(i);
for (int i = 6; i <= 10; i++)
v2.push_back(i);
v1.swap(v2);
for(int i=0;i<v1.size();i++)
cout<<v1[i]<<" ";
for(int i=0;i<v2.size();i++)
cout<<v2[i]<<" ";
cout<<endl;
return 0;
}5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void empty() throw()
{
cout << "In empty()";
}
void with_type() throw(int)
{
cout << "Will throw an int";
throw(1);
}
int main()
{
try
{
empty();
with_type();
}
catch (int)
{
cout << "Caught an int";
}
}
#include <iostream>
using namespace std;
void empty() throw()
{
cout << "In empty()";
}
void with_type() throw(int)
{
cout << "Will throw an int";
throw(1);
}
int main()
{
try
{
empty();
with_type();
}
catch (int)
{
cout << "Caught an int";
}
}6. What will be the output of the following C++ code?
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T, class U>
class A
{
T x;
U y;
};
int main()
{
A<char, char> a;
A<int, int> b;
cout << sizeof(a) << endl;
cout << sizeof(b) << endl;
return 0;
}
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T, class U>
class A
{
T x;
U y;
};
int main()
{
A<char, char> a;
A<int, int> b;
cout << sizeof(a) << endl;
cout << sizeof(b) << endl;
return 0;
}7. Return type of uncaught_exception() is . . . . . . . .
8. Exception handlers are declared with . . . . . . . . keyword.
9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class bowl
{
public:
int apples;
int oranges;
};
int count_fruit(bowl * begin, bowl * end, int bowl :: *fruit)
{
int count = 0;
for (bowl * iterator = begin; iterator != end; ++ iterator)
count += iterator ->* fruit;
return count;
}
int main()
{
bowl bowls[2] = {{ 1, 2 },{ 3, 5 }};
cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: apples) << " apples\n";
cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: oranges) << " oranges\n";
return 0;
}
#include <iostream>
using namespace std;
class bowl
{
public:
int apples;
int oranges;
};
int count_fruit(bowl * begin, bowl * end, int bowl :: *fruit)
{
int count = 0;
for (bowl * iterator = begin; iterator != end; ++ iterator)
count += iterator ->* fruit;
return count;
}
int main()
{
bowl bowls[2] = {{ 1, 2 },{ 3, 5 }};
cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: apples) << " apples\n";
cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: oranges) << " oranges\n";
return 0;
}10. What will be the output of the following C++ code?
#include <iostream>
#include <Valarray>
using namespace std;
int main()
{
Valarray<int> varr = { 10, 2, 20, 1, 30 };
for (int &x: varr) cout << x << " ";
cout<<endl;
varr = varr.apply([](int x){return x+=5;});
for (int &x: varr) cout << x << " ";
return 0;
}
#include <iostream>
#include <Valarray>
using namespace std;
int main()
{
Valarray<int> varr = { 10, 2, 20, 1, 30 };
for (int &x: varr) cout << x << " ";
cout<<endl;
varr = varr.apply([](int x){return x+=5;});
for (int &x: varr) cout << x << " ";
return 0;
}Read More Section(C plus plus miscellaneous)
Each Section contains maximum 100 MCQs question on C plus plus miscellaneous. To get more questions visit other sections.
- C plus plus miscellaneous - Section 1
- C plus plus miscellaneous - Section 2
- C plus plus miscellaneous - Section 3
- C plus plus miscellaneous - Section 4
- C plus plus miscellaneous - Section 5
- C plus plus miscellaneous - Section 6
- C plus plus miscellaneous - Section 7
- C plus plus miscellaneous - Section 8
- C plus plus miscellaneous - Section 10
- C plus plus miscellaneous - Section 11
