The code snippet

if( "Welcome".trim() == "Welcome".trim() )
System.out.println("Equal");
else
System.out.println("Not Equal");

will

Options
- compile and display “Equal”
- compile and display “Not Equal”
- cause a compiler error
- compile and display NULL


CORRECT ANSWER : compile and display “Equal”

Discussion Board
==

Is anyone else bothered by the use of == instead of equals(). If you change the code to include whitespace to be trimmed from one or both of the strings the answer is Not Equal. Duh! Because of course you don''t use == with Strings. Sure, I like curly braces as much as the next guy (or gal), but that''s a style issue.

Bob 07-11-2017 08:54 AM

It will print equal

trim() method is used to remove the leading spaces from a string.both strings are same so,they are pointing to same memory location.so,if condition returns true..

Harika 05-21-2017 05:20 AM

It will compile and print equal

Its not about the braces. Its about constant string "Welcome" and its reference. trim() method return the reference of string with leading and trailing spaces removed. Now both are pointing to same constant String "Welcome" and the memory location is same. that why if condition is true and it prints equal.

ankit 03-20-2015 03:54 AM

Conditional Statement

if-else statements are used when there needs to select one option out of many present. It is a conditional operator and there are conditions that allow the program to choose one state. There are certain rules that needs to follow:

1. An if can have zero or one else's and it must come after any else if's.
2. An if can have zero to many else if's and they must come before the else.
3. Once an else if succeeds, none of the remaining else if's or else's will be tested.

The above statement can be written as:
if ("Welcome".trim()=="Welcome".trim()
{
System.out.println("Equal");
}
else
{
System.out.println("Not Equal");
}


Rohit Sharma 07-28-2014 03:57 AM

shashank goyal is wrong about it being wrong.

This is bad code according to convention but single line branching is allowed in Java without the user of curly braces.

J Lowe 04-18-2014 08:35 PM

Correct answer is Wrong one

There is no curly opening and closing bracekets, so the answer will be compilation error...

shashank goyal 04-10-2014 05:00 AM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement