下面哪些表達(dá)式是聲明一個含有10個String對象的數(shù)組?
說明理由。(C)
A.char str[]; B. char str[][];
C. String str[]=new String[10]; D. String str[10];
解:選項C聲明的是一個含有10個String對象的數(shù)組。選項A和選項B聲明的是字符對象數(shù)組。選項D的形式是不對的。
根據(jù)下列語句判斷哪些表達(dá)式返回true。說明理由。(D)
String s=”hello ”;
String t=” hello”;
Char c[]={‘h’,‘e’,‘l’,‘l’,‘o’};
A.s.equels(t); B.t.equals(c);
C.s==t; D.t.equals(new String(“hello”))
E.t==c.
解:因為==操作符比較的是操作符兩端的操作數(shù)是否是同一個對象,而String的equals()方法比較的是兩個String對象的內(nèi)容是否一樣,其參數(shù)是一個String對象時才有可能返回true,其他對象都返回false。所以只有選項D返回true。
下面的哪些程序片段可能導(dǎo)致錯誤?說明理由。(B D)
A. String s = “Gone with the wind”;
String t = “good”;
String k = s+t;
B. String s = “Gone with the wind”;
String t;
t = s[3]+“one”;
C. String s = “Gone with the wind”;
String standard = s.toUpperCase();
D. String s = “home directory”;
String t = s – “directory”;
解:因為選項A中使用的是String類型可以直接使用的連接運算+,是正確的。選項B是錯誤的,因為String是一種對象,不是簡單的字符數(shù)組,不能使用下標(biāo)運算符取其值的某個元素。選項C也是正確的,因為toUpperCase()方法是String對象的一個方法,作用是將字符串的內(nèi)容全部轉(zhuǎn)換為大寫并返回轉(zhuǎn)換后的結(jié)果(String類型)。選項D是錯誤的,因為String類型不能進(jìn)行減(-)運算。
2015職稱計算機(jī)考試書PowerPoint2007中 .. 定價:¥45 優(yōu)惠價:¥42 更多書籍 | |
2015年全國職稱計算機(jī)考試教材(2007模 .. 定價:¥225 優(yōu)惠價:¥213 更多書籍 |