CHAPTER TER - 5 General General OOP OOP Concepts Concepts 1. Ans. 2. Ans. 3. Ans.
4. Ans.
5. Ans.
6. Ans.
7. Ans. 8. Ans.
9. Ans.
10. Ans.
11. 11. Ans.
12. Ans.
TYPE A : VERY VERY SHORT ANSWER ANSWER QUESTIONS What What are are the the two majo majorr typ types es of prog progra ramm mmin ing g lan langu guag ages es? ? The two major types of programming languages are Low Level Languages and High Level Languages. Which Which two two prog progra ramm mmin ing g lan langu guag ages es are are low low leve levell lan langu guag ages es? ? Machine language and Assembly language are low level languages. How How are are pro progra grams ms written written in (i) (i) mach machine ine langua language, ge, (ii) (ii) asse assembl mbly y langu language age? ? (i) machine language: In machine language instructions are written in binary binary code (using 0 and 1). (ii) assembly language: In assembly language instructions are written using symbolic names for machine operations (e.g., READ, ADD, STORE etc. ) and operands. Why Why are are low low leve levell lang langua uage gess cons consid ider ered ed clo close se to to the the mac machi hine ne? ? Low level level languages languages are considered considered close close to the the machine machine because because it provide provide a vehicle vehicle for for the progra programmers mmers to specify actions to be executed, so that all important aspects of a machine are handled handled simply and efficiently in a way that is reasonably obvious to the programmer. Why is it it easier easier to progra program m with with high high level language languages? s? Why Why are are high high level level language languagess conside considered red close close to the the programmer? High level languages offer English like keywords, constructs for sequence, selection and iteration and use of variables and constants. Thus, is it easier to program with high level languages. High level languages are considered close to the programmer because it it provides a set of concepts concepts for the programmer to use when when thinking about what can be done. Which Which two two rel relat ated ed pur purpo pose sess shou should ld be be serv served ed by a pro progr gram ammi ming ng lan langu guag age? e? A programming language should serve two related purposes as following: (i) it should provide a vehicle vehicle for the programmers to specify actions to be executed and (ii) it should a set of concepts for the programmer to use when thinking about what can be done. Why Why ‘C+ ‘C++’ +’ is call called ed ‘mid ‘middl dle e lev level el lang langua uage ge’? ’? ‘C++’ is both “close to the machine” and “close to the programmer”, so it is called ‘middle level language’. What What do you you unde underst rstan and d by progr program ammi ming ng parad paradigm igm? ? Name Name vario various us prog program rammi ming ng parad paradigm igms. s. A Programming Paradigm defines the methodology of designing designing and implementing programs using the key features and building blocks of a programming language. Following are the different programming paradigms: (i) Procedural Programming (ii) Object Based Programmin Programming g (iii) Object Oriented Programming What What are are the the char chara acter cteris isti tics cs of of pro proce cedu dura rall para paradi digm gm? ? Do not model real world very well. More emphasis is on doing things. It employs top-down approach in program design. It is based on the problem at hand. Sequence or procedure or functionality is paramount. What What is a module? module? What What is modular modular progra programmin mming g paradig paradigm? m? What What are are its characteri characteristics stics? ? A set of related procedure with the data they manipulate is called a Module. Modular programming paradigm combines related procedures in a module and hides data under modules. The data are dependent on the functions. The arrangement of data can’t be changed without modifying all the functions that access it. What What is an obje object? ct? Wha Whatt is a cla class? ss? How is an objec objectt diffe differen rentt from from a clas class? s? An object is an identifiable entity with some characteristics and behavior. behavior. It represents an entity that can store data and its associated functions. A class is a group of objects that share common properties and relationship. relationship. It represents a group of similar objects. What What is object object oriented oriented programm programming ing parad paradigm? igm? Name the four four basic basic concepts concepts of OOP. OOP. We can define the object oriented programming (OOP) paradigm as following: Decide which classes and objects are needed; provide a full full set of operations for each class. Following Following are the basic OOP concept concepts: s: 1
http://cbsecsnip.in
13. Ans. 14. Ans. 15. 15. Ans.
16. 16. Ans.
17. 17. Ans. 18. 18. Ans. 19. 19. Ans.
20. 20. Ans.
21. 21. Ans. 22. 22. Ans. 23. 23. Ans. 24. Ans. 25. 25. Ans. 26. 26. Ans. 27. 27. Ans.
1. Da Data Abstrac raction 2. Dat Data En Encap capsulation 3. Mo Modularity 4. In Inheri eritance 5. Polymorphism What is is mea meant by by Abstra stract ctio ion n? Abstraction is the act of representing essential features without including the background background details. The real world concept concept gets gets simpli simplified fied using using concept concept of abstra abstraction ction.. True True or or False? False? True. Give Give an an exa exampl mple e to illust illustra rate te the the conc concept ept of abstr abstract action ion.. In switch board we only press certain switches switches according to our requirement. requirement. What is happening inside, inside, how it is happening etc. we need not know. So, this is abstraction, we know only the essential things to operate on switch board without knowing the background details of switchboard. What What is encaps encapsula ulatio tion? n? Why Why data data is consid considere ered d saf safe e if encap encapsul sulate ated? d? Encapsulation is the way of combining both data and the functions that operate on the data under a single unit. In encapsulation, data can’t access directly. The only way to access the data is provided by the functions. functions. The data is hidden, so data is considered safe if encapsulated How How are are the the terms terms abstr abstract action ion and and encap encapsul sulati ation on rel relate ated? d? Encapsulation is the way of implementing abstraction. Thus, the terms abstraction and encapsulation are related because while implementing encapsulation, abstraction is also implemented. Why Why clas classe sess are are cal calle led d Abs Abstra tract ct Dat Data a Typ Type? e? Since the classes use the concept concept of data abstraction, they are known as Abstract Data Types (ADT). What What is a base base clas class? s? What What is a subcla subclass ss? ? What What is the the relati relations onshi hip p betwee between n a base base class class and and subcl subclas ass? s? A subclass is a class that inherits properties from some other class. A base class is a class whose properties are inherited by subclass. subclass. A subclass has nearly all the properties of base class but the reverse of it is not true. What What is is modu modula larit rity? y? Wha Whatt bene benefi fits ts doe doess it off offer er? ? Modularity is the property of decomposing a system into into a set of cohesive and loosely coupled modules. It offers following benefits: (i) It reduces its complexity to some degree . (ii) It creates a number of we ll-defined, documented boundaries within the program. Inheri Inheritan tance ce ensur ensures es clos closene eness ss with with the rea real-wo l-world rld model models. s. True True or or False False? ? True. How How does does inh inher erit itan ance ce sup suppo port rt ‘reu ‘reusa sabi bili lity ty’? ’? Inheritance supports ‘reusability’ by allowing the addition of additional features to a existing class without modifying it. What What do you mean mean by the transi transitiv tive e natur nature e of of inheri inherita tance nce? ? The transitive nature of inheritance states that if a class A inherits properties from its base class B then all its subclasses will also also be inheriting the properties of base class of A i.e., B. A subcla subclass ss defin defines es all all the the feature featuress of base class class and and its its additi additional onal features. features. True or False False? ? False A subcl subclass ass inheri inherits ts all all the prop propert erties ies of of its base base class class and and vice vice versa versa.. True True or False False? ? False What What are are object object-ba -based sed lan langua guages ges? ? Give Give an exa exampl mple e of obje object-b ct-bas ased ed langu language age.. Languages that support programming with objects are said to be object-based programming languages. They do not support inheritance and dynamic binding. Ada is a typical object-based programming language. What What is polym polymorp orphis hism? m? Give Give an exa exampl mple e illustr illustrat ating ing poly polymor morphi phism sm.. Polymorphism is the ability for a message or data to be processed in more than one form. For example, consider an addition operation. It shows different behavior in different types of data. For two numbers, it will generate a sum. The numbers may integers or float. T hus the addition for integer is different from the addition to floats. TYPE B : SHORT ANSWER ANSWER QUESTIONS QUESTIONS
1. Ans.
Brie Briefl fly y expl explai ain n the the two two majo majorr type typess of pro progr gram ammi ming ng lang langua uage ges. s. Low Level language: These are machine-oriented and require require extensive knowledge of computer computer circuitry. In machine language, the instructions are given in binary codes whereas in assembly language, language, instructions are 2
http://cbsecsnip.in
2. Ans. 3. Ans.
4. Ans.
5. Ans.
6. Ans.
7. Ans.
8. Ans.
9. Ans.
10. Ans.
given in symbolic names. High Level language: High level languages like BASIC, PASCAL etc. offer English like keywords, programming constructs for selection and iteration and allow use of variables and constants. Programmers feel much at ease while working in HLLs as compared to low level languages. Writ Wr ite e a sho short rt not note e on prog progra ramm mmin ing g in two two maj major or typ types es of of lang langua uage ges. s. Same as above Q. No. 1 What What are are prog program rammin ming g parad paradigm igms? s? Give Give name namess of some some popu popular lar progra programm mming ing parad paradigm igms. s. Programming Paradigm: A Programming Paradigm Paradigm defines the methodology of designing and implementing programs using the key features and building blocks of a programming language. Following are the some popular programming paradigms: (i) Procedural Programming (ii) Modular Modular Programming Programming (iii) Object Oriented Programming Writ Wr ite e a shor shortt not note e on on pro proce cedu dura rall pro progr gram ammi min ng. The programming approach that focuses on the solution of a problem is known as procedural programming paradigm. This approach emphasis on the ‘doing’ rather than the ‘data’. Languages support this paradigm by providing facilities for passing argument to functions and return values from functions. Writ Wr ite e a shor shortt not note e on on mod modul ular ar prog progra ramm mmin ing. g. In modular programming, a set of related procedures with the data they manipulate is combined under a uit called module. In modular programming, since many modules access the same data, the way the data is stored becomes critical. The arrangement of the data can’t be changed without modifying all the functions that access it. What What are are the the shor shortco tcomin mings gs of of proce procedu dural ral and and modul modular ar prog program rammin ming g appro approach aches? es? In procedural Programming more emphasis is on doing things. Procedural Programming is susceptible to design changes. Procedural Programming leads to increased time and cost overheads during design c hanges. the arrangement of the data can’t be changed without modifying all the In modular programming, the functions that access it. chief components – The problem associated with procedural and modular programming is that their chief functions etc. do not model the real world ve ry well. Write rite a sho short note ote on on OO OO pro prog gramm rammin ing g. OOP stands for for Object Oriented Programming. In, Object-Oriented Programming (OOP), the program is organized around the data being operated upon rather than the operations performed. The basic idea behind OOP is to combine both, data and its functions that operate on the data into a single unit called object. Following Following are the basic OOP concept concepts: s: 1. Data Abstraction 2. Data Encapsulation 3. Modularity 4. Inheritance 5. Polymorphism How How does does OOP overc overcom ome e the the shortc shortcom oming ingss of of trad traditi ition onal al progra programm mming ing appro approach aches? es? OOP provides the following advantages to overcome the shortcomings of traditional programming approaches: OOPs is closer to real world model. Hierarchical relationship among objects can be well-represented through inheritance. Data can be made hidden or public as per the need. Only the necessary data is exposed enhancing the data security. Increased modularity adds ease to program development. Private data is accessible only through designed interface in a way suited to the program. What What is the diffe differen rence ce betw between een objec objectt and and class class? ? Exp Explai lain n with with exampl examples es.. An object represents an entity that can store data and its associated functions whereas a class represents a group of similar objects. ‘Object’ is an instance of ‘class’. For example, we can say ‘bird’ is a class but ‘parrot’ is an object. Explain Explain briefly briefly the concept concept of of data data abstract abstraction ion with the help of an example. example. Abstraction refers to the act of representing essential features without including including the background details or 3
http://cbsecsnip.in
11. 11. Ans.
12. Ans.
13. Ans.
14. 14. Ans.
15. 15. Ans.
16. 16. Ans.
17. Ans.
18. 18. Ans.
19. 19. Ans.
explanations. Abstraction is implemented through public public members of a class, as the outside world is given only the essential and necessary information through public public members, rest of the things remain hidden. For example, while drive a car, we only knows the essential features to drive drive a car without including including the background details details or explanations. This is abstraction. Expla Explain in brief briefly ly the the concep conceptt of encap encapsul sulati ation on with with the the help help of of an exa exampl mple. e. The wrapping up of data and operations/functions (that operate o the data) into a single unit (called class) is known as Encapsulation. Encapsulation is implemented implemented with the help of a class as a class binds together data and its associated function function under one unit. unit. For example, cars and owners. All the functions of cars are encapsulated encapsulated with the owners, no one else can access it. How the the data data is hidden hidden and and safe safe if encapsula encapsulation tion is is implem implemented ented? ? Explain Explain with exam example. ple. In encapsulation, data cannot be accessed directly. The o nly way to access the data is provided by the functions. To read the item in an object, call a member function in the obje ct. It will read the item and return the value. The data is hidden, so so it is safe from accidental alteration. alteration. The best example of encapsulation encapsulation is a CLASS because a class hides class variables/functions from outside outside the class. Simulate Simulate a daily daily life exam example ple (other (other than than the the one mentio mentioned ned in the chapter) chapter) that that explains explains encap encapsula sulation. tion. In daily life, mobile mobile phone explains encapsulation. Mobile phone has some some interface which helps us to interact with cell phone and we can uses the services of mobile phone. But the actually working in cell phone is hidden. We don’t know how it works internally. Writ Wr ite e a sho short rt not note e on on inh inher erit itan ance ce.. Inheritance enables us to create new c lasses that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A der ived class can have only one direct base class. Inheritance is transitive. When we define a class to derive from another class, the derived class implicitly implicitly gains all the members of the base class, except for its constructors constructors and destructors. What What are are the the adv advan anta tage gess offe offere red d by inh inher erit itan ance ce? ? Inheritance ensures the closeness with the re al-world models. Allows the code to be reused as many times as needed. The base class once defined and once it is compiled, it need not be reworked. some new features. We can extend the already made classes by adding some nature of real-world’s inheritance, which in turn saves on Inheritance is capable of simulating the transitive nature modification time time and efforts, if required. How How is reus reusab abili ility ty supp support orted ed by inhe inherit ritan ance? ce? Exp Explai lain n with with exam example ple.. Inheritance allows the addition of additional features to an existing class without modifying it. One can derive a new class from an existing one and add new features to it. For example, we have a class ‘Student’, and we need to add a new class called ‘GraduateStudent’. W e derive the new class ‘GraduateStudent’ from the existing class ‘Student’ and then all we really need to add the extra features to ‘GraduateStudent’ that describe the difference between students and graduate students. What What is the benefit benefit of tran transitive sitive nature nature of of inheri inheritanc tance? e? Expla Explain in with example. example. Suppose we inherit class B from existing class A. the classes C and D inherit inherit from class B. later we find that class A has a bug that must be correct ed. After correcting the bug in A, it automatically will be reflected across all classes that inherit from A, if the class A has been inherited without changes. The benefit of being transitive is reduction in amount of efforts that one would have done if each class inherited from A was to be modified separately. What What is is poly polymo morp rphi hism sm? ? Expl Explai ain n with with exa examp mple le.. Polymorphism is the ability for a message or data to be be processed in more than one form. It is a property by by which the several different objects respond in a different way to the same message. Suppose we have three different classes called rectangle, circle and triangle. Each class contains a draw() method to draw the re levant shape on the screen. When you call a draw method through a function call, the required draw() w ill be executed depending on the class, i.e., rectangle, circle or triangle. Do you you think think OOP OOP is is more more clos closer er to real real world world prob problem lems? s? Why? Why? How? How? Yes, OOP is more closer to real world problems because object oriented programming implement inheritance which has capability to express the inheritance relationship which makes it ensure the c loseness with the real4
http://cbsecsnip.in
world models. Object oriented programming implement inheritance by allowing one class class to inherit from another. By implementing inheritance, inheritance, real-world relations among objects can be represented programmatically. TYPE C : LONG LONG ANSWER ANSWER QUESTIONS 1. Ans.
Write rite a note ote on soft softwa ware re evo evoluti lution on.. A program serves the purpose of commanding the computer. The two major types of programming languages languages are as follows: (a) Low Level Languages: These are mac hine-oriented languages and require extensive knowledge of computer circuitry. Machine language and Assembly language are low level languages. The low level languages provide a vehicle for the programmer to specify actions to be executed, so they are closer c loser to machine. (b) High Level Languages: Offers English like like keywords constructs for sequence, sequence, selection and iteration and use of variables and constants. High level languages are considered close to the programmer programmer because it it provides a set of concepts for the programmer to use when thinking about what can be done. ‘C++’ is both “close to the t he machine” and “close to the programmer”, so it is called ‘middle level language’. Programming Paradigm: Paradigm means organizing principle principle of program. It is an approach to programming. Following are there different programming paradigms: paradigms: (i) Procedural Programming: Decide which procedure you want; use the best algorithm you can find. (ii) Modular Modular Programmi Programming: ng: Decide which which module module you want; partition partition the program program so that that data is hidden hidden in modules. (iii) Object Oriented Programming: Programming: Decide which classes and objects are needed; provide a full set of operations for each class. 2. Expla Explain in differ different ent prog progra ramm mming ing para paradig digms ms,, their their shortco shortcomin mings gs etc. etc. with appr approp opria riate te exampl examples. es. Ans. Following are the different programming paradigms: (i) Procedural Programming: Procedural programming paradigm lays lays more emphasis on procedure or the algorithm. Data is is considered secondary. Data is loosely available to many functions. The problem associated with procedural and modular programming is that their chief components – functions etc. do not model the real world very well. For instance, a procedural program for library maintenance aims at the operations Issue, Return etc. whereas real world entities are Books. (ii) (ii) Modula Modularr Program Programmin ming: g: In modular programming, a set of related procedures with t he data they manipulate is combined under a unit called module. In this paradigm, since many modules access the same data, the way the data is stored stored becomes critical. The arrangement of the data can’t be changed without modifying all the functions that access it. Another drawback is same as above in procedural programming. (iii) Object Oriented Programming: The object oriented approach views a problem in terms of objects involved rather than procedure for doing it. OOP implements inheritance, inheritance, so that real-world relations among among objects can be represented programmatically. The problem associated with OOP is that OOP programs’ design is tricky and to program with OOP, programmer need proper skills such as design skills, programming skills, thinking in terms of objects etc. 3. Expl Explai ain n the the basi basicc conc concep epts ts of OOP OOP wit with h exam exampl ples es.. Ans. Following are the general OOP concepts: information to the outside word and hiding 1. Data Abstraction: Data abstraction means, providing only essential information their background details i.e. to represent represent the needed information in program without presenting the details. For example, while drive a car, we only know the essential things to drive a car without including including the background details or explanations. This is abstraction. 2. Data Encapsulation: The wrapping up of data and operations/functions (that operate o the data) into a single unit (called class) is known as Encapsulation. Encapsulation. For example, in a big company, there are so many departments. Suppose an employee in the production dept. wants to know about purchased material. The production dept employee will have to issue a memo to the ‘purchase’ requesting for the r equired information. Then some employee of the ‘purchase’ dept. will send the replay with the asked information. Here, we can say t hat 5
http://cbsecsnip.in
‘Department data and department employees are encapsulated into a single e ntity, the department’. 3. Modularity: Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules. modules. For example, the music system system comprises of speakers, cassette-player, record-player, cd-player etc. Now, these parts are complete units in themselves, yet they are a subpart of the music system. This is modularity. 4. Inheritance: Inheritance is the capability of one class of things to inherit capabilities or properties from another class. For example, humans inherit from the class ‘Humans’ certain properties, properties, such as ability to speak, eat, drink etc. The c lass ‘Human’ inherits these properties from the class ‘Mummal’ which again inherits some of its properties from another class ‘Animal’. ability for a message or data to be processed in more than one form. For 5. Polymorphism: Polymorphism is the ability example, 5 + 7 results into 12, the sum of 5 and 7. And ‘A’ + ‘B’ results into ‘AB’, the concatenated strings. The same operation symbol ‘+’ is able to distinguish between the two operations depending depending upon the data type it is working on.
6
http://cbsecsnip.in