Sie sind auf Seite 1von 32

Exam

Name___________________________________

MULTIPLECHOICE.Choosetheonealternativethatbestcompletesthestatementoranswersthequestion.
1) Whichofthefollowingstatementsisfalse?
A) Aprivatemethodcannotbeaccessedbyaclassinadifferentpackage.
B) Aprotectedmethodcanbeaccessedbyasubclassinadifferentpackage.
C) Amethodwithnovisibilitymodifiercanbeaccessedbyaclassinadifferentpackage.
D) Apublicclasscanbeaccessedbyaclassfromadifferentpackage.

1)

2) Analyzethefollowingcode.

2)

//Program1:
publicclassTest{
publicstaticvoidmain(String[]args){
Objecta1=newA();
Objecta2=newA();
System.out.println(a1.equals(a2));
}
}
classA{
intx;
publicbooleanequals(Aa){
returnthis.x==a.x;
}
}
//Program2:
publicclassTest{
publicstaticvoidmain(String[]args){
Aa1=newA();
Aa2=newA();
System.out.println(a1.equals(a2));
}
}
classA{
intx;
publicbooleanequals(Aa){
returnthis.x==a.x;
}
}
A) Program1displaystrueandProgram2displaysfalse
B) Program1displaysfalseandProgram2displaysfalse
C) Program1displaysfalseandProgram2displaystrue
D) Program1displaystrueandProgram2displaystrue
1

3) ThegetValue()methodisoverriddenintwoways.Whichoneiscorrect?

3)

I:
publicclassTest{
publicstaticvoidmain(String[]args){
Aa=newA();
System.out.println(a.getValue());
}
}
classB{
publicStringgetValue(){
returnAnyobject;
}
}
classAextendsB{
publicObjectgetValue(){
returnAstring;
}
}
II:
publicclassTest{
publicstaticvoidmain(String[]args){
Aa=newA();
System.out.println(a.getValue());
}
}
classB{
publicObjectgetValue(){
returnAnyobject;
}
}
classAextendsB{
publicStringgetValue(){
returnAstring;
}
}
A) I

B) II

C) BothIandII

4) Polymorphismmeans________.
A) thatdatafieldsshouldbedeclaredprivate
B) thataclasscancontainanotherclass
C) thataclasscanextendanotherclass
D) thatavariableofsupertypecanrefertoasubtypeobject

D) Neither
4)

5) Object-orientedprogrammingallowsyoutoderivenewclassesfromexistingclasses.Thisis
called________.
A) inheritance
B) encapsulation
C) generalization
D) abstraction

5)

6) Youcanassign________toavariableofObject[]type.(Chooseallthatapply.)
A) newjava.util.Date[100]
B) newint[100]
C) newchar[100]
D) newString[100]
E) newdouble[100]

6)

7) Analyzethefollowingcode:

7)

Cylindercy=newCylinder(1,1);
Circlec=cy;
A) Thecodehasaruntimeerror.
B) Thecodeisfine.
C) Thecodehasacompileerror.

8) Giventhefollowingcode,findthecompileerror.(Chooseallthatapply.)

8)

publicclassTest{
publicstaticvoidmain(String[]args){
m(newGraduateStudent());
m(newStudent());
m(newPerson());
m(newObject());
}
publicstaticvoidm(Studentx){
System.out.println(x.toString());
}
}
classGraduateStudentextendsStudent{
}
classStudentextendsPerson{
publicStringtoString(){
returnStudent;
}
}
classPersonextendsObject{
publicStringtoString(){
returnPerson;
}
}
A) m(newObject())causesanerror
B) m(newPerson())causesanerror
C) m(newStudent())causesanerror
D) m(newGraduateStudent())causesanerror
9) Thevisibilityofthesemodifiersincreasesinthisorder:
A) private,protected,none(ifnomodifierisused),andpublic.
B) private,none(ifnomodifierisused),protected,andpublic.
C) none(ifnomodifierisused),protected,private,andpublic.
D) none(ifnomodifierisused),private,protected,andpublic.
10) Aclassdesignrequiresthataparticularmembervariablemustbeaccessiblebyanysubclasses
ofthisclass,butotherwisenotbyclasseswhicharenotmembersofthesamepackage.What
shouldbedonetoachievethis?
A) Thevariableshouldbemarkedpublic.
B) Thevariableshouldbemarkedprivateandanaccessormethodprovided.
C) Thevariableshouldbemarkedprivate.
D) Thevariableshouldhavenospecialaccessmodifier.
E) Thevariableshouldbemarkedprotected.

9)

10)

11) Whatmodifiershouldyouuseonthemembersofaclasssothattheyarenotaccessibleto
anotherclassinadifferentpackage,butareaccessibletoanysubclassesinanypackage?
A) private
B) public
C) protected
D) Usethedefaultmodifier.

11)

12) Analyzethefollowingcode:

12)

Circlec=newCircle(5);
Cylinderc=cy;
A) Thecodehasacompileerror.
B) Thecodeisfine.
C) Thecodehasaruntimeerror.
13) Analyzethefollowingcode:(Chooseallthatapply.)
publicclassTestextendsA{
publicstaticvoidmain(String[]args){
Testt=newTest();
t.print();
}
}
classA{
Strings;
A(Strings){
this.s=s;
}
publicvoidprint(){
System.out.println(s);
}
}
A) Theprogramcompiles,butithasaruntimeerrorduetotheconflictonthemethodname
print.
B) TheprogramwouldcompileifadefaultconstructorA(){}isaddedtoclassAexplicitly.
C) TheprogramdoesnotcompilebecauseTestdoesnothaveadefaultconstructorTest().
D) TheprogramhasanimplicitdefaultconstructorTest(),butitcannotbecompiled,because
itssuperclassdoesnothaveadefaultconstructor.Theprogramwouldcompileifthe
constructorintheclassAwereremoved.

