Class as the basis of all computation
Class as the basis of all computation
Question 1
Question 2
Abstraction refers to the act of representing essential features without including the
background details or explanations.
Question 3
Encapsulation is the way of combining both data and the functions that operate on that data
under a single unit.
Question 4
Question 5
Question 6
Question 7
The data in an object is accessed through the member functions of the object known
as methods.
Question 8
The real world objects are implemented in software form by representing their state through
variables or data items called member variables.
Question 9
Question 10
The values of data member or member variables at any given point of time determine an
object's state.
Question 11
A class is a blueprint of set of objects that have a common structure and common behaviour.
Question 12
Question 13
In Object Oriented Programming, objects are defined by defining a class for them.
Question 14
Question 15
Question 16
Question 17
Question 18
Question 19
Static or class variables are created once for entire class and are shared by all the objects. [(a)
static or class variables (b) Instance variables (c) shared variable (d) none of the above]
Assignment Questions
Question 1
Answer
1. A car is an object.
Characteristics — It has colour, length, width, height, model, a steering wheel, gear
box etc.
Behaviour — Accelerating, changing gears, braking, etc.
2. A book is an object.
Characteristics — It has a name, ISBN number, author, publisher, price, etc.
Behaviour — Book can be read, opened/closed, borrowed/returned, etc.
3. A student is an object.
Characteristics — It has a name, age, address, class, section, subjects etc.
Behaviour — He/she can study, play, sleep.
Question 2
'An object's state and behaviour are two distinct things yet linked to one another'. Comment.
Answer
An object's state refers to the values of its attributes or properties, while an object's behaviour
refers to the actions or methods that the object can perform.
An object's state and behaviour are closely linked and can affect each other. The behaviour of
an object is dependent on its current state, and changes in its behaviour can cause changes in
its state.
For example, taking fan as a real-world object, a fan in OFF state might have a behaviour of
switching it on. However, if the fan's state changes to ON then behaviour of switching the fan
ON may no longer be applicable.
Conversely, an object's behavior can also affect its state. Fan object will have a behaviour of
switching it OFF. Calling this behaviour will change the state of the fan to OFF.
Question 3
Answer
Abstraction refers to the act of representing essential features without including the back
ground details or explanations.
Question 4
Define Encapsulation.
Answer
The wrapping up of data (characteristics) and functions (behaviour) into a single unit, called
class, is known as Encapsulation. It is a way to implement data abstraction.
Question 5
Answer
When the objects need to interact with one another, they pass/request information to/from
one-another. This interaction is known as message passing. Objects interact with one another
through messages.
Question 6
Answer
Question 7
Answer
Information hiding is the process of hiding all the secrets of an object that do not contribute
to its essential characteristics. The structure of an object is hidden, as well as the
implementation of its methods. Only the essential characteristics of object are visible.
Question 8
Answer
In Java, every class is a subclass of Object class and so, all objects inherit the methods of the
Object class. Thus, it is not possible for objects to be without any methods.
Question 9
What is a class ?
Answer
A class is a blueprint of a set of objects that have a common structure and common
behaviour.
Question 10
Answer
Abstraction refers to the act of representing essential features without including the back
ground details or explanations.
Yes, there can be be multiple abstractions of a real-world entity because abstraction is always
relative to the purpose or user.
For instance, if we consider a student, we can talk of anything that belongs to her in the real
world like her name, family, parents' profession, locality she lives in, marks obtained by her,
her roll-number in the class, her medical history, her talents, her awards etc. But when we
talk of a student result tracking system, the abstraction for it would be her roll-no, name,
marks obtained etc. For extra-curricular activities, the abstraction would be her roll-no,
talents and awards.
Question 11
Answer
For example, the abstract concept of a car can be represented in the program by a class with
properties like make, model, and year, and methods like start() and stop().
Question 12
Answer
We map an abstraction into software form by depicting all its physical characteristics through
data items and all its functionality i.e., behaviour through functions which are called methods.
Thus, in an object:
Question 13
Answer
Thus, a real world object is represented in software terms as a software bundle of variables
and related methods.
Question 14
Answer
Abstraction and encapsulation are complementary concepts. Abstraction focuses upon the
observable behaviour of an object, whereas encapsulation focuses upon the implementation
that gives rise to this behaviour. Encapsulation is most often achieved through information
hiding, which is the process of hiding all the secrets of an object that do not contribute to its
essential characteristics, the structure of an object is hidden, as well as the implementation of
its methods. Only the essential characteristics of object are visible.
Thus, encapsulation is a way to implement data abstraction. Encapsulation hides the details of
the implementation of an object.
Question 15
Answer
There can be many classes defined for an entity, for a class represents an abstraction. An
abstraction is always relative to the purpose or user.
For instance, if we talk of a student, we can talk of anything that belongs to her in the real
world like her name, family, address, marks obtained by her, her roll-number in the class, her
medical history, her talents, her awards etc. But when we talk of a student result tracking
system, the abstraction for it would be - her roll_no, name, marks obtained etc. For extra-
curricular activities, the abstraction would be - her roll_no, talents and awards.
Question 16
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 17
Answer
When the objects need to interact with one another, they pass/request information to/from
one-another. This interaction is known as message passing. Objects interact with one another
through messages.
For instance, to represent a school's working, we can represent its various units — admission
desk, fee department, enquiry desk, academic department, activities department, transport
department etc.
Now, let us consider a new student who wants admission in the school. Here, the admission
desk will get the details of the student and direct him to fee department to deposit the fee,
which will require the admission desk and fee department to communicate regarding the
amount of fee to be paid by the student. This communication takes place through message
passing.
Question 18
Answer
In Java, the class forms the basis of all computation. A class is a blueprint for an object and
an object is an instance of a class. It allows a programmer to define all of the properties and
methods that define an object.
Anything that has to exist as a part of a Java program has to exist as a part of a class, whether
that is a variable or a function or any other code-fragment. Without classes, there can be no
objects and without objects, no computation can take place in Java. Thus, classes form the
basis of all computation in Java.
Question 19
Answer
A class is used to create various objects that have different characteristics and common
behaviour. 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 and an object is referred to as an
instance of a class. This way, we can say that they are inter-related.
Question 20
Can there be multiple classes for the complete representation of a real world entity ? Give
examples.
Answer
Yes, there can be multiple classes for the complete representation of a real world entity.
For example, let us consider a school. A school has multiple units to perform different
functions — enquiry/admission desk, fee counter, academic board, activities board, sports
board, student help desk, etc. All these units work in conjunction for the efficient working of
the school. Now, each unit can be treated as a different class which has required member
variables and methods to perform their tasks.
Question 21
Answer
A class is a blueprint of a set of objects that have a common structure and behaviour. Once a
class is defined, any number of objects can be created. All the objects have common
attributes and behaviour. Thus, classes are said to be abstractions of or a set of objects. We
can think of a class as a cookie cutter and an object as an actual cookie.
Question 22
Answer
A class represents a blueprint based on which actual memory is allocated for data members
and methods of a class and it is called an object. So an object gets its own copy of data
members and methods based on class details. Hence, objects are said to be instances of a
class.
Question 23
Consider the following code fragment assuming that a class namely Student has been defined
earlier :
Student stud1;
What is above statement doing ?
Answer
The given statement declares a variable stud1 of type Student. It will be used to refer to
a Student object. stud1 is a null reference.
Question 24
The statement Student stud1; only declared a variable stud1 of type Student.
The statement Student stud1 = new Student( ); not only declares a variable but it
actually instantiates the object stud1 of class Student by using the 'new' keyword.
Question 25
Write a program to accept a number and check and display whether it is a spy number or not.
(A number is spy if the sum of its digits equal the product of its digits.)
Example : Consider the number 1124,
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1 x 1 x 2 x 4 = 8.
Answer
import java.util.Scanner;
public class KboatSpyNumber
{
public static void main(String args[]) {
sum += digit;
prod *= digit;
num /= 10;
}
if (sum == prod)
System.out.println(orgNum + " is Spy Number");
else
System.out.println(orgNum + " is not Spy Number");
}
}
Output