Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
979 views

Principles of Object Oriented Programming Solution

The document discusses principles of object oriented programming. It begins with multiple choice and true/false questions that test understanding of key concepts like classes, objects, encapsulation, inheritance, polymorphism and abstraction. It then provides fill in the blank questions and short answer questions to further explain object oriented programming concepts. The questions cover defining characteristics of objects, relationships between different OOP principles, real-world examples of objects and polymorphism, and differences between procedural and object oriented languages.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
979 views

Principles of Object Oriented Programming Solution

The document discusses principles of object oriented programming. It begins with multiple choice and true/false questions that test understanding of key concepts like classes, objects, encapsulation, inheritance, polymorphism and abstraction. It then provides fill in the blank questions and short answer questions to further explain object oriented programming concepts. The questions cover defining characteristics of objects, relationships between different OOP principles, real-world examples of objects and polymorphism, and differences between procedural and object oriented languages.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Principles of Object Oriented Programming

A. Tick the correct option.


1. A set of instructions given to a computer to do a particular task.
a. Program
b. High level Language
c. Object
d. None of these

2. An object is represented by two attributes, out of which one is characteristics and the other one is __.
a. Behaviour
b. Situation
c. Abstraction
d. Encapsulation

3. Name the programming technique that implements programs as an organized collection of


interactive objects.
a. Procedure Oriented Programming b. Modular Programming
c. Object Oriented Programming
d. None of these

4. Name the programming technique that specifies a series of well-structured steps and procedures
within its programming context to compose a program.
a. Procedure Oriented Programming
b. Modular Programming
c. Object Oriented Programming
d. None of these

5. Name the characteristics of Object Oriented Programming that hides the complexity and provides a
simple interface.
a. Encapsulation
b. Polymorphism
c. Abstraction
d. Inheritance

6. What is the behaviour aspect of an object represented by?


a. Member Functions
b. Data Members
c. Both a and b
d. None of these

7. What is the ability of an object to take on many forms called?


a. Polymorphism
b. Encapsulation
c. Abstraction
d. Inheritance
8. What is the term that is used to represent hierarchical relationship of generalization?
a. Polymorphism
b. Encapsulation
c. Abstraction
d. Inheritance
9. Name the art of implementing Encapsulation in Object Oriented Programming.
a. Polymorphism
b. Encapsulation
c. Abstraction
d. class

10. What is meant by state of an object?


a. Functions of the object
b. Data Members of the object
c. Content of an object
d. All of these

11. In object oriented programming, the stress is given on:


a. procedure
b. methods
c. class
d. data

12. Which of the following principle does not allow to access directly from outside the class premises?
a. data hiding
b. encapsulation
c. abstraction
d. all of the above

13. The process of combining data and functions that enables them to be together as a single entity is
called:
a. inheritance
b. encapsulation
c. classification
d. attributes

14. The process by which a class acquires the properties from another class is called:
a. polymorphism
b. inheritance
c. abstraction
d. object

15. In procedural programming, the stress is laid on:


a. class
b. function
c. data
d. object

16. A ……. is a set of objects that has common attributes and common behavior:
a. abstraction
b. encapsulation
c. class
d. function

17. Which of the following is not an object oriented programming language?


a. C++
b. Simula
c. BASIC
d. Java

18. The process of restricting the free flow of data from the outside world is known as:
a. encapsulation
b. inheritance
c. function
d. class

B. State whether the following statements are True (T) or False (F).
1. Encapsulation refers to the art of hiding the complexities and giving a simple interface. F
2. Procedure Oriented Language follows top down approach. T
3. Java is an example of Object Oriented Language. T
4. Hiding the complexity and giving a simple interface is called Inheritance. F
5. Abstraction is same as Encapsulation. F
6. A process according to which a class acquires the characteristics from another class is
Encapsulation. F
7. Procedure oriented program stresses on data. F
8. C++ is also an object oriented programming language. T
9. An object is identified by its characteristics. T
10. Function is a set of objects that share the common state and behaviour. F
11. Encapsulation keeps data safe from outside interference. T

C. Fill in the blanks.


