C++ Misc

Question 1
Inline functions are useful when
Cross
Function is large with many nested loops
Cross
Function has many static variables
Tick
Function is small and we want to avoid function call overhead.
Cross
None of the above


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;
}
Tick
1
Cross
2
Cross
3
Cross
0


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;
}
Cross
Compiler Error: union can\'t have functions
Cross
Compiler Error: can\'t access private members of A
Tick
10
Cross
garbage value


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.
Cross
Inline functions do type checking for parameters, macros don\'t
Cross
Macros are processed by pre-processor and inline functions are processed in later stages of compilation.
Cross
Macros cannot have return statement, inline functions can.
Cross
Macros are prone to bugs and errors, inline functions are not.
Tick
All of the above


Question 4-Explanation: 
An inline function is a normal function which is defined by the keyword inline. It is a short function which is expanded by the compiler, And its arguments are evaluated only once. An inline functions are the short length functions that are automatically made the inline functions without using the inline keyword inside the class. Inline function do type checking for parameters whereas macros do not check the parameter. Macros are processed by pre-processor and inline functions are processed in later stages of compilation. Macros function can not have return statement, whereas inline functions have return function. And Macros are prone to bugs and errors, inline functions are not. So, option (E) is correct.
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.
Cross
Not possible
Tick
By making destructor private
Cross
By making constructor private
Cross
By making both constructor and destructor private


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;
}
Cross
v1
Cross
v2
Cross
v1 and v2
Tick
no destructor call


Question 7
#include<iostream>
using namespace std;

int x[100];
int main()
{
    cout << x[99] << endl;
}
This question is contributed by Sudheendra Baliga
Cross
Unpredictable
Cross
Runtime error
Tick
0
Cross
99


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.
Cross
error in using cin keyword
Tick
cin+junk value
Cross
cin+input
Cross
Runtime error


Question 9
The associativity of which of the following operators is Left to Right, in C++ ?
Cross
Unary Operator
Cross
Logical not
Tick
Array element access
Cross
addressof


Question 9-Explanation: 
Array element access has left to right associativity in C++. So, option (C) is correct.
Question 10
A member function can always access the data in __________ , (in C++).
Tick
the class of which it is member
Cross
the object of which it is a member
Cross
the public part of its class
Cross
the private part of its class


Question 10-Explanation: 
A member function can access it\'s class member variables, irrespective of the access specifier in which the member variable is declared.So, a member function can always access the data in the class of which it is a member. So, option (A) is correct.
There are 15 questions to complete.

  • Last Updated : 29 Jul, 2020

Share your thoughts in the comments
Similar Reads