第八章 判断题-字符串

更新时间:2023-10-28 15:23:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第八章 判断题

1.用= =比较字符串对象时,如果字符串包含的是同一个值时,结果为true。( ) 2.字符串在创建后可以被修改。( )

3.String类的charAt方法返回字符穿串中字符的个数。( )

4.String类的compareTo方法在所比较的字符串相等时返回0。( )

5.运算符“==”用于比较引用时,如果两个引用指向内存中同一个对象,则返回true。( ) 6.StringBuffer类的indexOf方法在字符串中定位某个字符或子串第一次出现的位置。( ) 7.String类的substring方法拷贝并返回字符串对象的一部分。( ) 8.String类的replace方法只返回一个新的字符串对象。( ) 9.String类的valueOf方法由它的参数转换成的字符串。( )

10.StringTokenizer类的hasMoreTokens方法确定在字符串中是否还有语言符号返回。(第八章 选择题

1.下面语句书写错误的是:

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”; 2.请看下面的代码 String s = “hello”; String t = “hello”;

String c[ ] = {?h?,?e?,?l?,?l?,?o?}; 下面哪一选项的语句返回值为真: A.s .equals(t); B.t .equals(c); C.s==t;

D.t .equals(new String(“hello”)); E. t==c; 3.请看下面的代码 String s = “story”; 下面选项语句书写正确的是: A.s += “books”; B.char c = s[1]; C.int len = s .length;

D.String t = s.toLowerCase( ); 4.请看下面的代码 1.class Example { 2. String str;

) 3. public Example( ){ 4. str = “example”; 5. }

6. public Example(String s){ 7. str = s; 8. } 9..}

10.class Demo extends Example { 11.}

12.public class Test{ 13. public void f(){

14. Example ex = new Example(“good”); 15. Demo d = new Demo(“Good”); 16. } 17.}

那一行将导致发生错误: A.3 B.6 C.10 D.14 E.15

5.请看下面的代码 public class Example{

String str = new String(“hello”); Char ch[ ]={?d?,?b?,?c?};

public static void main(String args[ ]){ Example ex=new Example( ); ex.change(ex.str,ex.ch);

System .out .println(ex.str +“and”+ex.ch); }

public void change(String str,char ch[ ]){ str=”world”; ch[0]=?a?; } }

该程序的输出结果是: A.hello dbc B.hello abc C.world dbc D.world abc

6.下面选项正确创建一个字符型数组(含有4个元素)的是: A.String a[ ] = new String[4]; for(int i=0;i<4;a[i++]=””); B.String a[ ]={“”,””,””,””};

C.String a[4]; D.String [4]a;

E. String [ ]a = new String[4]; For(int i=0;i<4;a[i++]=null); 7.下面选项正确的是:

A.String temp[ ]= new String{“j””a””z”}; B.String temp[ ]= {“j””a””z”}; C.String temp= {“j”,”a”,”z”}; D.String temp[ ]= {“j”,”a”,”z”}; 8.请看下面的代码

1.StringBuffer sb = new StringBuffer(“abc”); 2.String s = new String(“abc”); 3.sb.append(“def”); 4.s.append(“def”); 5.sb.inser(1,”zzzz”); 6.s.concat(sb); 7.s.trim( ); 下面说法正确的是:

A.编译时在第一行发生一个错误 B.编译时在第二行发生一个错误 C.编译时在第三行发生一个错误 D.编译时在第四行发生一个错误 E. 编译时在第五行发生一个错误 F. 编译时在第六行发生一个错误 G. 编译时在第七行发生一个错误 9.请看下面的代码

String s1=new String(“hello”); String s2=new String(“there”); String s3=new String( ); 下面选项中语句正确的是: A.s3=s1+s2; B.s3=s1-s2; C.s3=s1&s2; D.s3=s1&&s2; 10.请看下面的代码 public class StrEq{

public static void main(String argv[ ]){ StrEq s = new StrEq( ); }

private StrEq( ){ String s = “Marcus”;

String s2 = new String(“Marcus”); If(s == s2){

System .out .println(“we have a match”);

}else{

System .out .pritln(“Not equal”); } } }

