You are on page 1of 52

SCJP 1.5 (Sun Certified Java Programmer (SCJP) Mock Questions) Final Test Questions Questions: 68 www.techfaq360.

com

Buy 800 Questions with details explanations

Question - 1
What is the output for the below code ? 1. public class A { 2. int add(int i, int j){ 3. return i+j !. " #." $.public class % e&tends A{ '. public static (oid )ain(*trin+ ar+(,-){ .. short s / 0 0. *1ste).out.println(add(s,$)) 12. " 11."

Options are A.Compile fail due to error on line no 2 B.Compile fail due to error on line no 9 C.Compile fail due to error on line no 8 D.1 Answer ! B is the "orre"t answer. Cannot ma#e a stati" referen"e to the non-stati" method add$int% int& from the type A. 'he short s is auto(oxed "orre"tly% (ut the add$& method "annot (e in)o#ed from a stati" method (e"ause add$& method is not stati".

Question - 2
What is the output for the below code ? public class A { int 3 boolean istrue static int p public (oid print4alue() { *1ste).out.print(3) *1ste).out.print(istrue) *1ste).out.print(p) " " public class 5est{ public static (oid )ain(*trin+ ar+(,-){ A a / new A() a.print4alue() " "

Options are A.0 false 0 B.0 true 0 C.0 0 0 D.Compile error - stati" )aria(le must (e initiali*ed (efore use. Answer ! A is the "orre"t answer. +lo(al and stati" )aria(le need not (e initiali*ed (efore use. Default )alue of ,lo(al and stati" int )aria(le is *ero. Default )alue of (oolean )aria(le is false. -emem(er lo"al )aria(le must (e initiali*ed (efore use.

Question - .

What is the output for the below code ? public class 5est{ int 67 int 7' int do public static (oid )ain(*trin+ ar+(,-){ 5est test / new 5est() test.7'/' test.do/0 *1ste).out.println(test.7') *1ste).out.println(test.do) *1ste).out.println(test.67) " "

Options are A./ 9 0 B./ 0 0 C.Compile error - 0/ is not )alid identifier. D.Compile error - do is not )alid identifier. Answer ! D is the "orre"t answer. 0/ is )alid identifier. 1dentifiers must start with a letter% a "urren"y "hara"ter $0&% or unders"ore $ 2 &. 1dentifiers "annot start with a num(er. 3ou "an4t use a 5a)a #eyword as an identifier. do is a 5a)a #eyword.

Question - 6
What is the output for the below code ? pac3a+e co) class Ani)al { public (oid print8a)e(){ *1ste).out.println(9Ani)al9) " "

pac3a+e e&a) i)port co).Ani)al public class :at e&tends Ani)al { public (oid print8a)e(){ *1ste).out.println(9:at9) " "

pac3a+e e&a) i)port co).Ani)al public class 5est { public static (oid )ain(*trin+,- ar+s){ Ani)al a / new :at() a.print8a)e() " "

