Math & Numbers

Math & Numbers

24 questions · Fundamental Engineering

Practice

From the answer group below, select the correct combination of answers to be inserted into A through C in the description.

Function f receives an integer value as argument argval and returns the grade character. The return value of f(92), f(72), and f(-10) are “A,” “B,” and “C,” respectively. Note that the function has a specification bug.

[Program]
○ character: f(integer: argval)
character: retvar ← "w"
if (argval > 90)
retvar ← "a"
endif
if (argval > 70)
retvar ← "b"
elseif (argval > 60)
retvar ← "c"
elseif (argval ≤ 60)
retvar ← "d"
endif
return retvar

Answer group

OptionABC

From the answer group below, select the correct combination of answers to be inserted into A through C in the program.

The function isHarshad checks whether the given integer n is a Harshad number. The function returns true if the given number is a Harshad number, or false otherwise. A Harshad number, also known as a Niven number, is a positive integer that is divisible by the sum of its digits. For instance, 156 is a Harshad number because the sum of its digits (1 + 5 + 6 = 12) divides it evenly without a remainder (156 ÷ 12 = 13).

[Program]
○ boolean: isHarshad(integer: n)
integer: num ← n
integer: sum ← 0
if (num < 1)
return false
endif
while (num > 0)
sum ← sum + (A)
num ← (B)
endwhile
return (C) // Return true if n is a Harshad number,
// or false otherwise.

Answer group

OptionABC

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The quadratic equation a1x2 + b1x + c1 = 0 can be formulated as x2 − 2(−b1/2a1) + (c1/a1) = 0 , where a1 ≠ 0 . If we replace (−b1/2a1) and (c1/a1) by b and c respectively, it can be further formulated as x2 − 2bx + c = 0, and the roots are as follows:
Case 1: if b2 − c = 0, double root b,

Case 2: if b2 − c > 0, two real roots b ± √b2 − c,

Case 3: if b2 − c < 0, two imaginary roots b ± i√c − b2, where i = √−1.

The procedure quadEquation takes the coefficients of a quadratic equation in arguments a1, b1, and c1 and uses the above formula to determine and output the roots. Here, a1 is not 0 and function sqrt returns the principal square root of its non-negative argument.

[Program]
○ quadEquation(real: a1, real: b1, real: c1)
real: b ← -b1 ÷ (2 × a1)
real: c ← c1 ÷ a1
real: val ← b × b - c
real: tmp
if (val = 0)
output "root1=root2=", A
elseif (val > 0)
tmp ← sqrt(val)
output "root1=", b + tmp
output "root2=", b - tmp
else
tmp ← B
output "root1=", b, "+", tmp, "i"
output "root2=", b, "-", tmp, "i"
endif

Answer group

OptionAB

From the answer group below, select the correct answer to be inserted into blank in the program. The same answer goes into both blanks blank. Here, the array index starts at 1.

The program converts the code point of a Unicode character to a 2-byte UTF-8 encoding. In this question, “(16)” after a numerical value indicates a hexadecimal value. Each Unicode character is given an integer value that is known as a code point. A character with a code point that ranges from 80(16) to 7FF(16) is encoded to a 2-byte value as below.

Let the bit pattern with a 2-byte length be: 110xxxxx 10xxxxxx. The underlined 11 “x” positions in the bit pattern store the 11-bit code point. The code point is justified to the right, and 0 is stored in any leftover “x” positions. This 2-byte value is the encoded result. For instance, when the code point AE(16) for Registered Sign character “®” is represented in binary, it is 10101110. When this is stored right-justified in the “x” positions in the bit pattern above, it is 110xxx10 10101110. When 0 is stored in the three leftover “x” positions, the UTF-8 encoding for Registered Sign character “®” 11000010 10101110 is obtained. In decimal form, this corresponds to {194, 174}.

The function encode converts a Unicode code point that is passed as an argument to UTF-8 encoding, and returns an integer array that stores it one byte per element from the start of the array. It is assumed that only an integer value that ranges from 80(16) to 7FF(16) is passed to encode as an argument.

[Program]
○ integer []: encode(integer: codePoint)
/* the initial value of utf8Bytes is the value when “x”s in the bit pattern are replaced
with 0, divided into two 8-bit blocks, and each deemed to be binary */
integer []: utf8Bytes ← {192, 128}
utf8Bytes[2] ← utf8Bytes[2] + (codePoint mod blank)
utf8Bytes[1] ← utf8Bytes[1] + integer part of (codePoint ÷ blank)
return utf8Bytes