13)

14) Whichofthefollowingstatementsaretrue?(Chooseallthatapply.)
A) Itisacompilationerroriftwomethodsdifferonlyinreturntypeinthesameclass.
B) Overloadingamethodistoprovidemorethanonemethodwiththesamenamebutwith
differentsignaturestodistinguishthem.
C) Astaticmethodcannotbeoverridden.Ifastaticmethoddefinedinthesuperclassis
redefinedinasubclass,themethoddefinedinthesuperclassishidden.
D) Aprivatemethodcannotbeoverridden.Ifamethoddefinedinasubclassisprivateinits
superclass,thetwomethodsarecompletelyunrelated.
E) Tooverrideamethod,themethodmustbedefinedinthesubclassusingthesame
signatureandcompatiblereturntypeasinitssuperclass.

14)

15) Giventhefollowingclassesandtheirobjects:

15)

classC1{};
classC2extendsC1{};
classC3extendsC1{};
C2c2=newC2();
C3c3=newC3();
Analyzethefollowingstatement:
c2=(C2)((C1)c3);
A) Thestatementiscorrect.
B) c3iscastintoc2successfully.
C) Youwillgetaruntimeerrorbecauseyoucannotcastobjectsfromsiblingclasses.
D) YouwillgetaruntimeerrorbecausetheJavaruntimesystemcannotperformmultiple
castinginnestedform.
16)

16) Analyzethefollowingcode:
publicclassTest{
publicstaticvoidmain(String[]args){
Strings=newString(WelcometoJava);
Objecto=s;
Stringd=(String)o;
}
}
A) WhenassigningstooinObjecto= s,anewobjectiscreated.
B) s,o,anddreferencethesameStringobject.
C) WhencastingotosinStringd= (String)o,thecontentsofoischanged.
D) WhencastingotosinStringd= (String)o,anewobjectiscreated.

17)

17) Analyzethefollowingcode:(Chooseallthatapply.)
publicclassTest{
publicstaticvoidmain(String[]args){
Objecta1=newA();
Objecta2=newObject();
System.out.println(a1);
System.out.println(a2);
}
}
classA{
intx;
publicStringtoString(){
returnAsxis+x;
}
}
A) WhenexecutingSystem.out.println(a1),thetoString()methodintheAclassisinvoked.
B) WhenexecutingSystem.out.println(a2),thetoString()methodintheObjectclassis
invoked.
C) Theprogramcannotbecompiled,becauseSystem.out.println(a1)iswronganditshould
bereplacedbySystem.out.println(a1.toString());
D) WhenexecutingSystem.out.println(a1),thetoString()methodintheObjectclassis
invoked.
18) TheequalsmethodisdefinedintheObjectclass.Whichofthefollowingiscorrecttooverride
itintheStringclass?
A) publicbooleanequals(Objectother)
B) publicstaticbooleanequals(Stringother)
C) publicbooleanequals(Stringother)
D) publicstaticbooleanequals(Objectother)

18)

19) WhichofthefollowingareJavakeywords?
A) cast
B) instanceOf

19)
C) casting

D) instanceof

20) Whichofthefollowingclassescannotbeextended?
A) classA{privateA();}
B) finalclassA{}
C) classA{}
D) classA{protectedA();}

20)

21) SwingcomponentsthatdontrelyonnativeGUIarereferredtoas________.
A) GUIcomponents
B) non-GUIcomponents
C) heavyweightcomponents
D) lightweightcomponents

21)

22) CanyouusethesetToolTipmethodtosetatooltipfor________?(Chooseallthatapply.)
A) JButton
B) JLabel
C) Component
D) JComponent
E) Container

22)

23) ________createsacolorobject.(Chooseallthatapply.)
A) newColor(255,255,255)
B) newColor(1,2,3)
C) newColor(0,266,0)
D) newColor(0,0,0)

23)

24) Analyzethefollowingcode:

24)

importjavax.swing.*;
publicclassTestextendsJFrame{
privateJButtonjbtOK=newJButton(OK);

publicstaticvoidmain(String[]args){
//Createaframeandsetitsproperties
JFrameframe=newTest();
frame.setTitle(LogicError);
frame.setSize(200,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
publicTest(){
jbtOK.setToolTipText(Thisisabutton);
add(newJButton(OK));
}
}
A) Thetooltiptextisdisplayedwhenyoumovethemouseonthebutton.
B) Thetooltiptextwillbedisplayedifyoureplaceadd(newJButton(OK))withadd(jbtOK
=newJButton(OK)).
C) ThetooltiptextwillbedisplayedifyouswapthetwolinesintheTestconstructor.
D) Thetooltiptextwillbedisplayedifyoureplaceadd(newJButton(OK))with
add(jbtOK).
25) Themethod________setstheforegroundcolortoyellowinJFramef.(Chooseallthatapply.)
A) f.setForeground(Color.YELLOW)
B) setForeground(Color.YELLOW)
C) setForeground(Color.yellow)
D) f.setForeGround(Color.yellow)
E) f.setForeground(Color.yellow)

25)

26) CanyouusethesetBordermethodtosetaborderfor________?(Chooseallthatapply.)
A) Container
B) JComponent
C) JButton
D) JLabel
E) Component

26)

27) Youcanusemethods________onanyinstanceofjava.awt.Component.(Chooseallthat
apply.)
A) setBackground
B) setBorder
C) getHeight
D) getBackground
E) getWidth

