Thursday, May 31, 2007

Simple Question in OOPs and C++

These are some of the simple question in OOPs and C++ .
These are from my notes while preparing for exams and getting my job training.
I just wanted to keep it safe here and will be happy if these questions will be of any help to you.
I will update the blog if I get any more questions.

1. What is a class?
Ans: A class is a represents the method of binding data and the related functions together.
Class is a blueprint from which objects are created and manipulated.

2. What are the objectives of class declaration?
Ans: A class declaration is done to specify the properties and data required to model the entity in an application.

3. What are different data assessors used in C++?
Ans: Private, public, protected.

4. What is the default level access specified in a c++ program?
Ans: Private.

4. What is data hiding?
Ans: Defining the accessor properties for a method, variable using the keywords private/public is called data hiding.

5. What are member functions?
Ans: Members that are class specific are called member functions.

6. How to declare a member function?
Ans: Member functions are declared as a prototype in a class. The exact function is declared outside the class.

7. What is the syntax of a member function in C++?
Ans: Return type Class :: FunctionName(arguments)
{
Function body
};

8. What is “::” called and what does it signify?
Ans: “::” is called a “scope resolution operator” it signifies the belonging of a particular function or member to a particular class.

9. What are the ways in which arguments can be passed to a function in C++ , how does it differ from passing parameters in C# ?
Ans: In c++ parameters can be passed in three forms: Value, reference and pointer.
In C# parameters can be passed either as value or as a reference.

10. How is member function passed to a class as ------?
Ans: As a pointer.

11. What is a constructor?
Ans: Constructor is a member function that creates an instance of the class. A Constructor has the same name as the class for which the object is being created for.

12. What is memory pool?
Ans: Memory pool is the memory space provided to a program to use at run time.

13. What do you mean by Dynamic memory allocation?
Ans: Memory space is provided to a program during run time this is called dynamic memory allocation.

14. How the operator “new” works?
Ans: Operator “new “first allocates the required memory to object from the memory pool, and then calls the constructor of the class. The constructor the address of the memory allocated to the object.

15. What is a destructor?
Ans: Destructor is a member function called by the class to deinitialize the objects that are initialized by the constructor.

16. When shall a destructor be called and how should it be called?
Ans: A destructor is called whenever the object of the class goes out of scope. The syntax of destructor is class name preceded by a “~” sign.
Ex: Destructor for class1 is ~class1 ();

17. How do you de-allocate the memory space given to a member of a class?
Ans: Using the function Delete ().

18. What is the difference between Delete and De-initialize functions?
Ans: De-initialize function, de-initializes or destroys objects in a function. Whereas delete function releases the memory of the object initialized in a class.

19. What do you mean by composition and composite objects?
Ans: Composition refers to the “component relationship” between two objects ,where one object is dependent on the other.For instance, let us take the example of a class named “Student” in this class the “Student Name” and “Course taken “are dependent on each other. Such objects are called composite objects. Composition is initialized for mutally dependent objects.

20. What is the relationship between constructor, destructor and composition?
Ans: The sequencing of constructor and destructor is dependent on composition. If we take the previous example then the destructor for “Course taken” cannot be called before destructor for “Student Name”.

21. Give the syntax to declare a composition.
Ans: Following is the example to show the composition for the example given above.
Class Student
{
Private:
String Student Name;
Int CourseNumber;
String Address;
Public:
Student (const String&, const Int&);
};
Student::Student (Const String& n, const Int& c): name (n), course(c)
{
}
Here whenever name object is created, course object is created and initialized automatically.
Whenever the composite object Student is destroyed the objects are de-initialized and destroyed in the reverse order of their initialization and creation.

So, in our example first the Student object is destroyed and initialized, then the course taken object and finally the name object.

22. What is association?

23. What is the syntax used to define an association?

24. Describe how would you create an association using address pointer?

25. What is Inheritance?
Ans: When we want to declare a class Class 2 which has features similar to a class Class1 defined earlier , then instead of declaring Class 2 again , we can derive it from Class1 .This is called inheritance.
To give a complete definition:
Inheritance is the process by which one object/class acquires the properties of another object/class.

26. What are the advantages of inheritance?
Ans: Code reusability and maintainability, reduces development time.

27. Are the private members of the base class available to child class?
Ans: No. Only Public and protected variables are accessible.

28. What is the syntax to inherit a subclass from a base class?
Ans: Subclass: public baseclass
{
}
29. If Class C inherits Class B and ClassB inherits Class A.Then what informations are present in Class C?
Ans: Informations from Both class A and Class B.This is called multilevel inheritance.

30. What is member initialization list?
Ans: Member initialization list is the information containing an entry call to derived class’s immediate base class.

31. If you say that member initialization list contains only the call to immediate base class of a derived class, how can you justify your answer for Q29?
Ans: The member initialization list contains only the call to immediate base class of the derived class but the immediate parent’s constructor invokes the immediate base class until the uppermost class is reached.

32. What is dynamic binding/late binding are the same or different?
Ans: Dynamic binding is same as late binding. This is the process of invoking appropriate behavior of an operation based upon the arguments.

33. Can you explain this concept?
Ans: Lets as take the example of “+” operator, the “+” operator can be used for addition of two integers or concatenation of two strings. The behavior of this operator in the program is known only during the run time not during compile time. If the variables are int “+” does addition. Otherwise “+” is used for concatenation. This is what is called late binding or dynamic binding.

34. What do you call this other than late binding /dynamic binding?
Ans: This process is also known as polymorphism. That is one operator taking multiple roles.

35. What is the most important property of polymorphism?
Ans: Polymorphism allows objects that have different internal structure to take the same function name.

36. What do you mean by overriding?
Ans: Overriding means changing the implementation of a function in a derived class.

37. How is polymorphism supported in C++?
Ans: By declaring the function virtual in base class and derived class.

38. What do you mean by Virtual function?
Virtual function is a function in derived class with features specific to the derived class.

39. So how a virtual function does differs from a general derived function?
Ans: A virtual function in a derived class has same function name, same parameters, same return type as that of the base class but the method implementation is different.

40. What are the two features available in c++ for error checking in development?
Ans: Const function and assert () macro.

41. What does the following declaration signify? int String(const *P)
Ans: It signifies that the value of the variable declared at address P should not be modified.
In case of modification the compiler throws an error.

42. How can u use assert () macro?
By including assert
The declaration is given as #include
The syntax to use assert macro is:
#include

Void class ClassName::FunctionName(arguments)
{
Assert (condition);
};
Here function name represents the function that represents error.
Assert macro checks for errors based on the condition.

43. Where is the assert macro generally placed in a function?
Ans: Assert macro is place at head as well as at the end of the function. It is placed at the head to evaluate the incoming parameter/arguments, and at the end to validate the object is in its proper state before the function terminates.

44. What is a fiend class in C++? What is its Syntax?
Ans: A class can access the private variables of other class by declaring the class as a friend class.
The syntax of friend class is friend class
Friend function

45. A friend class allows unrestricted access to the members of a class. True/False?
True.

46. What are the advantages and disadvantages of declaring a class as a fiend class?
Ans: Advantage: A declaring a class as a friend class allows the subclasses to access the private variables and functions.
Disadvantage: Declaring a class as a friend class allows other classes to modify the base class implementations of the functions. Which can cause complications in a program.

47. What do you mean by default initialization in C++?
When an object is passed as a value to a function, the compiler creates a local copy of the variable in the stack this is called default initialization.
Default initialization also happens when an object is returned back from a function by value.

48. What are the disadvantages of default initialization?
1. In default initialization, the initialized object of deleted buffer will not get the correct value.
2. The newly created object of the same type also points to the same dynamically allocated buffer.

49. What is shallow copy?


50. What do you mean by pure virtual function?
Ans: Declaring a function as pure virtual function ensures that the base class virtual function does not override the derived class function. That is whenever the function is called it is not the base class function but the derived class function.

51. Can a pure virtual function be called from a base class? Why?
Ans: No. Because the pointer to pure virtual function in pure virtual table is null. When the base class constructor calls a pure virtual function it gives a run time error.

52. What is the declaration for a pure virtual class function?
Ans: class BaseClass
{
Public:
Virtual void functionName() = 0;
};

53.What are the impoprtant concepts in OOP?
Ans: Abstraction,Inheritance, Polymorphism


mail me at: archita.dash@rediffmail.com

No comments: