C Plus Plus Miscellaneous - Study Mode

[#716] What will be the output of the following C++ code? #include <iostream>
#include <algorithm>
#include <vector>

using namespace std
bool IsOdd (int i)
{
return (i%2)==1
}

int main ()
{
vector<int> v = {4,2,10,5,1,8}
for(int i=0
i<v.size()
i++)
cout<<v[i]<<" "
cout<<endl
sort(v.begin(), v.end())
for(int i=0
i<v.size()
i++)
cout<<v[i]<<" "
return 0
}
Correct Answer

(A) 4 2 10 5 1 8 1 2 4 5 8 10

[#717] What will be the output of the following C++ code? #include <iostream>
using namespace std
template<typename T>
inline T square(T x)
{
T result
result = x * x
return result
}
int main()
{
int i, ii
float x, xx
double y, yy
i = 2
x = 2.2
y = 2.2
ii = square(i)
cout << i << " " << ii << endl
yy = square(y)
cout << y << " " << yy << endl
}
Correct Answer

(A) 2 4 2.2 4.84

[#718] What will be the output of the following C++ code? #include <iostream>
using namespace std
class Base
{
protected:
int a
public:
Base()
{
a = 34
}
Base(int i)
{
a = i
}
virtual ~Base()
{
if (a < 0) throw a
}
virtual int getA()
{
if (a < 0)
{
throw a
}
}
}
int main()
{
try
{
Base b(-25)
cout << endl << b.getA()
}
catch (int)
{
cout << endl << "Illegal initialization"
}
}
Correct Answer

(B) Terminate called after throwing an instance of 'int'

[#719] What will be the output of the following C++ code? #include<iostream>
#include<stdlib.h>
using namespace std
template<class T, class U, class V=double>
class A
{
T x
U y
V z
}
int main()
{
A<int, int> a
A<double, double> b
cout << sizeof(a) << endl
cout << sizeof(b) << endl
return 0
}
Correct Answer

(A) 16 24

[#720] What will be the output of the following C++ code? #include <iostream>
#include <queue>
using namespace std
int main ()
{
queue<int> myqueue
int sum (0)
for (int i = 1
i <= 10
i++)
myqueue.push(i)
while (!myqueue.empty())
{
sum += myqueue.front()
myqueue.pop()
}
cout << sum << endl
return 0
}
Correct Answer

(D) 55