C Plus Plus Miscellaneous - Study Mode

[#596] What will be the output of the following C++ code? #include <iostream>
#include <queue>
using namespace std

int main ()
{
priority_queue<int> mypq

mypq.push(30)

mypq.push(100)

mypq.push(25)

mypq.push(40)

while (!mypq.empty())
{
cout << " " << mypq.top()

mypq.pop()

}
cout << endl

return 0

}
Correct Answer

(A) 100 40 30 25

[#597] What will be the output of the following C++ code? #include <iostream>
#include <iterator>
#include <list>
using namespace std

int main ()
{
list<int> firstlist, secondlist

for (int i = 1

i <= 2

i++)
{
firstlist.push_back(i)

secondlist.push_back(i * 10)

}
list<int> :: iterator it

it = firstlist.begin()

advance (it, 3)

copy (secondlist.begin(), secondlist.end(), inserter(firstlist, it))

for ( it = firstlist.begin()

it != firstlist.end()

++it )
cout << *it << " "

return 0

}
Correct Answer

(A) 10 20 1 2

[#598] What will be the output of the following C++ code? #include <iostream>
using namespace std

template<typename type>
type Max(type Var1, type Var2)
{
return Var1 > Var2 ? Var1:Var2

}
int main()
{
int p

p = Max(100, 200)

cout << p << endl

return 0

}
Correct Answer

(B) 200

[#599] What will be the output of the following C++ code? #include <iostream>
#include <exception>
using namespace std

class base { virtual void dummy() {} }

class derived: public base { int a

}

int main ()
{
try
{
base * pba = new derived

base * pbb = new base

derived * pd

pd = dynamic_cast<derived*>(pba)

if (pd == 0)
cout << "Null pointer on first type-cast" << endl

pd = dynamic_cast<derived*>(pbb)

if (pd == 0)
cout << "Null pointer on second type-cast" << endl

}
catch (exception& e)
{
cout << "Exception: " << e.what()

}
return 0

}
Correct Answer

(B) Null pointer on second type-cast

[#600] What will be the output of the following C++ code? #include <iostream>
using namespace std

struct A
{
virtual ~A()
{
cout << "~A()" << endl

}
void operator delete[](void* p, size_t)
{
cout << "A :: operator delete[]" << endl

delete [] p

}
}

struct B : A
{
void operator delete[](void* p, size_t)
{
cout << "B :: operator delete[]" << endl

delete [] p

}
}

int main()
{
A* bp = new B[3]

delete[] bp

}

Correct Answer

(D) Warning