Upload
bharathi
View
231
Download
0
Embed Size (px)
Citation preview
8/12/2019 Over All 2 Marks
1/47
HINDUSTHAN COLLEGE OF ENGINEERING AND TECHNOLOGY
COIMBATORE-32
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING
Sub Name: OBJECT ORIENTED PROGRAMMING
Sub Coe: !"!2"!!#$
T%O MAR&S ' (UESTION BAN&
UNIT-$
1
8/12/2019 Over All 2 Marks
2/47
INTRODUCTION
$) E*+,a.: Ob/e01 oe.1e +oamm. +aam:
OOP treats data as a critical element in the program development and does not allow it
to flow freely around the system.
An object is considered to be a partitioned area of computer memory that stores data
and functions that can be used to access that data The data of an object can be accessed only by the functions associated with that object.
However functions of one object can access the functions of other object
Object A Object A
Object
2) Fea1ue4 o5 ob/e01 oe.1e +oamm.:
! "mphasis is on data rather than procedure.
! Programs are divided many numbers of objects.! #unctions that operate on the data of an object or tied together in the data structure.
! $ata is hidden and cannot be accessed by the e%ternal functions.
3) Ba40 E,eme.14 o5 oo+4:
! Objects
&
$ata
#unctions
$ata
#unctions
$ata
#unctions
8/12/2019 Over All 2 Marks
3/47
! lasses
! $ata abstraction 'encapsulation
! (nheritance
! Polymorphism
! $ynamic binding
! )essage passing
#) E*+,a. abou1 ob/e014:
! Objects are the runtime entities in an object oriented system.! "%: a person* a place* a ban+ account etc.
! Objects are a physical representation.
! ,hen a program is e%ecuted* the objects interact by sending messages to one another.! "%: ustomer and account are & objects in a program then the customer object may send
a message to account object re-uesting for the ban+ balance.
! "ach object contains data and code to manipulate that data.! An object is an instance of a class
6) E*+,a. abou1 0,a44e4:
! A class is a collection of data members and member functions.
! Once a class has been defined* we can create any number of objects
! elonging to that class.! "%: mango* apple are members of the class fruit.
#ruit mango/
A class is a logical representation
7) E*+,a. abou1 a1a ab41a01o. :
! Abstraction is the process of hiding the comple% details of a system and providing only
essential features.
! "%: ,hen u switch on a television using remote control device* u only see the image
display on the screen
The internal circuits of the television 0Or the digital instructions that enable the
remote control o function at the press of a button are not e%posed to you.
These details are hidden inside the tv or remote control device.
8) E*+,a. abou1 a1a e.0a+4u,a1o.:
! The process of binding or wrapping data and code in to a single entity is called
encapsulation.! This protects a class from any accidental modification from outside.
2
8/12/2019 Over All 2 Marks
4/47
! "ncapsulation is implemented by using classes. A class wraps data and methods in to a
single unit.
! "ncapsulation provides data hiding and data security.
") E*+,a. abou1 .9e1a.0e: Pae.1-C9, Re,a1o.49+;
! The derived class is derived from the base class. 0or inheritance is the process by which of
objects of one class ac-uire the properties of objects of another class. it also supports
hierarchical classification.
8/12/2019 Over All 2 Marks
5/47
"mployee67Object
3alary67)essage
8ame67(nformation
$2) Me14 o5 oo+4:
! 9eusability
! ode sharing
! $ata hiding
! 9educed comple%ity of a problem
! Prototyping
! )essage passing techni-ue
! "%tendibility
$3) Deme14 o5 oo+:
! (t not supports networ+ing facilities.
! Oop has not support multithreaded programming.
$#) C>> Fu.ame.1a,4:
! is an object oriented programming language. (t was developed by bjarne stroutsup at
AT'T bell laboratories in murray hill* new jerasy*;3A in the year 1? s.
! is a superset of .there are a few minor differences that
will prvent a program to run under compiler.
Sam+,e 0>> +oam:
@includeiostream.h7
void main0
B
coutC collegeC /
return0>/ D
$6) E*+,a. abou1 S1eam:
The flow of $ata between program and file is called 3tream.
There are two types of stream classes.
! (stream class
! Ostream class
E
(nput $evice (nput $evice (nput $evice
8/12/2019 Over All 2 Marks
6/47
? I41eam 0,a44:The flow of data between (nput device and program.
? O41eam 0,a44: The flow of data between program and outputdevice.
$7) E*+,a. abou1 C. S1a1eme.1:
in is a object of istream class. (t is used to get a input from input device li+e +eyboard.
S=.1a*:
in77var177var&77var2FF.77varn/
$8) E*+,a. abou1 Cou1 S1a1eme.1:
out is a Object of Ostream class. it is used to display the output list
S=.1a*:
outOutputlist/
$") 4ome o5 19e 9eae 5,e 1=+e4:
ctype.h7
float.h7math.h7
iomanip.h7
$
8/12/2019 Over All 2 Marks
7/47
2!) E*+,a. abou1 U4e4 o5 @o:
void is a uilt6in type of data . The two normal uses of void are
1. To specify the return type of function when it is not returning any value.
&. To indicate an empty argument list be a function.
E*am+,e:
void function1 0void/
2$) E*+,a. abou1 U4e De5.e Da1a 1=+e4:
! allows us to define our own types based on other e%isting data types. (n order to do that we
shall use +eyword typedef)
Ge.ea, Fom:
1=+ee5 e%istingtype newtypename /
where e%istingtype is a fundamental or any other defined type and newtypenameisthe name that the new type we are going to define will receive.
I
8/12/2019 Over All 2 Marks
8/47
E*am+,e:
typedef char /
typedef unsigned int ,O9$/
typedef char J stringt/
typedef char field KE>L/
22) E*+,a. abou1 U.o.4:
! ;nions allow a portion of memory to be accessed as different data types* since all of them are in
fact the same location in memory.
! (ts declaration and use is similar to the one of structures but its functionality is totally different:
u.o. modelname
type1 element1
type& element&
type2 element2
)
)
objectname
23) E*+,a. abou1 E.umea1o.4 e.um;:
! "numerations serve to create data types to contain something different that is not limited to
either numerical or character constants nor to the constants 1ue and 5a,4e.
! (ts form is the following:
e.um modelname
value1
value&
value2
)
)
objectname
! #or e%ample* we could create a new type of variable called 0o,o to store colors with the
following declaration:
! enum colorst Bblac+* blue* green* cyan* red* purple* yellow* whiteD/
! 8otice that we do not include any fundamental data type in the declaration.
=
8/12/2019 Over All 2 Marks
9/47
! To say it another way* we have created a new data type without it being based on any e%isting
one: the type 0o,o1* whose possible values are the colors that we have enclosed within curly
brac+ets BD.2#) E*+,a. abou1 A00e44 S+e05e4:
+@a1e
+ub,0
or
+o1e01e
They ma+e reference to the permission which the following membersac-uire:! +@a1e members of a class are accessible only from other members of their same class or from
their MfriendM classes.! +o1e01e members are accessible from members of their same class and friendclasses* andalso from members of their derivedclasses.! #inally* +ub,0 members are accessible from anywhere the class is visible.
! (f we declare members of a class before including any permission label* the members are
considered +@a1e* since it is the default permission that the members of a class declared with
the 0,a44 +eyword ac-uire.
#or e%ample:
class 9ectangleB
int %* y/
public:
void setvalues 0int*int/
int area 0void/
D rect/
! $eclares class CRe01a.,e and an object called e01 of this class 0type.
! This class contains four members: two variables of type .1 0* and = in the
+@a1e section 0because private is the default permission and two functions in the
+ub,0 section:
4e1@a,ue4; and aea;* of which we have only included the prototype.
26) E*+,a. abou1 O+ea1o4 . C>>:
8/12/2019 Over All 2 Marks
10/47
has a rich set of operators .some of the operators have to be
followed
: : 67 scope resolution operator
: : J 67 pointer to member declaration
NJ pointer to member operator
. J pointer to member operator
delete 67memory release operator
endl 67line feed operator
new 67memory allocation operator
stew 67field width operator
27) E*+,a. abou1 40o+e e4o,u1o. o+ea1o:
The scope of the variable e%tends from the point of its declaration till the end of the bloc+containing the declaration
general form:
: : @aab,e-.ame
28) E*+,a. abou1 Memo= ma.aeme.1 o+ea1o4:
supports malloc and calloc functions to allocate memory dynamically at run time
.it also defines two unary operators
! new! delete
These two operations are to perform the tas+ of allocating and freeing the memory in a
better and easier way.
4eneral form:
pointer6variable new data type
The new operator can be used to create objects of any type .
2") E*+,a. abou1 E*+e44o.:
1>
8/12/2019 Over All 2 Marks
11/47
! An e%pression is combination of constants* variables and operators are arranged as per the rules
of the language.
! An e%pression may consist of one or more operands and ero or more operators to produce a
value.
T=+e4:
! constant e%pression
! integral
! float
! pointer
: : @aab,e-.ame
! relational
! logical
! bitwise
1.constant e%pression: it consists of only one constant value.
&.float e%pression: flalot e%pressions are those which *after call conversations.
2
8/12/2019 Over All 2 Marks
12/47
if0e%pression is true
B
action 1/
D
action &/
action 2/
3$) E*+,a. abou1 Aa=4:
An array is a collection of variables of the same type that are referred to through a
common name. A specific element in an array is accessed by an order.
T=+e4:
? S.,e me.4o. aa=4:
The general for declaring single dimension array is*
Type var6nameKsieL/
? To me.4o.a, aa=4:
! supports multidimensional arrays.
! the simplest form of a multidimensional array is two6dimensional array.
"%: int dK1>LK&>L/
? Mu,1me.4o.a, aa=4:
(t allows of more than two dimensions.
4eneral form:
Type nameKsie1LKsie&LFFKsienL/
32) E*+,a. abou1 Po.1e4:
Pointer data type is one of the strengths of c language.a pointer is a variable which
holds the memory address of another variable.sometimes*only with the pointer a comple% data
type can be declared and accessed easily.
! (t allows pass variables*arrays*functions*strings and structures as function arguments.
! A pointer allows to return structured variable from functions.
1&
8/12/2019 Over All 2 Marks
13/47
! (f provides functions which can modify their calling arguments.
! (t supports dynamic allocation and deallocation of memory segments.
! ,ith the help of a pointer*variables can be swapped without physically moving them.
! A pointer improves the efficiency of certain routines.
33) Po.1e o+ea1o:
A pointer operator can be represented by a combination of J 0asteris+ with a variable.
"%: (nt Jptr/
,here ptr is a pointer variable which holds the address of an integer data type.
3#) E*+,a. abou1 Ae44 +o.1e:
The ' is a unary operator that returns the memory address of its operand.
A unary operator re-uires only one operand."%: ptr'm*
No1e:
! The pointer operator ' is an operator that returns the address of the variable.
! The other operator J is the complement of '. (t is a unary operator that returns the value of the
variable located at the address.
36) U4e4 o5 +o.1e4 . 5u.01o.4:
The use of the pointers in a function definition may be classified into two groups:
! all by value
! all by reference
!all by value:
,hen the control is transferred bac+ from the function to the calling portion of the
program*the altered values are not transferred bac+.this type of passing formal arguments to a
function is technically +nown as call by value.
RJsample programJR
@includeiostream.h7
void main0void
B
12
8/12/2019 Over All 2 Marks
14/47
void funt0int %*int y/
int %*y*/
%1>/
y&>/
666666
666666
funcct0%*y/RRcall by value
666666
666666
%y/
D
void funct0int a*int b
B
int sum/
aaJa/bbJb/
D
?0a,, b= e5ee.0e:
! The content of the variable that are altered within the function bloc+ are returned to the calling
portion of a program in the altered form itself* s the formal and the actual arguments are
referencing the same memory location or address.
! This is technically +nown as call by reference or call by address or call by location.
UNIT II
PROGRAMMING IN C>>
$) E*+,a. abou1 C S1u01ue
15
8/12/2019 Over All 2 Marks
15/47
A structure is a collection of different types of variables.
Once the structure type has been defined we can create variables of that type usingdeclarations that are similar to the built6in type declarations.
#or e%ample consider the following declaration:
struct student
Bchar nameK&>L/
int rollnumber/
float totalmar+s/
D/
The +eyword Sstruct? declares student as a new data type that can hold three fields of
different data types. These fields are +nown as structure members or elements.
The identifier students* which is refered to as structure name or structure tag can be used
to create variables of type student.
2) Lm1a1o.4 o5 C S1u01ue:
1. does not allow the struct data type to be treated li+e built6in types. #or e%ample:
struct comple%
Bfloat %/
float y/
D/struct comple% c1*c&*c2/
The comple% numbers c1*c&*c2 can easily be assigned values using the dot operator but
we cannot add two comple% numbers or subtract one from another.
"%: c2c1c& is illegal in .
&. does not have the data hiding process
3. Daba04 . 41u01ue4:
(n structures we can +eep only data. it is not possible to +eep functions.
$ata hiding is not possible in structures the above drawbac+s can be overcome by the
data.
1E
8/12/2019 Over All 2 Marks
16/47
#) Ho 0a. =ou 4+e05= a 0,a44:
A class is a way to bind the data and its associated functions together. (t allows the
data to be hidden* if necessary* from e%ternal use.
when defining a class* we are creating a new abstract data type that can be treated
li+e any other built6in data type.
6) T=+e4 o5 0,a44 4+e050a1o.:
class declaration
class functions definitions
The class declaration describes the type and scope of its members. the class function
definitions describe how the class functions are implemented.
7) De5.e C,a44:
A class definition contains a +eyword class and a t defined class6name followed by data
and functions named as members of the class. The members are enclosed with l braces.
The general form is
class classname
B
private:variable declaration/
function declaration or definition/
public:
variable declaration/function declaration or definition/
protected:
variable declaration/function declaration or definition/
D/
The class declaration is similar to a struct declaration.
8) %1e abou1 u,e4 1o 0ea1e 0,a44:
The heads private* public and protected are all optional. (f nothing is given the data and
functions are treated as private.
1G
8/12/2019 Over All 2 Marks
17/47
The closing brace should end with semicolon.
(f the function is declared inside the class it should be defined outside the class.
All member functions must be defined either inside or outside the class.
The variables declared inside the class are called data members and the functions are
called as member functions
") Ho 0a. =ou 0ea1e a. ob/e01
Once a class has been declared* we can create variables of that type by using the class
name."%: item %/ RR )emory for % is created
The necessary memory space is allocated to an object at this stage.
>*IE.E/
)ember function name to the defined member function.
The dot operator connects the object and the member function. This is also called class
member access.
$!) %9a1 4 19e 55ee.0e be1ee. +ub,0 19e 5u.01o. a. +@a1e membe 5u.01o.
An object cannot call a private member function. ut an object can call a public member
function.
Public member function can call private member function using dot operator.
$$) Ho 0a. =ou e5.e Membe 5u.01o.
)ember functions can be defined in two places:
Outside the class definition
(nside the class definition
$) De5.e Co.41u01o:
A constructor is a special member function whose tas+ is to initialie the objects of its class. (t is
special because its name is the same as the class name. The constructor is invo+ed whenever an object of
1I
8/12/2019 Over All 2 Marks
18/47
its associated class is created. (t is called constructor because it constructs the values of data members of
the class.
4eneral #orm:
class integerB
int m*n/public:
integer0void/
F..
F..
D/
integer::integer0void
B
m>/
n>/
D
2) C9aa01e4104 o5 Co.41u01o:
They should be declared in the public section
They are invo+ed automatically when the objects are created
They do not have return types* not even void and therefore* and they cannot returns
values
They cannot be inherited* though a derived class can call the base class constructor.
3) E*+,a. Paame1ee 0o.41u01o:
onstructor with arguments are +nown as parameteried constructors
"%:
class integer
B
int m*n/
public:
integer0int %*int y/ RRparaeteried constructor
F..
F..
integer::integer0int %*int y
Bm%/
ny/
D
#) G@e .o1e4 o. 0o.41u01o4 19 e5au,1 aume.14:
(t is possible to define constructors with default arguments.
1=
8/12/2019 Over All 2 Marks
19/47
"%:
comple%0float real* float imag>/
The default value of the argument imag is ero.
E) E*+,a. Co+= Co.41u01o:
A copy constructor is used to declare and initialie an object from another object.integer0integer 'i
"%:
integer (&0(1/ would define the object (& and at the same time initialie it to the values of (1.
7) De5.e De41u01o4:
A destructor* is used to destroy the objects that have been created by a constructor. Qi+e a
constructor* the destructor is a member function whose name is the same as the class name but is preceded
by a tilde.
integer0BD
8) %9a1 ae 19e 1=+e4 o5 .9e1a.0e
The various types of inheritance are*
3ingle inheritance
)ulti6level inheritance
)ultiple inheritance
Hierarchical inheritance
Hybrid inheritance
") G@e 19e 4=.1a* 5o .9e1a.0e:
The synta% of deriving a new class from an already e%isting class is given by*
class derived6class : visibility6mode base6class
B
body of derived class
D
8/12/2019 Over All 2 Marks
20/47
Here A is the base class and is the derived class.
$!) De5.e mu,1-,e@e, .9e1a.0e:
(n multi6level inheritance* a new class is derived from a class already derived from the
base class.
Here* class is derived from class A and class is further derived from the derived class
.
$$) De5.e mu,1+,e .9e1a.0e4:
(n multiple inheritances* a single class is derived from more than one base class.
Here class is derived from two base classes A and .
$2) De5.e Hea090a, .9e1a.0e:
(n hierarchical inheritance* more than one class is derived from a single base class.
Here class and are derived from class A.
&>
A
A
A
8/12/2019 Over All 2 Marks
21/47
$3) De5.e H=b .9e1a.0e:
Hybrid inheritance is defined as a combination of more than one inheritance.
Here* lasses A* and represent hierarchical inheritance. lasses A* ' $ and
classes A* and $ represent multilevel inheritance. lasses * and $ represent multiple
inheritance.
$#) %9a1 4 a @1ua, ba4e 0,a44
Here* class $ inherits both classes and which are derived from the same base class A.
Hence $ has two copies of the properties of class A. This can be avoided by declaring classes
and as virtual base classes.
$6) %9a1 ae 19e 1=+e4 o5 +o,=mo+94m
The two types of polymorphism are*
ompile time polymorphism U The compiler selects the appropriate function for a
particular call at the compile time itself. (t can be achieved by function overloading and
operator overloading.
9un time Polymorphism 6 The compiler selects the appropriate function for a particular
call at the run time only. (t can be achieved using virtual functions.
$7) %9a1 4 a @1ua, 5u.01o.
&1
A
$
A
$
8/12/2019 Over All 2 Marks
22/47
,hen a function is declared as virtual* determines which function to use at run time
based on the type of object pointed to by the base pointer* rather than the type of the pointer.
$8) Ho 0a. a +@a1e membe be mae .9e1ab,e
A private member can be made inheritable by declaring the data members as protected.
,hen declared as protected the data members can be inherited by the friend classes.
UNIT I
BASICS OF JAA
$) %9a1 ae a,, 19e /a@a 5ea1ue4
ompiled and interpreted
Platform independent and portable
Object6oriented
9obust and secure
&&
8/12/2019 Over All 2 Marks
23/47
$istributed
#amiliar* 3imple and 3mall
)ultithreaded and (nteractive
High Performance
$ynamic and "%tensible
2) %9= /a@a 4 obu411. )emory )anagement: ;nused objects are collected and remove from memory automatically.
This process is +nown as automatic garbage collection.
&. "%ception Handling: Vava has e%ception handlers to handle abnormal condition and to avoid
abnormal program termination
3) %9= /a@a 4 4e0ue
The absence of pointers in java ensures that programs cannot gain access to memory
locations without proper authoriation.
Vava 3ystems not only verify all memory access but also ensure that no viruses arecommunicated with an applet.
#) %9= /a@a 4 bo19 a 0om+,e a. a. .1e+e1e ,a.uae
%9a1 4 19e u4e o5 /a@a 0om+,e
Translates source code in to what is +nown as bytecode instructions. ytecodes are not
machine instructions.
%9a1 4 19e u4e o5 /a@a .1e+e1e
Vava (nterpreter generates machine code that can be directly e%ecuted by the machine that is
running the java program.
6) %9= Ja@a 4 D=.am0
Vava is a dynamic language. Vava is capable of dynamically lin+ing in new class libraries*methods and objects. Vava can also determine the type of class through a -uery* ma+ing it
possible to either dynamically lin+ or abort the program* depending on the response.
7) %9= Ja@a 4 +,a15om .e+e.e.1
&2
8/12/2019 Over All 2 Marks
24/47
Vava programs can be easily moved from one computer system to another* anywhere and
anytime. hanges and upgrades in operating systems* processors and system resources will not
force any changes in java programs.
8) %9= /a@a 4 ob/e01 oe.1e
Vava is true object oriented language. Almost everything in java is an object. All
program code and data reside within objects and classes. Vava comes with an e%tensive set of
classes* arranged in pac+ages that we can use in our programs by inheritance.
") Ja@a +oam 41u01ue:
3uggested
Optional
Optional
Optional
Optional
"ssential
8/12/2019 Over All 2 Marks
25/47
Vava adds labeled brea+ and continue statements
$$) Com+ae 19e 5ea1ue4 Ja@a a. C>>
Vava does not support operator overloading
Vava does not have template classes as in
Vava does not use pointers
Vava does not have header files
$2) De5.e API:
The development tools are part of the system +nown as java development +it0V$W and
the classes and methods are part of the Vava 3tandard Qibrary0V3Q also +nown as the
Application Programming (nterface0AP(.
$3) Too,4 19. JD&:
V$W include:
appletviewer6#or viewing Vava applets
javac6java compiler
java6java (nterpreter
javap6java disassemble
javah6for c header files
javadoc6for creating html documentsjdb6java debugger
$#) %1e a 4m+,e /a@a +oam:
class 3ample
B
public static void main03tring argsKL
B
3ystem.out.println0X,elcome to VavaC/
D
D
&E
8/12/2019 Over All 2 Marks
26/47
$6) E*+,a. +ub,0 41a10 @o ma.;:
41a10:The +eyword static* which declares this method as one that belongs to the entire class and
not a part of any objects of the class. The main must always be declared as static since the
interpreter uses this method before any objects are created.
@o:The type modifier void states that the main method does not return any value. 0ut simple
prints some te%t to the screen.
$7) U4e o5 Ma19 5u.01o.:
import java.lang.)ath/
The purpose of this statement is to instruct the interpreter to load the )ath class from the
pac+age lang. )ath class contains the mathematical notations* which are used in program.
$8) %9a1 ae a,, 19e Comme.14 /a@a 9a4
Vava permits both the single line comments and multi line comments available in .
RR 3ingle line comment at the starting of the line
RJF.JR )ultiple line.
$") %1e a /a@a +oam 19 1o 0,a44e4
class 9oomBfloat length/
float breadth/
void get$ata0float a* float b
B
lengtha/
breadthb/
D
D
class 9oomArea
B
public static void main03tring argsKL
B
float area/
9oom room1new 9oom0/
room1.get$ata015*1>/
arearoom1.lengthJroom1.breadth/
&G
8/12/2019 Over All 2 Marks
27/47
3ystem.out.println0XAreaCarea/
D
D
$
8/12/2019 Over All 2 Marks
28/47
Po0e44 o5 Co.@e1. b=1e 0oe .1o ma09.e 0oe:
2#) %1e a Ja@a +oam u4. Comma. L.e Aume.1:
$epending on the input provided at the time of e%ecution. This is achieved in Vava programs by
using what are +nown as command line arguments.
Poam:
lass ommanQine
B
Public static void main03tring argsKLB
(nt count* i>/
3tring string/
ountargs.length/
3ystem.out.println0X8umber of argumentsCcount/
,hile0icount
B
3tringargsKiL/
(i1/
3ystem.out.println0iC:CCjava isCstringC[C/D
D
D
26) %9a1 4 a 1oe. %9a1 ae 19e 1oe.4 u4e . /a@a
3mallest individual units in a program are +nown as to+ens. The various to+ens used in
java are*
Y 9eserved +eywords
Y (dentifiers
Y Qiterals
Y Operators
Y 3eparators
27) Ie.15e4:
&=
)achine codeVava (nterpreterytecode
8/12/2019 Over All 2 Marks
29/47
(dentifiers are programmer6designed to+ens. They are used for naming classes* methods*
variables* objects* labels* pac+ages and interfaces in a program. Vava identifiers follow the
following rules:
1. They can have alphabets* digits and the underscore and dollar sign characters
&. They must not begin with a digit
2. ;ppercase and lowercase letters are distinct5. They can be of any length
28) L1ea,4:
Qiterals in java are a se-uence of characters 0digits* letters and other characters that
represent constant values to be stored in variables.
Vava language specifies five major types of literals. They are
2") I.1ee ,1ea,4:
#loating point literals
haracter literals
3tring literals
oolean literals
2
8/12/2019 Over All 2 Marks
30/47
3$) Ho oe4 /a@a a09e@e a091e01ue .eu1a,1=
The java compiler produces an intermediate code +nown as byte code for a machine that
does not e%ist. This machine is called the java virtual machine and it e%ists only within the
computer memory.
32) %9a1 4 a 0o.41a.1 %9a1 ae 19e 1=+e4 o5 0o.41a.1 u4e . /a@a
A constant is a fi%ed value that does not change during the e%ecution of a program. The
various constants used in java are*
Y (nteger constants
Y 9eal constants
Y 3ingle character constantsY 3tring constant
Y ac+slash character constant
33) L41 ou1 19e 0o.1o.4 a @aab,e 4 4ub/e01e 1o)
The conditions a variable is subjected to are*
Y They must begin with a digit
Y ;ppercase and lowercase are distinct
Y (t should not be a +eyword
Y ,hite space is not allowedY Zariable names can be of any length
3#) %9a1 ae 19e .4 o5 /a@a @aab,e4
The three +inds of java variables are*
Y (nstance variables U They are created when the objects are instantiated and therefore
they are associated with objects.
Y lass variables U They are global to a class and belong to the entire set of the objects
that class creates.
Y Qocal variables U They are declared and used within methods.
36) G@e 19e ba40 5om o5 a 0,a44 e5.1o. . /a@a)
The basic form of a class definition is given by*
2>
8/12/2019 Over All 2 Marks
31/47
class class6name
Ke%tends superclassnameL
B
variable declaration/
methods declaration/
D
37) %9a1 ae 19e ba40 +a14 o5 a me19o e0,aa1o.
The basic parts of a method declaration are*
Y The name of the method
Y The return type of the method
Y Parameter list
Y ody of the method
38) %9a1 4 .9e1a.0e %9a1 ae 14 1=+e4
The mechanism of deriving a new class from an already e%isting class is +nown as
inheritance. (ts types are*
Y 3ingle inheritance
Y )ulti6level inheritance
Y Hierarchical inheritance
Y Hybrid inheritance
3") Ho oe4 a 4ub0,a44 0o.41u01o .@oe a 4u+e 0,a44 0o.41u01o
A subclass constructor invo+es a super class constructor by using the super +eyword. The
super +eyword is subjected to certain conditions*
Y 3uper may be used within a subclass constructor method
Y (t should be the first statement of the sub class method.
Y (t must match the order and type of the instance variables declared in the super class.
3
8/12/2019 Over All 2 Marks
32/47
#!) %9a1 ae 5.a, @aab,e4 me19o4 a. 0,a44e4
(n order to prevent the subclasses from overriding the members of a super class we can
declare them as final using the +eyword Sfinal?.
e.g* final int 3(\" 1>>/
final void show 0
BFF..D
final class A BFFD
#$) %9a1 4 19e @aou4 @4b,1= 0o.1o,4 u4e . /a@a
The various visibility controls used in java are*
Y Public access
Y Private accessY Protected access
Y Private protected access
Y#riendly access
#2) %9a1 ae 19e 41e+4 .@o,@e . 0ea1. a. aa=
reation of an array involves three steps*
Y $eclare the array U two forms*
type array6name KL/type KL array6name/
Y reate memory locations
array6name new type KsieL/
Y Put values into the memory locations
array6name KsubscriptL value/
type array6name KL B list of valuesD/
#3) API:
Vava 3tandard library or AP( includes hundreds of classes and methods grouped into several
functional pac+ages.
1. Qanguage support pac+age
&. ;tilities pac+age
2. (nputROutput pac+age
2&
8/12/2019 Over All 2 Marks
33/47
5. 8etwor+ing pac+age
E. A,T pac+age
G. Applet Pac+age
##) T=+e Ca41.
The process of converting one data type to another is called casting.
Type variable10type variable&/
"g:
(nt mE>/
(nt n0bytem/
Qong count0long m/
#6) S=mbo,0 0o.41a.14:
A constant is declared as follows 0must be written in AP(TAQ3.
"g:
#inal int 3T9"84TH1>>/
#inal int PA336)A9WE>/
#inal float P(2.151E
8/12/2019 Over All 2 Marks
34/47
Person1.salary066666reference to the method salary0.
#") F.a, 0,a44:
A class that cannot be subclassed is called a final class.
#inal class AclassBF..D
#inal class class e%tends someclassBFFFFD
Any attempt to inherit these classes will cause an error and the compiler will not allow it.
$eclaring a class final prevents any unwanted e%tensions to the class. (t also allows the
compiler to perform some optimiations when a method of a final class is invo+ed.
#
8/12/2019 Over All 2 Marks
35/47
3ynta%: class6var new classname0 /
62) De5.e +aame1ee Co.41u01o4
To construct an object of various dimensions we can add parameters to the constructor.
"g: o% mybo%1 new o%01>*&>*1E/
o% mybo%1 new o%0I*1E/
63) De5.e Me19o o@e,oa.
(n Vava it is possible to define two or methods with the same class that share the same
name* as long as the parameter declarations are different. ,hen this is the case the methods are
said to be overloaded 'the process is referred to as method overloading.
void test0int a
B
3ystem.out.println0Xa: X a/
D
void test0double a
B
3ystem.out.println0Xa: X a/
Dvoid test0int a*int b
B
3ystem.out.println0Xa and b: X a C X b/
D
6#) Me.1o. 4ome o5 19e e4101o.4 9,e u4. 41a10 e=o
They an call other static methods.
They must only access the static data.
They cannot refer to this or super any way.
66) %9a1 4 19e u4e o5 5.a, e=o
(t is used to prevent its contents from being modified. (t is similar to const in R.,e
must initialie a final variable when it is declared.
2E
8/12/2019 Over All 2 Marks
36/47
"g: final int #(Q"8",1/
67) Ho 1o 0a,, a Su+e 0,a44 0o.41u01o4
A subclass can call a constructor method defined by its super class by use of the
following form of super.
3uper0parameter6list/
Parameter list any parameters needed by the constructor in the superclass.super0 must always be
the first statement e%ecuted inside a subclass?s constructor.
68) %9a1 ae 19e u4e4 o5 4u+e e=o
1.;sing super we can call 3uper class constructor.&.it acts li+e this* e%cept that it always refers to the super class of the subclass in
which it is used.
6") De5.e Me19o o@e.
,hen a method in a subclass has the same name ' type signature as a method in thesubclass is said to override the method in the superclass.when an overridden method is called
from within a subclass it will always refer to the version of that method defined by the subclass.
class A
B
FF.
Zoid show0
B
3ystem.out.println0X3uper classC
D
class e%tends A
B
F.
Zoid show0
B
3ystem.out.println0Xsub classC
D
2G
8/12/2019 Over All 2 Marks
37/47
class override
B
sub new 0 /
3ub. show0 /
D
6
8/12/2019 Over All 2 Marks
38/47
case value1:
RRstatement se-uence
brea+/
case value&:
RRstatement se-uence
brea+/
.
.
.
case value8:
RRstatement se-uence
brea+/
default:
RRdefault statement se-uence
D
73) %9a1 4 19e 55ee.0e be1ee. bea K 0o.1.ue 41a1eme.14
rea+: ,e can force immediate termination of a loop* bypassing the conditional* the loop
e%pression ' any remaining code in the body of the loop. ,hen a brea+ statement is encountered
in a loop* the loop is terminated ' the program control resumes at the ne%t statement following
the loop.
ontinue: useful to force early termination. it continue running the loop* but stop
processing the remainder of the code in it?s body for this particular iteration
7#) %9a1 ae 19e u4e4 o5 bea 41a1eme.14
1.(t terminates a statement se-uence in switch statement.
&.(t can be used to e%it a loop.
2=
8/12/2019 Over All 2 Marks
39/47
2.it can be used as a civilied form of goto.
76) %9a1 4 19e u4e o5 T94 e=o
This is always a reference to the object on which the method was invo+ed. this can be
used inside any method to refer to the current object.
o%0double w*double h*double dB
This. width w/
This. height h/
This. depth d/
D
77) De5.e Gabae 0o,,e01o.
The techni-ue used to handle the deal location automatically. ,hen no references to anobject e%ists* that object is assumed to be no longer needed*' the memory occupied by the object
can be reclaimed. 4arbage collection occurs sporadically during the e%ecution of your program.
78) %9a1 4 19e u4e o5 5.a,e; me19o
(f an object is holding some non6java resource such as a file handle* or a window
character font which might be freed before an object is destroyed. To handle such situation Vavaprovides a mechanism called finaliation. y using finaliation we can define specific action that
will occur when an objects just about to reclaimed by the 4arbage collector.
3ynta%:
protected void finalie0
B
RRfinaliation code
D
7") De5.e Re0u4o.
9ecursion is the process of defining something in terms of itself.9ecrsion is the attribute that
allows a method to call itself* A method that call itself is said to be recursive.
int fact0int n
2
8/12/2019 Over All 2 Marks
40/47
B
int result/
if 0n 1 return 1/
result fact0n61 J n/
return result/
D
UNIT
PROGRAMMING USING ARRAYS AND STRING
INTERFACES AND PAC&AGE
$) S1e+4 .@o,@e . 0ea1. a. aa=:
reation of an array involved three steps:
$) $eclare the array
2) reate memory locations
3) Put values into the memory locations
2) E*+,a.-De0,aa1o. o5 aa=4:
Arrays in Vava may be declared in two forms:
Fom$: type arraynameKL/
Fom2: typeKL arrayname/
E*:int numberKL/
float averageKL/
intKL counter/
5>
8/12/2019 Over All 2 Marks
41/47
floatKL mar+s/
3) E*+,a.-Cea1o. o5 memo= ,o0a1o.:
After declaring an array* we need to create in the memory. Vava allows us to create arrays
using new operator only.3ynta%: arraynamenew typeKsieL/
"%: numbernew intKEL/Averagenew floatK1>L/
#) %9a1 4 mea.1 b= .1a,a1o. o5 Aa=4) Ho 0a. =ou a09e@e 19a1
#inal step is to put values in to the array created. This process is +nown as initialiation.
This is done using the array subscripts as shown below:
ArraynameKsubscriptLvalue/
E*:
numberK>L2E/numberK1L5>/
FF.
FFnumberK5L1Lnew intK&L/%K1Lnew intK5L/
%K&Lnew intK2L/
8) %9a1 ae a,, 19e S1. Me19o4:
51
8/12/2019 Over All 2 Marks
42/47
s&s1.toQowerase/ 666 onverts the string s1 to all lowercase
s&s1.to;pperase/ 666 onverts the string s1 to all uppercases&s1.replace0S%?*?y?/ 666 9eplace all appearance of % with y
s&s1.trim0/ 666 9emove white spaces at the beginning and end of the string s1.
") G@e .ame a. u4e4 o5 S1. bu55e Me19o4:
s1.setharAt0n*?%? 666 )odifies the nth character to %s1.append0s& 666 Appends the string s& to s1 at the end
s1.insert0n*s& 666inserts the string s& at the position n of the string s1
s1.setQength0n 666sets the length of the string s1 to n.
(f ns1.length0 s1 is truncated.(f n7s1.lengh0 eros are added to s1.
8/12/2019 Over All 2 Marks
43/47
list.elementAt01>6664ives the name of the 1>thobject
list.sie06664ives the number of objects present
list.remove"lement0item6669emoves the specified item from the list
$3) E*+,a.- %a++e C,a44e4:
Zectors cannot handle primitive data types li+e int* float* long* char and double.
Primitive data types may be converted into object types by using the wrapper classes contained
in the java.lang pac+age.
%a++e 0,a44e4 5o 0o.@e1. 4m+,e 1=+e4:
Sm+,e T=+e %a++e C,a44
oolean oolean
har haracter
$ouble $ouble
#loat #loat
$#) %9a1 4 a. .1e5a0e
An interface is basically a +ind of class. (t contains abstract methods and final variabledeclarations. (t does not specify any code to implement these methods and data fields contain
only constants. (t is used to implement multiple inheritance.
$6) Ho o =ou m+,eme.1 a. .1e5a0e
An interface can be implemented as follows*
class class6name implements interface6name
B
variable declaration/methods declaration/
D
$7) Ho 0a. =ou e*1e. a. .1e5a0e
An interface can be e%tended as follows:
(nterface name& e%tends name1
B
ody of name&
52
8/12/2019 Over All 2 Marks
44/47
D
"%: interface (temconstantsB
int code1>1/
3tring nameC#anC/D
interface (tem e%tends (tem constants
Bvoid display0/
D
$8) G@e e*am+,e 1o m+,eme.1 a. .1e5a0e:
(nterface are implemented as follows:
class classname implements interfacenameB
ody of classnameD
Here the class classname XimplementsC the interface interfacename.
E:
class classname e%tends superclass implements interface1*interface&*B
ody of classname
D
$") %9a1 4 mea.1 b= Pa0ae4
Pac+ages are java?s way of grouping a variety of classes andRor interfaces together. Thegrouping is usually done according to functionality. Pac+ages act as containers for classes.
S=.1a*:
pac+age p+g/
P+g . name of the pac+age.
,e can create a hierarchy of classes. #or that separate each pac+age name from
the one
above it by use of a period.
55
8/12/2019 Over All 2 Marks
45/47
S=.1a*:
pac+age p+g1Kp+g&K.p+g2LL/
$
8/12/2019 Over All 2 Marks
46/47
Y 3tore the listing
Y ompile the file
2#) Me.1o. 4ome o5 19e Se+aa1o4 u4e . Ja@a Poamm.
0 6 ontain a list of parameters in method definition ' invocation.B D 6 ontain the value of automatically initialied arrays.
KL 6 $eclare array types.
/ 6 Terminate statements.. 6 3eparate pac+age name from sub pac+ages.
26) %9a1 4 boo,ea. a1a 1=+e
Vava has simple type called boolean for logical values. (t can have only one of two
possible values* true or false. This is the type returned by all relational operators li+e ab.
boolean is also re-uired by the conditional e%pression that governs the control statements
such as if for.
S=.1a*:boolean variablename/
27) Ho =.am0 .1a,a1o. o5 @aab,e4 4 a09e@e . /a@a
Vava allows variables to be initialied dynamically* using any e%pression valid at the timethe variable is declared.
double a 2.>*b5.>
double c)ath.s-rt0 a J a b J b/
here XcC is initialied dynamically to the length of hypotenuse.
28) Ho 1o 0ea1e a o.e me.4o.a, aa=4
A one dimensional array is a list of li+ed type variables. To create an array *first create an
array variable of desired data type.
S=.1a*:type var6nameKL/
2
8/12/2019 Over All 2 Marks
47/47
p+g1 name of the Top level pac+age.
p+g& name of the subordinate pac+age inside the outer pac+age separated by a dot.
3!) %1e o. 19e 4=.1a* 5o e5.. I.1e5a0e
An interface is defined similar to a classS=.1a*:
interface interfacenameB
variable declaration/
methods declaration/
D
3$) %9a1 ae 19e 41e+4 1o be 5o,,oe 9,e m+,eme.1. a. .1e5a0e
To implement an interface* include the implements clause in a class definition* and thencreate the methods defined by the interface.
S=.1a*:
class classnameKe%tends super classL implements interfaceK*interfaceFLL
BRRclass body D