Вы находитесь на странице: 1из 17

CS2305- PROGRAMMING PARADIGMS 2-MARK Questions and Answers UNI I

2) What is a lass? lass is a template for a set of objects that share a common str!ct!re and a common behavio!r.

do w

nl o

*) What is meant by abstraction? &bstraction defines the essential characteristics of an object that disting!ish it from all other +inds of objects. &bstraction provides crisply, defined concept!al bo!ndaries relative to the perspective of the viewer. #ts the process of foc!ssing on the essential characteristics of an object. &bstraction is one of the f!ndamental elements of the object model.

-) What is meant by Encaps!lation? Encaps!lation is the process of compartmentalising the elements of an abtraction that defines the str!ct!re and behavio!r. Encaps!lation helps to separate the contract!al interface of an abstraction and implementation. .) What are Encaps!lation$ #nheritance and Polymorphism? Encaps!lation is the mechanism that binds together code and data it manip!lates and +eeps both safe from o!tside interference and mis!se. #nheritance is the process by which one object ac/!ires the

ad ed

() What are the core OOP)s concepts? &bstraction$ Encaps!lation$#nheritance and Polymorphism are the core OOP)s concepts.

fro m

%) What is an #nstance? &n instance has state$ behavio!r and identity. 'he str!ct!re and behavio!r of similar classes are defined in their common class. &n instance is also called as an object.

re j

") What is an Object? Object is an instance of a class. #t has state$behavio!r and identity. #t is also called as an instance of a class.

in

pa ul .c

1) What is meant by Object Oriented Programming? OOP is a method of programming in which programs are organised as cooperative collections of objects. Each object is an instance of a class and each class belong to a hierarchy.

om

O!"#C -ORI#N #D PROGRAMMING $ %UNDAM#N A&S

properties of another object. Polymorphism is the feat!re that allows one interface to be !sed for general class actions. 0) What are methods and how are they defined? 1ethods are f!nctions that operate on instances of classes in which they are defined. Objects can comm!nicate with each other !sing methods and can call methods in other classes. 1ethod definition has fo!r parts. 'hey are name of the method$ type of object or primitive type the method ret!rns$ a list of parameters and the body of the method. & method)s signat!re is a combination of the first three parts mentioned above. 12) What are different types of access modifiers 3&ccess specifiers)? &ccess specifiers are +eywords that determine the type of access to the member of a class. 'hese +eywords are for allowing privileges to parts of a program s!ch as f!nctions and variables. 'hese are4 public' &ny thing declared as p!blic can be accessed from anywhere. private' &ny thing declared as private can)t be seen o!tside of its class. protected' &ny thing declared as protected can be accessed by classes in the same pac+age and s!bclasses in the other pac+ages. default modifier ' an be accessed only to classes in the same pac+age. 11) What is an Object and how do yo! allocate memory to it? Object is an instance of a class and it is a software !nit that combines a str!ct!red set of data with a set of operations for inspecting and manip!lating that data. When an object is created !sing new operator$ memory is allocated to it. 12) E5plain the !sage of 6ava pac+ages. 'his is a way to organi7e files when a project consists of m!ltiple mod!les. #t also helps resolve naming conflicts when different pac+ages have classes with the same names. Pac+ages access level also allows yo! to protect data from being !sed by the non,a!thori7ed classes.

do w

nl o

1") What is method overloading and method overriding? 1ethod overloading4 When a method in a class having the same method name with different arg!ments is said to be method overloading. 1ethod overriding 4 When a method in a class having the same method name with same arg!ments is said to be method overriding. 14) What gives java it)s 8write once and r!n anywhere9 nat!re? &ll 6ava programs are compiled into class files that contain bytecodes. 'hese byte codes can be r!n in any platform and hence java is said to be platform independent.

ad ed

fro m

re j

in

pa ul .c

om

1() What is a constr!ctor? What is a destr!ctor? onstr!ctor is an operation that creates an object and:or initialises its state. ;estr!ctor is an operation that frees the state of an object and:or destroys the object itself. #n 6ava$ there is no concept of destr!ctors. #ts ta+en care by the 6<1.

1-) What is Stati( )e)*er (+asses, & static member class is a static member of a class. =i+e any other static method$ a static member class has access to all static methods of the parent$ or top,level$ class. 1.) What is >arbage

21) What is finali7e3) method? finali7e 3) method is !sed j!st before an object is destroyed and can be called j!st prior to garbage collection.