27)

28) SupposeaJFrameusestheGridLayout(2,2).Ifyouaddsixbuttonstotheframe,howmany
columnsaredisplayed?
A) 4
B) 2
C) 3
D) 1

28)

29) Analyzethefollowingcode.

29)

importjava.awt.*;
importjavax.swing.*;
publicclassTest{
publicstaticvoidmain(String[]args){
Componentc=newJButton(OK);
JFrameframe=newJFrame(MyFrame);
frame.add(c);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
A) YoucannotaddaSwingcomponentdirectlytoaJFrameusingadd(c)priortoJDK1.4,
butitisOKinJDK1.5.
B) YoucanonlyaddctoacontainerbecausecstypeisComponent.
C) YoucannotassignaJButtontoavariableofjava.awt.Component.
D) YoucannotcreateaJFrameusingnewJFrame(MyFrame).
30) Whatlayoutmanagershouldyouusesothateverycomponentoccupiesthesamesizeinthe
container?
A) anylayout
B) aBorderLayout
C) aFlowLayout
D) aGridLayout

30)

31) Tospecifyafonttobeboldanditalic,usethefontstylevalue________.
A) Font.ITALIC
B) Font.PLAIN
C) Font.BOLD+Font.ITALIC
D) Font.BOLD

31)

32) WhatisbesttodescribetherelationshipbetweenComponentandFont?
A) Aggregation
B) Composition
C) Association

32)
D) Inheritance

33) ToaddacomponentctoaJPanelp,use________.
A) p.add(c)
B) p.append(c)
C) p.insert(c)
D) p.getContentPane(c)

33)

34) SupposeaJFrameusestheGridLayout(0,2).Ifyouaddsixbuttonstotheframe,howmany
columnsaredisplayed?
A) 1
B) 4
C) 3
D) 2

34)

35) Whichofthefollowingclassesisaheavyweightcomponent?
A) JTextField
B) JFrame
C) JButton

35)

36) ThedefaultlayoutoutofacontentPaneinaJFrameis________.
A) BorderLayout
B) GridLayout
C) None
37) Whatisdisplayedontheconsolewhenrunningthefollowingprogram?
classTest{
publicstaticvoidmain(String[]args){
try{
method();
System.out.println(Afterthemethodcall);
}
catch(RuntimeExceptionex){
System.out.println(RuntimeException);
}
catch(Exceptionex){
System.out.println(Exception);
}
}
staticvoidmethod()throwsException{
try{
Strings=5.6;
Integer.parseInt(s);//CauseaNumberFormatException
inti=0;
inty=2/i;
System.out.println(WelcometoJava);
}
catch(NumberFormatExceptionex){
System.out.println(NumberFormatException);
throwex;
}
catch(RuntimeExceptionex){
System.out.println(RuntimeException);
}
}
10

D) JPanel
36)
D) FlowLayout
37)

}
A) TheprogramdisplaysNumberFormatExceptionfollowedbyRuntimeException.
B) TheprogramdisplaysNumberFormatExceptionfollowedbyAfterthemethodcall.
C) TheprogramdisplaysNumberFormatExceptiontwice.
D) Theprogramhasacompilationerror.
38) Analyzethefollowingcode:

38)

classTest{
publicstaticvoidmain(String[]args){
try{
intzero=0;
inty=2/zero;
try{
Strings=5.6;
Integer.parseInt(s);//CauseaNumberFormatException
}
catch(Exceptione){
}
}
catch(RuntimeExceptione){
System.out.println(e);
}
}
}
A) Atry-catchblockcannotbeembeddedinsideanothertry-catchblock.
B) TheprogramhasacompilationerrorbecauseExceptionappearsbefore
RuntimeException.
C) Agoodprogrammingpracticeistoavoidnestingtry-catchblocks,becausenestingmakes
programsdifficulttoread.Youcanrewritetheprogramusingonlyonetry-catchblock.
D) Noneoftheabove.

11

39)

39) Analyzethefollowingcode:
classTest{
publicstaticvoidmain(String[]args)
throwsMyException{
System.out.println(WelcometoJava);
}
}
classMyExceptionextendsError{
}
A) YoushouldnotdeclareaclassthatextendsError,becauseErrorraisesafatalerrorthat
terminatestheprogram.
B) Youcannotdeclareanexceptioninthemainmethod.
C) Theprogramhasacompilationerror.
D) Youdeclaredanexceptioninthemainmethod,butyoudidnotthrowit.
40) Aninstanceof________describessystemerrors.Ifthistypeoferroroccurs,thereislittleyou
candobeyondnotifyingtheuserandtryingtoterminatetheprogramgracefully.
A) Error
B) NumberFormatException
C) Exception
D) Throwable
E) RuntimeException

12

40)

41) Whatisdisplayedontheconsolewhenrunningthefollowingprogram?

41)

classTest{
publicstaticvoidmain(String[]args){
try{
method();
System.out.println(Afterthemethodcall);
}
catch(NumberFormatExceptionex){
System.out.println(NumberFormatException);
}
catch(RuntimeExceptionex){
System.out.println(RuntimeException);
}
}
staticvoidmethod(){
Strings=5.6;
Integer.parseInt(s);//CauseaNumberFormatException
inti=0;
inty=2/i;
System.out.println(WelcometoJava);
}
}
A) TheprogramdisplaysNumberFormatExceptionfollowedbyAfterthemethodcall.
B) TheprogramdisplaysNumberFormatException.
C) TheprogramdisplaysNumberFormatExceptionfollowedbyRuntimeException.
D) Theprogramhasacompilationerror.
E) TheprogramdisplaysRuntimeException.
42) ThefollowingcodecausesJavatothrow________.
intnumber=Integer.MAX_VALUE+1;
A) Exception
B) RuntimeException
C) Throwable
D) Error
E) noexceptions

