C Plus Plus Miscellaneous - Study Mode
[#916] What will be the output of the following C++ code? #include <iostream>
#include <vector>
using namespace std
int main ()
{
vector<int> myvector (5)
int* p = myvector.data()
*p = 10
++p
*p = 20
p[2] = 100
for (unsigned i = 0
i < myvector.size()
++i)
cout << ' ' << myvector[i]
return 0
}
Correct Answer
(A) 10 20 0 100 0
[#917] What will be the output of the following C++ code? #include <iostream>
#include <exception>
#include <typeinfo>
using namespace std
class Test1
{
virtual int Funct()
{
}
}
int main ()
{
try
{
Test1 * var = NULL
typeid (*var)
}
catch (std::exception& typevar)
{
cout << "Exception: " << typevar.what() << endl
}
return 0
}
Correct Answer
(C) Exception:std:bad_typeid
[#918] 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}
int second[] = {50, 40, 30}
vector<int> v(4)
vector<int> :: iterator it
sort (first, first + 3)
sort (second, second + 3)
it = set_symmetric_difference (first, first + 2, second, second + 2,
v.begin())
v.resize(it - v.begin())
for (it = v.begin()
it != v.end()
++it)
cout << ' ' << *it
return 0
}
Correct Answer
(D) 5 10 30 40
[#919] What will be the output of the following C++ code? #include <iostream>
#include <array>
using namespace std
int main(int argc, char const *argv[])
{
array<int,10> arr = {1,2,3,4,5}
cout<<"size:"<<arr.size()<<endl
cout<<"maxsize:"<<arr.max_size()<<endl
return 0
}
Correct Answer
(A) size:10 maxsize:10
[#920] What will be the output of the following C++ code? #include <iostream>
#include <new>
#include <cstdlib>
using namespace std
const int bsize = 512
int *pa
bool allocate = true
void get_memory()
{
cerr << "free store exhausted" << endl
delete [] pa
allocate = false
}
void eat_memory(int size)
{
int *p = new int[size]
if (allocate)
eat_memory(size)
else
cerr << "free store addr = " << p << endl
}
int main()
{
set_new_handler(get_memory)
pa = new int[bsize]
cerr << "free store addr = " << pa << endl
eat_memory(bsize)
return 0
}
Correct Answer
(C) Segmentation fault