Options are A.Animal B.Cat C.Animal Cat D.Compile 7rror Answer ! D is the "orre"t answer. Cat "lass won4t "ompile (e"ause its super"lass% Animal% has default a""ess and is in a different pa"#a,e. 8nly pu(li" super"lass "an (e a""essi(le for different pa"#a,e.

Question What is the output for the below code ? public class A { int i / 12 public (oid print4alue() {

" "

*1ste).out.println(94alue;A9)

public class % e&tends A{ int i / 12 public (oid print4alue() { *1ste).out.print(94alue;%9) " " public class 5est{ public static (oid )ain(*trin+ ar+(,-){ A a / new %() a.print4alue() *1ste).out.println(a.i) " "

Options are A.9alue-B 11 B.9alue-B 10 C.9alue-A 10 D.9alue-A 11 Answer ! B is the "orre"t answer. 1f you "reate o(:e"t of su("lass with referen"e of super "lass li#e $ A a ; new B$&<& then su("lass method and super "lass )aria(le will (e exe"uted.

Question - =
What is the output for the below code ? public enu) 5est { %<=A>?A*5(', 32), @A8:B(12, 1#), CD88=<(10, !#) pri(ate int hh pri(ate int ))

5est(int hh, int ))) { assert (hh E/ 2 FF hh G/ 23) H 9Dlle+al hour.9 assert ()) E/ 2 FF )) G/ #0) H 9Dlle+al )ins.9 this.hh / hh this.)) / )) " public int +etBour() { return hh " public int +etIins() { return )) " public static (oid )ain(*trin+ ar+s,-){ 5est t / new %<=A>?A*5 *1ste).out.println(t.+etBour() +9H9+t.+etIins()) "

"

Options are A./!.0 B.Compile 7rror - an enum "annot (e instantiated usin, the new operator. C.12!.0 D.19!6 Answer ! B is the "orre"t answer. As an enum "annot (e instantiated usin, the new operator% the "onstru"tors "annot (e "alled expli"itly. 3ou ha)e to do li#e 'est t ; B-7A>?A@'<

Question - /
What is the output for the below code ? public class A { static{*1ste).out.println(9static9) " { *1ste).out.println(9bloc39) " public A(){ *1ste).out.println(9A9) " public static (oid )ain(*trin+,- ar+s){ A a / new A()

"

Options are A.A (lo"# stati" B.stati" (lo"# A C.stati" A D.A Answer ! B is the "orre"t answer. ?irst exe"ute stati" (lo"#% then statement (lo"# then "onstru"tor.

Question - 8
What is the output for the below code ? 1. 2. 3. !. #. $. '. .. public class 5est { public static (oid )ain(*trin+,- ar+s){ int i / 212 int j / 2' *1ste).out.println(i) *1ste).out.println(j) " "

Options are A.8 / B.10 / C.Compilation fails with an error at line . D.Compilation fails with an error at line Answer ! A is the "orre"t answer. By pla"in, a *ero in front of the num(er is an inte,er in o"tal form. 010 is in o"tal form so its )alue is 8.

Question - 9

What is the output for the below code ? 1. public class 5est { 2. public static (oid )ain(*trin+,- ar+s){ 3. b1te b / $ !. b+/. #. *1ste).out.println(b) $. b / b+' '. *1ste).out.println(b) .. " 0. "

Options are A.16 21 B.16 1. C.Compilation fails with an error at line = D.Compilation fails with an error at line 6 Answer ! C is the "orre"t answer. int or smaller expressions always resultin, in an int. @o "ompiler "omplain a(out 'ype mismat"h! "annot "on)ert from int to (yte for ( ; (A/< But ( A; /< BB Co pro(lem (e"ause A;% -;% D;% and B; will all put in an impli"it "ast. ( A; / is same as ( ; $(yte&(A/ so "ompiler not "omplain.

Question - 10
What is the output for the below code ? public class 5est { public static (oid )ain(*trin+,- ar+s){ *trin+ (alue / 9abc9 chan+e4alue((alue) *1ste).out.println((alue) " public static (oid chan+e4alue(*trin+ a){ a / 9&1J9 " "

Options are

A.a(" B.xy* C.Compilation fails D.Compilation "lean (ut no output Answer ! A is the "orre"t answer. 5a)a pass referen"e as )alue. passin, the o(:e"t referen"e% and not the a"tual o(:e"t itself. @imply reassi,nin, to the parameter used to pass the )alue into the method will do nothin,% (e"ause the parameter is essentially a lo"al )aria(le.

Question - 11
What is the output for the below code ? public class 5est { public static (oid print4alue(int i, int j, int 3){ *1ste).out.println(9int9) " public static (oid print4alue(b1te...b){ *1ste).out.println(9lon+9) " public static (oid )ain(*trin+... ar+s) { b1te b / 0 print4alue(b,b,b) " "

Options are A.lon, B.int C.Compilation fails D.Compilation "lean (ut throws -untime7x"eption Answer ! B is the "orre"t answer. Erimiti)e widenin, uses the smallest method ar,ument possi(le. $?or 7xample if you pass short )alue to a method (ut method with short ar,ument is not a)aila(le then "ompiler "hoose method with int ar,ument&. But in this "ase "ompiler will prefer the older style (efore it "hooses the newer style% to #eep existin, "ode more ro(ust. )ar-ar,s method is looser than widen.

Question - 12 ?ill in the ,ap!


public class 5est { public static (oid main(*trin+,- args) { *trin+,- words / new *trin+,- {"aaa", "bbb", "ccc", "aaa"" IapG*trin+, Dnte+erE m / new 5reeIapG*trin+, Dnte+erE() for (*trin+ word H words) { freq / m.get(word) m.put(word, freq // null ? 1 H freq + 1) " *1ste).out.println(m) "

Fse the followin, fra,ments *ero or many times @trin, 1nte,er Boolean ?loat Question - 1.
Kou ha(e a ja(a file na)e 5est.ja(a inside src folder of ja(aproject director1. Kou ha(e also classes folder inside ja(aproject director1. 1ou ha(e issued below co))and fro) co))and pro)pt. cd ja(aproject Which of the below co))and puts 5est.class file inside classes folder ?

Options are A.:a)a" -d "lasses sr"B'est.:a)a B.:a)a" 'est.:a)a C.:a)a" sr"B'est.:a)a D.:a)a" "lasses sr"B'est.:a)a

Answer ! A is the "orre"t answer. 'he -d option lets you tell the "ompiler in whi"h dire"tory to put the ."lass file it ,enerates $d for destination&

Question - 16
Kou ha(e two class files na)e 5est.class and 5est1.class inside ja(aproject director1. 5est.ja(a source code is H public class 5est{ public static (oid )ain (*trin+,- ar+s){ *1ste).out.println(9Bello 5est9) "

"

5est1.ja(a source code is H public class 5est1{ public static (oid )ain (*trin+,- ar+s){ *1ste).out.println(9Bello 5est19) " " 1ou ha(e issued below co))ands fro) co))and pro)pt. cd ja(aproject ja(a 5est 5est1 What is the output ?

Options are A.Gello 'est B.Gello 'est Gello 'est1 C.Gello 'est1 D.-un fails - "lass not found Answer ! A is the "orre"t answer.

3ou must spe"ify exa"tly one "lass file to exe"ute. 1f more than one then first one will (e exe"uted.

Question - 1
Kou ha(e a ja(a file na)e 5est.ja(a . 5est.ja(a needs access to a class contained in app.jar in 9e&a)9 director1. Which of the follwin+ co))and set classpath to co)pile clean?

Options are A.:a)a" -"lasspath examBapp.:ar 'est.:a)a B.:a)a" -"lasspath app.:ar 'est.:a)a C.:a)a" -"lasspath exam 'est.:a)a D.Cone of the a(o)e Answer ! A is the "orre"t answer. :a)a" -"lasspath examBapp.:ar 'est.:a)a is the "orre"t "ommand to set examBapp.:ar in "lasspath.

Question - 1=
What will be the result of co)pilin+ the followin+ codeH public class *uper:lass { public int doDt(*trin+ str, Dnte+er... data)throws =&ception{ *trin+ si+nature / 9(*trin+, Dnte+er,-)9 *1ste).out.println(str + 9 9 + si+nature) return 1 " " public class *ub:lass e&tends *uper:lass{ public int doDt(*trin+ str, Dnte+er... data) { *trin+ si+nature / 9(*trin+, Dnte+er,-)9

si+nature) "

*1ste).out.println(9L(erriddenH 9 + str + 9 9 + return 2

public static (oid )ain(*trin+... ar+s) { *uper:lass sb / new *ub:lass() sb.doDt(9hello9, 3) " "

Options are A.8)erridden! hello $@trin,% 1nte,erHI& B.hello $@trin,% 1nte,erHI& C.Complilation fails D.Cone of the a(o)e Answer ! C is the "orre"t answer. Fnhandled ex"eption type 7x"eption.

Question - 1/
What happens when the followin+ code is co)piled and run. *elect the one correct answer. for(int i / 2 i G ! i++) for(int j / 2 j G ! j++) if(i G j) assert iM/j H i

Options are A.'he "lass "ompiles and runs% (ut does not print anythin,. B.'he num(er 2 ,ets printed with Assertion7rror C."ompile error D.'he num(er . ,ets printed with Assertion7rror Answer ! A is the "orre"t answer.

Jhen if "ondition returns true% the assert statement also returns true. Gen"e Assertion7rror does not ,et ,enerated. .

Question - 18
What happens when the followin+ code is co)piled and run. *elect the one correct answer. for(int i / 2 i G ! i++) for(int j / 2 j G ! j++) assert iM/j H i

Options are A.'he "lass "ompiles and runs% (ut does not print anythin,. B.'he num(er 2 ,ets printed with Assertion7rror C."ompile error D.'he num(er . ,ets printed with Assertion7rror Answer ! B is the "orre"t answer. Jhen i and : are (oth 2% assert "ondition is false% and Assertion7rror ,ets ,enerated. .

Question - 19
tr1{ ?ile f / new ?ile(9a.t&t9) "catch(=&ception e){ "catch(DL=&ception io){ " Ds this code create new file na)e a.t&t ?

Options are A.'rue B.?alse C.Compilation 7rror D.Cone Answer ! C is the "orre"t answer.

187x"eption is unrea"ha(le to "ompiler (e"ause all ex"eption is ,oin, to "at"h (y 7x"eption (lo"#.

Question - 20
class A { A(*trin+ s) { " A() { " " 1. class % e&tends A { 2. %() { " 3. %(*trin+ s) { !. super(s) #. " $. (oid test() { '. NN insert code here .. " 0. " Which of the below code can be insert at line ' to )a3e clean co)pilation ?

Options are A.A a ; new B$&< B.A a ; new B$ &< C.A a ; new A$@trin, s&< D.All of the a(o)e Answer ! A is the "orre"t answer. A a ; new B$&< is "orre"t (e"ause anonymous inner "lasses are no different from any other "lass when it "omes to polymorphism.

Question - 21
What is the output for the below code ? interface A { public (oid print4alue()

" 1. public class 5est{ 2. public static (oid )ain (*trin+,- ar+s){ 3. A a1 / new A() { !. public (oid print4alue(){ #. *1ste).out.println(9A9) $. " '. " .. a1.print4alue() 0. " 12. "

Options are A.Compilation fails due to an error on line . B.A C.Compilation fails due to an error on line 8 D.null Answer ! B is the "orre"t answer. 'he A a1 referen"e )aria(le refers not to an instan"e of interfa"e A% (ut to an instan"e of an anonymous $unnamed& "lass. @o no "ompilation error.

Question - 22

class A { class A1 { (oid print4alue(){ *1ste).out.println(9A.A19) " " " 1. public class 5est{ 2. public static (oid )ain (*trin+,- ar+s){ 3. A a / new A() !. NN D8*=<5 :LC= #. a1.print4alue() $. " '. "

Which of the below code inserted at line !, co)pile and produce the output 9A.A19?

Options are A.A.A1 a1 ; new A.A1$&< B.A.A1 a1 ; a.new A1$&< C.A a1 ; new A.A1$&< D.All of the a(o)e Answer ! B is the "orre"t answer. "orre"t inner "lass instantiation syntax is A a ; new A$&< A.A1 a1 ; a.new A1$&<

Question - 2.
What is the output for the below code ? public class A { public (oid print4alue(){ *1ste).out.println(94alue;A9) "

" public class % e&tends A{ public (oid print8a)e%(){ *1ste).out.println(98a)e;%9) " " public class : e&tends A{ public (oid print8a)e:(){ *1ste).out.println(98a)e;:9) " " 1. public class 5est{ 2. public static (oid )ain (*trin+,- ar+s) { 3. % b / new %() !. : c / new :() #. newOrint(b) $. newOrint(c) '. " .. public static (oid newOrint(A a){ 0. a.print4alue() 12. "

11. "

Options are A.9alue-A Came-B B.9alue-A 9alue-A C.9alue-A Came-C D.Came-B Came-C Answer ! B is the "orre"t answer. Class B extended Class A therefore all methods of Class A will (e a)aila(le to "lass B ex"ept pri)ate methods. Class C extended Class A therefore all methods of Class A will (e a)aila(le to "lass C ex"ept pri)ate methods.

Question - 26
What is the output for the below code ? public class A { public (oid print8a)e(){ *1ste).out.println(94alue;A9) "

"

public class % e&tends A{ public (oid print8a)e(){ *1ste).out.println(98a)e;%9) " " public class : e&tends A{ public (oid print8a)e(){ *1ste).out.println(98a)e;:9) " " 1. public class 5est{ 2. public static (oid )ain (*trin+,- ar+s) { 3. % b / new %() !. : c / new :() #. b / c $. newOrint(b) '. "

.. 0. 12. 11. "

public static (oid newOrint(A a){ a.print8a)e() "

Options are A.Came-B B.Came-C C.Compilation fails due to an error on lines D.Compilation fails due to an error on lines 9 Answer ! C is the "orre"t answer. -eferen"e )aria(le "an refer to any o(:e"t of the same type as the de"lared referen"e 8"an refer to any su(type of the de"lared type. -eferen"e )aria(le K(K is type of "lass B and referen"e )aria(le K"K is a type of "lass C. @o Compilation fails.

Question - 2
What is the output for the below code ? public class : { " public class C e&tends :{ " public class A { public : +etL%P(){ *1ste).out.println(9class A ; return :9) return new :() " " public class % e&tends A{ public C +etL%P(){ *1ste).out.println(9class % ; return C9) return new C() "

" public class 5est { public static (oid )ain(*trin+... ar+s) { A a / new %() a.+etL%P() " "

Options are A."lass A - return C B."lass B - return D C.Compilation fails D.Compilation su""eed (ut no output Answer ! B is the "orre"t answer. ?rom 52@7 .0 onwards. return type in the o)erridin, method "an (e same or su(type of the de"lared return type of the o)erridden $super"lass& method.

Question - 2=
What is the output for the below code ? public class A { pri(ate (oid print8a)e(){ *1ste).out.println(94alue;A9) "

"

public class % e&tends A{ public (oid print8a)e(){ *1ste).out.println(98a)e;%9) " " public class 5est{ public static (oid )ain (*trin+,- ar+s) { % b / new %() b.print8a)e() "

"

Options are A.9alue-A B.Came-B C.9alue-A Came-B D.Compilation fails - pri)ate methods "an4t (e o)erride Answer ! B is the "orre"t answer. 3ou "an not o)erride pri)ate method % pri)ate method is not a)aila(e in su("lass . 1n this "ase printCame$& method a "lass A is not o)erridin, (y printCame$& method of "lass B. printCame$& method of "lass B different method. @o you "an "all printCame$& method of "lass B.

Question - 2/
What is the output for the below code ? i)port ja(a.io.?ile8ot?ound=&ception public class A { public (oid print8a)e() throws ?ile8ot?ound=&ception { *1ste).out.println(94alue;A9) "

" public class % e&tends A{ public (oid print8a)e() throws 8ullOointer=&ception{ *1ste).out.println(98a)e;%9) " " public class 5est{ public static (oid )ain (*trin+,- ar+s) throws =&ception{ A a / new %() a.print8a)e() " "

Options are A.9alue-A B.Compilation fails-7x"eption CullEointer7x"eption is not "ompati(le with throws "lause in A.printCame$& C.Came-B D.Compilation su""eed (ut no output Answer ! C is the "orre"t answer. 'he o)erridin, method "an throw any un"he"#ed $runtime& ex"eption% re,ardless of ex"eption thrown (y o)erridden method. CullEointer7x"eption is -untime7x"eption so "ompiler not "omplain.

Question - 28
What is the output for the below code ? public class A { public A(){ *1ste).out.println(9A9) " public A(int i){ this() *1ste).out.println(i) "

" public class % e&tends A{ public % (){ *1ste).out.println(9%9) " public % (int i){ this() *1ste).out.println(i+3) " " public class 5est{ public static (oid )ain (*trin+,- ar+s){ new %(#) "

"

Options are A.A B 8 B.A B 8 C.A B D.B 8 A Answer ! A is the "orre"t answer. Constru"tor of "lass B "all their super"lass "onstru"tor of "lass A $pu(li" A$&& % whi"h exe"ute first% and that "onstru"tors "an (e o)erloaded. 'hen "ome to "onstru"tor of "lass B $pu(li" B $int i&&.

Question - 29
What is the output for the below code ? 1. public interface DnfA { 2. protected *trin+ +et8a)e() 3. " public class 5est i)ple)ents DnfA{ public *trin+ +et8a)e(){ return 9test;na)e9 " public static (oid )ain (*trin+,- ar+s){ 5est t / new 5est() *1ste).out.println(t.+et8a)e()) " "

Options are A.test-name B.Compilation fails due to an error on lines 2 C.Compilation fails due to an error on lines 1 D.Compilation su""eed (ut -untime 7x"eption

Answer ! B is the "orre"t answer. 1lle,al modifier for the interfa"e method 1nfA.,etCame$&< only pu(li" and a(stra"t are permitted

Question - .0
What is the output for the below code ? public class C { int i int j public C(int i,int j){ this.i/i this.j/j " public (oid print8a)e() { *1ste).out.println(98a)e;C9) " " 1. public class 5est{ 2. public static (oid )ain (*trin+,- ar+s){ 3. C d / new C() !. d.print8a)e() #. $. " '. "

Options are A.Came-D B.Compilation fails due to an error on lines . C.Compilation fails due to an error on lines 6 D.Compilation su""eed (ut no output Answer ! B is the "orre"t answer. @in"e there is already a "onstru"tor in this "lass $pu(li" D$int i%int :&&% the "ompiler won4t supply a default "onstru"tor. 1f you want a no-ar,ument "onstru"tor to o)erload the withar,uments )ersion you already ha)e% you ha)e to define it (y yourself. 'he "onstru"tor D$& is undefined in "lass D. 1f you define expli"it "onstru"tor then default "onstru"tor will

not (e a)aila(le. 3ou ha)e to define expli"itly li#e pu(li" D$&L M then the a(o)e "ode will wor#. 1f no "onstru"tor into your "lass % a default "onstru"tor will (e automati"ally ,enerated (y the "ompiler.

Question - .1

public

"

class A { public (oid test1(){ *1ste).out.println(9test19) "

public class % e&tends A{ public (oid test2(){ *1ste).out.println(9test29) " " 1. public class 5est{ 2. public static (oid )ain (*trin+,- ar+s){ 3. A a / new A() !. A b / new %() #. % b1 / new %() $. NN insert code here '. " .. " Which of the followin+ , inserted at line $, will co)pile and print test2?

Options are A.$$B&(&.test2$&< B.$B&(.test2$&< C.(.test2$&< D.a.test2$&< Answer ! A is the "orre"t answer. $$B&(&.test2$&< is proper "ast. test2$& method is in "lass B so need to "ast ( then only test2$& is a""essi(le. $B&(.test2$&< is not proper "ast without the se"ond set of parentheses% the "ompiler thin#s it is an in"omplete statement.

Question - .2
What is the output for the below code ? 1. public class 5est { 2. public static (oid )ain(*trin+... ar+s) { 3. int & /# !. & Q/ 3 + ' #. *1ste).out.println(&) $. " '. "

Options are A.22 B. 0 C.10 D.Compilation fails with an error at line 6 Answer ! B is the "orre"t answer. x D; . A /< is same as x ; x D $. A/& ; D $10& ; 0 (e"ause expression on the ri,ht is always pla"ed inside parentheses.

Question - ..
What is the output for the below code ? 1. public class 5est { 2. enu) Ionth { PA8, ?=%, IA< " 3. public static (oid )ain(*trin+... ar+s) { !. Ionth )1 / Ionth.PA8 #. Ionth )2 / Ionth.PA8 $. Ionth )3 / Ionth.?=% '. *1ste).out.println()1 // )2) .. *1ste).out.println()1.eRuals()2)) 0. *1ste).out.println()1 // )3) 12. *1ste).out.println()1.eRuals()3)) 11. " 12. "

Options are

A.true true true false B.true true false false C.false false true true D.Compilation fails with an error at line 10 Answer ! B is the "orre"t answer. m1 and m2 refer to the same enum "onstant @o m1 ;; m2 returns true BF' m1 and m. refer to different enum "onstant @o m1 ;; m. returns false. m1.eNuals$m2& returns true (e"ause enum "onstant )alue is same $5AC and 5AC&. m1.eNuals$m.& return false (e"ause enum "onstants )alues are different $5AC and ?7B&.

Question - .6
What is the output for the below code ? 1. public class 5est { 2. public static (oid )ain(*trin+... ar+s) { 3. int ,- inde& / new int,#!. *1ste).out.println(inde& instanceof Lbject) #. " $. "

Options are A.true B.false C.Compilation fails with an error at line . D.Compilation fails with an error at line 6 Answer ! A is the "orre"t answer. An array is always an instan"e of 8(:e"t

Question - .
What is the output for the below code ? public class 5est {

public static (oid )ain(*trin+... ar+s) { int a /# , b/$, c /' *1ste).out.println(94alue is 9+ b +c) *1ste).out.println(a + b +c) *1ste).out.println(9*trin+ 9+(b+c)) " "

Options are A.9alue is =/ 18 @trin, 1. B.9alue is 1. 18 @trin, 1. C.9alue is 1. 18 @trin, D.Compilation fails Answer ! A is the "orre"t answer. 1f the left hand operand is not a @trin, then A operator treat as plus BF' if left hand operand is a @trin, then A perform @trin, "on"atenation.

Question - .=
What is the output for the below code? public class A { public A() { *1ste).out.println(9A9) " " public class % e&tends A i)ple)ents *erialiJable { public %() { *1ste).out.println(9%9) " " public class 5est { public static (oid )ain(*trin+... ar+s) throws =&ception { % b / new %() LbjectLutput*trea) sa(e / new LbjectLutput*trea)(new ?ileLutput*trea)(9datafile9)) sa(e.writeLbject(b) sa(e.flush()

LbjectDnput*trea) restore / new LbjectDnput*trea)(new ?ileDnput*trea)(9datafile9)) % J / (%) restore.readLbject()

" "

Options are A.A B A B.A B A B C.B B D.B Answer ! A is the "orre"t answer. 8n the time of deseriali*ation % the @eriali*a(le o(:e"t not "reate new o(:e"t. @o "onstru"tor of "lass B does not "alled. A is not @eriali*a(le o(:e"t so "onstru"tor is "alled.

Question - ./
What is the output for the below code? public class A { public A() { *1ste).out.println(9A9) " " public class 5est { public static (oid )ain(*trin+... ar+s) throws =&ception { A a / new A() LbjectLutput*trea) sa(e / new LbjectLutput*trea)(new ?ileLutput*trea)(9datafile9)) sa(e.writeLbject(a) sa(e.flush() LbjectDnput*trea) restore / new LbjectDnput*trea)(new ?ileDnput*trea)(9datafile9)) A J / (A) restore.readLbject()

" "

Options are A.A A B.A C.:a)a.io.Cot@eriali*a(le7x"eption D.Cone of the a(o)e Answer ! C is the "orre"t answer. Class A does not implements @eriali*a(le interfa"e. @o throws Cot@eriali*a(le7x"eption on tryin, to @eriali*e a non @eriali*a(le o(:e"t.

Question - .8
What will be the result of co)pilin+ and run the followin+ codeH public class 5est { public static (oid )ain(*trin+... ar+s) throws =&ception { Dnte+er i / 3! int l / 3! if(i.eRuals(l)){ *1ste).out.println(true) "else{ *1ste).out.println(false) " " "

Options are A.true B.false C.Compile error D.Cone of the a(o)e Answer ! A is the "orre"t answer.

eNuals$& method for the inte,er wrappers will only return true if the two primiti)e types and the two )alues are eNual.

Question - .9
What will be the result of co)pilin+ and run the followin+ codeH public class 5est { public static (oid )ain(*trin+... ar+s) throws =&ception { ?ile file / new ?ile(9test.t&t9) *1ste).out.println(file.e&ists()) file.create8ew?ile() *1ste).out.println(file.e&ists()) " "

Options are A.true true B.false true C.false true D.Cone of the a(o)e Answer ! B is the "orre"t answer. "reatin, a new instan"e of the "lass ?ile% you4re not yet ma#in, an a"tual file% you4re :ust "reatin, a filename. @o file.exists$& return false. "reateCew?ile$& method "reated an a"tual file.so file.exists$& return true.

Question - 60
What is the output for the below public class A {" code ?

public class % i)ple)ents *erialiJable { pri(ate static A a / new A() public static (oid )ain(*trin+... ar+s){ % b / new %() tr1{ ?ileLutput*trea) fs / new ?ileLutput*trea)(9b.ser9) LbjectLutput*trea) os / new LbjectLutput*trea)(fs)

os.writeLbject(b) os.close() "catch(=&ception e){ e.print*tac35race() " " "

Options are A.Compilation ?ail B.:a)a.io.Cot@eriali*a(le7x"eption! Be"ause "lass A is not @eriali*a(le. C.Co 7x"eption at -untime D.Cone of the a(o)e Answer ! C is the "orre"t answer. Co :a)a.io.Cot@eriali*a(le7x"eption% Be"ause "lass A )aria(le is stati". stati" )aria(les are not @eriali*a(le.

Question - 61
What will happen when 1ou atte)pt to co)pile and run the followin+ code ? 1. public class 5est e&tends 5hread{ 2. public static (oid )ain(*trin+ ar+(,-){ 3. 5est t / new 5est() !. t.run() #. t.start() $. " '. public (oid run(){ .. *1ste).out.println(9run;test9) 0. " 12. "

Options are A.run-test run-test B.run-test C.Compilation fails due to an error on line 6

D.Compilation fails due to an error on line / Answer ! A is the "orre"t answer. t.run$& Oe,al% (ut does not start a new thread % it is li#e a method "all of "lass 'est BF' t.start$& "reate a thread and "all run$& method.

Question - 62
What is the output for the below code ? class A i)ple)ents <unnable{ public (oid run(){ *1ste).out.println(9run;a9) " " 1. public class 5est { 2. public static (oid )ain(*trin+... ar+s) { 3. A a / new A() !. 5hread t / new 5hread(a) #. t.start() $. t.start() '. " .. "

Options are A.run-a B.run-a run-a C.Compilation fails with an error at line = D.Compilation su""eed (ut -untime 7x"eption Answer ! D is the "orre"t answer. 8n"e a thread has (een started% it "an ne)er (e started a,ain. 2nd time t.start$& throws :a)a.lan,.1lle,al'hread@tate7x"eption.

Question - 6.

What is the output for the below code ? class A i)ple)ents <unnable{ public (oid run(){ tr1{ for(int i/2 iG! i++){ 5hread.sleep(122) *1ste).out.println(5hread.current5hread().+et8a)e()) " "catch(Dnterrupted=&ception e){ " " "

public class 5est { public static (oid )ain(*trin+ ar+(,-) throws =&ception{ A a / new A() 5hread t / new 5hread(a,9A9) 5hread t1 / new 5hread(a,9%9) t.start() t.join() t1.start() " "

Options are A.A A A A B B B B B.A B A B A B A B C.8utput order is not ,uaranteed D.Compilation su""eed (ut -untime 7x"eption Answer ! A is the "orre"t answer. t.:oin$&< means 'hreat t must finish (efore 'hread t1 start.

Question - 66
What is the output for the below code ? public class % { public s1nchroniJed (oid print8a)e(){ tr1{

*1ste).out.println(9print8a)e9) 5hread.sleep(#Q1222) "catch(Dnterrupted=&ception e){ " " public " " s1nchroniJed (oid print4alue(){ *1ste).out.println(9print4alue9)

public class 5est e&tends 5hread{ % b / new %() public static (oid )ain(*trin+ ar+(,-) throws =&ception{ 5est t / new 5est() 5hread t1 / new 5hread(t,9t19) 5hread t2 / new 5hread(t,9t29) t1.start() t2.start() " public (oid run(){ if(5hread.current5hread().+et8a)e().eRuals(9t19)){ b.print8a)e() "else{ b.print4alue() " " "

Options are A.print ! printCame % then wait for se"onds then print ! print9alue B.print ! printCame then print ! print9alue C.print ! printCame then wait for minutes then print ! print9alue D.Compilation su""eed (ut -untime 7x"eption Answer ! A is the "orre"t answer. 'here is only one lo"# per o(:e"t% if one thread has pi"#ed up the lo"#% no other thread "an pi"# up the lo"# until the first thread releases the lo"#. printCame$& method a"Nuire the lo"# for se"onds% @o other threads "an not a""ess the o(:e"t. 1f one syn"hroni*ed method of an instan"e is exe"utin, then other syn"hroni*ed method of the same instan"e should wait.

Question - 6
What is the output for the below code ? public class % { public static s1nchroniJed (oid print8a)e(){ tr1{ *1ste).out.println(9print8a)e9) 5hread.sleep(#Q1222) "catch(Dnterrupted=&ception e){ " " public s1nchroniJed (oid print4alue(){ *1ste).out.println(9print4alue9) " "

public class 5est e&tends 5hread{ % b / new %() public static (oid )ain(*trin+ ar+(,-) throws =&ception{ 5est t / new 5est() 5hread t1 / new 5hread(t,9t19) 5hread t2 / new 5hread(t,9t29) t1.start() t2.start() " public (oid run(){ if(5hread.current5hread().+et8a)e().eRuals(9t19)){ b.print8a)e() "else{ b.print4alue() " " "

Options are A.print ! printCame % then wait for se"onds then print ! print9alue B.print ! printCame then print ! print9alue C.print ! printCame then wait for minutes then print ! print9alue D.Compilation su""eed (ut -untime 7x"eption

Answer ! B is the "orre"t answer. 'here is only one lo"# per o(:e"t% if one thread has pi"#ed up the lo"#% no other thread "an pi"# up the lo"# until the first thread releases the lo"#. 1n this "ase printCame$& is stati" % @o lo"# is in "lass B not instan"e (% (oth method $one stati" and other no-stati"& "an run simultaneously. A stati" syn"hroni*ed method and a non stati" syn"hroni*ed method will not (lo"# ea"h other.

Question - 6=
What is the output for the below code ? class A e&tends 5hread{ int count / 2 public (oid run(){ *1ste).out.println(9run9) s1nchroniJed (this) { for(int i /2 i G #2 i++){ count / count + i " notif1() " " "

public class 5est{ public static (oid )ain(*trin+ ar+(,-) { A a / new A() a.start() s1nchroniJed (a) { *1ste).out.println(9waitin+9) tr1{ a.wait() "catch(Dnterrupted=&ception e){ " *1ste).out.println(a.count) " " "

Options are A.waitin, run 122 B.waitin, run 0 C.waitin, run and "ount "an (e anythin, D.Compilation fails Answer ! A is the "orre"t answer. a.wait$&< put thread on wait until not ,et notifed. A thread ,ets on this waitin, list (y exe"utin, the wait$& method of the tar,et o(:e"t. 1t doesn4t exe"ute any further instru"tions until the notify$& method of the tar,et o(:e"t is "alled. A thread to "all wait$& or notify$&% the thread has to (e the owner of the lo"# for that o(:e"t.

Question - 6/
Which of the followin+ state)ents about this code are true? class A e&tends 5hread{ public (oid run(){ for(int i /2 i G 2 i++){ *1ste).out.println(i) " "

"

public class 5est{ public static (oid )ain(*trin+ ar+(,-){ 5est t / new 5est() t.chec3(new A(){") " public (oid chec3(A a){ a.start() " "

Options are A.0 0 B.Compilation error% "lass A has no start method C.0 1 D.Compilation su""eed (ut runtime ex"eption

Answer ! C is the "orre"t answer. "lass A extends 'hread means the anonymous instan"e that is passed to "he"#$& method has a start method whi"h then "alls the run method.

Question - 68
BashIap can be s1nchroniJed b1 6666666 ?

Options are A.Pap m ; Colle"tions.syn"hroni*ePap$hashPap&< B.Pap m ; hashPap.syn"hroni*ePap$&< C.Pap m ; Colle"tion.syn"hroni*ePap$hashPap&< D.Cone of the a(o)e Answer ! A is the "orre"t answer. GashPap "an (e syn"hroni*ed (y Pap m ; Colle"tions.syn"hroni*ePap$hashPap&<

Question - 69
What is the output for the below code? i)port ja(a.util.@in3ed@ist i)port ja(a.util.Sueue public class 5est { public static (oid )ain(*trin+... ar+s) {

Sueue R / new @in3ed@ist() R.add(9new1or39) R.add(9ca9) R.add(9te&as9) show(R) " public static (oid show(Sueue R) { R.add(new Dnte+er(11)) while (MR.is=)pt1 ( ) ) *1ste).out.print(R.poll() + 9

9)

" "

Options are A.Compile error ! 1nte,er "an4t add B.newyor# "a texas 11 C.newyor# "a texas D.Cone of the a(o)e Answer ! B is the "orre"t answer. K N was ori,inally de"lared as QueueQ@trin,R% But in show$& method it is passed as an untyped Queue. nothin, in the "ompiler or 59P pre)ents us from addin, an 1nte,er after that. | 1f the show method si,nature is pu(li" stati" )oid show$QueueQ@trin,R N& than you "an4t add 1nte,er% 8nly @trin, allowed. But pu(li" stati" )oid show$Queue N& is untyped Queue so you "an add 1nte,er. 3 poll$& -etrie)es and remo)es the head of this Nueue% or returns null if this Nueue is empty. Question - 1
What is the output for the bellow code? i)port ja(a.util.Dterator i)port ja(a.util.*et i)port ja(a.util.5ree*et public class 5est { public static (oid )ain(*trin+... ar+s) { *et s / new 5ree*et() s.add(9'9) s.add(0) Dterator itr / s.iterator() while (itr.has8e&t()) *1ste).out.print(itr.ne&t() + 9 9)

" "

Options are

A.Compile error B.-untime 7x"eption C./ 9 D.Cone of the a(o)e Answer ! B is the "orre"t answer. | Jithout ,eneri"s% the "ompiler does not #now what type is appropriate for this 'ree@et% so it allows e)erythin, to "ompile. But at runtime he 'ree@et will try to sort the elements as they are added% and when it tries to "ompare an 1nte,er with a @trin, it will throw a ClassCast7x"eption. T 7x"eption in thread KmainK :a)a.lan,.ClassCast7x"eption! :a)a.lan,.@trin, "annot (e "ast to :a)a.lan,.1nte,er. Question - 2
What is the output for the below code? i)port ja(a.util.Dterator i)port ja(a.util.5ree*et public class 5est { public static (oid )ain(*trin+... ar+s) { 5ree*et s1 / new 5ree*et() s1.add(9one9) s1.add(9two9) s1.add(9three9) s1.add(9one9) Dterator it / s1.iterator() while (it.has8e&t() ) { *1ste).out.print( it.ne&t() + 9 9 ) "

" "

Options are A.one three two B.-untime 7x"eption C.one three two one D.one two three

Answer ! A is the "orre"t answer. h 'ree@et assures no dupli"ate entries.it will return elements in natural order% whi"h% for @trin,s means alpha(eti"al. Question - .
Df we do Arra1@ist lst / new Arra1@ist() What is the initial capacit1 of the Arra1@ist lst ?

Options are A.10 B.8 C.1 D.12 Answer ! A is the "orre"t answer. BDD D Constru"ts an empty list with an initial "apa"ity of ten. DB pu(li" ArrayOist$& L this$10&< M

Question - 6
What is the output for the below code ? pac3a+e bean public class Abc { public static int inde&6(al / 12 " pac3a+e co) i)port static bean.Abc.inde&6(al public class 5est1 { public static (oid )ain(*trin+... ar+s) { *1ste).out.println(inde&6(al) " "

Options are

A.10 B."ompile error% index2)al not defined C.Compile error at import stati" (ean.A(".index2)al< D.Cone of the a(o)e Answer ! A is the "orre"t answer. 'he stati" import "onstru"t allows unNualified a""ess to stati" mem(ers without inheritin, from the type "ontainin, the stati" mem(ers. 52@7 .0 onwards it allows stati" import li#e import stati" (ean.A(".index2)al< and "an (e use dire"tly @ystem.out.println$index2)al&<

Question Which of the followin+ state)ent is true about jar co))and?

Options are A.'he :ar "ommand "reates the P7'A-1C? dire"tory impli"itly. B.'he :ar "ommand "reates the PAC1?7@'.P? file impli"itly. C.'he :ar "ommand would not pla"e any of your files in P7'A-1C? dire"tory. D.All of the a(o)e are true Answer ! A is the "orre"t answer. All statements are true.

Question - =
Kou ha(e a class file na)e 5est.class inside ja(aproject director1. 5est.ja(a source code is H i)port ja(a.util.Oroperties class 5est { public static (oid )ain (*trin+,- ar+s){ Oroperties p / *1ste).+etOroperties() *1ste).out.println(p.+etOropert1(93e119)) " "

1ou ha(e issued below co))ands fro) co))and pro)pt. cd ja(aproject ja(a ;C 3e11/(alue1 5est What is the output ?

Options are A.)alue1 B.null C.-un su""essfully (ut no output D.-un fails - :a)a.lan,.CoClassDef?ound7rror! #ey1;)alue1 Answer ! D is the "orre"t answer. -D option % name;)alue pair must follow immediately% no spa"es allowed. 1n this "ase there is spa"e (etween -D and #ey1;)alue1 @o :a)a.lan,.CoClassDef?ound7rror! #ey1;)alue1.

Question - /
What is the output for the below code ? public class A { public (oid print4alue(){ *1ste).out.println(9A9) " " public class % e&tends A { public (oid print4alue(){ *1ste).out.println(9%9) " " 1. public class 5est { 2. public static (oid )ain(*trin+... ar+s) { 3. A b / new %() !. new4alue(b) #. " $. public static (oid new4alue(A a){ '. if(a instanceof %){ .. ((%)a).print4alue() 0. " 12. "

11. "

Options are A.A B.B C.Compilation fails with an error at line 6 D.Compilation fails with an error at line 8 Answer ! B is the "orre"t answer. instan"eof operator is used for o(:e"t referen"e )aria(les to "he"# whether an o(:e"t is of a parti"ular type. 1n new9alue$(&< ( is instan"e of B @o wor#s properly.

Question - 8
What is the output for the below code ? 1. public class 5est { 2. static int i /# 3. public static (oid )ain(*trin+... ar+s) { !. *1ste).out.println(i++) #. *1ste).out.println(i) $. *1ste).out.println(++i) '. *1ste).out.println(++i+i++) .. 0. " 12. "

Options are A. = / 1= B.= = = 1= C.= = / 1= D. = = 1= Answer ! A is the "orre"t answer. iAA ! print )alue then in"rement $postfix - in"rement happens after the )alue of the )aria(le is used& AAi ! in"rement the print $prefix - in"rement happens (efore the )alue of the )aria(le is used&

Question - 9
What is the output for the below code ? 1. public class 5est { 2. public static (oid )ain(*trin+... ar+s) { 3. Dnte+er i / 3! !. *trin+ str / (iG21)?9jan9H(iG#$)?9feb9H9)arch9 #. *1ste).out.println(str) $. " '. "

Options are A.fe( B.:an C.mar"h D.Compilation fails with an error at line 6 Answer ! A is the "orre"t answer. 'his is nested "onditional with un(ox. $iQ21& is false ,oto $iQ =&% $iQ =& is true so result is Kfe(K.

Question - =0
What is the output ? public class 5est { public static (oid )ain(*trin+... ar+s) { Oattern p / Oattern.co)pile(9a+b?cQ9) Iatcher ) / p.)atcher(9ab9) boolean b / ).)atches() *1ste).out.println(b)

" "

Options are A.true

B.false C.Compile error D.Cone of the a(o)e Answer ! A is the "orre"t answer. ST S% on"e or not at all SD S% *ero or more times SA S% one or more times

Question - =1
What is the output for the below code ? 1. public class 5est { 2. public static (oid )ain(*trin+,- ar+s){ 3. b1te i / 12. !. *1ste).out.println(i) #. " $. "

Options are A.128 B.0 C.Compilation fails with an error at line . D.Compilation fails with an error at line 6 Answer ! C is the "orre"t answer. (yte "an only hold up to 12/. @o "ompiler "omplain a(out possi(le loss of pre"ision.

Question - =2
What is the output for the below code ? 1. public class 5est { 2. int i/. 3. int j/0 !. public static (oid )ain(*trin+,- ar+s){ #. add() $. " '. public static (oid add(){ .. int 3 / i+j

0. 12. " 11. "

*1ste).out.println(3)

Options are A.1/ B.0 C.Compilation fails with an error at line D.Compilation fails with an error at line 8 Answer ! D is the "orre"t answer. i and : are instan"e )aria(le and attemptin, to a""ess an instan"e )aria(le from a stati" method. @o Compilation fails .

Question - =.
Which collection class +rows or shrin3s its siJe and pro(ides inde&ed access to its ele)ents, but )ethods are not s1nchroniJed?

Options are A.:a)a.util.ArrayOist B.:a)a.util.Oist C.:a)a.util.Gash@et D.:a)a.util.9e"tor Answer ! A is the "orre"t answer. | ArrayOist pro)ides an index to its elements and methods are not syn"hroni*ed. Question - =6
What is the output of bellow code ? public class %ean{ pri(ate *trin+ str %ean(*trin+ str ){ this.str / str " public *trin+ +et*tr() { return str

" public boolean eRuals(Lbject o){ if (M(o instanceof %ean)) { return false " return ((%ean) o).+et*tr().eRuals(str) " public int hash:ode() { return 123!# " public *trin+ to*trin+() { return str "

"

i)port ja(a.util.Bash*et public class 5est { public static (oid )ain(*trin+ ... sss) { Bash*et )1Iap / new Bash*et() *trin+ s1 / new *trin+(9das9) *trin+ s2 / new *trin+(9das9) %ean s3 / new %ean(9abcdef9) %ean s! / new %ean(9abcdef9) )1Iap.add(s1) )1Iap.add(s2) )1Iap.add(s3) )1Iap.add(s!) *1ste).out.println()1Iap) " "

Options are A.das a("def B.das a("def das a("def C.das das a("def a("def D.das Answer ! A is the "orre"t answer. implemented 4eNuals4 and 4hashCode4 methods to ,et uniNue result in @et.

Question - =
What will happen when 1ou atte)pt to co)pile and run the followin+ code ? class A i)ple)ents <unnable{ public (oid run(){ *1ste).out.println(9run;A9) "

" 1. public class 5est { 2. public static (oid )ain(*trin+ ar+(,-){ 3. A a / new A() !. 5hread t / new 5hread(a) #. *1ste).out.println(t.isAli(e()) $. t.start() '. *1ste).out.println(t.isAli(e()) .. " 0. "

Options are A.false run-A true B.false run-A false C.true run-A true D.Compilation fails due to an error on line / Answer ! A is the "orre"t answer. 8n"e the start$& method is "alled% the thread is "onsidered to (e ali)e.

Question - ==
What will happen when 1ou atte)pt to co)pile and run the followin+ code ? 1. public class 5est e&tends 5hread{ 2. public static (oid )ain(*trin+ ar+(,-){ 3. 5est t / new 5est() !. t.run() #. t.start() $. " '. public (oid run(){

.. *1ste).out.println(9run;test9) 0. " 12. "

Options are A.run-test run-test B.run-test C.Compilation fails due to an error on line 6 D.Compilation fails due to an error on line / Answer ! A is the "orre"t answer. t.run$& Oe,al% (ut does not start a new thread % it is li#e a method "all of "lass 'est BF' t.start$& "reate a thread and "all run$& method.

Question - =/
Which of the followin+ are )ethods of the 5hread class? 1) 2) 3) !) 1ield() sleep(lon+ )sec) +o() stop()

Options are A.1 % 2 and 6 B.1 and . C.. only D.Cone of the a(o)e Answer ! A is the "orre"t answer. Che"# out the 5a)a2 Do"s for an explanation

Question - =8
What notif1All() )ethod do?

Options are

A.Ja#es up all threads that are waitin, on this o(:e"t4s monitor B.Ja#es up one threads that are waitin, on this o(:e"t4s monitor C.Ja#es up all threads that are not waitin, on this o(:e"t4s monitor D.Cone of the a(o)e Answer ! A is the "orre"t answer. notifyAll$& ! Ja#es up all threads that are waitin, on this o(:e"t4s monitor.A thread waits on an o(:e"t4s monitor (y "allin, one of the wait methods.

You might also like