13

42)

43) Whatexceptiontypedoesthefollowingprogramthrow?

43)

publicclassTest{
publicstaticvoidmain(String[]args){
Objecto=newObject();
Stringd=(String)o;
}
}
A) ArithmeticException
B) StringIndexOutOfBoundsException
C) ArrayIndexOutOfBoundsException
D) Noexception
E) ClassCastException
44) WhichofthefollowingisnotanadvantageofJavaexceptionhandling?
A) Javaseparatesexceptionhandlingfromnormalprocessingtasks.
B) Exceptionhandlingmakesitpossibleforthecallerscallertohandletheexception.
C) Exceptionhandlingimprovesperformance.
D) Exceptionhandlingsimplifiesprogrammingbecausetheerror-reportingand
error-handlingcodecanbeplacedatthecatchblock.

44)

45) Whatexceptiontypedoesthefollowingprogramthrow?

45)

publicclassTest{
publicstaticvoidmain(String[]args){
Objecto=null;
System.out.println(o);
}
}
A) NullPointerException
B) ArrayIndexOutOfBoundsException
C) ArithmeticException
D) StringIndexOutOfBoundsException
E) Noexception

14

46) Whatexceptiontypedoesthefollowingprogramthrow?

46)

publicclassTest{
publicstaticvoidmain(String[]args){
Objecto=null;
System.out.println(o.toString());
}
}
A) ClassCastException
B) NullPointerException
C) ArithmeticException
D) ArrayIndexOutOfBoundsException
E) StringIndexOutOfBoundsException
47)

47) Analyzethefollowingprogram.
classTest{
publicstaticvoidmain(String[]args){
try{
Strings=5.6;
Integer.parseInt(s);//CauseaNumberFormatException
inti=0;
inty=2/i;
System.out.println(WelcometoJava);
}
catch(Exceptionex){
System.out.println(ex);
}
}
}
A) Theprogramcompilesandrunswithoutexceptions.
B) Anexceptionisraiseddueto2/i;
C) AnexceptionisraisedduetoInteger.parseInt(s);
D) Theprogramhasacompilationerror.

15

48) Whatisdisplayedontheconsolewhenrunningthefollowingprogram?

48)

classTest{
publicstaticvoidmain(String[]args){
try{
System.out.println(WelcometoJava);
inti=0;
inty=2/i;
System.out.println(WelcometoHTML);
}
finally{
System.out.println(Thefinallyclauseisexecuted);
}
}
}
A) Theprogramdisplaysthreelines:WelcometoJava,WelcometoHTML,Thefinallyclause
isexecuted.
B) WelcometoJavafollowedbyThefinallyclauseisexecutedinthenextline.
C) WelcometoJava.
D) Noneoftheabove.
49)

49) Analyzethefollowingcode.
1.publicclassTest{
2.publicstaticvoidmain(String[]args){
3.Fruit[]fruits={newFruit(2),newFruit(3),newFruit(1)};
4.java.util.Arrays.sort(fruits);
5.}
6.}
classFruit{
privatedoubleweight;

publicFruit(doubleweight){
this.weight=weight;
}
}
A) TheprogramhasaruntimeerroronLine4becausetheFruitclassdoesnotimplement
thejava.lang.ComparableinterfaceandtheFruitobjectsarenotcomparable.
B) TheprogramhasacompileerrorbecausetheFruitclassdoesnothaveadefault
constructor.
C) TheprogramhasacompileerroronLine4becausetheFruitclassdoesnotimplementthe
java.lang.ComparableinterfaceandtheFruitobjectsarenotcomparable.
D) TheprogramhasaruntimeerroronLine3becausetheFruitclassdoesnothaveadefault
constructor.

16

50) TheGeometricObjectandCircleclassesaredefinedinChapter11.Analyzethefollowingcode.
(Chooseallthatapply.)

50)

publicclassTest{
publicstaticvoidmain(String[]args){
GeometricObjectx=newCircle(3);
GeometricObjecty=(Circle)(x.clone());
System.out.println(x);
System.out.println(y);
}
}
A) Theprogramhasacompileerrorbecausetheclone()methodisprotectedintheObject
class.
B) ToenableaCircleobjecttobecloned,theCircleclasshastooverridetheclone()method
andimplementthejava.lang.Cloneableinterface.
C) Afteryouoverridetheclone()methodandmakeitpublicintheCircleclass,theproblem
cancompileandrunjustfine,butyisnullifCircledoesnotimplementtheCloneable
interface.
D) IfGeometricObjectimplementsCloneableandCircleoverridestheclone()method,the
clone()methodwillworkfinetocloneCircleobjects.
51) Whichofthefollowingclassesareimmutable?(Chooseallthatapply.)
A) Double
B) String
C) BigInteger
D) Integer
E) BigDecimal

51)

52) Whichofthefollowingstatementswillconvertastringsintoadoublevalued?
A) d=Double.valueOf(s).doubleValue();
B) d= Double.parseDouble(s);
C) d=(newDouble(s)).doubleValue();
D) Alloftheabove.

52)

53) Whichofthefollowingstatementsarecorrect?(Chooseallthatapply.)
A) Doublei=4.5;
B) Integeri= 4.5;
C) Numberi=4.5;
D) Objecti= 4.5;

53)

