Question
15.Consider the following function typoglycemia , which takes as input a valid string s , containing at least one character. It is intended to return
15.Consider the following functiontypoglycemia, which takes as input a valid strings, containing at least one character. It is intended to return a string formed by taking the first and last letter of each of the words in the input string, as well as the space in between words. Return 'Error' if the input string is only one word.
DEFINE typoglycemia(s)
size = 0
abbr=""
FOR i = 0 TO length(s) BY 1 DO
IF (s[i]==' ') AND (i < length(s) - 1) THEN
size = size + 1
abbr = abbr + s[i - 1]
abbr = abbr + s[i]
abbr = abbr + s[i + 1]
ELSEIF (i==0) THEN
abbr = abbr + s[i]
ELSEIF (i==(length(s)-1)) THEN
abbr = abbr + s[i]
ENDIF
ENDFOR
IF ( /* missing code 1 */) THEN
/* missing code 2 */
ELSE
/* missing code 3 */
ENDIF
ENDDEF
Which of the following can be used to replace /* missing code 1 */, /* missing code 2 */, and /* missing code 3 */ so thattypoglycemiaworks as intended. For example, the function call typoglycemia("Can you read this ?") would return the following line:
Cn yu rd ts
-/* missing code 1 */ length(abbr) != 0
/* missing code 2 */ RETURN abbr
/* missing code 3 */ RETURN "Error"
-/* missing code 1 */ length(s) == 0
/* missing code 2 */ RETURN "Error"
/* missing code 3 */ RETURN abbr
-/* missing code 1 */ size == 0
/* missing code 2 */ RETURN "Error"
/* missing code 3 */ RETURN abbr+typoglycemia(s.substring(1:length(s)))
-/* missing code 1 */ size == 1
/* missing code 2 */ RETURN abbr
/* missing code 3 */ RETURN "Error"
-/* missing code 1 */ size == 0
/* missing code 2 */ RETURN "Error"
/* missing code 3 */ RETURN abbr
16.Which of the following functions will correctly calculate the average cost of a trip to the grocery store based upon he following adjustable information:
- Average apple price - average price of app1es, where the price is per pound
- Average apple weight - average weight of apples to be bought, in pounds
- Average pasta box price - average price of a box of pasta
- Number of pasta boxes - number of pasta boxes to be bought
- Average juice bottle price - average price of a juice bottle
- Number of juice bott1es - number of juice bottles to be bought
-DEFINE shop (APPLEl, APPLE2, APPLE3, APPLE4, Pastal, Pasta2, Pasta3,
Pasta4, Juicel, Juice2, Juice3)
averageCost = 0
appleWeight = (APPLE1 + APPLE2 + APPLE3 + APPLE4) / 4
numPasta = (Pasta1 + Pasta2 + Pasta3 + Pasta4) / 4
numJuice = (Juicel + Juice2 + Juice3) / 3
averageCost = (appleWeight*applePrice)+(numPasta*pastaPrice)+
(numJuice*juicePrice)
RETURN averagecost
ENDDEF
-DEFINE shop (appleWeight, numPasta, numJuice)
applePrice = 0.25
pastaPrice = 0.25
juicePrice = 0.25
averagecost = 0
averageCost = (appleWeight*applePrice)+ (numPasta*pastaPrice) +
(numJuice* juicePrice)
RETURN averageCost
ENDDEF
-DEFINE shop(applePrice, pastaPrice, juicePrice)
APPLES = (l, 5, 2, 3)
pasta = (4, 10, 6, 2)
juice = (24, 8, 12)
averageCost = 0
appleWeight = (APPLES[0] + APPLES[1] + APPLES [2]+APPLES[3])/length(APPLES)
numPasta - (Pasta[O] + Pasta.[l] + Pasta[2] + Pasta[3J), / length(Pasta)
numJuice = (Juice [ ,Q] + Juice [ 1] + Juice [ 2]) / length (Juice)
averageCost= (appleWeight*applePrice)+(numPasta*pastaPrice)+
(numJuicejuicePrice)
RETURN averagecost
ENDDEF
-DEFINE shop(appleWeight, applePrice, numPasta, pastaPrice,
numJuice, juicePrice)
averageCost = 0
averageCost=(appleWeight*applePrice)+(numPasta*pastaPrice)+
(numJuice * juicePrice)
RETURN averageCost
ENDDEF
-DEFINE shop(appleWeight, applePrice, numPasta, pastaPrice,
numJuice, juicePrice)
averageCost = O
averagecost = (appleWeight*applePrice)/l00+(numPasta*pastaPrice)/100+
(numJuice * juicePrice)/100
RETURN averageCost
ENDDEF
17.
charList=['h','a','p','p','y','b','i','r','t','h','d','a','y']
firstWord = []
secondWord = [ ]
flag = false
FOR i = 0 TO length(charList) BY 1 DO
IF (/* missing code 1 */) THEN
/* missing code 2 */
flag = true
ELSE
/* missing code 3 */
ENDIF
ENDFOR
Fill in /* missing code 1 */, /* missing code 2 */,and /* missing code 3 */to complete the code above so that it separates the array of characters into two arrays, firstWord, containing "happy", and secondWord, containing "birthday".
-/* missing code 1 */ charList[i]=='b' OR flag==true
/* missing code 2 */ add(firstWord[i], charList[i])
/* missing code 3 */ add(secondWord[i], charList(i])
-/* missing code 1 */ charList[i]=='b'
/* missing code 2 */ add(secondWord[i], charList[i])
/* missing code 3 */ add(firstWord[i], charList[i])
-/* missing code 1 */ charList[i]=='b' OR flag==true
/* missing code 2 */ add(secondWord[i-length(firstWord)], charList[i])
/* missing code 3 */ add(firstWord[i], charList[i])
-/* missing code 1 */ charList[i]=='b'
/* missing code 2 */ add(firstWord[i], charList[i])
/* missing code 3 */ add(secondWord[i-length(firstWord)], charList[i])
-/* missing code 1 */ charList[i]=='b'
/* missing code 2 */ add(secondWord[i-length(firstWord)], charList(i])
/* missing code 3 */ add(firstword[i], charList[I])
18.
DEFINE reverse(s)
IF ( /* missing code l */) THEN
/* missing code 2 */
ELSE
PRINT s[length(s) - l]
/* missing code 3 */
ENDIF
ENDDEF
Write code to fill in /* missing code l */, /* missing code 2 */, and /* missing code 3 */ to complete the mirror function such that it prints the mirror image of a valid string s, where s is a non-empty string. For example the function call reverse ("Computers") would print
'sretupmoC'
-/* missing code 1 */ length(s) == 0
/* missing code 2 */ RETURN ""
/* missing code 3 */ RETURN s[length(s) - l] +
reverse(s.substring(0 : length(s) - 1))
-/* missing code 1 */ length(s) == 1
/* missing code 2 */ RETURN s[0]""
/* missing code 3 */ reverse(s.substring(0 : length(s) - 1))
-/* missing code 1 */ length(s) == 0
/* missing code 2 */ RETURN s[0]""
/* missing code 3 */ reverse(s.substring(0 : length(s)-1))
-/* missing code 1 */ length(s) == 0
/* missing code 2 */ RETURN s[0] ""
/* missing code 3 */ RETURN s[length(s) - l] +
reverse(s.substring(0 : length(s) - 1))
-/* missing code 1 */ length(s) == 1
/* missing code 2 */ RETURN ""
/* missing code 3 */ reverse(s.substring(1 : length(s) - 1))
19.
x = 3
y = 9
z = 5
IF y % x == 0 THEN
IF y >= z THEN
x = x * x
ELSE
z = z * z
ENDIF
ELSE
IF z >= X THEN
IF y >= z THEN
y = y * y
ENDIF
ELSE
z = z * z
ENDIF
ENDIF
IF y % x == 0 THEN
x = x + 3
ELSE
y = y * 2
ENDIF
PRINTLN "x = ", x
PRINTLN "y = ", y
PRINTLN "z= ", z
What is printed as a result of this code segment?
-x = 3
y = 81
z = 5
-x = 3
y = 9
z = 25
-x = 12
y = 9
z = 5
-x = 12
y = 81
z = 15
-x = 9
y = 81
z = 25
20.Given the function definition for computing the displacement of an object:
DEFINE calculate (a, b, c)
solution= a * c + 0.5 * b * c * c
RETURN solution
ENDDEF
And the following code statements:
x = 3
y = 12
z = 9
solution = calculate(x, y, z)
Which of the following statements is true after the call tocalculate( x, y, z)has completed execution?
-The order of inputs to the function call is unimportant; the function definition declares how the inputs are used. e.g.,
calculate(x, y, z) == calculate(y, x, z)
-a, b, and c are undefined.
-a = 3, b = 12 , and c = 9
-calculate is called with up to 3 variables.
-If the value of a changes inside the calculate function.,. the value of x will also be changed.
21.
x = 3
y = 10
z = 50
IF(/* condition 1 */) THEN
IF(/* condition 2 */) THEN
PRINTLN "Green is my favorite color!"
ELSE IF(/* condition 3 */) THEN
PRINTLN "Blue rules!"
ELSE
PRINTLN "All of the rainbow!"
ENDIF
ELSE
x = x * 2
IF(/* condition 1 */) THEN
PRINTLN "All of the rainbow!"
ENDIF
ENDIF
Which of the following can be used to replace /* condition 1 */, /* condition 2 */, and /* condition 3 */ so that the following line is printed:
All of the rainbow!
-/* condition 1 */ /* condition 2 */ /* condition 3 */
x < y (y * 3) < zz <= ((y + 1) / x)
-/* condition 1 */ /* condition 2 */ /* condition 3 */
x > y (y * 3) < zz > x
-/* condition 1 */ /* condition 2 */ /* condition 3 */
(x * 2) < yy < zz >= ((y + 1) / x)
-/* condition 1 */ /* condition 2 */ /* condition 3 */
(x * 2) > yy <= zz >= x
-/* condition 1 */ /* condition 2 */ /* condition 3 */
x <= yy > zz >= x
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started