面向对象编程基础习题及答案.docx

上传人:b****2 文档编号:12645798 上传时间:2023-04-21 格式:DOCX 页数:48 大小:54.30KB
下载 相关 举报
面向对象编程基础习题及答案.docx_第1页
第1页 / 共48页
面向对象编程基础习题及答案.docx_第2页
第2页 / 共48页
面向对象编程基础习题及答案.docx_第3页
第3页 / 共48页
面向对象编程基础习题及答案.docx_第4页
第4页 / 共48页
面向对象编程基础习题及答案.docx_第5页
第5页 / 共48页
点击查看更多>>
下载资源
资源描述

面向对象编程基础习题及答案.docx

《面向对象编程基础习题及答案.docx》由会员分享,可在线阅读,更多相关《面向对象编程基础习题及答案.docx(48页珍藏版)》请在冰豆网上搜索。

面向对象编程基础习题及答案.docx

面向对象编程基础习题及答案

面向对象编程基础习题及答案

PartI.ChoiceQuestions(1ptforeachquestion).

1.Toaddatobandstoreresultinb,youwrite(Note:

Javaiscase-sensitive)

A.b+=a;

B.a=b+a;

C.b=A+b;

D.a+=b;

2.TodeclareaconstantPI,youwrite

A.finalstaticPI=3.14159;

B.finalfloatPI=3.14159;

C.staticdoublePI=3.14159;

D.finaldoublePI=3.14159;

3.Toimprovereadabilityandmaintainability,youshoulddeclare_________insteadofusingliteralvaluessuchas3.14159.

A.variables

B.methods

C.constants

D.classes

4.Todeclareanintvariablexwithinitialvalue200,youwrite

A.intx=200L;

B.intx=200l;

C.intx=200;

D.intx=200.0;

5.Toassignadoublevariabledtoanintvariablex,youwrite

A.x=(long)d

B.x=(int)d;

C.x=d;

D.x=(float)d;

6.InJava,thewordtrueis________.

A.aJavakeyword

B.aBooleanliteral

C.sameasvalue1

D.sameasvalue0

7.WhichoftheBooleanexpressionsbelowhasincorrectsyntax?

A.(true)&&(3>4)

B.!

(x>0)&&(x>0)

C.(x>0)||(x<0)

D.(x!

=0)||(x=0)

8.Whichofthefollowingisthecorrectexpressionthatevaluatestotrueifthenumberxisbetween1and100orthenumberisnegative?

A.1

B.((x<100)&&(x>1))||(x<0)

C.((x<100)&&(x>1))&&(x<0)

D.(1>x>100)||(x<0)

9.Whichofthefollowingisthecorrectexpressionofcharactera?

A.'a'

B."a"

C.'\000a'

D.Noneoftheabove.

10.Whichofthefollowingstatementprintssmith\exam1\test.txt?

A.System.out.println("smith\exam1\test.txt");

B.System.out.println("smith\\exam1\\test.txt");

C.System.out.println("smith\"exam1\"test.txt");

D.System.out.println("smith"\exam1"\test.txt");

11.Supposeiisaninttypevariable.WhichofthefollowingstatementsdisplaythecharacterwhoseUnicodeisstoredinvariablei?

A.System.out.println(i);

B.System.out.println((char)i);

C.System.out.println((int)i);

D.System.out.println(i+"");

12.TheUnicodeof'a'is97.WhatistheUnicodefor'c'?

A.96

B.97

C.98

D.99

13.Whichofthefollowingisaconstant,accordingtoJavanamingconventions?

A.MAX_VALUE

B.Test

C.read

D.ReadInt

14.Whichofthefollowingassignmentstatementsisillegal?

A.floatf=-34;

B.intt=23;

C.shorts=10;

D.floatf=34.0;

15.AJavastatementendswitha__________.

A.comma(,)

B.semicolon(;)

C.period(.)

D.closingbrace

16.TheassignmentoperatorinJavais__________.

A.:

=

B.=

C.==

D.<-

17.Supposemandrareintegers.WhatisthecorrectJavaexpressionform/r^2toobtainafloatingpointresultvalue?

(r^2denotesrraisedtothepowerof2).

A.m/r*r

B.m/(r*r)