54) AssumeCalendarcalendar=newGregorianCalendar().________returnstheweekoftheyear.
A) calendar.get(Calendar.MONTH_OF_YEAR)
B) calendar.get(Calendar.MONTH)
C) calendar.get(Calendar.WEEK_OF_MONTH)
D) calendar.get(Calendar.WEEK_OF_YEAR)

54)

17

55) Whichofthefollowingstatementsregardingabstractmethodsaretrue?(Chooseallthat
apply.)
A) Anabstractclasscanbeusedasadatatype.
B) Anabstractclasscanhaveinstancescreatedusingtheconstructoroftheabstractclass.
C) Asubclasscanoverrideaconcretemethodinasuperclasstodeclareitabstract.
D) Anabstractclasscanbeextended.
E) Asubclassofanon-abstractsuperclasscanbeabstract.

55)

56) InJDK1.5,analyzethefollowingcode.(Chooseallthatapply.)

56)

Line1:Integer[]intArray={1,2,3};
Line2:inti=intArray[0]+intArray[1];
Line3:intj=i+intArray[2];
Line4:doubled=intArray[0];
A) Line4isOK.AnintvaluefromintArray[0]objectisassignedtoadoublevariabled.
B) ItisOKtoassign1,2,3toanarrayofIntegerobjectsinJDK1.5.
C) ItisOKtomixanintvaluewithanIntegerobjectinanexpressioninLine3.
D) ItisOKtoautomaticallyconvertanIntegerobjecttoanintvalueinLine2.
57)

57) Theprintoutfromthefollowingcodeis________.
java.util.ArrayListlist=newjava.util.ArrayList();
list.add(NewYork);
java.util.ArrayListlist1=(java.util.ArrayList)(list.clone());
list.add(Atlanta);
list1.add(Dallas);
System.out.println(list1);
A) [NewYork,Dallas]
C) [NewYork]

B) [NewYork,Atlanta]
D) [NewYork,Atlanta,Dallas]
58)

58) Theprintoutfromthefollowingcodeis________.
java.util.ArrayListlist=newjava.util.ArrayList();
list.add(NewYork);
java.util.ArrayListlist1=list;
list.add(Atlanta);
list1.add(Dallas);
System.out.println(list1);
A) [NewYork,Dallas]
C) [NewYork,Atlanta]

B) [NewYork]
D) [NewYork,Atlanta,Dallas]

59) TheheaderforthepaintComponentmethodis________.
A) protectedvoidpaintComponent()
B) privatevoidpaintComponent(Graphicsg)
C) protectedvoidpaintComponent(Graphicsg)
D) publicvoidpaintComponent(Graphicsg)

18

59)

60)

60) Analyzethefollowingcode.
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassTest1extendsJFrame{
publicTest1(){
add(newMyCanvas());
}
publicstaticvoidmain(String[]args){
JFrameframe=newTest1();
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
classMyCanvasextendsJPanel{
privateStringmessage;
publicvoidsetMessage(Stringmessage){
this.message=message;
}
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
g.drawString(message,20,20);
}
}
A) TheprogramhasaNullPointerExceptionsincemessageisnullwhen
g.drawString(message,20,20)isexecuted.
B) Theprogramrunsfineanddisplaysnothingsinceyouhavenotsetastringvalue.
C) TheprogramhasacompileerrorbecausenewTest1()isassignedtoframe.
D) TheprogramwoulddisplayWelcometoJava!ifyoureplacenewMyCanvas()bynew
MyCanvas(WelcometoJava!).
61) Invoking________returnsthewidthofthestringinaFontMetricsobjectfm.
A) fm.stringWidth(s)
B) getLength(s)
C) fm.getWidth(s)
D) fm.getHeight(s)

61)

62) Whichofthefollowingstatementsaretrue?(Chooseallthatapply.)
A) OnceaGUIcomponentisvisible,getGraphics()returnstheobject.
B) TheGraphicsobjectisautomaticallycreatedforeachvisibleGUIcomponent.
C) IfaGUIcomponentisnotvisible,getGraphics()returnsnull.
D) EachGUIcomponentcontainsaGraphicsobjectthatcanbeobtainedusinggetGraphics()
method.

62)

19

63) Torepaintgraphics,invoke________onaSwingcomponent.
A) repaint()
B) update()
C) init()
D) paintComponent()

63)

64) Analyzethefollowingcode.

64)

importjava.awt.*;
importjavax.swing.*;
publicclassTestextendsJFrame{
publicTest(){
add(newMyDrawing(WelcometoJava!));
}
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame();
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
classMyDrawingextendsJPanel{
Stringmessage;
publicMyDrawing(Stringmessage){
this.message=message;
}
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
g.drawString(message,20,20);
}
}
A) TheprogramwoulddisplayWelcometoJava!ifnewJFrame()isreplacedbyTest().
B) TheprogramwoulddisplayWelcometoJava!ifnewJFrame()isreplacedbynew
Test(MyFrame).
C) TheprogramrunsfineanddisplaysWelcometoJava!
D) TheprogramwoulddisplayWelcometoJava!ifnewJFrame()isreplacedbynewTest().
65) Whichofthefollowingstatementsaretrue?(Chooseallthatapply.)
A) YoucancreateaFontMetricusingnewFontMetrics().
B) Youcanobtaintheleading,ascent,descent,andheightforafontfromaFontMetrics
object.
C) Afontdeterminesthefontmetrics.
D) YoucanobtainaFontMetricsfromaFontobjectusingthegetFontMetrics()method.

20

65)

66) ThefollowingarethemethodstoobtainfontpropertiesinaFontMetricsobjectfm.(Chooseall
thatapply.)
A) fm.getAscent()
B) fm.getHeight()
C) fm.getLeading()
D) fm.getDescent()

