1)Which code fragment would correctly identify the number of arguments passed via the command line to a Java application, excluding the name of the class that is being invoked?

哪个代码片段可以正确地标识通过命令行传递给Java应用程序的参数的数量,而不包括正在调用的类的名称?

A)int count=0; while (!(args[count].equals(""))) count ++;  
B)int count = args.length - 1;  
C)int count = args.length;  
D)int count = 0; while (args[count] != null) count ++;

3)Analyze the following code.

class Test {
public static void main(String[ ] args) {
String s;
System.out.println("s is " + s);
}
}

A)The program compiles and runs fine.
B)The program has a runtime error because s is not initialized, but it is referenced in the println statement.
C)The program has a compilation error because s is not initialized, but it is referenced in the println statement.
D)The program has a runtime error because s is null in the println statement.

4)How can you get the word "abc" in the main method from the following call?
java Test "+" 3 "abc" 2   4) _______
A)args[2] B) args[1] C) args[3] D) args[0]

5)What is the output of the following code?

public class Test {
public static void main(String[ ] args) {
String s1 = new String("Welcome to Java!");
String s2 = new String("Welcome to Java!");
if (s1.equals(s2))
System.out.println("s1 and s2 have the same contents");
else
System.out.println("s1 and s2 have different contents");
}
}

A)s1 and s2 have the same contents

B)s1 and s2 have different contents

s.equals()方法是判断字符串的内容是否相等,只要内容相等就返回true

6)What is the output of the following code?

public class Test {
public static void main(String[ ] args) {
String s1 = "Welcome to Java!";
String s2 = "Welcome to Java!";
if (s1 == s2)
System.out.println("s1 and s2 reference to the same String object");
else
System.out.println("s1 and s2 reference to different String objects");
}
}

A)s1 and s2 reference to the same String object

B)s1 and s2 reference to different String objects

"=="方法是判断两个字符串的地址是否相同,即如果有a,b两个字符串,他们都指向同一个对象,即同一个地址,则用"=="返回true

7)Which of the following is true? (Choose all that apply.) 7) _______
A)You can add characters into a string buffer.
B)The capacity of a string buffer can be automatically adjusted.
C)You can reverse the characters in a string buffer.
D)You can delete characters into a string buffer.

String类是字符串常量,是不可更改的常量。而StringBuffer是字符串变量,它的对象是可以扩充和修改的。

8)To check if a string s contains the suffix "Java", you may write  (Choose all that apply.)
A)if (s.charAt(s.length() - 4) == 'J' && s.charAt(s.length() - 3) == 'a' && s.charAt(s.length() - 2) == 'v' && s.charAt(s.length() - 1) == 'a') ...
B)if (s.substring(s.length() - 5).equals("Java")) ... s可能只包含有“Java”
C)if (s.lastIndexOf("Java") >= 0) ... lastIndexOf返回值一定是大于0的!不存在找不到返回-1的情况!
D)if (s.endsWith("Java")) ...
E)if (s.substring(s.length() - 4).equals("Java")) ...

9)What is displayed by the following code?

  public static void main(String[ ] args) throws Exception {
String[ ] tokens = "Welcome to Java".split("o");
for (int i = 0; i < tokens.length; i++) {
System.out.print(tokens[i] + " ");
}
}

A)Welc me to Java

B) Welcome to Java
C)Welcome t  Java

D) Welc me t  Java

split  函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回

11)Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.

Scanner scanner = new Scanner(System.in);
int value = scanner.nextDouble();
int doubleValue = scanner.nextInt();
String line = scanner.nextLine();

A)After the last statement is executed, intValue is 34. 注意这里没有进行强制类型转换!
B)After the last statement is executed, line contains characters '7', '8', '9', '\n'.
C)After the last statement is executed, line contains characters '7', '8', '9'.
D)The program has a runtime error because 34.3 is not an integer.

13)Which of following is not a correct method in Character? (Choose all that apply.) 13) ______
A)isLetter(char) B)isDigit() C)isLetterOrDigit(char) D)toLowerCase(char) E)toUpperCase()

B、E需要有参数

