C Plus Plus Miscellaneous - Study Mode

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

int main ()
{
int myints[] = {10, 20, 30, 30, 20, 10, 10, 20}

vector<int> v(myints, myints + 8)

sort (v.begin(), v.end())

vector<int> :: iterator low, up

low = lower_bound (v.begin(), v.end(), 20)

up = upper_bound (v.begin(), v.end(), 20)

cout << (low - v.begin()) << ' '

cout << (up - v.begin()) << '
'

return 0

}
Correct Answer

(A) 3 6

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

int main ()
{
vector<int> myvector

for (int i = 1

i < 4

++i)
myvector.push_back(i*10)

ostream_iterator<int> out_it (cout,", ")

copy ( myvector.begin(), myvector.end(), out_it )

return 0

}
Correct Answer

(C) 10, 20, 30,

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

double division(int a, int b)
{
if ( b == 0 )
{
throw "Division by zero condition!"

}
return (a / b)

}
int main ()
{
int x = 50

int y = 0

double z = 0

try
{
z = division(x, y)

cout << z << endl

}
catch (const char* msg)
{
cout << msg << endl

}
return 0

}
Correct Answer

(C) Division by zero condition!

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

int main()
{
Valarray<int> varr = { 1, 2, 3, 4, 5 }

for (int &x: varr) cout << x << " "

cout<<endl

varr = varr.cshift(-3)

for (int &x: varr) cout << x << " "

return 0

}
Correct Answer

(C) 1 2 3 4 5 3 4 5 1 2

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

struct A
{
private:
int i, j, k

public:
int f()

void g()

}

int A :: f()
{
return i + j + k

}
void A :: g()
{
i = j = k = 0

}
class B
{
int i, j, k

public:
int f()

void g()

}

int B :: f()
{
return i + j + k

}
void B :: g()
{
i = j = k = 0

}
int main()
{
A a

B b

a.f()

a.g()

b.f()

b.g()

cout << "Identical results would be produced"

}
Correct Answer

(B) Identical results would be produced