java认证考试问题及答案
所以答案是c。
12.whatistheresultafterthefollowingcodeexecutes?
1 short s=0x00FD软盘;
2字节=(字节) s;
3system.out.println(b;
Select 1 correct answer:
A. Compile time error in line 1
B. Compile time error in line 2
C. 0
D. -3
E. -2
解析:考察对强制类型转换的理解,在java中,短类型为16位,占2字节,byte类型为8位。 在第二行中,我们强制对s进行了类型转换,但这实际上是收缩的基本转换,从有符号整数到整数t的电缆转换可能会改变数量和符号,而只是简单地丢弃除了n个最低有效位以外的所有位。
所有Java整数类型都有符号位,因此必须考虑二进制表示
b=0xFD=1111 1101是补数。
将最高位为1的补数转换为原始代码的步骤是(最高位为0的补数与原始代码相同),首先将补数全部反向后再转换为1
如果将b反转,则得到0000 0010,将1 : 0000 0011即3相加,考虑到其为带负数,最终结果为-3
答案: d
13.giventhefollowingmethodinanapplication :
1.publicstringsetfiletype (字符串名称) {
2. int p=fname.indexOf ('.';
3.if(p0 ) fname=fname.substring(0,p );
4. fname ='.TXT ';
5 .返回名称;
6. }
andgiventhatanotherpartoftheclasshasthefollowingcode :
7. String TheFile='Program.java ';
8.filef=新文件(设置文件类型) thefile );
9.system.out.println (创建' the file );
watwillbeprintedbythestatementinline 9?
Select 1 correct answer:
A. Created Program.java
B. Created Program.txt
C. Created Program.java.txt
答案:答
14.hereistheactioneventfamilytree :
java.lang.Object
|--- java.util.EventObject
|--- java.awt.AWTEvent
|---- Java.awt.event.actionevent
supposewehavethefollowingcodetocounteventsand
保存最后一次事件:
int evtCt=0;
awt事件最后;
公共语音事件(活动事件) )。
{
lastE=evt;
evtCt;
}
whichofthefollowingcallsofsaveeventwouldrun
without causing an exception?
选择所有可订购answers :
a.callwithanawteventobjectreference
b.callwithanactioneventobjectreference
c.callwithaneventobjectobjectreference
d.call with空值
答案: ABD
15.supposewehavetwoclassesdefinedasfollows 3360
classapbaseextendsobjectimplementsrunnable
classapderivedextendsapbaseimplementsobserver
Given two va
riables created as follows:ApBase aBase = new ApBase() ;
ApDerived aDer = new ApDerived();
Which of the following Java code fragments will
compile and execute without error?
Select 1 correct answer:
A. Object obj = aBase ; Runnable rn = obj ;
B. Object obj = aBase ; Runnable rn = (Runnable) obj ;
C. Object obj = aBase ; Observer ob = (Observer)aBase ;
D. Object obj = aDer ; Observer ob2 = obj ;
答案:B?
16. The following lists the complete contents
of the file named Derived.java:
1. public class Base extends Object {
2. String objType ;
3. public Base(){ objType = "I am a Base type" ;
4. }
5. }
6.
7. public class Derived extends Base {
8. public Derived() { objType = "I am a Derived type";
9. }
10. public static void main(String args[] ){
11. Derived D = new Derived();
12. }
13. }
What will happen when this file is compiled?
Select 1 correct answer:
A. Two class files, Base.class and Derived.class will be created
B. The compiler will object to line 1
C. The compiler will object to line 7
解析:
答案:B
17. The following method is designed to convert an input string
to a floating point number, while detecting a bad format.
Assume that factor is correctly defined elsewhere:
public boolean strCvt( String s ){
try {
factor = Double.valueOf( s ).doubleValue();
return true ;
} catch(NumberFormatException e){
System.out.println("Bad number " + s );
factor = Double.NaN ;
}finally { System.out.println("Finally");
}
return false ; }
Which of the following descriptions of the results of various
inputs to the method are correct? Select all possible answers:
A. Input = "0.234"
Result:factor = 0.234, "Finally" is printed, true is returned.
B. Input = "0.234"
Result:factor = 0.234, "Finally" is printed, false is returned.
C. Input = null
Result:factor = NaN, "Finally" is printed, false is returned.
D. Input = null
Result:factor unchanged,"Finally" is printed,
NullPointerException is thrown.
解析:finally无论在什么情况下都会执行。
对于double.valueof,当输入为null(注意,不是字符串null,字符串null仍然会导致NumberFormatException)时,会导致NullPointerException
答案:A D
18. Here is the hierarchy of Exceptions related to
array index errors:
Exception
+-- RuntimeException
+-- IndexOutOfBoundsException
+-- ArrayIndexOutOfBoundsException
+-- StringIndexOutOfBoundsException
Suppose you had a method X which could throw both
array index and string index exceptions. Assuming
that X does not have any try - catch statements,
which of the following statements are correct?
A. The declaration for X must include
"throws ArrayIndexOutOfBoundsException,
StringIndexOutOfBoundsException".
B. If a method calling X catches IndexOutOfBoundsException, both
array and string index exceptions will be caught.
C. If the declaration for X includes "throwsIndexOutOfBoundsException",
any calling method must use a try - catch block.
D. The declaration for X does not have to mention exceptions.
解析:RuntimeException是运行时异常,属于不可查异常,程序无需声明,也无需处理。
答案:BD
19. Given the following listing of the Widget class:
1 class Widget extends Thingee{
2 static private int widgetCount = 0 ;
3 public String wName ;
4 int wNumber ;
5
6 static synchronized int addWidget(){ widgetCount++ ;
7 wName = "I am Widget # " + widgetCount ;
8 return widgetCount ;
9 }
10 public Widget(){
11 wNumber = addWidget();
12 }
13 }
What happens when we try to compile the class and use
multiple Widget objects in a program?
Select 1 correct answer:
A. The class compiles and each Widget will get a unique wNumber
and wName reflecting the order in which the Widgets were created.
B. The compiler objects to line 7
C. A runtime error occurs in the addWidget method
答案:B 原因:静态方法操作的必须是静态变量
20. Given the following class definition:
public class DerivedDemo extends Demo
{
int M, N, L ;
public DerivedDemo( int x, int y )
{
M = x ; N = y ;
}
public DerivedDemo( int x )
{
super( x );
}
}
Which of the following constructor signatures MUST exist
in the Demo class for DerivedDemo to compile correctly?
Select 2 correct answers:
A. public Demo( int a, int b )
B. public Demo( int c )
C. public Demo( )
答案:B C
相关文章推荐:
【java认证考试试题及答案】相关文章: