Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
PHP Math Functions Aptitude Questions and Answers
PHP Math Functions Aptitude: This section contains aptitude questions and answers on PHP Math Functions.
By Nidhi Last updated : December 15, 2023
This section contains Aptitude Questions and Answers on PHP Math Functions.
1) What is the correct output of the given code snippets?
<?php
$num1 = - 10;
$num2 = - 20;
$num3 = abs($num1 - $num2);
echo ($num3);
?>
- 30
- -10
- 10
- Error
Correct answer: 3
10
Explanation:
The above code will print "10" on the webpage. The abs() is a PHP inbuilt function that always returns a positive value.
2) What is the correct output of the given code snippets?
<?php
$r = 5;
$area = PY() * $r * $r;
echo ($area);
?>
- 78.539816339745
- 78
- Garbage value
- Error
Correct answer: 4
Error
Explanation:
The above code will generate a syntax error because PY() is not an inbuilt function in PHP. We need to use PI() function to get PI value to calculate the area of the circle.
3) Which of the following function is used to convert degree to radian in PHP?
- toradian()
- torad()
- deg2rad()
- None of the above
Correct answer: 3
deg2rad()
Explanation:
The deg2rad() function is used to convert degree to radian in PHP.
4) Which of the following function is used to convert radian to the degree in PHP?
- todegree()
- todeg()
- rad2deg()
- None of the above
Correct answer: 3
rad2deg()
Explanation:
The rad2deg() function is used to convert radian to the degree in PHP.
5) What is the correct output of the given code snippets?
<?php
$A = 11.2;
$B = 3;
$rem = $A % $B;
echo ($rem);
?>
- 2
- 2.2
- Error
- None of the above
Correct answer: 1
2
Explanation:
The above code will print "2" on the webpage. Because modulus '%' operator returns only integer remainder.
6) What is the correct output of the given code snippets?
<?php
$A = 11.2;
$B = 3;
$rem = modf($A, $B);
echo ($rem);
?>
- 2
- 2.2
- Error
- None of the above
Correct answer: 3
Error
Explanation:
The above code will generate error, because modf() is not a inbuilt function in PHP.
7) What is the correct output of the given code snippets?
<?php
$A = 11.2;
$B = 3;
$rem = fmod($A, $B);
echo ($rem);
?>
- 2
- 2.2
- Error
- None of the above
Correct answer: 2
2.2
Explanation:
The above code will print "2.2" on the webpage. The fmod() function is used to get the remainder with floating-point numbers.
Learn: PHP Numbers
8) What is the correct output of the given code snippets?
<?php
$num = - 7.8;
echo (floor($num));
?>
- -7
- -7.5
- -8
- None of the above
Correct answer: 3
-8
Explanation:
The above code will print "-8" on the webpage. The floor() function is used to truncate floating-point values, here -7.8 becomes -8.
9) What is the correct output of the given code snippets?
<?php
$num = 0.70;
echo (ciel($num));
?>
- 1
- 0
- 0.50
- Syntax error
Correct answer: 4
Syntax error
Explanation:
The above code will generate a syntax error because ciel() is not an inbuilt function in PHP.
10) What is the correct output of the given code snippets?
<?php
$num = 0.70;
echo (ceil($num));
?>
- 1
- 0
- 0.50
- Syntax error
Correct answer: 1
1
Explanation:
The above code will print "1" on the webpage. The ceil() roundoff the given floating-point number.
11) Which of the following constants are used in round() function in PHP?
- PHP_ROUND_HALF_UP
- PHP_ROUND_HALF_DOWN
- PHP_ROUND_HALF_EVEN
- PHP_ROUND_HALF_ODD
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct answer: 4
A, B, C, and D
Explanation:
All given constants are used in the round() function to round off the specified floating-point numbers in PHP.
12) What is the correct output of the given code snippets in PHP?
<?php
echo (round(3.456789, 2));
?>
- 3.45
- 3.46
- Error
- None of the above
Correct answer: 2
3.46
Explanation:
The above code will print "3.46" on the webpage. Here, 2nd argument specifies that the given number should contain 2 decimal points then it returns 3.46 by rounding of it.
13) What is the correct output of the given code snippets?
<?php
echo (round(12345678, -2));
?>
- 12345600
- 12345700
- Error
- None of the above
Correct answer: 2
12345700
Explanation:
The above code will print "12345700" on the webpage. Here, -2 specify that the last 2 digits should be 0 then it returns number 12345700 after rounding off except the last two digits.
14) What is the correct output of the given code snippets?
<?php
echo (round(12.5, 0, PHP_ROUND_HALF_UP));
?>
- 12
- 13
- Error
- None of the above
Correct answer: 2
13
Explanation:
The above code will print 13 on the webpage. Here, PHP_ROUND_HALF_UP constant is used to half up the given specified floating-point number.
15) What is the correct output of the given code snippets?
<?php
echo (round(12.5, 0, PHP_ROUND_HALF_ODD));
?>
- 12
- 13
- Error
- None of the above
Correct answer: 2
13
Explanation:
The above code will print 13 on the webpage. Here, PHP_ROUND_HALF_ODD constant is used to rounding off the specified number and the resulted number will be an ODD number.
16) Which of the following function is used to calculate the power of a number?
- power()
- pow_number()
- pow()
- None of the above
Correct answer: 3
pow()
Explanation:
The pow() function is used to calculate the power of a specified number based on given power value.
17) Which of the following function is used to calculate the square root of a number?
- sqrt()
- square_root()
- squareroot()
- None of the above
Correct answer: 1
sqrt()
Explanation:
The sqrt() function is used to calculate the square root of a specified number.
18) Which of the following function is used to get the natural logarithm of a number based on a specified base?
- log()
- logarithm()
- logs()
- None of the above
Correct answer: 1
log()
Explanation:
The log() function is used to get the natural logarithm of a number based on a specified base.
19) Which of the following functions are used for logarithm in PHP?
- log()
- log10()
- log1p()
- log1q()
Options:
- A and B
- B and C
- A, B, and C
- A, B, C, and D
Correct answer: 3
A, B, and C
Explanation:
The log(), log10(), and log1p() are used for logarithm in PHP.
20) What is the correct output of the given code snippets in PHP?
<?php
$NUM = max(1, 2, 8, 5, 6);
echo $NUM;
?>
- 8
- 0
- Garbage value
- Error
Correct answer: 1
8
Explanation:
The above code will print "8" on the webpage. The max() function is used to get the largest number from the specified list.
21) What is the correct output of the given code snippets?
<?php
$NUM = max(array(
1,
2,
8,
5,
6
));
echo $NUM;
?>
- 8
- 0
- Garbage value
- Error
Correct answer: 1
8
The above code will print "8" on the webpage. The max() function is also used to get the largest number from the specified array.
Learn: PHP Arrays
22) What is the correct output of the given code snippets?
<?php
$NUM = max("1,2,8,5,6");
echo $NUM;
?>
- 8
- 0
- Garbage value
- Error
Correct answer: 4
Error
PHP Warning: max(): When only one parameter is given,
it must be an array in /home/main.php on line 2
23) Which of the following functions are used to generate random numbers in PHP?
- rand()
- getrandmax()
- mt_rand()
- xt_rand()
Options:
- A and B
- C and D
- A, B, and C
- A, B, C, and D
24) What is the correct output of the given code snippets in PHP?
<?php
$NUM = rand(10, 20);
echo $NUM;
?>
- Print a random number that will be within 10 to 20
- Error
Correct answer: 1
Print a random number that will be within 10 to 20
Explanation:
The code will print a random number that will be within the range of 10 to 20.
25) What is the correct output of the given code snippets?
<?php
$NUM = rand();
echo $NUM;
?>
- Print a random number
- Error
Correct answer: 1
Print a random number
Explanation:
The code will print a random number.