What will be the output of the code given below?


public class Tester {	
    public static void main(String args[]) {	
        String input1 = "warner";		
        String input2 = new String("WARNER");		
        input2.toLowerCase();		
		
        if (input1 == input2) {			
            System.out.println(" Welcome " + input1);		
        } else if (input1.equals(input2)) {			
            System.out.println(" Welcome " + input2);		
        } else {			
            System.out.println("Welcome");		
        }	
    }
}

Welcome

Welcome warner

Welcome WARNER

Compilation error - datatype mismatch, cannot compare String literal with String object

Verified Answer
Correct Option - a
Anush said

The output of the code will be Welcome. Here's why:
1. The method input2.toLowerCase() is called, but it doesn't change the original input2 string since strings are immutable in Java. input2 remains "WARNER".
2. The comparison input1 == input2 checks if both variables reference the same object. Since input1 is a string literal and input2 is created using new, they are different objects, so this condition is false.
3. The input1.equals(input2) method compares the content of both strings. input1 is "warner" and input2 is still "WARNER", so they are not equal in a case-sensitive comparison. This condition is also false.
4. Since neither condition is true, the code falls to the else block and prints "Welcome".

Let’s break the code down step by step to understand its output:

Key Points in the Code:

  • Variable Declarations:
    - input1 is a string literal: "warner".
    - input2 is created using new String("WARNER"). This creates a new String object in memory that is not the same as input1 (even if their values match).
  • input2.toLowerCase() Method Call:
    - The toLowerCase() method converts a string to lowercase. However, strings in Java are immutable, meaning the original input2 is not changed.
    - The lowercase version of input2 is created but not assigned back to input2. So, input2 still holds "WARNER".
  • Equality Checks:
    - input1 == input2: This compares the memory locations of the two string objects, not their values. Since input1 and input2 are stored in different locations, this condition evaluates to false.
    - input1.equals(input2): This checks the values of the strings. Since input1 is "warner" and input2 is "WARNER", this condition is also false because the comparison is case-sensitive.
  • The else Block:
    - Since both if and else if conditions are false, the program enters the else block.

Final Output:
The output of the code will be:

Welcome

Why?
Because:

  • input1 == input2 is false (different memory locations).
  • input1.equals(input2) is false (case-sensitive mismatch).
  • The program defaults to the else block.

To get all Self Assessment - Programming using Java Exam questions Join Telegram Group https://rebrand.ly/lex-telegram-236dee

Telegram

We're passionate about offering best placement materials and courses!! A one stop place for Placement Materials. We daily post Offcampus updates and Placement Materials.

Qtr No. 213, New Town Yehlanka Indore 454775

admin@prepflix.in

Updated on Mon, 8 Dec 2025