0% found this document useful (0 votes)
8 views57 pages

Object Oriented Programming Quiz and Concepts

The document provides an introduction to Object Oriented Programming (OOP) concepts through multiple-choice questions, true/false statements, and assignment questions. It covers key topics such as programming languages, paradigms, characteristics, limitations, and principles of both procedural and object-oriented programming. Additionally, it explains important OOP concepts like abstraction, encapsulation, inheritance, and polymorphism with examples.

Uploaded by

Misba Begg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views57 pages

Object Oriented Programming Quiz and Concepts

The document provides an introduction to Object Oriented Programming (OOP) concepts through multiple-choice questions, true/false statements, and assignment questions. It covers key topics such as programming languages, paradigms, characteristics, limitations, and principles of both procedural and object-oriented programming. Additionally, it explains important OOP concepts like abstraction, encapsulation, inheritance, and polymorphism with examples.

Uploaded by

Misba Begg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Object Oriented Programming Concepts

Multiple Choice Questions

Question 1

Procedure Oriented Programming gives importance to ...........

1. Instructions only ✓

2. Instructions and data

3. Data only

4. None of these

Explanation

As Procedure Oriented Programming follows Top-down approach so


the focus is on the steps or instructions to solve a problem.

Question 2

Procedure Oriented Programming mainly uses ...........

1. Top-down approach ✓

2. Top-down and bottom-up approach

3. Bottom-up approach

4. 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 3

Object Oriented Programming mainly uses ...........

1. Top-down approach

2. Top-down and bottom-up approach

3. Bottom-up approach ✓

4. 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 4

An object belonging to a particular class is known as a/an ........... of that


class.

1. Interface

2. Instance ✓

3. Alias
4. Member

Explanation

The terms object and instance are often interchangeable.

Question 5

Objects that share the same attributes and behaviour are grouped
together into a/an ...........

1. Interface

2. Instance

3. Alias

4. 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 6

........... is the technique of binding both data and functions together to


keep them safe from unauthorised access and misuse.

1. Abstraction

2. Inheritance

3. Encapsulation ✓

4. Polymorphism
Explanation

This is the definition of Encapsulation.

Question 7

........... refers to the act of representing essential features without


including the background details.

1. Abstraction ✓

2. Inheritance

3. Encapsulation

4. Polymorphism

Explanation

This is the definition of Abstraction.

Question 8

........... is the feature by means of which one class acquires the


properties of another class.

1. Abstraction

2. Inheritance ✓

3. Encapsulation

4. Polymorphism

Explanation

This is the definition of Inheritance.

Question 9
The ability of a function or object to take on multiple forms is called
...........

1. Abstraction

2. Inheritance

3. Encapsulation

4. Polymorphism ✓

Explanation

This is the definition of Polymorphism.

Question 10

The term OOP stands for

1. Object Oriented Procedure

2. Object Oriented Packet

3. Object Oriented Programming ✓

4. Object Orientation Procedure

Explanation

Short form of Object Oriented Programming is OOP

State whether the given statements are True or False

Question 1

Low-level languages are closer to computer hardware.


True

Question 2
In a procedural language, code and data are held separately.
True

Question 3

In object-oriented programming, code and data are held separately.


False

Question 4

Wrapping up data and related functions in a single unit represents


encapsulation.
True

Question 5

The object is also known as an instance.


True

Question 6

The class that is derived from another class is called a superclass.


False

Question 7

Classes can be derived from other classes.


True

Question 8

Inheritance allows a class to acquire the properties of another class.


True

Question 9
A class is a blueprint for the attributes and behaviours of a group of
objects.
True

Question 10

Objects from the same class do not share the same definition of
attributes and behaviours.
False

Assignment Questions

Question 1

What are programming languages? Describe the various generations of


programming languages.

Answer

A Programming Language is a set of commands and syntax used to


create a computer program. The various generations of programming
languages are:

1. First Generation Programming Language — Machine language is


the first-generation programming language (1GL). It is made up of
binary number 0 (Zero) and 1 (One) so instructions in Machine
language are sequences of zeros and ones. It is a low-level
language.

2. Second Generation Programming Language — Assembly language


is second-generation language (2GL). It uses symbolic operations
called mnemonics instead of binary digits that can have up to
maximum of five letters. Assembly language is also a low-level
language.
3. Third Generation Programming Language — A third-generation
language (3GL) is close to English in vocabulary. These languages
are easier to read and require less time to write programs. Third-
generation programming languages are high-level programming
languages, such as FORTRAN, Java, C, and C++.

4. Fourth Generation Programming Language — A fourth-


generation language (4GL) is closer to a natural language (for
example, English) than a third- generation language. These
languages are non-procedural. It means that the programmer
specifies what is required as opposed to how it is to be done.
Database languages such as Structured Query Language (SQL),
report generators such as Oracle Reports, and Python are
examples of fourth-generation languages.

5. Fifth Generation Programming Language — A fifth-generation


language (5GL) is designed to solve a given problem using
constraints given to the program, rather than using an algorithm
written by a programmer. The fifth-generation languages are
mainly used in Artificial Intelligence. Smalltalk, Prolog, and
Mercury are good examples of the fifth-generation languages.

Question 2

What are programming paradigms? Briefly explain two popular


programming paradigms.

Answer

A programming paradigm is an approach or style of programming that is


used to classify programming languages. Each programming language
uses one or more programming paradigms. Two popular programming
paradigms are:

1. Procedure Oriented Programming — In this programming


paradigm, a complex programming problem is solved by dividing it
into smaller problems using functions (or procedures).

2. Object Oriented Programming — In this programming paradigm,


data and functions are wrapped into a single unit.

Question 3

What are the characteristics of procedural programming?

Answer

The characteristics of procedural programming are:

1. Procedural programming follows a top-down approach.

2. The program is divided into blocks of codes called functions,


where each function performs a specific task.

3. Procedural programs model real-world processes as 'procedures'


operating on 'data'.

4. The data and functions are detached from each other.

5. The data moves freely in a program.

6. It is easy to follow the logic of a program.

7. A function can access other function's data by calling that


function.

Question 4
What are the limitations of procedural programming?

Answer

The limitations of procedural programming are:

1. Procedural programming mainly focuses on procedures or


functions. Less attention is given to the data.

2. The data and functions are separate from each other.

3. 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.

4. Changes in data types need to be carried out manually all over the
program and in the functions using the same data type.

5. Limited and difficult code reusability.

6. 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.

7. The procedural programming approach does not work well for


large and complex systems.

Question 5

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 6

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:

1. Tangible Objects — These are the objects that we can see and
touch. For example, chair, pen, book, door, etc.

2. 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.

3. Roles — Roles played by people, such as a student, a teacher or a


clerk.

4. 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 7

What are the characteristics of object-oriented programming?


Answer

The characteristics of object-oriented programming are:

1. It follows a bottom-up approach.

2. The program resulting from object-oriented programming is a


collection of objects. Each object has its own data and a set of
operations.

3. OOP restricts the free movement of data and the functions that
operate on it.

4. A properly defined class can be reused, giving way to code


reusability.

5. The concept of object-oriented programming models real-world


entities very well.

6. Due to its object-oriented approach, it is extremely useful in


solving complex problems.

Question 8

What are the limitations of object-oriented programming?

Answer

Limitations of object-oriented programming:

1. The size of the programs created using this approach may become
larger than the programs written using procedure-oriented
programming approach.

2. Software developed using this approach requires a substantial


amount of pre-work and planning.
3. OOP code is difficult to understand if you do not have the
corresponding class documentation.

4. In certain scenarios, these programs can consume a large amount


of memory.

Question 9

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 10

Differentiate between the Owner's and the Vet's perspective in Figure


1.24
Answer

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 11
Differentiate between the Car Mechanic's and Car Driver's perspective
in Figure 1.25

Answer

From the viewpoint of the driver, a car is one single object. It can be
operated using gears, indicators, accelerator, brake, etc. The driver
doesn't need to know or understand the internal mechanisms of how
these components actually work. From the viewpoint of the mechanic,
it is important for him to understand how the internal mechanisms like
gears, indicators, accelerator, brake, etc. actually work so that they can
identify and fix issues with the cars. For a mechanic, each car part is a
single object.

Question 12

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 13

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 14

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 15

Write a short note on the principles of Object Oriented Programming.

Answer

There are four principles of Object Oriented Programming:

1. Data Abstraction — It refers to the act of representing essential


features without including the background details.

2. Encapsulation — It is a mechanism that binds the data and code


(functions) together into a single unit.

3. Inheritance — It is a powerful mechanism by which one class


acquires the properties of another class.

4. Polymorphism — It is the ability of a function or object to take on


multiple forms.

Question 16
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 17

What are the differences between Procedural Programming and Object-


oriented Programming?

Answer

Procedural Programming Object-Oriented Programming

Follows a top-down
Follows a bottom-up approach
approach

Instruction Oriented Data Oriented

The abstraction is at The abstraction is at object


procedure (function) level. (class) level.

The sequence of events in a Entire program is divided into


large program is divided objects.
Procedural Programming Object-Oriented Programming

into functions.

Interaction with program is via


Interaction with program is
functions defined in the class
via direct function calls.
only.

Real world is represented by


Real world is represented
objects and the operations that
by 'procedures' operating
can be performed on these
on data.
objects.

Data and functions are Data and functions are


separate. encapsulated into a single unit.

Data security is less as it It is more secure as one of its


contains lesser features to primary features include data
protect the data. hiding.

A function can access any Only the data whose access has
other function's data by been granted can be accessed
calling that function. by another function.

Limited and difficult code Versatile and easy code


reusability. reusability.

Code is difficult to modify, Code is easy to modify, extend


extend and maintain. and maintain.
Procedural Programming Object-Oriented Programming

Some examples of
Some examples of Object
Procedural Programming
Oriented languages are C++,
languages are C, COBOL,
Java, C#.
Pascal.

Introduction to Java

Multiple Choice Questions

Question 1

Java applications ...........

1. Are platform dependent

2. Don't need a platform to run

3. Are platform independent ✓

4. Can't run on Windows

Question 2

JVM stands for ...........

1. Java Visual Monitor

2. Java Video Monitor

3. Java Virtual Monitor

4. Java Virtual Machine ✓


Question 3

JRE stands for ...........

1. Java Runtime Editor

2. Java Runtime Environment ✓

3. Java Runtime Expression

4. Java Runtime Enabler

Question 4

Stand-alone Java applications ........... for execution.

1. Need Java-compatible web browser

2. Need an assembler

3. Don't need Java-compatible web browser ✓

4. Need a linker

Question 5

Java uses ........... for execution.

1. Compiler and interpreter ✓

2. Interpreter only

3. Compiler only

4. None of these

Question 6
A program that translates code written in a high-level language into
machine code is called ...........

1. Assembler

2. Linker

3. Compiler ✓

4. None of these

Question 7

A program that translates an assembly language program into machine


code is called a/an ...........

1. Assembler ✓

2. Linker

3. Compiler

4. None of these

Question 8

Java is ...........

1. Robust

2. Object oriented

3. Secure

4. All of these ✓

Question 9

Java can be used to write ...........


1. Stand-alone applications only

2. Both stand-alone and internet applications ✓

3. Internet applications only

4. None of these

Question 10

Java applications can run on ...........

1. Windows platform

2. Macintosh platform

3. UNIX platform

4. All of these ✓

State whether given statements are True or False

Question 1

Stand-alone Java applications cannot run independently on a computer.


False

Question 2

A console based Java application is designed to be used via a text-only


interface.
True

Question 3

Java Applets are Java applications that run within a web browser.
True
Question 4

The machine language version of the source code generated by the


compilation process is specific to the processor you are compiling on.
True

Question 5

The Bytecode files are generated with the ".java" extension.


False

Question 6

Java uses a combination of compilation and interpretation.


True

Question 7