do w

nl o

22) What is the difference between ?tring and ?tring A!ffer? a) ?tring objects are constants and imm!table whereas ?tringA!ffer objects are not. b) ?tring class s!pports constant strings whereas ?tringA!ffer class s!pports growable and modifiable strings. 2") What is the difference between &rray and vector? &rray is a set of related data type and static whereas vector is a growable array of objects and dynamic

ad ed

22) What is static variable and static method? static variable is a class variable which val!e remains constant for the entire class static method is the one which can be called with the class itself and can hold only the staic variables

fro m

10) #n 6ava$ @ow to ma+e an object completely encaps!lated? &ll the instance variables sho!ld be declared as private and p!blic getter and setter methods sho!ld be provided for accessing the instance variables.

re j

ollection and how to call it e5plicitly? When an object is no longer referred to by any variable$ java a!tomatically reclaims memory !sed by that object. 'his is +nown as garbage collection. ?ystem. gc3) method may be !sed to call it e5plicitly

in

pa ul .c

om

1*) What is the difference between constr!ctor and method? onstr!ctor will be a!tomatically invo+ed when an object is created whereas method has to be called e5plicitly

2%) What is a pac+age? & pac+age is a collection of classes and interfaces that provides a high,level layer of access protection and name space management. 2() What is the difference between this3) and s!per3)? this3) can be !sed to invo+e a constr!ctor of the same class whereas s!per3) can be !sed to invo+e a s!per class constr!ctor.

UNI II

O!"#C -ORI#N #D PROGRAMMING $ IN-#RI ANC#

") What is the difference between s!perclass and s!bclass? & s!per class is a class that is inherited whereas s!b class is a class that does the inheriting.

nl o

%) ;ifferentiate between a

'he Object class is the highest,level class in the 6ava class hierarchy. 'he lass class is !sed to represent the classes and interfaces that are loaded by a 6ava program. 'he lass class is !sed to obtain information abo!t an objectBs design. & lass is only a definition or prototype of real life object. Whereas an object is an instance or living representation of real life object. Every object belongs to a class and every class contains one or more related objects. () What is meant by Ainding? Ainding denotes association of a name with a class

do w

ad ed

2) What is meant by #nheritance and what are its advantages? #nheritance is the process of inheriting all the feat!res from a class. 'he advantages of inheritance are re!sability of code and accessibility of variables and methods of the s!per class by s!bclasses.

fro m

1) What is meant by #nheritance? #nheritance is a relationship among classes$ wherein one class shares the str!ct!re or behavio!r defined in another class. 'his is called ?ingle #nheritance. #f a class shares the str!ct!re or behavio!r from m!ltiple classes$ then it is called 1!ltiple #nheritance. #nheritance defines 8is,a9 hierarchy among classes in which one s!bclass inherits from one or more generalised s!perclasses.

lass and an Object?

re j

in

pa ul .c

2*) E5plain wor+ing of 6ava <irt!al 1achine 36<1)? 6<1 is an abstract comp!ting machine li+e any other real comp!ting machine which first converts .java file into .class file by !sing ompiler 3.class is nothing b!t byte code file.) and #nterpreter reads byte codes.

om

*) What is meant by Polymorphism? Polymorphism literally means ta+ing more than one form. Polymorphism is a characteristic of being able to assign a different behavior or val!e in a s!bclass$ to something that was declared in a parent class. -) What is ;ynamic Ainding? Ainding refers to the lin+ing of a proced!re call to the code to be e5ec!ted in response to the call. ;ynamic binding 3also +nown as late binding) means that the code associated with a given proced!re call is not +nown !ntil the time of the call at r!n,time. #t is associated with polymorphism and inheritance. .) What is final modifier? 'he final modifier +eyword ma+es that the programmer cannot change the val!e anymore. 'he act!al meaning depends on whether it is applied to a class$ a variable$ or a method.

do w

nl o

11) What is an #nterface? #nterface is an o!tside view of a class or object which emphai7es its abstraction while hiding its str!ct!re and secrets of its behavio!r. 12) What is a base class? Aase class is the most generalised class in a class str!ct!re. 1ost applications have s!ch root classes. #n 6ava$ Object is the base class for all classes. 1") What is reflection in java? Ceflection allows 6ava code to discover information abo!t the fields$ methods and constr!ctors of loaded classes and to dynamically invo+e them.

ad ed

12) What are inner class and anonymo!s class? #nner class4 classes defined in other classes$ incl!ding those defined in methods are called inner classes. &n inner class can have any accessibility incl!ding private. &nonymo!s class4 &nonymo!s class is a class defined inside a method witho!t a name and is instantiated and declared in the same place and cannot have e5plicit constr!ctors

fro m

0) What is an &bstract lass? &bstract class is a class that has no instances. &n abstract class is written with the e5pectation that its concrete s!bclasses will add to its str!ct!re and behavio!r$ typically by implementing its abstract operations.

re j

final lasses, & final class cannot have s!bclasses. final <ariables, & final variable cannot be changed once it is initiali7ed. final 1ethods, & final method cannot be overridden by s!bclasses.

in

pa ul .c

om

1%) ;efine s!perclass and s!bclass? ?!perclass is a class from which another class inherits. ?!bclass is a class that inherits from one or more classes. 1() What is meant by Ainding$ ?tatic binding$ ;ynamic binding? Binding: Ainding denotes association of a name with a class. Static binding: ?tatic binding is a binding in which the class association is made d!ring compile time. 'his is also called as Early binding. Dynamic binding4 ;ynamic binding is a binding in which the class association is not made !ntil the object is created at e5ec!tion time. #t is also called as Late binding. 1*) What is reflection &P#? @ow are they implemented? Ceflection is the process of introspecting the feat!res and state of a class at r!ntime and dynamically manip!late at r!n time. 'his is s!pported !sing Ceflection &P# with b!ilt,in classes li+e lass$ 1ethod$ Dields$ onstr!ctors etc. E5ample4 Esing 6ava Ceflection &P# we can get the class name$ by !sing the getFame method. 1-) What is the difference between a static and a non,static inner class? & non,static inner class may have object instances that are associated with instances of the classBs o!ter class. & static inner class does not have any object instances. 1.) What is the difference between abstract class and interface? a) &ll the methods declared inside an interface are abstract whereas abstract class m!st have at least one abstract method and others may be concrete or abstract. b) #n abstract class$ +ey word abstract m!st be !sed for the methods whereas interface we need not !se that +eyword for the methods. c) &bstract class m!st have s!bclasses whereas interface can)t have s!bclasses. 10) an yo! have an inner class inside a method and what variables can yo! access? Ges$ we can have an inner class inside a method and final variables can be accessed. 22) What is interface and its !se? #nterface is similar to a class which may contain method)s signat!re only b!t not bodies and it is a formal set of method and constant declarations that m!st be defined by the class that implements it. #nterfaces are !sef!l for4 a) ;eclaring methods that one or more classes are e5pected to implement b) apt!ring similarities between !nrelated classes witho!t forcing a class relationship. c) ;etermining an

do w

nl o

ad ed

fro m

re j

in

pa ul .c

om

object)s programming interface witho!t revealing the act!al body of the class. 21) @ow is polymorphism acheived in java? #nheritance$ Overloading and Overriding are !sed to acheive Polymorphism in java. 22) What modifiers may be !sed with top,level class? p!blic$ abstract and final can be !sed for top,level class.

2") What is a cloneable interface and how many methods does it contain? #t is not having any method beca!se it is a '&>>E; or 1&CHEC interface. 2%) What are the methods provided by the object class? 'he Object class provides five methods that are critical when writing m!ltithreaded 6ava programs4

nl o

2*) What is object cloning? #t is the process of d!plicating an object so that two identical objects will e5ist in the memory at the same time.
UNI III #.#N -DRI.#N PROGRAMMING

do w

1) What is the relationship between the anvas class and the >raphics class? & anvas object provides access to a >raphics object via its paint3) method. 2) @ow wo!ld yo! create a b!tton with ro!nded edges? 'here)s 2 ways. 'he first thing is to +now that a 6A!tton)s edges are

ad ed

'he class loader to define the pro5y class &n invocation handler to intercept and handle method calls & list of interfaces that the pro5y instance implements

fro m

2() ;efine4 ;ynamic pro5y. & dynamic pro5y is a class that implements a list of interfaces$ which yo! specify at r!ntime when yo! create the pro5y. 'o create a pro5y$ !se the static method java.lang.reflect.Proxy::newProxy n!tance"). 'his method ta+es three arg!ments4

re j

notify notify&ll wait 3three versions)

in

pa ul .c

om

