0% found this document useful (0 votes)
21 views29 pages

Objective

The document contains a series of Java code snippets and programming questions related to object-oriented programming concepts, including class inheritance, method overriding, and error handling. It also includes examples of compilation errors and expected outputs for various code scenarios. The content is structured as a self-assessment for programming using Java, aimed at evaluating understanding of the language's syntax and functionality.

Uploaded by

januneela17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views29 pages

Objective

The document contains a series of Java code snippets and programming questions related to object-oriented programming concepts, including class inheritance, method overriding, and error handling. It also includes examples of compilation errors and expected outputs for various code scenarios. The content is structured as a self-assessment for programming using Java, aimed at evaluating understanding of the language's syntax and functionality.

Uploaded by

januneela17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

c1ass A {

pubU C int cou nt;


publ iC AO {
cou nt= 10;
}
pub1iC int meth odl () {
iJlt cou nt= 20;
retu rn this .cou nt;
}
}
c1a ss B exte nds A {
pub lic int metho dl() {
retu rn this . cou nt = 1.s;
}
pub lic int , methcl.12 0 {
retu rn 20 ;
}
}
clas s c exte nds B { ~
publ ic i nt method!/.() {
retu rn 40 ;
} c., ,,\ ,,
} ' :,.,
publ ic ( I.ass Tes t~r {'
publ it stat i c void mai n(:St
'.r~ng ' arg s[] ) {
A obj l c new A();
B obj2 = new B() ;
C obj3 ., ~e l, C() ;
syst em. out .pri ntl ri(ob jl . m~t
hod l () + obj3 . methodl () + O'
,!! }
}
Clicking on !)!ext o r Pre,vious
will s ubm it y,our a nsw e r.
class E111Ployee{
!poolic int employ~Id;
private double bas icsalar,f ;
private double t ~ 'lfflal.Salary; u~ )"1
,imblic ~emplo~ee(d~uble basic;s~J.ary) t
. lfl! .I
I;~s.basicsa~ary ':' basicsa1ahi "" "
public double get:!Basicsalary() {
return basicsalary;
}
public do~bl e gEeyotalsalary() {
returJt totalsalarv;
} ,1 1111
1
,;1irn'i!;
II
11; }
'li ,11
l\'I\\;• - :J:: II
putilic ,ii.nt
'
,ge-U
,111,t;
111
re"l:urn~etiployeeJ
~
}
pub~,~i:: double calculpt:i:!1:_iite-1Sa1ar,y ( ~rt bonus~'er-ce nt) {
I/ line 22
ret1,1rn total
}
}
I~ ~:1\ij
,~~blil
,',Lbl:j1
Emp.loy
;\!111,
~ ploy
employ""'.,
,,
II lin~ :\ 2
,}
}
tt ~,, q:sp~av : t!)e folJ0 win,g cnJ~'p~t by ,,~l'\aos,,ng the appropriate code to be implem en t ed in Line 2 1 an d Li
llli
1 1
1 1
2
Y\~~111 2 , ~
n1 11 1'
1 11
11
-, :; •,
nL<1u,.;:;.L,111,..,,11~ ;.:,:1...:lll1,,111.1111r,.11,:;.~;,,;:1;, 1!ll..,.cli..w..,1ll'. J1,. ;,,1,.,r...i~......__,,
- --· ""'':-711
1
1
1' 11111 1
1_1,
f,,
lie class Te r :11 ,j,
1
~ • i l1 J
pi.t>lic static; vo~ Iii.inf
Ellployee employee= new ~I, j,
I\....,~ E"~--~ . . .
eaployee.empioye,eid 9 ..181]
eaployee.caltulilteTot:alSa[larytB);
// line ,~2 ,I,
} ,11 11~:~ ' 1111 !!Ji] !ll 1!l' 11l!li r iil1! 11!11! , ll!! I'
r r. , ,
.Jt~ ~J_ '1 41. · ."Ill;' 1··.. "111',
1
'"'."""''!!' "':ifflr '''· ""''I' ·•,· ii',,
11111111:_, ·,1:l1/·1ri._
I , :,['t . ·
, 1111,' ;;;!,•' .•.•···. ·. ·. ' ·1'1'1' ' 1d
~~-·.~1._i ,.;m,1,<1.!l1i'" _l:l 11ulfll, 11~,,,11·
1 I
~ir. ,~ . "
· I"· ,. 1
} 1111"'' 1
1iil~1 _.· ,.:· , ,: : §·!~•,u. ' i Iii,,,,, iJ_~il'"i":t~l
'' •
1 11
I . 1
'i 1111 ' hei p 11111.· e olli11,i1;I1e v " lo~er
111 _· 1
a:,, thi; f@llo
• 1
i•f"
ou 11rneA~ 11J¢11 ch · lill ff' ""
1
l1i11 111111!,,l'''I 11· ,,, ··-
..t ' ._Y nth_thdlil apo_,ropriate cefde,to be impl~mented
11
L f':i, ;I!,
I' f"' ,m .I ]:I:' .,,,.
111
11
·1_00 in Line
111111
'\". •·. I
·,111111~
EmflOyee I' !Iii 1
111' ,1 ri111 h ""
, . , 1,r .. 1111 llll'
Une 21 - tnis.tota1Sa1arv = th1s.basicSalarv 1!ti (1 + lbonusPerce · 1
1
,:, 11111 :11111 Ill . p .11:,
m 11 yee,em
1f
, Tqtal salary: "+ ,employee.fbtalSal ary}; 1 ···11
---·7r
to ~sicSalary * (1 + (d~ l~ ~n~ Percrent _ 1OOJ; ,l - .........
~--·----__;_-------------,-....- -----·-~-- ..... -------............1
lo !fee. Id:." + employee.getEmployee:ld()+ ·, Total salary~• + employee.getTotalSalary0)1
,,.... mi• -
·• ,
- ~ - •·ni:1,}
,11,1,1,1,11,
·•·
11
'11!!
11.,I
'"' -
~.
·,,.:-~,,~~=t.... -~..{t}:)5.{{j))}~}~~f !!!!!/!!!!!!!ffffjfffffffffffffJff ....
........ .... ........ ......................................
,, , , ,
- ~.........!...!...A-......... ....................... _.__._ ._ . .. - ~_,._ _. _._ _._.._.# :I
,,
••••••••••••••••••••• ! ~ .! .! .! _!--=--=---= ~~·::::::::::;;;;;;;;::t*11WK11fiMt!.@mHnIHifi\%1
A11Sessment - Programming using Java > 3: self Assessment - Programming using Java-20

What will be the output of the code.JJiven below?


public class Calculator{
private int add(int nUllll, int m.1n2j-{
return nuffll+num2j
}
}

public class Tester{


public static void main(str ing args[J) {
Calculator calculator= new calculator(};
system.out.println(calculator.add(l , 2));
}
'.}

!Iii

0 The code will result in a compilation-errot~a:?'1~e:met hod add cannot be accessed outside the-class.
- - - ~ r:1~ · - - - -4.. -

c:~pili:n
jj,,_. - - - ___- ....,;,;;.;,.I

The code win not result in a ~~·lw;~:~oot"~isplay any output. -


0 -
1
-: ,. : ,• .
;--=-
' .: I~,.I -
'
I -
- ...,...

Q The code will result in a c ompilat ion error as·a non-static method cannot be invoked from a static m ethod.

Clicking on Next or Previous will submit your answer.


self Atlll18111&Jl - PiUyta.nmina using Jalil > 4: Self Aal■l■■st •

e '
Fil ,1Q IR11:S T 1 [/Ii ' 1I ,,

e Clicking on Next or Prevrous will submit your,!illn::iwer


systm.wt.pr'DJ't.Ull -Q:,;--
}
}
}

:::::::::::::::::::······························ ·············· ······ ·······························


····· ·
.!. Killamsethy Praneetha

Self Assessment - Programming using Java > 5: Self Assessment - Programming using Java-12

What will be the output of the code given below?


public class Tester {
puplic static void main (String[] args) {
short discountPercentage = 7;
int noofltems = 10;
float pricePerrtem = 255.6f;
float taXAmount = 135 . Sef;
int discountedAmount = (noOfitems * (int ) pricePeritem)
* (1 - discountPercentage / 100);
double totalAmount = discount edAmount + taxAmount;
system.out.println("Total amount to be paid is"+ totalAmount);
}
}

IQ
I
Compilation error - cannot convert from float to int

Q Total amount to be paid is 2512.58

Q Total amount to be paid is 2685.5

Q Tot al amount t o be paid is 2591 .5

D O Clicking on Next or Previous Will submit your answer.


{
. getcounter()J i
11

111/%1 mil' ;r; "

. ,:,11
jJ:,/1 fil II I 11 .
,i

0 'I 00'1 'I 00 . . 002 ·1003 'I 003 'J 003


1
pUbliC statiC illt C

retuf1l couoteG
}
J-

Q 1001100210021oaa1m1eio04

0 ,0081 ooa 1 003 1003 HJ03 :JDGS


e Clicking on Next or Previous will submit your answer.
essmant- rammin

int nwn ="


int num2. = - .
int m.me = 19;
int null4 = ---40;
if {numl + num2 >= fl
j_f {num4 < num3) {
i..P={ntiin4 % n = =
cc sxstem.oot. .
} as-e: {

Q 1 ,.. J'

0 2

03
() 4

8 Clicking on Next or Previous will submit your answer


I
I
I' '
Ii I 11
I I ! )I
11
',, ·I

Q. syst:em.dut.prl11tl11(this.address.get21p0ode());
11

n Svstern.o~tcrintln(CLIStomer.address.zioCode}:
<c

· -~-· .. ~en t- Programming u

I I 11,111!111 l!II'
Iii I
I I
/1111 lid
, I I I Ill

!!!!E 0 Co111pUat1on ~rtor - no, f;)asslbie "~,pqpde'of Address class

8 Clicking on Ne;:t or Pri:-v1ous will submit your answe ·


!.. Killarnsethy Praneetha I
Self Assessment - Programming using Java > 1o: Self Assessment - Programming using J

hat wHt qe the output!


,,
public class Tester { 1, 1 •

public stat i c void main(string( } "args) {


int sum= e;
for (i nt i= 2,; i < s; i += 2) {
for (int 1 = 8,j j > ij j -= 2) { mi

if ( i >= j / 2) {
continue:;
1
} el se {
sum +=- .il +· j ~
'"
}
} m

} ..

system. out,_pr--intln( "Su


} .

Sum = B

1:1 Q Sum = 18
Iii)) 111

'ii I

I)) 11

0 Sum=
·7~1i-
~
36

11
• k' N ' p 1
,,,,. II ,,,u""' • 11111111
0 Cl 1c ing on ext or rev 1ous w1 su"ullll11t your answ~r
' I I 1111, , I 11 ,

'I . I
Self Assessma . ., . 1: Seit As~Htssment - Programming using
1

- --'.'..-IOilliloll,,,,-~ _~----------i
1

~-ll""'il"""'IMl~i oo,!l,,,• . - - - - - - - - - ~ - ; ; . , . . . . . ........

What will 01e the output qf th~ coq~ .ffiven below?


public ~lass Tester { ,.
P.~li,c st~tic void ~in~st~~nr;~,~
:i.rtt oumt - 2, nurnZ - :z~, I \l . I,. i,
h~s)
II ,
i,11 1
11 {

,
cJoi {,' I, .!-.,,, 1
•Ii
,1

num~ = num2 / mJmlj


it (numt > num2) {
break;
}
, num2--;

..--prin:tln{ 11n ,.- + num2);

IO
·,. '· nurn l = 5 qn d n,um2 = -1
+ I

'II,
i I,
11·
I 11 . .
H ),1fl4 = -1
.
111
I rl'

0 Clicking on Next qt Pre!)io~s ,r,-111 ~µbrnlt JOI.Hi answer


12: self .usessmant - Pl'Ollrarnming usinll Ja
--
.!. Killa1n:;dhY Pra11eet/1a

nee variallleS a'nd olljects are there in the cc

class student {
public int studentid;
public string name;
}
1

public class rester { . i1lli 111


public static void main(Striin
student s1 = new st utlent () ; l)ill
student s2 = new st udl:!nt( );
student s3 = s1; ..
}
}

aria1:Jl,es : 3, .objects: 3
1

- _ ,,r,11,-I
"'~

