Mock Exam p
Thep prospectp ofp takingp thep OCPJPp 8p examp raisesp manyp questionsp inp yourp mind.
• “Whatp typesp ofp questionsp arep askedp inp thep exam?”
• “Whatpispthepformatpofpthepquestions?”
• “Howp hardp arep thep questions?”
• “HowpdopIpknowpifpI’mpreadyptoptakepthepexam?”
Thisp chapterp presentsp ap mockp examp thatp helpsp answerp thesep questions.p Usep thisp mockp examp asp ap mentalp
dipstickptopgaugephowppreparedpyoupareptoppasspthepOCPJPp8pexam.
ThepquestionspinpthispmockpexampcloselypmimicpthepactualpquestionspyoupwillpencounterponpyourpOC
PJPp8pexam.pForpinstance,pyoupwillpfindpthesepaspectspinpthepactualpOCPJPp8pexam:pthepquestionspwillpassu
mepthatpnecessarypimportpstatementsparepincluded;pmostpquestionspwillpcontainponlyprelevantpcodepsegment
sp (andp notp completep programs);p andp questionsp willp appearp inp randomp orderp (andp notp accordingptoptheps
equencepofpexamptopicspgivenpinpthepexampsyllabus).pInpthispmockpexam,pwephavepadoptedpapsimilarpapproa
chptopmakepthispmockpexampcloselypmimicpthepquestionpformatpinpthepactualpOCPJPp8pexam.
Beforep youp getp started,p takep ap printp outp ofp thep answerp sheetp givenp atp thep endp ofp thisp exam.p Takep thisp exam
paspifpitpwerepyourprealpOCPJPp8pexampbypsimulatingprealptestpconditions.pFindpapquietpplacepwherepyoupcanpt
akepthispmockpexampwithoutpinterruptionporpdistraction.pMarkpyourpstartpandpfinishptimes,pandpstoppifpyou
crossp thep examp timep limitp (2.5p hours).p Observep closed-
bookp rules:p dop notp consultp thep answerp keyp orp anyp otherpanypprint,phuman,porpwebpresourcespduringpthispmoc
kpexam.pCheckpthepanswersponlypafterpyoupcompletepthepexam.p Outp ofp 85p questions,p youp needp top answerp atp l
eastp 55p questionsp correctlyp top passp thisp examp (thep passingpscorepisp65%).
Bestpofpluck!
413
,CHAPTERp14p■pMOCKpEXAM
TIME:p2pHOURSp30pMINUTES No.p oFp QUESTIoNS:p 85
1. Whatpwillpbepthepresultpofpexecutingpthispcodepsegment?
Stream.of("acep ",p "jackp ",p "queenp ",p "kingp ",p "jokerp ")
.mapToInt(cardp ->p card.length())
.filter(lenp ->p lenp >p 3)
.peek(System.out::print)
.limit(2);
a) Thispcodepsegmentpprints:pjackp queenp kingp joker
b) Thispcodepsegmentpprints:pjackp queen
c) Thispcodepsegmentpprints:pkingp joker
d) Thispcodepsegmentpdoespnotpprintpanythingponpthepconsole
2. Considerpthepfollowingpsnippet:
intp chp =p 0;
tryp (FileReaderp inputFilep =p newp FileReader(file))p {
//p #1
System.out.print(p p (char)chp p );
}
}
Whichponepofpthepfollowingpstatementspcanpbepreplacedpwithpstatementp#
1psopthatpthepcontentspofpthepfileparepcorrectlypprintedponpthepconsolepandpthe
pprogrampterminates.
a) while(p (chp =p inputFile.read())p !=p null)p {
b) while(p (chp =p inputFile.read())p !=p -1)p {
c) while(p (chp =p inputFile.read())p !=p 0)p {
d) while(p (chp =p inputFile.read())p !=p EOF)p {
3. Whatpwillpbepthepoutputpofpthepfollowingpprogram?
classpBasep{
publicpBase()p{pSystem.out.println(
"Base");
}
}
classpDerivedpextendspBasep{ppu
blicpDerived()p{
System.out.println("Derived");
}
}
414
, CHAPTERp14p■pMOCKpEXAM
classpDeriDerivedpextendspDerivedp{
ppublicpDeriDerived()p{
System.out.println("DeriDerived");
}
}
classp Testp {
publicp staticp voidp main(Stringp []args)p {pD
erivedp bp =p newp DeriDerived();
}
}
a) BasepDeriv
edpDeriDeri
ved
b) DerivedpDe
riDerived
c) DeriDerivedp
DerivedpB
ase
d) DeriDerivedp
Derived
e) DeriDerived
4. Givenpthispcodepsegment:
finalp CyclicBarrierp barrierp =
newp CyclicBarrier(3,p ()p ->p System.out.println("Let'sp play"));
//p LINE_ONE
Runnableprp=p()p->p{
//pLINE_TWO
pSystem.out.println("Awaiting");
tryp{
barrier.await();
}pcatch(Exceptionpe)p{p/*pignorep*/p}
};
Threadpt1p=pnewpThread(r);p
Threadpt2p=pnewpThread(r);p
Threadp t3p =p newp Thread(r);
t1.start();
t2.start();
t3.start();
415
, CHAPTERp14p■pMOCKpEXAM
Choosepthepcorrectpoptionpbasedponpthispcodepsegment.
a) Thispcodepsegmentpresultspinpapcompilerperrorpinplinepmarkedpwithpthepc
ommentpLINE_ONE
b) Thispcodepsegmentpresultspinpapcompilerperrorpinplinepmarkedpwithpthepc
ommentpLINE_TWO
c) Thispcodepprints:
Let'sp play
d) Thispcodepprints:
AwaitingpA
waitingpA
waitingpLe
t'spplay
e) Thispcodepsegmentpdoespnotpprintpanythingponpthepconsole
5. Givenpthispclasspdefinition:
classp Pointp {
privatep intp xp =p 0,p y;
publicpPoint(intpx,pintpy)p{p
this.xp=px;
this.yp =p y;
}
//p DEFAULT_CTOR
}
WhichponepofpthepfollowingpdefinitionspofpthepPointpconstructorpcanpbepre
placedpwithoutpcompilerperrorspinpplacepofpthepcommentpDEFAULT_CTOR?
a) publicp Point()p {
this(0,p 0);
super();
}
b) publicp Point()p {
super();
this(0,p 0);
}
c) privatep Point()p {
this(0,p 0);
}
d) publicp Point()p {
this();
}
416