CPlusPlus Interview Questions and Answers
Question - 1 : - What is C++?
Answer - 1 : - C++ is a programming language. It literally means "increased C", reflecting its nature as an evolution of the C language.
Question - 2 : - Is it necessary to already know another programming language before learning C++?
Answer - 2 : - Not necessarily. C++ is a simple and clear language in its expressions. It is true that a piece of code written with C++ may be seen by a stranger of programming a bit more cryptic than some other languages due to the intensive use of special characters ({}[]*&!|...), but once one knows the meaning of such characters it can be even more schematic and clear than other languages that rely more on English words.
Also, the simplification of the input/output interface of C++ in comparison to C and the incorporation of the standard template library in the language, makes the communication and manipulation of data in a program written in C++ as simple as in other languages, without losing the power it offers.
Question - 3 : - Does delete p delete the pointer p, or the pointed-to-data *p?
Answer - 3 : - The keyword should really be delete_the_thing_pointed_to_by. The same abuse of English occurs when freeing the memory pointed to by a pointer in C: free(p) really means free_the_stuff_pointed_to_by(p).
Question - 4 : - Can I free() pointers allocated with new? Can I delete pointers
allocated with malloc()?
Answer - 4 : - No!
It is perfectly legal, moral, and wholesome to use malloc() and delete in the
same program, or to use new and free() in the same program. But it is illegal, immoral, and despicable to call free() with a pointer allocated via new, or to call delete on a pointer allocated via malloc().
Beware! I occasionally get e-mail from people telling me that it works OK for
them on machine X and compiler Y. That does not make it right! Sometimes
people say, "But I'm just working with an array of char." Nonetheless do not
mix malloc() and delete on the same pointer, or new and free() on the same
pointer! If you allocated via p = new char[n], you must use delete[] p; you
must not use free(p). Or if you allocated via p = malloc(n), you must use
free(p); you must not use delete[] p or delete p! Mixing these up could cause a catastrophic failure at runtime if the code was ported to a new machine, a new compiler, or even a new version of the same compiler.
You have been warned.
Question - 5 : - Count Number of characters using Pointers?
Answer - 5 : - #include
void main()
{
int Char_Count(char *get_string);
char *strcount "THIS IS A STRING";
int int_word_count Char_Count(strcount);
cout<<"word count is "<
Question - 6 : - What is the difference between realloc() and free()?
Answer - 6 : - 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.
Question - 7 : - What is function overloading and operator overloading?
Answer - 7 : - Function overloading: C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types are concerned). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types.
Operator overloading allows existing C++ operators to be redefined so that they work on objects of user-defined classes. Overloaded operators are syntactic sugar for equivalent function calls. They form a pleasant facade that doesn't add anything fundamental to the language (but they can improve understandability and reduce maintenance costs).
Question - 8 : - What is the difference between declaration and definition?
Answer - 8 : - The declaration tells the compiler that at some later point we plan to present the definition of this declaration.
E.g.: void stars () //function declaration
The definition contains the actual implementation.
E.g.: void stars () // declarator
{
for(int j=10; j > =0; j--) //function body
cout << *;
cout << endl; }
Question - 9 : - What are the advantages of inheritance?
Answer - 9 : - It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional.
Question - 10 : - How do you write a function that can reverse a linked-list?
Answer - 10 : - 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;
}
}