- 相關(guān)推薦
java認(rèn)證考試試題及答案
1. What gets printed when the following program
is compiled and run?
class Test {
public static void main(String args[]) {
int i;
do {
i++;
} while (i < 0);
System.out.println(i);
}
}
Select 1 correct answer:
A. The program does not compile as i is not initialized.
B. The program compiles but does not run.
C. The program compiles and runs but does not print anything.
D. The program prints 0.
E. The program prints 1.
答案:A:如果沒有初始化便使用基本變量類型,會導(dǎo)致編譯時異常,程序不能編譯。
2. What gets printed when the following program
is compiled and run?
public class XYZ {
public static void main(String args[]) {
int i,j,k;
for (i = 0; i < 3; i++)
{
for(j=1; j < 4; j++)
{
for(k=2; k<5; k++)
{
if((i == j) && (j==k))
System.out.println(i);
} } } } }
Select 1 correct answer:
A. 0
B. 1
C. 2
D. 3
E. 4
答案:C
3. Given the following code :
class Base{}
public class MyCast extends Base{
static boolean b1=false;
static int i = -1;
static double d = 10.1;
public static void main(String argv[]){
MyCast m = new MyCast();
Base b = new Base();
//Here
}
}
Which of the following, if inserted at the comment //Here
will allow the code to compile and run without error?
Select 2 correct answers:
A. b = m;
B. m = b;
C. d = i;
D. b1 = i;
解析:A 從子類型到父類型的轉(zhuǎn)換是擴展引用轉(zhuǎn)換,不需要在運行時采取特殊的動作,不會在運行時拋出異常。
B 從超類型到子類型的轉(zhuǎn)換是收縮引用轉(zhuǎn)換,需要在運行時執(zhí)行測試,以查明實際的引用值是否是新類型的合法值.如果不是, 則會拋出ClassCascException 。在這里,b本身不是MyCast類型的值,因此會導(dǎo)致運行時異常。
C 從int到double的轉(zhuǎn)換是擴展基本轉(zhuǎn)換,基本類型之間的擴展轉(zhuǎn)換永遠(yuǎn)不會導(dǎo)致運行時異常。但從int或long到float,或者是從long到double都可能導(dǎo)致精度丟失。
D 不允許進(jìn)行int和boolean之間的類型轉(zhuǎn)換
答案:A、C
4. Given the following classes which of the following
will compile without error?
interface IFace{}
class CFace implements IFace{}
class Base{}
public class ObRef extends Base{
public static void main(String argv[])
{
ObRef ob = new ObRef();
Base b = new Base();
Object o1 = new Object();
IFace o2 = new CFace();
}
}
Select 3 correct answers:
A. o1 = o2;
B. b = ob;
C. ob = b;
D. o1 = b;
解析:A 任何對象都可以賦給Object類型的對象,正確
B 子類賦給超類是擴展引用轉(zhuǎn)換,可以進(jìn)行
C 收縮引用轉(zhuǎn)換,錯誤
D 擴展引用轉(zhuǎn)換,可以進(jìn)行
答案 A B D
5. What is the result of compiling and running the following code?
1 public class Test {
2 static int total = 10;
3 public static void main (String args []) {
4 new Test();
5 }
6 public Test () {
7 System.out.println("In test");
8 System.out.println(this);
9 int temp = this.total;
10 if (temp > 5) {
11 System.out.println(temp);
12 }
13 }
14 }
Select 1 correct answer:
A. The class will not compile
B. The compiler reports an error at line 2
C. The compiler reports an error at line 9
D. The value 10 is one of the printed elements
E. The class compiles but generates a runtime error
答案:D 由于test類沒有override toString方法,故在調(diào)用println(this)時會直接顯示類的有關(guān)信息。
6. Carefully examine the following code:
public class StaticTest {
static { System.out.println("Hi there"); }
public void print() {
System.out.println("Hello");
}
public static void main(String args []) {
StaticTest st1 = new StaticTest();
st1.print();
StaticTest st2 = new StaticTest();
st2.print();
}}
When will the string "Hi there" be printed?
Select 1 correct answer:
A. Never
B. Each time a new instance is created
C. Once, when the class is first loaded into the JVM
D. Only when the static method is called explicitly
答案 C
Java虛擬機規(guī)范約定,類的初始化發(fā)生在首次主動使用時。靜態(tài)變量和靜態(tài)初始化塊的先后,實際上取決于它們在類中出現(xiàn)的先后順序。
7. Given the following code what is the effect of a being 5?
public class Test {
public void add(int a) {
loop: for (int i = 1; i < 3; i++){
for (int j = 1; j < 3; j++) {
if (a == 5) {
break loop;
}
System.out.println(i * j);
}
}
}
}
Select 1 correct answer:
A. Generates a runtime error
B. Throws an ArrayIndexOutOfBoundsException
C. Prints the values: 1, 2, 2, 4
D. Produces no output
解析:考察標(biāo)記和break的使用。單純使用break只跳出當(dāng)前循環(huán)(一層)。如果在要跳出的循環(huán)前加標(biāo)記,那么在加過標(biāo)記的循環(huán)中的任何地方都可以使用break 標(biāo)記 來跳出該循環(huán)
答案:D
8. Given the following class definition:
public class Droitwich{
class one{
private class two{
public void main(){
System.out.println("two");
}
}
}
}
Which of the following statements are true?
Select 1 correct answer:
A. The code will not compile because the classes are nested to
more than one level
B. The code will not compile because class two is marked as private
C. The code will compile and output the string two at runtime
D. The code will compile without error
答案:D
9. Given the following code:
class Base { static int oak=99; }
public class Doverdale extends Base{
public static void main(String argv[]){
Doverdale d = new Doverdale();
d.amethod();
}
public void amethod(){
//Here
}
}
Which of the following if placed after the comment //Here,
will compile and modify the value of the variable oak?
Select all possible answers:
A. super.oak=1;
B. oak=33;
C. Base.oak=22;
D. oak=50.1;
解析:當(dāng)子類沒有重新定義靜態(tài)屬性則子類的靜態(tài)屬性與父類的靜態(tài)屬性為同一個變量
當(dāng)子類重新定義了父類靜態(tài)屬性則子類的靜態(tài)屬性與父類的靜態(tài)屬性是兩個不同的變量
靜態(tài)方法調(diào)用的是定義這個靜態(tài)方法的類的靜態(tài)屬性。
答案:A B C
10. What will happen when you attempt to compile
and run the following code?
public class Inc {
public static void main(String argv[]) {
Inc inc = new Inc();
int i =0;
inc.fermin(i);
i = i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
Select 1 correct answer:
A. Compile time error
B. Output of 2
C. Output of 1
D. Output of 0
答案:D
i++和++i是什么意思?_百度知道
答 都是i=i+1的意思,區(qū)別在于i++是i先不自加,在語句完后自加,++i先自加
11. In the code fragment below, what is the value of k
after line 3 executes?
1 int i = -1;
2 int j = i >> 3;
3 int k = j & 129;
A. -1
B. 0
C. 129
D. A very large negative number
E. A very large positive number
解析:本題考察了整數(shù)在java虛擬機中的二進(jìn)制表示。
計算機中的符號數(shù)有三種表示方法,即原碼、反碼和補碼。三種表示方法均有符號位和數(shù)值位兩部分,符號位都是用0表示“正”,用1表示“負(fù)”,而數(shù)值位,三種表示方法各不相同。
在計算機系統(tǒng)中,數(shù)值一律用補碼來表示和存儲。原因在于,使用補碼,可以將符號位和數(shù)值域統(tǒng)一處理;同時,加法和減法也可以統(tǒng)一處理。此外,補碼與原碼相互轉(zhuǎn)換,其運算過程是相同的,不需要額外的硬件電路。
正整數(shù)的補碼是其二進(jìn)制表示,與原碼相同,
求負(fù)整數(shù)的補碼,將其對應(yīng)正數(shù)二進(jìn)制表示所有位取反(包括符號位,0變1,1變0)后加1。
簡單起見,我們不表示出所有32位,僅表示十二位。
i : -1 1::0000 0000 0001 -1: 1111 1111 11111
第二行,對i進(jìn)行了移位操作,>>為帶符號右移,其移出的空位需要用符號位補齊。格式為 需要移位的數(shù)字>>要移的位數(shù)
故j = 1111 1111 1111
第三行,&為按位與運算符,129=0000 1000 0001,k=0000 1000 0001,即129
相關(guān)文章推薦:
3.2016年JAVA認(rèn)證經(jīng)典面試題匯編
故答案為C。
12. What is the result after the following code executes?
1 short s = 0x00FD;
2 byte b = (byte)s;
3 System.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
解析:考察對強制類型轉(zhuǎn)換的理解,java中,short類型為16位,占據(jù)兩個字節(jié),byte類型為八位。第二行,對s進(jìn)行了強制類型轉(zhuǎn)換,而這實際上是一個收縮基本轉(zhuǎn)換,從帶符號整數(shù)到整型T的收索轉(zhuǎn)換只是簡單地丟棄除n個最低階位以外的其它所有位,這可能導(dǎo)致數(shù)目或者符號的變化。
Java中所有的整數(shù)類型都具有符號位,故需要考慮二進(jìn)制表示
b=0xFD=1111 1101,是一個補碼。
將最高位為1的補碼轉(zhuǎn)換為原碼的步驟為(最高位為0的補碼和原碼相同)先將補碼全部取反,然后+1
b取反后得到0000 0010,加1 : 0000 0011,即3,考慮到這是一個附負(fù)數(shù),最后的結(jié)果為-3
答案:D
13. Given the following method in an application:
1. public String setFileType( String fname ){
2. int p = fname.indexOf( '.' );
3. if( p > 0 ) fname = fname.substring( 0,p );
4. fname += ".TXT";
5. return fname;
6. }
and given that another part of the class has the following code:
7. String TheFile = "Program.java";
8. File F = new File( setFileType( TheFile ) );
9. System.out.println( "Created " + TheFile );
what will be printed by the statement in line 9?
Select 1 correct answer:
A. Created Program.java
B. Created Program.txt
C. Created Program.java.txt
答案:A
14. Here is the ActionEvent family tree:
java.lang.Object
|--- java.util.EventObject
|--- java.awt.AWTEvent
|---- java.awt.event.ActionEvent
Suppose we have the following code to count events and
save the most recent event:
int evtCt = 0 ;
AWTEvent lastE ;
public void saveEvent( AWTEvent evt )
{
lastE = evt ;
evtCt++ ;
}
Which of the following calls of saveEvent would run
without causing an exception?
Select all possible answers:
A. call with an AWTEvent object reference
B. call with an ActionEvent object reference
C. call with an EventObject object reference
D. call with null value
答案:ABD
15. Suppose we have two classes defined as follows:
class ApBase extends Object implements Runnable
class ApDerived extends ApBase implements Observer
Given two variables 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無論在什么情況下都會執(zhí)行。
對于double.valueof,當(dāng)輸入為null(注意,不是字符串null,字符串null仍然會導(dǎo)致NumberFormatException)時,會導(dǎo)致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 原因:靜態(tài)方法操作的必須是靜態(tài)變量
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
相關(guān)文章推薦:
3.2016年JAVA認(rèn)證經(jīng)典面試題匯編
【java認(rèn)證考試試題及答案】相關(guān)文章:
Java認(rèn)證考試真題及答案10-11
2016年Java認(rèn)證考試題03-08
2017年Java認(rèn)證考試試題03-04
java考試試題及答案10-25
華為認(rèn)證考試試題及答案03-05