Você está na página 1de 12

1. What is object-oriented programming?

How is it
different from the procedure-oriented programming?

Ans.

Object- oriented programming is a method of


implementation in which programs are organized as
co-operative collection of objects, each of which
represents an instance of some class and whose
classes all members of a hierarchy of classes united
in inheritance relationships.

With procedural programming you are able to


combine returning sequences of statements into one
single place. A procedure call is used to invoke the
procedure. After the sequence is processed, flow of
control proceeds right after the position where the
call was made

3. Distinguish between the following terms:


1Objects and classes
2Data abstraction and data encapsulation
3Inheritance and polymorphism
4Dynamic binding and message passing
Ans.
1Objects and classes: Object is a physical entity
which represents a person, vehicle or a conceptual
entity (thing in existence) like bank account,
company etc.
A set of variables and functions used to describe
an object is a "class".
A class defines the structure and behavior (data
and code) that will be shared by a set of objects.
Each object of a given class contains the structure
and behavior defined by the class, as if it were
stamped out of a mould in the shape of a class. A
class is a logical construct. An object has physical
reality. When you create a class, you will specify
the code and data that will constitute that class.
Collectively, these elements are called the
members of the class. Specifically, the data
defined by the class are referred to as member
variables or instance variables. The code that
operates on that data is referred to as member
methods or just methods, which define the use of
the member variables.
1Data abstraction and data encapsulation:
Abstraction - the act or process of leaving out of
consideration one or more qualities of a complex
object so as to attend to others. Solving a problem
with objects requires you to build the objects
tailored to your solution. We choose to ignore its
inessential details, dealing instead with the
generalized and idealized model of the object.
51)Encapsulation - The ability to provide users with a
well-defined interface to a set of functions in a
way, which hides their internal workings. In object-
oriented programming, the technique of keeping
together data structures and the methods
(procedures) which act on them. The easiest way
to think of encapsulation is to reference phones.
There are many different types of phones, which
consumers can purchase today. All of the phones
used today will communicate with each other
through a standard interface. For example, a
phone made by GE can be used to call a phone
made by Panasonic. Although their internal
implementation may be different, their public
interface is the same. This is the idea of
encapsulation.
1Inheritance and polymorphism: Inheritance in
object-oriented programming means that a class
of objects can inherit properties from another class
of objects. When inheritance occurs, one class is
then referred as the 'parent class' or 'superclass'
or 'base class'. In turn, these serve as a pattern for
a 'derived class' or 'subclass'.
Inheritance is an important concept since it allows
reuse of class definition without requiring major
code changes. Inheritance can mean just reusing
code, or can mean that you have used a whole
class of object with all its variables and functions.
Polymorphism: It is a key concept in object-
oriented programming. Poly means many, morph
means change (or 'form').
Polymorphism is simply a name given to an action
that is performed by similar objects. Polymorphism
allows a common data-gathering message to be
sent to each class and allows each subclass object
to respond to a message format in an appropriate
manner to its own properties. Polymorphism
encourages something we call 'extendibility'. In
other words, an object or a class can have its uses
extended.
1Dynamic binding and message passing:
Dynamic binding in java is the mechanism by
which compiler cannot determine which method
implementation to use in advance. Based on the
class of the object, the runtime system selects the
appropriate method at runtime. Dynamic binding
is also needed when the compiler determines that
there is more than one possible method that can
be executed by a particular call.
Java's program units, classes, are loaded
dynamically (when needed) by the Java run-time
system. Loaded classes are then dynamically
linked with existing classes to form an integrated
unit. The lengthy link-and-load step required by
third-generation programming languages is
eliminated.
Message Passing: In an object based world the
only way for anything to happen is by objects
communicating with each other and acting on the
results. This communication is called message
passing and involves one object sending a
message to another and (possibly) receiving a
result.

4. Describe inheritance as applied to OOP.

Ans.

Inheritance in object oriented programming means


that a class of objects can inherit properties from
another class of objects. When inheritance occurs,
one class is then referred to as the 'parent class' or
'superclass' or 'base class'. In turn, these serve as a
pattern for a 'derived class' or 'subclass'.

Inheritance is an important concept since it allows


reuse of class definition without requiring major code
changes. Inheritance can mean just reusing code, or
can mean that you have used a whole class of object
with all its variables and functions.

5. List the eight basic data types used in Java. Give


examples.

Ans.

The eight basic data types used in java are:

byte: It is the smallest integer type. This is a signed


8-bit type and has a range from -128 to 127. For
example, the following declaration declares two
variables B and C of type byte.

byte b,c;

b =2;

c = -114;

short: It is a signed 16-bit type and has a range from


-32,768 to 32,767. For example, the following
declaration declares variable K of type short.
short k;

k = 2;

int: It is a signed 32-bit type and has a range from


-2,147,483,648 to 2,147,483,647.

For example,

int x = 10;

int j = 98;

long: This is signed 64-bit type and has a range from


-263 to 263 -1.

For example,

long ds = 1000;

long se;

se =ds * 24 * 60 * 60;

double: It uses 64 bits to store a value.

For example,

double P, R;

P = 10.8;

R =3.14215;

float: It uses 32 bits to store a value.

For example,

float x;

x = -1111;

int : It uses 32 bits to store a value.

For example,
Int score;

Score=90;

char: this data type is used to store characters. It is


16-bit type and has a range from 0 to 65,536.

For example,

char c1,c2;

c1 =84;

c2 ='g';

boolean: it can have only one of two possible


values, true or false.

For example,

boolean flag;

flag= false;