16)________ returns the last character in a StringBuilder variable named strBuf? 16) ______
A)StringBuilder.charAt(strBuf.capacity() - 1)
B)strBuf.charAt(strBuf.capacity() - 1)
C)StringBuilder.charAt(strBuf.length() - 1)
D)strBuf.charAt(strBuf.length() - 1)

17)Assume StringBuilder strBuf is "ABCCEFC", after invoking ________, strBuf contains "ABTTEFT". 17) ______
A)strBuf.replace("C", "T")
B)strBuf.replace("CC", "TT")
C)strBuf.replace('C', 'T')
D)strBuf.replace('C', "TT")
E)strBuf.replace(2, 7, "TTEFT")

19)Analyze the following code.

class Test {
public static void main(String[ ] args) {
StringBuilder strBuf = new StringBuilder(4);
strBuf.append("ABCDE");
System.out.println("What's strBuf.charAt(5)? " + strBuf.charAt(5));
}
}

A)The program has a runtime error because the length of the string in the buffer is 5 after "ABCDE" is appended into the buffer. Therefore, strBuf.charAt(5) is out of range.
B)The program compiles and runs fine.
C)The program has a runtime error because because the buffer's capacity is 4, but five characters "ABCDE" are appended into the buffer.
D)The program has a compilation error because you cannot specify initial capacity in the StringBuilder constructor.

20)Suppose Character x = new Character('a'), ________ returns true. (Choose all that apply.)

A)x.equals(new Character('a'))
B)x.compareToIgnoreCase('A')
C)x.equals('a')
D)x.equals("a")
E)x.equalsIgnoreCase('A')

21)The following program displays ________.

public class Test {
public static void main(String[ ] args) {
String s = "Java";
StringBuilder buffer = new StringBuilder(s);
change(s);
System.out.println(s);
}
private static void change(String s) {
s = s + " and HTML";
}
}

A)Java and HTML B) and HTML C)nothing is displayed D) Java

StringBuilder是可变类,String是不可变类!!

22)To check if a string s contains the prefix "Java", you may write (Choose all that apply.)
A)if (s.indexOf("Java") == 0) ...
B)if (s.startsWith("Java")) ...
C)if (s.substring(0, 4).equals("Java")) ...
D)if (s.charAt(0) == 'J' && s.charAt(1) == 'a' && s.charAt(2) == 'v' && s.charAt(3) == 'a') ...

23)Which of the following statements is preferred to create a string "Welcome to Java"?
A)String s; s = new String("Welcome to Java");
B)String s = "Welcome to Java";
C)String s; s = "Welcome to Java";
D)String s = new String("Welcome to Java");

注意题目的要求,更适合!其实D也是正确的,但没有B更适合!

26)Assume StringBuilder strBuf is "ABCDEFG", after invoking ________, strBuf contains "AEFG". 26) ______
A)strBuf.delete(1, 3) B) strBuf.delete(2, 4) C)strBuf.delete(0, 3) D) strBuf.delete(1, 4)

从指定位置beginIndex的字符开始到下标为endIndex-1的字符,注意位于endIndex位置的字符不属于该字符串的一部分!

28)Which of the following is the correct statement to return a string from an array a of characters?
A)toString(a) B) convertToString(a) C)new String(a) D) String.toString(a)

29)What is the return value of "SELECT".substring(0, 5)? 29) ______
A)"SELECT" B) "SELE" C)"SELEC" D) "ELECT"

从指定位置beginIndex的字符开始到下标为endIndex-1的字符,注意位于endIndex位置的字符不属于该字符串的一部分!

30)Suppose s1 and s2 are two strings. What is the result of the following code?
    s1.equals(s2) == s2.equals(s1)
A)true B) false

32)Assume s is "ABCABC", the method ________ returns an array of characters. 32) ______
A)String.toCharArray()
B)String.toChars()
C)s.toCharArray()
D)s.toChars()
E)toChars(s)

33)Which correctly creates an array of five empty Strings? 33) ______
A)String[ ] a = new String [5];  
B)String[ ] a = {"", "", "", "", ""};  
C)String[ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);  
D)String[5] a;

