C++ Interview Question and Answers Part 1

 

C++ Interview Question and Answers Part 1

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

1. What is the difference between declaration and definition?

Answer:

The declaration tells the compiler that at some time later point we plan to present the definition of this declaration.

void stars () //function declaration

The definition contains the actual implementation.

void stars () // function declaration
{
	//function body
	for(int j=10; j > =0; j--) 
		cout << *;
	cout << endl; 
} 

2. How do you find out if a linked-list has an end? (i.e. the list is not a cycle).

Answer: You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 node each time. If there is a cycle, the one that goes 2 nodes each time will eventually meet the one that goes slower. If that is the case, then you will know the linked-list is a cycle.

3. What is the difference between realloc() and free()?

Answer: The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur. The realloc() subroutine changes the size of the block of memory pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer specified by the Pointer parameter must have been created with the malloc, calloc(), or realloc() subroutines and not been deallocated with the free or realloc() subroutines. Undefined results occur if the Pointer parameter is not a valid pointer.

4. Anything wrong with this code?
T *p = 0;
delete p;

Answer: Yes, the program will crash in an attempt to delete a null pointer.

5. How do you decide which integer type to use?

Answer: It depends on our requirements. When we are required an integer to be stored in 1 byte (means
less than or equal to 255) we use short int, for 2 bytes we use int, for 8 bytes we use long int. A char is for 1-byte integers, a short is for 2-byte integers, an int is generally a 2-byte or 4-byte integer (though not necessarily), a long data type is a 4-byte integer, and a long data type is an 8-byte integer.

6. What are the advantages of inheritance?

Answer: It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducing problems after a system becomes functional.

This tutorial discusses the most frequently asked C++ Interview Question and Answers (Part -1). 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 *