Sie sind auf Seite 1von 28

Generics (SCJP 5.

0)
by www.javabeat.net
1) What will be the output of the following program? package generics; import java.util.ArrayList; import java.util.List; public class Ques01 { public static void main(String ! args" { List#String$ colors % ne& ArrayList#String$("; colors.add('()*'"; colors.add('+L,)'"; String green % '-()).'; /bject bro&n % ne& String('+ro&n'"; colors.add(green"; colors.add(bro&n"; 0 0

1. The program will compile fine but will cause a Runtime exception to be thrown. 2. The program will compile fine and also will run without any problem. 3. The program will not compile, as an attempt is made to add an object of different type to the Collection.

2) Which statements are true about the following program? package generics; import java.a&t.+utton; import java.util.ArrayList; import java.util.List; public class Ques01 { public static void main(String ! args" { List#/bject$ any/bjects % ne& ArrayList#/bject$("; +utton button/bject % ne& +utton("; String string/bject % ne& String("; any/bjects.add(ne& /bject(""; any/bjects.add(button/bject"; any/bjects.add(string/bject"; 0 0

1. The program will not compile 2. The program will compile and run smoothly

3. t is not possible to add any other type of objects other then the type ja!a.lang."bject into the list #any"bjects#. $. t is possible to add any type of objects in the list #any"bject# e!en #null#.

3). Will the following program will compile? package generics; import java.a&t.+utton; import java.a&t.2omponent; import java.a&t.3enu; import java.a&t.4e5t6ield; import java.util.ArrayList; import java.util.List; public class Ques07 { public static void main(String ! args" { List#2omponent$ all2omponents % ne& ArrayList#2omponent$("; all2omponents.add(ne& +utton(""; all2omponents.add(ne& 4e5t6ield(""; all2omponents.add(ne& /bject(""; 0 0

1. %es 2. &o

4) Which of the following statements hold true in the following program? package generics; import java.util.ArrayList; import java.util.List; public class Ques08 { public static void main(String ! args" { List#9 e5tends :nstrument$ all:nstruments % ne& ArrayList#:nstrument$("; ;; <<$= all:nstruments.add(ne& -uitar(""; all:nstruments.add(ne& >iolin(""; 0 0 inter?ace :nstrument { public void play("; 0 class -uitar implements :nstrument { public void play("{ System.out.println('@laying -uitar.'"; 0 0 class >iolin implements :nstrument { public void play("{ System.out.println('@laying >iolin.'";

1. The program will not compile. 2. The program will compile but will throw run'time exception. 3. The statement #(# manifests that objects of type nstrument as well as sub'types can be added to the list #all nstruments#. $. t is not possible to add any type of objects to the list #all nstruments#. ). The only possible element that can be added to the list #all nstruments# is #null#.

5). How will the following program behave? package generics; import java.util.ArrayList; import java.util.List; public class Ques0A { public static void main(String ! args" { List#:nstrument$ instruments % ne& ArrayList#:nstrument$("; instruments.add(ne& -uitar(""; instruments.add(ne& >iolin(""; play(instruments"; 0 static void play(List#9 e5tends :nstrument$ instruments"{ ?or (:nstrument instrument B instruments"{ instrument.play("; 0 0 0

1. The program will compile and the output the message #*laying +uitar.# followed by #*laying ,iolin#. 2. The program will throw compilation errors. 3. The program will compile and throw exception at the run'time.

) What will be the behaviour of the following program? package generics; import java.util.ArrayList; import java.util.List; public class Ques0C { public static void main(String ! args" { List#:nstrument$ instruments1 % ne& ArrayList#:nstrument$("; instruments1.add(ne& -uitar(""; instruments1.add(ne& >iolin(""; play(instruments1"; List#9 e5tends :nstrument$ instruments1 % ne& ArrayList#:nstrument$("; instruments1 % instruments1; play(instruments1";

0 static void play(List#9 e5tends :nstrument$ instruments"{ ?or (:nstrument instrument B instruments"{ instrument.play("; 0 0 0

1. The program will compile and the output the messages. 2. The program will throw compilation errors. 3. The program will compile and throw exception at the run'time.

!) Which of the following statements are true about the following program? package generics; import java.util.ArrayList; import java.util.List; public class Ques0D { public static void main(String ! args" { List#9 e5tends /bject$ objects % ne& ArrayList#/bject$("; objects.add(ne& /bject(""; objects.add(null"; 0 0

1. 2. 3. $.

t is possible to add objects only of type #ja!a.lang."bject# into the list #objects#. "nly #null# objects can be added to the list #objects#. The program will not compile The program will compile and will throw run'time exception.

") What will be the behaviour of the following program? pac-age generics. import java.util.ArrayList; import java.util.List; public class Ques0E { public static void main(String ! args" { List#String$ strings % ne& ArrayList#String$("; List#9$ any/bjects % null; ;; <<$ = any/bjects % strings; ;; <<$ F 0 0

1. The line pointed by #(# will throw a compile'time error. 2. t is not possible to assign the object #string# which is of type #/ist01tring2# to the

reference object #any"bjects#. 3. The program will compile fine without any compilation errors.

#) Which statements are true about the following program? package generics; import java.util.ArrayList; import java.util.List; public class Ques0G { public static void main(String ! args" { List#9 e5tends /bject$ objects % ne& ArrayList#/bject$("; List#:nstrument$ instruments1 % ne& ArrayList#:nstrument$("; List#9 e5tends :nstrument$ instruments1 % ne& ArrayList#:nstrument$("; List#9$ any/bjects % null; any/bjects % objects; ;; <<$ A any/bjects % instruments1; ;; <<$ + any/bjects % instruments1; ;; <<$ 2 objects % any/bjects; ;; <<$ * 0 0

1. 2. 3. $.

/ines 3 and C will raise compile'time errors. /ines 4 and 5 will raise compile'time errors. /ines 3, C and 5 will raise compile'time errors. The program will compile without any compilation errors.

1$) Will the following program compile? package generics; import java.util.ArrayList; import java.util.List; public class Ques10 { public static void main(String ! args" { List#String$ string/bjects % ne& ArrayList#String$("; /bject object % ne& String("; string/bjects.add(ne& String(""; string/bjects.add((String"object"; 0 0

1. %es 2. &o

11)What is result of compiling and running the following code? 1"ArrayList sampleList % ne& ArrayList#:nteger$("; 1"sampleList.add(80"; 7"sampleList.add(10"; 8"sampleList.add(D0"; A"?or(int num B sampleList" C"System.out.println(num H IJKLMNO IJKLMP"; "ptions 6 a7 compiler error at line 1. b7 compiler error at line 2,3,$. c7 compiler error at line ). d7 compiles fine and prints $8 28 98.

12)What is result of compiling and running the following code? QasRSet#String$ Rs % ne& QasRSet#String$("; Rs.add('scjp'"; Rs.add('e5am'"; QasRSet#/bject$ s % ne& QasRSet#/bject$("; s%Rs; System.out.println(s"; "ptions6 a7 compiler error b7 ClassCast:xception is thrown at runtime. c7 prints ;scjp, exam< d7 prints ;exam, scjp<

13)What is result of compiling and running the following code? import java.util.S; class >eRicle {0 class 2ar e5tends >eRicle {0 class +us e5tends >eRicle {0 class 4estSamp { public static void main(String ! args" { ArrayList#2ar$ a % ne& ArrayList#2ar$("; a.add(ne& 2ar(""; ArrayList b % a; ArrayList#+us$ c % (ArrayList#+us$"b; c.add(ne& +us(""; ?or (/bject obj B b" System.out.println(obj"; 0 0 "ptions6

1.compiler error 2.compiles with warning and gi!es some output 3.compiles without warning and gi!es some output $.copiles and run with no output $. List #9 super :nteger$ al % ne& ArrayList#.umber$("; ;;line 1 al.add(11"; ;;line 1 al.add(11H 17"; ;;line 7 ?or (.umber noBal" ;;line 8 { System.out.println(no"; 0

14)What will be the possible result of the above program? a7 :rror at /ine 1 b7 :rror at /ine 2 c7 :rror at /ine 3 d7 :rror at /ine $ e7 Compile and execute 1ucuessfully )7 List #:nteger$ ls % ne& ArrayList#:nteger$("; ls.add(11"; ls.add((int"11.0?"; ls.add((:nteger"11S11"; ls.add((:nteger"(11.0?""; ?or (:nteger llBls" { System.out.println(ll"; 0 ;;line 1 ;;line 1 ;;line 7 ;;line 8 ;;line A

15)What will be the possible result of the above program? a7:rror at line 3 b7:rror at line $ c7:rror at line ) d7Run Time error e7:xecutes 1ucuessfully

1 )%ind errors if an&' in the following lines of code List#:nteger$ ls1 % ne& LinkedList("; List#String$ ls1 % ne& LinkedList("; List ls7 % ne& LinkedList("; List ls8 %ls1; List lsA %ls1; List lsC%lsA%ls8; ;;line 1 ;;line 1 ;;line 7 ;;line 8 ;;line A ;;line C

a7 :rror at /ine 1 and /ine 2 b7 :rror at /ine $ and /ine ) c7 :rror at line = d7 Run time :rror e7 &o :rror 1!)Which line inserted independtl& at line 1 will allow to compile? 2ollection#9$ c % ne& ArrayList#String$("; ....;;line 1 a7c.add>new "bject>77. b7c.add>?@a!a?7. c7c.add>null7. d73ll 3 lines abo!e inserted independtely will allow to compile e73ll 3 lines abo!e inserted independtely will not allow to compile

1") Which line(s) is free from compile time error?

3ssume that *arent is a 4ase class and Child is a 5eri!ed Class List#9 super @arent$ alList % ne& ArrayList#2Rild$("; ;;line 1 List#9$ alList % ne& ArrayList#9 super 2Rild$("; ;;line 1 List#9$ alList % ne& ArrayList#9 e5tends @arent$("; ;;line 7 List#9 e5tends 2Rild$ alList % ne& ArrayList#@arent$("; ;;line 8 List#9$ alList % ne& ArrayList#@arent$("; ;;line A a7only 1 b7only $ c7only ) d72 and 3 e71 and $

1#)Which call(s) to method(addand)isp) are error free? public static #4$ void addand*isp(2ollection#4$ csT 4 t" { ?or (4 o B cs" { s.add(o"; 0 ?or (4 o B cs" { System.out.println(o"; 0 0

;;call 1 List#9 super /bject$ ls1 % ne& LinkedList#/bject$("; addand*isp(ls1Tne& String(""; ;;call 1 List#9 e5tends /bject$ ls1 % ne& LinkedList#/bject$("; addand*isp(ls1Tne& /bject(""; ;;call 7 List#/bject$ ls7 % ne& LinkedList#/bject$("; addand*isp(ls7Tne& String(""; ;;call 8 List#9 super /bject$ ls8 % ne& LinkedList#/bject$("; addand*isp(ls8Tne& /bject(""; a7only 3 b7only 1 and 3 c7only 1 and $ d7only 1,2 and 3 e7only 1,3 and $

2$)What are all t&pes in the below cannot appilcable for *enerics

17:num 271tatic class 37:xception $7nested interfaces )73nonymous inner classes =7 nner class

21)Which one(s) are true? class +ase { public List#9$ asList(" { return null; 0 ;;line 1 0 class *erived e5tends +ase { public List asList(" { return null;0 ;; line 1 0 a7 t is o!erloading b7 t is o!erriding c7Compile time error d7"nly public /ist0A2 as/ist>7 in 5eri!ed class ma-ed the method o!erriding e7Run time :rror

22)What will be the output? public class 4est { static void addand*isp(List list" { list.add(GEDCA8711"; list.add(11.78A"; ?or(/bject eBlist" { System.out.print(e.get2lass("H' '"; 0 0 public static void main(String ! args" { List#String$ ls % ne& ArrayList#String$("; ls.add('Uava'"; addand*isp(ls"; 0 0 a7*rints class ja!a.lang.1tring class ja!a.lang.long class ja!a.lang.5ouble b7*rints class ja!a.lang.1tring class ja!a.lang. nteger class ja!a.lang.5ouble c7*rints class ja!a.lang.1tring class ja!a.lang.5ouble class ja!a.lang.Bloat d7Compile time :rror e7Run time :rror

23)What are all +dvantages of using *enerics?

a7*erformance mpro!ement. b7188C free from bugs. c7Code will be more readable. d7Do!ing comments to code. e7Reduce in &o.of./ines of code. 24)what is the result compiling and running the following piece of code? import java.util.S; class 4est { public static void main(String ! args" { Set vals % ne& 4reeSet#String$("; vals.add('one'"; vals.add(1"; vals.add('t&o'"; System.out.println(vals"; 0 0 "ptions 6 a75oes not Compile

b7Compiles with warning and prints output ;one, 1, two< c7Compiles without warning and prints output ;one, 1, two< d7Compiles with warning and throws exception at runtime e7Compiles without warning and throws exception at runtime

25)Which of the following piece of code can be inserted to ma,e the following code to compile? import java.util.S; class @ick4Re@iece { public static void main(String ! args" { ;;insert tRe ?irst line Rere datas.add('delRi'"; datas.add(ne& /bject(""; ;;insert tRe second line Rere 0 0 "ptions 6 a7 /ist0"bject2 datas E new /in-ed/ist0"bject2>7. 1tring data E datas.get>87. b7 /ist0"bject2 datas E new /in-ed/ist0"bject2>7. 1tring data E >1tring7datas.get>87. c7 /ist01tring2 datas E new /in-ed/ist01tring2>7. 1tring data E >1tring7datas.get>87. d7 /ist01tring2 datas E new /in-ed/ist01tring2>7. 1tring data E datas.get>87. e7 all the abo!e 2 )What is the result of compiling and running the following code? import java.util.S; class Sample4est { public static void main(String ! args" { List samples % ne& ArrayList("; samples.add('100'"; samples.add(100"; samples.add('700'"; print*ata(samples"; 0 static void print*ata(List#String$ samples" { ?or(String sample B samples" { System.out.print(sample H ' '"; 0 0 0

"ptions 6 a7 *rints 188 288 388 b7 Compile time error c7 Compiles without warning d7 Compiles with warning e7 Runtime :xception

2!)-onsider the following code' select the valid options given below. class 6ruit {0 class Apple e5tends 6ruit {0 class /range e5tends 6ruit {0 "ptions 6 a7 /ist0A extends Bruit2 stmt E new 3rray/ist0Bruit2>7. b7 /ist0A super 3pple2 stmt E new 3rray/ist0Bruit2>7. c7 /ist0A extends Bruit2 stmt E new 3rray/ist03pple2>7. d7 /ist0A super "range2 stmt E new 3rray/ist0"range2>7. e7 3ll the abo!e f7 &one of these 2")What is the output of the following code? import java.util.S; class 2olor {0 class +lue e5tends 2olor {0 class (ed e5tends 2olor {0 class 4est2olor { public static void main(String ! args" { 1" List#2olor$ colors % ne& ArrayList#2olor$("; 1" colors.add(ne& 2olor(""; 7" colors.add(ne& +lue(""; 8" colors.add(ne& (ed(""; A" alter2olor(colors"; C" System.out.println(colors"; 0 D" 0 static void alter2olor(List clrs" { clrs.add(ne& /bject(""; 0

"ptions 6 a7 Compile time error due to lines 3 and $. b7 Compile time error due to line ). c7 Compile time error due to line 9. d7 Compiles with warning and produces some output. e7 Compiles without warning and produces some output. f7 Compiles fine and :xception is thrown at runtime.

2#)What is the result of the following code ? import java.util.S; class +ird {0 class *uck e5tends +ird {0 class Qen e5tends +ird {0

class 6uVVy4est { public static void main(String ! args" { 3ap#StringT +ird$ birds % ne& QasR3ap#StringT +ird$("; birds.put('bird'T ne& +ird(""; birds.put('Ren'T ne& Qen(""; birds.put('duck'T ne& *uck(""; 3ap bs % add+irds(birds"; ?or(String b B bs.keySet("" System.out.print(b H ' '"; 0 static 3ap add+irds(3ap brds" { brds.put('bird'T ne& /bject(""; return brds; 0

"ptions 6 a7 Compiles and prints output ?bird hen duc-?. b7 Compiles and prints output ?bird duc- hen?. c7 Compiles and prints some output order cannot be determined. d7 Run time :xception. e7 Compilation fails.

3$)What are the valid statements can be filled in the blan,' to ma,e the code to compile and run? import java.util.S; inter?ace )at{0 class Animal implements )at{0 class *og e5tends Animal {0 class 2at e5tends Animal {0 class Animal4est { public static void main(String ! args" { List#Animal$ a % ne& ArrayList#Animal$("; List#*og$ d % ne& ArrayList#*og$("; List#2at$ c % ne& ArrayList#2at$("; cReckAnimal(a"; cReckAnimal(d"; cReckAnimal(c"; 0 static void cReckAnimal( WWWWWWWWWWWWWWWW pets" { System.out.print('animals cRecked Rere'"; 0 0

"ptions 6 a7 /ist0A extends 3nimal2 b7 /ist0A super 3nimal2

c7 /ist0A extends :at2 d7 /ist0A super :at2 e7 /ist0A2 f7 3ll of the abo!e

31)What is the output of the following code? import java.util.S; class )5ample { public static void main(String ! args" { Set#String$ values % ne& 4reeSet#String$("; values.add('yet'"; values.add('get'"; values.add('bet'"; display>alues(values"; 0 static void display>alues(Set#9$ values" { values.add('&et'"; ?or(/bject v B values" System.out.print(v H ' '"; 0 0 "ptions 6 a7 Compiles and gi!es output ?yet get bet wet?. b7 Compiles and gi!es output ?bet get wet yet?. c7 Compilation fails d7 Compiles with warning and :xception thrown at runtime. e7 Compiles without warning and :xception thrown at runtime.

32)-hoose the valid wa&s to create an ob.ect for the following class. class -en4est#4 super .umber$ { 4 num; public 4 cReck.umber(4 n" { return n; 0 0 "ptions 6 a7 Compilation fails. b7 +enTest0&umber2 gt E new +enTest0&umber2>7. c7 +enTest0 nteger2 gt E new +enTest0 nteger2>7. d7 +enTest0"bject2 gt E new +enTest0"bject2>7. e7 &one of the abo!e.

33)-hoose the valid constructors for the following class.

class -enerics#4${0 "ptions 6 a7 public +enerics>7FG b7 public +enerics0T2>7FG c7 public 0T2 +enerics>T t7FG d7 public 0T2 +enerics>7FG e7 3ll the abo!e. 34)What is the effect of the following program List#String$ lsStr % ne& ArrayList#String$("; lsStr.add('1'"; lsStr.add('Uava'"; /bject obj % lsStr ; List#:nteger$ ls:nt % (List#:nteger$"obj; :nteger i1 % ls:nt.get(0"; :nteger i1 % ls:nt.get(1"; System.out.println(i1"; System.out.println(i1"; a7Compile time :rror at line $ b7Compile time :rror at line ) c7Compile time :rror at line = d7Compile time :rror at line 9 e7Run Time :rror ;;line 1 ;;line 1 ;;line 7 ;;line 8 ;;line A ;;line C ;;line D ;;line E ;;line G

35)What is the correct option for /uestions mar,? List#String$ arr ! % 9

a7new 3rray/ist>7. b7new 3rray/ist01tring2>7. c7new 3rray/ist;18<. d7null. e7new 3rray/ist0"bject2>7.

3 )What is the effect of the following program public class -en#-$ - g; -en(- g" { tRis.g %g; 0 public static void main(String ! args"

-en#String$ arr ! % ne& -en A!; arr 0! % ne& -en('Uava'"; arr 1! % ne& -en(1"; arr 1! % (-en#String$"ne& -en(1"; arr 7! % (-en#String$"ne& -en#:nteger$(1"; ?or(-en oBarr" { System.out.println(o"; 0

;;line 1 ;;line 1 ;;line 7 ;;line 8 ;;line A

a7Compile time :rror at line 1 b7Compile time :rror at line 3 c7Compile time :rror at line $ d7Compile time :rror at line ) e7Run Time :rror

3!) 0n which following t&pe(s) Wild card cannot be used

a7Bor creation "f "bjects b73s argument type of method c73s Return type of method d7 n instance of expressions e7:xception Handling

3") Which method call insert here with argument of ls will allow to compile? public static void m1(ArrayList#9$ aa" { 0 public static void m1(List#9$ aa" { 0 public static void m7(List#String$ aa" { 0 public static void m8(2ollection#9$ aa" { 0 public static void mA(/bject aa" { 0 public static void main(String ! args" { List ls % ne& ArrayList#.umber$("; ls.add(117"; ;;insert metRod call Rere 0 a7 m2,m$,m) b7 m1,m2,m3 c7 m2,m$,m) d7 m2,m3,m$,m) e7 3ll will wor-

3#)What is the effect of the following program? class 4est { public static public static public static public static public static ;;3etRodA public static { List#.umber$ List#String$ m1(ls1Tls1"; 0 0

void void void void void

m1(List#/bject$ l1TList#String$ m1(List#/bject$ l1TList#/bject$ m1(List#.umber$ l1TList#String$ m1(List#.umber$ l1TList#/bject$ m1(List#9$ l1TList#9$ l1"

l1" { 0 l1" { 0 l1" { 0 l1" { 0

;;3etRod1 ;;3etRod1 ;;3etRod7 ;;3etRod8 { 0

void main(String ! args" ls1 % ne& ArrayList#.umber$("; ls1 % ne& ArrayList#String$(";

a only method 3 b method 1 and method 3 c method 3,method $ and method ) d compile time error e 3mbiguous method call

4$)What is the effect of the above program class 3ain { public static void do@rocess(List#9$ l1" { ?or(String eBl1" { System.out.println(e.toString(" 'string Ras 'H e.lengtR("H' letters'"; 0 0 public static void main(String ! args" { List#String$ ls % ne& ArrayList#String$("; ls.add('Uava'"; ls.add('(ules'"; ls.add('Xorld'"; do@rocess(ls"; 0 0 a7 Compiles with warning b7 Run time :rror c7 Compile and executes 1uccessfully d7 Compiles with warning

e7 compile time error 41)which are all valid method(s) inserted independentl& will allow program to compile class @arent#)$ { public ) get(" { return null;0 public void set() e" { 0 0 class 2Rild e5tends @arent#.umber$ { ;;insert Rere 0

a public "bject get>7 F ... G b public /ong get>7 F ... G c public !oid set>"bject arg7 F ... G d public !oid set>/ong arg7 F ... G e public &umber get>7 F ... G 42)what will be the effect of the program? class +o5#4$ { private 4 tRe/bject; public +o5(4 arg" { tRe/bject % arg; 0 public +o5(" { 0 0 class 4est { public static void main(String ! args" { +o5#String$ b1 % ne& +o5#String$('Uava'"; /bject#String$ b1 % ne& +o5#String$('Struts'"; +o5#String$ b7 % ne& +o5(')U+'"; +o5 b8 % ne& +o5#String$('Aja5'"; /bject bA % ne& +o5#/bject$("; 0 0 a7compile time error at line ) only b7compile time error at line 2 only c7compile time error at line 2 and line ) d7compile time error at line 3 and line $ e7Run time error

;;line 1 ;;line 1 ;;line 7 ;;line 8 ;;line A

43) Which of the following statements are true?

17+eneric types can ha!e static members, including static fields, static methods and static nested types. 27we can use a concrete parametriIed type li-e any other type with fewer restriction. 37we can create an array whose component type is a concrete parametriIed type $7+eneric enum types illegal in ja!a )7we can use generic or parametriIed types in exception handling "ptions a7 1,2,3,$ b7 1,2,$ c7 1,2,3 d7 1,3,) e7 3ll are true 44) which of the following code can be independentl& inserted' to code to compile and print output 1delhi1 import java.util.S; class -en4est { 3ap#StringTString$ m % ne& QasR3ap("; { m.put('japan'T 'tokyo'"; m.put('india'T 'delRi'"; 0 public static void main(String ! args" { -en4est gt % ne& -en4est("; ;;insert code Rere String capital % gt.m.get('india'"; System.out.println(capital"; 0 0 a7 1tring capital E gt.m.get>?delhi?7. b7 1tring capital E >1tring7gt.m.get>?delhi?7. c7 both a and b d7 none of the abo!e

45) What is the result of following code? import java.util.S; class AsList { public static void main(String ! args" { String ! s % ne& String 8!; s 0! % 's?g'; s 1! % 'asd?'; s 7!%'sad?'; List#String$ l % Arrays.asList(s"; ;;line G System.out.println(l"; l.add('sdd'"; System.out.println(l.get(8""; 0 0

a7 :xception thrown at runtime b7 prints sdd c7 compilation fails at line J d7 prints sadf

4 ) what is the result of following code? class class class class *ummy{0 *um e5tends *ummy{0 +um e5tends *ummy{0 -um#)$ e5tends *ummy { ) value; void set>alue() v"{value %v;0 ) get>alue("{return value;0 public static void main(String ! args" { -um b % ne& -um("; b.set>alue(ne& /bject(""; -um#*um$ d % b; -um#+um$ e % b; *um R % (*um" d.get>alue("; 0

a7 Compilation fails b7 &o output produced c7 Compiles with warning d7 :xception thrown at runtime

4!) what is the output of compiling an running the following code? import java.util.S; class 2Reck4est { public static void main(String ! args" { QasRtable Rt % ne& QasRtable#StringT String$("; Rt.put('cRec'T 'cReck'"; Rt.put(1000T 'cReck'"; ;; line 1 Rt.put('cReck'T 10.01"; ;; line 1 System.out.print(Rt.get('cRec'" H ' '"; System.out.print(Rt.get(1000" H ' '"; ;;line 7 System.out.print(Rt.get('cReck'""; ;;line 8 0 0 a7 Compilation fails due to error at line 1,2,3,$ b7 Compilation fails due to error at line 3,$ c7 Compiles fine and exception is thrown at runtime. d7 Compiles fine and prints ?chec- chec- 28.81? e7 Compilation fails due to error at line 1,2

4") which of the following are2is true?

a7 +enerics gi!es us only compile time safety. b7 +enerics gi!es us both compile time a runtime safety. c7 +enerics gi!e us only runtime safety. d7 +enerics has bac-ward compatibility

4#) What is the output of compiling an running the following code? import java.util.S; public class QasR4est{ int RasR2ode; public QasR4est(int RasR2ode"{ tRis.RasR2ode % RasR2ode; 0 public boolean eYuals(/bject otRer"{ i?(otRer instanceo? QasR4est"{ return ((QasR4est"otRer".RasR2ode %% tRis.RasR2ode; 0 return ?alse; 0 public static void main(String ! args" { QasR4est ob1 % ne& QasR4est(7"; QasR4est ob1 % ne& QasR4est(1"; QasR4est ob7 % ne& QasR4est(1"; 3ap mp % ne& QasR3ap("; mp.put(ob1T'one'"; mp.put(ob1T't&o'"; mp.put(ob7T'tRree'"; ob1 % ne& QasR4est(1"; System.out.println(mp.get(ob1""; 0 0 a7 one b7 two c7 three d7 null e7 Compilation fails

5$) What is the output of compiling and running the following code? import java.util.S; class .on-en { public static void main(String

! args" {

0 public static void call.on-en(List li" { li.add(117"; li.add('117'"; 0 0 a7 Compiles with warning b7 Compiles fine c7 :xception thrown at runtime d7 *rints 123 e7 &one of the abo!e

List#String$ l% ne& ArrayList#String$("; call.on-en(l"; System.out.println(l.get(1"";

51) Which are following can be inserted in the following code will run properl&. import java.util.S; class Animal {0 class *og e5tends Animal{0 class 4ypeSa?e{ public static void main(String ! args" { List#Animal$ a % ne& ArrayList#Animal$("; ;; yes List#9 e5tends Animal$ aL % ne& ArrayList#*og$("; ;; yes List#*og$ aLi% ne& ArrayList#*og$("; List#9 super *og$ aslis% ne& ArrayList#Animal$("; 0 0 a7 /ist03nimal2 a E new 3rray/ist03nimal2>7. b7 /ist0A extends 3nimal2 d E new 3rray/ist05og2>7. c7 /ist05og2 d E new 3rray/ist05og2>7. d7 /ist0A super 5og2 a E new 3rray/ist03nimal2>7. e7 all of the abo!e

52) what is the output of compiling an running the following code? import java.util.S; public class /lympics e5tends QasRSet#-ame$ { public static void main(String ! args" { /lympics o % ne& /lympics("; o.add(ne& -ame('S&imming'""; o.add(ne& -ame('Long Uump'""; o.add(ne& -ame('SRooting'""; o.add(ne& -ame('S&imming'""; System.out.println('4otalB ' H o.siVe(""; 0 public boolean add(-ame o" { System.out.println('Adding'";

return super.add(ne& -ame('Qockey'""; 0 0 class -ame{ String name; public -ame(String name" { tRis.name % name; 0 public boolean eYuals(/bject otRer"{ return true; 0 public int RasR2ode("{ return 0; 0 0 a7 "utput contains ?3dding ...? b7 *rints K c7 *rints $ d7 *rints 1 e7 Compilation fails

53) What is the output of compiling an running the following code? class 0 class 0 class 0 class 6lo&er{ (ose e5tends 6lo&er{ Lilly e5tends 6lo&er{ )5tens4est { public static void main(String ! args" { List#6lo&er$ list % ne& ArrayList#6lo&er$("; list.add(ne& 6lo&er(""; list.add(ne& (ose(""; list.add(ne& Lilly(""; 0

0 a7 Compiles and runs fine b7 Compiles with warning and exception will cause at runtime. c7 Compilation fails d7 Compiles fine an exception thrown at runtime. 54) What will be the output of the following program? package scjpA0.generics; import java.util.ArrayList; import java.util.List; public class Ques01 { public static void main(String ! args" { .umber ! numbers % {10T 100; List#.umber$ numberList % convertArray4oList(numbers";

System.out.println(numberList"; 0 static #=$ List#=$ convertArray4oList(= ! array"{ List#=$ list % ne& ArrayList#=$("; ?or (= element B array"{ list.add(element"; 0 return list; 0

a. The program won#t compile as it is not possible to parameteriIe the list object with type (. b. The program will compile but will throw a run'time exception. c. The program will output the contents of the list object >18, 287.

55) Which of the following statements are true about the following program? package scjpA0.generics; import java.util.ArrayList; import java.util.List; public class Ques01 { public static void main(String ! args" { List#9 super 2$ list1 % ne& ArrayList#2$("; list1.add(ne& 2(""; list1.add(ne& *(""; list1.add(ne& )(""; List#9 super 2$ list1 % ne& ArrayList#)$("; list1.add(ne& )(""; list1.add(ne& 2(""; List#9 super 2$ list7 % ne& ArrayList#*$("; 0 0 inter?ace A {0 inter?ace + {0 class 2 implements AT +{0 class * implements + {0 class ) e5tends 2 {0 a. b. c. d. e. The program will compile smoothly. The creation of list object #list1# will cause compilation error. The creation of list object #list3# will cause compilation error. t is not possible to add elements of type : in list #list1#. t is not possible to add elements of type C in list #list2#.

5 ) Which of the following statements are true in the following program? package scjpA0.generics; import java.util.ArrayList; import java.util.2ollection; import java.util.List; public class Ques07 { public static void main(String ! args" {

0 0 a. b. c. d. e.

List#9 e5tends /bject$ object1 % ne& ArrayList#String$("; object1.add(ne& String(""; object1.add(null"; List#9 super .umber$ object1 % ne& ArrayList#/bject$("; object1.add(ne& /bject(""; object1.add(ne& :nteger(1""; object1.add(null";

The program will compile and run smoothly. t is not possible to add #null# objects in list #object1#. t is not possible to add nteger objects in list #object2#. t is possible to add 1tring objects in list #object1#. t is possible to add #null# objects in list #object2#.

5!) What will be the output of the following program? package scjpA0.generics; import java.util.LinkedQasR3ap; import java.util.3ap; public class Ques08 { public static void main(String ! args" { 3ap#StringT String$ map1 % ne& LinkedQasR3ap#StringT String$("; map1.put('1'T '1'"; map1.put('1'T '1'"; System.out.println(mapSiVe)Yuals(map1T null""; 0 static boolean mapSiVe)Yuals(/bject o1T /bject o1" { i? ((o1 instanceo? 3ap#StringT String$" ZZ (o1 instanceo? 3ap#StringT String$""{ 3ap#StringT String$ m1 % (3ap#StringT String$"o1; 3ap#StringT String$ m1 % (3ap#StringT String$"o1; return ne& :nteger(m1.siVe("".eYuals(ne& :nteger(m1.siVe("""; 0 return ?alse; 0 0 a. The program will throw &ull*ointer:xception at run'time. b. The program will output false. c. The program won#t compile because #instance of# expressions can#t be applicable for generic types. d. The program won#t compile because type'casting operations can#t be applicable for generic types.

5") *iven a situation that a class - has to be parameteri3ed with the constraint that the parameteri3ed t&pe should implement all the behaviors 41' 42 and 43. 0n

this case which of the following defines a valid s&nta5 for such a class declaration? a. inter?ace inter?ace inter?ace class 2#4 { 0 d. inter?ace inter?ace inter?ace class 2#4 { 0 c. inter?ace inter?ace inter?ace class 2#4 { 0 d. inter?ace inter?ace inter?ace class 2#4 { 0

+1{0 +1{0 +7{0 e5tends +1 Z +1 Z +7$

+1{0 +1{0 +7{0 e5tends +1T +1T +7$

+1{0 +1{0 +7{0 e5tends +1 ZZ +1 ZZ +7$

+1{0 +1{0 +7{0 e5tends +1$T #4 e5tends +1$T #4 e5tends +1$

5#) What will be the output of the following program? package scjpA0.generics; public class Ques0A { public static void main(String ! args" { :de#/sT Language$ netbeans % ne& :de#/sT Language$(/s.Xindo&sT Language.Uava"; :de#/sT Language$ eclipse % ne& :de#/sT Language$(/s.,ni5T Language.Uava"; System.out.println(netbeans.getLanguage(" %% eclipse.getLanguage(""; 0 0 enum /s{ Xindo&sT ,ni5 0 enum Language{ UavaT 2 0

class :de#/sT Language$ { private /s operatingSystem; private Language language; public :de(/s osT Language lang"{ tRis.operatingSystem % os; tRis.language % lang; 0 public /s get/peratingSystem("{ return operatingSystem; 0 public Language getLanguage("{ return language; 0 0 a. The program won#t compile as it is not possible to define enum types as parametric types in a generic class. b. The program will output #true#. c. The program will output #false#.

$) What will be the output of the following program? package scjpA0.generics; import java.util.ArrayList; import java.util.List; public class Ques0C { public static void main(String ! args" { List#:nteger$ list % ne& ArrayList#:nteger$("; System.out.println(convertList4oArray(list" 0!"; 0 static #4$ 4 ! convertList4oArray(List#4$ list"{ 4 retArray ! % ne& 4 list.siVe("!; ?or (int i%0; i#list.siVe("; iHH"{ retArray i! % list.get(i"; 0 return retArray; 0 0 a. The program will raise # ndex"ut"f4ounds:xception# at run'time. b. The program will output 8. c. The program will fail to run because of compilation errors.

6-78 group 9 http6LLtech.groups.yahoo.comLgroupL@a!a4eatM1C@*L 6-78 %orums 9 http://forums.javabeat.net/index.php?board=2.0 Web URL : http6LLwww.ja!abeat.netLja!abeatLscjp)Lmoc-sL 6end &our feedbac, to 9 adminNja!abeat.net

Das könnte Ihnen auch gefallen