1

Misc C++

Question 1
Inline functions are useful when
A
Function is large with many nested loops
B
Function has many static variables
C
Function is small and we want to avoid function call overhead.
D
None of the above
Misc C++    
Discuss it


Question 1 Explanation: 
Inline functions are generally used in place of small macros. They are substitute to macros and better than macros. See following for details. http://en.wikipedia.org/wiki/Inline_function#Comparison_with_macros
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;
}
A
1
B
2
C
3
D
0
Misc C++    
Discuss it


Question 2 Explanation: 
The value of ::x is 1. The scope resolution operator when used with a variable name, always refers to global variable.
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;
}
A
Compiler Error: union can't have functions
B
Compiler Error: can't access private members of A
C
10
D
garbage value
Misc C++    
Discuss it


Question 3 Explanation: 
Like struct and class, union can have methods. Like struct and unlike class, members of union are public by default. Since data members of union share memory, the value of b becomes same as a.
Question 4
Which of the following is true about inline functions and macros.
A
Inline functions do type checking for parameters, macros don't
B
Macros are processed by pre-processor and inline functions are processed in later stages of compilation.
C
Macros cannot have return statement, inline functions can.
D
Macros are prone to bugs and errors, inline functions are not.
E
All of the above
Misc C++    
Discuss it


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.
A
Not possible
B
By making destructor private
C
By making constructor private
D
By making both constructor and destructor private
Misc C++    
Discuss it


Question 5 Explanation: 
See the following example.
// Objects of test can only be created using new
class Test
{
private:
    ~Test() {}
friend void destructTest(Test* );
};
 
// Only this function can destruct objects of Test
void destructTest(Test* ptr)
{
    delete ptr;
}
 
int main()
{
    // create an object
    Test *ptr = new Test;
 
    // destruct the object
    destructTest (ptr);
 
    return 0;
}
See http://www.geeksforgeeks.org/private-destructor/ for more details.
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;
}
A
v1
B
v2
C
v1 and v2
D
no destructor call
Misc C++    
Discuss it


Question 7
#include<iostream>
using namespace std;

int x[100];
int main()
{
    cout << x[99] << endl;
}
This question is contributed by Sudheendra Baliga
A
Unpredictable
B
Runtime error
C
0
D
99
Misc C++    
Discuss it


Question 7 Explanation: 
The correct answer is c. In C++ all the uninitialized global variables are initialized to 0.
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.
A
error in using cin keyword
B
cin+junk value
C
cin+input
D
Runtime error
Misc C++    
Discuss it


There are 8 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice