C++ Practice Questions
C++ Practice Questions
1. If a global function is declared as a friend of some class, then such a function has
‘this’ pointer or not? -NO
2. If a member function of some class is declared as a friend of another class, then such a
function has ‘this’ pointer or not? -YES
3. If a global function is declared as a friend of some class, then such a function can be
called using an object of type of class to which it is declared as a friend and the dot
operator? -NO
4. All types of member functions of some class can be defined inside a class-NO.
Restrictions apply.
5. All types of functions can be made Inline. – NO. Restrictions apply.
6. Constructor functions can be explicitly called using an object of the class and dot
operator. -NO. Constructor functions are implicitly called.
7. Constructor functions can be declared as private or protected member of some class. –
NO
8. Constructor functions cannot have parameters. – FALSE.
9. Member functions of some class (Other than constructor functions) can be declared as
private or protected members of the class. -YES
10. Private or Protected data members or member functions of some class cannot be
accessed by other members of the same class. –FALSE
11. Private or Protected data members or member functions of some class can-not be
accessed by any function which is not a member of the same class. –TRUE
12. Structures cannot have functions as members. – FALSE
13. Structures cannot have access specifiers. – FALSE
14. If no access specifier is mentioned for some members of a structure, then by default
such members of the structure become private members. – FALSE. They become
public members.
15. If no access specifier is mentioned for some members of a class, then by default such
members of the class become private members. – TRUE.
16. A class must have member functions. - FALSE
17. Which of the following is true in the context of inline functions in C++?
i. It increases code size but decreases control-transfer overhead. - TRUE
ii. It decreases code size but increases control-transfer overhead. - FALSE
iii. If a function is made inline it is guaranteed to be inline. – FALSE. Inline is
only a request to the compiler.
What will be the output? int main()
#include<iostream> a) x=10 y=5
{ b) x=5 y=10
using namespace std;
demo obj; c) error
class demo{
int y; obj.x=10; d) x=10 y=0
public: obj.y=5;
int x; cout<<"x="<<obj.x<<endl;
}; cout<<"y="<<obj.y<<endl;
return 0;
}