下面说法正确的是:

A.由于使用“private StrEq”编译时会出现错误 B.输出“we have a match” C.输出“Not equal”

D.由于使用“==”操作符导致编译时出现错误 第八章 程序设计题

1.编写Applet程序,接受用户输入的一个字符串和一个字符,把字符串中所有指定的字符删除后输出。 2.编程判断一个字符串是否回文。

3.编写一个应用程序,它使用card对象创建了一副52张的纸牌,然后使用户能够通过单击Deal card按钮来发牌,在JtextField中显示了所发的牌,用户还可以随时单击Shuffle card按钮来洗牌。 4.编写一个应用程序,对两个大型字符串进行比较。

5.编写一个应用程序,用来演示StringBuffer类提供的10个重载append方法。 第八章判断题答案 1.难度:容易 答案:错误

知识点:用运算符“= =”比较字符串对象,实际上是比较并确定它们是否内存中的同一个对象。 2.难度:容易 答案:错误

知识点:字符串对象是常量,在创建后不能修改。 3.难度:容易 答案:错误

知识点:String类的charAt方法返回指定位置上的字符。 4.难度:容易 答案:正确

知识点:不仅如此,如果调用compareTo的字符串比作为参数的字符串小,则返回一个负数;如果调用compareTo的字符串比作为参数的字符串大时,则返回一个正数。 5.难度:适中 答案:正确

知识点:运算符“==”用于比较基本类型的数据时,如果两个值相同则返回true,并且在比较引用时,如果两个引用指向内存中同一个对象,则返回true。 6.难度:适中 答案:错误

知识点:indexOf方法是String类的方法。 7.难度:容易 答案:正确

知识点:String类的substring方法的使用。 8.难度:适中 答案:错误

知识点:String类的replace方法不仅返回一个新的字符串对象,而且该对象中原字符串所以的由第1个字符参数指定的字符都被第2个字符参量替换。 9.难度:容易 答案:正确

知识点:String类的valueOf方法的使用。 10.难度:适中 答案:正确

知识点:StringTokenizer类的hasMoreTokens方法的使用。 第八章 选择题答案 1.难度:容易 答案:BD

知识点:字符串不是数组,不能使用s[3];“-”操作符在字符串中不能使用。 2.难度:适中 答案:AD

知识点:s和t不是相同的引用,而在使用==比较引用时,如果两个引用指向内存中的同一个对象,则结果为真。 3.难度:容易 答案:AD

知识点:s是字符串不是数组,因此B错误;C错误是由于length后要加括号。 4.难度:适中 答案:E

知识点:类Demo没有被初始化,在15行对它的引用导致了一个错误。 5.难度:适中 答案:B

知识点:由于str=”world”语句,str的引用为world,而不是hello。 6.难度:容易 答案:AB

知识点:每个数组元素在初始化之前都为null。 7.难度:容易 答案:D

知识点:数组的正确初始化。 8.难度:容易 答案:DF

知识点:append方法是StringBuffer类的方法,而contcat方法是String类的方法。 9.难度:适中 答案:A

知识点:“+”操作符的使用。 10.难度:适中 答案:C

知识点:“==”操作符的使用。 第八章 程序设计题答案 1.难度:适中

答案:源程序:Zifu.java

import java.applet.*; import java.awt.*; import java.awt.event.*;

