Sei sulla pagina 1di 4

Given a Binary Search Tree, write a program to print the kth smallest element without using any static/global

variable. You cant pass the value k to any function also. What are the 4 basics of OOP? Define Data Abstraction. What is its importance? Given an array of size n. It contains numbers in the range 1 to n. [...]

C++ interview questions and answers


By admin | December 8, 2007

1.

2. 3. 4. 5.

What is the most efficient way to reverse a linklist? How to sort & search a single linklist? Which is more convenient - single or double-linked linklist? Discuss the trade-offs? What about XORlinked linklist? How does indexing work? char s[10]; s=Hello; printf(s); What will be the output? Is there any error with this code? What is the difference between char s[]=Hello; char *s=Hello; Please give a clear idea on this? Why do we pass a reference for copy constructors? If it does shallow copy for pass by value (user defined object), how will it do the deep copy? What is the difference between shallow copy & deep copy? What is the difference between strcpy and memcpy? What rule should we follow when choosing between these two? If we declare two variable and two applications are using the same variable, then what will its value be, will it be the same?

#1. The most efficient way to reverse a linked list is thorough recursion. example: void rev(struct node *list) { if (list->next==NULL)return; rev(list->next); printf(%d,list->info); } #2. Searching a linked list is a very easy concept, just traverse each node till the next node pointer value is NULL and see if the value in the node matched the one we have to search or not. Sorting a linked list can be a bit complicated though, we can use bubble sort technique and swap values using functions.

#3. A singly linked list can be very easy to handle but it can be traversed in one direction only whereas a douly linked list can be traversed in both reverse and forward direction. Thus a doubly linked list is advantageous over singly linked list. #5. The error here is that the assignment used here that is s=hello; is not correct. either we will have to change the declaration to char *s; or we will have to use the function strcpy(s,hello); to do such assignment.

6) First one is a const pointer to a const memory Where second one is dynamic pointer to a const memory 8) User has created its own class now two ways to explain this. if there is no pointer use as an attribute of the class, than both deep copy & shallow copy are the same but if their is any pointer use as an attribute than behaviour of both will be diff.shallow copy will only create new reference and will refer it to the same memory location where the previous one is pointing, means single memory location poited by two pointers..where deep copy will also allocate new memory for the new reference and then will copy the contents of the first memory location to the second one. 10) it depends on the scope of the variable , if its scope is of application level than both will get different copies of the variablebut if it is a system variable or scope not restricted to a single application than both will recieve same value/copy.

1. 2. 3. 4. 5. 6. 7.

8. 9.

What is a void return type? How is it possible for two String objects with identical values not to be equal under the == operator? What is the difference between a while statement and a do statement? Can a for statement loop indefinitely? How do you link a C++ program to C functions? How can you tell what shell you are running on UNIX system? Using getenv function(get environment) How do you find out if a linked-list has an end? (i.e. the list is not a cycle) Check the next(pointer) of each node, if it is NULL that is the end node. Linked list will be a loop if there is no NULL value in any of the nodes pointer. How do you write a function that can reverse a linked-list?

Can a copy constructor accept an object of the same class as parameter, instead of reference of the object? It can accept but it will go on infinite loop 10. What is a local class? If a class is defined within a function that class is a local class which is local to the function and it can not be accessed outside the class. 11. What is a nested class? Class within a class.

12. What are the access privileges in C++? What is the default access level? Public,private,protected. Public can be accesed by any subclass or evevn from outside the scope of the class Protected cean accesed only from within the scope of the class n its subclasses Private can be used wihin the scope of that class but it is forbidden to any subclass By default access is private. What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages? Multiple Inheritance means there is only one base class and multiple no. of derived class are inheriting it. Virtual Inheritance: Suppose class A is inherited by class B and class C. Another class D is inheriting class B and C. Now class D is indirectly inheriting class A that to two times. This is an ambiguity. To avoid this we have amke the class A as Virtual base class and class B and C will have inherit is virtually. Now when D is inheriting both B and C, it will get only one copy of A rather than 2 copies(as in the earlier case). This virtual Inheritance. 13. What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages? In a diamond hierarchy, virtual inheritance allows a derive class D to share only one copy of the common base case A. This is done by using the keyword virtual on the declaration. For example class A{} class B : virtual public A {} class C : virtual public A {} class D : public B, public C {} // class D only contains one copy of A. Advantage: The derived class contains only one copy of the base class. Disadvantage: The runtime cost is similar to the one of a virtual function. The compiler inserts a pointer (in D) that points to the location of the base A. And it has to dereference it, whenever is needed. 14. How do you access the static member of a class? static data member -you can access thru member function or thru class name using scope resolution operator. static member function:-you can access thru class name using scope resolution operator. 15. What does extern int func(int*, Foo) accomplish?

Question:1 void doesnt return any value Question:2 By using an operator overloading we find the strings are same or not Question:3 while check the condition in entry,do-while check the condition at exit of the loop statement ie., do-while works atleast once

4. Can a for statement loop indefinitely? YES, leave conditional expression blank ie. in the format for(i=0;;i++) 5.by using extern C {} construct extern "C" is meant to be recognized by a C++ compiler and to notify the compiler that the noted function is (or to be) compiled in C style.

Potrebbero piacerti anche