1. An object is an identity with certain characteristic and behaviour.
2. The values/attributes of the characteristics of an object are called the state of an object.
3. All the complexities of a program should be encapsulated in such a way so that Abstraction is obtained.
4. Inheritance allows us to encompass the parent class’ state and behaviours into its child.
5. Poly-means many and Morphism means forms.
6. Encapsulation is a principle of Object Oriented Programming (OOP) that binds together characteristics and
behaviour of an object.
7. Abstraction is the reduction of a particular body of data to a simplified representation of the whole.
8. The characteristics of an object are represented through data members and behaviour is represented through
member functions.
9. A program written in a high level language is also called Source Code.
10. All objects have identity and are distinguishable even if the constituent components are same.
11. An act of using essential features without including background details is called Data Abstraction.
12. In an Object Oriented Programming, the stress is given on data.
13. Wrapping of data and function together as a single unit is called Encapsulation.
14. An object has unique identity through which it may differ with some characteristics and behaviour.
15. The objects may communicate with each other through function.
16. In POP, the global data are loosely attached to the function.
17. The process by which a class acquires the property of another class is known as inheritance.
18. In Object Oriented Programming, using a function for many purposes is termed as polymorphism.
19. Data abstraction is the fundamental concept on object oriented programming language.
20. Feature of wrapping data and function as a single unit is called encapsulation.
21. Inheritance principle of object oriented programming promotes reusability feature.

Answer the following questions.


1. Give two examples of real world objects. Also specify their characteristics and behaviour.
Ans: The book that you are reading now is also an example of an object. Its characteristics are
represented by the information it holds, size, volume and its colour. The behavioural aspect is referred
by the methods for accessing the information it contains.
A pen is also an example of object. Its characteristics are represented by its colour, shape, brand, etc.,
and its behaviour is represented by its use such as writing, drawing, etc.

2. What do you understand by state of an object? Explain with an example.


Ans: The state of an object is the particular condition it is in. For example, a lamp can be on or off. The
lamp’s switch (methods) turn lamp on and turn lamp off are used to access the state of the lamp.

3. How are objects implemented in Software?


Ans: In software the characteristics of an object are represented through data members and behaviour
is represented through member functions.

4. What is abstraction? How is encapsulation related to it?


Ans: Abstraction is a principle of Object Oriented Programming (OOP) that hides certain details and
only shows the essential features of the object.
Encapsulation is also frequently confused with abstraction, since the two concepts are closely related.
Abstraction is a process of hiding the complexity and giving a simple interface. Encapsulation on the
other hand is the mechanism by which the abstraction is implemented.
5. Define Encapsulation.
Ans: Encapsulation is a principle of Object Oriented Programming (OOP) that binds together
characteristics and behaviour of an object into a single unit represented by a class.

6. Explain the term object using an example.


Ans: The book that you are reading now is also an example of an object. Its characteristics is
represented by the information it holds, size, volume and its colour. The behavioral aspect is referred
by the methods for accessing the information it contains. For example, you can open the book, turn a
page, read a paragraph, and search the table of contents, and so on. The information contained in the
book along with the methods for accessing it for gaining information is what comprises the object
known as this book.

7. What is Object Oriented Programming?


Ans: Object Oriented Programming is an approach in which stress is laid on data rather than functions.
The data values remain associated with the functions of a particular block of the program so as to
encourage data security.
It is a technique of implementing programs which are organized as a co-interactive collection of
objects, each of which represents an instance of a class.

8. State differences between Procedure Oriented Language and Object Oriented Languages.
Ans.
Procedure Oriented Programming Object Oriented Programming
A large program is divided into smaller segments or A program is represented as an object.
procedures.
The stress is put on function rather than data. The stress is put on data rather than functions.
It allows data to flow freely throughout the The data is restricted, to be used in a specific program
program. area.
It follows top-down programming approach. It follows bottom-up programming approach.

9. State the four characteristics/principles of Object Oriented Programming.


Ans: Encapsulation, Abstraction, Polymorphism and Inheritance.

10. Give a real life example of Polymorphism.


Ans: Example in real life of polymorphism- Shobha is a married girl and mother of 2 children doing
teaching job then she is a women first, teacher in a school when she is at school, wife of someone at
home, mother of her children and obvious daughter of someone. Thus you can see that Shobha plays
different roles at different times that is what polymorphism is.

11. What are the elements of Object Oriented Programming? Explain each in one sentence.
Elements of Object Oriented Programming are:
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
If I must describe each of these concepts in just one sentence, then:
 For Abstraction, I will say Eliminate the irrelevant & amplify the essential
 Encapsulation means Hiding the unnecessary
 Inheritance helps in Modelling the similarities
 Polymorphism means Same function different behavior
