Documente Academic
Documente Profesional
Documente Cultură
Chapter 8:
Practice Questions
-------------------------------------->Objective-Type Questions<--------------------------------State True or False:
1. It is possible to find the square root of a number using the Math.pow( )
function.
2. Math.sqrt( ) function is used to find the square of a number.
3. All trigonometric functions works with angles specified in radians.
4. Math.abs( ) function is used to round off a real number to the nearest
integer.
5. Math. rint(12.5) will return 12.0.
6. Math.ceil( ) and Math.floor( ) are same.
7. NaN stands for Not a Number, which is received when you pass a
negative number to the Math.sqrt( ) function.
8. Math.cosec( ) function is the reciprocal of Math.sin( ) function.
9. Math.cot(x) function is used as a reciprocal of Math.tan(x) function.
10. Math.round( ) function returns an int value if the parameter is float
data type and long value if the parameter is of double data type.
[True]
[True]
[True]
[False]
[True]
[False]
[True]
[False]
[False]
[True]
---------------------------------->Subjective-Type Questions-------------------------------------A. Answer the following questions:
1. Name the class that is used for different mathematical functions. Give an
example of a mathematical function.
[ICSE 2007]
[ICSE 2008]
Ans. i) 9.99
ii) 3.0
3. Give the output of the following functions:
a) Math.floor(126.349)
b) Math.max(45.6,17.3)
d) Math.pow(4,3)
e) Math.sqrt(625)
g) Math.log10(1000)
h) Math.rint(14.5)
j) Math.floor(15.36)
k) Math.round(146.5)
m) Math.min(14.3,14.3)
n) Math.rint(14.562)
Ans. a)
d)
g)
j)
m)
-127.0
64.0
3.0
15.0
14.3
b)
e)
h)
k)
n)
45.6
25.0
14.0
147
15.0
c) Math.min(0.0,0.0)
f) Math.cbrt(125)
i) Math.ceil(12.56)
l) Math.max(11,11)
o) Math.ceil(14.0)
c)
f)
i)
l)
o)
-0.0
5.0
-12.0
11
-14.0
Ans. a)
b)
c)
d)
e)
Math.sqrt(a+b)
1/3.0*a*a*a+1/4.0*b*b*b
s=u*t+1/2.0*a*t*t
d=Math.sqrt(l*l+b*b)
-b+Math.sqrt(b*b-4*a*c)/(2*a)
-b-Math.sqrt(b*b-4*a*c)/(2*a)
Math.pow(Math.sin(x),2)+Math.pow(Math.cos(y),2)
(Math.tan(a)+Math.tan(b))/(1-Math.tan(a)*Math.tan(b))
(1-Math.log10(x))/(1+Math.log10(x))
Math.cbrt(5*x*x+Math.sin(y))/Math.pow(Math.cos(x)+11*y*y*y,1/4.0)
(Math.asin(x)+Math.acos(y))/Math.sqrt(Math.log(Math.cos(x)*Math.cos
f)
g)
h)
i)
j)
(x)))
k)
Math.atan(x)+Math.cos(y)*Math.cos(y)-2*x*x*y*y*yMath.sqrt(4*Math.exp(2*x))
l) Math.sqrt(2*a*s+u*u)
[ICSE 2006]
) No. of years
If the values of r and x are entered through the keyboard. Write a program to
calculate the above values of the circle.
if(a*a==b*b+c*c)
area=1/2.0*b*c;
else if(b*b==a*a+c*c)
area=1/2.0*a*c;
else
area=1/2.0*a*b;
System.out.println("It is a right-angled triangle");
}
else
{
s=(a+b+c)/2.0;
area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("It is a scalene triangle");
}
System.out.println("Area of the triangle="+area);
}
else
System.out.println("Triangle cannot be formed");
}
}
6. Write a program to calculate the values of X, where
() + ()
X=+
+ () ()
Where the value of a ranges from 0 to 90 and the corresponding value of b is 90 a.
9. Write a program to input two integers, say x and n, and find the sum of the
following series:
for(i=1;i<=n;i++)
{
sum+=Math.pow(x,i)/i;
}
System.out.println("Answer="+sum);
}
static void series_ii()
{
int x,n,i;
double sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integers:");
x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
sum+=Math.pow(x,i)/(i+1);
}
System.out.println("Answer="+sum);
}
static void series_iii()
{
int x,n,i;
double sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integers:");
x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
sum+=Math.pow(x,i+1)/i;
}
System.out.println("Answer="+sum);
}
static void series_iv()
{
int x,n,i;
double sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integers:");
x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(i%2==0)
sum-=Math.pow(x,i)/i;
else
sum+=Math.pow(x,i)/i;
}
System.out.println("Answer="+sum);
}
static void series_v()
{
int x,n,i;
double sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integers:");
x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(i%2==0)
sum-=Math.pow(x,i)/(i+1);
else
sum+=Math.pow(x,i)/(i+1);
}
System.out.println("Answer="+sum);
}
static void series_vi()
{
int i,j,x,n;
long f;
double sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integers:");
x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=i;j++)
f=f*j;
sum+=Math.pow(x,i)/f;
}
System.out.println("Answer="+sum);
}
produces a table of the following form for all angles from 30 to degrees in 5
degree increments and initial velocities from 500 to 1000m/sec in 100m/sec
increments:
(INITIAL VELOCITY=500m/sec)
ANGLE
DISTANCE
TIME
HEIGHT
12. A game of dice is to be simulated for two players, each player gets a chance to
throw his dice, and the value is added to his points, this process continues
alternately until for the player whose added points equal to 20 and is declared the
winner. Write a program to simulate this process using the random( ) function.
d1=1+(int)(Math.random()*6);
d2=1+(int)(Math.random()*6);
if(d1+d2==n)
break;
}while(true);
return c;
}
static void main()
{
int s;
s=rollingPair(2);
System.out.println("Number of rollings for Snake eyes:"+s);
}
}
16. The roots (say x1 and x2) of a quadratic equation ax2+bx+c=0 can be
determined by its discriminant (d), where d = b2 4ac. Depending upon its value
the nature of the roots and the value of the roots can be determined.
i) If d=0 then the roots are Real and Equal and the roots can be found using the
formula:
ii) If d>0 then the roots are Real and Distinct and the roots can be found using the
formula:
iii) If d<0 then the roots are Imaginary and therefore roots are not found.
Write a program to input a,b and c the coefficient of the quadratic equation and
find its roots.
System.out.println("Roots are:"+r1+"\t"+r2);
}
else if(d>0)
{
System.out.println("Roots are Real and Distinct");
r1=(-b+Math.sqrt(d))/(2*a);
r2=(-b-Math.sqrt(d))/(2*a);
System.out.println("Roots are:"+r1+"\t"+r2);
}
else
{
System.out.println("Roots are Imaginary");
System.out.println("Roots not found");
}
}
}
17. Write a program for all four operations (addition, subtraction, multiplication
and division according to the users choice). Each of the problem is to be random.
Also if the user gets a problem incorrect, give him/her a second try at the problem.
If the problem is correct display Right, otherwise display Wrong after the
second try.
Ans.
import java.util.*;
class Question17
{
System.out.print("Right");
else
{
System.out.println("Incorrect-please try again");
System.out.print("What is "+a+"*"+b+"=?");
c=sc.nextInt();
if(c==a+b)
System.out.print("Right");
else
System.out.print("Wrong "+a+"*"+b+"="+(a*b));
}
case 4:
a=1+(int)(Math.random()*100); //generating a random number between 1 to 100
b=1+(int)(Math.random()*100); //generating a random number between 1 to 100
System.out.print("What is "+a+"/"+b+"=?");
c=sc.nextInt();
if(c==(float)a/b)
System.out.print("Right");
else
{
System.out.println("Incorrect-please try again");
System.out.print("What is "+a+"/"+b+"=?");
c=sc.nextInt();
if(c==(float)a/b)
System.out.print("Right");
else
System.out.print("Wrong "+a+"/"+b+"="+(float)(a/b));
}
break;
default:
System.out.println("Wrong Choice!");
}
}
}
18. Write a program to input a real number (floating point number) and round off
each number to 2 places of decimal and display the answer.
For example, if
INPUT: 12.3867
OUTPUT: 12.39
similarly, if
INPUT: 73.2846
OUTPUT: 73.28
{
f=1;
break;
}
s=s+d*(int)Math.pow(2,c++);
n=n/10;
}
if(f==0)
System.out.println("Decimal Equivalent="+s);
else
System.out.println("Not a proper binary number");
}
}