java测试字符串的编码

Welcome to Java String Quiz. String is one of the most important classes in Java. If you have done any programming in java, you must have used it.

The string is very popular when it comes to java interview questions or quiz. So I have gathered some great and tricky java string quiz questions that you should try.

欢迎使用Java字符串测验。 字符串是Java中最重要的类之一。 如果您已经用Java完成了任何编程,则必须使用它。

在涉及 Java面试问题或测验 时,该字符串非常受欢迎 。 因此,我收集了一些您应该尝试的出色且棘手的Java字符串测验问题。

Java字符串测验 (Java String Quiz)

There are 21 questions in this quiz. If you can correctly answer 15 or more, then consider yourself really good in String concepts. You can check the answer and detailed explanation by clicking on the “Reveal Answer” button after each question.

Let’s start the String Quiz and best of luck.

此测验中有21个问题。 如果您能正确回答15个或更多,则认为您在String概念方面真的很不错。 您可以通过在每个问题后单击“ 显示答案 ”按钮来查看答案和详细说明。

让我们开始String Quiz,祝您好运。

1.以下语句的输出是什么? ( 1. What will be the output of below statements?)

String s = "Java String Quiz";
System.out.println(s.charAt(s.toUpperCase().length()));

A. Convert “Z” to int 90 and prints “90”
B. Runtime Exception
C. Prints “z”
D. Prints “Z”

A.将“ Z”转换为int 90并打印“ 90”
B.运行时异常
C.打印“ z”
D.打印“ Z”

点击以显示答案 (Click to Reveal Answer)

Correct Answer: B

It will throw the runtime exception.Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 16 It’s because the index value starts from 0.

正确答案:B

它将抛出运行时异常。 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 16这是因为索引值从0开始。

2.以下语句的输出是什么? (2. What will be output of below statements?)

String s = "Java String Quiz";
System.out.println(s.substring(5,3));

A. Prints “Str”
B. Runtime Exception
C. IndexOutOfBoundsException Runtime Exception
D. StringIndexOutOfBoundsException Compile-time error

A.打印“ Str”
B.运行时异常
C. IndexOutOfBoundsException运行时异常
D. StringIndexOutOfBoundsException编译时错误

点击以显示答案 (Click to Reveal Answer)

Correct Answer: B

It will throw the runtime exception with the error message as Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 5, end 3, length 16. It’s because the end index is smaller than the start index.

正确答案:B

它将Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 5, end 3, length 16引发运行时异常,并以错误消息作为Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 5, end 3, length 16 。 这是因为结束索引小于开始索引。

3.选择String类的所有有效方法。 (3. Select all valid methods of String class.)

A. trim()
B. intern()
C. toLower()
D. split()

修剪()
B.实习生
C.toLower()
D. split()

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A, B

Valid methods of String class are – trim(), intern(), toLowerCase(), and split(String regex).

正确答案:A,B

String类的有效方法是– trim(),intern(),toLowerCase()和split(String regex)。

4.以下语句的输出是什么? (4. What will be the output of below statements?)

String s1 = "Cat";
String s2 = "Cat";
String s3 = new String("Cat");
System.out.print(s1 == s2);
System.out.print(s1 == s3);

A. truefalse
B. truetrue
C. falsefalse
D. falsetrue

A.真假


D.假真

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

When we use double quotes to create a String, it first looks for String with the same value in the String pool. If found, then it returns the reference else it creates a new String in the pool and then returns the reference.

However using new operator, we force String class to create a new String object in heap space. So s1 and s2 will have reference to the same String in the pool whereas s3 will be a different object outside the pool, hence the output.

正确答案:A

当我们使用双引号创建字符串时,它首先在字符串池中查找具有相同值的字符串。 如果找到,它将返回引用,否则它将在池中创建一个新的String,然后返回该引用。

但是,使用new运算符,我们强制String类在堆空间中创建一个新的String对象。 因此s1和s2将在池中引用相同的String,而s3将是池外的不同对象,因此是输出。

5.在切换情况下,以下哪个语句适用于字符串? (5. Which of the following statements are true for string in switch case?)

A. String is allowed in switch case for Java 1.5 or higher versions.
B. String is allowed in switch case for Java 1.7 or higher versions.
C. The equals() method is used by switch-case implementation, so add null check to avoid NullPointerException.

答:对于Java 1.5或更高版本,在切换情况下允许使用字符串。
B.对于Java 1.7或更高版本,在切换情况下允许使用字符串。
C.切换案例实现使用equals()方法,因此添加null检查以避免NullPointerException。

点击以显示答案 (Click to Reveal Answer)

Correct Answer: B, C

Read more at java switch case string

正确答案:B,C

在Java开关案例字符串中了解更多信息

6.对于StringBuffer和StringBuilder,以下哪个语句为True? (6. Which of the following statements are True for StringBuffer and StringBuilder?)

A. StringBuilder is not thread-safe.
B. StringBuffer is thread safe because its methods are synchronized.
C. StringBuilder was introduced in Java 1.4
D. StringBuffer and StringBuilder are immutable.

答:StringBuilder不是线程安全的。
B. StringBuffer是线程安全的,因为它的方法是同步的。
C. StringBuilder是Java 1.4中引入的
D. StringBuffer和StringBuilder是不可变的。

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A, B

StringBuffer object is thread-safe because its methods are synchronized. But that’s an overhead in most of the cases, hence StringBuilder was introduced in Java 1.5. StringBuilder is not thread-safe. StringBuffer and StringBuilder are mutable classes. Read more at String vs StringBuffer vs StringBuilder.

正确答案:A,B

StringBuffer对象是线程安全的,因为其方法是同步的。 但这在大多数情况下都是开销,因此Java 1.5中引入了StringBuilder。 StringBuilder不是线程安全的。 StringBuffer和StringBuilder是可变的类。 在String vs StringBuffer vs StringBuilder中了解更多信息。

7.字符串实现遵循以下哪种设计模式? (7. String implementation follows which of the below design pattern?)

A. Flyweight Design Pattern
B. Factory Pattern
C. Singleton Pattern
D. None of the above

A.举重设计模式
B.工厂模式
C.单例模式
D.以上都不是

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

String pool implementation follows the flyweight design pattern.

正确答案:A

字符串池的实现遵循flyweight设计模式 。

8.以下语句的输出是什么? (8. What will be the output of below statements?)

String s1 = "abc";
String s2 = "def";
System.out.println(s1.compareTo(s2));

A. 0
B. true
C. -3
D. false

A.0
B.真实
C.-3
D.假

点击以显示答案 (Click to Reveal Answer)

Correct Answer: C

From String compareTo() method documentation:

compareTo method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string — that is, the value:

this.charAt(k)-anotherString.charAt(k)

In our example the “abc” precedes “def”, hence negative integer is returned. Then the lowest index with different char is 0 and a-d equals to -3.

正确答案:C

从String compareTo()方法文档中:

compareTo方法按字典顺序比较两个字符串。 比较是基于字符串中每个字符的Unicode值。 在字典上比较此String对象表示的字符序列与自变量字符串表示的字符序列。 如果此String对象在字典上在参数字符串之前,则结果为负整数。 如果此String对象在字典上跟随自变量字符串,则结果为正整数。 如果字符串相等,则结果为零;否则,结果为零。 当equals(Object)方法返回true时,compareTo确切返回0。

这是字典顺序的定义。 如果两个字符串不同,那么它们要么在某个索引处具有不同的字符(这是两个字符串的有效索引),要么它们的长度不同或两者都相同。 如果它们在一个或多个索引位置具有不同的字符,则令k为最小索引; 那么在位置k处的字符具有较小值的字符串(通过使用<运算符在字典上确定)在另一个字符串之前。 在这种情况下,compareTo返回两个字符串中位置k处两个字符值的差,即值:

this.charAt(k)-anotherString.charAt(k)

在我们的示例中,“ abc”在“ def”之前,因此返回负整数。 然后,具有不同字符的最低索引为0,而ad等于-3。

9.以下程序的输出是什么? (9. What will be the output of below program?)

public class Test {public static void main(String[] args) {String x = "abc";String y = "abc";x.concat(y);System.out.print(x);}
}

A. abc
B. abcabc
C. null

A.abc
B.abcabc
C.空

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

The x.concat(y) will create a new string but it’s not assigned to x, so the value of x is not changed.

正确答案:A

x.concat(y)将创建一个新字符串,但未将其分配给x,因此x的值不会更改。

10.以下程序的输出是什么? (10. What will be the output of below program?)

public class Test {public static void main(String[] args) {String s1 = "abc";String s2 = "abc";System.out.println("s1 == s2 is:" + s1 == s2);}
}

A. false
B. s1 == s2 is:true
C. s1 == s2 is:false
D. true

A.假
B. s1 == s2是:true
C. s1 == s2是:false
D.真实

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

The given statements output will be “false” because in java + operator precedence is more than == operator. So the given expression will be evaluated to “s1 == s2 is:abc” == “abc” i.e false.