instance variabl es:~' ref erence V1ariables : 3, objects: 3

instance variables : 2, reference variables •. 2 , ob,i .-tas:


·. Jee - 2
... "
. II ,.,. ,
1

, ernoce variables,: ~-1


mstance variables .· 2' re~ • ~I
, ~~jaqfst1

11 '1'1[ 1111

. ,1
Clicking cm Ne:,ct or Previous will .,ubmit. your. ariswer.
·, thy Pran<-4'.'tha

Self As•essmcn1 - Programming using Java > 13: Self Aa1e1ament - Programming uaing Java-23

l!lt)lOY(~ ( trl ng name) {

J)Ubli C String gctNa~() {


n!tu n name;
)
)

cl · · :;y• trmt nu:Ln er c->Ct ends Eni,loyee{

tr!fflll1fl1n~er(Strfoc nlllllc) {
) .
)
)

maw(Str1ng[] angs) {
stcfflE "lneer = c:: S} stc:t1E~nc::er( •t-iaria•) ;
.our.pr!ntln(s)stcmEnginccr.gct~());

I c:1 a

as absua t class ca not ho ,econ uu tor

Of ,a
.!._ Killarnsethy Pr~=rn eetha

Se Aase I
- Programmi •
. elf Assessment -
111 11 i
mming using J

