C++ Interview Question and Answers Part 2

 

C++ Interview Question and Answers Part 2

Here you can find the most frequently asked C++ Interview Question asked in interviews of reputed IT companies with Answers (part 2).

1. How do you write a function that can reverse a linked-list?

Answer:

void reverselist(void)
{
	if(head==0)
		return;
	if(head->next==0)
		return;
	if(head->next==tail)
	{
		head->next = 0;
		tail->next = head;
	}
	else
	{
		node* pre = head;
		node* cur = head->next;
		node* curnext = cur->next;
		head->next = 0;
		cur-> next = head;
		for(; curnext!=0; )
		{
			cur->next = pre;
			pre = cur;
			cur = curnext;
			curnext = curnext->next;
		}
		curnext->next = cur;
	}
}

2. How do I initialize a pointer to a function?

Answer: This is the way to initialize a pointer to a function.

void fun(int a)
{
}
void main()
{
	void (*fp)(int);
	fp=fun;
	fp(1);
}

3. How do you link a C++ program to C functions?

Answer: By using the extern “C” linkage specification around the C function declarations.

4. Explain the scope resolution operator.

Answer: It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.

5. What are the differences between a C++ struct and C++ class?

Answer: The default member and base-class access specifier are different.

6. How many ways are there to initialize an int with a constant?

Answer:

Two.
There are two formats for initializers in C++ as shown in the example that follows. The first format uses traditional C notation. The second format uses the constructor notation.
int foo = 123;
int bar (123);

7. How does throwing and catching exceptions differ from using setjmp and longjmp?

Answer: The throw operation calls the destructors for automatic objects instantiated since entry to the try block.

8. What is a default constructor?

Answer: Default constructor WITH arguments,

class B 
{
	public: B (int m = 0) : n (m) 
	{
		
	} 
	int n; 
}; 
int main(int argc, char *argv[])
{
	B b; 
	return 0; 
}

9. What is a conversion constructor?

Answer: A constructor that accepts one argument of a different type.

10. What is the difference between a copy constructor and an overloaded assignment operator?

Answer: A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

This tutorial discusses the most frequently asked C++ Interview Question and Answers (Part -2). Subscribe to our YouTube channel for more videos and like the Facebook page for regular updates.

Leave a Comment

Your email address will not be published. Required fields are marked *