6. What is type casting? Why is it required in


programming?

Ans.

The process of converting one data type to another is


called casting.

Type variable1 = (type) variable2;

Examples:

int m = 50;

byte n = (byte)m;

long distance = (long)m;

Type casting is required in programming when you


want to assign the value of one variable to another
variable of different data type.

7. In what ways does a switch statement differ from


an if statement?

Ans.

An if statement can be used to make decisions based


on range of values or conditions, whereas a switch
statement can make decisions based only on a single
integer value. Also, the value provided to each case
statement must be unique.

1. Compare in terms of their functions, the following


pairs of statements:

(a) while and do........while.

(b) while and for.

(c) break and continue.

Ans.

(a) The difference between the do-while statement


and the while statement is that in the while
statement, the loop body is executed only when the
condition stated in the statement is true. In the do-
while loop, the loop body is executed at least once,
regardless of the condition evaluating to true or
false. The while loop is also called the top tested loop
whereas the do-while loop is also called the bottom
tested loop.

(b) In the for loop, three sections, initialization, test


condition and increment/decrement are placed in the
same line whereas in the while loop, all three
sections are placed in three different places in a
program. In the for loop, more than one variable can
be initialized, tested and incremented at a time.

(c) The continue statement stops the current


iteration of a loop and immediately starts the next
iteration of the same loop. When the current iteration
of a loop stops, the statements after the continue
statement in the loop are not executed. The break
statement immediately terminates the loop,
bypassing the conditional expression and any
remaining code in the body of the loop. When a
break statement is encountered inside the loop, the
loop is terminated and program control resumes the
next statement following the loop.

8. How do classes help us to organize our programs?

Ans.

In essence a class definition is very simple. There are


just two kinds of things that you can include in a
class definition:

Variables
Variables are the data types that store data items
that typically differentiate one object of the class
from another. They are also referred to as data
members of a class. Every class you write in Java is
generally made up of two components: attributes
and behavior. Let's consider an object to define a
motorcycle. Attributes are the individual things that
differentiate one object from another and determine
the state, appearance, or other qualities of that
object. The attributes of our motorcycle might
include:
1color: red, green, silver, brown.
2make: Honda, BMW, Bultaco.
3engineOn: true, false.
Attributes are defined by variables, in fact, you can
consider them because each instance of a class can
have different values for its variables, each variable
is called an instance variable.
Methods
These define the operations you can perform for the
class--so they determine what you can do to, or with,
objects of the class. Methods typically operate on the
fields--the variables of the class. A class's behavior
determines what instances of that class do when
asked to by another class or object. Behavior is the
only way that objects can have anything done to
them. Our motorcycle class might well have the
following behavior:

Start the engine

Stop the engine

Speed up

Change gear

To define an object's behavior you create methods.

9. What are objects? How are they created from a


class?

Ans.

An object in java is a block of memory that contains a


space to store all the instance variables. As with real-
world objects, software objects have state and
behavior. In programming terms the state of an
object is determined by its data (variables); the
behavior by its methods. Thus a software object
combines data and methods into one unit.

Creating an object is referred to as instantiating an


object. The creating object to a class is two-step
process:
1.Declare a variable of class type. This variable does
not define an object instead it is a simply a
variable that can refer to an object.
2.Physical copy of the object is created and assigned
to that variable.
10. What is a constructor? What are its special
properties?

Ans.

The central player in object initialization is the


constructor. In Java, constructors are similar to
methods, but they are not methods. Like a method, a
constructor has a set of parameters and a body of
code. Unlike methods, however, constructors have
no return type. Like methods, you can give access
specifiers to constructors, but unlike methods,
constructors with public, protected, or package
access are not inherited by subclasses. (Also, instead
of determining the ability to invoke a method, the
access level of a constructor determines the ability to
instantiate an object.)

The special properties of a constructor are:

Constructor- names are the same as the name of the


class.

Constructor does not specify a return type.

11. What is an array?

Ans.

An array is a sequence of logically related data


items. It is a kind of row made of boxes, with each
box holding a value. The number associated with
each box is the index of the item. Each box can be
accessed by, first box, second box, third box, and so
on, till the nth box. The first box, or the lowest bound
of an array is always zero, which means, the first
item in an array is always at position zero of that
array. Position in an array is called index. So the third
item in an array would be at index 2 (0, 1, 2).

12. Why are arrays easier to use compared to a


bunch of related variables?

Ans.

An array is a sequence of logically related data


items. It is a kind of row made of boxes, with each
box holding a value.

Arrays have following advantages over bunch of


related variables:
1Arrays of any type can be created. They can have
one or more dimensions.
2Any specific element can be indexed in an array by
its index.
3All like type variables in an array can be referred by
a common name.

13. How does String class differ from the


StringBuffer class?

Ans.

String class has a limit while there is no limit in


StringBuffer class. In the StringBuffer class input is
flushed while it is not so in the String class.

String is an array whose all elements are of character


type. String class is used in the same manner that
we might use any other predefined class. For
example we might declare an instance of the String
class as follows:

String name = new String();

14. Distinguish between Multiprocessing and


Multithreading.

Ans.

Multiprocessing: Refers to a computer system's


ability to support more than one process (program)
at the same time. Multiprocessing operating systems
enable several programs to run concurrently
Multiprocessing systems are much more complicated
than single-process systems because the operating
system must allocate resources to competing
processes in a reasonable manner.

Multithreading : The ability of an operating system to


execute different parts of a program, called threads,
simultaneously. The programmer must carefully
design the program in such a way that all the threads
can run at the same time without interfering with
each other.