You are on page 1of 11

Kuvempu University Assignments for B.Sc.(IT) & M.Sc.(IT) Courses Subject !

A"A #rogr$mming Subject Co%e BSIT & '( Assignment TA (Compu)sory) 1) Explain basic features of Java. 2) Explain how Java differs from C and C++. 3) What is the difference between ob ect oriented and procedure oriented pro!rammin! " #) Explain the followin! $$%s concept. a) ob ect and classes. b) &bstraction. c) Encapsulation. d) 'nheritance. e) %ol(morphism. f) )essa!e passin!. *) What are the advanta!es of ob ect oriented pro!rammin! " +) Explain the structure of a Java pro!ram. ,) With an example explain Java to-ens. .) With an example explain different data t(pes in Java. /) With an example explain all the operators in Java. 10) With an example explain the followin! statements in Java. a) simple if b) if..else c) nested if d) else..if ladder 11) With an example explain the switch statement in Java. 12) Compare while and do..while statements in Java. 13) With an example explain for statement in Java. 1#) With the help of an example pro!ram explain brea- and continue statements in Java.

Assignment TB (Compu)sory)

PART-A 1. How platform independence is achieved in JAVA ?

Java is a platform independent language.After compiling the ".java" file ,that will be converting into the ".class" file,which is a byte code having the capability run on any OS.Basing on the concept byte code java achieving the platform independent,it leads to " rite onece run anyware".
2. List any three features of JAVA.
The Java Virtual Machine - Garbage Collection - Code Security Java Features: Java Virtual Machine (JVM) - an imaginary machine that is implemented by emulating so t!are on a real machine - provides the hard!are plat orm speci ications to !hich you compile all Java technology code Bytecode - a special machine language that can be understood by the Java Virtual Machine "JVM# - independent o any particular computer hard!are$ so any computer !ith a Java interpreter can e%ecute the compiled Java program$ no matter !hat type o computer the program !as compiled on& Garbage collection thread - responsible or reeing any memory that can be reed& This happens automatically during the li etime o the Java program& - programmer is reed rom the burden o having to deallocate that memory themselves Code security is attained in Java through the implementation o its Java 'untime (nvironment "J'(#&

3. What is Java ! Javadoc and Jd"c?


The )avac command is used to invo*e Java+s compiler and compile a Java source ile& , typical invocation o the )avac command !ould loo* li*e belo!:

)avac -options. -source iles. Javadoc is a documentation generator rom Sun Microsystems or generating ,/0 documentation in 1TM2 ormat rom Java source code& (Java DataBase Connectivity) A programming interface that lets Java applications access a database via the SQL language. Since Java interpreters (Java Virtual Machines) are available for all ma or client platforms! this allo"s a platform# independent database application to be "ritten

#. List different features of $$%.


3& 4& 5& (ncapsulation /olymorphism 0nheritance

&. 'ame different types of Java to(ens.


0n a Java program$ all characters are grouped into symbols called tokens& 2arger language eatures are built rom the irst ive categories o to*ens "the si%th *ind o to*en is recogni6ed$ but is then discarded by the Java compiler rom urther processing#& 7e must learn ho! to identi y all si% *ind o to*ens that can appear in Java programs& 0n (89F !e !rite one simple rule that captures this structure: token :; identifier < keyword < separator < operator < literal < comment The di erent types o To*ens are: 0denti iers: names the programmer chooses =ey!ords: names already in the programming language Separators "also *no!n as punctuators#: punctuation characters and paired-delimiters ?perators: symbols that operate on arguments and produce results 2iterals "speci ied by their type# 9umeric: int and double 2ogical: boolean Te%tual: char and String 'e erence: null Comments 2ine

3& 4& 5& >&

&.
o o o o o

@&

). What is the tas( of the main method in Java pro*ram ?


0 you run an )ava ile"as an &class or &)ar ile# there+s al!ays 3 method being called: The main"String-. args# method& The method is only called once& (%ample o an main method: public static void main"String args-.# throws 0?(%ception A 2ogging8ootstrap&bootstrap"#B gui ; new GC0"#B D

0n this case it only bootstraps the logger and uses the constuctor method o GC0"the graphical user inter ace o the program#

+. Why can,t we use a (eyword as a varia"le name ?


0t is because$ the *ey!ords are de ined in the )ava language or a speci ic purpose& 0 !e start declaring variables !ith the same name$ the compiler !ould not *no! i you are trying to use a variable are trying to tell it to per orm an action and hence *ey!ords cannot be used as a variable name&

-. Why main method in Java is declared as static ?


8ecause$ the main method is the starting point o the )ava program and i !e need an ob)ect o that class even be ore the main can be invo*ed$ it is not possible& 1ence it is declared static so that the JVM Can acess the main method !ithout having to instantiate that particular class

.. Write an e/uivalence of switch statement and if statement.


Write an e1uivalent 2witch 2tatement for the if statement if 3ch445+5) cout 667arithmetic operator78 else if 3ch44565) cout667relational operator78 else if 3ch44595) cout66 7punctuation78 else cout 667identification is not supported7.

10. onstructor is1111111111111.


, constructor creates an ?b)ect o the class that it is in by initiali6ing all the instance variables and creating a place in memory to hold the ?b)ect& 0t is al!ays used !ith the *ey!ord new and then the Class name& For instance$ ne! String"#B constructs a ne! String ob)ect&

PART - B 1. a2 What is inheritance and how one can achieve code reusa"ility? 34plain with an e4ample.
0nheritance in Java re ers to the eature !herein the codeE unctionality o one class can be used in another class that e%tends this class& (%ample: public class /arent A &&& && D

public class Child e%tends /arent A &&& && & D 1ere the class Child e%tends the class /arent and hence the methods o /arent are available or Child to use& This !ay !e are re-using the code in the parent class in the child class instead o re-!riting the !hole thing again&

"2 What is a class? How does it accomplish data hidin*? 34plain with e4ample.
, class is a collection o data and methods to !or* !ith that$ and other$ data& ,n ob)ect is a speci ic instance o a class& For e%ample$ F,rray2istF is a class that contains methods such as FaddF and FremoveF or changing the items in the list& The instances "ob)ects# o ,rray2ist can use these methods to per orm operations& 0t+s easier to sho! the structure o things !ith a custom class& public class MyClass A EEGata E FFieldsF: private int numB EEConstructors: public MyClass"# A D public MyClass"int initial9umValue# A num ; initial9umValueB D EEFGettersF and FSettersF: public int get9um"# A return numB D public void set9um"int ne!9umValue# A num ; ne!9umValueB D D

2. a2 ompare in terms of their functions and semantics the followin* pairs of statements5 i2 do while and while
7hile : 0n 7hile loop the condition is tested irst and then the statements are e%ecuted i the condition turns out to be true&

Go-7hile : 0n do !hile the statements are e%ecuted or the irst time and then the conditions are tested$ i the condition turns out to be true then the statements are e%ecuted again&

ii2 while and for iii2 nested if and switch iv2 "rea( and continue. "2 Write a pro*ram to find sum of the followin* harmonic series for a *iven value of n 16 172 6 173 6 . . . .6 17n.
There is no correct simple general ormula or sum to n terms o the series 3H3E4H3E5H3E>H &&&&&&&&&&&&& H 3En The ollo!ing e%pression is relatively a very good appro%imation& S ; ln"n H I&J# H I&JKK4 H I&I5KJLE"nMn H 3&3K3# Geviation rom the actual value luctuates but remains relatively lo!&

3. a2 34plain different data types in Java with e4ample.


