Quiz your self: String manipulation and native and occasion variables

[ad_1]

Think about that your colleague from the IT division is engaged on a brand new chatbot software implementing—amongst some others—the Command design sample. The bot’s command objects are created as soon as upon startup and reused by way of the lifetime of the appliance. Beneath is the partial code of the appliance, particularly, the AboutCommand interface, which should return the textual content Copyright (c) 2023, http://mycompany.com.

Quiz Yourself, String Manipulation, Local Instance Variables, Oracle Java Tutorial and Materials, Java Career, Java Skills, Java Jobs, Java Certification, Java String, Oracle Java Tutorial and Materials

public interface Command {

  String execute(Context c);

}

public class AboutCommand implements Command {

  personal String url;

  public AboutCommand(String s) {

    url = s;

  }

  public String execute(Context c) {

    url = url.substring(0, url.lastIndexOf(“https://oraclejavacertified.blogspot.com/”)); // line 2

    url = url.substring(0, url.lastIndexOf(“https://oraclejavacertified.blogspot.com/”)); // line 3

    return “Copyright (c) 2023, ” + url; // line 4

  }

}

public class ChatBot {

  public static closing String HOME_PAGE = “http://mycompany.com/botapp/index.html”;

  personal closing Command about;

  ChatBot() {

    about = new AboutCommand(HOME_PAGE);

    … // create some extra instructions

  }

}

Which assertion is right? Select one.

A. The code is compilable and works as anticipated.

B. The code shouldn’t be compilable. To repair the code, the ultimate modifier on the variable HOME_PAGE have to be eliminated.

C. The code is inaccurate. To repair it, modify traces 2 and three like this:

url.substring(0, url.lastIndexOf(“https://oraclejavacertified.blogspot.com/”)); // line 2

url.substring(0, url.lastIndexOf(“https://oraclejavacertified.blogspot.com/”)); // line 3

D. The code is inaccurate. To repair it, modify traces 2 and three like this:

var url = this.url.substring(0, this.url.lastIndexOf(“https://oraclejavacertified.blogspot.com/”)); // line 2

url = this.url.substring(0, this.url.lastIndexOf(“https://oraclejavacertified.blogspot.com/”)); // line 3

E. The code is inaccurate. To repair it, modify traces 2 and three like this:

var url = this.url.substring(0, this.url.lastIndexOf(“https://oraclejavacertified.blogspot.com/”)); // line 2

url = url.substring(0, url.lastIndexOf(“https://oraclejavacertified.blogspot.com/”)); // line 3

Reply. This query investigates points of string manipulation, native and occasion variables, and logical pondering.

To start with, choice A is inaccurate. Whereas the primary name of the execute() technique will return the required textual content, the strategy has unintended effects. Particularly, it modifies the occasion variable. Due to this fact, a second name will return solely Copyright (c) 2023, http:. A 3rd name will throw java.lang.StringIndexOutOfBoundsException.

To right this, you could possibly both recalculate the message every time with out altering the worth of the sector named url, or you could possibly calculate the message as soon as and retailer the outcome for future use. The latter could be extra environment friendly. Let’s examine the opposite choices to see if any obtain this outcome.

Choice B is inaccurate and completely irrelevant to the issue you may have. At no level does any of the code proven within the query try to reassign the HOME_PAGE fixed, so the ultimate modifier can’t be the reason for any issues.

Choice C can also be incorrect. It avoids reassigning the url discipline, which looks as if a step ahead. Nevertheless, strings are immutable, so line 4 will return the preliminary unchanged worth. Line 2 will create a brand new string containing the textual content http://mycompany.com/botapp, however that string is straight away deserted as a result of the reference to it (which is carried because the return worth of the lastIndexOf technique) shouldn’t be saved anyplace. Line 3 merely repeats the method, and the brand new string is deserted once more. Due to this fact, the outcome will probably be Copyright (c) 2023, http://mycompany.com/botapp/index.html.

Quiz Yourself, String Manipulation, Local Instance Variables, Oracle Java Tutorial and Materials, Java Career, Java Skills, Java Jobs, Java Certification, Java String, Oracle Java Tutorial and Materials

As well as, choice D is inaccurate. It introduces a brand new native variable, String url. Line 2 creates a brand new string (as in choice C) by eradicating the /index.html half from the unique textual content. This new string is saved within the native variable referred to as url. Nevertheless, line 3 merely repeats the habits of line 2, as a result of it reads its beginning worth from the sector referred to as url, not the worth created by line 2. Consequently, this model produces the message Copyright (c) 2023, http://mycompany.com/botapp.

Choice E is right. Strains 2 and three each retailer the results of their operations, and line 3 makes use of the results of line 2 as its start line. Consequently, the code cuts off two parts from the url: /index.html first after which /botapp. Critically, this code doesn’t modify the occasion variable, so every subsequent run of the execute() technique will return the specified textual content: Copyright (c) 2023, http://mycompany.com.

To be clear, this isn’t an environment friendly strategy to coding this downside. It might be higher to calculate the outcome as soon as and retailer it for reuse. The next code, used as a constructor for the AboutCommand class, would obtain that:

public AboutCommand(String s) {

  url = s.substring(0, s.lastIndexOf(“https://oraclejavacertified.blogspot.com/”));

  url = url.substring(0, url.lastIndexOf(“https://oraclejavacertified.blogspot.com/”));

  url = “Copyright (c) 2023, ” + url;

}

Given this initialization, the execute technique would merely appear to be the next:

public String execute(Context c) {

  return url;

}

There’s a closing word to say right here. Have been you questioning about that constructor argument Context c? In quiz questions, it’s best to assume that there’s sufficient code surrounding what’s proven, or sufficient context, to permit the code to work if it may possibly work. If there’s an issue you might be anticipated to acknowledge, it’ll be within the code you might be proven, not in one thing that’s not proven.

The Java 17 examination aims embrace some notes in regards to the assumptions of this sort that you’re anticipated to make. The notes are current for the Java 8 examination too, however for some motive, probably easy oversight, they weren’t included for the Java 11 model.

Conclusion. The proper reply is choice E.

Supply: oracle.com

[ad_2]

Leave a Comment

Your email address will not be published. Required fields are marked *