Class & Object Past Paper PDF

Summary

This document is a question-and-answer set for a computer science class, likely covering object-oriented programming (OOP) in C++. It includes definitions, explanations, and examples related to global and local variables, procedural programming, and fundamental OOP concepts.

Full Transcript

“Class & Object” Question Answer for HS 2nd Year COMPUTER SCIENCE AND APPLICATION Unit – I Object Oriented Programming in C++ “Class & Object” Main Topics 1. Local and Global Variable 2. Procedure Oriente...

“Class & Object” Question Answer for HS 2nd Year COMPUTER SCIENCE AND APPLICATION Unit – I Object Oriented Programming in C++ “Class & Object” Main Topics 1. Local and Global Variable 2. Procedure Oriented Programming 3. Object Oriented Programming (OOP) 4. Encapsulation 5. Abstraction 6. Polymorphism 7. Function overloading 8. Class and Object 9. Passing an object to a function 10. Data hiding 11. Static data member and member function 12. Inline Function 13. Friend Function 14. Friend Class 1. What is the difference between Global variable and Local variable? Answer: - Global Variable Local Variable 1. Global variables are declared after 1. Local variable iable is declared within a the preprocessor directives in a function. program, i.e. outside any function. 2. The scope of global variable is in 2. The scope of local variable is in the entire program, i.e. the global the function in which it is variable can be used by every declared, i.e. the local variable function within the program. cannot be used or referenced They are known by every outside that function. They are functions within the program. not known to other function. 3. The lifetime of global variable as 3. The lifetime of the local variable vari long as the program run. as long as the function is executing, in which it is declared. 2. What is procedure oriented programming? Explain its characteristics, advantages and disadvantages. Mark – 4 Answer: - The procedure oriented programming is a method of programming in which the problem is viewed as a sequence of things to be done, such as get some input, do some computational task and finally display the output. Thus, a program in a procedural language is a list of instructions. For very small small programs, no other organizing principles are needed to write a solution. A programmer creates a set of instructions and the computer carries them out. All conventional programming languages such as Pascal, C, FORTRAN and COBOL used to model programs based based on the procedure oriented at approach. 1 Tapas Kr Borah “Class & Object” Question Answer for HS 2nd Year Characteristics of Procedure Procedu Oriented Programming: Some characteristics exhibited by procedure oriented programming are – (a) More emphasis is on doing things (algorithms). (b) Large programs are divided into smaller programs known as functions. (c) Most of the functions share global data. (d) Data move openly around the system from function to function. (e) Employs top-down top approach in program design. Advantage of Procedure Procedu Oriented Programming: Some of the advantage of procedure oriented programming are – (a) It is relatively simple and easy to implementation. (b) An easier way to keep track of program flow. (c) Needs only less memory. Disadvantage of Procedure Procedu Oriented Programming: Some of the disadvantage of procedure oriented programming are – (a) Global data is exposed to whole program, so no security for data. (b) Does not model the real world problems very well. (c) Difficult to create new data types reduces extensibility. (d) Difficult to reuse the set of instructions for other type of data. (e) Importancee is given to the operation on data rather than the data. 3. What is OOP? Mark – 1 Answer: - OOP stand for Object Oriented Programming.. It is a method of programming in which independent blocks of codes or objects are built to interact with each other similar to our real-world world objects. 4. What do you mean by object oriented programming language? Mark – 2 OR Write the basic features of object oriented programming. Mark – 3 Answer: - Object Oriented Programming language is the computer programming language in which independent blocks of codes or objects are built to interact with each other similar to our real-world real world objects. Major features that are required for Object Oriented Programming are – (i) Data Abstraction (ii) Data Encapsulation (iii)Inheritance Inheritance (iv) Polymorphism (v) Dynamic Binding (vi) Automatic initialization and clear-up clear objects Some of the examples of object oriented programming language are C++, Java, Smalltalk, Object-Pascal Pascal etc. 5. What are the advantages of object oriented programming? progr Mark – 4 Answer: - Object Oriented Programming offers several benefits to both the program developer and the user. This technology of programming provides greater programmer productivity, better quality of software and less maintenance cost. The major advantages are – (1) Eliminate Redundancy: Through inheritance, eritance, we can eliminate redundant code and extend the use of existing classes. 2 Tapas Kr Borah “Class & Object” Question Answer for HS 2nd Year (2) Allows building secure programs: The data hiding principle helps the programmer to build secure programs that cannot be accessed by code in other parts of the program. (3) Provide ide Extensibility: Object Oriented Systems can be easily upgraded from small to large system. (4) Reduce Complexity: Software complexity can be easily managed. (5) Save development time and increases productivity: Instead of writing code from scratch, programs are developed from the standard working modules that communicate with one another. This leads to solving of development time and higher productivity. (6) Ease in division of job: Since it is possible to map objects jects in the problem domain to those objects in the program, the work in a project can be easily partitioned based on objects. (7) Allows designing simpler interfaces: Message passing technique between objects allows making simpler interface descriptions with external systems. 6. What are the advantages of object oriented programming over procedure oriented programming? Mark – 4 Answer: - The advantages of Object Object Oriented Programming over Procedure Oriented Programming are well understood from the comparative characteristics of the two. Which are in following table – Procedure Oriented Programming Object Oriented Programming 1. Large programs are divided into 1. Programs are divided into entities smaller programs known as known as objects which functions. communicate with each other. 2. More emphasis is on doing things 2. More emphasis is on data rather (algorithms). than procedure. 3. Data move openly around the 3. Data are hidden and cannot be system from function to function. accessed by external functions. 4. Most of the functions share global 4. Since the data is not global, there data which lead to the risk of is no question of any operations unwanted access and modification other than those defined within of data. the object, accessing the data. Therefore, there is no scope of unwanted access and accidental modification of data. 5. Modification of the program is 5. Modifications are easy as objects very difficult and it may affect the stay independent to declare and whole program. define. 6. No reusable of the codes. 6. Objectss can reusable, which can save many human hours of effort, is possible. An application developer can use objects like ‘list’, ‘windows’, ‘menus’, ‘event’ and many other components, which were developed by other programmer, in his/her program and thus reduc reduce program development time. 3 Tapas Kr Borah “Class & Object” Question Answer for HS 2nd Year 7. Features like encapsulation, 7. Its features like encapsulation, polymorphism, inheritance are not polymorphism, inheritance are available in this type of present. programming. 8. Follows top-down top approach in 8. Follows bottom-up bottom approach in program m design. program design. 7. What do you understand by data Encapsulation? Mark – 1 Answer: - The data Encapsulation is the way of combining both the data and the functions that operate on the data under a single unit. 8. Why is data considered safe if encapsulated? Mark – 1 Answer: - Encapsulation is the way of combining data and associated functions under single unit. Encapsulation implements abstraction. Abstraction means representation of essential features without including the background details or explanation. Thus, the outside world is given only the essential and necessary information through public members, rest of the things remain hidden through private and protected members. Therefore data are considered safe if encapsulated. 9. What is meant by Encapsulation and Abstraction? Mark – 2 Answer: - The Data Encapsulation is the way of combining both the data and the functions that operate on the data under a single unit. Abstraction is the act of representing essential features without including the background details or explanation. 10. What do you understand by data encapsulation and data hiding? Mark – 2 Answer: - The Data Encapsulation is the way of combining both the data and the functions that operate on the data under a single unit. The Data Hiding is the act of hiding members of a class from the outside world. 11. What is data hiding and data encapsulation? Mark – 3 Answer: - The non-accessibility accessibility of class members (both data members and member functions) from outside world (i.e. dose not accessed by other classes or functions) is called data hiding. A member (data and functions) declared as private or protected remains hidden from outside world. Thus through private and protected members, a class enforces enforce data- hiding. The Data Encapsulation is the way of combining both the data and the functions that operate on the data under a single unit. Encapsulation implements abstraction. Abstraction means representation of essential features without including the background background details or explanation. Thus, the outside world is given only the essential and necessary information through public members, rest of the things remain hidden through private and protected members. Therefore data are considered safe if encapsulated. 12. How does a class enforce data hiding, abstraction and encapsulation? Mark – 3 Answer: - A class binds together data and its associated functions under one unit thereby enforcing encapsulation as encapsulation means wrapping up data and associated functions together into a single unit. 4 Tapas Kr Borah “Class & Object” Question Answer for HS 2nd Year A class groups its members into three sections – private, protected and public. The private and protected members remain hidden from outside world. Thus through private and protected members, a class enforces data-hiding. The outside world is given only the essential and necessary information through public members, rest of the things remain hidden, which is nothing but abstraction. As abstraction meansns representation of essential features without including the background details or explanation. 13. What is abstraction? Answer: - Abstraction refers to the act of representing essential features without including the background details or explanations. [To understand abstraction, let us take an example of a ‘switch board’. We only press certain switches according to our requirement. What is happening inside, how it is happening etc. we needn’t know. This is abstraction, where we know only the essential things gs to operate on switch board without knowing the background details of switch board like wiring and circuit are hidden from us. Take another example of ‘car’. We are driving a car. We only know the essential features to drive a car e.g., gear handling, steering steering handling, use of clutch, accelerator, brakes etc. etc. But while driving we are not aware of internal details of the car like wiring, motor working etc. What is happening inside is hidden from us. This abstraction where we only know the essential things things to drive a car without including the background details or explanations.] Abstraction supports data hiding so that only relevant information is exposed to the user and rest of the information remains hidden from the user. The class groups its members (data and functions) into three sections – private, protected and public,, where private and protected members remain hidden from outside world and thereby support data hiding. The public members form the interface by providing the essential and relevant information information to outside world. Thus only the essential features (public members) are represented to outside world, without including the background details (private private and protected members) which is nothing but abstraction. 14. What is polymorphism? hism? Mark – 1 Answer: - Polymorphism means the ability to take more than one ne form of the same property (function or operator). C++ implements polymorphism through function overloading, overloading operator overloading and virtual function. function 15. Write short notes – Polymorphism Mark – 4 Answer: - Polymorphism: Polymorphism means the ability to take more than one o form of the same property (function or operator). C++ implements polymorphism through function overloading, overloading operator overloading and virtual function. function With the facility of the function overloading the programmer can have multiple functions with the same name but their functionality changes according to the situations. Suppose a programmer wants to have a function for calculating the absolute value va of a numeric argument. Now this argument can be any numeric data type like integer, float etc. Since the functionality remains the same, there is no need to create many functions with different names for different numeric data types. The OOP overcomes this situation by allowing the programmer to create functions with the same name like abs. This is 5 Tapas Kr Borah “Class & Object” Question Answer for HS 2nd Year called function overloading. overloading. The compiler will automatically call the required function depending on the type of the argument. Similarly, operator overloading is another form of polymorphism. An operator may exhibit different behavior in different instances. The behavior depends upon the types of data used in the operation. For example, let us consider an addition operation performed by ‘+’ operator. For two numbers, the operator will generate the sum of the numbers. If the operands are string, then the operator will generate a third string by concatenation. With the help of virtual function the programmer can use the same message to different objectss and thus make them to behave differently. These objects must be related with each other by a base class. The different objects respond differently to the same message. For example, in the shape base class we had overridden a draw( ) function in each of its ts subclasses, viz. triangle, circle, rectangle etc. Now suppose we have to make a picture by grouping a number of these elements together. For this, we can simply create an array of pointer of base class which points to the different shapes and thus draw the entire picture. 16. What is function overloading? Mark – 2 Answer: - The mechanism of defining two or more functions using the same function name that perform a variety of different tasks depending on the argument list is called function overloading.. Function overloading is one of the forms of polymorphism in Object Oriented Programming. That means, using the concept of function overloading, we can design a family of functions with one function name but with different argument lists. The function would perform different operations depending on the argument list in the function call. The correct function to be invoked is determined by checking the number and type of the arguments but not on the function type. For example, an overloaded function function add( ) call the appropriate function depending on the prototype having the same number and type of arguments as follows – //Declaration of function int add(int a, int b) //prototype 1 int add(int a, int b, int c) //prototype 2 float add(int a, float b) //prototype 3 float add(float a, float b) //prototype 4 //Function calls cout

Use Quizgecko on...
Browser
Browser