JAVA面试题二.docx
《JAVA面试题二.docx》由会员分享,可在线阅读,更多相关《JAVA面试题二.docx(23页珍藏版)》请在冰点文库上搜索。
JAVA面试题二
JAVA开发工程师面试题
(一)
备注:
请将答案填在答题纸上,100分钟内完成
1.根据这个类选择所有正确的内部类:
(多选ac)
classA{
protectedinti;
A(inti){
this.i=i;
}
}
a.
classB{
}
b.
classBextendsA{
}
c.classB{
B(){
System.out.println("i="+i);
}
}
d.
classB{
classA{
}
}
e.
classA{
}
2.下列所给出的声明中,什么会写入标准输出中?
(单选d)
System.out.println(4|7)?
a.4b.5c.6d.7e.0
3.下列有关方法notify()(用于和wait()连接)的正确描述是:
(单选c)
a.ifthereismorethanonethreadwaitingonacondition,onlythethreadthathasbeenwaitingthelongestisnotified.
b.ifthereismorethanonethreadwaitingonacondition,thereisnowaytopredictwhichthreadwillbenotified.
c.Notify()isdefinedintheThreadclass.
d.Itisnotstrictlynecessarytoownthelockfortheobjectyouinvokenotify()for.
e.Notify()shouldonlybeinvokedfromwithinawhileloop.
4.给出下列类的定义,如没有其他实例的引用,method2()能直接访问到:
(多选abc)
classA{
publicintx;
privateinty;
classB{
protectedvoidmethod1(){
}
classC{
privatevoidmethod2(){
}
}
}
}
classdextendsA{
publicfloatz;
}
a.ThevariablexdefinedinA
b.ThevariableydefinedinA
c.method()definedinB
d.ThevariablezdefinedinD
5.你有一个用ISO8859-8定义的8位字符串文档,当你要在文本域中写一个显示该文档的应用,而局部编码已经设置成ISO8859-8时,你如何写一段代码来读取该文档的第一行?
(你有3个可获取的变量c)
a.InputStreamReaderreader=newInputStreamReader(stream,"8859-8");
BufferedReaderbuffer=newBufferedReader(reader);
s=buffer.readLine();
b.InputStreamReaderreader=newInputStreamReader(stream);
BufferedReaderbuffer=newBufferedReader(reader);
s=buffer.readLine();
c.InputStreamReaderreader=newInputStreamReader(myfile,"8859-8");
BufferedReaderbuffer=newBufferedReader(reader);
s=buffer.readLine();
d.InputStreamReaderreader=newInputStreamReader(myfile);
BufferedReaderbuffer=newBufferedReader(reader);
s=buffer.readLine();
e.FileReaderreader=newFileReader(myfile);
BufferedReaderbuffer=newBufferedReader(reader);
s=buffer.readLine();
6.哪一个对单机程序中的main()方法的声明是错误的?
(多选cd)
a.publicstaticvoidmain()
b.publicstaticvoidmain(String[]string)
c.publicstaticvoidmain(stringargs)
d.staticpublicintmain(String[]args)
e.staticvoidmain(String[]args)
7.作为文本框对象的listeners,你可以添加哪一个执行接口?
(多选acd)
a.ActionListener
b.FocusListener
c.MouseMotionListener
d.WindowListener
e.ContainerListener
8.当你试着编译和运行下面的程序时,结果怎样?
(单选e)
classMyTest{
Strings;
publicstaticvoidmain(String[]args){
MyTestm=newMyTest();
m.go();
}
voidMyTest(){
s="constructor";
}
voidgo(){
System.out.println(s);
}
}
a.Thiscodewillnotcompile.
b.Thiscodecompilesbutthrowsanexceptionatruntime.
c.Thiscoderunsbutnothingappearsinthestandardoutput.
d.Thiscoderunsand"constructor"inthestandardoutput.
e.Thiscoderunsandwrites"null"inthestandardoutput.
9.试分析以下两行代码:
(多选ad)
floatf=3.2;
inti=f;
a.Thiscodewouldnotcompile.
b.Thiscodewouldcompileandiwouldbesetto3.
c.Thesecondlinewouldcompileifitwerewritteninsteadas:
inti=(byte)if;
d.Thefirstlinewouldcompileifitwerewritteninsteadas:
floatf=3.2F;
10.在下面init()方法给出的applet中,用户接口将出现什么情况?
(单选d)
publicvoidinit(){
setLayout(newBorderLayout());
add(newButton("hello"));
}
a.Nothingwillappearintheapplet.
b.Abuttonwillappearintheappletsetinttheexactcenter.
c.Abuttonwillappearintheappletalongthetopandcenteredhorizontally.
d.Abuttonwillappearinthetopleftcorner.
11.如果你在JDK1.2下想要用值将关键字联系起来,则以下哪个类位首选?
(多选be)
a.Dictionary
b.HashTable
c.Properties
d.HashMap
e.TreeMap
12.定义一个名为Key的无继承类正确方法是?
(单选e)
a.classKey{}
b.abstractfinalclassKey{}
c.nativeclassKey{}
d.classKey{
final;
}
e.finalclassKey{}
13.下列哪一个能代表一个octalnumber?
(单选c)
a.0x12
b.320
c.032
d.(octal)2
e.12
14.当你运行Tester2类时,将得到什么标准输出结果?
(单选d)
classTester2{
intvar;
Tester(doublevar){
this.var=(int)var;
}
Tester(intvar){
this("hello");
}
Tester(Strings){
this();
System.out.println(s);
}
Tester(){
System.out.println("good-bye");
}
publicstaticvoidmain(String[]args){
Testert=newTester(5);
}
}
a.nothing
b."hello"
c.5
d."hello"followingby"good-bye"
e."good-bye"followedby"hello"
15.根据提供如下代码,如果方法tryThis()发生一个NumberFormatException,将得到什么标准结果?
(单选c)
try{
tryThis();
return;
}catch(IOExceptionx1){
System.out.println("exception1");
return;
}catch(Exceptionx2){
System.out.println("exception2");
return;
}finally{
System.out.println("finally");
}
a.Nothing
b."exception1"followedby"finally"
c."exception2"followedby"finally"
d."exception1"
e."exception2"
16.给出如下代码,如果此snippet在标准输出中显示为90,则method()调用了何种数学方法?
(单选c)
doublea=90.7;
doubleb=method(a);
System.out.println(b);
a.abs
b.min
c.floor
d.round
e.ceil
17.如果"Ran.test"文档并不存在,而你试图编译并运行以下代码时,将会发生什么情况?
(单选c)
importjava.io.*;
classRan{
publicstaticvoidmain(String[]args)throwsIOException{
RandomAccessFileout=newRandomAccessFile("Ran.test","rw");
out.writeBytes("Ninotchka");
}
}
a.ThecodedosenotcompilebecauseRandomAccessFileisnotcreatedcorrectly.
b.ThecodedosenotcompilebecauseRandomAccessFiledosenotimplementthewriteBytes()method.
c.ThecodecompilesandrunsbutthrowsIOExceptionbecause"Ran.test"doesnotyetexist.
d.Thecodecompilesandrunsbutnothingappearsintthefile"Ran.test"thatitcreates.
e.Thecodecompilesandrunsand"Ninotchka"appearsinthefile"Ran.test"thatitcreates.
18.当你创建一个新的线程时,如果提供一个目标对象,TargetObject是什么类型的实例才能作为合法参数传入:
(a)
Threadt=newThread(targetObject);
a.targetObjectinstanceofThread
b.targetObjectinstandeofObject
c.targetObjectinstanceofApplet
d.targetObjectinstanceofRunnable
e.targetObjectinstanceofString
19.给出如下的代码片断,哪一个表达式为合法的Java表达式,且返回值为真?
(单选b):
Booleanb1=newBoolean(true);
Booleanb2=newBoolean(true);
a.b1==b2
b.b1.equals(b2)
c.b1&b2
d.b1|b2
e.b1&&b2
f.b1||b2
20.下列哪一个布局管理器以从左至右,然后从上到下,以每一行为中心移动的方式排列组件?
(单选b)
a.BorderLayout
b.FlowLayout
c.GridLayout
d.CardLayout
e.GridBagLayout
21.下列哪一个关于垃圾收集的描述是正确的?
(多选de)
a.Youcandirectlyfreethememoryallocatedbyanobject.
b.Youcandirectlyrunthegarbagecollectorwheneveryouwantto.
c.Thegarbagecollectorinformsyourobjectwhenitisabouttobegarbagecollected.
d.Thegarbagecollectorreclaimsanobject'smemoryassoonasitbecomesacandidateforgarbagecollection.
e.Thegarbagecollectorrunsinlow-memorysituations.
22.Giventhisinterfacedefinition:
根据该界面定义,下列哪一个类能实现该接口并且不是抽象类?
(多选ce)
interfaceA{
intmethod1(inti);
intmethod2(intj);
}
classB
implementsA{
intmethod1(){}
intmethod2(){}
}
b.classB{
intmethod1(inti){}
intmethod2(intj){}
}
c.classBimplementsA{
intmethod1(inti){}
intmethod2(intj){}
}
d.classBextendsA{
intmethod1(inti){}
intmethod2(intj){}
}
e.classBimplementsA{
intmethod1(intj){}
intmethod2(inti){}
}
23.Examinethefollowingswitchblock根据如下的转换流,下列哪个命题是正确的?
(多选be)
charmychar='c';
switch(mychar){
default:
case'a':
System.out.println("a");break;
case'b':
System.out.println("b");break;
}
a.Thisswitchblockisillegal,becauseonlyintegerscanbeusedintheswitchstatement.
b.Thisswitchblockisfine.
c.Thisswitchblockisillegal,becausethedefaultstatementmustcomelast.
d.Whenthiscoderuns,nothingiswrittentothestandardoutput.
e.Whenthiscoderuns,theletter"a"iswrittentothestandardoutput.
24.如果你编译,运行以下类,将会出现什么情况?
(单选d)
classTest{
staticintmyArg=1;
publicstaticvoidmain(String[]args){
intmyArg;
System.out.println(myArg);
}
}
a.Thiscodecompilesanddisplays0inthestandardoutputwhenrun.
b.Thiscodecompilesanddisplays1inthestandardoutputwhenrun.
c.Thiscodedoesnotcompilebecauseyoucannotdefinealocalvariablenamedthesameasastaticvariable.
d.Thiscodedoesnotcompilebecausethelocalvariableisusedbeforeitisinitialized.
25.如果你用名为c的变量为引用组件调用addContainerListener()函数,请估计下列哪个表达式将为这个问题提出正确或错误的回答?
(单选c)
a.c==Container
b.c.equals(Class.Container)
c.cinstanceofContainer
d.cinstanceofComponent
e.cimplementsContainer
26.当下列程序运行“javaMysteryMightyMouse”命令时将得到什么结果?
(单选b)
javaMysteryMightyMouse
classMystery{
publicstaticvoidmain(String[]args){
Changerc=newChanger();
c.method(args);
System.out.println(args[0]+""+args[1]);
}
staticclassChanger{
voidmethod(String[]s){
Stringtemp=s[0];
s[0]=s[1];
s[1]=temp;
}
}
}
a.ThisprogramcausesanArrayIndexOutOfBoundsExceptiontobethrown.
b.Thisprogramrunsbutdoesnotwriteanythingtothestandardoutput.
c.Thisprogramwrites"MyightyMouse"tothestandardoutput.
d.Thisprogramwrites"MouseMyighty"tothestandardoutput.
27.根据提供的如下代码,请问在//A处应该补充什么代码该程序可以标准输出单词‘running’?
(单选a)
classRunTestimplementsRunnable{
publicstaticvoidmain(String[]args){
RunTestrt=newRunTest();
Threadt=newThread(rt);
//A
}
publicvoidrun(){
Systme.out.println("running");
}
voidgo(){
start
(1);
}
voidstart(inti){
}
}
a.System.out.println("running");
b.rt.start();
c.rt.go();
d.rt.start
(1);
28.分析下面的一行代码,选择一个正确的回答(单选c)
if(5&7>0&&5|2)System.out.println("true");
a.Thislineofcodewillnotcompile.
b.Thiscodewillcompilebutnothingwillappearinthestandardoutput.
c.Thiscodewillcompileandwritetheword"true"inthestandardoutput.
29.请选出有关在Component类中定义的paint()方法所有正确的描述:
(单选d)
a.Itisprotected.
b.IttakesaninstanceofclassGraphics.
c.Itisstatic.
d.Itisinvokedautomaticallywheneveryouminimizeandthenma