C Plus Plus Miscellaneous - Study Mode

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

int main()
{
complex <double> cn(3.0, 5.0)

cout<<"Complex number is: "<<real(cn)<<" + "<<imag(cn)<<"i"<<endl

return 0

}
Correct Answer

(A) Complex number is: 3 + 5i

Explanation

Solution: In the given C++ code, the complex class from the C++ Standard Library is used to represent a complex number. In the statement complex cn(3.0, 5.0) , the constructor initializes the real part as 3.0 and the imaginary part as 5.0 . The function real(cn) returns the real part of the complex number, which is 3 . The function imag(cn) returns the imaginary part, which is 5 . Hence, the output will be: Complex number is: 3 + 5i .

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

class poly
{
protected:
int width, height

public:
void set_values(int a, int b)
{
width = a

height = b

}
}

class Coutput
{
public:
void output(int i)

}

void Coutput::output(int i)
{
cout << i

}
class rect:public poly, public Coutput
{
public:
int area()
{
return(width * height)

}
}

class tri:public poly, public Coutput
{
public:
int area()
{
return(width * height / 2)

}
}

int main()
{
rect rect

tri trgl

rect.set_values(3, 4)

trgl.set_values(4, 5)

rect.output(rect.area())

trgl.output(trgl.area())

return 0

}
Correct Answer

(B) 1210

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

int main ()
{
map<char, int> mymap

map<char, int> :: iterator it

mymap['b'] = 100

mymap['a'] = 200

mymap['c'] = 300

for (map<char, int> :: iterator it = mymap.begin()

it != mymap.end()

++it)
cout << it -> first << " => " << it -> second << '
'

return 0

}
Correct Answer

(C) a => 200 b => 100 c => 300

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

int main ()
{
unsigned int i

deque<int> mydeque

deque<int> :: iterator it

mydeque.push_back ( 100 )

mydeque.push_back ( 200 )

mydeque.push_back ( 300 )

for (it = mydeque.begin()

it != mydeque.end()

++it)
mydeque.clear()

cout << ' ' << *it

}
Correct Answer

(D) error

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

bool myfunction (int i,int j)
{
return (i < j)

}
int main ()
{
int myints[] = {9, 8, 7, 6}

vector<int> myvector (myints, myints + 4)

partial_sort (myvector.begin(), myvector.begin() + 2, myvector.end())

partial_sort (myvector.begin(), myvector.begin() + 2, myvector.end(),
myfunction)

for (vector<int> :: iterator it = myvector.begin()

it != myvector.end()

++it)
cout << ' ' << *it

return 0

}
Correct Answer

(C) 6 7 9 8