C Plus Plus Miscellaneous - Study Mode
[#721] What will be the output of the following C++ code? #include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std
int main()
{
srand((unsigned)time(0))
int ran
int low = 1, high = 10
int range = (high - low) + 1
for(int index = 0
index < 1
index++)
{
ran = low + int(range * rand() / (RAND_MAX + 1.0))
cout << ran << endl
}
}
Correct Answer
(A) 1
[#722] What will be the output of the following C++ code? #include <iostream>
#include <utility>
#include <string>
using namespace std
int main ()
{
pair <int,int> p1(1,2)
pair <int,int> p2(3,4)
cout<<"Pair(first,second) = ("<<p1.first<<","<<p1.second<<")
"
p1.swap(p2)
cout<<"Pair(first,second) = ("<<p1.first<<","<<p1.second<<")
"
return 0
}
Correct Answer
(A) Pair(first,second) = (1,2) Pair(first,second) = (3,4)
[#723] 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 = next(ptr, 3)
cout << *ptr << endl
return 0
}
Correct Answer
(D) 4
[#724] What will be the output of the following C++ code? #include <iostream>
#include <exception>
using namespace std
struct MyException : public exception
{
const char * what () const throw ()
{
return "C++ Exception"
}
}
int main()
{
try
{
throw MyException()
}
catch(MyException& e)
{
cout << "Exception caught" << std::endl
cout << e.what() << std::endl
}
catch(std::exception& e)
{
}
}
Correct Answer
(C) Exception caught C++ Exception
[#725] What will be the output of the following C++ code? #include <iostream>
#include <algorithm>
#include <vector>
using namespace std
int main ()
{
vector<int> myvector (5)
fill (myvector.begin(), myvector.begin() + 4, 5)
fill (myvector.begin() + 3,myvector.end() - 2, 8)
for (vector<int> :: iterator it = myvector.begin()
it != myvector.end()
++it)
cout << ' ' << *it
return 0
}
Correct Answer
(A) 5 5 5 5 0