Vidyalelo
C++ Programming · all questions

C++ miscellaneous
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

1,008

Questions

34/51

Page

Pick an option on a question to see the right answer and solution.

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;
}

Select an option to see the answer and solution.

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;
};

Select an option to see the answer and solution.

What is the Run-Time Type Information?

Select an option to see the answer and solution.

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;
}

Select an option to see the answer and solution.

Which function is used to check whether a character is tab or a control code?

Select an option to see the answer and solution.

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;
}

Select an option to see the answer and solution.

What does the first parameter of the main function represent?

Select an option to see the answer and solution.

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; 
}

Select an option to see the answer and solution.

Which looping process is best used when the number of iterations is known?

Select an option to see the answer and solution.

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;
}

Select an option to see the answer and solution.

What is meant by ofstream in c++?

Select an option to see the answer and solution.

What is the use of reference member type in allocator?

Select an option to see the answer and solution.

Pick out the correct answer.

Select an option to see the answer and solution.

What are different operations are used in Pseudo-random number engines?

Select an option to see the answer and solution.

How many different ways are there to access an element of array classes at the ith position?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream> 
#include <vector> 
#include <forward_list>  
 
using namespace std; 
 
int main() 
{ 
    forward_list<int> fl1 = {1,2,3,4,5};
    for (int&c : fl1)  
        cout << c << " ";
    cout<<endl;
    forward_list<int>::iterator ptr = fl1.begin(); 
    fl1.erase_after(ptr);
    for (int&c : fl1)  
        cout << c << " ";
    cout<<endl;
    return 0; 
}

Select an option to see the answer and solution.

Which of the following is correct way of copying the values of pair p1 into other pair p2?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <vector>
#include<iterator>
using namespace std;
int main ()
{
    vector<int> myvector;
    for (int i = 1; i <= 10; i++)
    myvector.push_back(i);
    myvector.erase (myvector.begin() + 6);
    myvector.erase (myvector.begin(), myvector.begin() + 4);
    for (unsigned i = 0; i < myvector.size(); ++i)
    cout << ' ' << myvector[i];
    return 0;
}

Select an option to see the answer and solution.

Which is the correct syntax of constructing a bitset?

Select an option to see the answer and solution.

In what form does the STL provides heap?

Select an option to see the answer and solution.