正确答案:A

给定的语句输出将为“ false”,因为在Java +运算符中,优先级大于==运算符。 因此,给定的表达式将被评估为“ s1 == s2 is:abc” ==“ abc”,即false。

11.以下语句的输出是什么? (11. What will be the output of below statements?)

String s = "Java"+1+2+"Quiz"+""+(3+4);
System.out.println(s);

A. Java3Quiz7
B. Java12Quiz7
C. Java12Quiz34
D. Java3Quiz34

A.Java3Quiz7
B.Java12Quiz7
C.Java12Quiz34
D.Java3Quiz34

点击以显示答案 (Click to Reveal Answer)

Correct Answer: B

First of all, the expression in the bracket is executed. Then it’s all + operators, so they get executed from left to right.

We get String with each concatenation, hence the output gets produced as shown below.

“Java”+1+2+”Quiz”+””+(3+4)
= “Java”+1+2+”Quiz”+””+7
= “Java1″+2+”Quiz”+””+7
= “Java12″+”Quiz”+””+7
= “Java12Quiz”+””+7
= “Java12Quiz”+7
= “Java12Quiz7”

正确答案:B

首先,执行括号中的表达式。 然后是所有+运算符,因此它们从左到右执行。

每次串联我们都得到String,因此产生的输出如下所示。

“ Java” + 1 + 2 +“测验” +”” +(3 + 4)
=“ Java” + 1 + 2 +“测验” +“” + 7
=“ Java1” +2+“测验” +“” + 7
=“ Java12” +“测验” +“” + 7
=“ Java12Quiz” +“” + 7
=“ Java12Quiz” +7
=“ Java12Quiz7”

12.在以下语句中创建了多少个String对象? (12. How many String objects created in below statements?)

String s = "abc"; // statement 1
String s1 = new String("abcd"); // statement 2

A. 1
B. 2
C. 3
D. 4

A.1
B.2
C.3
D.4

点击以显示答案 (Click to Reveal Answer)

Correct Answer: C

In statement 1, “abc” is created in the String pool.

In statement 2, first of all “abcd” is created in the string pool. Then it’s passed as an argument to the String new operator and another string gets created in the heap memory.

So a total of 3 string objects gets created.

正确答案:C

在语句1中,在字符串池中创建了“ abc”。

在语句2中,首先在字符串池中创建“ abcd”。 然后将其作为参数传递给String new运算符,并在堆内存中创建另一个字符串。

因此,总共创建了3个字符串对象。

13.以下语句的输出是什么? (13. What will be the output of below statements?)

String s1 = "abc";
String s2 = new String("abc");
System.out.print(s1==s2);
System.out.println(s1==s2.intern());

A. falsetrue
B. falsefalse
C. truetrue
D. truefalse

A.假真

C. truetrue
D.真假

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

The s1 is in the string pool whereas s2 is created in heap memory.

Hence s1==s2 will return false.

When s2.intern() method is called, it checks if there is any string with value “abc” in the pool. So it returns the reference of s1. So both s1 and s2 are pointing to the same string instance now.

Hence s1==s2.intern() will return true.

正确答案:A

s1在字符串池中,而s2在堆内存中创建。

因此s1 == s2将返回false。

s2.intern()方法时,它将检查池中是否存在任何值为“ abc”的字符串。 因此,它返回s1的引用。 因此s1和s2现在都指向同一个字符串实例。

因此s1 == s2.intern()将返回true。

14.选择String类实现的所有接口。 (14. Select all the interfaces implemented by String class.)

A. Serializable
B. Comparable
C. Constable
D. Cloneable

A.可序列化
B.可比
C.稳定
D.可克隆

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A, B, C

String is serializable and comparable. The Constable is a new interface from the Java 12 release.

正确答案:A,B,C

字符串是可序列化的,并且具有可比性。 Constable是Java 12版本中的新接口。

15.选择使String成为Map密钥最佳候选者的所有原因? (15. Select all the reasons that make String perfect candidate for Map key?)

A. String is immutable
B. String is final
C. String properly implements hashCode() and equals() method
C. String hashcode is cached

A.字符串是不可变的
B.字符串是最终的
C.字符串正确实现hashCode()和equals()方法
C.字符串哈希码被缓存

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A, B, C

The proper implementation of hashCode() and equals() method is a must for a Map key. Since the string is final and immutable, there are no chances of corruption of the key data.

正确答案:A,B,C

对于Map键,必须正确实现hashCode()和equals()方法。 由于字符串是最终的且不可变,因此不会损坏键数据。

