C Plus Plus Miscellaneous - Study Mode

[#926] What will be the output of the following C++ code? #include <cstdlib>
#include <iostream>
using namespace std
class X
{
public:
void* operator new(size_t sz) throw (const char*)
{
void* p = malloc(sz)
if (p == 0)
throw "malloc() failed"
return p
}
void operator delete(void* p)
{
cout << "X :: operator delete(void*)" << endl
free(p)
}
}
class Y
{
int filler[100]
public:
void operator delete(void* p, size_t sz) throw (const char*)
{
cout << "Freeing " << sz << " bytes" << endl
free(p)
}
}
int main()
{
X* ptr = new X
delete ptr
Y* yptr = new Y
delete yptr
}
Correct Answer

(D) Both X::operator delete(void*) & Depends on the compiler

[#927] What will be the output of the following C++ code? #include <iostream>
#include <vector>

using namespace std
int main()
{
vector<int> v
for (int i = 1
i <= 5
i++)
v.push_back(i)
cout<<v.size()<<endl
v.resize(4)
cout<<v.size()<<endl
return 0
}
Correct Answer

(A) 5 4

[#928] What will be the output of the following C++ code? #include <iostream>
#include <string>
#include <tuple>
using namespace std
int main()
{
tuple <int, char, string> tp = {"Hello", 1, 's'}
return 0
}
Correct Answer

(B) Compile-time error

[#929] What will be the output of the following C++ code? #include <iostream>
#include <array>

using namespace std
int main(int argc, char const *argv[])
{
int arr1[5] = {1,2,3,4,5}
int arr2[5] = {6,7,8,9,10}
arr1.swap(arr2)
for(int i=0
i<5
i++)
cout<<arr1[i]<<" "
cout<<endl
for(int i=0
i<5
i++)
cout<<arr2[i]<<" "
cout<<endl
return 0
}
Correct Answer

(C) Error

[#930] What will be the output of the following C++ code? #include <iostream>
using namespace std
class Parent
{
public:
Parent (void)
{
cout << "Parent()
"
}
Parent (int i)
{
cout << "Parent("<< i << ")
"
}
Parent (void)
{
cout << "~Parent()
"
}
}
class Child1 : public Parent { }
class Child2 : public Parent
{
public:
Child2 (void)
{
cout << "Child2()
"
}
Child2 (int i) : Parent (i)
{
cout << "Child2(" << i << ")
"
}
~Child2 (void)
{
cout << "~Child2()
"
}
}
int main (void)
{
Child1 a
Child2 b
Child2 c(42)
return 0
}
Correct Answer

(B) error