Index | Prev | Next

C & C++ Programming

1. Explain Copy Constructor.

It is a constructore which initializes it's object member variable with another object of the same class. If you don't implement a copy constructor in your class, the compiler automatically does it.

2. When do you call copy constructors?

Copy constructors are called in these situations:
i.)when compiler generates a temporary object
ii.)when a function returns an object of that class by value
iii.)when the object of that class is passed by value as an argument to a function
iv.)when you construct an object based on another object of the same class

3. Name the implicit member functions of a class.

i.) default ctor
ii.) copy ctor
iii.) assignment operator
iv.) default destructor
v.) address operator

4. Explain storage qualifiers in C++.

i.) Const - This variable means that if the memory is initialised once, it should not be altered by a program.
ii.) Volatile - This variable means that the value in the memory location can be altered even though nothing in the program code modifies the contents.
iii.) Mutable - This variable means that a particular member of a structure or class can be altered even if a particular structure variable, class, or class member function is constant.

5. How would you find out if a linked-list is a cycle or not?

We can find out if the linked-list is not a cycle by using two pointers. One of them goes 2 nodes every time while the second one goes at 1 node each time. If there is a cycle, the one that goes 2 nodes each time will meet the one that goes slower. If this happens, you can say that the linked-list is a cycle else not.

6. In what situations do you have to use initialization list rather than assignment in constructors.

When you want to use non-static const data members and reference data members you should use initialization list to initialize them.

7. When does a class need a virtual destructor?

If your class has at least one virtual function, you should have a virtual destructor. This allows you to delete a dynamic object through a baller to a base class object. In absence of this, the wrong destructor will be invoked during deletion of the dynamic object.

8. What is the type of “this” pointer? When does it get created?

It is a constant pointer type. It gets created when a non-static member function of a class is called.

9. How would you differentiate between a pre and post increment operators while overloading?

Mentioning the keyword int as the second parameter in the post increment form of the operator++() helps distinguish between the two forms.

10. What is a pdb file?

A program database (PDB) file contains debugging and project state information that allows incremental linking of a Debug configuration of the program. This file is created when you compile a C/C++ program with /ZI or /Zi or a Visual Basic/C#/JScript .NET program with /debug.

11. You run a shell on UNIX system. How would you tell which shell are you running?

To check this you can simply do the Echo $RANDOM.

The results will be:

- Undefined variable if you are from the C-Shell,
- A return prompt if you are from the Bourne shell,
- A 5 digit random number if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.

12.What are Stacks? Give an example where they are useful. 

A Stack is a linear structure in which insertions and deletions are always made at one end i.e the top - this is termed as last in, first out (LIFO). Stacks are useful when we need to check some syntex errors like missing parentheses. 

13. Differentiate between an external iterator and an internal iterator? What is the advantage of an external iterator. 

An external iterator is implemented as a separate class that can be "attach" to the object that has items to step through while an internal iterator is implemented with member functions of the class that has items to step through. With an external iterator many different iterators can be active simultaneously on the same object - this is its basic advantage. 

14. Do you think the following code is fine? If not, what is the problem? 

T *p = 0;
delete p;

No, the code has a problem. The program will crash in an attempt to delete a null pointer.

15. In a function declaration, what does extern mean?

The extern here tells the compiler about the existence of a variable or a function, even though the compiler hasn’t yet seen it in the file currently being compiled. This variable or function may be defined in another file or further down in the current file.

16. You want to link a C++ program to C functions. How would you do it?

This can be done by using the extern "C" linkage specification around the C function declarations.

17. Exlpain STL.

STL stands for Standard Template Library. It is a library of container templates approved by the ANSI committee for inclusion in the standard C++ specification.

18. What are the different types of STL containers?

Following are the 3 types of STL containers:

1. Adaptive containers - for e.g. queue, stack
2. Associative containers - for e.g. set, map
3. Sequence containers - for e.g. vector, deque

19. Explain Stack unwinding?

Stack unwinding is a process during exception handling when the destructor is called for all local objects between the place where the exception was thrown and where it is caught.

20. When is dynamic checking necessary?

Dynamic checking is necessary in the following scenarios: Whenever the definition of a variable is not necessary before its usage

21. Define structured programming

Structured programming techniques use functions or subroutines to organize the programming code. The programming purpose is broken into smaller pieces.

22. Explain object oriented programming. 

Object oriented programming uses objects to design applications. This technique is designed to isolate data. The data and the functions that operate on the data are combined into single unit.

23. What is reference variable in C++?

A reference variable is just like pointer with few differences. It is declared using & operator. A reference variable must always be initialized. The reference variable

24. What is class in C++?

A class holds the data and functions that operate on the data. It serves as the template of an object.

25. What is local class in C++?

Local class is define within the scope of a function and nested within a function.

26. What is the default access level?

The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private.

27. What is friend class in C++?

When a class declares another class as its friend, it is giving complete access to all its data and methods including private and protected data and methods to the friend class member methods.

28. What is a default constructor?

Default constructor is the constructor with no arguments or all the arguments has default values.

29. Define abstraction. What is abstraction? What is an abstraction and why is it important?

The process of hiding unnecessary data and exposing essential features is called abstraction. Abstraction is separating the logical properties from implementation details.

30. What is overriding?

Defining a function in the derived class with same name as in the parent class is called overriding. In C++, the base class member can be overridden by the derived

31. What is copy constructor?

A copy constructor is a special type of constructor that is used to create an object as a copy of an existing object.

32. Define private, protected and public access control

Private is the default access specifier for every declared data item in a class. Private data belongs to the same class in which it is created and can only be used

33. How to detect an end of a file?

You can either use the ifstream object ‘fin’ which returns 0 on an end of file or you can use eof()