16.下面的代码片段的输出是什么? (16. What will be the output of below code snippet?)

String s1 = new String("java");
String s2 = new String("JAVA");
System.out.println(s1 = s2);

A. JAVA
B.java
C. true
D. false

A.JAVA
B.java
C.真实
D.假

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

It will print “JAVA” because the argument inside the println() method is an assignment. So it will be treated as System.out.println("JAVA").

正确答案:A

因为println()方法中的参数是一个赋值,所以它将打印“ JAVA”。 因此它将被视为System.out.println("JAVA")

17.以下语句的输出是什么? (17. What will be the output of below statements?)

String s1 = "abc";
StringBuffer s2 = new StringBuffer(s1);
System.out.println(s1.equals(s2));

A. false
B. true
C. ClassCastException at runtime
D. Compile-time error

A.假
B.真实
C.运行时ClassCastException
D.编译时错误

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

It will print false because s2 is not of type String. If you will look at the String equals() method implementation, you will find a check using instanceof operator to check if the type of passed object is String? If not, then return false.

正确答案:A

因为s2不是String类型,它将打印false。 如果您查看String equals()方法的实现,则会发现使用instanceof运算符进行检查以检查传递的对象的类型是否为String? 如果不是,则返回false。

18.下面的代码片段的输出是什么? (18. What will be the output of below code snippet?)

String s1 = "abc";
String s2 = new String("abc");
s2.intern();
System.out.println(s1 == s2);

A. false
B. true
C. null

A.假
B.真实
C.空

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

It’s a tricky question and output will be false. We know that intern() method will return the String object reference from the string pool, but since we didn’t assign it back to s2, there is no change in s2. Hence both s1 and s2 are having a different reference.

If we change the code in line 3 to s2 = s2.intern(); then the output will be true.

正确答案:A

这是一个棘手的问题,输出将是错误的。 我们知道intern()方法将从字符串池中返回String对象引用,但是由于我们没有将其分配回s2,因此s2中没有任何变化。 因此,s1和s2都有不同的参考。

如果我们将第3行中的代码更改为s2 = s2.intern(); 那么输出将为true。

19.选择所有扩展String类的类。 (19. Select all the classes that extend String class.)

A. StringBuffer
B. StringBuilder
C. StringWriter
D. None

A.StringBuffer
B. StringBuilder
C.StringWriter
D.无

点击以显示答案 (Click to Reveal Answer)

Correct Answer: D

It’s a tricky question. The String is a final class, so you can’t extend it.

正确答案:D

这是一个棘手的问题。 字符串是最后一个类,因此您不能扩展它。

20.关于Java中的String,以下哪些陈述是正确的? (20. Which of the following statements are true about String in java?)

A. We can extend String class like StringBuffer does it.
B. String class is defined in java.util package.
C. String is immutable in Java.
D. String is thread-safe in Java.
E. String is case sensitive in Java.

答:我们可以像StringBuffer一样扩展String类。
B.字符串类在java.util包中定义。
C.字符串在Java中是不可变的。
D.字符串在Java中是线程安全的。
E.字符串在Java中区分大小写。

点击以显示答案 (Click to Reveal Answer)

Correct Answer: C, D, E

We can’t extend String class because it’s final. StringBuffer doesn’t extend it. String class is defined in java.lang package.The string is immutable and hence thread-safe in java. String is case sensitive, so “abc” is not equal to “ABC”.

正确答案:C,D,E

我们不能扩展String类,因为它是最终的。 StringBuffer不会扩展它。 字符串类是在java.lang包中定义的。字符串是不可变的,因此在Java中是线程安全的。 字符串区分大小写,因此“ abc”不等于“ ABC”。

21.以下语句的输出是什么? (21. What will be the output of below statements?)

String s1 = null;
System.out.print(s1); // line 2
System.out.print(s1.toString()); // line 3

A. nullnull
B. null followed by NullPointerException
C. NullPointerException

A.nullnull
B. null后跟NullPointerException
C.NullPointerException

点击以显示答案 (Click to Reveal Answer)

Correct Answer: B

Line 2 will print null because print method has null check like this:

if (s == null) { s = "null";}

Line 3 will throw NullPointerException because we are trying to invoke toString() function on null.

正确答案:B

第2行将打印null因为print方法具有如下的null检查:

if (s == null) { s = "null";}

第3行将抛出NullPointerException,因为我们试图在null上调用toString()函数。

结论 (Conclusion)

I have tried to cover most of the important points about String in this Quiz. If you think some interesting concept has been missed, please let me know through comments. If you liked the Quiz, share it with others too.