Just-In-Time (JIT) compiler compiles selected portions of Bytecode into


executable code.
True

Question 8

An interpreter translates the source code instructions into machine


code all at once.
False

Question 9

JVM stands for Java Virtual Monitor.


False

Question 10
Machine language for JVM is called Bytecode.
True

Assignment Questions

Question 1

Write a short note on types of Java programs.

Answer

There are two types of Java programs — Java Stand-Alone Applications


and Java Applets.

Java Stand-Alone Applications


A stand-alone Java application refers to a Java program that can run
independently on a computer. Acrobat Reader is an excellent example
of this type of application. In Java, every stand-alone application begins
its execution with the main() method. Java stand-alone applications can
be classified into two types:
a. Console based applications
b. Graphical User Interface based applications

Java Applets
Java applets are Java applications that run within a web browser. They
are mainly used for internet programming. The applet is capable of
performing many tasks on a web page, such as displaying graphics,
playing sounds, and accepting user input.

Question 2

How is Java platform independent?

Answer
Java applications are platform independent, which means that Java
applications can be run on any platform. After Java source code is
compiled, it is converted into bytecode. The bytecode is then
interpreted by the JVM. JVM can be installed on several different
operating systems such as Windows, UNIX, Macintosh, etc. JVM allows
Java programs to run on almost any computer regardless of its
hardware configuration. Due to this factor, Java is considered as a
platform independent language.

Question 3

Describe the traditional compilation process with a suitable diagram.

Answer

In traditional compilation process, the machine language


code generated by the compiler is specific to the platform for which
the source code is compiled. Every high-level language has a separate
platform specific compiler to generate the machine code that can run
on the specific platform. For example, the executable file generated by
compiling a C++ program on Windows platform will not run on
Macintosh. It needs to be compiled with C++ Macintosh compiler. The
below diagram illustrates this:
Question 4

Describe the Java compilation process with a suitable diagram.

Answer

In Java compilation process, the source code of a Java program is


compiled to an intermediate binary code called the Bytecode. This
Bytecode cannot be directly executed by the machine. It is understood
by a virtual machine known as Java Virtual Machine or JVM. JVM
contains a Java interpreter which converts the Bytecode into machine
code of the target computer. JVM is platform specific i.e. each platform
has its own JVM. But once the proper JVM is installed on the machine, it
can run any Java Bytecode program. The below diagram illustrates this:
Question 5

How is traditional compilation process different from Java compilation


process?

Answer

In traditional compilation process, the machine language code


generated by the compiler is specific to the platform for which the
source code is compiled. The executable file generated for one platform
like Windows will not work on other platforms like Macintosh. In Java
compilation process, the source code of a Java program is compiled to
an intermediate binary code called the Bytecode. This Bytecode is
interpreted and executed by JVM on the target platform. Bytecode is
platform independent so the same Bytecode which is generated for one
platform like Windows will work on other platforms like Macintosh
without any modifications.

Question 6
What are JVM and JIT?

Answer

JVM
JVM stands for the Java Virtual Machine. It is a virtual machine that runs
Java programs and can be installed on several different operating
systems such as Windows, UNIX, Macintosh etc. JVMs allow Java
programs to run on almost any computer. A JVM processes instructions
similar to a physical processor. However, the Java code (with "java"
extension) must first be converted into Bytecode that the JVM
understands.

JIT
JIT is the Just-In-Time compiler included in the JVM that compiles
selected portions of the Bytecode into executable codes to improve the
execution performance.

Question 7

Distinguish between the following:

a. Source code and Object code

Answer

Source Code Object Code

It is a set of instructions written in It is the compiler generated


a high-level programming machine language version of the
language. source code.

It can be read and understood by It is not human readable but is


Source Code Object Code

humans. understood by machines.

b. Compiler and Interpreter

Answer

Compiler Interpreter

It converts the whole source It converts the source program into


program into the object the object program, one line at a
program at once. time.

It displays the errors for the It displays the error one line at a time
whole program together, after and only after fixing that error the
the compilation. control goes to the next line.

