Exercise: Questions on Misc C++


Questions on Misc C++ : Question 1 :
Inline functions are useful when

Function is large with many nested loops
Function has many static variables
Function is small and we want to avoid function call overhead.
None of the above
Show Answer
Questions on Misc C++ : Question 2 :
#include<iostream>
using namespace std;
int x = 1;
void fun()
{
    int x = 2;
    {
        int x = 3;
        cout << ::x << endl;
    }
}
int main()
{
    fun();
    return 0;
}

1
2
3
0
Show Answer
Questions on Misc C++ : Question 3 :
Predict the output of following C++ program
#include<iostream>
using namespace std;

union A {
  int a;
  unsigned int b;
  A() { a = 10; }
  unsigned int getb() {return b;}
};

int main()
{
    A obj;
    cout << obj.getb();
    return 0;
}

Compiler Error: union can't have functions
Compiler Error: can't access private members of A
10
garbage value
Show Answer
Questions on Misc C++ : Question 4 :
Which of the following is true about inline functions and macros.

Inline functions do type checking for parameters, macros don't
Macros are processed by pre-processor and inline functions are processed in later stages of compilation.
Macros cannot have return statement, inline functions can.
Macros are prone to bugs and errors, inline functions are not.
All of the above
Show Answer
Questions on Misc C++ : Question 5 :
How can we make a C++ class such that objects of it can only be created using new operator? If user tries to create an object directly, the program produces compiler error.

Not possible
By making destructor private
By making constructor private
By making both constructor and destructor private
Show Answer
Questions on Misc C++ : Question 6 :
Would destructor be called, if yes, then due to which vector?
#include <iostream>
#include <vector>
using namespace std;

class a
{
public :
    ~a()
    {
        cout << "destroy";
    }
};
int main()
{
   vector <a*> *v1  = new vector<a*>;
   vector <a> *v2  = new vector<a>;
   return 0;
}

v1
v2
v1 and v2
no destructor call
Show Answer
Questions on Misc C++ : Question 7 :
#include<iostream>
using namespace std;

int x[100];
int main()
{
    cout << x[99] << endl;
}
This question is contributed by Sudheendra Baliga

Unpredictable
Runtime error
0
99
Show Answer
Questions on Misc C++ : Question 8 :
#include<iostream>
using namespace std;
int main ()
{
       int cin;
       cin >> cin;
       cout << "cin" << cin;
       return 0;
}
Thanks to Gokul Kumar for contributing this question.

error in using cin keyword
cin+junk value
cin+input
Runtime error
Show Answer
Questions on Misc C++ : Question 9 :
The associativity of which of the following operators is Left to Right, in C++ ?

Unary Operator
Logical not
Array element access
addressof
Show Answer
Questions on Misc C++ : Question 10 :
A member function can always access the data in __________ , (in C++).

the class of which it is member
the object of which it is a member
the public part of its class
the private part of its class
Show Answer