12. List the features of Object Oriented Programming.
Some of the features of Object Oriented Programming are:
1. It gives stress on data items rather than functions.
2. It makes the complete program/problem simpler by dividing it into number of objects.
3. The objects can be used as a bridge to have data flow from one function to another.
4. The concept of data hiding enhances security in programs.
5. It is highly beneficial to solve complex programs.

13. Name any two Object Oriented Programming and Procedure Oriented Programming
languages.
Object Oriented Programming languages ⇒ C++, Java
Procedure Oriented Programming languages ⇒ BASIC, C

14. Which OOP principle implements function overloading?


Polymorphism implements function overloading.

15. Define the following terms:


(a) Encapsulation with an example
Wrapping of data and functions that operate on that data into a single unit is called Encapsulation.
In object-oriented programming languages, encapsulation is used to refer to one of two related but
distinct notions, and sometimes to the combination thereof:
 A language mechanism for restricting direct access to some of the object's components.
 A language construct that facilitates the bundling of data with the methods (or other functions)
operating on that data

For the purposes of our discussion, we will look at both properties which are defined under
Encapsulation. First one being restricting direct access to some of the object's components.
Rephrasing it in a little simpler wording, it means hiding some of the implementation details from
the outside world. This is also known as Data hiding. The second point in the definition above on
facilitating bundling of data with methods means characteristics and behaviour are combined into a
single unit.
Let's look at an ATM. You go to the ATM, swipe your card, enter your pin and then enter the
amount of money you want to withdraw. ATM processes your request and gives you the money. If
you were to see ATM through object-oriented lens you will realize that ATM contains different
denominations of currency notes and all the operations to read your debit card, verify your pin, count
currency notes, dispense the money to you, etc. ATM combines all the different denominations of
currency notes and all the operations required to withdraw money into a single unit. You interact
with this one machine which is wrapping all this together and very conveniently withdraw cash. Here
ATM can be viewed as a class which is wrapping together the data which are the currency notes and
methods which are the operations required to withdraw money into a single unit. In other words, we
can say that ATM encapsulates money and operations required to withdraw it into a single unit.
You can interact with the ATM only through the numeric keypad and the onscreen options it
provides. You cannot access the details about internal data like how much money does this ATM
has, how many notes of each denomination are present, how much money has been withdrawn so far,
etc. This is the Data Hiding concept of Encapsulation.

Encapsulation & Abstraction are complementary


You might be a little confused at this point between Abstraction and Encapsulation as I said
something similar for Abstraction too that it hides the background details. In fact, Abstraction and
Encapsulation are complementary concepts. Abstraction solves the problem at the design level
whereas Encapsulation solves the problem at implementation level. Abstraction cares about what
something does but not how it does it. Encapsulation cares about how something does what is does
such that others don't have to worry about the implementation details. Going back to our ATM
example, when I am designing the ATM, I will think about what steps the user needs to follow in
order to withdraw money. I will list down that the user will swipe his card, enter pin, enter amount
and finally collect cash, that's Abstraction at work. I am only focusing on things that are relevant
from the user's point of view and eliminating all other stuff. When I am actually making that ATM, I
will look at the design and go through the steps and think about how I should build this ATM so that
the user can withdraw cash by just following these steps and not worrying about anything else, that's
Encapsulation. Hence, we can say that Encapsulation is a way to implement Data

Abstraction. Encapsulation hides the details of the implementation of an object.


Java supports Encapsulation through classes and access specifiers - public, private and protected. We
will look into this a little bit later in this course.

(b) Data Abstraction with an example


Data Abstraction is the act of representing the essential features without knowing the background
details.
Abstraction refers to the act of representing essential features without including the background
details or explanations. The core things here are - essential features and not including background
details. Let’s first try to understand the essential features part.
Real-World Example of Abstraction

Consider this example where a lady has brought her pet cat to a vet for treatment. Let's see the cat
from Vet's point of view, what are the essential features of the cat for the Vet?
Vet’s aim is to examine the cat and diagnose any health issues the cat is having so for the Vet the
essential thing is the body structure of the cat. The Vet is interested in the different body parts of the
cat like heart, lungs, stomach, kidney, intestine, liver bladder to ensure everything is functioning
perfectly and the cat is healthy. Some features irrelevant to the Vet are the favourite toy of the cat,
what food the cat likes the most or what is the color of the cat's bed. These things are of little h elp to
diagnose any health problems the cat is having so they are mostly irrelevant to the Vet.
Now let's see the cat from the lady's point of view