c. ".java" file and ".class" file

Answer

".java" file ".class" file

It contains the Java source code of It contains the Bytecode of the


the program. program.

It is generated by Java
It is written by human (programmer)
compiler.

Question 8

What is Java Bytecode?


Answer

The programs written in Java are compiled into a machine language for
a virtual computer called Java Virtual Machine (JVM). The machine
language for this special Java Virtual Machine is called Java Bytecode.
The Bytecode files are generated with the ".class" extension.

Question 9

Describe the slogan, "Write once, run anywhere" in relation to Java.

Answer

Bytecode and JVM makes Java programs platform independent. This is


one of the most important and powerful features of Java. The Bytecode
of a Java program can be run on any platform that has a corresponding
JVM installed on it. No modifications to this Bytecode are required.
Hence, it is said that Java provides "Write once, run anywhere"
capabilities.

Question 10

Describe four features of Java.

Answer

Four features of Java are:

1. Object Oriented — Java is an object-oriented programming


language because it treats everything as an object. The entire
program code and data resides within objects and classes. This
enables us to easily use and extend the object model.

2. Robust — Java is a robust and reliable programming language. It


has strict compile time and runtime checking of data types.
Memory allocation and de-allocation is automatic in Java, so there
is less for the programmer to worry about.

3. Platform Independent — The concept of "Write once, run


anywhere", is one of the most important and powerful features of
Java. Java applications can run on any platform that has a
corresponding JVM installed on it.

4. Simple — Java is simple and easy to learn programming language.


The removal of many unreliable features of C and C++ (its
predecessors) has simplified its understanding and
implementation.

Elementary Concepts of Objects and Classes

Multiple Choice Questions

Question 1

An object has ...........

1. Attributes

2. State

3. Behaviour

4. 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 2

A class is ...........

1. An object factory

2. A blueprint to create objects

3. A specification for objects

4. 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 3

........... represents an entity in the real-world with its identity and


behaviour.

1. A class

2. An object ✓

3. A procedure

4. A method

Explanation

An object is an identifiable entity with a set of attributes, behaviour


and state.

Question 4
........... is a template to create similar objects that share common
characteristics and behaviour.

1. A function

2. A procedure

3. An attribute

4. 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 5

The values of an object's ........... represent the state of the object.

1. Methods

2. Procedures

3. Attributes ✓

4. Classes

Explanation

Attributes of an object define its state.

Question 6

The terms object and ........... are often interchangeable.

1. Instance ✓
2. Behaviour

3. Attribute

4. State

Explanation

The terms object and instance are often interchangeable.

State whether the given statements are True or False

Question 1

A class is a specification about the object.


True

Question 2

Only one instance can be created from a single class.


False

Question 3

A class is a user-defined data type.


True

Question 4

Real world objects encapsulate state and behaviour.


True

Question 5

There can be multiple abstraction of the same entity.


True

Question 6
Objects interact with each other through messages.
True

Assignment Questions

Question 1

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 2

Explain the anatomy of an object.

Answer

Objects are the basic units of an object oriented system. An object is an


identifiable entity that has its own set of attributes, behaviour and
state. Attributes are individual characteristics that differentiate one
object from another. Behaviour is defined by the set of functions or
operations an object can perform. State is defined by the set of values
held by its attributes.

Question 3

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 4

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 5

Identify five possible attributes and three possible behaviours of the


following entities:

a. School

Answer
Attributes Behaviours

Name School Starts

Address School gets over

Board School conducts exams

Principal

Number of Students

Number of Teachers

b. Student

Answer

Attributes Behaviours

Name Study

Address Play

Class Sleep

Section

Roll No.
c. Teacher

Answer

Attributes Behaviours

Name Teach Class

School Name Conduct Tests

Subject Maintain discipline in class

Class

Address

d. Computer

Answer

Attributes Behaviours

Manufacturer Start Computer

Model Shutdown Computer


Attributes Behaviours

Processor Run applications

RAM

Hard Disk

e. Television

Answer

Attributes Behaviours

Manufacturer Turn On

