C++ Interview Questions and Answers Part (2)

on 8:12 AM

Check out this collection of C++ Interview Questions and Answers.This is the new series on C++ Interview Questions and Answers from very basic level to expert level.This is second first episode of a series.
  1. What is an orthogonal base class?
  2. What is a container class? What are the types of container classes?
  3. How do you write a function that can reverse a linked-list?
  4. What is polymorphism?
  5. How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
  6. How can you tell what shell you are running on UNIX system?
  7. What is Boyce Codd Normal form?
  8. What is pure virtual function?
  9. Write a Struct Time where integer m, h, s are its members
  10. How do you traverse a Btree in Backward in-order?
  11. What is the two main roles of Operating System?
  12. In the derived class, which data member of the base class are visible?
  13. How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
  14. What is the difference between realloc() and free()?
  15. What is function overloading and operator overloading?
  16. What is the difference between declaration and definition?
  17. What are the advantages of inheritance?
  18. How do you write a function that can reverse a linked-list?
  19. What do you mean by inline function?
  20. Write a program that ask for user input from 5 to 9 then calculate the average
 What is an orthogonal base class?
If two base classes have no overlapping methods or data they are said to be independent of, or orthogonal to each other. Orthogonal in the sense means that two classes operate in different dimensions and do not interfere with each other in any way. The same derived class may inherit such classes with no difficulty.
What is a container class? What are the types of container classes?
A container class is a class that is used to hold objects in memory or external storage. A container class acts as a generic holder. A container class has a predefined behavior and a well-known interface. A container class is a supporting class whose purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; when the container is holding a group of objects that are all the same, the container is called a homogeneous container.
How do you write a function that can reverse a linked-list?
Answer1:

void reverselist(void)
{
 if(head==0)
  return;
 if(head-next) 
//if head->next ==0 should return 
head instead of 0;
return 0;

{
node* prev = head;
node* curr = head->next;
node* next = curr->next;

for(; next!=0; )
{
curr->next = prev;
prev = curr;
curr = next;
next = next->next;
}
curr->next = prev;

head->next = 0;
head = curr;
}

return head;
}

 What is polymorphism?
Polymorphism is the idea that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects.
How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 nodes 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.
How can you tell what shell you are running on UNIX system?
You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.
What is Boyce Codd Normal form?
A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form a->b, where a and b is a subset of R, at least one of the following holds:

* a->b is a trivial functional dependency (b is a subset of a)
* a is a superkey for schema R
What is pure virtual function?
A class is made abstract by declaring one or more of its virtual functions to be pure. A pure virtual function is one with an initializer of = 0 in its declaration
Write a Struct Time where integer m, h, s are its members
struct Time
{
int m;
int h;
int s;
};
How do you traverse a Btree in Backward in-order?
Process the node in the right subtree Process the root Process the node in the left subtree
 What is the two main roles of Operating System?
As a resource manager As a virtual machine
In the derived class, which data member of the base class are visible?
In the public and protected sections.
How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 nodes 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
What is the difference between realloc() and free()?
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.
What is function overloading and operator overloading?
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)
What is the difference between declaration and definition?
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; }
What are the advantages of inheritance?
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.
How do you write a function that can reverse a linked-list?
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;
 }
}
What do you mean by inline function?
The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application's performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.
Write a program that ask for user input from 5 to 9 then calculate the average
#include "iostream.h"
int main() {
int MAX = 4;
int total = 0;
int average;
int numb;

for (int i=0; i<< 
 "Please enter your input between 5 and 9: ";
cin >> numb;
while ( numb<5 || numb>9) {
cout << "Invalid input, please re-enter: ";
cin >> numb;
}
total = total + numb;
}
average = total/MAX;
cout << "The average number is: "
      << average << "\n";
return 0;
}






0 comments:

Post a Comment