66)

67) Inheritancemeans________.
A) thatavariableofsupertypecanrefertoasubtypeobject
B) thatdatafieldsshouldbedeclaredprivate
C) thataclasscanextendanotherclass
D) thataclasscancontainanotherclass

67)

68) Whatmodifiershouldyouuseonaclasssothataclassinthesamepackagecanaccessitbuta
classinadifferentpackagecannotaccessit?
A) private
B) public
C) protected
D) Usethedefaultmodifier.

68)

69) SupposeArrayListxcontainstwostrings[Beijing,Singapore].Whichofthefollowingmethods
willcausethelisttobecome[Beijing,Chicago,Singapore]?
A) x.add(0,Chicago)
B) x.add(Chicago)
C) x.add(2,Chicago)
D) x.add(1,Chicago)

69)

70) SupposeArrayListxcontainstwostrings[Beijing,Singapore].Whichofthefollowingmethod
willcausethelisttobecome[Beijing]?(Chooseallthatapply.)
A) x.remove(0)
B) x.remove(2)
C) x.remove(1)
D) x.remove(Singapore)

70)

71) Whatistheoutputofthefollowingcode:

71)

publicclassTest{
publicstaticvoidmain(String[]args){
Strings1=newString(Java);
Strings2=newString(Java);
System.out.print((s1==s2)++(s1.equals(s2)));
}
}
A) falsetrue

B) truetrue

C) falsefalse

21

D) truefalse

72)

72) WhatistheoutputofrunningclassC?
classA{
publicA(){
System.out.println(
ThedefaultconstructorofAisinvoked);
}
}
classBextendsA{
publicB(){
System.out.println(
ThedefaultconstructorofBisinvoked);
}
}
publicclassC{
publicstaticvoidmain(String[]args){
Bb=newB();
}
}
A) ThedefaultconstructorofAisinvokedThedefaultconstructorofBisinvoked
B) ThedefaultconstructorofBisinvokedThedefaultconstructorofAisinvoked
C) ThedefaultconstructorofBisinvoked
D) ThedefaultconstructorofAisinvoked
E) Nothingdisplayed
73) ________arereferredtoasheavyweightcomponents.
A) GUIcomponents
B) AWTcomponents
C) Swingcomponents
D) Non-GUIcomponents

73)

74) Whichofthefollowingstatementsisforplacingtheframesupperleftcornerto(200,100)?
A) frame.setLocation(200,200)
B) frame.setLocation(100,200)
C) frame.setLocation(200,100)
D) frame.setLocation(100,100)

74)

75) Whichofthefollowingaresubclassesofjava.awt.Component?(Chooseallthatapply.)
A) Containerclasses
B) Layoutmanagers
C) HelperclassessuchasColorandFont
D) Swinguserinterfaceclasses

75)

76) WhatshouldyouusetopositionaButtonwithinanapplicationFramesothatthesizeofthe
ButtonisNOTaffectedbytheFramesize?
A) theNorthorSouthareaofaBorderLayout
B) theEastorWestareaofaBorderLayout
C) aGridLayout
D) aFlowLayout
E) thecenterareaofaBorderLayout

76)

22

77)

77) Howmanyframesaredisplayed?
importjavax.swing.*;
publicclassTestextendsJFrame{
publicstaticvoidmain(String[]args){
JFramef1=newTest();
JFramef2=newTest();
JFramef3=newTest();
f1.setVisible(true);
f2.setVisible(true);
f3.setVisible(true);
}
}
A) 0.

B) 1.

C) 2.

78) Whatisdisplayedontheconsolewhenrunningthefollowingprogram?
classTest{
publicstaticvoidmain(String[]args){
try{
method();
System.out.println(Afterthemethodcall);
}
catch(RuntimeExceptionex){
System.out.println(RuntimeException);
}
catch(Exceptionex){
System.out.println(Exception);
}
}
staticvoidmethod()throwsException{
try{
Strings=5.6;
Integer.parseInt(s);//CauseaNumberFormatException
inti=0;
inty=2/i;
System.out.println(WelcometoJava);
}
catch(RuntimeExceptionex){
System.out.println(RuntimeException);
}
catch(Exceptionex){
System.out.println(Exception);
}
}
}

23

D) 3.
78)

A) TheprogramdisplaysExceptionfollowedbyRuntimeException.
B) TheprogramdisplaysRuntimeExceptiontwice.
C) TheprogramdisplaysRuntimeExceptionfollowedbyAfterthemethodcall.
D) TheprogramdisplaysExceptiontwice.
E) Theprogramhasacompilationerror.
79) Whatisdisplayedontheconsolewhenrunningthefollowingprogram?
classTest{
publicstaticvoidmain(String[]args){
try{
System.out.println(WelcometoJava);
inti=0;
inty=2/i;
System.out.println(WelcometoJava);
}
catch(RuntimeExceptionex){
System.out.println(WelcometoJava);
}
finally{
System.out.println(Endoftheblock);
}
}
}
A) TheprogramdisplaysWelcometoJavathreetimes.
B) TheprogramdisplaysWelcometoJavatwotimes.
C) TheprogramdisplaysWelcometoJavathreetimesfollowedbyEndoftheblock.
D) TheprogramdisplaysWelcometoJavatwotimesfollowedbyEndoftheblock.

24

79)

80) Whatisdisplayedontheconsolewhenrunningthefollowingprogram?

80)

