Java String Quiz - String Tricky Coding Questions

Welcome to our Java String Coding Quiz! Strings are an essential part of Java programming, and understanding how to manipulate and process strings is crucial for developing robust applications. In this quiz, we will test your knowledge of various string operations and concepts. Let's get started and see how well you fare in these multiple-choice questions!

Learn and Master Java Programming: Learn Java Programming with Examples

Learn everything about Java 8 features: Java 8 Tutorial and Examples

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills

I suggest you, try these code snippets in Eclipse IDE and understand how the program works (However, the answer with the explanation given at the end of this post). These questions may ask in interviews or similar questions may appear in interviews so prepare yourself.

The answer and explanation of each question have given at the end of this post.

Q1 - Consider the following program:

public class StrEqual {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = new String("hello");
        String s3 = "hello";
        if (s1 == s2) {
            System.out.println("s1 and s2 equal");
        } else {
            System.out.println("s1 and s2 not equal");
        }
        if (s1 == s3) {
            System.out.println("s1 and s3 equal");
        } else {
            System.out.println("s1 and s3 not equal");
        }
    }
}
Which one of the following options provides the output of this program when executed? 
a)
s1 and s2 equal
s1 and s3 equal
b)
s1 and s2 equal
s1 and s3 not equal
c)
s1 and s2 not equal
s1 and s3 equal
d)
s1 and s2 not equal
s1 and s3 not equal

Q2 - Consider the following program:

public class Test {
    public static void main(String[] args) {
        String str = null;
        System.out.println(str.valueOf(10));
    }
}
Which of the following statements correctly describes the behavior of this program?
a) This program will result in a compiler error.
b) This program will throw a NullPointerException.
c) This program will print 10 in the console.
d) This program will print null in the console.

Q3 - Consider the following program and predict the output:

public class Test {
    public static void main(String[] args) {
        String s = new String("5");
        System.out.println(1 + 10 + s + 1 + 10);
    }
}
a) 11511
b) 1105110
c) 115110
d) 27

Q4 - Consider the following program and predict its output:

public class Test {
    public static void main(String[] args) {
        String str = null;
        switch (str) { // #1
            case "null":
                System.out.println("null string"); // #2
                break;
        }
    }
}
a) This program results in a compiler error in statement #1.
b) This program results in a compiler error in statement #2.
c) This program results in throwing a NullPointerException.
d) This program prints the following: null string.

Q5 - What will be the output of the below statements?

public class Test {

    public static void main(String[] args) {
        String s1 = null;
        System.out.println(s1); //line 2
        System.out.println(s1.toString()); //line 3
    }
}
a) null null
b) null NullPointerException
c) NullPointerException NullPointerException
d) None

Q6 - What is the output of the following program?

public class Test {

    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = new String("hello");

        s2 = s2.intern();
        System.out.println(s1 == s2);
    }
}
a) false
b) true
c) None

Q7 - What will be the output of the below statements?

public class Test {

    public static void main(String[] args) {
        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

Q8 - What is the output of the following code snippet?

public class Main {
    public static void main(String[] args) {
        String str = "Hello";
        str += " World!";
        System.out.println(str.length());
    }
}
a) 5
b) 11
c) 12
d) Compile-time error

Q9 - What is the output of the following code snippet?

public class Main {
    public static void main(String[] args) {
        String str = "Java";
        str.concat(" Programming");
        System.out.println(str);
    }
}
a) Java
b) Java Programming
c) Programming
d) Compile-time error

Q10 - What is the output of the following code snippet?

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        System.out.println(str.indexOf("o"));
    }
}
a) 4
b) 5
c) 7
d) -1

Answers

Q1

Answer: 
c)
s1 and s2 not equal
s1 and s3 equal
Explanation: JVM sets a constant pool in which it stores all the string constants used in the type. If two references are declared with a constant, then both refer to the same constant object. The == operator checks the similarity of objects itself (and not the values in it). Here, the first comparison is between two distinct objects, so we get s1 and s2 not equal. On the other hand, since references to s1 and s3 refer to the same object, we get s1 and s3 equal.

Q2

Answer:
c) This program will print 10 in the console.
Explanation: The valueOf(int) method is a static method in String that returns the String representation of the integer value that is passed as its argument. Since calling a static method does not require dereferencing the reference variable on which it is called, this program does not throw a NullPointerException.

Q3

Answer:
c) 115110
Explanation: The string concatenation operator works as follows: if both the operands are numbers, it performs the addition; otherwise it concats the arguments by calling the toString() method if needed. It evaluates from left to right. Hence, the expression in the program results in the string 115110.

Q4

Answer:
c) This program results in throwing a NullPointerException.
If a null value is passed to a switch statement, it results in a NullPointerException.

Q5

Answer:
b) null NullPointerException
Explanation: Line 2 will print null because println() method has a null check like below.
if (s == null) {
    s = "null";
}

Q6

Answer:
b) true
Explanation: We know that the intern() method will return the String object reference from the string pool since we assign it back to s2 and now both s1 and s2 are having the same reference. It means that s1 and s2 references point to the same object.

Q7

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

Q8

Answer:
c) 12

Explanation: 
When "Hello" is concatenated with " World!", it forms a new string "Hello World!". The length of this string is 12 characters, so the output is 12.

Q9

Answer:
a) Java 

Explanation:
The concat() method returns a new string resulting from concatenating the specified string to the original string. However, in this code, the result of concat() is not assigned back to the str variable. Therefore, the original string "Java" remains unchanged, and the output is "Java".

Q10

Answer:
a) 4
Explanation: The indexOf() method returns the index of the first occurrence of a specified substring within a string.

Conclusion

Congratulations on completing our Java String Coding Quiz! These questions tested your understanding of string manipulation operations, such as concatenation, length determination, character retrieval, comparison, substring extraction, and case conversion. By mastering these concepts, you'll be better equipped to work with strings effectively in Java programming. 

Learn and Master Java Programming: Learn Java Programming with Examples

Learn everything about Java 8 features: Java 8 Tutorial and Examples

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills

Keep exploring and practicing to enhance your skills in working with Java strings!

Related Posts

  1. Java String Quiz
  2. Java Arrays Quiz
  3. Java Loops Quiz
  4. Java OOPS Quiz
  5. Java OOPS Quiz - Part 1
  6. Java OOPS Quiz - Part 2
  7. Java Exception Handling Quiz
  8. Java Collections Quiz
  9. Java Generics Quiz
  10. JDBC Quiz
  11. Java Lambda Expressions Quiz
  12. Java Functional Interfaces Quiz
  13. Java Streams API Quiz
  14. Java Date Time Quiz
  15. Java 8 Quiz

Comments