C Plus Plus Miscellaneous - Study Mode
[#681] What will be the output of the following C++ code? #include <iostream>
#include <iterator>
#include <list>
using namespace std
int main ()
{
list<int> mylist
for (int i = 0
i < 5
i++)
mylist.push_back (i * 20)
list<int> :: iterator first = mylist.begin()
list<int> :: iterator last = mylist.end()
cout << distance(first, last) << endl
return 0
}
Correct Answer
(C) 5
[#682] What will be the output of the following C++ code? #include <iostream>
using namespace std
int main ()
{
int a = 100
double b = 3.14
cout << a
cout << endl
cout << b << endl << a * b
endl (cout)
return 0
}
Correct Answer
(D) All of the mentioned
[#683] What will be the output of the following C++ code? #include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std
int op_increase (int i)
{
return ++i
}
int main ()
{
vector<int> a
vector<int> b
for (int i = 1
i < 4
i++)
a.push_back (i * 10)
b.resize(a.size())
transform (a.begin(), a.end(), b.begin(), op_increase)
transform (a.begin(), a.end(), b.begin(), a.begin(), plus<int>())
for (vector<int> :: iterator it = a.begin()
it != a.end()
++it)
cout << ' ' << *it
return 0
}
Correct Answer
(D) 21 41 61
[#684] What will be the output of the following C++ code? #include <iostream>
using namespace std
struct A
{
virtual void f()
{
cout << "Class A" << endl
}
}
struct B : A
{
virtual void f()
{
cout << "Class B" << endl
}
}
struct C : A
{
virtual void f()
{
cout << "Class C" << endl
}
}
void f(A* arg)
{
B* bp = dynamic_cast<B*>(arg)
C* cp = dynamic_cast<C*>(arg)
if (bp)
bp -> f()
else if (cp)
cp -> f()
else
arg -> f()
}
int main()
{
A aobj
C cobj
A* ap = &cobj
A* ap2 = &aobj
f(ap)
f(ap2)
}
Correct Answer
(C) Both Class C & A
[#685] What will be the output of the following C++ code? #include <iostream>
using namespace std
class Base
{
public:
int m
Base(int n=0)
: m(n)
{
cout << "Base" << endl
}
}
class Derived: public Base
{
public:
double d
Derived(double de = 0.0)
: d(de)
{
cout << "Derived" << endl
}
}
int main()
{
cout << "Instantiating Base" << endl
Base cBase
cout << "Instantiating Derived" << endl
Derived cDerived
return 0
}
Correct Answer
(A) Instantiating Base Base Instantiating Derived Base Derived