byte: The byte data type is an N-bit signed t!o+s complement integer& 0t has a minimum value o -34N and a ma%imum value o 34K "inclusive#& The byte data type can be use ul or saving memory in large arrays$ !here the memory savings actually matters& They can also be used in place o int !here their limits help to clari y your codeB the act that a variable+s range is limited can serve as a orm o documentation& short: The short data type is a 3@-bit signed t!o+s complement integer& 0t has a minimum value o -54$K@N and a ma%imum value o 54$K@K "inclusive#& ,s !ith byte$ the same guidelines apply: you can use a short to save memory in large arrays$ in situations !here the memory savings actually matters& int: The int data type is a 54-bit signed t!o+s complement integer& 0t has a minimum value o -4$3>K$>N5$@>N and a ma%imum value o 4$3>K$>N5$@>K "inclusive#& For integral values$ this data type is generally the de ault choice unless there is a reason "li*e the above# to choose something else& This data type !ill most li*ely be large enough or the numbers your program !ill use$ but i you need a !ider range o values$ use long instead& long: The long data type is a @>-bit signed t!o+s complement integer& 0t has a minimum value o -L$445$5K4$I5@$NJ>$KKJ$NIN and a ma%imum value o L$445$5K4$I5@$NJ>$KKJ$NIK "inclusive#& Cse this data type !hen you need a range o values !ider than those provided by int& float: The float data type is a single-precision 54-bit 0((( KJ> loating point& 0ts range o values is beyond the scope o this discussion$ but is speci ied in section >&4&5 o the Java 2anguage Speci ication& ,s !ith the recommendations or byte and short$ use a float "instead o double# i you need to save memory in large arrays o loating point numbers& This data type should never be used or precise values$ such as currency& For that$ you !ill need to use the )ava&math&8igGecimal class instead& 9umbers and Strings covers Big!ecimal and other use ul classes provided by the Java plat orm& double: The double data type is a double-precision @>-bit 0((( KJ> loating point& 0ts range o values is beyond the scope o this discussion$ but is speci ied in section >&4&5 o the Java 2anguage Speci ication& For decimal values$ this data type is generally the de ault choice& ,s mentioned above$ this data type should never be used or precise values$ such as currency&

boolean: The boolean data type has only t!o possible values: true and false& Cse this data type or simple lags that trac* trueE alse conditions& This data type represents one bit o in ormation$ but its Fsi6eF isn+t something that+s precisely de ined& char: The char data type is a single 3@-bit Cnicode character& 0t has a minimum value o "#u$$$$" "or I# and a ma%imum value o "#uffff" "or @J$J5J inclusive#&

"2 8escri"e the structure of a typical Java pro*ram.


, Typical Java class !ould have the ollo!ing& /ac*age statement import statements Class comments "?ptional# Class Geclaration A 0nstance variable declarations Constructor declaration Method declarations D

#. a2 What is JV9 ? How does it help to achieve platform independence? :f JV9 is availa"le for windows then can we run pro*ram written on ;ni4 platform ? omment.
, Java Virtual Machine "JVM# is a set o computer so t!are programs that use a virtual machine model or the e%ecution o Java computer programs and scripts& The JVM accepts data in a orm commonly re erred to as Java bytecode& This language conceptually represents the instruction set o a stac*-oriented$ capability architecture&

)avac - it is compiler !hich converst )ava source code to byte code ie&class ile& This byte code is standard or all plat orms$ machines or operating systems& )ava - this is interpreter& this interprets the &class ile based on a particular plat orm and e%cutes them&e )vm -O )ava virtual machine comes into play& )vm or !indo!s !ill be di erent rom )vm or solarais or linu% & but all the )vm ta*e the same byte code and e%ecutes them in that plat orm& Source code -O )avac -OCniversal byte code Cniversal byte -O)vmE)ava -O e%ecute them on a particular machine& There can be any number o )vm+s but they all understand the one common language called byte code and translates them into the binary e%ecutable o a particular plat orm or !hich they are developed& There is separate development section in sun microsystems is to develop the )vm or a particular plat orm& or each upgrade o the )d* $ the )vm or each plat orm is updated&

"2 How are data and method or*ani<ed in an o"=ect oriented pro*ram ? :llustrate the same for car o"=ect.
There are 4 !ays: Fprivate or protectedF and FpublicF& , programmer has the choice o ma*ing the ob)ect+s data either protected or public& /rotected means the data associated !ith that ob)ect can be only accessed by the ob)ect+s o!n methods& 0t can+t be globally accessed by simply calling the variable& /ublic means it can be accessed rom any unction& (&g: Class /erson A /rotected: Char nameB Char hair-colourB change haircolour change name The above class can have an ob)ect o person !ith his name and hair-colour& ,s you can see it is protected and no one else can change them e%cept your o!n unctions o change hair-colour and change name& That+s ho! data and methods are organi6ed& 0t is a security eature$ called data encapsulation&