classTest{
publicstaticvoidmain(String[]args){
try{
System.out.println(WelcometoJava);
inti=0;
doubley=2.0/i;
System.out.println(WelcometoHTML);
}
finally{
System.out.println(Thefinallyclauseisexecuted);
}
}
}
A) WelcometoJava.
B) Theprogramdisplaysthreelines:WelcometoJava,WelcometoHTML,Thefinallyclause
isexecuted.
C) WelcometoJavafollowedbyThefinallyclauseisexecutedinthenextline.
D) Noneoftheabove.
81) Aninstanceof________describestheerrorscausedbyyourprogramandexternal
circumstances.Theseerrorscanbecaughtandhandledbyyourprogram.
A) NumberFormatException
B) Error
C) Exception
D) Throwable
E) RuntimeException

81)

82) WhatistheoutputofrunningclassTest?

82)

publicclassTest{
publicstaticvoidmain(String[]args){
newCircle9();
}
}
publicabstractclassGeometricObject{
protectedGeometricObject(){
System.out.print(A);
}
protectedGeometricObject(Stringcolor,booleanfilled){
System.out.print(B);
}
}
publicclassCircle9extendsGeometricObject{
/**Defaultconstructor*/
publicCircle9(){
25

this(1.0);
System.out.print(C);
}
/**Constructcirclewithaspecifiedradius*/
publicCircle9(doubleradius){
this(radius,white,false);
System.out.print(D);
}
/**Constructacirclewithspecifiedradius,filled,andcolor*/
publicCircle9(doubleradius,Stringcolor,booleanfilled){
super(color,filled);
System.out.print(E);
}
}
A) BACD

B) CBAE

C) ABCD

D) BEDC

E) AEDC
83)

83) Analyzethefollowingcode.
publicclassTest{
publicstaticvoidmain(String[]args){
Numberx=newInteger(3);
System.out.println(x.intValue());
System.out.println((Integer)x.compareTo(newInteger(4)));
}
}
A) TheprogramhasacompileerrorbecauseintValueisanabstractmethodinNumber.
B) Theprogramcompilesandrunsfine.
C) TheprogramhasacompileerrorbecausexcannotbecastintoInteger.
D) TheprogramhasacompileerrorbecauseanIntegerinstancecannotbeassignedtoa
Numbervariable.
E) Theprogramhasacompileerrorbecausethememberaccessoperator(.)isexecuted
beforethecastingoperator.
84) Whichofthefollowingstatementswillconvertastringsintoiofinttype?(Chooseallthat
apply.)
A) i=Integer.parseInt(s);
B) i=(newInteger(s)).intValue();
C) i=(int)(Double.parseDouble(s));
D) i=Integer.valueOf(s);
E) i=Integer.valueOf(s).intValue();

84)

85) Whichofthefollowingstatementsarecorrect?(Chooseallthatapply.)
A) Youcansetanimageonabutton,buttheimageisnotresizable.
B) YoucandrawanimageonaGUIcomponentusingthedrawImagemethodinthe
Graphicsobject.Thisimageisresizable.
C) Youcansetanimageonalabel,buttheimageisnotresizable.

85)

26

86) Youshouldoverridethe________methodtodrawthingsonaSwingcomponent.
A) init()
B) update()
C) repaint()
D) paintComponent()

86)

87) Todrawgraphics,itisbettertodeclareaclassthatextends________andoverridethe
paintComponentmethod.
A) JPanel
B) JButton
C) JLabel
D) JComponent

87)

88) Whatistheoutputofthefollowingcode:

88)

publicclassTest{
publicstaticvoidmain(String[]args){
Objecto1=newObject();
Objecto2=newObject();
System.out.print((o1==o2)++(o1.equals(o2)));
}
}
A) falsefalse

B) truetrue

C) truefalse

D) falsetrue

89) Whichofthestatementsregardingthesuperkeywordisincorrect?
A) Youcanusesuper.super.ptoinvokeamethodinsuperclasssparentclass.
B) Youcanusesupertoinvokeasuperclassmethod.
C) Youcanusesupertoinvokeasuperclassconstructor.
D) Youcannotinvokeamethodinsuperclasssparentclass.

89)

90) Analyzethefollowingcode.

90)

importjava.awt.*;
importjavax.swing.*;
publicclassTest{
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame(MyFrame);
frame.add(newJButton(OK));
frame.add(newJButton(Cancel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
A) BothbuttonOKandbuttonCancelaredisplayedandbuttonOKisdisplayedontheright
sideofbuttonOK.
B) BothbuttonOKandbuttonCancelaredisplayedandbuttonOKisdisplayedontheleft
sideofbuttonOK.
C) OnlybuttonOKisdisplayed.
D) OnlybuttonCancelisdisplayed.

27

91) WhatisbesttodescribetherelationshipbetweenComponentandColor?
A) Composition
B) Inheritance
C) Aggregation

91)
D) Association

92) AJavaexceptionisaninstanceof________.
A) RuntimeException
B) Throwable
C) Exception
D) Error
E) NumberFormatException

92)

93) Thejava.lang.ComparableinterfaceisintroducedinChapter11.Analyzethefollowingcode:
(Chooseallthatapply.)

93)

