1. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <tuple>
using namespace std;
int main()
{
tuple <int, string> tp1;
tuple <int, string> tp2;
tp1 = make_tuple(0, "Hello");
tp2 = make_tuple(1, "World");
auto tp3 = tuple_cat(tp1, tp2);
cout<<"("<<get<0>(tp3)<<", "<<get<1>(tp3)<<", "<<get<2>(tp3)<<",
"<<get<3>(tp3)<<")"<<endl;
return 0;
}
#include <iostream>
#include <string>
#include <tuple>
using namespace std;
int main()
{
tuple <int, string> tp1;
tuple <int, string> tp2;
tp1 = make_tuple(0, "Hello");
tp2 = make_tuple(1, "World");
auto tp3 = tuple_cat(tp1, tp2);
cout<<"("<<get<0>(tp3)<<", "<<get<1>(tp3)<<", "<<get<2>(tp3)<<",
"<<get<3>(tp3)<<")"<<endl;
return 0;
}
2. What will be the output of the following C++ code if the following arguments are executed on terminal?
===============program.cpp=============
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
for(int i=0;i<argc;i++)
cout<<argv[i]<<"\n";
}
=======================================
================commands===============
$ g++ program.cpp -o output
$ ./output Hello World
=======================================
===============program.cpp=============
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
for(int i=0;i<argc;i++)
cout<<argv[i]<<"\n";
}
=======================================
================commands===============
$ g++ program.cpp -o output
$ ./output Hello World
=======================================
3. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int x = -1;
char *ptr;
ptr = new char[256];
try
{
if (x < 0)
{
throw x;
}
if (ptr == NULL)
{
throw " ptr is NULL ";
}
}
catch (...)
{
cout << "Exception occurred: exiting "<< endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int x = -1;
char *ptr;
ptr = new char[256];
try
{
if (x < 0)
{
throw x;
}
if (ptr == NULL)
{
throw " ptr is NULL ";
}
}
catch (...)
{
cout << "Exception occurred: exiting "<< endl;
}
return 0;
}
4. When do we call that resource is leaked?
5. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool myfunction (int i, int j)
{
return (i==j);
}
int main ()
{
int myints[] = {10, 20, 20, 20, 30, 30, 20, 20, 10};
vector<int> myvector (myints, myints + 9);
vector<int> :: iterator it;
it = unique (myvector.begin(), myvector.end());
myvector.resize( distance(myvector.begin(), it) );
unique (myvector.begin(), myvector.end(), myfunction);
for (it = myvector.begin(); it != myvector.end(); ++it)
cout << ' ' << *it;
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool myfunction (int i, int j)
{
return (i==j);
}
int main ()
{
int myints[] = {10, 20, 20, 20, 30, 30, 20, 20, 10};
vector<int> myvector (myints, myints + 9);
vector<int> :: iterator it;
it = unique (myvector.begin(), myvector.end());
myvector.resize( distance(myvector.begin(), it) );
unique (myvector.begin(), myvector.end(), myfunction);
for (it = myvector.begin(); it != myvector.end(); ++it)
cout << ' ' << *it;
return 0;
}
6. How many types of specialization are there in c++?
7. What will be the output of the following C++ code?
#include<iostream>
#include<iterator>
#include<vector>
using namespace std;
int main()
{
vector<int> ar = { 1, 2, 3, 4, 5 };
vector<int>::iterator ptr = ar.begin();
ptr = advance(ptr, 2);
cout << *ptr << endl;
return 0;
}
#include<iostream>
#include<iterator>
#include<vector>
using namespace std;
int main()
{
vector<int> ar = { 1, 2, 3, 4, 5 };
vector<int>::iterator ptr = ar.begin();
ptr = advance(ptr, 2);
cout << *ptr << endl;
return 0;
}
8. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
int first[] = {5, 10, 15, 20, 25};
int second[] = {50, 40, 30, 20, 10};
vector<int> v(10);
vector<int> :: iterator it;
sort (first, first + 5);
sort (second, second + 5);
it = set_union (first, first + 5, second, second + 5, v.begin());
v.resize(it-v.begin());
for (it = v.begin(); it != v.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
int first[] = {5, 10, 15, 20, 25};
int second[] = {50, 40, 30, 20, 10};
vector<int> v(10);
vector<int> :: iterator it;
sort (first, first + 5);
sort (second, second + 5);
it = set_union (first, first + 5, second, second + 5, v.begin());
v.resize(it-v.begin());
for (it = v.begin(); it != v.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
9. Which objects information is loaded in locale object?
10. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class X
{
public:
int a;
void f(int b)
{
cout<< b << endl;
}
};
int main()
{
int X :: *ptiptr = &X :: a;
void (X :: * ptfptr) (int) = &X :: f;
X xobject;
xobject.*ptiptr = 10;
cout << xobject.*ptiptr << endl;
(xobject.*ptfptr) (20);
}
#include <iostream>
using namespace std;
class X
{
public:
int a;
void f(int b)
{
cout<< b << endl;
}
};
int main()
{
int X :: *ptiptr = &X :: a;
void (X :: * ptfptr) (int) = &X :: f;
X xobject;
xobject.*ptiptr = 10;
cout << xobject.*ptiptr << endl;
(xobject.*ptfptr) (20);
}
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 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 9
- C plus plus miscellaneous - Section 10
- C plus plus miscellaneous - Section 11