Model Turn OFF

Size Watch Programmes(Movies, News, Cartoons, etc.)

Smart

Resolution

f. Washing Machine

Answer

Attributes Behaviours

Manufacturer Start Wash


Attributes Behaviours

Model Stop Wash

Capacity Rinse

Energy Rating

Noise Level

g. Chair

Answer

Attributes Behaviours

Material Buy

Colour Sell

Weight Sit

Revolving Repair

Price

h. Person

Answer

Attributes Behaviours
Attributes Behaviours

First Name Eat

Last Name Sleep

Age Walk

Gender

Address

Question 6

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 7

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 8

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 Registration Number: KA 01 AA


Stop car
Number 1234

Apply
Colour Colour: Red
break

Attributes Behaviours State

Model Start car Model: Baleno

Registration Registration Number: WB 01 AZ


Stop car
Number 6789

Apply
Colour Colour: White
break
Question 9

What do you understand by the term 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 10

Explain how objects communicate with each other?

Answer

Objects communicate with each other by sending messages. The sender


object requests the receiver object to perform an action.

Values and Data Types

Multiple Choice Questions

Question 1

ASCII stands for ...........

1. American Standard Code for Information Interchange ✓


2. American Simulated Code for Information Interchange

3. American Standard Code for Interchange of Information

4. American Standard Code for Interaction of Information

Question 2

ASCII is ...........

1. 6-bit set of codes

2. 8-bit set of codes

3. 7-bit set of codes ✓

4. 16-bit set of codes

Question 3

Extended ASCII is ...........

1. 6-bit set of codes

2. 8-bit set of codes ✓

3. 7-bit set of codes

4. 16-bit set of codes

Question 4

Which of the following is not a token?

1. Keyword

2. Identifiers

3. Operators
4. Procedure ✓

Question 5

Which of the following is a keyword?

1. character

2. break ✓

3. object

4. attribute

Question 6

Which of the following is not a legal identifier?

1. _age

2. $Amount

3. 9thClass ✓

4. nullValue

Question 7

Which of the following is an invalid integer?

1. 1999

2. 1999999

3. 199 99 ✓

4. 199

Question 8
Which of the following is not a character literal?

1. '\t'

2. "t" ✓

3. 't'

4. All of these

Question 9

Which of the following punctuator is the statement terminator in Java?

1. ; ✓

2. .

3. ,

4. All of these

Question 10

Which of the following is not a primitive data type in Java?

1. boolean

2. short

3. float

4. class ✓

Question 11

Which of the following is the size of a long data type in Java?

1. 32 bits
2. 64 bits ✓

3. 48 bits

4. Long data type is not supported in Java.

Question 12

Which of the following is the size of a boolean data type in Java?

1. 1 bit

2. 16 bits

3. 8 bits ✓

4. Boolean data type doesn't take any space in memory.

State whether the given statements are True or False

Question 1

Java supports the use of the ASCII character set only.


False

Question 2

The smallest unit in a Java program is known as token.


True

Question 3

The Unicode character set uses 8 to 32 bits per character.


True

Question 4
In an escape sequence, a character is preceded by a forward slash (/).
False

Question 5

In Java, an identifier cannot begin with a $ sign.


False

Question 6

The boolean data types are used for storing logical values.
True

Question 7

Java offers five types of tokens.


True

Question 8

The char data type reserves 8 bits in memory.


False

Question 9

If a literal constant contains a decimal point, then it is of the type


double by default.
True

Question 10

The modifier final can be applied to a variable declaration to ensure


that the value stored in the variable cannot be changed after the
variable has been initialised.
True
Assignment Questions

Question 1

What are escape sequences in Java? Give three examples.

Answer

An escape sequence is a set of characters that has a special meaning to


the Java compiler. In the escape sequence, a character is preceded by a
backslash (\). Some examples of escape sequences are \n, \' and \t.

Question 2

What is the result of evaluating the following expression?


(1 + 2 * 2) / 2 + 2

Answer

