Most Tricky C++ Interview Questions - Part 1



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++ ?

Generally everyone knows that main() is the first method to be called on program execution. So how can this be done?
Well, here I provide two methods to do this :

Method 1. Using Global object

A global object is always created before the program starts and it lives for the entire execution of the program. Now if we create a global object, as soon as it is created the class construcor will be called and inside the constructor we can call any desired function.

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

The lifetime of static variables doesn’t depend on the execution: they always exist until the program is terminated. This leads to the unique property that they can be potentially evaluated and initialized at compile time.
Static class variables are always initialized outside the class using the membership label '::' .


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++?


In C++, if base class and derived class have functions with same name and same/different signature (which we generally refer to as overloading), the all the overloaded functions in base class are hidden by default in derived class.This concept is called as function hiding.

For example, consider the code snippet given below :

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
}

This program will not compile because the derived class redefines the function func() and therefore func(int var) in Base is hidden in Derived class. To find out the workaround for this problem, checkout the next question.


3. How can a derived class access overloaded functions in base class( Function hiding) ?


As discussed in the previous question, overloaded functions in base class are hidden by default in derived class.
Below I have provided two methods using which derived class can access these overloaded functions.

Method 1. Using scope resolution operator

Considering the code segment provided in the previous problem, we can use scope resoulution operator inside main() when we call the function func(int var) as shown below.
'::' tells the compiler that func(int var) methods belongs to base class.
 
int main(){
	Derived object;
	object.Base::func(5);
}

Method 2. Through 'using' keyword

'using' keyword is used in C++ to import contents of a class from its scope to another class scope. 

Consider the code snippet below :

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
}

Focus on : using Base::func;

This imports all the functions named func() from Base class scope to derived class scope. Therefore now a derived class object can directly call any func() defined in the base class scope.


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.

This brings us to the end of Part-1 of this series. Also check out the other parts in the same series to find more challenging C++ interview questions :

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. 

Happy learning!!

2 comments

Click here for comments
Selena_
admin
October 29, 2020 at 3:35 PM ×

awesome!!great work

Reply
avatar
Unknown
admin
November 12, 2020 at 4:52 PM ×

bhai maje khade kar diye tune.... awsm work... keep up the good work.... lage raho munna bhai....

Reply
avatar