C Plus Plus Miscellaneous - Study Mode
[#771] What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
char* ptr
unsigned long int Test = sizeof(size_t(0) / 3)
cout << Test << endl
try
{
ptr = new char[size_t(0) / 3]
delete[ ] ptr
}
catch (bad_alloc &thebadallocation)
{
cout << thebadallocation.what() << endl
}
return 0
}
Correct Answer
(D) depends on compiler
[#772] What will be the output of the following C++ code? #include <iostream>
#include <exception>
using namespace std
void myunexpected ()
{
cout << "unexpected called
"
throw 0
}
void myfunction () throw (int)
{
throw 'x'
}
int main ()
{
set_unexpected (myunexpected)
try
{
myfunction()
}
catch (int)
{
cout << "caught int
"
}
catch (...)
{
cout << "caught other exception
"
}
return 0
}
Correct Answer
(D) both caught int & unexpected called
[#773] What will be the output of the following C++ code? #include <iostream>
using namespace std
int main ()
{
char first, second
cout << "Enter a word: "
first = cin.get()
cin.sync()
second = cin.get()
cout << first << endl
cout << second << endl
return 0
}
Correct Answer
(C) returns first 2 letter or number from the entered word
[#774] What will be the output of the following C++ code? #include <iostream>
#include <vector>
using namespace std
int main ()
{
vector<int> myvector
int * p
unsigned int i
p = myvector.get_allocator().allocate(5)
for (i = 0
i < 5
i++)
myvector.get_allocator().construct(&p[i], i)
for (i = 0
i < 5
i++)
cout << ' ' << p[i]
for (i = 0
i < 5
i++)
myvector.get_allocator().destroy(&p[i])
myvector.get_allocator().deallocate(p, 5)
return 0
}
Correct Answer
(B) 0 1 2 3 4
[#775] What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
try
{
try
{
throw 20
}
catch (char n)
{
cout << "Inner Catch
"
}
}
catch (int x)
{
cout << "Outer Catch
"
}
return 0
}
Correct Answer
(B) Outer Catch