C Plus Plus Miscellaneous - Study Mode
[#586] What will be the output of the following C++ code? #include <iostream>
using namespace std
long factorial (long a)
{
if (a > 1)
return (a * factorial (a + 1))
else
return (1)
}
int main ()
{
long num = 3
cout << num << "! = " << factorial ( num )
return 0
}
Correct Answer
(C) segmentation fault
[#587] What will be the output of the following C++ code? #include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std
int square(int i) { return i * i
}
int main()
{
vector<int> V, V2
V.push_back(0)
V.push_back(1)
V.push_back(2)
transform(V.begin(), V.end(), back_inserter(V2), square)
copy(V2.begin(), V2.end(), ostream_iterator<int>(cout, " "))
cout << endl
}
Correct Answer
(D) 0 1 4
[#588] What will be the output of the following C++ code? #include <iostream>
using namespace std
class BaseClass
{
int x
public:
void setx(int n)
{
x = n
}
void showx()
{
cout << x
}
}
class DerivedClass : private BaseClass
{
int y
public:
void setxy(int n, int m)
{
setx(n)
y = m
}
void showxy()
{
showx()
cout << y << '
'
}
}
int main()
{
DerivedClass ob
ob.setxy(10, 20)
ob.showxy()
return 0
}
Correct Answer
(C) 1020
[#589] What will be the output of the following C++ code? #include <iostream>
#include <vector>
#include <algorithm>
using namespace std
void show(const vector<int>& vi)
{
for (size_t i = 0
i < vi.size()
++i)
cout << vi[i]
cout << endl
}
int main()
{
vector<int> vi
vi.push_back(3)
vi.push_back(5)
vi.push_back(5)
sort(vi.begin(), vi.end())
show(vi)
while(next_permutation(vi.begin(), vi.end()))
show(vi)
return 0
}
Correct Answer
(D) 355 535 553
[#590] What does this template function indicates? ==================
template<class T, class U>
U func(T a, U b)
{
cout<<a<<" "<<b
}
==================
Correct Answer
(A) A function taking a single generic parameter and returning a generic type which may be different from argument type