~Jiat will be the ou tJpu t of the cod 1

public class Test~r {


public static void main(string args[]) { I
string inputl = "warner";
string input2 = new string( "WARNER");
input 2. t ol owercase();
if (inputl == input2) {
system,out.println {" welcome " + inputl);
} else if (input1.equals (input2)) {
syst~m.-0ut.println( Welcome"+ input2) :; 0

} else {
system. out. println( "Welcome'');
}
}
}

-
l O..
----- r~~i'!--
Welcome . .
- """I

Welcome warner

I'

~r- 'ldat'atyp.e rn i~l(natch, ca1.1not (iQllljlare String literal with String object
If· 1111111111m1ii1 ·1 jlll1rli
:Ii
1

I I 11IHli I.I II

I Ill I

~g ~., ~ext or Pr . $ ~Ill "' . n11


II II

Iii I l i Wi I
Self Assaa.smeot - '

clJs~ t-.iovee '1{ ij '

p.Llbli€: StFing name;


publi.~ char gender; ,
1: JUblic double salar-y;
I :~ ·!!
, pu.~llc Employee(:Stri'Jlg na
rf:hi,s !,name = ,•name·: A:. 'TI
~

":this •ge~der == .~ ,
} s,, "·

ss f::.mployee h
I

0 Com r ~Qeritler is riot initi~ lized ,,

Q Alex, , Alex I ' I

11

111
; 11[1 '

Q Alex, ,Robert

~
O Clicking on Next or Previous will submit your answer.
.!. K1 llamsethy Prnneetha

Self Asses,ment - Program~ing using -1ava > 16: Se1f Assessment - Programming using Java-22

What will be t he output of the code given below?


cl ass Parent{
public final void displayMessage () {
system.out.println( "displayMessage() method of Parent invoked");
I
}

class child extends Parent{


public void displayMessage () {
System.out. printl n(''displayMes.sage.() method of Child invoked");
}
}

public class Tester{


public static void mai,n(string[]
Parent parent = new Chila ( );
parent.displayM essage () ;
}
}

Q displayMessageO method-of Parent invoked

Q displayMessageO method of Child invoked

Q Cor11pilation error as final method cannot be overriddep

Q displayMessageQ method of Parent invoked displayMes~age() method of Child invoked

8 Clicking on Next or Previous will submit your answer


}
}

plll!f!.1.c ~l,s$ i1

.!i 1:u;1b}~c st.:1t


C obj= € .I

I' I

O Clickirng on 1-!ek'I or Previbu~ wil l submit ~mur a1,1~we1·


1• l•,11

Clicking on Next or Previous will submit your an~w~1r,


l.1,,1· ~'I ,IIIE:-'l;'tl1i1

Self Asseasment - Programming using Jata > 1B: Self Assessment - Programming using Java-3

What will !>e the output of the code given below?


public class Tester- { !I Ill
11
public static void main(string[] args) {
int numt = 28;
int num2 = 36;
int nulll3 = 0;
if {~(nurn2 / num1) < e && (numl + num2) % 4 == e) {
;um1 = num1 + --num3; V i
if ({num2 / numl) > 1 I I num3 == 01 {
num1 = num1 + nlR!l3++;
Syst em.out.pr i nt ln(num1 + - -num3);
} dse {
System. out. println(num1 + --num3);
}
}
}

lo Compilation error - u · e m - . e

-- - -~ ---
0 28
0 Z6

0 25
Iii I
:1·

• 1· · 'IL. I 1 1
1

C 1ck111g on Next o r Preyious will sub""' 't .


I

li[our answer.
•u l
00 hr 19
!. i(i\lamsethy Praneetha

Self Assessment - Progra . . , Assessment ,_ P ' mming using


I I 1

Which of ,th; followi~ g


I i~
~ill
mat~ I '
I
1
~1·. jll ,ll 1[[1 ~
II1 11111
bllo.lt ~glJ.lar e~preslsio.h? !I
1
1

I 11 1
String ~f!elj; = "[A-Z)[ a-z0-9 ' %& u. , '
111
111

i i 1*";•
I

1
'A-2'.a z •
11 I

I I

'Ill 11111

1,1

I,

9 Clicking on Next or Previous will sulm1it your answer.


Self Assessment - 'Programmittgusing Ja!a. > 2St-SaJf Assessment - Arogramming 1

- -

What will be the output or t he code g.ivewoeJow?


class Cal~ulator {
public int numl = 10; f'

public float num2 -= 20;


~
public -double sum;
public double product; I
public double add ( int numl, lfl\""num2) {
.doubie sum= this . numl + this .num2;__
return sum:;
_j
}
I
public double mult:i:pl y{ini:'
. ,, {
double sum= this .add(numl, ,Jium2)J
double product = t his . sum ~ t_bis .ntiml;
returA ( int) product;
}
}

pub!it clas.s Test~r {


putl!:k st~~c 11oid main (String[] ar g~)
CalcJlator c = n~w Calculator();
System, outtprintln(c .n1ultiply(10, 5) );
}

( -.....,, '150.0
\.,_,I

O Clickmg on !-.ext or Previous w1I sulm1 t your answer


Self Assessment - Programming ~ing Java i -- ~ '
Self Assessment - Progra
=-

double sum= this . num1 -+


return sum;
},

p~q'l ic
,: 1
1
double mu!tip~~€4.n-
' double sum = th is . add ( nl!iitl,,
;;JL . . .. --
double product = this .suff! '!'-:ftfts .,numl;;
retr..trn (int) pr oduct; -
}
}
I

pue.J.ic: class Teste


,µ1(b1.l~Jf.at-ic
':r "I' . •~-- · ·- '
void.
~i/
~r,ptlat-or. c = ,ne~
5y stemr"o~t :pr inti '
}

150.0

0 300.0

() 0

:a () 0.0

9 Clicking on Next or Previous will sul)m1t your answer


pur~t:ill ~1J1ls Tester { , . 1
·:

II ~,~ti'.~1lht~f i o:id ma~~{StrirJ~ ~,~I~


1

[I :itnt~] empipy/eesSa 1ary = { 1 1350lt ;11


1 5.vt comft = 0 ; 1

,II

fnr (int salary: employeesSalary) {


switth (salary~ 2 ) {
default !
emp,l 0yeesSalary[count] , i = salary + 1;
q~se 0 :
1
ii
mployt:esSalary [ tount] =
_..!JI

,w I

~~r t++; ,I '

case 1 :
employeesSalary[count] = sa
llr~ak;
}
}
or (int i = 0 ; i < empl
s em. out._print(emplaye

'.~l-1jH:-•1i'I'!'.

- !jl' •ii
1 .. II II
6755 1201 1364 1S63

ii
I
I

'21343 6755 120·111364

~, I
for (,i~~ l~~JI~ 1

switch f(J~];;1rt
1
' iffii 1/1 1/lllilii .
'1

I
f
1· I 11

defa11111i Ii l l!IL 1
:

1
em,~oyee,~aUL · ~c
~aie 0 ~ ,
e~, 1oy~e1 l~J~ ry ~t~
1 1 Ill

1lf~f,f+t.
ca~e' :IJ :
I l•
1

11
1
1

1 '~rqrllQYee,s
;:
""ilm
Seilan;-[l:011
'l /[1
h1;J :::;
I "'1
S
1111 1I

I
brea~; . 1
I
I } 11 Iii 1 1 Ir 1· 1111

I I
i'lli ';I } . 1
1111 ifl';: 11 1

foi (int i = 0; i < employees Salar ·


~Yist'~f ! o'u t. print ( employeesSaHiry[I'
1

Ii ,t 11 U•

. 2344,.6755 1 -

6755 12011364

'3343 676,5 1201 1364


1

,.
1/ _'.j1

t come ~t t h~ end,,pf ,!witof11- gase statements


efault mus1

You might also like