34. What are recursive functions? What are the advantages and disadvantages of Recursive algorithms?

A recursive function is a function which calls itself.  The advantages of recursive functions are: -Avoidance of unnecessary calling of functions. A substitute for iteration where the iterative solution is very complex.

35. What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator.

An internal iterator is implemented by the member functions of the class which has the iteration logic.

36. Explain passing objects by reference, passing objects by value and passing objects by pointer.

Pass by value: The callee function receives a set of values that are to be received by the parameters. All these copies of values have local scope, i.e., they can be accessed only by the callee function.

37. Explain the concepts of throwing and catching exceptions with an example using C++.

C++ provides a mechanism to handle exceptions which occurs at runtime. C++ uses the keywords – throw, catch and try to handle exception mechanism.

38. What are the advantages of “throw.....catch” in C++?

The following are the advantages of throw and catch in c++. Code isolation: All code that is to be handled when an exception raises is placed in the catch block, which is not part of the original code.

39. What are friend classes? What are advantages of using friend classes?

The friend function is a ‘non member function’ of a class. It can access non public members of the class. A friend function is external to the class definition.

40. What are zombie objects in C++? Explain its purpose.

An object creation of a class is failed before executing its constructor. As a constructor would not return a value, there is no confirmation that whether the constructor is executed completely or not.

41. Explain the difference between Stack and Queue

Stack is a Last in First out (LIFO) data structure whereas Queue is a First in First out (FIFO) data structure.

42. What is Stack? Explain its uses.

Stack is a Last in First out (LIFO) data structure. It is a linear structure and one can insert and delete from one end only which is called the top.

43. What is the use of ‘using’ declaration?

In using-declaration, we have using keyword followed by a fully qualified name of a namespace member.

44. What is the difference between Mutex and Binary semaphore?

Semaphore synchronizes processes where as mutex synchronizes threads running in the same process.

45. What is the scope resolution operator?

Scope resolution operator allows a program to reference an identifier in the global scope that is hidden by another identifier with the same name in the local scope.

46. What are the advantages of inheritance?

Code reusability. Saves time in program development.

47. What is a conversion constructor?

It is a constructor that accepts one argument of a different type.

48. Explain the advantages of inline functions.

It relieves the burden involved in calling a function. It is used for functions that need fast execution.

49. Explain the different access specifiers for the class member in C++.

private: It is default one and can be access from class member of the same class.
protected: The protected members can be access from member functions of the

50. What is the importance of mutable keyword?

The mutable keyword allows the data member of a class to change within a const member function.

51. Pointer vs. reference.

A reference must be initialized at the time of declaration whereas not needed in case of pointer. A pointer can point to a null object.

52. Explain how to call C functions from C++.

The extern keyword is used to call C functions from C++.

53. Is it possible to overload a constructor?

Yes it is possible. A constructor can be overloaded to pass different arguments to the object.

54. Overriding vs. overloading

Overloading means having methods with same name but different signature
Overriding means rewriting the virtual method of the base class.

55. What do you mean by binding? Static vs. dynamic binding

Binding means connecting the function call to a function implementation.

56. What is abstract class?

A class whose object can't be created is abstract class. You can create a class as abstract by declaring one or more virtual functions to pure.

57. Vector vs. Map

In a vector, all the elements are in a sequence whereas Map stores element in the form of a key-value association pair.

58. Explain container class and its types in C++.

A container stores many entities and provide sequential or direct access to them. Like List, vector and strings.

59. Define storage classes in C++

Storage class defined for a variable determines the accessibility and longevity of the variable. The accessibility.

60. Define an Iterator class

A container class hold group of objects and iterator class is used to traverse through the objects maintained.

61. ATL Server interview questions

Explain the concepts and capabilities of ATL Server. Explain the ATL Server architecture.
Why ATL Server? What is SRF Files? Explain with an example.

62. C++ COM and Active X: Interview Questions

What is the STL, standard template library? What is the object serialization? What are ActiveX and OLE. What are GUID? Why does COM need GUIDs? What is a type library? What is the IUnknown interface? Explain Binary object model. How does COM provide language transparency?.

63. Explain how to call C code from C++ code. Explain with an example.

To call a C program in C++ , the basic requirement is both the compilers and runtime libraries must be compatible. They must define the primitive data types such as int, float, char etc.

64. Accessing C Code From Within C++ Source

A linkage specification is provided by C++, which is used to declare a function that follows the program linkage conventions for a supported language. C++ compilers support C linkage for compatible C compilers.

65. Why does COM require GUIDs?

Globally Unique Identifier, a unique 128-bit number used in a windows registry to identify COM DLL. Lot of COM object information is known by identifying the registry and the correct GUID. Windows also identifies the user accounts by a user name and assigns it a GUID. GUIDS are also used by database administrators as primary key values in the databases.

66. What happens when an object is destroyed that doesn't have an explicit destructor?

Destructor synthesizing is done by a compiler of an object’s class.
For instance, if an object of a class Sample is destroyed without invoking the destructor explicitly, the compiler synthesizes the destructor which destroys the object of the class Sample. This process is done by invoking
Sample::~Sample().

67. Explain the problem with dynamic type checking.

Authoring code without errors is very difficult using dynamic type checking.
It is hard to read or modify or maintain the code for dynamic type checking.

68. What is wrong with an object being assigned to itself?

A seg fault occurs at runtime.

69. Explain how static member functions are called.

Static member functions are called by referencing its containing class without using an object. The following code snippet illustrates the use of static member functions.

Test::set_number(22);
Test::print_number();
where set_number() and print_number() are static functions

70. What happens when a pointer is deleted twice?

It is not certain and depends. If the value of the pointer is set to 0 soon after first delete, then not operation will perform. If not, there are chances for program crash.

Index | Prev | Next