ЁЯУв Too many exams? DonтАЩt know which one suits you best? Book Your Free Expert ЁЯСЙ call Now!


    Question

    You are trying to parse a JSON string in Java using a

    library like org.json. import org.json.JSONObject; import org.json.JSONException; public class JsonParser { ┬а ┬а public static void main(String[] args) { ┬а ┬а ┬а ┬а String jsonString = "{'name': 'Alice', 'age': 30}"; // Potential bug here ┬а ┬а ┬а ┬а try { ┬а ┬а ┬а ┬а ┬а ┬а JSONObject jsonObject = new JSONObject(jsonString); ┬а ┬а ┬а ┬а ┬а ┬а System.out.println("Name: " + jsonObject.getString("name")); ┬а ┬а ┬а ┬а } catch (JSONException e) { ┬а ┬а ┬а ┬а ┬а ┬а System.err.println("JSON Parsing Error: " + e.getMessage()); ┬а ┬а ┬а ┬а } ┬а ┬а } } What is the most likely error this Java code will encounter when trying to parse jsonString?
    A NullPointerException because jsonString is not valid. Correct Answer Incorrect Answer
    B JSONException because JSON requires double quotes for string literals and keys. Correct Answer Incorrect Answer
    C ClassCastException because age is an integer, not a string. Correct Answer Incorrect Answer
    D FileNotFoundException because the JSON is not from a file. Correct Answer Incorrect Answer
    E No error, it will parse correctly. Correct Answer Incorrect Answer

    Solution

    тАв Dry Run: o The JSONObject constructor will attempt to parse the jsonString. o When it encounters the single quotes, it will recognize that the string does not conform to the standard JSON specification. o The org.json library (and most other JSON parsers) will throw a JSONException because of the invalid syntax. тАв Why Correct Answer (B): JSONException because JSON requires double quotes for string literals and keys. o This is the fundamental reason for the parsing failure. The org.json library is strict about JSON syntax.

    Practice Next
    More IT Operating System Questions
    ask-question