0% found this document useful (0 votes)
133 views

Flow Control Java 6 Questions and Answers

This document contains 14 multiple choice questions about Java programming concepts like flow control, loops, arrays, and switch statements. Each question includes the relevant code snippet and asks the user to determine the output or identify any errors. The questions cover topics like for, while, and do-while loops; break and continue statements; using enums in switch; and ensuring case expressions are constants. The correct answers are also provided.

Uploaded by

krauserman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

Flow Control Java 6 Questions and Answers

This document contains 14 multiple choice questions about Java programming concepts like flow control, loops, arrays, and switch statements. Each question includes the relevant code snippet and asks the user to determine the output or identify any errors. The questions cover topics like for, while, and do-while loops; break and continue statements; using enums in switch; and ensuring case expressions are constants. The correct answers are also provided.

Uploaded by

krauserman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

FlowControl

FormoredetailsonSUNCertifications,visitJavaScjpDumps

Q:01Given:
10.publicclassBar{
11.staticvoidfoo(int...x){
12.//insertcodehere
13.}
14.}
Whichtwocodefragments,insertedindependentlyatline12,willallowtheclassto
compile?(Choose
two.)
A.foreach(x)System.out.println(z)
B.for(intz:x)System.out.println(z)
C.while(x.hasNext())System.out.println(x.next())
D.for(inti=0i<x.lengthi++)System.out.println(x[i])
Answer:B,D
Q:02ClicktheTaskbutton.