publicclassTest1{
publicObjectmax(Objecto1,Objecto2){
if((Comparable)o1.compareTo(o2)>=0){
returno1;
}
else{
returno2;
}
}
}
A) TheprogramhasacompileerrorbecauseyoucannotcastanObjectinstanceo1into
Comparable.
B) Theprogramwouldcompileif((Comparable)o1.compareTo(o2)>= 0)isreplacedby
(((Comparable)o1).compareTo(o2)>=0).
C) Theprogramhasacompileerrorbecauseo1isanObjectinstanceanditdoesnothavethe
compareTomethod.
D) TheprogramhasacompileerrorbecauseTest1doesnothaveamainmethod.
94) GivenaGraphicsobjectg,todrawafilledarcwithradius20centeredat(50,50)andstart
angle0andspanningangle90,youuse________.
A) g.fillArc(50,50,40,40,0,90)
B) g.fillArc(30,30,40,40,0,Math.toRadian(90))
C) g.fillArc(50,50,20,20,0,90)
D) g.fillArc(30,30,40,40,0,90)
E) g.fillArc(50,50,40,40,0,Math.toRadian(90))

94)

95) Compositionmeans________.
A) thataclasscancontainanotherclass
B) thatdatafieldsshouldbedeclaredprivate
C) thataclasscanextendanotherclass
D) thatavariableofsupertypecanrefertoasubtypeobject

95)

96) Whatisbesttodescribetherelationshipbetweenacontainerandalayoutmanager?
A) Inheritance
B) Aggregation
C) Association
D) Composition

96)

28

97) Whatexceptiontypedoesthefollowingprogramthrow?

97)

publicclassTest{
publicstaticvoidmain(String[]args){
Strings=abc;
System.out.println(s.charAt(3));
}
}
A) ClassCastException
B) ArrayIndexOutOfBoundsException
C) StringIndexOutOfBoundsException
D) Noexception
E) ArithmeticException
98) Analyzethefollowingcode.

98)

publicclassTest{
publicstaticvoidmain(String[]args){
Numberx=newInteger(3);
System.out.println(x.intValue());
System.out.println(x.compareTo(newInteger(4)));
}
}
A) TheprogramhasacompileerrorbecauseanIntegerinstancecannotbeassignedtoa
Numbervariable.
B) Theprogramcompilesandrunsfine.
C) TheprogramhasacompileerrorbecausexdoesnothavethecompareTomethod.
D) TheprogramhasacompileerrorbecauseintValueisanabstractmethodinNumber.

29

99)

99) Analyzethefollowingcode.
importjava.awt.*;
importjavax.swing.*;
publicclassTest{
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame(MyFrame);
frame.add(newMyDrawing(WelcometoJava!));
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setVisible(true);
}
}
classMyDrawingextendsJPanel{
Stringmessage;
publicMyDrawing(Stringmessage){
this.message=message;
}
publicvoidpaintcomponent(Graphicsg){
super.paintComponent(g);
g.drawString(message,20,20);
}
}
A) TheprogramrunsfineanddisplaysWelcometoJava!
B) Theprogramhasacompileerrorbecausethepaintcomponentshouldbespelledas
paintComponent.
C) ItisaruntimeerrortoinvokethesetVisible(true)twice.
D) Theprogramruns,butitdoesnotdisplaythemessage.
E) Theprogramhasaruntimeerrorbecausethepaintcomponentshouldbespelledas
paintComponent.
100) Whichofthefollowingstatementsaretrue?(Chooseallthatapply.)
A) Dynamicbindingcanapplytoinstancemethods.
B) Youcanalwayspassaninstanceofasubclasstoaparameterofitssuperclasstype.This
featureisknownaspolymorphism.
C) Thecompilerfindsamatchingmethodaccordingtoparametertype,numberof
parameters,andorderoftheparametersatcompilationtime.
D) Amethodmaybeimplementedinseveralsubclasses.TheJavaVirtualMachine
dynamicallybindstheimplementationofthemethodatruntime.
E) Dynamicbindingcanapplytostaticmethods.

30

100)

AnswerKey
Testname:QUIZ3

1) C
2) C
3) B
4) D
5) A
6) A,D
7) B
8) A,B
9) B
10) E
11) C
12) A
13) B,D
14) A,B,C,D,E
15) C
16) B
17) A,B
18) A
19) D
20) B
21) D
22) A,B,D
23) A,B,D
24) D
25) A,E
26) B,C,D
27) A,C,D,E
28) C
29) A
30) D
31) C
32) C
33) A
34) D
35) B
36) A
37) A
38) C
39) A
40) A
41) B
42) E
43) E
44) C
45) E
46) B
47) C
48) B

49)
50)
51)
52)
53)
54)
55)
56)
57)
58)
59)
60)
61)
62)
63)
64)
65)
66)
67)
68)
69)
70)
71)
72)
73)
74)
75)
76)
77)
78)
79)
80)
81)
82)
83)
84)
85)
86)
87)
88)
89)
90)
91)
92)
93)
94)
95)
96)

A
A,B,C,D
A,B,C,D,E
D
A,C,D
D
A,C,D,E
A,B,C,D
A
D
C
A
A
A,B,C,D
A
D
B,C,D
A,B,C,D
C
D
D
C,D
A
A
B
C
A,D
D
D
C
D
B
C
D
E
A,B,C,E
A,B,C
D
A
A
A
D
D
B
B,C
D
A
B

97) C
98) C
99) D
100) A,B,C,D

31

1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)
18)
19)
20)
21)
22)
23)
24)
25)
26)
27)
28)
29)
30)
31)
32)
33)
34)
35)
36)
37)
38)
39)
40)
41)
42)
43)
44)
45)
46)
47)
48)
49)
50)
51)
52)

53)
54)
55)
56)
57)
58)
59)
60)
61)
62)
63)
64)
65)
66)
67)
68)
69)
70)
71)
72)
73)
74)
75)
76)
77)
78)
79)
80)
81)
82)
83)
84)
85)
86)
87)
88)
89)
90)
91)
92)
93)
94)
95)
96)
97)
98)
99)
100)

32

Das könnte Ihnen auch gefallen