drawn by a Aorder. so yo! can override the A!tton)s paint omponent3>raphics) method and draw a circle or ro!nded rectangle 3whatever)$ and t!rn off the border. Or yo! can create a c!stom border that draws a circle or ro!nded rectangle aro!nd any component and set the b!tton)s border to it.

() Which containers !se a border =ayo!t as their defa!lt layo!t? 'he window$ Drame and ;ialog classes !se a border layo!t as their defa!lt layo!t.

do w

nl o

.) What is a layo!t manager and what are different types of layo!t managers available in java &W'? & layo!t manager is an object that is !sed to organi7e components in a container. 'he different layo!ts are available are Dlow=ayo!t$ Aorder=ayo!t$ ard=ayo!t$ >rid=ayo!t and >ridAag=ayo!t.

0) @ow are the elements of different layo!ts organi7ed? #lowLayout4 'he elements of a Dlow=ayo!t are organi7ed in a top to bottom$ left to right fashion. BorderLayout: 'he elements of a Aorder=ayo!t are organi7ed at the borders 3Forth$ ?o!th$ East and West) and the center of a container. $ardLayout: 'he elements of a ard=ayo!t are stac+ed$ on top of the other$ li+e a dec+ of cards. %ridLayout: 'he elements of a >rid=ayo!t are of e/!al si7e and are laid o!t !sing the s/!are of a grid. %ridBagLayout: 'he elements of a >ridAag=ayo!t are organi7ed according to a grid. @owever$ the elements

ad ed

-) ;ifference between ?wing and &wt? &W' are heavy,weight componenets. ?wings are light,weight components. @ence swing wor+s faster than &W'.

fro m

*) What is the difference between applications and applets? a)&pplication m!st be r!n on local machine whereas applet needs no e5plicit installation on local machine. b)&pplication m!st be r!n e5plicitly within a java,compatible virt!al machine whereas applet loads and r!ns itself a!tomatically in a java,enabled browser. c)&pplication starts e5ec!tion with its main method whereas applet starts e5ec!tion with its init method. d)&pplication can r!n with or witho!t graphical !ser interface whereas applet m!st r!n within a graphical !ser interface.

re j

in

pa ul .c

%) What is the difference between the paint3) and repaint3) methods? 'he paint3) method s!pports painting via a >raphics object. 'he repaint3) method is !sed to ca!se paint3) to be invo+ed by the &W' painting thread.

om

") What is the difference between the IDont) and IDont1etrics) class? 'he Dont lass is !sed to render Iglyphs) , the characters yo! see on the screen. Dont1etrics encaps!lates information abo!t a specific font on a specific >raphics object. 3width of the characters$ ascent$ descent)

are of different si7e and may occ!py more than one row or col!mn of the grid. #n addition$ the rows and col!mns may have different si7es. 'he default =ayo!t 1anager of Panel and Panel s!b classes is Dlow=ayo!t.

12) What is the difference between scrollbar and scrollpane? & ?crollbar is a omponent$ b!t not a ontainer whereas ?crollpane is a onatiner and handles its own events and perform its own scrolling.

1() What is the difference between a Choice and a List? & hoice is displayed in a compact form that re/!ires yo! to p!ll it down to see the list of available choices. Only one item may be selected from a hoice. & =ist may be displayed in s!ch a way that several =ist items are visible. & =ist s!pports the selection of one or more =ist items. 1*) What is the purpose of the enableEvents() method? The enableEvents() method is used to enable an event for a particular object. Normall !an event is enabled when a listener is added to an object for a particular event. The enableEvents() method is used b objects that handle events b overridin" their eventdispatch methods. 1-) What is the difference between the #ile and $andom%ccess#ile classes?

do w

nl o

ad ed

1%) What is meant by controls and what are different types of controls in &W'? ontrols are components that allow a !ser to interact with yo!r application and the &W' s!pports the following types of controls4 =abels$ P!sh A!ttons$ hec+ Ao5es$ hoice =ists$ =ists$ ?crollbars$ and 'e5t omponents. 'hese controls are s!bclasses of omponent.

fro m

1") Why won)t the 6<1 terminate when # close all the application windows? 'he &W' event dispatcher thread is not a daemon thread. Go! m!st e5plicitly call ?ystem.e5it to terminate the 6<1.

re j

in

11) What is an event and what are the models available for event handling? &n event is an event object that describes a state of change in a so!rce. #n other words$ event occ!rs when an action is generated$ li+e pressing b!tton$ clic+ing mo!se$ selecting a list$ etc. 'here are two types of models for handling events and they are4 a) event,inheritance model and b) event, delegation model

