Ca 1
Ca 1
Usin g Java
Unit- 1 Impo rtant Que stion s
With Answ ers
SUBSCRIBE ~ Share
\\'hat an: the features of Java? [ANU 17; AdNU 16; AU 16; BRAU 18; KU 17, 16;
AU 18, 17; SVU 17; VSU 18; YVU 16)
Sun Micro systems officially describes java with the following anributcs.
I) Compiled iln<.Iinterpreted :Usunlly a computer language_is ei_thcr compiled or
interpreted. Ja\'a combines both tJ1ese approaches thus _makmg Java a two stage
s,·stem. First. ja\':l compiler translates source code into known as bytecode
i~structions. Bvtec,odes arc not machine instructions and therefore, in the second
stage. ja\'a inte~relcr generates machine code that can be directly executed by the
machine that is running the java program.
~) Platform-independent and portable : Java programs can be easily moved
from one computer system to another, anywhere and an)1ime. Changes and upgrad~
in operating systems. processors and system resources will not force any changes in
java programs.
Ja\'a ensures portability in two ways. First, java compiler generates bytecode
instructions that can be implemented on any machine. Secondly, the size of the
primitive datatypes are machine independent.
3) Object-oriented : Java is true object oriented language. All program code and
data reside with in objects and classes. Java comes with an extensive set of classes,
arranged in packages, that we can use in our programs by inheritence.
4) Robust and secure: Java is robust language. It provides much secure & rc!liable
code. It has strict compile time and runtime checking for data types. It has a garbage
collector to rectify memory management problems. It also has the concept of
exception handling.
5) Distributed : Java applications can open and access remote objects on internet.
Multiple programs can communicate from client system to the server system.
6) Fam iliar, simple and small : Java is a small and simple language. Because,
java does not use pointers, preprocessor header fi !es, goto statement and many
others. It also eliminated operator overloading and multiple inheritence.
Familiarity is another striking feature of Java, to make the language look familiar
to the existing programmers, it was modeled on C and C++ languages.
7) Multithreaded and interactive : Multithreaded means handling multiple tasks
simultaneously. Java supports multithreadcd programming.
This means that we need not wait for the application to finish one task before
beginning another. This feature greatly improves the interactive performance of
graphics applications.
8) High performance : Java performance is impressive for an interpreted language,
mainly due to the use of intem1ediate byte code. According to sun, java speed is
comparable to the native CIC++.
an d I.angc or all integer data types .
.
Table below show s the memo ry. size
Type Size Minimum v~aluc Maximum Value
(in by~s)
byte I - 128 127
short 2 -32,768 32,767
int 4 -2, 147, 483, 648 2,147, 483,647
long 8 . -9,223,372, 036, 854, 775,808 9,223 , 372.036.854, 775,80".'
We can make iniegers long by appending the letter Lor I at the end
of th"·
number.
Example : 123Lor 1231.
2) Floating point Type s: Floating point type to hold numbers co~ta
ining fra~ti?nal
parts such as 12.34 and - 1.34. There are two kinds of floating pomt st0
rage m Java
as shown in figure below.
--- --- ,
Floating Point
Double ·
The float type values are single-precision numbers while the double types
represent
double precision numbers. Table below gives the size and range of these
two types.
'I)'pe Size (in bytes) Mini mum Value Maxi mum Valu e
Float 4 3.4e - 038 3.4e + 038
Double 8 l.7e- 308 l.7e + 308
Floating point data types suppo11 a special value known as Not-a-Num
ber (NaN) is
used to represent the result of operations such as dividing zero by
zero, where an
actual number is not produced.
3) Char acter Type : Java provides a character data type called
char. The char
type assumes a size of 2 bytes but, basically, it can hold only a single
character.
4) Boolean Type : Boolean type is used when we want to test a partic
ular condition
during the execution of the program. There are only two values that
a boolean type
can take: true or false. Boolean type is denoted by the keyword boole
an and uses
only one bit of storage.
·
Explain th~ various 1ypcs of opcralors used in Java? (ANU 18; AdNU 17;
BRAU 18,17,16; KU 18,17,16; RU 17,16: SKU 18,17; SVU 18,17; YVU 18,17,16)
Operators are used in programs to manipulate dala and variables. They usu~lly f?nn
a part of malhematical or logical expressions. Java operators can be classified 11110
a number of related categories as below :
i) Arithmetic operators
iij Relational operators
iiQ Logical operators
iv) Assignment operators
v) Increment and decrement operators
vi) Conditional operators
vii)Bitwise operators
viii) Special operators
Q ARITHMETIC OPERATORS : Java provides all the basic arithmetic
operators to construct mathematical expression. The operators+, -. • and I all work
the same way as they do in other languages.
Arithmetic operators
Operator Meaning
+ Addition
\
- Subtraction
\
• Multiplication
I Division
% Modulo division
Arithmetic operation are used as shown below :
a-b a+ b . a%b
a •b aI b -a • b
Here a and b may be variables or constants and are known as operands.
a) Integer Arithmetic: When both tht operands in a single arithmetic expression
such as a+b are integers, the expression is called an integer expression and the
operation is called integer arithmetic.
b) Real Arithmetic: An arithmetic operation involving only real operands is called
real arithmetic. A real operand may assume values either in decimal or exponential
notation. Unlike C and c-+, modulus operator% can bt= applied to the floating point
data as well.
c) Mixed-mode Arithmetic : When one of the operands is real and the other is
integer, the expression is called a mixed-mode arithmetic expression.
ii) RELATIONAL OPERATORS : We often compare two quantities and
depending on their relation, take certain decisions. These comparisons can be done
with the help of relational operators. Java supports six relational operators These
operators and their meanings are shown below. '
0 crator Mcanin
< is lessthan
<= is less than or equal to
> is grcntcrlhan
>= is grcnlcr than or equal to
is cqunl tu
!= · is not cqunl to
Truth table:
Value or expression
OP-1 OP-2 OP- l&&OP-2 OP-1 II OP- 2
true true ~
_J
true true
~
All language compilers translate source code into machine code for a specific
computer. Java compiler also does the same thing. Java compiler produces an
inte1111cdiate code known as byte code for a machine that doesnot exist. This machine
is called the Java virtual machine and it exist only inside the computer memory.
The Java language has an unusual operator, useful for making two-way decisions.
This operator is a combination of? and: and takes three operands. This operator is
popularly known as the conditional operator. This is also called ternary operator.
The general form oHASe the conditional operator is as follows:
Conditional expression? expression 1 : expression 2
'
. The conditional expression is evaluated first. If the result is true, expression 1 is
evaluated and is returned as the value of the conditional expression. Otherwise,
expression2 is evaluated and its value is returned.
Example:
class ConditionalOperator .
. {
public static void main (String args0)
{
True
False
Statement- block
Statement - x
}
else
{
· System.out.println("you are not.eligible to vote");
}
System.out.println ("Thank you");
}
}
ili) Nesting of if ...else statements : When a series of decisions are involved, we
may have to use morethan one if .... else· statement is nested form as follows.
if (test condition 1)
{
if(test condition 2)
{
statement-1;
}
else
{
statement- 2;
}
}
else
{
statement - 3;
}
statement - x ;
Ifthe condition-I is false, the statement-3 will be executed : Otherwise it continues
to perfonn the second test. Ifthe condition-2 true, the statement-I will be evaluated;
otherwise the statement-2 will be evaluated and then the control is transferred to the
statement-x .
l:111ry
Fuhc ·1rue
Slalcmcnt•x
else if (condition n)
statement-n;
else default-statement ;
statement-x;
This construct is known as the else if ladder. The con~itions are evaluated from
the top to downwards. Ifthe true condition is found, the statement associated with it
is executed and the control is transferred to the statement - x.
. .
True
Statement-I
Statement-2
False
Si.tement-3
Statement-n Dl!!'ault
Statement
Statement • n
Java bas a built in multi way decision statement known as switch. The switch
statement tests the value of a given variable( or expression) against a list of case
values and when a match is found, a block of statements associated with that case
is executed. The general form of the switch statement is as shown below.
switch(variable)
{
case value I :
Statement block I :
break;
case value2 :
Statement block2;
break;
case valueN :
Statement blockD;
break;
}
Statement-X;
e ofthe variable given in the switch-
The switch case statement compares the valu
nt that follows . When the value of the
statement with the value of eac h case stateme
statement block of that pan icu lar case
switch and the case stah:~rnen t matches, the
is executed.
break statement must be used at
In the syntax of the switch case statement, The
tells the compiler to jum p out of the
the end of each case. The break statement
nt following the switch case construct.
switch case statement and execute the stateme
us go through the following rules :
To summarize the switch case construct, let
word switch must be inte gral type.
• The control expression that follows the key
• Case lab le must end with a colon.
d only when the value of the expression
• The default label is optional and is execute
ression .
does not match with .:? :iy labe lled constant exp
TRU I::
l'Ce ---4
l
Statement block2
TRU E
Statement blockD
- - - - - - - - - - - - J S1atc mcn1X
'
switch (ch)
{
case 'a' :
System.out.println("lt is vowel");
break;
case 'e' :
System.out.println('_'lt is vowel~');
break;
case ';1 :
System.out.println("lt is vowel");
break;
case 'o' :
System.out.println("lt is vowel");·
break ;
case 'u':
System.out.println("lt is vowel");
break;
default :
System.out.println("lt is not vowel");
}
}
}
Output :
It is vowe l.
Explain about various types of looping statements in Java program?
[ANU 18, 16i AdNU 18, 17, 16i AU 18i BRAU 18, 17, 1&i KU 18, 16i
RU 17, 16; SKU 18, 16; SVU 18, 17; VSU 18, 17, 16; YVU 18]
(or)
Write a short notes on iterative statements that Java language supports?
When a single statement or a group of statements will be executed again and again
in a program then such type processing is called Loop(or iteration). The Java
programming language contains 3 different programming statements for program
looping
1. for Loop 2. while Loop 3. do while Loop
1. For Loop: It is a looping statement which repeat again and again till it satisfies
the define condition it is also entry control loop.
Syntax:
for(initialization statement; test expression; update statement)
{
codc/s to he executed~
lnitiolizotion
stotcmcnl
}
The initia lizati on statem ent is Updotc
stotcmcnt
executed only once at the beginn ing
of the for loop . Then the test
expre ssion is check ed ~y th_e
True Body of
progra m. (f the test expres sion ~s for loop
false, for loop is termin ated. But tf
test expres sion is true then the code/
s inside body of for loop is execu ted Exit for loop
and then updat e expre ssion is
update d. This proces s repeat s until
test expres sion is false . Statement just
below for loop
Exam ple: Progr am to displa y from
l to 10 using for loop
class ForEx ample Flowchart of for loop
{
public static void main (Strin g args[] )
{
for(int i=l ; i<IO; i++)
{
System .out.p rint(i+ " ");
}
}
}
Output:
1 234 567 891 0
2. While Loop : The while loop is best suited to repeat a statem
ent or a set of
statem ent as long as some condi tion is satisfi ed the ge_neral form
of while loop is
while ( test condi tion)
{
Body of the loop
}
The while is an entry contro lled loop statem ent. The test condi tion
1s evalua ted
and if the condi tion is true, then body of the loop is execu ted. After
execu tion of
the body, the test condi tion is once again evalua ted and if it is true,
the body is
execu ted once again. This proces s contin ues until the test- condit ion
finally becom es
false the contro l is transf erred out of the loop and the body of the
loop may have
one or more statem ents. If the test condi tion evalu ate to false at
the first time the
statem ents are never execu ted.
False
>-----< E x it while loop
True
Body of
whil e loop
Body of
while loop
True
True break ;
False
Remaining part of
loop
An array is a kind of variable that refers to a collection of da1:i items whidi all h.ivc
same name an<t same data type. The clements of an ;-irray c:in b,.: <l:11a1:, p-.:. Al l th.:
elements in an array must be of sam~ type. There arc two Iy p~s of mr:1ys.
1. One Dimensional arrays
2. Multi Dimensional arrays
1) One Dimensional Arrays : A List of items can be given arc variable Name
using only one subscript([]) and such ,1 variable is called a arc dimcn.,iunal /\rroy
(or) Single subscripted variable.
The general systax of declaring one dimensional array is :
rype ~rrayName [] = new type [size];
Example : int a[] = new int [3];
Here type declares the basic type of the Array, arrayName is the name of the
array size the number of elements that anay contains.
2) Multidemensional Arrays : Multidemensional arrays represent ~D, 3~ ....
arrays. A Two dimensional array is a conrbiuation of two or more (ID) the d1mens1onal
1111 -ny'i. 1111cc cli1111.·11,1111111I a,rny '"
;\ II c11mh11111t.io11 of rwu or nuu,c (21>) '""o
d1111cw,i111111I 111111) s
, ) I\, 11 I >iIII l' 1"'i111111 I II rrn ~ s ( 21> nr rn y )
/\ t\\11 tli111c11"in11nl nrrny rrpn:sc11ls scvcrnl rows 1111<1 columns of cl1111. To
ll'ptt.''it.'nl II lw,, "i111cnsi111111I 111rny, we should 11:--c rwu pain uf ' llllllrC hr:u.:"cls I I, II
11 lh-1 thl· 11n11y N11111e. ;\ two di111cnsi111111I nrray dcclurr,1 ns type nrrnyNnmc (III •
""'" rypc I110\\ -~i,c II col-si ,c J:
l•:x1111119lc : int 11 1111 ·- 11cw i11t I) I PI:
ii) Three <limc11sinn11I Arrnys : We c1111 c,111sidcr II three dimcnsionnl nrrny u a
crn11in11t11r of scvcrnl two di111cn-.i111111I nrrnys. To rcprc:--cnt a three dimcusiunal
11rrny. we shoulil use three pairs uf squnrc hrm:kcrs.
;\ three tfo11c11siu11nl nrruy dcdnrcd ll'i
type 11rrnyN11111c 111111 u ncw lsi,cl lsi1cl [sizeJ;
l•'.x111111,lc: int 11111111 • new int Pl (31 IJJ;