61. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
class base { virtual void dummy() {} };
class derived: public base { int a; };
int main ()
{
try
{
base * pba = new derived;
base * pbb = new base;
derived * pd;
pd = dynamic_cast<derived*>(pba);
if (pd == 0)
cout << "Null pointer on first type-cast" << endl;
pd = dynamic_cast<derived*>(pbb);
if (pd == 0)
cout << "Null pointer on second type-cast" << endl;
}
catch (exception& e)
{
cout << "Exception: " << e.what();
}
return 0;
}
#include <iostream>
#include <exception>
using namespace std;
class base { virtual void dummy() {} };
class derived: public base { int a; };
int main ()
{
try
{
base * pba = new derived;
base * pbb = new base;
derived * pd;
pd = dynamic_cast<derived*>(pba);
if (pd == 0)
cout << "Null pointer on first type-cast" << endl;
pd = dynamic_cast<derived*>(pbb);
if (pd == 0)
cout << "Null pointer on second type-cast" << endl;
}
catch (exception& e)
{
cout << "Exception: " << e.what();
}
return 0;
}62. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
struct A
{
virtual ~A()
{
cout << "~A()" << endl;
}
void operator delete[](void* p, size_t)
{
cout << "A :: operator delete[]" << endl;
delete [] p;
}
};
struct B : A
{
void operator delete[](void* p, size_t)
{
cout << "B :: operator delete[]" << endl;
delete [] p;
}
};
int main()
{
A* bp = new B[3];
delete[] bp;
};
#include <iostream>
using namespace std;
struct A
{
virtual ~A()
{
cout << "~A()" << endl;
}
void operator delete[](void* p, size_t)
{
cout << "A :: operator delete[]" << endl;
delete [] p;
}
};
struct B : A
{
void operator delete[](void* p, size_t)
{
cout << "B :: operator delete[]" << endl;
delete [] p;
}
};
int main()
{
A* bp = new B[3];
delete[] bp;
};63. What is the Run-Time Type Information?
64. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;
void myterminate ()
{
cerr << "terminate handler called";
abort();
}
int main (void)
{
set_terminate (myterminate);
throw 0;
return 0;
}
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;
void myterminate ()
{
cerr << "terminate handler called";
abort();
}
int main (void)
{
set_terminate (myterminate);
throw 0;
return 0;
}65. Which function is used to check whether a character is tab or a control code?
66. What will be the output of the following C++ code?
#include
int main ()
{
FILE * p;
long size;
p = fopen ("test.txt", "rb");
if (p == NULL)
perror ("Error opening file");
else
{
fseek (p, 0, SEEK_END);
size = ftell (p);
fclose (p);
printf (" %ld\n", size);
}
return 0;
}
#include
int main ()
{
FILE * p;
long size;
p = fopen ("test.txt", "rb");
if (p == NULL)
perror ("Error opening file");
else
{
fseek (p, 0, SEEK_END);
size = ftell (p);
fclose (p);
printf (" %ld\n", size);
}
return 0;
} 67. What does the first parameter of the main function represent?
68. 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);
vector<int> :: iterator i;
i = v.begin();
*i = 3;
for (i = v.begin(); i != v.end(); ++i)
cout << *i << " ";
cout<<endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
for (int i = 1; i <= 5; i++)
v.push_back(i);
vector<int> :: iterator i;
i = v.begin();
*i = 3;
for (i = v.begin(); i != v.end(); ++i)
cout << *i << " ";
cout<<endl;
return 0;
}69. Which looping process is best used when the number of iterations is known?
70. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class MyException
{
public:
MyException(int value) : mValue(value)
{
}
int mValue;
};
class MyDerivedException : public MyException
{
public:
MyDerivedException(int value, int anotherValue) : MyException(value), mAnotherValue(anotherValue)
{
}
int mValue;
int mAnotherValue;
};
void doSomething()
{
throw MyDerivedException(10,20);
}
int main()
{
try
{
doSomething();
}
catch (MyDerivedException &exception)
{
cout << "\nCaught Derived Class Exception\n";
}
catch (MyException &exception)
{
cout << "\nCaught Base Class Exception\n";
}
return 0;
}
#include <iostream>
using namespace std;
class MyException
{
public:
MyException(int value) : mValue(value)
{
}
int mValue;
};
class MyDerivedException : public MyException
{
public:
MyDerivedException(int value, int anotherValue) : MyException(value), mAnotherValue(anotherValue)
{
}
int mValue;
int mAnotherValue;
};
void doSomething()
{
throw MyDerivedException(10,20);
}
int main()
{
try
{
doSomething();
}
catch (MyDerivedException &exception)
{
cout << "\nCaught Derived Class Exception\n";
}
catch (MyException &exception)
{
cout << "\nCaught Base Class Exception\n";
}
return 0;
}Read More Section(C plus plus miscellaneous)
Each Section contains maximum 100 MCQs question on C plus plus miscellaneous. To get more questions visit other sections.
- C plus plus miscellaneous - Section 1
- C plus plus miscellaneous - Section 2
- C plus plus miscellaneous - Section 3
- C plus plus miscellaneous - Section 4
- C plus plus miscellaneous - Section 5
- C plus plus miscellaneous - Section 6
- C plus plus miscellaneous - Section 8
- C plus plus miscellaneous - Section 9
- C plus plus miscellaneous - Section 10
- C plus plus miscellaneous - Section 11