pa ul .c

# want to !pdate a ?wing component b!t #)m not in a callbac+. #f # want the !pdate to happen immediately 3perhaps for a progress bar component) then #)d !se invo+e&ndWait. #f # don)t care when the !pdate occ!rs$ #)d !se invo+e=ater.

om

12) Why wo!ld yo! !se ?wingEtilities.invo+e&ndWait or ?wingEtilities.invo+e=ater?

The #ile class encapsulates the files and directories of the local file s stem. The $andom%ccess#ile class provides the methods needed to directl access data contained in an part of a file. 1.) What is the lifecycle of an applet? init3) method , an be called when an applet is first loaded start3) method , an be called each time an applet is started. paint3) method , an be called when the applet is minimi7ed or ma5imi7ed. stop3) method , an be !sed when the browser moves off the applet)s page. destroy3) method , an be called when the browser is finished with the applet.

10) What is the difference between a 1en!#tem and a hec+bo51en!#tem? 'he hec+bo51en!#tem class e5tends the 1en!#tem class to s!pport a men! item that may be chec+ed or !nchec+ed. 22) What class is the top of the &W' event hierarchy?

do w

nl o

2") @ow wo!ld yo! detect a +eypress in a 6 omboAo5? 'his is a tric+. most people wo!ld say Iadd a Hey=istener to the 6 omboAo5) , b!t the right answer is Iadd a Hey=istener to the 6 omboAo5)s editor component.) 2%) What an #:O filter? &n #:O filter is an object that reads from one stream and writes to another$ !s!ally altering the data in some way as it is passed from one stream to another. 2() @ow can # create my own >E# components? !stom graphical components can be created by prod!cing a class that inherits from java.awt. anvas. Go!r component sho!ld override the paint method$ j!st li+e an applet does$ to provide the graphical feat!res of the component.
UNI I.

ad ed

22) E5plain how to render an @'1= page !sing only ?wing. Ese a 6EditorPane or 6'e5tPane and set it with an @'1=EditorHit$ then load the te5t into the pane.

fro m

21) What is so!rce and listener? !ource : & so!rce is an object that generates an event. 'his occ!rs when the internal state of that object changes in some way. li!tener : & listener is an object that is notified when an event occ!rs. #t has two major re/!irements. Dirst$ it m!st have been registered with one or more so!rces to receive notifications abo!t specific types of events. ?econd$ it m!st implement methods to receive and process these notifications.

re j

'he java.awt.&W'Event class is the highest,level class in the &W' event,class hierarchy.

in

pa ul .c

om

G#N#RIC PROGRAMMING

1) What is an e5ception? &n exception is an event$ which occ!rs d!ring the e5ec!tion of a program$ that disr!pts the normal flow of the programBs instr!ctions. 2) What is error? &n Error indicates that a non,recoverable condition has occ!rred that sho!ld not be ca!ght. Error$ a s!bclass of 'hrowable$ is intended for drastic problems$ s!ch as O!tOf1emoryError$ which wo!ld be reported by the 6<1 itself. ") Which is s!perclass of E5ception? J'hrowableJ$ the parent class of all e5ception related classes.

?eparating Error @andling ode from JCeg!larJ Propagating Errors Ep the all ?tac+. >ro!ping Error 'ypes and Error ;ifferentiation.

() What are the types of E5ceptions in 6ava

'here are two types of e5ceptions in 6ava$ !nchec+ed e5ceptions and chec+ed e5ceptions.

do w

nl o

(nc&ec'ed exception!: &ll E5ceptions that e5tend the C!ntimeE5ception class are !nchec+ed e5ceptions. lass Error and its s!bclasses also are !nchec+ed.

*) Why Errors are Fot hec+ed? & !nchec+ed e5ception classes which are the error classes 3Error and its s!bclasses) are e5empted from compile,time chec+ing beca!se they can occ!r at many points in the program and recovery from them is diffic!lt or impossible. & program declaring s!ch e5ceptions wo!ld be pointlessly. -) &ow does a tr statement determine which catch clause should be used to handle an e'ception?

ad ed

$&ec'ed exception!: & chec+ed e5ception is some s!bclass of E5ception 3or E5ception itself)$ e5cl!ding class C!ntimeE5ception and its s!bclasses. Each method m!st either handle all chec+ed e5ceptions by s!pplying a catch cla!se or list each !nhandled chec+ed e5ception as a thrown e5ception.

