Objective Type Questions
Question 1
An object is an identifiable entity with some characteristics and behaviour.
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
Object can be defined as a software bundle of variables and related methods.
Question 5
Real world objects encapsulate characteristics and behaviour.
Question 6
Objects interact with one another through messages.
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
The behaviour of the object is represented through functions called methods.
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
An object is identified by its attributes.
Question 13
In Object Oriented Programming, objects are defined by defining a class for them.
Question 14
A class is a named software representation for an abstraction.
Question 15
The class is an object maker or object factory.
Question 16
A class basically represents an abstraction of an object.
Question 17
In software terms, an entity's characteristics are represented through data members.
Question 18
There can be multiple classes representing different abstractions of an object.
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
What is an object ? Give examples of some real world objects.
Answer
An object is an entity having a specific identity, specific characteristics and specific behaviour.
Examples:
- 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. - 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. - 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
Define data abstraction.
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
What is message passing among objects ?
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
What are methods ? What do they represent ?
Answer
The behaviour of an object is described through associated functions called methods. In object oriented programming, the state of an object can only be accessed through its associated methods.
Methods represent the behaviour of an object.
Question 7
What is information hiding ?
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
What is a method ? Can there be objects without having any methods ?
Answer
The behaviour of an object is described through associated functions called methods.
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
What is an abstraction ? Can there be multiple abstractions of a real-world entity ?
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
How are classes and abstraction linked ?
Answer
A class is a named software representation for an abstraction. A class actually represents an abstraction, where characteristics are implemented through data and behaviour is implemented through methods.
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
How do you map an abstraction into software ?
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:
- there are variables to hold data, called member variables.
- the values of these member variables define the state of an object.
- there are member functions/operations/methods that define the behaviour of objects.
Question 13
How are real world objects implemented/represented in software terms ?
Answer
The object is implemented in software terms as follows :
- Characteristics/attributes are implemented through member variables or data items of the object.
- Behaviour is implemented through member functions called methods.
- Data and methods are encapsulated into one unit and given a unique name to give it identity.
Thus, a real world object is represented in software terms as a software bundle of variables and related methods.
Question 14
How is data abstraction associated with encapsulation ?
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
'There can be many abstractions of an object'. Comment.
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.
Thus, there can be many abstractions of an object.
Question 16
How does object encapsulate its 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 17
How do objects interact with one another? Give an example.
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
Why are classes needed ?
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
How are classes and objects related to one another?
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
Why are classes said to be abstractions of or a set of objects ?
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
Why are objects said to be instances of a class ?
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
How is following statement different from statement given in previous question ?Student stud1 = new Student( );
Answer
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[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Number: ");
int num = in.nextInt();
int digit, sum = 0;
int orgNum = num;
int prod = 1;
while (num > 0) {
digit = num % 10;
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");
}
}