我已尝试在本测验中涵盖有关String的大多数重要观点。 如果您错过了一些有趣的概念,请通过评论告知我。 如果您喜欢测验,也可以与他人分享。

翻译自: https://www.journaldev.com/15315/java-string-quiz

java测试字符串的编码

java测试字符串的编码_Java字符串测验相关推荐

  1. java字符串颠倒位置_java字符串第一个字符与最后一个字符颠倒位置测试

    最近做了一个需求,就是 通过OA 连接到我们系统,然后request里 带有base64加密后的字符串,并且第一个与最后一个位置颠倒了,所以需要先恢复正常的加密串,然后在解密. 所以首先工作是 先测试 ...

  2. java字符替换函数示例_Java字符串替换函数replace()用法解析

    Java字符串替换函数replace()用法解析 这篇文章主要介绍了Java字符串替换函数replace()用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的 ...

  3. java string 转码问题_Java字符串转码

    Java 正确的做字符串编码转换 字符串的内部表示? 字符串在java中统一用unicode表示( 即utf-16 LE) , 对于 String s = "你好哦!"; 如果源码 ...

  4. java按特殊标志截取_java 字符串分割处理split及特殊符号

    String类型的对象在 用split()方法进行字符分割的时候常常会遇到用特殊字符进行分割的情况,看JDK知道split()实际上是用的正则实现的分割,当遇到一些用特殊 字符作为分割标志的时候,不使 ...

  5. java字符串的用法_Java字符串的重要方法的使用实例

    String类将是您存储和处理语言的主要工具之一,本篇文章将带领大家对Java字符串的方法有个基础的认识,了解他的功能使用方法. 本文摘自千锋教育编著的<Java语言程序设计>,如需转载请 ...

  6. java字符串拆分 空字符_Java字符串拆分

    java字符串拆分 空字符 Java String split() method is used to split the string into a string array based on th ...

  7. java中的字符串常量池_java字符串常量池

    字符串常量池SCP jdk1.6是放在永久代(8中叫方法区或叫元空间)中; jdk1.7+中,字符串常量池放入了堆中,注意运行时常量依然存放在方法区,例如,Integer a = 40:Java在编译 ...

  8. java split 字符串作为分隔符_Java 字符串分隔 split

    Java中的我们可以利用 split 方法(Java.lang.string.split)把字符串按照指定的分割符进行分割,然后返回字符串数组,下面是string.split的用法实例及注意事项. s ...

  9. java字符串拆数组_java 字符串拆分字符串数组

    java 字符串拆分字符串数组 用split(); if(associatedCheckMoreOldStr.length()>0){ String[] tmp = associatedChec ...

最新文章

  1. 创建新的ros工作空间
  2. shopify api php 开发,Shopify插件开发基础篇-获取店铺信息
  3. 【深度探讨】阿里巴巴万级规模 K8s 集群全局高可用体系之美
  4. 雪花算法之唯一ID生成器理解
  5. 下列不属于html5语义元素,HTML5 新的语义元素
  6. 操作系统实战45讲01:运行HelloOS
  7. 不同版本的散点图矩阵
  8. Hibernate之一对多与多对一映射
  9. 微信机器人接口,微信ipad协议
  10. 计算机网络实验2-二层交换机的基本配置(eNSP)
  11. 从零实现一个3D建模软件
  12. 软件设计师----计算机网络
  13. Linux修改系统时间为东八区北京时间(上海时间)
  14. 推荐系统组队学习之协同过滤
  15. 胡适资助林语堂出国留学的故事
  16. 语音识别方案 - 联想语音团队多场景实时语音文字转换方案
  17. JavaWeb项目实践
  18. 小程序setData报错未定义
  19. android 进度条边框,android用户界面-组件Widget-进度条ProgressBar
  20. html中如果设置颜色为半透明状态,css如何利用transparent属性设置透明度?

热门文章

  1. PHP合并2个数字键数组的值
  2. web前端的十种jquery特效及源码下载
  3. WPF自定义窗体仿新毒霸关闭特效(只能在自定义窗体中正常使用)
  4. [转载] python divmod()函数
  5. [转载] Python Dictionary用法小结
  6. WEBBASE篇: 第六篇, CSS知识4
  7. nginx.conf添加lua.conf配置
  8. 83)PHP,配置文件功能
  9. JVM参数之-XX:+HeapDumpOnOutOfMemoryError(导出内存溢出的堆信息(hprof文件))
  10. 苹果被拒的血泪史。。。(update 2015.11)