&. a2 8istin*uish "etween the followin* terms5 i2 8ynamic "indin* and messa*e passin*. ii2 :nheritance and polymorphism. "2 What are the difference "etween and Java ?How Java and 66 are similar?
Java is an pure ob)ect oriented programming language$ it uses the concepts o Classes$ ?b)ects$ 0nheritance$ /olymorphism& ,nd the e%ecution o a program is non-linear& 0t is so called because you can+t !rite a program !ith out using classes P ob)ects& C uses concept o structures "not ob)ect oriented#& 0n C !e use the concept o pointers !hereas there are no pointers used in J,V, 0n C the programmer needs to manage memory manually& Fmalloc"#F and F ree"#F are the undamental memory allocation library calls& Java !as created or the purpose o ma*ing a language that could be implemented on many di erent types o computers "cell phone$ mac$ /C$ linu%$ etc&# C on the other hand can only be run on a computer o the same type as the one that compiled the program& Java is also ob)ect-oriented$ !hereas C is not& Java allo!s a user to create classes that contain data and methods& C is not capable o doing that&

+. a2 8efine pro*rammin*. What are the types of pro*rammin*? 34plain.

& progr$mming )$ngu$ge is an artificial lan!ua!e desi!ned to express computations that can be performed b( a machine: particularl( a computer. %ro!rammin! lan!ua!es can be used to create pro!rams that control the behavior of a machine: to express al!orithms precisel(: or as a mode of human communication.

structured programming procedure oriented programming ob)ect oriented programming

"2 34plain "itwise operators in Java with e4ample


Java+s bitwise operators operate on individual bits o integer "int and long# values& 0 an operand is shorter than an int$ it is promoted to int be ore doing the operations& 0t helps to *no! ho! integers are represented in binary& For e%ample the decimal number 5 is represented as 33 in binary and the decimal number J is represented as 3I3 in binary& 9egative integers are store in two's complement orm& For e%ample$ -> is 3333 3333 3333 3333 3333 3333 3333 33II&

he bitwise operators

P - and < - or Q - Ror S - not :: - le t shi t OO - right shi t OOO - right shi t (%amples: 5 P J ; 3 "3 i both bits are 3&# 5 < J ; K "3 i either bits are 3# 5QJ ; @ "3 i both bits are di erent# S5 ; -> "0nverts the bits# 5 :: 4 ; 34 "Shi ts the bits o n le t p positions& Tero bits are shi ted into the lo!-order positions&# J OO 4 ; 3 "Shi ts the bits o n right p positions& 0 n is a 4+s complement signed number$ the sign bit is shi ted into the high-order positions&# -> OOO 4N ; 3J "Shi ts the bits o n right p positions& Teros are shi ted into the high-order positions&#

. +. a2 List and e4plain different types of loops in Java.


Java has three *inds o loops 3& For 2oop 4& 7hile 2oop 5& Go - 7hile 2oop 8oth For loop and 7hile loop !ould iterate through a certain lines o code !ithin the loop+s limit as long as the loop condition is satis ied& , do !hile loop !ould e%ecute the loop once even be ore chec*ing the condition& So in a do !hile loop$ even i the loop condition is not satis ied the loop !ould e%ecute once& (%ample Geclarations: or"int i ; IB i : nB iHH# A &&&&& D

!hile "i : n# A &&& iHHB D do A &&& iHHB D !hile "i : n# B

"2 What is polymorphism? 34plain method overridin* with e4ample and prove that it is a polymorphism.
polymorphism means the ability to ta*e more than one orm& This is something similar to a !ord having several di erent meanings depending on the conte%t& /olymorphism in Java is implemented using Method ?verloading and Method ?verriding& ?verloading - More than one method in the same class !ith the same name but di erent signature ?verriding - Method !ith the same name in child class mas*ingEhiding the method in the parent class

-. a2 What is a parameterised constructor? 34plain its usa*e with an e4ample.


, parameteri6ed constructor in )ava is )ust a constructor !hich ta*e some *ind o parameter "variable# !hen is invo*ed& For e%ample& class MyClass A EEthis is a normal constructor public MyClass"#A EEdo something D EEthis is a parameteri6ed constructor public MyClass"int var#A EEdo something D EEthis is another parameteri6ed constructor public MyClass"String var$ 0nteger var4#A EEdo something D D

"2 34plain the methods >i2 ?rim

>ii2 @u"strin* >iii2 Len*th.

You might also like