C.1.0*m/r*r

D.1.0*m/(r*r)

18.Whichofthesedatatypesrequirestheleastamountofmemory?

A.float

B.double

C.short

D.byte

19.Whichofthefollowingoperatorshasthehighestprecedence?

A.casting

B.+

C.*

D./

20.Anintvariablecanhold__________.

A.'x'

B.93

C.98.3

D.true

E.aandb

21.Whichofthefollowingassignmentstatementsiscorrecttoassigncharacter5toc?

A.charc='5';

B.charc=5;

C.charc="5";

D.charc="344";

22.NotethattheUnicodeforcharacterAis65.Theexpression"A"+1evaluatesto________.

A.66

B.B

C.A1

D.Illegalexpression

23.ThenotequalcomparisonoperatorinJavais__________.

A.<>

B.!

=

C.!

==

D.^=

24.Ifyouattempttoaddanint,abyte,along,andadouble,theresultwillbea__________value.

A.byte

B.int;

C.long;

D.double;

25.Whatisthevalueof(double)5/2?

A.2;

B.2.5;

C.3;

D.2.0;

26.Whatisthevalueof(double)(5/2)?

A.2;

B.2.5;

C.3;

D.2.0;

27.Analyzethefollowingcode.

publicclassTest{

publicstaticvoidmain(String[]args){

intmonth=09;

System.out.println("monthis"+month);

}

}

A.Theprogramdisplaysmonthis09

B.Theprogramdisplaysmonthis9

C.Theprogramdisplaysmonthis9.0

D.Theprogramhasasyntaxerror,because09isanincorrectliteralvalue.

28.Toassignadoublevariabledtoafloatvariablex,youwrite

A.x=(long)d

B.x=(int)d;

C.x=d;

D.x=(float)d;

29.whatisydisplayedinthefollowingcode?

publicclassTest1{

publicstaticvoidmain(String[]args){

intx=1;

inty=x=x+1;

System.out.println("yis"+y);

}

}

A.yis0.

B.yis1becausexisassignedtoyfirst.

C.yis2becausex+1isassignedtoxandthenxisassignedtoy.

D.Theprogramhasasyntaxerrorsincexisredeclaredinthestatementinty=x=x+1.

30.Ifaprogramcompilesfine,butitterminatesabnormallyatruntime,thentheprogramsuffers__________.

A.asyntaxerror

B.aruntimeerror

C.alogicerror

31.Supposex=0andy=0whatisxafterevaluatingtheexpression(y>0)&(1>x++).

A.0

B.-1

C.1

32.Supposex=0andy=0whatisxafterevaluatingtheexpression(y>0)&&(1>x++).

A.0

B.-1

C.1

33.SupposeyoudefineaJavaclassasfollows:

publicclassTest{

}

Inordertocompilethisclass,theclassshouldbestoredinafilenamed

A.Test.class

B.Test.doc

C.Test.txt

D.Test.java

E.Anynamewithextension.java

34.ThecommandtocompileaclassinthefileTest.javais

A.javaTest

B.javaTest.java

C.javacTest.java

D.javacTest

E.JAVACTest.java

35.WhichJDKcommandiscorrecttorunaJavaapplicationinByteCode.class?

A.javaByteCode

B.javaByteCode.class

C.javacByteCode.java

D.javacByteCode

E.JAVACByteCode

36.Whatis1%2?

A.0

B.1

C.2

37.Whatis"Welcome"+1+1*2?

A.Welcome11*2

B.Welcome4

C.Welcome12

D.Welcome3

38.Whatistheprintoutofthefollowingcode:

doublex=10.1;

inty=(int)x;

System.out.println("xis"+x+"andyis"+y);

A.xis10andyis10

B.xis10.0andyis10.0

C.xis11andyis11

D.xis10.1andyis10

E.xis10.1andyis10.0

39.Whichofthefollowingcodedisplaystheareaofacircleiftheradiusispositive.

A.if(radius!

=0)System.out.println(radius*radius*Math.PI);

B.if(radius>=0)System.out.println(radius*radius*Math.PI);

C.if(radius>0)System.out.println(radius*radius*Math.PI);

D.if(radius<=0)System.out.println(radius*radius*Math.PI);