Solution:
int[]y={1,2,4,8,16,32}
System.out.print("output:")
for(intx:y){
System.out.println(x)
System.out.println("")

Q:03Given:
25.intx=12
26.while(x<10){
27.x
28.}
29.System.out.print(x)
Whatistheresult?
A.0
B.10
C.12
D.Line29willneverbereached.
Answer:C
Q:04Given:
11.publicstaticvoidmain(String[]args){
12.Objectobj=newint[]{1,2,3}
13.int[]someArray=(int[])obj
14.for(inti:someArray)System.out.print(i+"")
15.}
Whatistheresult?
A.123
B.Compilationfailsbecauseofanerrorinline12.
C.Compilationfailsbecauseofanerrorinline13.
D.Compilationfailsbecauseofanerrorinline14.
E.AClassCastExceptionisthrownatruntime.
Answer:A
Q:05Given:
11.publicstaticvoidmain(String[]args){
12.for(inti=0i<=10i++){
13.if(i>6)break
14.}
15.System.out.println(i)
16.}
Whatistheresult?
A.6
B.7
C.10
D.11
E.Compilationfails.
F.Anexceptionisthrownatruntime.
Answer:E
Q:06Given:
11.publicstaticvoidmain(String[]args){
12.Integeri=newInteger(1)+newInteger(2)
13.switch(i){
14.case3:System.out.println("three")break
15.default:System.out.println("other")break
16.}
17.}
Whatistheresult?
A.three
B.other
C.Anexceptionisthrownatruntime.
D.Compilationfailsbecauseofanerroronline12.
E.Compilationfailsbecauseofanerroronline13.
F.Compilationfailsbecauseofanerroronline15.
Answer:A
Q:07Given:
10.publicclassClassA{
11.publicvoidcount(inti){
12.count(++i)
13.}
14.}
And:
20.ClassAa=newClassA()
21.a.count(3)
Whichexceptionorerrorshouldbethrownbythevirtualmachine?
A.StackOverflowError
B.NullPointerException
C.NumberFormatException
D.IllegalArgumentException
E.ExceptionInInitializerError
Answer:A
Q:08Given:
35.intx=10
36.do{37.x
38.}while(x<10)
Howmanytimeswillline37beexecuted?
A.tentimes
B.zerotimes
C.onetoninetimes
D.morethantentimes
Answer:D
9.Giventhefollowingcode:
publicclassOrtegorumFunction{
publicintcomputeDiscontinuous(intx){
intr=1
r+=x
if((x>4)&&(x<10)){
r+=2*x
}else(x<=4){
r+=3*x
}else{
r+=4*x
}
r+=5*x
returnr
}
publicstaticvoidmain(String[]args){
OrtegorumFunctiono=newOrtegorumFunction()
System.out.println("OF(11)is:"+o.computeDiscontinuous(11))
}}
Whatistheresult?
A.OF(11)is:45
B.OF(11)is:56
C.OF(11)is:89
D.OF(11)is:111
E.Compilationfails.
F.Anexceptionisthrownatruntime.
Answer:
>Eiscorrect.Theifstatementisillegal.Theifelseelsemustbechangedtoifelse
ifelse,whichwouldresultinOF(11)is:111.
>A,B,C,D,andFareincorrectbasedontheabove.(Objective2.1)
10.Given:
1.classCrivitch{
2.publicstaticvoidmain(String[]args){
3.intx=0
4.//insertcodehere
5.do{}while(x++<y)
6.System.out.println(x)
7.}
8.}
Which,insertedatline4,producestheoutput12?
A.inty=x
B.inty=10
C.inty=11
D.inty=12
E.inty=13
F.Noneoftheabovewillallowcompilationtosucceed.
Answer:
>Ciscorrect.xreachesthevalueof11,atwhichpointthewhiletestfails.
xisthenincremented(afterthecomparisontest!),andtheprintln()methodruns.
>A,B,D,E,andFareincorrectbasedontheabove.
11.Given:
classSwill{
publicstaticvoidmain(String[]args){
Strings=""
switch(TimeZone.CST){
caseEST:s+="e"
caseCST:s+="c"
caseMST:s+="m"
default:s+="X"
casePST:s+="p"
}
System.out.println(s)
}
}
enumTimeZone{EST,CST,MST,PST}
Whatistheresult?
A.c
B.X
C.cm
D.cmp
E.cmXp
F.Compilationfails.
G.Anexceptionisthrownatruntime.
Answer:
>Eiscorrect.Itslegaltouseenumsinaswitch,andnormalswitchfallthroughlogic
appliesi.e.,onceamatchismadetheswitchhasbeenentered,andallremainingblockswill
runifnobreakstatementisencountered.Note:defaultdoesnthavetobelast.
>A,B,C,D,andFareincorrectbasedontheabove.
(Objective2.1)
12.Given:
classCircus{
publicstaticvoidmain(String[]args){
intx=9
inty=6
for(intz=0z<6z++,y){
if(x>2)x
label:
if(x>5){
System.out.print(x+"")
x
continuelabel
}
x
}
}
}
Whatistheresult?
A.8
B.87
C.876
D.Compilationfails.
E.Anexceptionisthrownatruntime.
Answer:
>Discorrect.Alabeledcontinueworksonlywithloops.Inthiscase,althoughthelabelis
legal,labelisnotalabelonaloopstatement,itsalabelonanifstatement.
>A,B,C,andEareincorrectbasedontheabove.(Objective2.2)
13.Given:
1.classLoopy{
2.publicstaticvoidmain(String[]args){
3.int[]x={7,6,5,4,3,2,1}
4.//insertcodehere
5.System.out.print(y+"")
6.}
7.}}
Which,insertedindependentlyatline4,compiles?(Chooseallthatapply.)
A.for(inty:x){
B.for(x:inty){
C.inty=0for(y:x){
D.for(inty=0,z=0z<x.lengthz++){y=x[z]
E.for(inty=0,intz=0z<x.lengthz++){y=x[z]
F.inty=0for(intz=0z<x.lengthz++){y=x[z]
Answer:
>A,D,andFarecorrect.Aisanexampleoftheenhancedforloop.DandFareexamples
ofthebasicforloop.
>Bisincorrectbecauseitsoperandsareswapped.Cisincorrectbecausetheenhanced
formustdeclareitsfirstoperand.Eisincorrectsyntaxtodeclaretwovariablesinafor
statement.(Objective2.2)
14.Given:
1.classRing{
2.finalstaticintx2=7
3.finalstaticIntegerx4=8
4.publicstaticvoidmain(String[]args){
5.Integerx1=5
6.Strings="a"
7.if(x1<9)s+="b"
8.switch(x1){
9.case5:s+="c"
10.casex2:s+="d"
11.casex4:s+="e"
12.}
13.System.out.println(s)
14.}
15.}
Whatistheresult?
A.abc
B.abcde
C.Compilationfailsdueonlytoanerroronline7.
D.Compilationfailsdueonlytoanerroronline8.
E.Compilationfailsdueonlytoanerroronline10.
F.Compilationfailsdueonlytoanerroronline11.
G.Compilationfailsduetoerrorsonmultiplelines.
Answer:
>Fiscorrect.Aswitchstatementrequiresitscaseexpressionstobeconstants,andwrapper
variables(evenfinalstaticones)arentconsideredconstants.Therestofthecodeiscorrect.
>A,B,C,D,E,andGareincorrectbasedontheabove.(Objective2.1)

You might also like