final set- final check scjp question

Upload: sanjuindia2005

Post on 02-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Final Set- Final Check SCJP Question

    1/41

    SCJP 1.6 (CX-310-065 , CX-310-066)

    Subject: Mock Questions Final Check

    Total Questions : 60

    Prepared by : http://www.techfaq360.com

    SCJP 6.0: Test your preparation (60 Questions)Questions 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 / 00. * ste . out . println ( add ( s , $))1 . "11. "

    1.Compile fail due to error on line no 22.Compile fail due to error on line no !.Compile fail due to error on line no "#.1$

    %&planation :' is t e orre t ans*er.

    Cannot ma+e a stati referen e to t e non-stati met od add(int, int) from t e type .T e s ort s is auto o&ed orre tly, ut t e add() met od annot e in/o+ed from astati met od e ause add() met od is not stati .

    Question - 2

    What is the output for the below code ?

    public class A { int boolean istrue static int p public oid print4alue () { * ste . out . print ( ) * ste . out . print ( istrue ) * ste . out . print ( p )

    "

    http://www.techfaq360.com/http://www.techfaq360.com/http://www.techfaq360.com/
  • 8/10/2019 Final Set- Final Check SCJP Question

    2/41

    " public class 5est {

    public static oid ain ( *trin ar -){

    A a / new A () a . print4alue ()

    ""

    1.0 false 02.0 true 0!.0 0 0#.Compile error - stati /aria le must e initiali ed efore use.

    %&planation : is t e orre t ans*er.

    lo al and stati /aria le need not e initiali ed efore use. efault /alue of 3lo aland stati int /aria le is ero. efault /alue of oolean /aria le is false. 4emem erlo 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

    * ste . out . println ( test . 7' ) * ste . out . println ( test . do ) * ste . out . println ( test . 67 )

    ""

    1.5 02.5 0 0!.Compile error - 5 is not /alid identifier.#.Compile error - do is not /alid identifier.

    %&planation : is t e orre t ans*er.

  • 8/10/2019 Final Set- Final Check SCJP Question

    3/41

    5 is /alid identifier. 7dentifiers must start *it a letter, a urren y ara ter ( ), orunders ore ( 8 ). 7dentifiers annot start *it a num er. 9ou an t use a Ja/a +ey*ordas an identifier. do is a Ja/a +ey*ord.

    Question - #

    What is the output for the below code ?

    pac a e co class Ani al {

    public oid print8a e (){ * ste . out . println ( 9Ani al9 ) "

    "

    pac a e e&a i port co . Ani al

    public class :at e&tends Ani al {

    public oid print8a e (){ * ste . out . println ( 9:at9 ) "

    "

    pac a 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 ()

    "

    "

    1. nimal2.Cat!. nimal Cat#.Compile %rror

    %&planation : is t e orre t ans*er.

    Cat lass *on t ompile e ause its super lass, nimal, as default a ess and is in adifferent pa +a3e. ;nly pu li super lass an e a essi le for different pa +a3e.

  • 8/10/2019 Final Set- Final Check SCJP Question

    4/41

    Question - $

    What is the output for the below code ?

    public class A { int i / 1 public oid print4alue () { * ste . out . println ( 94alue;A9 ) "

    "

    public class % e&tends A { int i / 12 public oid print4alue () { * ste . out . print ( 94alue;%9 ) ""

    public class 5est {

    public static oid ain ( *trin ar -){

    A a / new %() a . print4alue () * ste . out . println ( a . i )

    ""

    1.

  • 8/10/2019 Final Set- Final Check SCJP Question

    5/41

    pri ate int

    5est ( int hh , int ) { assert ( hh E/ FF hh G/ 23 ) H 9Dlle al hour.9 assert ( E/ 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*5

    * ste . out . println ( t . etBour () +9H9+t . etIins ()) ""

    1.5:!02.Compile %rror - an enum annot e instantiated usin3 t e ne* operator.!.12:!0#.1 :#$

    %&planation :' is t e orre t ans*er.

    s an enum annot e instantiated usin3 t e ne* operator, t e onstru tors annot ealled e&pli itly. 9ou a/e to do li+e Test t > '4% @A ST?

    Question - 5

    What is the output for the below code ?

    public class A { static {* ste . out . println ( 9static9 ) "

    { * ste . out . println ( 9bloc 9 ) " public A (){ * ste . out . println ( 9A9) "

    public static oid ain ( *trin - ar s ){ A a / new A ()

    "

    1. lo + stati2.stati lo +

    !.stati #.

  • 8/10/2019 Final Set- Final Check SCJP Question

    6/41

    %&planation :' is t e orre t ans*er.

    Airst e&e ute stati lo +, t en statement lo + t en onstru tor.

    Question - "

    What is the output for the below code ?

    1. public class 5est {2. public static oid ain ( *trin - ar s ){3. int i / 1!. int j / '#. * ste . out . println ( i )

    $. * ste . out . println ( j )'. ". "

    1." 52.10 5!.Compilation fails *it an error at line !#.Compilation fails *it an error at line $

    %&planation : is t e orre t ans*er.

    'y pla in3 a ero in front of t e num er is an inte3er in o tal form. 010 is in o talform so its /alue is ".

    Question -

    What is the output for the below code ?

    1. public class 5est {2. public static oid ain ( *trin - ar s ){

    3. b te b / $!. b +/#. * ste . out . println ( b )$. b / b +''. * ste . out . println ( b )

    . "0. "

    1.1# 212.1# 1!!.Compilation fails *it an error at line 6#.Compilation fails *it an error at line #

  • 8/10/2019 Final Set- Final Check SCJP Question

    7/41

    %&planation :C is t e orre t ans*er.

    int or smaller e&pressions al*ays resultin3 in an int. So ompiler omplain a outType mismat : annot on/ert from int to yte for > B5? 'ut B> 5? Do

    pro lem e ause B>, ->, E>, and > *ill all put in an impli it ast. B> 5 is same as > ( yte) B5 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 ) * ste . out . println ( alue ) "

    public static oid chan e4alue ( *trin a ){ a / 9& J9" "

    1.a

    2.&y!.Compilation fails#.Compilation lean ut no output

    %&planation : is t e orre t ans*er.

    Ja/a pass referen e as /alue. passin3 t e o =e t referen e, and not t e a tual o =e titself. Simply reassi3nin3 to t e parameter used to pass t e /alue into t e met od *illdo not in3, e ause t e 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 ){ * ste . out . println ( 9int9 ) " public static oid print4alue ( b te ... b ){ * ste . out . println ( 9lon 9 ) "

  • 8/10/2019 Final Set- Final Check SCJP Question

    8/41

    public static oid ain ( *trin ... ar s ) { b te b / 0 print4alue ( b , b , b ) ""

    1.lon32.int!.Compilation fails#.Compilation lean ut t ro*s 4untime%& eption

    %&planation :' is t e orre t ans*er.

    Primiti/e *idenin3 uses t e smallest met od ar3ument possi le. (Aor %&ample if you pass s ort /alue to a met od ut met od *it s ort ar3ument is not a/aila le t en

    ompiler oose met od *it int ar3ument). 'ut in t is ase ompiler *ill prefer t eolder style efore it ooses t e ne*er style, to +eep e&istin3 ode more ro ust. /ar-ar3s met od is looser t an *iden.

    Question - 12Aill in t e 3ap:

    public class 5est {

    public static oid main ( *trin - args ) { *trin - words / new *trin - { "aaa" , "bbb" , "ccc" , "aaa" " Iap G *trin , Dnte er E m / new 5reeIap G *trin , Dnte er E() for ( *trin word H words ) { freq / m . get ( word ) m . put ( word , freq // null ? 1 H freq + 1 ) " * ste . out . println ( m)

    "

    Fse t e follo*in3 fra3ments ero or many timesStrin3

    7nte3er

    'oolean

    Aloat

    Question - 1!

    Kou ha e a ja a file na e 5est . ja a inside src folder of ja aprojectdirector .

    Kou ha e also classes folder inside ja aproject director .

  • 8/10/2019 Final Set- Final Check SCJP Question

    9/41

    ou 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?

    1.=a/a -d lasses sr Test.=a/a2.=a/a Test.=a/a!.=a/a sr Test.=a/a#.=a/a lasses sr Test.=a/a

    %&planation : is t e orre t ans*er.

    T e -d option lets you tell t e ompiler in * i dire tory to put t e . lass file it3enerates (d for destination)

    Question - 1#

    Kou ha e two class files na e 5est . class and 5est1 . class insideja aproject director .

    5est . ja a source code is H

    public class 5est {

    public static oid ain ( *trin - ar s ){ * ste . out . println ( 9Bello 5est9 ) ""

    5est1 . ja a source code is H

    public class 5est1 {

    public static oid ain ( *trin - ar s ){ * ste . out . println ( 9Bello 5est19 ) ""

    ou ha e issued below co ands fro co and pro pt .

    cd ja aprojectja a 5est 5est1

    What is the output ?

    1.Gello Test2.Gello Test Gello Test1!.Gello Test1

    #.4un fails - lass not found

  • 8/10/2019 Final Set- Final Check SCJP Question

    10/41

    %&planation : is t e orre t ans*er.

    9ou must spe ify e&a tly one lass file to e&e ute. 7f more t an one t en first one *ill e e&e 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 9director .

    Which of the follwin co and set classpath to co pile clean ?

    1.=a/a - lasspat e&am app.=ar Test.=a/a2.=a/a - lasspat app.=ar Test.=a/a!.=a/a - lasspat e&am Test.=a/a#.Done of t e a o/e

    %&planation : is t e orre t ans*er.

    =a/a - lasspat e&am app.=ar Test.=a/a is t e orre t ommand to set e&am app.=ar inlasspat .

    Question - 16

    What will be the result of co pilin the followin code H

    public class *uper:lass { public int doDt ( *trin str , Dnte er ... data ) throws =&ception { *trin si nature / 9(*trin , Dnte er -)9 * ste . 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 * ste . out . println ( 9L erriddenH 9 + str + 9 9 + si nature ) return "

    public static oid ain ( *trin ... ar s )

    { *uper:lass sb / new *ub:lass () sb . doDt ( 9hello9 , 3)

  • 8/10/2019 Final Set- Final Check SCJP Question

    11/41

    " "

    1.;/erridden: ello (Strin3, 7nte3erHI)2. ello (Strin3, 7nte3erHI)!.Complilation fails#.Done of t e a o/e

    %&planation :C is t e orre t ans*er.

    Fn andled e& eption type %& eption.

    Question - 15 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 i M/j H i

    1.T e lass ompiles and runs, ut does not print anyt in3.2.T e num er 2 3ets printed *it ssertion%rror

    !. ompile error #.T e num er ! 3ets printed *it ssertion%rror

    %&planation : is t e orre t ans*er.

    en if ondition returns true, t e assert statement also returns true. Gen essertion%rror does not 3et 3enerated. .

    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 ++) assert i M/j H i

    1.T e lass ompiles and runs, ut does not print anyt in3.2.T e num er 2 3ets printed *it ssertion%rror !. ompile error

    #.T e num er ! 3ets printed *it ssertion%rror

  • 8/10/2019 Final Set- Final Check SCJP Question

    12/41

    %&planation :' is t e orre t ans*er.

    en i and = are ot 2, assert ondition is false, and ssertion%rror 3ets 3enerated. .

    Question - 1

    tr { 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 ?

    1.True2.Aalse!.Compilation %rror #.Done

    %&planation :C is t e orre t ans*er.

    7;%& eption is unrea a le to ompiler e ause all e& eption is 3oin3 to at y%& eption lo +.

    Question - 20

    class A { A ( *trin s ) { " A () { ""

    1. class % e&tends A {2. %() { "3. %( *trin s ) {!. super ( s )#. "$. oid test () {'. // insert code here

    . "0. "

    Which of the below code can be insert at line ' to a e cleanco pilation ?

    1. a > ne* '()?2. a > ne* '($)?

  • 8/10/2019 Final Set- Final Check SCJP Question

    13/41

    !. a > ne* (Strin3 s)?#. ll of t e a o/e

    %&planation : is t e orre t ans*er.

    a > ne* '()? is orre t e ause anonymous inner lasses are no different from anyot er lass * en it omes to polymorp ism.

    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 (){#. * ste . out . println ( 9A9)$. "'. "

    . a1 . print4alue () 0. "1 . "

    1.Compilation fails due to an error on line !2.!.Compilation fails due to an error on line "#.null

    %&planation :' is t e orre t ans*er.

    T e a1 referen e /aria le refers not to an instan e of interfa e , ut to an instan eof an anonymous (unnamed) lass. So no ompilation error.

    Question - 22

    class A { class A1 { oid print4alue (){ * ste . out . println ( 9A.A19 ) " ""

    1. public class 5est { 2. public static oid ain ( *trin - ar s ){

  • 8/10/2019 Final Set- Final Check SCJP Question

    14/41

    3. A a / new A ()!. // INSERT CODE #. a1 . print4alue ()$. "'. "

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

    1. . 1 a1 > ne* . 1()?2. . 1 a1 > a.ne* 1()?!. a1 > ne* . 1()?#. ll of t e a o/e

    %&planation :' is t e orre t ans*er.

    orre t inner lass instantiation synta& is a > ne* ()? . 1 a1 > a.ne* 1()?

    Question - 2!

    What is the output for the below code ?

    public class A { public oid print4alue (){

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

    " public class % e&tends A {

    public oid print8a e% (){ * ste . out . println ( 98a e;%9 ) " "

    public class : e&tends A {

    public oid print8a e: (){ * ste . out . println ( 98a e;:9 ) "

    "

    1. public class 5est { 2. public static oid ain ( *trin - ar s ) {3. % b / new %()!. : c / new : ()#. newNrint ( b )$. newNrint ( c ) '. "

    . public static oid newNrint ( A a ){0. a . print4alue ()1 . "

    11. "

  • 8/10/2019 Final Set- Final Check SCJP Question

    15/41

    1.

  • 8/10/2019 Final Set- Final Check SCJP Question

    16/41

    %&planation :C is t e orre t ans*er.

    4eferen e /aria le an refer to any o =e t of t e same type as t e de lared referen e;4 an refer to any su type of t e de lared type. 4eferen e /aria le K K is type of

    lass ' and referen e /aria le K K is a type of lass C. So Compilation fails.

    Question - 2$

    What is the output for the below code ?

    public class : {

    "

    public class C e&tends : {

    "

    public class A {

    public : etL%O (){ * ste . out . println ( 9class A ; return :9 ) return new : ()

    "

    "

    public class % e&tends A {

    public C etL%O (){ * ste . out . println ( 9class % ; return C9 ) return new C ()

    "

    "

    public class 5est {

    public static oid ain ( *trin ... ar s ) { A a / new %() a . etL%O ()

    ""

    1. lass - return C2. lass ' - return !.Compilation fails#.Compilation su eed ut no output

    %&planation :' is t e orre t ans*er.

  • 8/10/2019 Final Set- Final Check SCJP Question

    17/41

    Arom J2S% $.0 on*ards. return type in t e o/erridin3 met od an e same or su typeof t e de lared return type of t e o/erridden (super lass) met od.

    Question - 26

    What is the output for the below code ?

    public class A { pri ate oid print8a e (){

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

    public class % e&tends A { public oid print8a e (){

    * ste . out . println ( 98a e;%9 ) " "

    public class 5est { public static oid ain ( *trin - ar s ) {

    % b / new %() b . print8a e () " "

    1.

  • 8/10/2019 Final Set- Final Check SCJP Question

    18/41

    public class % e&tends A { public oid print8a e () throws 8ullNointer=&ception {

    * ste . out . println ( 98a e;%9 ) " "

    public class 5est { public static oid ain ( *trin - ar s ) throws =&ception {

    A a / new %() a . print8a e () " "

    1.

  • 8/10/2019 Final Set- Final Check SCJP Question

    19/41

    public class 5est {

    public static oid ain ( *trin - ar s ){ new %( #) ""

    1. ' "2. $ ' "!. ' $#.' " $

    %&planation : is t e orre t ans*er.

    Constru tor of lass ' all t eir super lass onstru tor of lass (pu li ()) , * ie&e ute first, and t at onstru tors an e o/erloaded. T en ome to onstru tor of

    lass ' (pu li ' (int i)).

    Question - 2

    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 () * ste . out . println ( t . et8a e ())

    ""

    1.test-name2.Compilation fails due to an error on lines 2!.Compilation fails due to an error on lines 1#.Compilation su eed ut 4untime %& eption

    %&planation :' is t e orre t ans*er.

    7lle3al modifier for t e interfa e met od 7nf .3etDame()? only pu li and a stra t are permitted

  • 8/10/2019 Final Set- Final Check SCJP Question

    20/41

    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 () { * ste . 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 ()#. $. "'. "

    1.Dame-2.Compilation fails due to an error on lines !!.Compilation fails due to an error on lines ##.Compilation su eed ut no output

    %&planation :' is t e orre t ans*er.

    Sin e t ere is already a onstru tor in t is lass (pu li (int i,int =)), t e ompiler*on t supply a default onstru tor. 7f you *ant a no-ar3ument onstru tor to o/erloadt e *it -ar3uments /ersion you already a/e, you a/e to define it y yourself. T e

    onstru tor () is undefined in lass . 7f you define e&pli it onstru tor t en defaultonstru tor *ill not e a/aila le. 9ou a/e to define e&pli itly li+e pu li ()L M t en

    t e a o/e ode *ill *or+. 7f no onstru tor into your lass , a default onstru tor *ill e automati ally 3enerated y t e ompiler.

    Question - !1

    public class A { public oid test1 (){

    * ste . out . println ( 9test19 ) ""

    public class % e&tends A { public oid test2 (){

  • 8/10/2019 Final Set- Final Check SCJP Question

    21/41

    * ste . 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 %()$. // insert code here'. "

    . "

    Which of the followin , inserted at line $, will co pile and printtest2 ?

    1.((') ).test2()?2.(') .test2()?

    !. .test2()?#.a.test2()?

    %&planation : is t e orre t ans*er.

    ((') ).test2()? is proper ast. test2() met od is in lass ' so need to ast t en onlytest2() is a essi le. (') .test2()? is not proper ast *it out t e se ond set of

    parent eses, t e ompiler t in+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 & / #!. & P/ 3 + '#. * ste . out . println ( &)$. "'. "

    1.222.$0!.10#.Compilation fails *it an error at line #

    %&planation :' is t e orre t ans*er.

    & E> ! B 5? is same as & > & E (! B5) > $ E (10) > $0 e ause e&pression on t e ri3 t isal*ays pla ed inside parent eses.

  • 8/10/2019 Final Set- Final Check SCJP Question

    22/41

    Question - !!

    What is the output for the below code ?

    1. public class 5est { 2. enu Ionth { OA8 , =%, IA< "3. public static oid ain ( *trin ... ar s ) {!. Ionth 1 / Ionth . OA8#. Ionth 2 / Ionth . OA8$. Ionth 3 / Ionth . =%'. * ste . out . println ( 1 // 2 )

    . * ste . out . println ( 1 . eQuals ( 2 ))0. * ste . out . println ( 1 // 3 )1 . * ste . out . println ( 1 . eQuals ( 3 ))11. "12. "

    1.true true true false2.true true false false!.false false true true#.Compilation fails *it an error at line 10

    %&planation :' is t e orre t ans*er.

    m1 and m2 refer to t e same enum onstant So m1 >> m2 returns true 'FT m1 andm! refer to different enum onstant So m1 >> m! returns false. m1.eNuals(m2)

    returns true e ause enum onstant /alue is same (J D and J D). m1.eNuals(m!)return false e ause enum onstants /alues are different (J D and A%').

    Question - !#

    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 #-!. * ste . out . println ( inde& instanceof Lbject )#. "$. "

    1.true2.false!.Compilation fails *it an error at line !#.Compilation fails *it an error at line #

    %&planation : is t e orre t ans*er.

    n array is al*ays an instan e of ; =e t

  • 8/10/2019 Final Set- Final Check SCJP Question

    23/41

    Question - !$

    What is the output for the below code ?

    public class 5est { public static oid ain ( *trin ... ar s ) {

    int a / # , b / $, c / ' * ste . out . println ( 94alue is 9 + b +c ) * ste . out . println ( a + b +c ) * ste . out . println ( 9*trin 9 +( b +c )) ""

    1.

  • 8/10/2019 Final Set- Final Check SCJP Question

    24/41

    % J / ( %) restore . readLbject ()

    "

    "

    1. ' 2. ' '!.' '#.'

    %&planation : is t e orre t ans*er.

    ;n t e time of deseriali ation , t e Seriali a le o =e t not reate ne* o =e t. So

    onstru tor of lass ' does not alled. is not Seriali a le o =e t so onstru tor isalled.

    Question - !5

    What is the output for the below code ?

    public class A { public A () { * ste . 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 ( newileLutput*trea ( 9datafile9 ))

    sa e . writeLbject ( a ) sa e . flush ()

    LbjectDnput*trea restore / new LbjectDnput*trea ( newileDnput*trea ( 9datafile9 ))

    A J / ( A ) restore . readLbject ()

    " "

    1.

    2.!.=a/a.io.DotSeriali a le%& eption

  • 8/10/2019 Final Set- Final Check SCJP Question

    25/41

    #.Done of t e a o/e

    %&planation :C is t e orre t ans*er.

    Class does not implements Seriali a le interfa e. So t ro*s DotSeriali a le%& eption on tryin3 to Seriali e a non Seriali a le o =e t.

    Question - !"

    What will be the result of co pilin and run the followin code H

    public class 5est {

    public static oid ain ( *trin ... ar s ) throws =&ception { Dnte er i / 3! int l / 3! if ( i . eQuals ( l )){ * ste . out . println ( true ) " else { * ste . out . println ( false ) "

    " "

    1.true2.false!.Compile error #.Done of t e a o/e

    %&planation : is t e orre t ans*er.

    eNuals() met od for t e inte3er *rappers *ill only return true if t e t*o primiti/etypes and t e t*o /alues are eNual.

    Question - !

    What will be the result of co pilin and run the followin code H public class 5est {

    public static oid ain ( *trin ... ar s ) throws =&ception {

    ile file / new ile ( 9test.t&t9 ) * ste . out . println ( file . e&ists ())

    file . create8ew ile () * ste . out . println ( file . e&ists ())

    " "

  • 8/10/2019 Final Set- Final Check SCJP Question

    26/41

    1.true true2.false true!.false true#.Done of t e a o/e

    %&planation :' is t e orre t ans*er.

    reatin3 a ne* instan e of t e lass Aile, you re not yet ma+in3 an a tual file, you re =ust reatin3 a filename. So file.e&ists() return false. reateDe*Aile() met od reatedan a tual file.so file.e&ists() return true.

    Question - #0

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

    public class % i ple ents *erialiJable { pri ate static A a / new A () public static oid ain ( *trin ... ar s ){ % b / new %() tr { 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*tac 5race () "

    "

    "

    1.Compilation Aail2.=a/a.io.DotSeriali a le%& eption: 'e ause lass is not Seriali a le.!.Do %& eption at 4untime

    #.Done of t e a o/e

    %&planation :C is t e orre t ans*er.

    Do =a/a.io.DotSeriali a le%& eption, 'e ause lass /aria le is stati . stati/aria les are not Seriali a le.

    Question - #1

  • 8/10/2019 Final Set- Final Check SCJP Question

    27/41

    What will happen when ou atte pt to co pile and run the followincode ?

    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 (){

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

    1.run-test run-test2.run-test!.Compilation fails due to an error on line ##.Compilation fails due to an error on line 5

    %&planation : is t e orre t ans*er.

    t.run() Oe3al, ut does not start a ne* t read , it is li+e a met od all of lass Test'FT t.start() reate a t read and all run() met od.

    Question - #2

    What is the output for the below code ?

    class A i ple ents

  • 8/10/2019 Final Set- Final Check SCJP Question

    28/41

    ;n e a t read as een started, it an ne/er e started a3ain. 2nd time t.start() t ro*s =a/a.lan3.7lle3alT readState%& eption.

    Question - #!

    What is the output for the below code ?

    class A i ple ents

  • 8/10/2019 Final Set- Final Check SCJP Question

    29/41

    " "

    public s nchroniJed oid print4alue (){ * ste . 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 (). eQuals ( 9t19 )){ b . print8a e () " else { b . print4alue () "

    "

    "

    1.print : printDame , t en *ait for $ se onds t en print : print

  • 8/10/2019 Final Set- Final Check SCJP Question

    30/41

    " catch ( Dnterrupted=&ception e ){ "

    "

    public s nchroniJed oid print4alue (){ * ste . 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 (). eQuals ( 9t19 )){ b . print8a e () " else { b . print4alue () "

    "

    "

    1.print : printDame , t en *ait for $ se onds t en print : print

  • 8/10/2019 Final Set- Final Check SCJP Question

    31/41

    count / count + i " notif () " ""

    public class 5est {

    public static oid ain ( *trin ar -) { A a / new A () a . start () s nchroniJed ( a ) { * ste . out . println ( 9waitin 9 ) tr { a . wait ()

    " catch ( Dnterrupted=&ception e ){ "

    * ste . out . println ( a . count )

    "

    " "

    1.*aitin3 run 122$2.*aitin3 run 0!.*aitin3 run and ount an e anyt in3#.Compilation fails

    %&planation : is t e orre t ans*er.

    a.*ait()? put t read on *ait until not 3et notifed. t read 3ets on t is *aitin3 list ye&e utin3 t e *ait() met od of t e tar3et o =e t. 7t doesn t e&e ute any furt erinstru tions until t e notify() met od of t e tar3et o =e t is alled. t read to all*ait() or notify(), t e t read as to e t e o*ner of t e lo + for t at o =e t.

    Question - #5

    Which of the followin state ents about this code are true ?

    class A e&tends 5hread { public oid run (){

    for ( int i / i G 2 i ++){ * ste . out . println ( i ) "

    ""

  • 8/10/2019 Final Set- Final Check SCJP Question

    32/41

    public class 5est { public static oid ain ( *trin ar -){ 5est t / new 5est () t . chec ( new A (){")

    "

    public oid chec ( A a ){ a . start () "

    "

    1.0 02.Compilation error, lass as no start met od!.0 1#.Compilation su eed ut runtime e& eption

    %&planation :C is t e orre t ans*er.

    lass e&tends T read means t e anonymous instan e t at is passed to e +()met od as a start met od * i t en alls t e run met od.

    Question - #"

    BashIap can be s nchroniJed b 6666666 ?

    1. ap m > Colle tions.syn roni e ap( as ap)?2. ap m > as ap.syn roni e ap()?!. ap m > Colle tion.syn roni e ap( as ap)?#.Done of t e a o/e

    %&planation : is t e orre t ans*er.

    Gas ap an e syn roni ed y ap m > Colle tions.syn roni e ap( as ap)?

    Question - #

    What is the output for the below code ?

    i port ja a . util . @in ed@isti port ja a . util . Rueue

    public class 5est { public static oid ain ( *trin ... ar s ) {

    Rueue G *trin E Q / new @in ed@ist G *trin E()

  • 8/10/2019 Final Set- Final Check SCJP Question

    33/41

    Q . add ( 9new or 9 ) Q . add ( 9ca9 ) Q . add ( 9te&as9 ) show ( Q ) "

    public static oid show ( Rueue Q ) { Q . add ( new Dnte er ( 11 )) while (MQ . is= pt ( ) ) * ste . out . print ( Q . poll () + 9 9 ) "

    "

    1.Compile error : 7nte3er an t add2.ne*yor+ a te&as 11!.ne*yor+ a te&as

    #.Done of t e a o/e

    %&planation :' is t e orre t ans*er.| N *as ori3inally de lared as Queue Strin3R, 'ut in s o*() met od it is passed asan untyped Queue. not in3 in t e ompiler or J< pre/ents us from addin3 an7nte3er after t at.

    7f t e s o* met od si3nature is pu li stati /oid s o*(Queue Strin3R N) t anyou an t add 7nte3er, ;nly Strin3 allo*ed. 'ut pu li stati /oid s o*(Queue N) isuntyped Queue so you an add 7nte3er.3 poll() 4etrie/es and remo/es t e ead of t is Nueue, or returns null if t is Nueue is

    empty.

    Question - $0Aill t e 3ap

    public class 5est {

    public static oid main ( *trin ... args ) throws =&ception { @in ed@ist G *trin E lst / new @in ed@ist G *trin E()

    lst . add ( "one" ) lst . add ( "two" ) * ste . out . println ( lst .)

    ""

    Fse t e follo*in3 fra3ments ero or many times3et7nde&()

    add7nde&()

    poll()

    Question - $1

    What is the output for the bellow code ?

  • 8/10/2019 Final Set- Final Check SCJP Question

    34/41

    i port ja a . util . Dteratori port ja a . util . *eti 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 ()) * ste . out . print ( itr . ne&t () + 9 9 )

    "

    "

    1.Compile error 2.4untime %& eption!.5 #.Done of t e a o/e

    %&planation :' is t e orre t ans*er.| it out 3eneri s, t e ompiler does not +no* * at type is appropriate for t isTreeSet, so it allo*s e/eryt in3 to ompile. 'ut at runtime e TreeSet *ill try to sort

    t e elements as t ey are added, and * en it tries to ompare an 7nte3er *it a Strin3 it*ill t ro* a ClassCast%& eption.| %& eption in t read KmainK =a/a.lan3.ClassCast%& eption: =a/a.lan3.Strin3 annot

    e ast to =a/a.lan3.7nte3er.

    Question - $2

    What is the output for the below code ?

    i port ja a . util . Dteratori 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 () ) { * ste . out . print ( it . ne&t () + 9 9 )

    "

  • 8/10/2019 Final Set- Final Check SCJP Question

    35/41

    "

    "

    1.one t ree t*o2.4untime %& eption!.one t ree t*o one#.one t*o t ree

    %&planation : is t e orre t ans*er.

    @ TreeSet assures no dupli ate entries.it *ill return elements in natural order, * i ,for Strin3s means alp a eti al.

    Question - $!

    Df we do

    Arra @ist lst / new Arra @ist ()

    What is the initial capacit of the Arra @ist lst ?

    1.102."!.1$#.12

    %&planation : is t e orre t ans*er.

    EE E Constru ts an empty list *it an initial apa ity of ten. E pu li rrayOist()L t is(10)? M

    Question - $#

    What is the output for the below code ?

    pac a e bean

    public class Abc { public static int inde&6 al / 1"

    pac a e co i port static bean . Abc . inde&6 al

    public class 5est1 {

    public static oid ain ( *trin ... ar s ) { * ste . out . println ( inde&6 al ) "

  • 8/10/2019 Final Set- Final Check SCJP Question

    36/41

    "

    1.102. ompile error, inde&8/al not defined!.Compile error at import stati ean. .inde&8/al?

    #.Done of t e a o/e

    %&planation : is t e orre t ans*er.

    T e stati import onstru t allo*s unNualified a ess to stati mem ers *it outin eritin3 from t e type ontainin3 t e stati mem ers. J2S% $.0 on*ards it allo*sstati import li+e import stati ean. .inde&8/al? and an e use dire tlySystem.out.println(inde&8/al)?

    Question - $$

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

    1.T e =ar ommand reates t e %T -7DA dire tory impli itly.2.T e =ar ommand reates t e D7A%ST. A file impli itly.!.T e =ar ommand *ould not pla e any of your files in %T -7DA dire tory.#. ll of t e a o/e are true

    %&planation :

    is t e orre t ans*er.

    ll statements are true.

    Question - $6

    Kou ha e a class file na e 5est . class inside ja aproject director .

    5est . ja a source code is H

    i port ja a . util . Nropertiesclass 5est { public static oid ain ( *trin - ar s ){ Nroperties p / * ste . etNroperties () * ste . out . println ( p . etNropert ( 9 e 19 )) ""

    ou ha e issued below co ands fro co and pro pt .

    cd ja aprojectja a ; C e 1 / alue1 5est

    What is the output ?

  • 8/10/2019 Final Set- Final Check SCJP Question

    37/41

    1./alue12.null!.4un su essfully ut no output#.4un fails - =a/a.lan3.DoClass efAound%rror: +ey1>/alue1

    %&planation : is t e orre t ans*er.

    - option , name>/alue pair must follo* immediately, no spa es allo*ed. 7n t is aset ere is spa e et*een - and +ey1>/alue1 So =a/a.lan3.DoClass efAound%rror:+ey1>/alue1.

    Question - $5

    What is the output for the below code ?

    public class A { public oid print4alue (){ * ste . out . println ( 9A9) ""

    public class % e&tends A { public oid print4alue (){ * ste . 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. "1 . "11. "

    1.2.'!.Compilation fails *it an error at line ##.Compilation fails *it an error at line "

    %&planation :' is t e orre t ans*er.

    instan eof operator is used for o =e t referen e /aria les to e + * et er an o =e t isof a parti ular type. 7n ne*

  • 8/10/2019 Final Set- Final Check SCJP Question

    38/41

    Question - $"

    What is the output for the below code ?

    1. public class 5est { 2. static int i / #3. public static oid ain ( *trin ... ar s ) { !. * ste . out . println ( i ++)#. * ste . out . println ( i )$. * ste . out . println (++ i )'. * ste . out . println (++ i +i ++)

    .0. "1 . "

    1.$ 6 5 16

    2.6 6 6 16!.6 6 5 16#.$ 6 6 16

    %&planation : is t e orre t ans*er.

    iBB : print /alue t en in rement (postfi& - in rement appens after t e /alue of t e/aria le is used) BBi : in rement t e print (prefi& - in rement appens efore t e/alue of t e /aria le is used)

    Question - $

    What is the output for the bellow code ?

    i port ja a . io . :onsole

    public class 5est { public static oid ain ( *trin ... ar s ) {

    :onsole con / * ste . console () boolean auth / false

    if ( con M/ null ) { int count / do { *trin una e / con . read@ine ( null ) char - pwd / con . readNassword ( 9=nter SsTs passwordH 9 ,una e )

    con . writer (). write ( 9UnUn9)

    " while (Mauth FF ++count G 3) "

  • 8/10/2019 Final Set- Final Check SCJP Question

    39/41

    "

    "

    1.DullPointer%& eption2.7t *or+s properly!.Compile %rror : Do readPass*ord() met od in Console lass.#.Done of t e a o/e

    %&planation : is t e orre t ans*er.

    passin3 a null ar3ument to any met od in Console lass *ill ause a DullPointer%& eption to e t ro*n.

    Question - 60

    What is the output for the below code ?

    i port ja a . util . Arra @isti port ja a . util . Dteratori port ja a . util . @isti port ja a . util . 8a i able*eti port ja a . util . 5ree*et

    public class 5est { public static oid ain ( *trin ... ar s ) {

    @ist G Dnte er E lst / new Arra @ist G Dnte er E() lst . add ( 3! ) lst . add ( $) lst . add ( 2) lst . add ( ) lst . add ( ' ) lst . add ( 1 )

    8a i able*et G Dnte er E n set / new 5ree*et ( lst ) * ste . out . println ( n set . head*et ( 1 , true ))

    "

    "

    1.Compile error : Do met od name li+e eadSet()2.2, 6, 5, ", 10!.2, 6, 5, "#.Done of t e a o/e

    %&planation :' is t e orre t ans*er.u eadSet(10) 4eturns t e elements elements are stri tly less t an 10.N eadSet(10,false) 4eturns t e elements elements are stri tly less t an 10.

  • 8/10/2019 Final Set- Final Check SCJP Question

    40/41

    eadSet(10,true) 4eturns t e elements elements are stri tly less t an or eNual to 10.

    Question - 61

    What is the output ?i port ja a . util . Arra @isti port ja a . util . @isti port ja a . util . 8a i able*eti port ja a . util . 5ree*et

    public class 5est { public static oid ain ( *trin ... ar s ) {

    @ist G Dnte er E lst / new Arra @ist G Dnte er E()

    lst . add ( 3! ) lst . add ( $) lst . add ( 2) lst . add ( ) lst . add ( ' ) lst . add ( 1 )

    8a i able*et G Dnte er E n set / new 5ree*et ( lst ) * ste . out . println ( n set . lower ( $)+ 9 9 +n set . hi her ( $)+ 99+ n set . lower ( 2))

    ""

    1.1 2 5 10 !# null2.2 5 null!.2 5 !##.1 2 5 10 !#

    %&planation :' is t e orre t ans*er. lo*er() 4eturns t e 3reatest element in t is set stri tly less t an t e 3i/en element,or null if t ere is no su element. i3 er() 4eturns t e least element in t is set stri tly 3reater t an t e 3i/en element,

    or null if t ere is no su element.

    Question - 62

    What will happen when ou atte pt to co pile and run the followincode ?

    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 (){

    . * ste . out . println ( 9run;test9 )

  • 8/10/2019 Final Set- Final Check SCJP Question

    41/41

    0. "1 . "

    1.run-test run-test2.run-test

    !.Compilation fails due to an error on line ##.Compilation fails due to an error on line 5

    %&planation : is t e orre t ans*er.

    t.run() Oe3al, ut does not start a ne* t read , it is li+e a met od all of lass Test'FT t.start() reate a t read and all run() met od.