C Plus Plus Miscellaneous - Study Mode

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

struct A
{
int i

char j

float f

void func()

}

void A :: func() {}
struct B
{
public:
int i

char j

float f

void func()

}

void B :: func() {}
int main()
{
A a

B b

a.i = b.i = 1

a.j = b.j = 'c'

a.f = b.f = 3.14159

a.func()

b.func()

cout << "Allocated"

return 0

}
Correct Answer

(A) Allocated

[#607] 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 = 2, ii

string ww("A")

ii = square<int>(i)

cout << i << ": " << ii

cout << square<string>(ww) << ":" << endl

}
Correct Answer

(A) 2: 4AA:

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

template<class T>
class A
{
public:
T func(T a, T b){
return a/b

}
}

int main(int argc, char const *argv[])
{
A <float>a1

cout<<a1.func(3,2)<<endl

cout<<a1.func(3.0,2.0)<<endl

return 0

}
Correct Answer

(D) 1.5 1.5

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

int main()
{
set<int> tst

tst.insert(12)

tst.insert(21)

tst.insert(32)

tst.insert(31)

set<int> :: const_iterator pos

for(pos = tst.begin()

pos != tst.end()

++pos)
cout << *pos << ' '

return 0

}
Correct Answer

(B) 12 21 31 32

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

int main ()
{
int myints[] = {1, 2, 3}

sort (myints, myints + 3)

do
{
} while ( next_permutation(myints, myints + 3) )

cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '
'

return 0

}
Correct Answer

(A) 1 2 3