34)Assume StringBuilder strBuf is "ABCDEFG", after invoking ________, strBuf contains "ABCRRRRDEFG". 34) ______
A)strBuf.insert(1, "RRRR") B) strBuf.insert(3, "RRRR")
C)strBuf.insert(2, "RRRR") D) strBuf.insert(4, "RRRR")

35)Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect? (Choose all that apply.) 35) ______
A)s1.charAt(0) = '5'
B)String s = new String("new string");
C)String s3 = s1 + s2
D)s1 >= s2
E)int i = s1.length

E需要写成int i = s1.length()

36)Suppose s1 and s2 are two strings. Which of the following statements or expressions is incorrect? (Choose all that apply.) 36) ______
A)char c = s1.charAt(s1.length()); 数组越界
B)boolean b = s1.compareTo(s2); 必为flase
C)char c = s1[0];
D)String s3 = s1 - s2;

38)Identify the problems in the following code.

public class Test {
public static void main(String argv[ ]) {
System.out.println("argv.length is " + argv.length);
}
}

A)The program has a compile error because String argv[ ] is wrong and it should be replaced by String[ ] args.
B)If you run this program without passing any arguments, the program would display argv.length is 0.
C)If you run this program without passing any arguments, the program would have a runtime error because argv is null.
D)The program has a compile error because String args[ ] is wrong and it should be replaced by String args[ ].

39)What is the return value of "SELECT".substring(4, 4)? 39) ______
A)T B) an empty string C)C D) E

40)Assume s is "   abc  ", the method ________ returns a new string "abc". 40) ______
A)trim(s) B) String.trim(s) C)s.trim(s) D) s.trim()

trim()的作用是去掉字符串两端的多余的空格,注意,是两端的空格,且无论两端的空格有多少个都会去掉,当然中间的那些空格不会被去掉

41)"abc".compareTo("aba") returns ________. 41) ______
A)1 B) 0 C) -1 D) -2 E) 2

从两个字符串的第一位开始比较, 如果遇到不同的字符,则马上返回这两个字符的ascii值差值.返回值是int类型

42)What is the output of the following code?

public class Test {
public static void main(String[ ] args) {
String s1 = new String("Welcome to Java");
String s2 = s1;
s1 += "and Welcome to HTML";
if (s1 == s2)
System.out.println("s1 and s2 reference to the same String object");
else
System.out.println("s1 and s2 reference to different String objects");
}
}

A)s1 and s2 reference to the same String object
B)s1 and s2 reference to different String objects

44)Which of the following is the correct header of the main method? (Choose all that apply.)

A)static void main(String[ ] args)
B)public static void main(String args[ ])
C)public static void main(String x[ ])
D)public static void main(String[ ] x)
E)public static void main(String[ ] args)

45)What is displayed by the following statement?
        System.out.println("Java is neat".replaceAll("is", "AAA")); 45) ______
A)JavaAAA neat B) Java AAAneat C)Java AAA neat D) JavaAAAneat

46)"AbA".compareToIgnoreCase("abC") returns ________. 46) ______
A)2 B) -1 C) -2 D) 0 E) 1

compareToIgnoreCase不区分大小写的比较,从两个字符串的第一位开始比较, 如果遇到不同的字符,则马上返回这两个字符的ascii值差值.返回值是int类型

47)The StringBuilder methods ________ not only change the contents of a string buffer, but also returns a reference to the string buffer. (Choose all that apply.) 47) ______
A)insert B)replace C)delete D)append E)reverse

48)________ returns true. (Choose all that apply.) 48) ______
A)"peter".equalsIgnoreCase("Peter")
B)"peter".compareToIgnoreCase("peter")
C)"peter".compareToIgnoreCase("Peter")
D)"peter".equals("peter")
E)"peter".equalsIgnoreCase("peter")

equalsIgnoreCase不区分大小写比较

49)The following program displays ________.

public class Test {
public static void main(String[ ] args) {
String s = "Java";
StringBuilder buffer = new StringBuilder(s);
change(buffer);
System.out.println(buffer);
}
private static void change(StringBuilder buffer) {
buffer.append(" and HTML");
}
}

A)Java B) Java and HTML C)nothing is displayed D) and HTML

StringBuilder是可变类!!

50)Suppose s is a string with the value "java". What will be assigned to x if you execute the following code?
char x = s.charAt(4);
A)'a' B)'v'
C)Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

51)What is the output of the following code?

public class Test {
public static void main(String[ ] args) {
String s1 = new String("Welcome to Java!");
String s2 = new String("Welcome to Java!");
if (s1 == s2)
System.out.println("s1 and s2 reference to the same String object");
else
System.out.println("s1 and s2 reference to different String objects");
}
}

这里s1与s2都是新new出来的,指向两个对象!
A)s1 and s2 reference to different String objects B)s1 and s2 reference to the same String object

52)________ returns a string. (Choose all that apply.) 52) ______
A)String.valueOf(new char[ ]{'a', 'b', 'c'})
B)String.valueOf(123)
C)String.valueOf(12.53)
D)String.valueOf(false)

String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 ,也就是 String.valueOf() 这个参数多载的方法

有以下几种

(1)String.valueOf(boolean b) : 将 boolean 变量 b 转换成字符串 
(2)String.valueOf(char c) : 将 char 变量 c 转换成字符串 
(3)String.valueOf(char[] data) : 将 char 数组 data 转换成字符串 
(4)String.valueOf(char[] data, int offset, int count) : 将 char 数组 data 中 由 data[offset] 开始取 count 个元素 转换成字符串

(5)String.valueOf(double d) : 将 double 变量 d 转换成字符串 
(6)String.valueOf(float f) : 将 float 变量 f 转换成字符串 
(7)String.valueOf(int i) : 将 int 变量 i 转换成字符串 
(8)String.valueOf(long l) : 将 long 变量 l 转换成字符串 
(9)String.valueOf(Object obj) : 将 obj 对象转换成 字符串, 等于 obj.toString()

53)Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.

Scanner scanner = new Scanner(System.in);
int intValue = scanner.nextInt();
int doubleValue = scanner.nextInt();
String line = scanner.nextLine();

A)After the last statement is executed, line contains characters '7', '8', '9', '\n'.
B)After the last statement is executed, intValue is 34.
C)After the last statement is executed, line contains characters '7', '8', '9'.
D)The program has a runtime error because 34.3 is not an integer.

54)Given the following program:

public class Test {
public static void main(String[ ] args) {
for (int i = 0; i < args.length; i++) {
System.out.print(args[i] + " ");
}
}
}

What is the output, if you run the program using
java Test 1 2 3
A)3 B) 1 2 C) 1 D) 1 2 3

该程序有三个参数!
55)What is the output of the following code?

public class Test {
public static void main(String[ ] args) {
String s1 = "Welcome to Java!";
String s2 = s1;
if (s1 == s2)
System.out.println("s1 and s2 reference to the same String object");
else
System.out.println("s1 and s2 reference to different String objects");
}
}

A)s1 and s2 reference to the same String object B)s1 and s2 reference to different String objects

56)Which of the following is the correct statement to return JAVA? 56) ______
A)"Java".toUpperCase() B) "Java".toUpperCase("Java")
C)toUpperCase("Java") D) String.toUpperCase("Java")

57)What is the output of the following code?
String s = "University";
s.replace("i", "ABC");
System.out.println(s);
A)University B) UnABCversABCty C)UnABCversity D) UniversABCty

s.replace("i", "ABC");没有返回对象!

58)Assume s is "ABCABC", the method ________ returns a new string "aBCaBC". 58) ______
A)s.replace("ABCABC", "aBCaBC")
B)s.toLowerCase()
C)s.replace('A', 'a')
D)s.toLowerCase(s)
E)s.replace('a', 'A')

59)What is the output of the following code?

public class Test {
public static void main(String[ ] args) {
String s1 = new String("Welcome to Java!");
String s2 = s1.toUpperCase();
if (s1 == s2)
System.out.println("s1 and s2 reference to the same String object");
else if (s1.equals(s2))
System.out.println("s1 and s2 have the same contents");
else
System.out.println("s1 and s2 have different contents");
}
}

A)s1 and s2 have the same contents
B)s1 and s2 reference to the same String object
C)s1 and s2 have different contents