Answer group

From the answer group below, select the correct combination of answers to be inserted into A through C in the program.

The procedure reverse_digits receives one positive integer argument named num and outputs both the original integer and the integer with its digits reversed. Assume that the ones digit of num is not 0.

Example:
Number is 456, output 654

Number is 23407, output 70432

[Program]
○ reverse_digits(integer: num)
integer: rev ← 0
integer: rm ← 0
integer: temp ← num
while (A)
rm ← temp mod 10
rev ← B
temp ← C
endwhile
output "Number is ", num, ", output ", rev

Answer group

OptionABC

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

Prime numbers are the positive integers that are only divisible by the number itself and 1. Procedure printPrime receives the integer N (N ≥ 1) as an argument and prints the first N prime numbers. For example, printPrime(6) will print “2 3 5 7 11 13”.

[Program]
○ printPrime(integer: N)
integer: count, number, i
boolean: isPrime
count ← 1
number ← 2
while (count ≤ N)
isPrime ← true
for (increase i from 2 to integer part of the square root of number by 1)
if (number mod i = 0)
A
exit the for block
endif
endfor
if (isPrime = true)
output " ", number
B
endif
number ← number + 1
endwhile

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A through C in the program.

Centurial years refer to the years that are divisible by 100. The centurial years are not leap years except for years that are exactly divisible by 400. The non-centurial years, that is, years that are not divisible by 100, refer to leap years that are divisible by 4. The function isLeapYear receives an integer number year and returns true if a given year is a leap year or false otherwise.

[Program]
○ boolean: isLeapYear(integer: year) // returns true if the variable year
// is a leap year; otherwise, returns false
if (A)
return B
elseif (C)
return false
elseif (year mod 4 = 0)
return true
else
return false
endif

Answer group

OptionABC

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The function isPerfect receives positive number n, and returns whether n is a perfect number. Here, a number is a “perfect number” if the sum of its positive divisors (excluding the number itself) is equal to the number itself. For instance, 28 is a perfect number because 28 has divisors 1, 2, 4, 7, and 14, and 1 + 2 + 4 + 7 + 14 = 28.

[Program]
○ boolean: isPerfect(integer: n)
integer: k
integer: sum ← 0
integer: half ← integer part of (n ÷ 2)
for (increase k from 1 to half by 1)
if (A)
B
endif
endfor
if (sum = n)
return true
else
return false
endif

Answer group

OptionAB

From the answer group below, select the correct answer to be inserted into blank in the description.

The greatest common divisor (GCD) of two numbers is the largest number that divides both of them. The function GCD receives two positive integer numbers and returns their GCD. When the function GCD is called as GCD(98, 56), the output is blank in (1)-(4) below. Here, the output statement “output m, n” outputs the values of variables m and n, and subsequently starts a new line.

(1)98 5642 1428 1414 7
(2)42 5642 1428 1414 14
(3)42 5642 2814 2814 14
(4)56 4242 1414 2814 14
[Program]
○ integer: GCD(integer: x, integer: y)
integer: m ← x
integer: n ← y
while (m ≠ n)
if (m > n)
m ← m - n
else
n ← n - m
endif
output m, n
endwhile
return m

Answer group

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The following function receives an integer number and returns the result of interpreting the decimal representation of the number as a binary number. Note that the number is non-negative, and its decimal representation comprises only the digits 0 and 1. For instance, if it receives 1100, it returns 12.

[Program]
○ integer: convert(integer: number)
integer: place, n, remainder, decimal
decimal ← 0
place ← 1
n ← number
while (n > 0)
remainder ← n mod 10
n ← integer part of (n ÷ 10)
decimal ← decimal + A
place ← B
endwhile
return decimal

Answer group

OptionAB

From the answer group below, select the correct answer to be inserted into blank in the program.

The function calc receives the positive real numbers x and y, and returns the result of the calculation of (√x + √y)2. For instance, when the function calc is called as calc(4, 9), the return value is 25. Here, the function pow(a, b) returns a raised to the power of b.

[Program]
○ real: calc(real: x, real: y)
return blank

Answer group

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

