Exam5Ch6 7 8.docx
- 文档编号:2690710
- 上传时间:2023-05-04
- 格式:DOCX
- 页数:16
- 大小:63.60KB
Exam5Ch6 7 8.docx
《Exam5Ch6 7 8.docx》由会员分享,可在线阅读,更多相关《Exam5Ch6 7 8.docx(16页珍藏版)》请在冰点文库上搜索。
Exam5Ch678
Name:
_______________________
CoversChapters6,7,and8
IntroductiontoProgramming
HarbinNormalUniversity
Instructor:
Fullway
PoweredBy:
Y.DanielLiang
(50minutes)
PartI:
MultipleChoiceQuestions:
(1ptseach)
1.Analyzethefollowingcode:
publicclassTest{
publicstaticvoidmain(Stringargs[]){
Testnc=newTest();
nc.t=nc.t++;
}
intt;
Test(){
}
}
a.Theprogramhasacompilationerrorbecausetisnotinitialized.
b.Theprogramdoesnotcompilebecausetheparameterlistofthemainmethodiswrong.
c.Theprogramcompiles,buthasaruntimeerrorbecausethasnoinitialvalue.
d.TheprogramhasacompilationerrorbecauseyouattempttocreateanobjectoftheTestinsidetheTestclass.
e.Theprogramcompilesandrunsfine.
Inthefollowingcode,supposethatfisaninstanceofFoo.AnswerQuestions2–3.
classFoo{
inti;
staticints;
voidimethod(){
}
staticvoidsmethod(){
}
}
2.Whichofthefollowingstatementsisincorrect?
a.System.out.println(f.s);
b.intt=f.imethod();
c.System.out.println(f.i);
d.f.smethod();
3.Whichofthefollowingstatementsisincorrect?
a.Foo.smethod();
b.System.out.println(Foo.s);
c.f.imethod();
d.System.out.println(Foo.i);
4.Torestrictaccessofadatamemberoramethodtotheclassitself:
a.Usetheprivatemodifier.
b.Youcannotusetheprivatemodifierwiththestaticmodifier.
c.Usethestaticmodifier.
d.Noneoftheabove.
5.Analyzethefollowingcode:
classTest{
publicstaticvoidmain(String[]args){
doubleradius=5;
finalstaticdoublePI=3.15169;
doublearea=radius*radius*PI;
System.out.println("Areais"+area);
}
}
a.Theprogramhassyntaxerrorsbecausethevariableradiusisnotinitialized.
b.TheprogramhassyntaxerrorsbecauseastaticPIisdefinedinsideamethod.
c.Theprogramhasnosyntaxerrorsbutwillgetaruntimeerrorbecauseradiusisnotinitialized.
d.Theprogramcompilesandrunsfine.
6.Analyzethefollowingcode:
classTest{
privateintt;
publicstaticvoidmain(String[]args){
Testtest=newTest();
intx;
System.out.println(test.t);
}
}
a.Thevariabletisnotinitializedandthereforecauseserrors.
b.Thevariabletisprivateandthereforecannotbeaccessedinthemainmethod.
c.Thevariablexisnotinitializedandthereforecauseserrors.
d.Theprogramcompilesandrunsfine.
7.Analyzethefollowingcode:
classTest{
publicstaticvoidmain(String[]args){
Aa=newA("test");
a.print();
}
}
classA{
Strings;
A(Strings){
this.s=s;
}
privatevoidprint(){
System.out.println(s);
}
}
a.Theprogramcompilesfine,buthasaruntimeerrorbecausetheprint()methodisprivate.
b.Theprogramhasacompilationerrorbecausetheprint()methodinclassAisprivate.
c.TheprogramrunsfineandprintsTest.
d.Noneoftheabove.
8.Supposes1ands2aretwostrings.Whichofthefollowingstatementsorexpressionsareincorrect?
a.Strings=newString("newstring");
b.Strings3=s1+s2;
c.Strings3=s1.concat(s2);
d.s1>=s2
e.inti=s1.length();
9.Supposes1ands2aretwostrings.Whichofthefollowingstatementsorexpressionsareincorrect?
a.Strings3=s1-s2;
b.inti=pareTo(s2);
c.charc=s1[0];
d.charc=s1.charAt(s1.length()-1);
e.aandc.
10.Whatistheoutputofthefollowingprogram?
importjava.util.StringTokenizer;
classTestStringTokenizer{
publicstaticvoidmain(String[]args){
//Createastringandstringtokenizer
Strings="I+am-learning?
Java.";
StringTokenizerst=newStringTokenizer(s,"+-");
//Retrieveanddisplaytokens
while(st.hasMoreTokens())
System.out.print(st.nextToken()+"");
}
}
a.IamlearningJava.
b.Iamlearning?
Java.
c.I+am-learningJava.
d.Noneoftheabove.
11.Whichofthefollowingstatementisnottrue?
a.Thecontentsofastringcanbepartiallychanged.
b.Youcanadd,insert,ordeletecharactersfromastringbuffer.
c.Youcancreateastringbufferfromastring.
d.Youcanconvertastringbufferintoastring.
e.Alloftheabove
12.Howcanyouinitializeastringwith"123"?
a.String[]string={'1','2','3'};
b.Stringstring={'1','2','3'};
c.Strings="123";
d.Strings=newString("123");
e.canddarebothfine,butcisbetter.
13.Analyzethefollowingcode.
classTest{
publicstaticvoidmain(String[]args){
String[]s=newString[3];
System.out.println("s[0]is"+s[0].toString());
}
}
a.Theprogramhasasyntaxerrorbecausethesizeofthearraywasn'tspecifiedwhendeclaringthearray.
b.Theprogramhasaruntimeerrorbecauses[0]isnull.
c.Theprogramrunsfineanddisplayss[0]is0.
d.Noneoftheabove.
14.Whatiswronginthefollowingcode?
classTest{
publicstaticvoidmain(String[]args){
Cc=newC(5.0);
System.out.println(c.value);
}
}
classC{
intvalue=2;
}
a.TheprogramhasacompilationerrorbecauseclassCdoesnothaveadefaultconstructor.
b.TheprogramhasacompilationerrorbecauseclassCdoesnothaveaconstructorwithanargumentofthedoubletype.
c.Theprogramcompilesfine,butitdoesnotrunbecauseclassCisnotpublic.
d.aandb.
15.Whatiswronginthefollowingcode?
publicclassFoo{
publicvoidmethod1(){
Circlec;
System.out.println("Whatisradius"+c.getRadius());
c=newCircle();
}
}
a.TheprogramhasacompilationerrorbecauseclassFoodoesnothaveamainmethod.
b.TheprogramhasacompilationerrorbecauseclassFoodoesnothaveadefaultconstructor.
c.Theprogramhasacompilationerrorintheprintlnstatementbecausechasnotbeenassignedaninitialvalue.
d.Theprogramcompilesfine,butithasaruntimeerrorbecausevariablecisnullwhentheprintlnstatementisexecuted.
16.Whatiswronginthefollowingcode?
publicclassFoo{
publicstaticvoidmain(String[]args){
method1();
}
publicvoidmethod1(){
method2();
}
publicvoidmethod2(){
System.out.println("Whatisradius"+c.getRadius());
}
Circlec=newCircle();
}
a.method2shouldbedeclaredbeforemethod1,sincemethod2isinvokedfrommethod1.
b.cshouldbedeclaredbeforemethod2,sincecisusedinmethod2.
c.Theprogramhasacompilationerrorintheprintlnstatementwherechasnotbeendefined.
d.Theprogramcompilesfine,butithasaruntimeerrorbecausevariablecisnullwhentheprintlnstatementisexecuted.
e.method1isaninstancemethodandcannotbeinvokedinthestaticcontextinthemainmethod.
17.Asubclassinherits_____________fromitssuperclasses.
a.privatedata
b.protecteddata
c.publicdata
d.aandc
e.bandc
18.Whenyouimplementamethodthatisdefinedinasuperclass,you__________theoriginalmethod.
a.overload
b.override
c.copy
d.call
19.WhatistheoutputofrunningtheclassC.
publicclassC{
publicstaticvoidmain(String[]args){
Object[]o={newA(),newB()};
System.out.print(o[0]);
System.out.print(o[1]);
}
}
classAextendsB{
publicStringtoString(){
return"A";
}
}
classB{
publicStringtoString(){
return"B";
}
}
a.AB
b.BA
c.AA
d.BB
e.Noneofabove
20.WhatistheoutputofrunningclassC?
classA{
publicA(){
System.out.println(
"ThedefaultconstructorofAisinvoked");
}
}
classBextendsA{
publicB(Strings){
System.out.println(s);
}
}
publicclassC{
publicstaticvoidmain(String[]args){
Bb=newB("TheconstructorofBisinvoked");
}
}
a.none
b."TheconstructorofBisinvoked"
c."ThedefaultconstructorofAisinvoked"
"TheconstructorofBisinvoked"
d."TheconstructorofBisinvoked"
"ThedefaultconstructorofAisinvoked"
e."ThedefaultconstructorofAisinvoked"
21.Themethod_____overridesthefollowingmethod:
protecteddoublexMethod(intx){…};
a.privatedoublexMethod(intx){…}
b.protectedintxMethod(doublex){…}
c.publicdoublexMethod(doublex){…}
d.publicdoublexMethod(intx){…}
PartII.(6pts)Explainwhytheunderlinedcodeiswrong.
A.(Syntaxerrors)
publicclassTest{
privateintx;
publicstaticvoidmain(String[]args){
newTest();
}
publicTest(intx){
this.x=x;
}
}
B.(Syntaxerrors)
publicclassTest{
publicstaticvoidmain(String[]args){
Aa=newA(5.5);
System.out.println(a.x);
}
}
publicclassA{
privatex;
publicvoidA(doublex){
this.x=x;
}
}
C.(Syntaxerrors)
publicclassA{
String[]myStrings=newString[2];
myStrings[0]=newString("A");
publicA(){
}
}
D.(Runtimeerror)
publicclassTest{
publicstaticvoidmain(String[]args){
Objectobject=newFruit();
Objectobject1=(Apple)object;
}
}
classAppleextendsFruit{
}
classFruit{
}
PartIII:
Showtheprintoutofthefollowingcode:
a.(2ptseach)
publicclassTest{
publicstaticvoidmain(String[]args){
Tt=newT();
swap(t);
System.out.println("e1="+t.e1+"e2="+t.e2);
}
publicstaticvoidswap(Tt){
inttemp=t.e1;
t.e1=t.e2;
t.e2=temp;
}
}
classT{
inte1=1;
inte2=2;
}
b.(2pts)
publicclassTest{
publicstaticvoidmain(String[]args){
Tt1=newT();
Tt2=newT();
System.out.println("t1'si="+t1.i+"andj="+t1.j);
System.out.println("t2'si="+t2.i+"andj="+t2.j);
}
}
classT{
staticinti=0;//Pleasenotethatiisstatic
intj=0;
T(){
i++;
j++;
}
}
c.(2pts)
importjava.util.*;
publicclassTest{
publicstaticvoidmain(String[]args){
Datedate=newDate();
Objecto=
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Exam5Ch6