C Plus Plus Miscellaneous - Study Mode
[#931] What will be the output of the following C++ code? #include <iostream>
#include <algorithm>
using namespace std
int main ()
{
int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }
int* pbegin = myints
int* pend = myints + sizeof(myints) / sizeof(int)
pend = remove (pbegin, pend, 20)
for (int* p = pbegin
p != pend
++p)
cout << ' ' << *p
return 0
}
Correct Answer
(B) 10 30 30 10 10
[#932] What will be the output of the following C++ code? #include <typeinfo>
#include <iostream>
using namespace std
class Myshape
{
public:
virtual void myvirtualfunc() const {}
}
class mytriangle: public Myshape
{
public:
virtual void myvirtualfunc() const
{
}
}
int main()
{
Myshape Myshape_instance
Myshape &ref_Myshape = Myshape_instance
try
{
mytriangle &ref_mytriangle = dynamic_cast<mytriangle&>(ref_Myshape)
}
catch (bad_cast)
{
cout << "Can't do the dynamic_cast lor!!!" << endl
cout << "Caught: bad_cast exception. Myshape is not mytriangle.
"
}
return 0
}
Correct Answer
(C) Can't able to create the dynamic instance for the triangle, So it is arising an exception
[#933] What will be the output of the following C++ code? #include <iostream>
#include <sstream>
using namespace std
int main()
{
stringstream mys(ios :: in | ios :: out)
std :: string dat("The double value is : 74.79 .")
mys.str(dat)
mys.seekg(-7, ios :: end)
double val
mys >> val
val = val*val
mys.seekp(-7,ios::end)
mys << val
std :: string new_val = mys.str()
cout << new_val
return 0
}
Correct Answer
(A) 5593.54
[#934] What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
int a = 10, b = 20, c = 30
float d
try
{
if ((a - b) != 0)
{
d = c / (a - b)
cout << d
}
else
{
throw(a - b)
}
}
catch (int i)
{
cout<<"Answer is infinite "<<i
}
}
Correct Answer
(B) -3
[#935] What will be the output of the following C++ code? #include <iostream>
using namespace std
template <class T>
inline T square(T x)
{
T result
result = x * x
return result
}
template <>
string square<string>(string ss)
{
return (ss+ss)
}
int main()
{
int i = 4, ii
string ww("A")
ii = square<int>(i)
cout << i << ii
cout << square<string>(ww) << endl
}
Correct Answer
(A) 416AA