Gray code is a sequence of binary numbers in which two successive values differ by only 1 bit. The function GrayBiCon converts the gray code to a binary code using bitwise operators. The bitwise operators operate on the individual bits of the variables. The function receives the 8-bit type argument x as a gray code, and returns a corresponding binary number of the given argument. The value of each bit after conversion is the exclusive OR of the most significant bit in the gray code up to the corresponding bit position and the converted value of the next higher bit position. For instance, when the function GrayBiCon is called as GrayBiCon(00001100), the return value is a binary number 00001000. The common bitwise operators are listed in the table below:

Table Operators

OperatorName
&Bitwise AND
|Bitwise OR
^Bitwise XOR (exclusive OR)
<<Shift left
>>Shift right

For instance, v << n performs a logical shift of the value of v by n bits to the left.

[Program]
○ 8-bit: GrayBiCon(8-bit: x)
8-bit: y ← x
8-bit: z ← x
while (z ≠ 00000000)
z ← z A 1
y ← B
endwhile
return y

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The function cosine(z) returns an approximate value of the cosine value of z degrees. The function uses Maclaurin series expansion for the cosine shown below and can be used to determine cos(y) for values of y radians.

cos(y) = 1 − y22! + y44! − y66! + ⋯

[Program]
○ real: cosine(real: z)
real: y ← z × π ÷ 180 // convert from degrees to radians
real: term ← 1
real: cosy ← term
integer: n ← 0
while (absolute value of A > 0.000000001)
n ← n + 1
term ← term × (-1 × (y B) ÷ (2 × n × (2 × n - 1)))
cosy ← cosy + term
endwhile
return cosy

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The function coupon receives the argument prod_id (a positive integer value) of product ID, and pur_prod (a positive integer value) of the number of purchased products by a customer. The function returns the number of coupons.
For every purchase of three products of the product ID of which the last digit is three, the customer receives one coupon. Otherwise, they receive no coupon.

[Program]
○ integer: coupon(integer: prod_id, integer: pur_prod)
integer: num_coupon ← 0
if (A)
num_coupon ← B
endif
return num_coupon

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The procedure primeFactors outputs the prime factors for the input values as its argument. The first few prime numbers are 2, 3, 5, 7, 11, and 13. For instance, if the input integer is 12, the output is “2×2×3”. If the input integer is 78, the output is “2×3×13”. The input integer must be greater than 1.

[Program]
○ primeFactors(integer: num)
integer: i
i ← 2
do
if (A)
num ← integer part of (num ÷ i)
output i
if (B)
output "×"
endif
else
i ← i + 1
endif
while (B)

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A through C in the program.

The standard form of a quadratic equation is as follows:

ax2 + bx + c = 0, where a, b and c are real numbers and a ≠ 0.

Here, the term b2 - 4ac is known as the discriminant of a quadratic equation. It indicates the nature of the roots. The formula for solving a quadratic equation is shown in the figure. If the discriminant value is positive, there are two solutions; if it is zero, there is one solution. Here, we assume that the discriminant value is non-negative.

If the discriminant > 0root1 = −b + √b2 − 4ac2aroot2 = −b − √b2 − 4ac2a
If the discriminant = 0root1 = root2 = −b2a

Figure The formula for solving a quadratic equation

The procedure findRoots receives three real number arguments a, b, and c as coefficients and outputs the value of the root(s) of the quadratic equation. Function sqrt(discriminant) returns the principal square root value of the parameter discriminant.

[Program]
○ findRoots(real: a, real: b, real: c)
real: discriminant, root1, root2
discriminant ← A
if (B)
root1 ← (-b + sqrt(discriminant)) ÷ (2 × a)
root2 ← (-b - sqrt(discriminant)) ÷ (2 × a)
output "root1 = ", root1, " and root2 = ", root2
elseif (C)
root1 ← -b ÷ (2 × a)
output "root1 = root2 = ", root1
endif

Answer group

OptionABC

From the answer group below, select the correct combination of answers to be inserted into A through C in the program.

A school determines the letter grade that a student will receive based on the score, which is an integer value between 0 and 100, as follows:

ScoreLetter gradeDescription
80 – 100DPass with distinction
50 – 79PPass
0 – 49FFail

The function grade receives a score (non-negative integer value between 0 and 100) and returns the letter grade as a character.

