If you want to move one step beyond the basic C++ questions and rock your dream interview, then I have brought an exquisite list of the most tricky and interesting C++ interview questions that every enthusiast must know. This will surely help to enhance your skills and boost your confidence in this language.
Now, if you are a beginner in C++ ,you might want to first understand the basic concepts of C++ programming language and then jump right back to this article to understand the content better.
With that being said, let's dive straight in and start with our most tricky C++ interview questions.
1. How to call some function before main() in C++ ?
Method 1. Using Global object
void funct(){
cout<<"I am executing before main()\n";
}
class CodingWithArt{
public:
CodingWithArt(){ // class constructor
funct(); // call your desired function here
}
};
CodingWithArt object; // declare a global object
int main(){
cout<<"main() will execute later\n";
}
Method 2. Using static member variable
int funct(){ // funct() has return type 'int' because value has to be assigned to 'variable'
cout<<"I am executing before main()\n";
return 1;
}
class CodingWithArt{
public:
static int variable;
};
int CodingWithArt::variable=funct(); // :: tells the compiler that variable belongs to class CodingWithArt
int main(){
cout<<"main() will execute later\n";
}
2. What is function hiding in C++?
class Base{ public: int func(int var){ return var+5; } }; class Derived:public Base { public: int func(){ return 5; } }; int main(){ Derived object; object.func(5); // compile time error }
3. How can a derived class access overloaded functions in base class( Function hiding) ?
Method 1. Using scope resolution operator
int main(){
Derived object;
object.Base::func(5);
}
Method 2. Through 'using' keyword
class Base{
public:
int func(int var){
return var+5;
}
};
class Derived:public Base{
public:
using Base::func;
int func(){
return 5;
}
};
int main(){
Derived object;
object.func(5); // works fine
}
4. Print "Hello World" 100 times but without using loop or recursion.
Now printing explicitly 100 times isn't really the solution.
We can use the concept of constructor and array in C++. Create a class and an array of objects of this class of size 100.
Inside the constructor print "Hello World". When the array is created, then the constructor will automatically be called for each object in the array.So simple yet so elegant.
class CodingWithArt{ public: CodingWithArt(){ // called 100 times
cout<<"Hello World"; } }; int main(){ CodingWithArt object[100]; }
5. What is object slicing in C++?
The concept of inheritance tells us that a derived class inherits the properties of base class, i.e adds the attributes of base class to its own unique attributes.
Now in C++, a derived class object can be assigned to a base class object. In this case the additional attributes of the derived class are sliced off from the object to form the base class object. This is referred to as object slicing.
For example, consider the code snipped given below :
class Base{
int baseVar;
};
class Derived:public Base{
int myVar;
}
int main(){
Derived derived;
Base base = derived; // object slicing
}
'derived' object has both the attributes : 'myVar' and 'baseVar' ( from Base).When we assign 'derived' to 'base' object then 'myVar' attribute is chopped off from the object and now 'base' object only contains 'baseVar' attribute.
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
2 comments
Click here for commentsawesome!!great work
Replybhai maje khade kar diye tune.... awsm work... keep up the good work.... lage raho munna bhai....
Reply