Multiple Choice Questions
Question 1
The term OOP stands for ...........
- Object Oriented Procedure
- Object Oriented Packet
- Object Oriented Programming ✓
- Object Orientation Procedure
Explanation
Short form of Object Oriented Programming is OOP
Question 2
Object Oriented Programming mainly uses ...........
- Top-down approach
- Top-down and bottom-up approach
- Bottom-up approach ✓
- None of these
Explanation
In Bottom-up approach to programming, the smallest individual parts of the system are first identified and specified in detail. These individual parts are then linked together to form larger and larger components until the entire program is ready. Object Oriented Programming follows bottom-up approach because in OOP we first identify the smallest parts of the program i.e. the objects. We then combine these objects to develop the complete program.
Question 3
An object belonging to a particular class is known as a/an ........... of that class.
- Interface
- Instance ✓
- Alias
- Member
Explanation
The terms object and instance are often interchangeable.
Question 4
Objects that share the same attributes and behaviour are grouped together into a/an ...........
- Interface
- Instance
- Alias
- Class ✓
Explanation
A class is a template or blueprint for multiple objects with similar features and maybe regarded as a specification for creating similar objects.
Question 5
........... is the technique of binding both data and methods together to keep them safe from unauthorised access and misuse.
- Abstraction
- Inheritance
- Encapsulation ✓
- Polymorphism
Explanation
This is the definition of Encapsulation.
Question 6
........... refers to the act of representing essential features without including the background details.
- Abstraction ✓
- Inheritance
- Encapsulation
- Polymorphism
Explanation
This is the definition of Abstraction.
Question 7
Procedure Oriented Programming mainly uses ...........
- Top-down approach ✓
- Top-down and bottom-up approach
- Bottom-up approach
- None of these
Explanation
In Top-down approach, we first list down the main steps involved in solving a problem. After that we break up each of those steps into their sub-steps and so on. This is similar to Procedure Oriented Programming where we first write a main function and then call sub-functions from the main function to perform each of these steps.
Question 8
........... is the feature using which one class acquires the properties of another class.
- Abstraction
- Inheritance ✓
- Encapsulation
- Polymorphism
Explanation
This is the definition of Inheritance.
Question 9
The ability of a method or object to take on multiple forms is called ...........
- Abstraction
- Inheritance
- Encapsulation
- Polymorphism ✓
Explanation
This is the definition of Polymorphism.
Question 10
An object has ...........
- Attributes
- State
- Behaviour
- All of these ✓
Explanation
An object is an identifiable entity that has a set of attributes, behaviour and state. So all of the options are applicable for objects.
Question 11
A class is ...........
- An object factory
- A blueprint to create objects
- A specification for objects
- All of these ✓
Explanation
A class is a template or blueprint for multiple objects with similar features and maybe regarded as a specification for creating similar objects. So all of the options are applicable for class.
Question 12
........... represents an entity in the real-world with its identity and behaviour.
- A class
- An object ✓
- A procedure
- A method
Explanation
An object is an identifiable entity with a set of attributes, behaviour and state.
Question 13
........... is a template to create similar objects that share common characteristics and behaviour.
- A method
- A procedure
- An attribute
- A class ✓
Explanation
A class is a template or blueprint for multiple objects with similar features and maybe regarded as a specification for creating similar objects.
Question 14
The values of an object's ........... represent the state of the object.
- methods
- procedures
- attributes ✓
- classes
Explanation
Attributes of an object define its state.
Question 15
The terms object and ........... are often interchangeable.
- instance ✓
- behaviour
- attribute
- state
Explanation
The terms object and instance are often interchangeable.
Question 16
Procedure Oriented Programming gives importance to
- Instructions only ✓
- Instructions and data
- Data only
- None of these
Explanation
Procedure Oriented Programming is focussed on the steps involved in solving a problem so it gives importance to instructions only.
State whether the given statements are True or False
Question 1
High-level languages are closer to computer hardware.
False
Question 2
Code and data are held separately in a procedural language.
True
Question 3
Code and data are held separately in OOP.
False
Question 4
When you wrap data and related methods into a single unit, it represents encapsulation.
True
Question 5
The class that is derived from another class is called a subclass.
True
Question 6
Classes can be derived from other classes.
True
Question 7
Inheritance allows a class to acquire the properties of another class.
True
Question 8
A class is a blueprint for the attributes and behaviours of a group of objects.
True
Question 9
Objects from the same class do not share the same definition of attributes and behaviours.
False
Question 10
A class is a specification about the object.
True
Question 11
Only one instance can be created from a single class.
False
Question 12
A class is a user-defined data type.
True
Question 13
The terms object and instance are often interchangeable.
True
Question 14
Collectively, the values of an object's attributes represent the state of the object.
True
Question 15
Objects interact with each other through messages.
True
Assignment Questions
Question 1
Write a short note on Object Oriented Programming.
Answer
Object Oriented Programming (OOP) is a programming paradigm which revolves around the behaviour of an object, and its interactions with other objects and classes. In OOP, the program is organised around data or objects rather than functions or procedures. It follows the design principles of Data Abstraction, Encapsulation, Inheritance, and Polymorphism.
Question 2
Explain the phrase, "Everything is an object".
Answer
The world around us is made up of objects such as people, vehicles, buildings, streets, mobiles, television, etc. Each of these objects has the ability to perform specific actions and each of these actions influences the other objects in some way or the other. The objects around us can be divided into the following categories:
Tangible Objects — These are the objects that we can see and touch. For example, chair, pen, book, door, etc.
Conceptual Objects — These objects exists as a conceptual entity that we cannot touch. We may or may not be able to see them. For example, an email, a bank account, a song, patents, etc.
Roles — Roles played by people, such as a student, a teacher or a clerk.
Events — An event is something occurring in a system or an organisation. For example, a sale or a purchase in a departmental store, someone's birthday, etc.
Question 3
What are the characteristics of object-oriented programming?
Answer
The characteristics of object-oriented programming are:
- It follows a bottom-up approach.
- The program resulting from object-oriented programming is a collection of objects. Each object has its own data and a set of operations.
- OOP restricts the free movement of data and the functions that operate on it.
- A properly defined class can be reused, giving way to code reusability.
- The concept of object-oriented programming models real-world entities very well.
- Due to its object-oriented approach, it is extremely useful in solving complex problems.
Question 4
What are the limitations of object-oriented programming?
Answer
Limitations of object-oriented programming:
- The size of the programs created using this approach may become larger than the programs written using procedure-oriented programming approach.
- Software developed using this approach requires a substantial amount of pre-work and planning.
- OOP code is difficult to understand if you do not have the corresponding class documentation.
- In certain scenarios, these programs can consume a large amount of memory.
Question 5
What do you mean by Abstraction? Give suitable examples.
Answer
Abstraction refers to the act of representing essential features without including the background details. For example, a building can be viewed as a single component (e.g., hospital) rather than separate components like cement, bricks, and rods. Abstraction is relative to the perspective of the viewer.
Question 6
Explain the term Encapsulation using appropriate examples.
Answer
Encapsulation is a mechanism that binds together code and the data it manipulates. It keeps them both safe from the outside world, preventing any unauthorised access or misuse. Only member methods, which are wrapped inside the class, can access the data and other methods. For example, an ATM contains different denominations of currency notes and it provides a set operations to the user to withdraw money. The different denominations of currency notes is the data, the set of operations are the methods and the ATM encapsulates them into a single unit enabling us to withdraw cash conveniently.
Question 7
What are the characteristics of procedural programming?
Answer
The characteristics of procedural programming are:
- Procedural programming follows a top-down approach.
- The program is divided into blocks of codes called functions, where each function performs a specific task.
- Procedural programs model real-world processes as 'procedures' operating on 'data'.
- The data and functions are detached from each other.
- The data moves freely in a program.
- It is easy to follow the logic of a program.
- A function can access other function's data by calling that function.
Question 8
What are the limitations of procedural programming?
Answer
The limitations of procedural programming are:
- Procedural programming mainly focuses on procedures or functions. Less attention is given to the data.
- The data and functions are separate from each other.
- Global data is freely moving and is shared among various functions. Thus, it becomes difficult for programmers to identify and fix issues in a program that originate due to incorrect data handling.
- Changes in data types need to be carried out manually all over the program and in the functions using the same data type.
- Limited and difficult code reusability.
- It does not model real-world entities (e.g., car, table, bank account, loan) very well where we as a human being, perceive everything as an object.
- The procedural programming approach does not work well for large and complex systems.
Question 9
Provide real-life examples to explain the term, Inheritance.
Answer
To explain Inheritance, let's take the example of traffic on the road. This traffic has some commonalities. It consists of things that can move on the road and transport people and goods from one place to another. We call these things vehicles. These vehicles differ from each other in certain aspects like whether it transports passengers or goods, how many passengers it can accommodate at a time, whether it is a two-wheeler or four-wheeler, etc. So, we have different types of vehicles like Cars, Bikes, Scooters, Auto rickshaw, Buses, Trucks, etc. We can represent this traffic using Inheritance as shown below. Here, Vehicle is the base class having the common characteristics and behaviours of all Vehicles. Then we have Car, Bike, Bus, Truck as subclasses of Vehicles.
Question 10
Polymorphism means different forms. Explain Polymorphism in Java and provide examples to support your answer.
Answer
Polymorphism is the ability of a method or an object to take on multiple forms. In OOP, polymorphism allows an operation to exhibit different behaviour in different instances. The behaviour depends upon the type of data used in the operation. For example, consider the operation of addition. For two numbers, the operation will generate a sum. If the operands are strings, then the operation would produce a third string by concatenation.
Question 11
Explain the difference between Inheritance and Encapsulation with suitable examples.
Answer
Encapsulation is a mechanism that binds together code and the data it manipulates whereas Inheritance is the mechanism by which a class acquires the properties and methods of another class. An ATM binding together the different denominations of currency notes and all the operations required to withdraw cash is an example of Encapsulation. Classifying Vehicles as Car, Bike, Bus, Truck, etc. is an example of Inheritance.
Question 12
What are the differences between Procedural Programming and Object-oriented Programming?
Answer
Procedural Programming | Object-Oriented Programming |
---|---|
Follows a top-down approach | Follows a bottom-up approach |
Instruction Oriented | Data Oriented |
The abstraction is at procedure (function) level. | The abstraction is at object (class) level. |
The sequence of events in a large program is divided into functions. | Entire program is divided into objects. |
Interaction with program is via direct function calls. | Interaction with program is via functions defined in the class only. |
Real world is represented by 'procedures' operating on data. | Real world is represented by objects and the operations that can be performed on these objects. |
Data and functions are separate. | Data and functions are encapsulated into a single unit. |
Data security is less as it contains lesser features to protect the data. | It is more secure as one of its primary features include data hiding. |
A function can access any other function's data by calling that function. | Only the data whose access has been granted can be accessed by another function. |
Limited and difficult code reusability. | Versatile and easy code reusability. |
Code is difficult to modify, extend and maintain. | Code is easy to modify, extend and maintain. |
Some examples of Procedural Programming languages are C, COBOL, Pascal. | Some examples of Object Oriented languages are C++, Java, C#. |
Question 13
What are objects? Give five examples.
Answer
Objects are identifiable entities that have a set of attributes, behaviour and state. Five examples of objects are car, pen, mobile, email, bank account.
Question 14
Explain the following statement — "Class is a specification for objects".
Answer
As class describes the common attributes and behaviours of its objects in detail, hence we can say that a class is a specification for objects. It can be viewed as a template or blueprint for multiple objects with similar features.
Question 15
Explain the following statement — "Abstraction is relative to the perspective of the viewer".
Answer
It means that there can be different abstractions of an entity depending on the viewpoint of the user. Lets take the example of a pet dog. From the viewpoint of the dog's owner, the things that are essential for the dog are his favorite food, the colour of his food bowl, his favorite game that he enjoys playing with his owner, his preferred time for walk, etc. From the viewpoint of the dog's vet (doctor for animals), the important things about the dog are whether the dog's body functions are normal or not to ensure that the dog is healthy. Like this, there can be two abstractions for the dog — one for the dog's owner and the other for the dog's vet.
Question 16
Mention five possible attributes and three possible behaviours of the following entities:
i. Employee
Answer
Attributes | Behaviours |
---|---|
Name | Take leave |
Employee Number | Calculate Salary |
Pan Number | Calculate Income Tax |
Desgination | |
Address |
ii. Teacher
Answer
Attributes | Behaviours |
---|---|
Name | Teach Class |
School Name | Conduct Tests |
Subject | Maintain discipline in class |
Class | |
Address |
iii. Laptop
Answer
Attributes | Behaviours |
---|---|
Manufacturer | Start laptop |
Model | Shutdown laptop |
Processor | Run applications |
RAM | |
Hard Disk |
iv. Radio
Answer
Attributes | Behaviours |
---|---|
Manufacturer | Select Band (AM/FM) |
Model | Tune Station |
Bands (AM/FM) | Adjust Volume |
Antenna | |
Colour |
v. Table
Answer
Attributes | Behaviours |
---|---|
Type | Put things on Table |
Colour | Remove things from Table |
Weight | Clean Table |
Height | |
Width |
vi. Telephone
Answer
Attributes | Behaviours |
---|---|
Manufacturer | Make calls |
Model | Receive Calls |
Caller Id Display | Adjust Ringer Volume |
Type (Corded/Cordless) | |
Price |
vii. Dish Washer
Answer
Attributes | Behaviours |
---|---|
Manufacturer | Start Wash |
Model | Stop Wash |
Colour | Load Utensils |
Energy Rating | |
Noise Level |
viii. Microwave
Answer
Attributes | Behaviours |
---|---|
Manufacturer | Start Microwave |
Model | Stop Microwave |
Colour | Set heating time |
Energy Rating | |
Price |
ix. Aeroplane
Answer
Attributes | Behaviours |
---|---|
Manufacturer | Take off |
Model | Land |
Capacity | Auto pilot |
Engine | |
Airline |
x. Tree
Answer
Attributes | Behaviours |
---|---|
Name | Release Oxygen |
Age | Absorb Carbon dioxide |
Height | Grow new leaves |
Scientific Name | |
Diameter |
xi. Cloud
Answer
Attributes | Behaviours |
---|---|
Name | Rain |
Type | Lighting |
Altitude | Movement |
Texture | |
Shape |
Question 17
Explain in detail how a class is different from an object.
Answer
The class is just a specification of the object. The attributes and methods in the class are thus declarations that do not contain any values. However, the object is a concrete instance of a class with properly defined values for each attribute and behaves as per the methods of the class.
Question 18
Give the reason why a class is known as:
i. An object factory
Answer
A class is called an object factory because objects are created from the class that contains common attributes and behaviour. The class behaves like a specification for creating such similar objects.
ii. A composite data type
Answer
A class is composed of member variables which are of different data types. Hence, a class can be viewed as a composite data type.
iii. A user-defined data type
Answer
The entire data and the code, contained in an object, becomes a user-defined data type using the concept of a class. The class may be considered as a data type and an object as a variable of that data type. For example, once the Bird class has been defined, the statement
Bird parrot;
will create a parrot object belonging to the Bird class.
Question 19
How are classes and objects inter-related? Support your answer with an example.
Answer
A Class is used to create various Objects that have different attributes and common behaviours. Each object follows all the features defined within a class. That is why class is also referred to as a blue print or prototype of an object. For example, a class representing a car will have the following characteristics and behaviours:
Attributes | Behaviours |
---|---|
Model | Start car |
Registration Number | Stop car |
Colour | Apply break |
We can create two objects of this class to represent a red i20 car and a white Baleno car as shown below:
Attributes | Behaviours | State |
---|---|---|
Model | Start car | Model: i20 |
Registration Number | Stop car | Registration Number: KA 01 AA 1234 |
Colour | Apply break | Colour: Red |
Attributes | Behaviours | State |
---|---|---|
Model | Start car | Model: Baleno |
Registration Number | Stop car | Registration Number: WB 01 AZ 6789 |
Colour | Apply break | Colour: White |
Question 20
What do you understand by the phrase, "Objects encapsulate state and behaviour"?
Answer
An object stores its state in member variables and exposes its behaviour through the member methods. The member methods operate on member variables and serve as the primary mechanism to interact with the object. Only the member methods which are wrapped inside the class can access the data and change its state. Hence, the state and behaviour are said to be encapsulated by the object, hiding internal state and requiring all interaction to be performed through the methods of the object.
Question 21
How do objects communicate with each other? Explain.
Answer
Objects communicate with each other by sending messages. The sender object requests the receiver object to perform an action.
Question 22
If ClassA inherits from ClassB, which is a superclass? Which is a subclass?
Answer
ClassB is superclass and ClassA is subclass.
Question 23
What are the other names for superclass and subclass?
Answer
superclass is also known as base class and subclass is also known as derived class.
Question 24
If we have Student, Graduate, and PostGraduate classes, which one will be the superclass?
Answer
Student will be the superclass.