Back to Predict the Output
Q1Question 1 of 10

Predict the output: concat without reassignment

Key Concepts
  • String is immutable – concat() returns a NEW String, does not modify the original
  • If you do not assign the result, the original is unchanged
  • s.concat("World") without s = s.concat("World") has no visible effect
  • This is one of the most common interview trick questions
  • Understanding this concept is essential for Java interviews at senior level

Definition

concat() returns a new String. Without reassignment, the original is unchanged. Output: "Hello" (not "HelloWorld").

The trick:

  • s.concat("World") creates a new String "HelloWorld" but does NOT assign it to s.
  • s still points to the original "Hello".
  • The new String is garbage-collected immediately.

Asking the printer to create a new document (concat) but never picking it up (no assignment). The original document on your desk is unchanged.

Understanding the Concepts

The code

JAVA
String s = "Hello";
s.concat(" World");
System.out.println(s);

Output

TEXT
Hello

Why?

String is immutable. concat() returns a new String. It does NOT modify s. Without reassignment (s = s.concat(" World")), the result is lost.

The correct version

JAVA
String s = "Hello";
s = s.concat(" World");  // reassign!
System.out.println(s);    // "Hello World"

This applies to ALL String methods: toUpperCase(), trim(), replace(), substring() – they all return new Strings.

What are common mistakes?

A common mistake is assuming the output without tracing through the code step by step.

Wrong approach:

JAVA
package com.bank.quiz;

public class OutputQuiz {
    public static void main(String[] args) {
        // Developers often guess the wrong output
        // because they skip the step-by-step evaluation
        String result = "Balance: " + 100 + 50;
        System.out.println(result);
        // Many guess: "Balance: 150"
        // Actual: "Balance: 10050" (left-to-right string concat)
    }
}

Correct reasoning:

JAVA
package com.bank.quiz;

public class OutputQuiz {
    public static void main(String[] args) {
        // Step 1: "Balance: " + 100 = "Balance: 100" (String + int = String)
        // Step 2: "Balance: 100" + 50 = "Balance: 10050" (String + int = String)
        String result = "Balance: " + 100 + 50;
        System.out.println(result);  // Balance: 10050

        // To get arithmetic: use parentheses
        String correct = "Balance: " + (100 + 50);
        System.out.println(correct);  // Balance: 150
    }
}

Always trace left to right and remember that once a String is involved in concatenation, everything after it becomes string concatenation too.

What should a senior engineer know?

A senior engineer working on banking systems should understand the production implications of this concept.

  • Performance impact. Measure with JMH before making assumptions about which approach is faster.
  • Thread safety. In a multi-threaded banking service processing concurrent transactions, always consider whether the approach is thread-safe.
  • Memory footprint. Use JFR and VisualVM to measure actual memory usage under realistic banking workloads.
  • Monitoring. Set up JMX metrics and alerts for production banking applications.
JAVA
package com.bank.monitoring;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;

public class MemoryMonitor {
    public static void printMemoryUsage() {
        MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
        System.out.println("Heap: " + mem.getHeapMemoryUsage());
        System.out.println("Non-Heap: " + mem.getNonHeapMemoryUsage());
    }
}

What are the edge cases?

Edge cases can cause unexpected behavior in banking applications. Here are some to watch for.

JAVA
package com.bank.edge;

public class EdgeCaseDemo {
    public static void main(String[] args) {
        // Edge case 1: null handling
        String accountId = null;
        try {
            System.out.println(accountId.length());
        } catch (NullPointerException e) {
            System.out.println("Null account ID");
        }

        // Edge case 2: empty vs null
        String emptyId = "";
        System.out.println(emptyId.isEmpty());  // true
        System.out.println(emptyId == null);     // false

        // Edge case 3: boundary values
        java.math.BigDecimal zero = java.math.BigDecimal.ZERO;
        java.math.BigDecimal negativeBalance = new java.math.BigDecimal("-0.01");
        System.out.println(zero.compareTo(negativeBalance) > 0);  // true
    }
}
TEXT
Null account ID
true
false
true

Always test with null values, empty strings, boundary values, and negative amounts in banking applications.

What are common interview follow-up questions?

Interviewers often follow up with these related questions:

  • How does this concept apply to a high-throughput banking transaction system?
  • What are the performance implications in a production environment?
  • How would you debug issues related to this concept?
  • What tools would you use to monitor this in production?
  • How does this behave differently in Java 8 vs Java 17+?

When answering follow-up questions, always relate your answer back to practical experience. For example, at National Bank of India, Rajesh debugged a production issue related to this concept by using JFR recordings and heap dumps. Providing concrete examples from banking systems shows the interviewer that you understand both the theory and the practice.

Visual Overview

DIAGRAM
  s = "Hello"
  s.concat(" World") → creates new String "Hello World" → NOT assigned → garbage
  s is still "Hello"
  Output: Hello

Key Takeaway

String.concat() returns a new String without modifying the original. Without reassignment, the original is unchanged. This is because String is immutable.

One sentence to remember: String methods return new Strings – without reassignment, the original never changes.

---

Share
Use ← → arrow keys to navigate