java笔试题1.docx

上传人:b****8 文档编号:10966001 上传时间:2023-02-24 格式:DOCX 页数:12 大小:18.40KB
下载 相关 举报
java笔试题1.docx_第1页
第1页 / 共12页
java笔试题1.docx_第2页
第2页 / 共12页
java笔试题1.docx_第3页
第3页 / 共12页
java笔试题1.docx_第4页
第4页 / 共12页
java笔试题1.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

java笔试题1.docx

《java笔试题1.docx》由会员分享,可在线阅读,更多相关《java笔试题1.docx(12页珍藏版)》请在冰豆网上搜索。

java笔试题1.docx

java笔试题1

1.Whatwillbetheoutputwhenyoucompileandexecutethefollowingprogram.

publicclassBase{

privatevoidtest(){

System.out.println(6+6+"(Result)");

}

staticpublicvoidmain(String[]a){

newBase().test();

}

}

Selectmostappropriateanswer.

A66(Result)

B12(Result)

CRuntimeError.Incompatibletypefor+.Can'tconvertaninttoastring.

DCompilationError.Incompatibletypefor+.Can'taddastringtoanint.

解答:

B

考点:

字符串与基本数据的链接

1.如果第一个是字符串那么后续都按字符串进行处理

2.如果第一个到第n个是基本数据,第n+1个是字符串,那么前n个数据都按加法计算,再与字符串链接

2..Whatwillbetheoutputwhenyoucompileandexecutethefollowingprogram.Thesymbol’⌴’meansspace.

1:

publicclassBase{

2:

3:

privatevoidtest(){

4:

5:

StringaStr="⌴One⌴";

6:

StringbStr=aStr;

7:

aStr.toUpperCase();

8:

aStr.trim();

9:

System.out.println("["+aStr+","+bStr+"]");

7:

}

8:

9:

staticpublicvoidmain(String[]a){

10:

newBase().test();

11:

}

12:

}

Selectmostappropriateanswer.

A[ONE,⌴One⌴]

B[⌴One⌴,One]

C[ONE,One]

D[ONE,ONE]

E[⌴One⌴,⌴One⌴]

解答:

E

通过StringbStr=aStr;这句代码使bStr和aStr指向同一个地址空间。

String类是定长字符串,调用一个字符串的方法以后会形成一个新的字符串。

3.Java语言中,String类的indexOf()方法返回的类型是?

A、Int16B、Int32C、intD、long

解答:

C

indexOf方法的声明为:

publicintindexOf(intch)

返回指定字符在字符串中第一次出现的索引,如果未出现该字符,则返回-1

4..执行下列代码后,哪个结论是正确的String[]s=newString[10];

A.s[9]为null;

B.s[10]为"";

C.s[0]为未定义

D.s.length为10

解答:

AD

s是引用类型,s中的每一个成员都是引用类型,即String类型,String类型默认的值为null,s数组的长度为10。

5.Given:

publicclassTest{

publicstaticvoidmain(String[]args){

Stringstr=NULL;

System.out.println(str);

}

}

Whatistheresult?

A.NULL

B.Compilationfails.

C.Thecoderunswithnooutput.

D.Anexceptionisthrownatruntime.

解答:

B

null应该小写

6、Given:

publicclassTest{

publicstaticvoidmain(Stringargs[]){

classFoo{

publicinti=3;

}

Objecto=(Object)newFoo();

Foofoo=(Foo)o;

System.out.println(foo.i);

}

}

Whatistheresult?

A.Compilationwillfail.

B.Compilationwillsucceedandtheprogramwillprint“3”

C.CompilationwillsucceedbuttheprogramwillthrowaClassCastExceptionatline6.

D.CompilationwillsucceedbuttheprogramwillthrowaClassCastExceptionatline7.

解答:

B

考点:

局部内部类的使用

局部内部类定义在方法中,作用域相当于局部变量的作用域

7、Given:

publicclassTest{

publicstaticvoidmain(String[]args){

Stringfoo=“blue”;

Stringbar=foo;

foo=“green”;

System.out.println(bar);

}

}

Whatistheresult?

A.Anexceptionisthrown.

B.Thecodewillnotcompile.

C.Theprogramprints“null”

D.Theprogramprints“blue”

E.Theprogramprints“green”

解答:

D

采用Stringfoo=“blue”定义方式定义的字符串放在字符串池中,通过Stringbar=foo;

他们指向了同一地址空间,就是同一个池子,当执行foo=“green”;foo指向新的地址空间。

8.下面哪个是正确的?

()

A.Stringtemp[]=newString{"a""b""c"};

B.Stringtemp[]={"a""b""c"};

C.Stringtemp={"a","b","c"};

D.Stringtemp[]={"a","b","c"};

解答:

D

A.Stringtemp[]=newString[]{"a","b","c"};

B.Stringtemp[]={"a","b","c"};

9.关于java.lang.String类,以下描述正确的一项是()

A.String类是final类故不可以继承;

B.String类是final类故可以继承;

C.String类不是final类故不可以继承;

D.String类不是final类故可以继承;

解答:

A

String类是final的,在java中final修饰类的不能被继承

10.从下面四段(A,B,C,D)代码中选择出正确的代码段()