[Program]
○ character: grade(integer: score)
character: ret
if (score A 80)
ret ← "D"
elseif (score A 50)
ret ← B
else
ret ← C
endif
return ret

Answer group

OptionABC

From the answer group below, select the correct answer to be inserted into blank in the program.

A bus company operates buses between two cities. The standard ticket price is 20 US dollars and the discount ticket price for passenger aged 10 and under or aged 60 and over is 10 US dollars. Additionally, registered members of all ages always get the ticket at the discount price.

The function ticketPrice receives the arguments age (a non-negative integer value) and isMember (a boolean indicating that the passenger is a member if the value is true), which returns the value ret as the ticket price (in US dollars).

[Program]
○ integer: ticketPrice(integer: age, boolean: isMember)
integer: ret
if (blank)
ret ← 10
else
ret ← 20
endif
return ret

Answer group

From the answer group below, select the correct answer to be inserted into blank in the program.

The program outputs the even numbers between 1 and 100. Subsequently, it prints the sum of those even numbers. Note that division is performed for data type integer, that is, a ÷ b is the quotient of a divided by b.

[Program]
integer: i
integer: sum ← 0
for(increase i from 1 to 100 by 1)
if(blank)
output i
sum ← sum + i
endif
endfor
output sum

Answer group

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The function sumDigits receives a non-negative integer value num as argument, and returns the sum of the digits of num.

[Program]
○ integer: sumDigits(integer: num)
integer: sum ← 0
while (num > 0)
sum ← A
num ← B
endwhile
return sum

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The function division receives two integer values a and b and returns the quotient of a divided by b. The function modulus receives two integer values a and b and returns the remainder of a divided by b. The procedure convert receives the value of seconds as the input argument and outputs that value in the form of hours, minutes, and seconds. For example, when the procedure convert is called as convert(5450) the output is “1, 30, 50”. Here, suppose that the range of input values satisfy 0 ≤ input < 86400.

[Program]
○ convert(integer: input)
integer: hour, minute, second
second ← modulus(input, 60)
minute ← A
hour ← B
output hour, minute, second
○ integer: division(integer: a, integer: b)
integer: u
u ← integer part of (a ÷ b)
return u
○ integer: modulus(integer: a, integer: b)
integer: u
u ← a mod b
return u

Answer group

OptionAB

From the answer group below, select the correct answer to be inserted into blank in the program.

The function count1 receives the bit8 type (8-bit type) argument byte, and returns the number of bits 1 in the argument. For example, when the function count1 is called as count1(11001011), the return value is 5.
Here, operator & represents a bitwise logical product, operator | represents a bitwise logical sum; operator >> represents a logical shift to the right, and operator << represents a logical shift to the left. For example, v << n performs a logical shift of the value of v by n bits to the left.

[Program]
○ integer: count1(bit8: byte)
bit8: rbyte ← byte
integer: r ← 0
integer: i
for (increase i from 1 to 8 by 1)
if ((blank) ≠ 00000000)
r ← r + 1
endif
endfor
return r

Answer group

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The Fibonacci sequence is a sequence in which each number is equal to the sum of the two preceding numbers. In this question, the sequence starts with 0 and 1. The first 10 values in the sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. For example, the 8-th number 13 is the sum of the two preceding numbers 5 and 8.

The function fibo takes an integer value n as the argument and returns the value at the n-th position. Here, n reflects the position of the number in the sequence, starting with 1 (one). When n is 9, the function returns 21.

[Program]
○ integer: fibo(integer: n)
if (A)
return n - 1
else
return B
endif

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The function m_sin calculates and returns the approximate value of sin(x) for the argument x using the Maclaurin expansion. The program calculates sin(x) using the approximate formula below:

sin(x) = x/1! - x3/3! + x5/5! - … + (-1)n × ( x(2n+1)/(2n+1)!)

Here, ! is the factorial symbol, and n is the first integer for which | x(2n+1) / (2n+1)! | ≤ 10-7 is satisfied.

[Program]
○ real: m_sin(real: x)
real: vn ← x
real: k ← 1
real: sum ← vn
real: epsi ← 1×10⁻⁷
while (abs(vn) A) // abs(vn) returns the absolute value of vn
k ← k + 2
vn ← B
sum ← sum + vn
endwhile
return sum

Answer group

OptionAB