Java String Quiz - String Tricky Coding Questions

In this post, I have included a few useful Java String programming/coding questions and answers (code snippets with output). I suggest you, try these code snippets in eclipse IDE and understand how the program works (However, the answer with the explanation given at end of this post). These questions may ask in interviews or similar questions may appear in interviews so prepare yourself.

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 - select all the classes that extend the String class.

a) StringBuffer
b) StringBuilder
c) StringWriter
d) None

Q7 - 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

Q8 - 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

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

d) None
Explanation: The String is a final class, so you can't extend it.

Q7

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 pointing to the same object.

Q8

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.

Let me know if this post is helpful so that I will prepare such articles/posts for you.

Comments