C Plus Plus Miscellaneous - Study Mode

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

bool same_integral_part (double first, double second)
{
return ( int(first) == int(second) )

}
struct is_near
{
bool operator() (double first, double second)
{
return (fabs(first - second) < 5.0)

}
}

int main ()
{
double mydoubles[] = { 12.15, 2.72, 73.0, 12.77, 3.14, 12.77, 73.35, 72.25, 15.3, 72.25 }

list<double> mylist (mydoubles, mydoubles + 10)

mylist.sort()

mylist.unique()

mylist.unique (same_integral_part)

mylist.unique (is_near())

for (list<double> :: iterator it = mylist.begin()

it != mylist.end()

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

cout << '
'

return 0

}
Correct Answer

(A) 2.72 12.15 72.25

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

int main()
{
float val = 5.5

any var(val)

cout<<any_cast<float>(var)<<endl

var.reset()

if(!var.has_value())
{
cout<<"var is empty
"

}
else{
cout<<"var is not empty
"

}
return 0

}
Correct Answer

(D) 5.5 var is empty

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

class X
{
int m

public:
X() : m(10)
{
}
X(int mm): m(mm)
{
}
int getm()
{
return m

}
}

class Y : public X
{
int n

public:
Y(int nn) : n(nn) {}
int getn() { return n

}
}

int main()
{
Y yobj( 100 )

cout << yobj.getm() << " " << yobj.getn() << endl

}
Correct Answer

(A) 10 100

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

int main()
{
bitset<8> b1(95)

bitset<8> b2(45)

cout<<~b1<<endl

cout<<(b1|b2)<<endl

cout<<(b1&b2)<<endl

}
Correct Answer

(A) 10100000 01111111 00001101

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

int myfunction (int x, int y)
{
return x + 2 * y

}
struct myclass
{
int operator()(int x, int y)
{
return x + 3 * y

}
} myobject

int main ()
{
int init = 100

int numbers[] = {10, 20, 30}

cout << accumulate(numbers, numbers + 3, init)

cout << endl

}
Correct Answer

(C) 160