Photo AI
Question 2
A string variable called month has been assigned the value ‘April’ and another string variable called year has been assigned the value ‘2019’ as shown below. Line 1... show full transcript
Step 1
Answer
To assign the value 'Apr19' to the variable shortDate
, we use substring operations. The expression month[0:3]
extracts the first three characters of the string 'April', which yields 'Apr'.
Next, year[2:4]
extracts the last two characters of the year string '2019', giving us '19'.
Concatenating these two substrings together gives us:
shortDate = month[0:3] + year[2:4]
Therefore, after execution, shortDate
will hold the value 'Apr19'.
Step 2
Answer
An alternative approach can use the substring
or similar functions depending on the programming language. For example:
shortDate = substring(month, 0, 3) + substring(year, 2, 2)
This achieves the same result, and shortDate
will also contain 'Apr19'.
Report Improved Results
Recommend to friends
Students Supported
Questions answered