Java题库——Chapter9 String的用法的更多相关文章

  1. Java题库——Chapter11 继承和多态

    1)Analyze the following code: public class Test { public static void main(String[ ] args) { B b = ne ...

  2. Java题库——Chapter13抽象类和接口

    )What is the output of running class Test? public class Test { public static void main(String[ ] arg ...

  3. Java题库——Chapter8 对象和类

    1)________ represents an entity(实体) in the real world that can be distinctly identified. 1) _______ ...

  4. JAVA题库01

    说出一些常用的类,包,接口,请各举5个 常用的类:BufferedReader  BufferedWriter  FileReader  FileWirter  String Integer java ...

  5. Java题库——Chapter17 二进制I/0

    Introduction to Java Programming 的最后一章,完结撒花!Chapter 17 Binary I/O Section 17.2 How is I/O Handled in ...

  6. Java题库——Chapter16 JavaFX UI组件和多媒体

    Chapter 16 JavaFX UI Controls and Multimedia Section 16.2 Labeled and Label1. To create a label with ...

  7. Java题库——Chapter14 JavaFX基础

    Chapter 14 JavaFX Basics Section 14.2 JavaFX vs Swing and AWT1. Why is JavaFX preferred?a. JavaFX is ...

  8. Java题库——Chapter12 异常处理和文本IO

    异常处理 1)What is displayed on the console when running the following program? class Test { public stat ...

  9. Java题库——Chapter10 面向对象思考

    1)You can declare two variables with the same name in ________. 1) _______ A)a method one as a forma ...

随机推荐

  1. java8-从Lamda到方法引用和构造引用

    一方法引用概述 经过前面2章Lamda原理引入和Lamda解析,基本就会熟练使用Lamda表达式,这次我们更深入点.来了解一下方法引用. 方法引用是特定Lamda表达式的一种简写,其思路就是能替换La ...

  2. 《Java基础知识》流程控制

    流程控制分类: 一.顺序结构如果没有流程控制(即没有分支结构和循环结构),Java方法里面的语句是一个顺序执行流,从上到下依次执行每条语句. 二.分支结构1.if语句if语句使用布尔表达式或者布尔值作 ...

  3. 《Java基础知识》Java super关键字

    super可以理解为是指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类. super也有三种用法: 1.普通的直接引用 与this类似,super相当于是指向当前对象的父类,这样 ...

  4. 图文结合深入理解JS中的this值

    文章目录 Js 中奇妙的this值 1. 初探this 2. this指向总结 2.1 普通函数调用 2.2 对象的方法调用 2.3 构造函数调用 2.4 利用call,apply,bind方法调用函 ...

  5. 函数知识总结(js)

    c语言中函数的形参必须定义类型,而且形参的个数和实参的个数必须相等.但是在js中形参不需要定义,在函数定义的小括号中只需要写形参名就可以了不用写var关键字,而且在函数调用时传入的实参可以和形参的个数 ...

  6. CouchDB学习-维护

    官方文档 1 压缩 压缩操作是通过从数据库或者视图索引文件中移除无用的和老的数据减少硬盘使用空间.操作非常简单类似于其他数据库(SQLite等)管理系统. 在压缩目标期间,CouchDB将创建扩展名为 ...

  7. OpenWrite一款博客可一文多发的实用工具

    前言 许多网友想看一文多发的OpenWrite,今天,它来了!别问落地价,因为内测无价! 这款实用工具,可支持十大博客平台一键发布,是博主们的发文神器 你看它多种平台.一键管理.后台界面优雅.还有签到 ...

  8. 数据库学习笔记day03

    创建两个表,一个名为emp,一个名为dept,并且插入数据 create table emp(empno number(4,0),ename varchar2(10),job varchar2(9), ...

  9. Mongdb可视化工具Studio 3T的使用

    一.官网地址 https://studio3t.com/ 二.下载和安装 点击DOWNLOAD即可下载 按照自己电脑系统进行选择,然后填写邮箱和选择行业,第一次登录如果不提交不会下载,下载完成是一个z ...

  10. 微信小程序之启动页的重要性

    启动页在APP中是个很常见的需求,为什么对于小程序来说也非常重要呢?首先我描述一下我在开发过程中遇到的一些问题以及解决的步骤,到最后为什么要加启动页,看完你就明白了. 小程序的首页需要展示用户关注的小 ...