SCJP题库.docx
- 文档编号:7685640
- 上传时间:2023-01-25
- 格式:DOCX
- 页数:30
- 大小:1MB
SCJP题库.docx
《SCJP题库.docx》由会员分享,可在线阅读,更多相关《SCJP题库.docx(30页珍藏版)》请在冰豆网上搜索。
SCJP题库
yModule1-JAVA基础
一、选择题:
Question1
Given:
35.String#name="JaneDoe";
36.int$age=24;
37.Double_height=123.5;
38.double~temp=37.5;
Whichtwoaretrue?
(Choosetwo.)
A.Line35willnotcompile.
B.Line36willnotcompile.
C.Line37willnotcompile.
D.Line38willnotcompile.
Answer:
AD标识符以字母,下划线,或者$开始
Question2
Given:
11.publicclassTest{
12.publicstaticvoidmain(String[]args){
13.intx=5;
14.booleanb1=true;
15.booleanb2=false;
16.
17.if((x==4)&&!
b2)
18.System.out.print("l");
19.System.out.print("2");
20.if((b2=true)&&b1)
21.System.out.print("3");
22.}
23.}
Whatistheresult?
A.2
B.3
C.12
D.23
E.123
F.Compilationfails.
G.Anexceptionalisthrownatruntime.
Answer:
D注意20行,=为赋值,不要被骗
Question3
Given:
42.publicclassClassA{
43.publicintgetValue(){
44.intvalue=0;
45.booleansetting=true;
46.Stringtitle="Hello";
47.if(value||(setting&&title=="Hello")){return1;}
48.if(value==1&title.equals("Hello")){return2;}
49.}
50.}
And:
70.ClassAa=newClassA();
71.a.getValue();
Whatistheresult?
A.1
B.2
C.Compilationfails.
D.Thecoderunswithnooutput.
E.Anexceptionisthrownatruntime.
Answer:
C编译不通过,47行value为int类型不是boolean
Question4
Given:
11.publicvoidtestIfA(){
12.if(testIfB("True")){
13.System.out.println("True");
14.}else{
15.System.out.println("Nottrue");
16.}
17.}
18.publicBooleantestIfB(Stringstr){
19.returnBoolean.valueOf(str);
20.}
WhatistheresultwhenmethodtestIfAisinvoked?
A.True
B.Nottrue
C.Anexceptionisthrownatruntime.
D.Compilationfailsbecauseofanerroratline12.
E.Compilationfailsbecauseofanerroratline19.
Answer:
A19行,如果str为true则返回ture,否则返回false
Question5
Given:
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就是两个Integer类型相加
Question6
Given:
11.publicstaticvoidmain(String[]args){
12.Stringstr="null";
13.if(str==null){
14.System.out.println("null");
15.}else(str.length()==0){
16.System.out.println("zero");
17.}else{
18.System.out.println("some");
19.}
20.}
‘Whatistheresult?
A.null
B.zero
C.some
D.Compilationfails.
E.Anexceptionisthrownatruntime.
Answer:
D这题真恶心15行少个if
Question7
Given:
10.intx=0;
11.inty=10;
12.do{
l3.y--;
14.++x;
15.}while(x<5);
16.System.out.print(x+","+y);
Whatistheresult?
A.5,6
B.5,5
C.6,5
D.6,6
Answer:
B(91,82,73,64,55)没啥争议
Question8
Given:
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:
C12>10,所以直接跳出循环,输出x=12
Question9
Given:
35.intx=10;
36.do{
37.x--;
38.}while(x<10);
Howmanytimeswillline37beexecuted?
A.tentimes
B.zerotimes
C.onetometimes
D.morethantentimes
Answer:
D死循环
Question10
Given:
11.publicstaticvoidmain(String[]args){
12.for(inti=0;i<=10;i++){
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:
E15行i超出了作用域
Question11
Given:
55.int[]x={1,2,3,4,5};
56.inty[]=x;
57.System.out.println(y[2]);
Whichistrue?
A.Line57willprintthevalue2.
B.Line57willprintthevalue3.
C.Compilationwillfailbecauseofanerrorinline55.
D.Compilationwillfailbecauseofanerrorinline56.
Answer:
B没争议,考察数组下标是从0开始
Question12
Whichtwocodefragments(片段)correctly(正确)createandinitializeastaticarrayofintelements?
(Choosetwo.)
A.staticfinalint[]a={100,200};
B.staticfinalint[]a;
static{a=newint[2];a[0]=100;a[1]=200;}
C.staticfinalint[]a=newint[2]{100,200};
D.staticfinalint[]a;
staticvoidinit(){a=newint[3];a[0]=100;a[1]=200;}
Answer:
ABc不能指定长度,d不能在init方法中赋值,要么在static代码块中
Question13
Given:
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没争议,foeach循环遍历数组
Question14
Given:
11.String[]elements={"for","tea","too"};
12.Stringfirst=(elements.length>0)?
elements[0]:
null;
Whatistheresult?
A.Compilationfails.
B.Anexceptionisthrownatruntime.
C.Thevariablefirstissettonull.
D.Thevariablefirstissettoelements[0].
Answer:
D
Question15
Given:
10.publicclassBar{
11.staticvoidfoo(int...x){
12.//insertcodehere
13.}
14.}
Whichtwocodefragments,insertedindependently(独立的)atline12,willallowtheclasstocompile?
(Choosetwo.)
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=0;i Answer: BDx相当于一个数组,a明显错没有foreach,c中x没有hadNext方法 Question16 Aprogrammer(程序员)needstocreatealoggingmethodthatcanaccept(接受)anarbitrary(随意任意)numberofarguments.Forexample,itmaybecalledinthese ways: logIt("logmessage1"); logIt("logmessage2”,”logmessage3"); logIt("logmessage4","logmessage5","logmessage6"); Whichdeclaration(说明)satisfies(符合)thisrequirement(需求)? A.publicvoidlogIt(String*msgs) B.publicvoidlogIt(String[]msgs) C.publicvoidlogIt(String...msgs) D.publicvoidlogIt(Stringmsg1,Stringmsg2,Stringmsg3) Answer: C可变长参数 Question17 1.publicclassA{ 2.publicStringdoit(intx,inty){ 3.return"a"; 4.} 5. 6.publicStringdoit(int...vals){ 7.return"b"; 8.} 9.} Given: 25.Aa=newA(); 26.System.out.println(a.doit(4,5)); Whatistheresult? A.Line26prints"a"toSystem.out. B.Line26prints"b"toSystem.out. C.Anexceptionisthrownatline26atruntime. D.CompilationofclassAwillfailduetoanerrorinline6. Answer: A确定参数和可变长参数同时存在的时候,优先考虑确定参数的 Question18 GivenafileGrizzlyBear.java: 1.packageanimals.mammals; 2. 3.publicclassGrizzlyBearextendsBear{ 4.voidhunt(){ 5.Salmons=findSalmon(); 6.s.consume(); 7.} 8.} andanotherfile,Salmon.java: 1.packageanimals.fish; 2. 3.publicclassSalmonextendsFish{ 4.voidconsume(){/*dostuff*/} 5.} Assume(假定)bothclassesaredefinedinthecorrectdirectoriesfortheftpackages,andthattheMammalclasscorrectlydefinesthefindSalmon()method.Whichtwochangesallowthiscodetocompilecorrectly? (Choosetwo.) A.addpublictothestartofline4inSalmon.java B.addpublictothestartofline4inGrizzlyBear.java C.addimportanimals.mammals.*;atline2inSalmon.java D.addimportanimals.fish.*;atline2inGrizzlyBear.java E.addimportanimals.fish.Salmon.*;atline2inGrizzlyBear.java F.addimportanimals.mammals.GrizzlyBear.*;atline2inSalmon.java Answer: AD调用不同包下的类,要先导入,方法权限要设置成public Question19 Given: 10.packagecom.sun.scjp; 11.publicclassGeodetics{ 12.publicstaticfinaldoubleDIAMETER=12756.32;//kilometers 13.} Whichtwocorrectlyaccess(访问)theDIAMETERmemberoftheGeodeticsclass? (Choosetwo.) A.importcom.sun.scjp.Geodetics; publicclassTerraCarta{ publicdoublehalfway() {returnGeodetics.DIAMETER/2.0;}} B.importstaticcom.sun.scjp.Geodetics; publicclassTerraCarta{ publicdoublehalfway(){returnDIAMETER/2.0;}} C.importstaticcom.sun.scjp.Geodetics.*; publicclassTerraCarta{ publicdoublehalfway(){returnDIAMETER/2.0;}} D.packagecom.sun.scjp; publicclassTerraCarta{ publicdoublehalfway(){returnDIAMETER/2.0;}} Answer: ACb中不能静态导入类,c中静态导入类属性,正确,d访问错误很明显 Question20 Givenclassesdefinedintwodifferentfiles: 1.packageutil; 2.publicclassBitUtils{ 3.privatestaticvoidprocess(byte[]b){} 4.} 1.packageapp; 2.publicclassSomeApp{ 3.publicstaticvoidmain(String[]args){ 4.byte[]bytes=newbyte[256]; 5.//insertcodehere 6.} 7.} Whatisrequired(必需的)atline5inclassSomeApptousetheprocessmethod ofBitUtils? A.process(bytes); B.BitUtils.process(bytes); C.app.BitUtils.process(bytes); D.util.BitUtils.process(bytes); E.importutil.BitUtils.*;process(bytes); F.SomeAppcannotusetheprocessmethodinBitUtils. Answer: F私有的,不能被访问 Question21 GivenaclassRepetition(重复): 1.packageutils; 2. 3.publicclassRepetition{ 4.publicstaticStringtwice(Strings){returns+s;} 5.} andgivenanotherclassDemo: 1.//insertcodehere 2. 3.publicclassDemo{ 4.publicstaticvoidmain(String[]args){ 5.System.out.println(twice("pizza")); 6.} 7.} Whichcodeshouldbeinsertedatline1ofDemo.javatocompileandrunDemotoprint“pizzapizza”? A.importutils.*; B.staticimportutils.*; C.importutils.Repetition.*; D.staticimportutils.Repetition.*; E.importutils.Repetition.twice(); F.importstaticutils.Repetition.twice; G.staticimportutils.Repetition.twice; Answer: F静态导入 Question22 Given: 1.packagetest; 2. 3.classTarget(目标){ 4.publicStringname="hello"; 5.} Whatcandirectly(直接的)accessandchangethevalueofthevariable(变量)name? A.anyclas
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SCJP 题库