C Plus Plus Miscellaneous - Study Mode

[#756] 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[] = {10, 20, 20, 20, 30, 30, 20, 20, 10}
vector<int> myvector (myints, myints + 9)
vector<int> :: iterator it
it = unique (myvector.begin(), myvector.end())
myvector.resize( distance(myvector.begin(), it) )
unique (myvector.begin(), myvector.end(), myfunction)
for (it = myvector.begin()
it != myvector.end()
++it)
cout << ' ' << *it
return 0
}
Correct Answer

(A) 10 20 30 20 10

[#757] What will be the output of the following C++ code? #include<iostream>
#include<iterator>
#include<vector>
using namespace std
int main()
{
vector<int> ar = { 1, 2, 3, 4, 5 }
vector<int>::iterator ptr = ar.begin()
ptr = advance(ptr, 2)
cout << *ptr << endl
return 0
}
Correct Answer

(C) Error

[#758] What will be the output of the following C++ code? #include <iostream>
#include <algorithm>
#include <vector>
using namespace std
int main ()
{
int first[] = {5, 10, 15, 20, 25}
int second[] = {50, 40, 30, 20, 10}
vector<int> v(10)
vector<int> :: iterator it
sort (first, first + 5)
sort (second, second + 5)
it = set_union (first, first + 5, second, second + 5, v.begin())
v.resize(it-v.begin())
for (it = v.begin()
it != v.end()
++it)
cout << ' ' << *it
cout << '
'
return 0
}
Correct Answer

(D) 20 25

[#759] What will be the output of the following C++ code? #include <iostream>
using namespace std
class X
{
public:
int a
void f(int b)
{
cout<< b << endl
}
}
int main()
{
int X :: *ptiptr = &X :: a
void (X :: * ptfptr) (int) = &X :: f
X xobject
xobject.*ptiptr = 10
cout << xobject.*ptiptr << endl
(xobject.*ptfptr) (20)
}
Correct Answer

(A) 10 20

[#760] What will be the output of the following C++ code? #include <iostream>
#include <string>
#include <sstream>
using namespace std
int main ()
{
string mystr
float price = 0
int quantity = 0
cout << "Enter price: "
getline (cin, mystr)
stringstream(mystr) >> price
cout << "Enter quantity: "
getline (cin, mystr)
stringstream(mystr) >> quantity
cout << "Total price: " << price * quantity << endl
return 0
}
Correct Answer

(B) Depends on value you enter