fro m

re j

in
ode.

%) What are the advantages of !sing e5ception handling? E5ception handling provides the following advantages over JtraditionalJ error management techni/!es4

pa ul .c

om

When an e'ception is thrown within the bod of a tr statement! the catch clauses of the tr statement are e'amined in the order in which the appear. The first catch clause that is capable of handlin" the e'ception is e'ecuted. The remainin" catch clauses are i"nored.

0) What is the difference between chec+ed and Enchec+ed E5ceptions in 6ava? &ll predefined e5ceptions in 6ava are either a chec+ed e5ception or an !nchec+ed e5ception. hec+ed e5ceptions m!st be ca!ght !sing try.. catch 3) bloc+ or we sho!ld throw the e5ception !sing throws cla!se. #f yo! dont$ compilation of program will fail.

12) When is the finally cla!se of a try,catch,finally statement e5ec!ted? 'he finally cla!se of the try,catch,finally statement is always e5ec!ted !nless the thread of e5ec!tion terminates or an e5ception occ!rs within the e5ec!tion of the finally cla!se. 1") What if there is a brea+ or ret!rn statement in try bloc+ followed by finally bloc+? #f there is a ret!rn statement in the try bloc+$ the finally bloc+ e5ec!tes right after the ret!rn statement enco!ntered$ and before the ret!rn e5ec!tes. 1%) What are the different ways to handle e5ceptions? 'here are two ways to handle e5ceptions4

do w

nl o

Wrapping the desired code in a try bloc+ followed by a catch bloc+ to catch the e5ceptions. =ist the desired e5ceptions in the throws cla!se of the method and let the caller of the method handle those e5ceptions.

ad ed

11) What is the catch or declare r!le for method declarations? #f a chec+ed e5ception may be thrown within the body of a method$ the method m!st either catch the e5ception or declare it in its throws cla!se.

fro m

)*) What is the difference between e5ception and error? 'he e5ception class defines mild error conditions that yo!r program enco!nters. E5ceptions can occ!r when trying to open the file$ which does not e5ist$ the networ+ connection is disr!pted$ operands being manip!lated are o!t of prescribed ranges$ the class file yo! are interested in loading is missing. 'he error class defines serio!s error conditions that yo! sho!ld not attempt to recover from. #n most cases it is advisable to let the program terminate when s!ch an error is enco!ntered.

re j

in

pa ul .c

om

.) What is the purpose of the finall clause of a tr (catch(finall statement? 'he finally cla!se is !sed to provide the capability to e5ec!te code no matter whether or not an e5ception is thrown or ca!ght.

1() @ow to create c!stom e5ceptions? Ay e5tending the E5ception class or one of its s!bclasses.
Example:

class MyException extends Exception { public MyException() { super(); } }

10) @ow does finally bloc+ differ from finali7e3) method? Dinally bloc+ will be e5ec!ted whether or not an e5ception is thrown. ?o it is !sed to free resoo!rces. finali7e3) is a protected method in the Object class which is called by the 6<1 j!st before an object is garbage collected. 22) What is the difference between throw and throws cla!se? throw is !sed to throw an e5ception man!ally$ where as throws is !sed in the case of chec+ed e5ceptions$ to tell the compiler that we havenBt handled the e5ception$ so that the e5ception will be handled by the calling f!nction.

do w

nl o

21) What are the different ways to generate and E5ception? 'here are two different ways to generate an E5ception.

1. E5ceptions can be generated by the 6ava r!n,time system. E5ceptions thrown by 6ava relate to f!ndamental errors that violate the r!les of the 6ava lang!age or the constraints of the 6ava e5ec!tion environment.

2. E5ceptions can be man!ally generated by yo!r code.

ad ed

fro m

1.) What is the !se of assert +eyword? &ssert +eyword validates certain e5pressions. #t replaces the if bloc+ effectively and throws an &ssertionError on fail!re. 'he assert +eyword sho!ld be !sed only for critical arg!ments 3means witho!t that the method does nothing).

re j

in

1*) an we have the try bloc+ witho!t catch bloc+? Ges$ we can have the try bloc+ witho!t catch bloc+$ b!t finally bloc+ sho!ld follow the try bloc+. Note' #t is not valid to !se a try cla!se witho!t either a catch cla!se or a finally cla!se. 1-) What is the difference between swing and applet? ?wing is a light weight component whereas &pplet is a heavy weight omponent. &pplet does not re/!ire main method$ instead it needs init method.