(1 + 2 * 2) / 2 + 2 ⇒ (1 + 4) / 2 + 2
⇒ (5) / 2 + 2
⇒ 2 + 2
⇒4

Question 3

What is a token in Java? Name the tokens available in Java.

Answer

All characters in a Java program are grouped into symbols called Tokens.
As you know, a computer program consists of a set of instructions called
statements. A statement is composed of various components. Each
individual component of a programming statement is referred to as a
token. Keywords, identifiers, Operators, Separators and literals are
three tokens in Java.
Question 4

Why can't you use a keyword as a variable name?

Answer

Keywords are reserved words that have a special meaning to the Java
compiler. As Java compiler reserves these words for its own use so they
are not available as names for variables or methods.

Question 5

Which of the following are Java keywords?


input, class, public, int, x, y, radius

Answer

class, public, int are Java keywords.

Question 6

What are identifiers in Java? List three identifier formation rules.

Answer

Identifiers are used to name different parts of a program such as


variables, methods, classes, objects, etc. Three identifier formation
rules are:

1. An an identifier can be a sequence of alphabets, digits, underscore


and dollar sign characters only.

2. Identifiers cannot start with a digit.

3. An identifier must not be a Keyword or a Boolean or null literal.

Question 7
Explain the following statement — "In Java, total, Total, ToTaL, and
TOTAL are all different identifiers."

Answer

Java is a case sensitive language. total, Total, ToTaL, and TOTAL have the
same set of letters but they differ in the case of these letters. Therefore,
Java considers each of them as a different identifier.

Question 8

How would you print characters like \, ' and " in Java?

Answer

We can print characters like \, ' and " using escape sequences i.e.
preceding it with a backslash (\) symbol.

Question 9

Distinguish between the following:

i. Token and Identifier

Answer

Token Identifier

Identifiers are fundamental building


Each individual component blocks of the program and are used to
of a programming statement name different components of a
is referred to as a token. program such as variables, methods
and objects.
Token Identifier

Tokens in Java are


categorised into 5 types —
Keywords, Identifiers, Identifier is a type of token in Java.
Literals, Punctuators,
Operators.

ii. Keyword and Identifier

Answer

Keyword Identifier

Keywords have a special Identifiers are used to name different


meaning for Java components of a program such as variables,
compiler. methods and objects.

Keywords are reserved


by the compiler for its An identifier must not be a Keyword.
own use.

iii. Character and String Constant

Answer

Character Constant String Constant

Character Constants are written String Constants are written by


by enclosing a character within a enclosing a set of characters within
pair of single quotes. a pair of double quotes.
Character Constant String Constant

Character Constants are assigned String Constants are assigned to


to variables of type char. variables of type String.

iv. Integer and float Constant

Answer

Integer Constant Float Constant

Integer Constants represent whole Float Constants represent


number values like 2, -16, 18246, fractional numbers like 3.14159, -
24041973, etc. 14.08, 42.0, 675.238, etc.

Integer Constants are assigned to Float Constants are assigned to


variables of data type — byte, variables of data type — float,
short, int, long, char double

Question 10

Distinguish between "A" and 'A'.

Answer

"A" is a string literal of length 1 containing the letter A in uppercase


whereas 'A' is a character literal having value of A in uppercase.

Question 11

Describe primitive data types in Java.

Answer
Primitive data types are fundamental data types that are an integral
part of the Java language and are used to declare a variable.

Question 12

List the size of primitive data types in Java.

Answer

Data Type Size in Bytes

byte 1

short 2

int 4

long 8

float 4

double 8

char 2

boolean 1

Question 13

Which integer and floating point data types take up the same number
of bits in computer's memory?

Answer
Both, int and float take up 32 bits in memory. Similarly, both long and
double take up 64 bits in memory.

Question 14

What is variable initialisation in Java? What are the default values of the
following type of variables:
short, int, long, float, double, and char.

Answer

Variable initialisation means assigning value to a variable for the first


time. Below are the default values of the different data types:

Data Type Default Value

short 0

int 0

long 0L

float 0.0f

double 0.0d

char '\u0000'

You might also like