Things about the cat that matter to the lady are cat's favorite toy, favorite food, color of cat's bed.
Because cat is the lady’s pet, everything that makes the cat feel happy is essential to the lady. On the
other hand, things like the anatomy of the cat, scientific names of cat's body parts, etc are irrelevant
to the lady as these things doesn’t help the lady to make her cat feel happy.
So now I hope you have a better sense of how essential information depends upon the problem at
hand. Depending on the problem domain, we need to eliminate the irrelevant & amplify the
essential.
There were two core parts in the definition of Data Abstraction. We have already looked at and
understood the essential features part. Now, let's try to understand hiding the background details part
with the example of a switchboard.
How do you switch on a light or fan in your room at home? Obviously, you go to the switchboard,
press the required switch to turn ON the light or fan. You don't worry about how that switch is
turning the light ON, what is the internal wiring, etc. Switchboard is hiding all such background
details from you and providing you a very simple way to switch on or off the light.
You encounter such examples of Abstraction every day in your life. While driving a car the driver
presses accelerator to speed up the car and brakes to stop it. He doesn’t worry about all t he
background details regarding how the engine is working, how the pistons in the engine are firing up
at an increased RPM to speed up the car or how the brakes are working to slow down and stop the
car. All these details are hidden away from the driver and he is presented with two very simple
interfaces – accelerator and brakes to speed up and slow down the car. Such hiding of background
details through Abstraction is making our day to day lives simpler.
Java supports Abstraction through abstract classes & interfaces.

Question 9
Explain 'Inheritance' with a suitable example.
Inheritance enables new classes to receive or inherit the properties and methods of existing classes.
In the world around us, we are surrounded with entities that are connected to each other with "is-A"
relationship. The concept of Inheritance helps us to model such "is-A" relationships in software. I
know, right now you are a little confused with what "is-A" relationships I am talking about. So as
always, lets look at some examples to develop more intuition about these "is-A" relationships and
concretely understand Inheritance.

Take the example of traffic on the road, what this traffic consists of? There are Cars, Bikes, Scooters,
Auto rickshaw, Buses, Trucks, etc. All of these have certain common characteristics and behaviour
— they can move on the road and transport people and goods from one place to another. These
commonalities help us to categorize them under one common category called Vehicles.
Now we can say that Car is a vehicle, Bus is a vehicle, Bike is a vehicle, Auto is a vehicle and so
on. This is just one of the examples of "is-A" relationship around us. This helps us to conveniently
say that there are many different types of vehicles on the road rather than calling out each of the type
separately.

Building on it a little further, there are many different types of cars - i10, i20, XUV 500, Fortuner,
Baleno, etc. This is also an example of is-A relationship where each one of these is-A car. Similarly
for bikes, you may have a TVS Apache, Bajaj Pulsar, KTM Duke or Harley Davidson and each of
them is a Bike. You can extend this similarly for buses and trucks as well. I hope this helps you to
see how we can use is-A relationship to categorize entities based on common characteristics and
behaviours. Also, most of the times these relationships are hierarchical — there are vehicles then
cars, bikes then the different models of these cars and bikes, etc.
You can look around and find other such examples as well like the different kind of animals - dog is
an animal, cat is an animal, goat is an animal, elephant is an animal. Of course, you can make it more
hierarchical like this

We have a base class Vertebrates for all the animals with a backbone. From there we can derive a
mammals class to group animals that have mammary glands, give live birth to young ones, have 3
middle ear bones, etc. Mammal is-A vertebrate. Then from mammal we can derive specific examples
of mammals like humans, whales, dog, Monkey, etc. Each one of these human, whale, dog, Monkey
is-A mammal. This is another example of hierarchical is-A relationship where whale in addition to
its own unique characteristics and behaviour, inherits all the characteristics and behaviour of
mammals and vertebrates. Similarly, we can expand it for Birds, Reptiles, Fishes & Amphibians as
well.
Inheritance helps in adapting to changes in software:
 It helps in avoiding code duplication. We can put the common code in a base class and share
it with derived classes.
 It also reduces the need for code manipulation — to implement a new functionality we can
always create a new derived class rather than changing the code of the base class which may
impact the entire application

16. What is meant by Polymorphism? Explain with an example.


In object-oriented programming, Polymorphism provides the means to perform a single action in
multiple different ways.
The word Polymorphism means having many forms. In object-oriented programming languages,
Polymorphism provides the means to perform a single action in multiple different ways.

You might also like