pa ul .c

om

public MyException(String s) { super(s); }

1an!ally generated e5ceptions are typically !sed to report some error condition to the caller of a method. 22) Where does E5ception stand in the 6ava tree hierarchy?

e.g. void swap3)K swap3)L M

ad ed

2() @ow do yo! get the descriptive information abo!t the E5ception occ!rred d!ring the program e5ec!tion? &ll the e5ceptions inherit a method print?tac+'race3) from the 'hrowable class. 'his method prints the stac+ trace from where the e5ception occ!rred. #t prints the most recently entered method first and contin!es down$ printing the name of each method as it wor+s its way down the call stac+ from the top.
UNI .

nl o

do w

1) E5plain different way of !sing thread? 'he thread co!ld be implemented by !sing r!nnable interface or by inheriting from the 'hread class. 'he former is more advantageo!s$ Bca!se when yo! are going for m!ltiple inheritance..the only interface can help. 2) What are the different states of a thread ? 'he different thread states are ready$ r!nning$ waiting and dead.

fro m

2%) E5plain the e5ception hierarchy in java. 'he hierarchy is as follows4 'hrowable is a parent class off all E5ception classes. 'hey are two types of E5ceptions4 hec+ed e5ceptions and Enchec+edE5ceptions. Aoth type of e5ceptions e5tends E5ception class

CONCURR#N PROGRAMMING

re j

in

pa ul .c

2") What is StackOverflowError? 'he ?tac+OverDlowError is an Error Object thorwn by the C!ntime ?ystem when it Enco!nters that yo!r application:code has ran o!t of the memory. #t may occ!r in case of rec!rsive methods or a large amo!nt of data is fetched from the server and stored in some object. 'his error is generated by 6<1.

om

java.lang.O*/e(t java.lang. 0rowa*+e java.lang.#1(e2tion java.lang.#rror

") Why are there separate wait and s+ee2 methods? 'he static 0read3s+ee24+on56 method maintains control of thread e5ec!tion b!t delays the ne5t action !ntil the sleep time e5pires. 'he wait method gives !p control over thread e5ec!tion indefinitely so that other threads can r!n. %) What is m!ltithreading and what are the methods for inter,thread comm!nication and what is the class in which these methods are defined? 1!ltithreading is the mechanism in which more than one thread r!n independent of each other within the process. wait 3)$ notify 3) and notify&ll3) methods can be !sed for inter,thread comm!nication and these methods are in Object class. wait3) 4 When a thread e5ec!tes a call to wait3) method$ it s!rrenders the object loc+ and enters into a waiting state. notify3) or notify&ll3) 4 'o remove a thread from the waiting state$ some other thread m!st ma+e a call to notify3) or notify&ll3) method on the same object.

With respect to m!ltithreading$ synchroni7ation is the capability to control the access of m!ltiple threads to shared reso!rces. Witho!t synchroni7ation$ it is possible for one thread to modify a shared object while another thread is in the process of !sing or !pdating that objectBs val!e. 'his often leads to significant errors. *) @ow does m!ltithreading ta+e place on a comp!ter with a single PE? 'he operating systemBs tas+ sched!ler allocates e5ec!tion time to m!ltiple tas+s. Ay /!ic+ly switching between e5ec!ting tas+s$ it creates the impression that tas+s e5ec!te se/!entially.

do w

nl o

.) What happens when yo! invo+e a threadBs interr!pt method while it is sleeping or waiting? When a tas+Bs interr!pt3) method is e5ec!ted$ the tas+ enters the ready state. 'he ne5t time the tas+ enters the r!nning state$ an #nterr!ptedE5ception is thrown. 0) @ow can we create a thread? & thread can be created by e5tending 'hread class or by implementing C!nnable interface. 'hen we need to override the method p!blic void r!n3). 12) What are three ways in which a thread can enter the waiting state? % thread can enter the waitin" state b invo+in" its sleep() method! b bloc+in" on ,-.! b unsuccessfull attemptin" to ac/uire an

ad ed

-) What is the difference between process and thread? Process is a program in e5ec!tion whereas thread is a separate path of e5ec!tion in a program.

fro m

re j

in

() What is synchroni7ation and why is it important?

pa ul .c

om

