Object Oriented Programming Structure (OOPs)
1. What is a class?
Class is a collection of members and member functions.
2. What is difference between Class and Object?
Class is a blueprint or prototype from which object created. Object is a software bundles of related state and behavior.
3. What is interface?
Interface also like a class contains collection of members and member functions but the member functions are not implemented, it has only signature.
4. What is the difference b/w abstract and interface?
Abstract can,t support multiple inheritence. Interface can suppport the multiple inheritence. Abstract have accesbulity modifiers. Interface have no accesbulity modifiers, by default all are public.
5. Explain polymorphism? What r the types of polymorphism?
Polymorphrism means the "Many forms", is feature that allows one interface to be used for a general class of actions. The specifc action is determined by the nature of the situation. Two types of polymorphrism, 1. Compile time (Operator/Functional overloading) [Language 'C' supported polymorphrism]. 2. Run time (Virtual function). Example: //function prototype int Addition(int iOperand1, int iOperand2); float Addition(float fOperand1, float fOperand2); float Addition(int iOperand1, float fOperand2);
6. What is the Advantage of Interface over the Inheritance in OOPS?
1. Provides flexibility in implementing the operations for the derived classes. 2. Avoid conflicts when more than one interfaces are derived in a class.
7. What is abstract class ? when is used in real time ?
An abstract class is a class contain collection members and member functions but the member functions are not defined, contain only signature. It would be implemented in child classes. Usually we use it as base class and all base method; we can't create an instance for abstract classes, we can only inherit. We also call it must inheritable class.
8. What is difference between overloading and overriding?
in overloading methods can have the same name but must have different parameter lists i.e signature while in overriding if you declare a field in a subclass with the same name as one in the superclass, the superclass field can only be accessed using super or the superclasses type
9. What is the difference between new and malloc?
both malloc and new functions are used for dynamic memory allocations and the basic difference is: malloc requires a special "typecasting" when it allocates memory for eg. if the pointer used is the char pointer then after the processor allocates memory then this allocated memory needs to be typecasted to char pointer i.e (char*). but new does not requires any typecasting. Also, free is the keyword used to free the memory while using malloc and delete the keyword to free memory while using new, otherwise this will lead the memory leak. Another answer: new automatically calls the constructor while malloc() dosen't., also new being an operator, it can be overloaded. since malloc returns a void pointer it is necessary to explicitly typecast it into an appropriate type of pointer. This gets completely avoided when we are using new operator
10. Different types of castings
2 types of casting Boxing(Implicit) Unboxing(Explicit)
11. What is the use of mutable key word?
This keyword can only be applied to non-static and non- const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function. if you write a class do you write Assignment operator and copy constructor Compiler provide the assignment operator/ copy constructor by default. The assignment operator and copy constructor do the bit wise copy of the object. If user want to do the member wise copy(ex: allocating the memory from the heap) for the members, one can write his/her own assignment operator/ copy constructor.
12. What is a copy constructor?
A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object. There are 3 important places where a copy constructor is called. When an object is created from another object of the same type When an object is passed by value as a parameter to a function When an object is returned from a function
13. What is the function of 'this' operator ?
Always this refers to current object.
14. What is a static variable and static function ?
Static variable is the one allocated statically, meaning that, it is allocated once in the program space and exists till the program space is avaialble(the close of the application). Note:- Some people get confused between stack allocation and static allocation. Both are different terms. A static function is again the same concept with static variable allocation, but here the allocation is not just a variable but a function's 'activation frame'. The activation frame, for now, consider the function's information required by the compiler to execute the function which is stored internally.
15. What is the virtual function ?
That can be overridden in derived class. Answer2: virtual methods are declared with 'virtual' keyword and it must be redefined in the derived classes. when more than one classes derives a method in the base class, that method can be written with virtual to avoid ambiguity.
16. What is the difference between new operator and operator new?
The "new" operator allocates a new instance of an object from the heap, utilizing the most appropriate constructor for the arguments passed. Like many operators in C++, the "new" operator for a particular class can be overridden, although there is rarely a need to do so. "operator new" is the mechanism for overriding the default heap allocation logic. Ask your interviewer to provide you with a concrete example of when he or she has been required to do this.
17. What is Agile methodology?
Agile methodology is anadaptaive methodology, its people oriented. Here are some of the other characteristices of the Agile methodology. 1. Delivery frequently. 2. Good ROI for client. 3. Test frequently. 4. Collaborative approach.
18. What are the different forms of polymorphism?
Overloading and Overriding.
19. What is multi level inheritance?
Deriving features from one class to other and other to other. The best example: we have 3 class ClassA, ClassB and ClassC. ClassB is derived from ClassA and ClassC is derived ClassB this is multi level inheritance.
20. Difference between data encapsulation and abstraction?
Wrapping of data and function together is called encapsulation. Abstract data can be virtual.
21. what is the difference between encapsulation and data hiding?
Data Abstraction is removing unnecessary details. Data Encapsulation is wrapping up the data into the single frame called class, for further reusability
22. Why do you need abstraction?
If you want to develop any program you need abstract idea to start. So you will abstract the things according to program. As you want to develop a library software you will not abstract any motor or financial things in your abstraction.
23. What is the difference between instance and object?
When we create object from a class, it is created on stack or on heap. The existence of an object in memory (stack or heap) is called instance of an object. Hence Instance and object is used interchangeably.
24. What is factory class?
A Factory class is one that is used to return instances of other classes. This is generally used in the context of Factory Design Pattern.
25. How do you differentiate a constructor from a ordinary function?
There are three differences. 1. Constructor has its class name. that can be unique. 2. It does not have any return type. 3. It cannot be subclass.
26. Can we inherit private members of class ?
we cannot inherit private members of a class
27. What is size of class having no variable & 1 function which returns int?
Integer constant always occupies two bytes in memory, so in 16 bits , highest bit is used to store the sign of the integer.
28. Can we create a object of Base class? If we can not create a object of base class then why we need Abstract class?
Yes, we can create an object of the Base class. If we make a class as abstract then we can't create an object of that class. we can create only pointer of that class this is because to provide one interface to access and so that runtime time polymorphism can implement.
29. What is the similarities between macro and function?
macro are the special function but they consume very less time with respect of function but there is some restriction in macro you do not use recursion as you can do in function.
30. What is late bound function call and early bound function call?
Functions are bound to there address so that they could be executed. if the address of the functions are known during compile time the compiler binds it, this kind of binding is known as compile time binding or early binding. Where as when we don't know which function needs to be executed during compile time (as in case of dynamic polymorphism), compiler uses mechanism of virtual table and binds the function address during runtime. this is known as runtime binding or late binding.
31. What is virtual constructors/destructors?
virtual constructors are used when we need to avoid the copy same. destructor frees the memory with a ~(tilde) symbol
32. What is the difference between declaration and definition?
During declaration we just specify the type and no memory is allocated to the variable. But during the definition an initial value is assigned and memory is allocated to the variable.
33. When is a memory allocated to a class?
When instance of that class is created by us
34. What are the advantages of inheritance?
In OOPs, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have combined features of both the classes.
35. What do you mean by pure virtual functions?
A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be "pure" using the curious "=0" syntax: class Base { public: void f1(); // not virtual virtual void f2(); // virtual, not pure virtual void f3() = 0; // pure virtual };
36. Why do we use virtual functions?
The virtual function can be allowed in base classes only. The functions marked Virtual will only be override able in derived classes. If I am having a function in base class that should come in derived class but I want it to have different behavior. This can only be achieved only if base class function is virtual function.
37. What is the difference between pass by reference(call by ref.) and pass by value(call by value)?
in pass by reference the parameters are passed as the address of the variables whereas in pass by value the variables are directly passed as parameters
38. What are generic functions and generic classes?
Generic functions are functions which are not used for any particular data type. they are useful for any data type ; making more global. Ex: if you write code for the Sorting program using templates then the function is useful for any data type
39. Difference between realloc() and free?
realloc(): memory when gets decreased, we can allocate it with the function called realloc(). free: it is used to free the memory in the program.
40. What is abstraction?
Abstraction is thing which you directly not use. like as in motor making all spare apart are come from different class which all are combined in one motor class where we use it. we directly not use any only one think as tire or gas or glass or steering, etc.., for use it we want to combine that all.
41. What is a scope resolution operator?
when a function is declared inside the class, it can be used or implemented with the operator called scope resolution operator with a :: symbol.
42. What is friend function?
It is a non-member function, but having access to class members.
43. What is the difference between class and structure?
structure is a value type class is reference type, struct Memory will be on stack and class Memory stored on Heap, struct not suports inheritance and class can support; By default the members of struct are public. By default the members of class are private. When an object is created to a class, constructor will be called automatically. But not in struct (if structure having member function with structure name)
44. In which Scenario you will go for Interface or Abstract Class?
Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes. Even though class inheritance allows your classes to inherit implementation from a base class, it also forces you to make most of your design decisions when the class is first published. Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code.
45. In which cases you use override and new base?
Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier. Override simple redefine a method.
46. Can we call a base class method without creating instance?
Yes, 1. You can call it using derived class object or in a derived class member function depending upon base class fun's access spicier and inheritance specified. 2. we have a keyword base; it directly refers to base class. 3. Creating static methods.
47. What is a constructor?
Constructor is an operation initializes by its state and it calls by itself when you create an instance.
48. What is a destructor?
Destructor is an operation that frees the state of an object and/or destroys the object itself. In Java, there is no concept of destructors. Its taken care by the JVM. In .Net its taken care by the CLR.
49. What is meant by static binding?
Static binding is a binding in which the class association is made during compile time. This is also called as Early binding.
50. What is meant by Dynamic binding?
Dynamic binding is a binding in which the class association is not made until the object is created at execution time. It is also called as Late binding.
51. Define Modularity?
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.
52. Limitations and Restrictions of Interface?
The essential idea to remember is that an interface never contains any implementation. The following restrictions and imitations are natural consequences of this: You're not allowed any fields in an interface, not even static ones. A field is an implementation of an object attribute. You're not allowed any constructors in an interface. A constructor contains the statements used to initialize the fields in an object, and an interface does not contain any fields! You're not allowed a destructor in an interface. A destructor contains the statements used to destroy an object instance. You cannot supply an access modifier. All methods in an interface are implicitly public. You cannot nest any types (enums, structs, classes, interfaces, or delegates) inside an interface.
53. What is a static class?
We can declare a static class. We use static class when there is no data or behavior in the class that depends on object identity. A static class can have only static members. We can not create instances of a static class using the new keyword. .NET Framework common language runtime (CLR) loads Static classes automatically when the program or namespace containing the class is loaded. Here are some more features of static class: • Static classes only contain static members. • Static classes can not be instantiated. They cannot contain Instance Constructors • Static classes are sealed.
54. What is static member of class?
A static member belongs to the class rather than to the instances of the class. In C# data fields, member functions, properties and events can be declared static. When any instances of the class are created, they cannot be used to access the static member. To access a static class member, use the name of the class instead of an instance variable Static methods and Static properties can only access static fields and static events. Like: int i = Car.GetWheels; Here Car is class name and GetWheels is static property. Static members are often used to represent data or calculations that do not change in response to object state.
55. What is the difference between reference parameter and output parameter?
Modifications of a reference parameter and output parameter impact the corresponding argument. So an output parameter is similar to a reference parameter, except that the initial value of the caller-provided argument is unimportant.