A.abstractclassName{

privateStringname;

publicabstractbooleanisStupidName(Stringname){}

}

B.publicclassSomething{

voiddoSomething(){

privateStrings="";

intl=s.length();

}

}

C.publicclassSomething{

publicstaticvoidmain(String[]args){

Othero=newOther();

newSomething().addOne(o);

}

publicvoidaddOne(finalOthero){

o.i++;

}

}

classOther{

publicinti;

}

D.publicclassSomething{

publicintaddOne(finalintx){

return++x;}

}

解答:

C

A..抽象方法不能有方法体

B.方法中定义的是局部变量,不能用类成员变量修饰符private

D.final修饰为常量,常量的值不能被改变

11Strings=“Example”;

合法的代码由哪些?

A)s>>>=3B)s[3]=“X”C)inti=s.length()D)s=s+10

解答:

CD

A.移位运算,要是整数类型。

B.s不是数组

C.String类取长度的方法为:

length()

D.字符串相加

12.已知表达式intm[]={0,1,2,3,4,5,6};

下面哪个表达式的值与数组下标量总数相等?

()

A.m.length()

B.m.length

C.m.length()+1

D.m.length+1

解答:

B

解答:

数组下标是从零开始的,但是数据下标的总量和数据长度相同。

只有String类型才有length()方法,其他的类型都是length属性

13.给出如下代码:

)(

classTest{

privateintm;

publicstaticvoidfun(){

//somecode„

}

}

如何使成员变量m被函数fun()直接访问?

()

A.将privateintm改为protectedintm

B.将privateintm改为publicintm

C.将privateintm改为staticintm

D.将privateintm改为intm

解答:

C

静态的方法中可以直接调用静态数据成员

14.下述代码的执行结果是

classSuper{

publicintgetLength(){return4;}

}

publicclassSubextendsSuper{

publiclonggetLength(){return5;}

publicstaticvoidmain(String[]args){

Supersooper=newSuper();

Supersub=newSub();

System.out.printIn(sooper.getLength()+“,”+sub.getLength()};

}

}

A.4,4

B.4,5

C.5,4

D.5,5

E.代码不能被编译

解答:

E

方法重写返回值类型与父类的一致

15.指出下列程序运行的结果

publicclassExample{

Stringstr=newString("good");

char[]ch={'a','b','c'};

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

ex.change(ex.str,ex.ch);

System.out.print(ex.str+"and");

System.out.print(ex.ch);

}

publicvoidchange(Stringstr,charch[]){

str="testok";

ch[0]='g';

}

}

Agoodandabc

Bgoodandgbc

Ctestokandabc

Dtestokandgbc

解答:

B

数组和字符串都是引用类型。

Java中参数传递是基于值的传递:

基本数据:

传递的是实际的值

引用类型:

传递的是地址值

16.Strings=newString(“hello”);

Stringt=newString(“hello”);

charc[]={‘h’,’e’,’l’,’l’,’o’};

下列哪些表达式返回true?

A.s.equals(t);

B.t.equals(c);

C.s==t;

D.t.equals(newString(“hello”));

E.t==c;

解答:

AD

String类的equals方法已经覆盖了Object类的equals方法,比较的是两个字符串的内容是否相等,双等号比较的是两个对象的内存地址是否相等

17.类Teacher和Student是类Person的子类;

Teachert;

Students;

//tandsareallnon-null.

if(tinstanceofPerson){s=(Student)t;}

最后一条语句的结果是:

A.将构造一个Student对象;

B.表达式是合法的;

C.表达式是错误的;

D.编译时正确,但运行时错误。

解答:

D

instanceof是Java的一个二元操作符,它的作用是测试它左边的对象是否是它右边的类的实例,返回boolean类型的数据。

Teahcer和Student之间没有继承关系不能做强制类型转换。

18.下面关于变量及其范围的陈述哪些是不正确的():

A.实例变量是类的成员变量

B.实例变量用关键字static声明

C.在方法中定义的局部变量在该方法被执行时创建

D.局部变量在使用前必须被初始化

解答:

BC

B.由static修饰的变量称为类变量或是静态变量

C.方法加载的时候创建局部变量

19.编译运行以下程序后,关于输出结果的说明正确的是():

publicclassConditional{

publicstaticvoidmain(Stringargs[]){

intx=4;

System.out.println(“valueis“+((x>4)?

99.9:

9));

}

}

A、输出结果为:

valueis99.99

B、输出结果为:

valueis9

C、输出结果为:

valueis9.0

D、编译错误

解答:

C

三目运算符中:

第二个表达式和第三个表达式中如果都为基本数据类型,整个表达式的运算结果由容量高的决定。

99.9是double类型而9是int类型,double容量高。

20.以下关于面向对象概念的描述中,不正确的一项是()。

(选择1项)

A.在现实生活中,对象是指客观世界的实体

B.程序中的对象就是现实生活中的对象

C.在程序中,对象是通过一种抽象数据类型来描述的,这种抽象数据类型称为类(class)

D.在程序中,对象是一组变量和相关方法的集合

解答:

B

 

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

当前位置:首页 > 高等教育 > 经济学

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

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