object0s loc+! or b invo+in" an object0s wait() method. ,t can also enter the waitin" state b invo+in" its (deprecated) suspend() method. 11) @ow can i tell what state a thread is in ? Prior to 6ava ($ is&live3) was commonly !sed to test a threads state. #f is&live3) ret!rned false the thread was either new or terminated b!t there was simply no way to differentiate between the two.

1") What is seriali7ation?

1%) What does the ?eriali7able interface do? ?eriali7able is a tagging interfaceL it prescribes no methods. #t serves to assign the ?eriali7able data type to the tagged class and to identify the class as one which the developer has designed for persistence. ObjectO!tp!t?tream seriali7es only those objects which implement this interface. 1() When yo! will synchroni7e a piece of yo!r code? When yo! e5pect yo!r code will be accessed by different threads and these threads may change a partic!lar data ca!sing data corr!ption. 1*) What is daemon thread and which method is !sed to create the daemon thread? ;aemon thread is a low priority thread which r!ns intermittently in the bac+ gro!nd doing the garbage collection operation for the java r!ntime system. set;aemon method is !sed to create a daemon thread. 1-) What is the difference between yielding and sleeping? When a tas+ invo+es its yield3) method$ it ret!rns to the ready state. When a tas+ invo+es its sleep3) method$ it ret!rns to the waiting state.

do w

nl o

1.) What is casting? 'here are two types of casting$ casting between primitive n!meric types and casting between object references. asting between n!meric types is !sed to convert larger val!es$ s!ch as do!ble val!es$ to smaller val!es$ s!ch as byte val!es. asting between object references is !sed to refer to an object by a compatible class$ interface$ or array type reference.

10) What classes of e5ceptions may be thrown by a throw statement? & throw statement may throw any e5pression that may be assigned to the 'hrowable type.

ad ed

fro m

re j

in

?eriali7ation is the process of writing complete state of java object into o!tp!t stream$ that stream can be file or byte array or stream associated with ' P:#P soc+et.

pa ul .c

12) What is synchroni7ed +eyword? #n what sit!ations yo! will Ese it? ?ynchroni7ation is the act of seriali7ing access to critical sections of code. We will !se this +eyword when we e5pect m!ltiple threads to access:modify the same data. 'o !nderstand synchroni7ation we need to loo+ into thread e5ec!tion manner.

om

22) & 0read is r!nnable$ how does that wor+? 'he 0read classB run method normally invo+es the run method of the Runna*+e type it is passed in its constr!ctor. @owever$ it is possible to override the threadBs run method with yo!r own. 21) an # implement my own start46 method? 'he 0read start46 method is not mar+ed 7ina+$ b!t sho!ld not be overridden. 'his method contains the code that creates a new e5ec!table thread and is very specialised. Go!r threaded application sho!ld either pass a Runna*+e type to a new 0read$ or e5tend 0read and override the run46 method.

do w

nl o

2() @ow wo!ld yo! implement a thread pool? 'he 'hreadPool class is a generic implementation of a thread pool$ which ta+es the following inp!t ?i7e of the pool to be constr!cted and name of the class which implements C!nnable 3which has a visible defa!lt constr!ctor) and constr!cts a thread pool with active threads that are waiting for activation. once the threads have finished processing they come bac+ and wait once again in the pool. 2*) What is a thread gro!p? & thread gro!p is a data str!ct!re that controls the state of collection of thread as a whole managed by the partic!lar r!ntime environment.

ad ed

2%) What are the different ways in which a thread can enter into waiting state? 'here are three ways for a thread to enter into waiting state. Ay invo+ing its sleep3) method$ by bloc+ing on #:O$ by !ns!ccessf!lly attempting to ac/!ire an objectBs loc+$ or by invo+ing an objectBs wait3) method.

fro m

2") What is thread priority? 'hread Priority is an integer val!e that identifies the relative order in which it sho!ld be e5ec!ted with respect to others. 'he thread priority val!es ranging from 1, 12 and the defa!lt val!e is (. A!t if a thread have higher priority doesnBt means that it will e5ec!te first. 'he thread sched!ling depends on the O?.

re j

in

22) ;o # need to !se s8n(0roni9ed on set.a+ue4int6? #t depends whether the method affects method local variables$ class static or instance variables. #f only method local variables are changed$ the val!e is said to be confined by the method and is not prone to threading iss!es.

pa ul .c

om

Вам также может понравиться