40.Supposex=1,y=-1,andz=1.Whatistheprintoutofthefollowingstatement?

if(x>0)

if(y>0)

System.out.println("x>0andy>0");

elseif(z>0)

System.out.println("x<0andz>0");

A.x>0andy>0;

B.x<0andz>0;

C.x<0andz<0;

D.noprintout.

41.Analyzethefollowingcode:

booleaneven=false;

if(even=true){

System.out.println("Itiseven!

");

}

A.Theprogramhasasyntaxerror.

B.Theprogramhasaruntimeerror.

C.Theprogramrunsfine,butdisplaysnothing.

D.TheprogramrunsfineanddisplaysItiseven!

.

42.Analyzethefollowingcode:

//Enteraninteger

StringnumString=JOptionPane.showInputDialog(null,

"Enteranumber:

",

"ExamInput",

JOptionPane.QUESTION_MESSAGE);

intnumber=Integer.parseInt(numString);

if(number<=0)

System.out.println(number);

A.Theifstatementiswrong,becauseitdoesnothavetheelseclause;

B.System.out.println(number);mustbeplacedinsidebraces;

C.Ifnumberiszero,numberisdisplayed;

D.Ifnumberispositive,numberisdisplayed.

E.numberenteredfromthekeyboardcannotbenegative.

43.Analyzethefollowingcode.

importjavax.swing.*;

publicclassShowErrors{

publicstaticvoidmain(String[]args){

inti;

intj;

Strings=JOptionPane.showInputDialog(null,

"Enteraninteger","Input",

JOptionPane.QUESTION_MESSAGE);

j=Integer.parseInt(s);

i=(i+4);

}

}

A.Theprogramcannotcompilebecausejisnotinitialized.

B.Theprogramcannotcompilebecauseidoesnothaveaninitialvaluewhenitisusedini=i+4;

C.Theprogramcompilesbuthasaruntimeerrorbecauseideosnothaveaninitialvaluewhenitisusedini=i+4;

D.Theprogramcompilesandrunsfine.

44.Whatistheoutputofthefollowingcode:

(Pleaseindentthestatementcorrectlyfirst.)

intx=9,y=8,z=7;

if(x>9)

if(y>8)

System.out.println("x>9andy>8");

elseif(z>=7)

System.out.println("x<=9andz>=7");

else

System.out.println("x<=9andz<7");

A.x>9andy>8;

B.x<=9andz>=7;

C.x<=9andz<7;

D.Noneoftheabove.

45.Analyzethefollowingcode:

StringnumString=JOptionPane.showInputDialog(null,

"Enteranumber:

",

"ExamInput",

JOptionPane.QUESTION_MESSAGE);

intnumber=Integer.parseInt(numString);

if(number<=0)

System.out.println(number);

System.out.println(number);

A.numberisalwaysprintedoutatleastonce;

B.numberisprintedouttwiceifnumberiszero;

C.numberisprintedouttwiceifnumberisnegative;

D.numberisprintedoutonceifnumberispositive.

E.Alloftheabove.

46.Whatisyafterthefollowingswitchstatement?

intx=0;

inty=0;

switch(x+1){

case0:

y=0;

case1:

y=1;

default:

y=-1

}

A.1

B.-1

C.0

D.Noneoftheabove

47.Whatistheprintoutofthefollowingswitchstatement?

charch='a';

switch(ch){

case'a':

case'A':

System.out.print(ch);break;

case'b':

case'B':

System.out.print(ch);break;

case'c':

case'C':

System.out.print(ch);break;

case'd':

case'D':

System.out.print(ch);

}

A.abcd

B.a

C.aa

D.ab

E.abc

48.Analyzethefollowingcode.

intx=0;

inty=((x<100)&(x>0))?

1:

-1;

A.Thecodehasasyntaxerrorbecause&mustbe&&.

B.ybecomes1afterthecodeisexecuted.

C.ybecomes-1afterthecodeisexecuted.

D.Noneoftheabove.

49.Whichofthefollowingisnotavalidbooleanexpression.

A.(1

B.(x=1)||(x!

=1)

C.a,bareallcorrect

D.a,bareallwrong

50.Whichofthefollowingexpressionisequivalentto(x>1

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1