public class Zifu extends Applet implements ActionListener {

String originalString, modifiedString; int delChar;

Label orgStringLbl = new Label(\原始字符串:\Label delCharLbl = new Label(\需去除字符:\TextField orgStringTfd = new TextField(20); TextField delCharTfd = new TextField(1); Button modifyBtn = new Button(\删除字符\public void init() {

add(orgStringLbl); add(orgStringTfd); add(delCharLbl); add(delCharTfd); add(modifyBtn);

orgStringTfd.setText(\delCharTfd.setText(\originalString = \modifiedString = \

modifyBtn.addActionListener(this); }

public void paint(Graphics g) {

g.drawString(modifiedString, 10, 150); }

public void actionPerformed(ActionEvent ae) {

if(ae.getSource() == modifyBtn) {

originalString = orgStringTfd.getText() ;

delChar = (int) (delCharTfd.getText() .charAt(0));

modifiedString = \int i=0, j=0;

while( (j=originalString.indexOf(delChar,i)) != -1 ) {

System.out.println(i + \System.out.println(originalString);

modifiedString = modifiedString + originalString.substring(i,j);

System.out.println(modifiedString); i = j+1; }

modifiedString = modifiedString + originalString.substring(i,originalString.length() ); repaint() ; } else {

showStatus(\事件\+ \未定义处理操作。\orgStringTfd.setText(\delCharTfd.setText(\originalString = \modifiedString = \} } }

知识点:对字符和字符串的操作。 2.难度:适中

答案:源程序:Huiwen.java import java.io.*; public Huiwen {

public static void main(String args[]) {

System.out.println(\请输入一个字符串:\

BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); String str = br.readLine(); if(isReversable(str))

System.out.println(\这个字符串是回文。\\n\else

System.out.println(\这个字符串不是回文。\\n\}

public static boolean isReversable(String s) {

for(int i=0,j=s.length() -1; i

if(s.charAt(i) != s.charAt(j)) return false; }

return true; }

}

知识点:对字符串的操作。 3.难度:难

答案:源程序:Deckofcards.java import javax.swing.*; import java.awt.*; import java.awt.event.*;

public class Deckofcards extends JFrame { private Card deck[]; private int currentCard;

private JButton dealButton, shuffleButton; private JTextField displayCard; private JLabel status; public DeckOfCards() {

super( \String faces[] = {

\\;

String suits[] = {

\;

deck = new Card[ 52 ]; currentCard = -1;

for ( int i = 0; i < deck.length; i++ ) deck[ i ] = new Card( faces[ i % 13 ],suits[ i / 13 ] ); Container c = getContentPane(); c.setLayout( new FlowLayout());

dealButton = new JButton( \

dealButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) {

Card dealt = dealCard() ;

if ( dealt != null ) {

displayCard.setText( dealt.toString() );

status.setText( \} else {

displayCard.setText( \status.setText( \} }

} );

c.add( dealButton );

shuffleButton = new JButton( \shuffleButton.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent e ) {

displayCard.setText( \shuffle();

displayCard.setText( \} } );

c.add( shuffleButton );

displayCard = new JTextField( 20 ); displayCard.setEditable( false ); c.add( displayCard ); status = new JLabel(); c.add( status ); setSize( 275, 120 );

// set the window size show(); // show the window } public void shuffle() {

currentCard = -1;

for ( int i = 0;i < deck.length;i++ ) { int j = ( int ) ( Math.random() * 52 );

Card temp = deck[ i ]; // swap deck[ i ] = deck[ j ]; // the deck[ j ] = temp; // cards }

dealButton.setEnabled( true ); }

public Card dealCard() {

if ( ++currentCard < deck.length ) return deck[ currentCard ]; else {

dealButton.setEnabled( false ); return null; } }

public static void main( String args[] )

{

DeckOfCards app = new DeckOfCards();

app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) {

System.exit( 0 ); } } ); } }

class Card { private String face; private String suit;

public Card( String f, String s ) { face = f; suit = s; }

public String toString() {

return face + \} }

知识点:引用程序的构造函数实例化。 4.难度:容易

答案:源程序:Bijiao .java import javax.swing.*; public class Bijiao {

public static void main( String args[] ) {

String s1, s2, s3, s4, output; s1 = new String( \s2 = new String( \

// Test strings to determine if they are the same // String object in memory. if ( s1 == s2 )

output = \else

output = \// Test strings for equality of contents if ( s1.equals( s2 ) )

output += \else

本文来源:https://www.bwwdw.com/article/rpe2.html

Top