Adobe Scan 11 Apr 2025
Adobe Scan 11 Apr 2025
Part V(a)
Introduction to Java
17
(c) Byte code: Java compiler converts Java source
code into an
intermediate
binary code called Byte code. This Byte
executed directly by the processor. It needs to be code can'tbe
machine code for its execution. converted into
7. What is BlueJ? What are the features of Blue)?
Ans. Blue) is an integrated development
environment (DE) for the Jaya
programming language. It is a Windows based application for Java, It
uses conmpiler as well as interpreter to convert
code. source code intO machine
Features of Blue):
" It uses a menu driven approach to
carry out various tasks.
" It supports graphical user interface.
" It facilitates easier
debugging.
8. Write down the syntax of output
Ans. We commonly use two output statement in Java with an example.
statements in Java.
() Syntax: System.out.println(<output value>);
This statement displays the values on the
printing the value, it places the cursor at the output screen. After
start of the next line.
So the next printing happens at the start of the next line.
For example:
int a =10, b = 15;
System.out.println(a);
System.out.println(b);
Output:
10
15
(i) Syntax: System.out.print(<output
This statement also prints data on themesSsage>);
line. After printing the value, the output screen but on the same
the end of the printed value. So cursor remains on the same line at
the next printing starts in the
line just after the end of the previous same
For example: printed value.
System.out.print("JVM stands for"):
System.out.print(Java ");
System.out.print("Virtual ");
System.out.print("Machine");
Output:
JVM stands for Java Virtual Machine
9. What is meant by Java
reserved words? Name five Java
which are commonly used reserved words
Ans. In Java, a reserved word is ain Java programming.
word
language. Moreover, the reserved that has a pre-defined meaning in
words the
variables, methods, cannot be used as names for
are also known as classes or any other identifier. The reserved words
keywords.
Five commonly used Java reserved
char. words are public, class, int, double,
18 Teacher'e
10. Differentiate between the
output statements System.out.println( ) and
System.out.print(0.
Ans. The differences are:
System.out.println(0 System.out.print( )
It displays the output of the t
displays the output of the
program and the cursOr moves to program
the next line. but the cursor remains at
the end of thedata in the same line.
Next printing takes place from the Next printing
next line. takes place fron the
same line.
20
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n= in.nextlnt();
double ans = Math.sqrt(n);
System.out.println("Result = + ans);
}
The above program will work for all positive values of n, entered by
the user. When the user entersa negative number, a run time error will
occur. As we know that the square root of a negative number cannot be
determined. So, it is a run time error.
4. What are the different types of errors that take place during the execution
of a program? Name them.
Ans. Logical errors, Syntax errors and Runtime errors occur during the
execution of the program.
5. Give twodifferences between Syntax error and Logical error.
Ans. The differences are:
Syntax Error Logical Error
Syntax errors Occur when we Logical errors occur due to our
violate the rules of writing the mistakes in the programming logic.
statements of the programming
language.
Program is unable to compile and Program compiles and executes but
execute. does not give the desired output.
Input inJava 21
shopkeeper offers 10% discounttonthe printed price of a mobile
Q.2 A
However, a customer has topay 9% GST on the remaining amount. phone.
Writo
a program in Javato calculate the amount to be paid by the
taking printed price as an input. customer
Prog. import java.util.";
public class Phone
else
num2;
numl=num1 +
num2;
num2 = numl -
num2;
num1 = numl-
Q.7 The co-ordinates of twO points A and Bon a straight line are given as
(x1, yl) and (x2, y2). Write a program to calculate the slope (m) of the
line by using formula:
Slope =(y2 - y1) / (x2-x1)
Take the co-ordinates (x1, yl) and (x2, y2) as input.
Prog. import java.util.";
publicclass Slope
public static void main(String args[|)
Scanner in =new Scanner(System.in);
int x1, y1, x2, y2;
float slope;
System.out.print("Enter x coordinate of point A: ");
x1 = in.nextlnt( );
System.out.print("Enter ycoordinate of point A: ");
yl = in.nextint( ):
System.out.print("Enter xcoordinate of point B: ");
x2 = in.nextlnt( );
System.out.print("Enter ycoordinate of point B: ");
y2 = in.nextInt( ):
slope =(y2- y1) / (x2-x1);
System.out.printin("Slope of line: "+slope);
Q.8 A dealer allows his customers a discount of 25% and still gains 25%.
Write a program to input the cost of an article and display its selling
price and markedprice.
Sample Input: 600
Sample Output: Selling Price =750
Marked Price = 1000
Input in Java 25
Chapter 1 Revision of Class IX Syllabus
Part VI
26
Prog. import java.util.";
publicclass Expression
public static void main(String argsll)
Scanner in = new Scanner(System.in);
int a,b, c;
double ans;
System.out.print("Enter the value of a: ");
a = in.nextint( );
System.out.print("Enter the value of b: ");
b= in.nextlnt( );
System.out.print("Enter the value of c: ");
c= in.nextInt( );
ans =(1/Math.pow(a,2) +(2/Math.pow(b,2) +
(3/Math.pow(c.2);
long round_ans =Math.round(ans);
System.out.println("Result roundedto whole number:" + round_ans);
Pythagorean
0.2 For every natural number m>1;2m,m²-1 and m²+1 form a
triplet. console to display a
Write a program to input the value of 'm' through
Pythagorean Triplet!.
Sample Input: 3
Then 2m=6, m²-1-8 and m²+1=10
Thus 6, 8, 10form a Pythagorean Triplet.
Prog. import java.util.*;
public class PIriplet
else
f_Triplet =2*m;
s_Triplet = (int)(Math.pow(m,2)-1);
th_Triplet = (int)(Math.pow(m,2) +1);
System.out.println("Pythagorean Triplets are:");
System.out.println("First Triplet: "+f_Triplet);
System.out.println("Second Triplet: "+s_Triplet);
Q3 Wnte aogram to inputa number. Calculate its square root and cube
Finallv. displav the result bv rounding it off.
Sample Input: 5
Square root of 5= 2.236067
Rounded fom= 2
Cube root of 5 = 1.7099759
Rounded form= 2
Prog. import java.util.",
public class Roots
29
Mathematical Library Methods
C= innextint( ):
d =Math pow(b,2) -(4"a');
System out printIn("Discriminant = "+d);
Q.7 The sum of first nodd natural numbers can be calculated as n', where.
n is the number of odd natural terms.
For example,
Sum= 1+3+5+7: number of odd natural terms =4
Therefore, Sum= 4'=16
Write aprogram to input number of odd natu°al termsand display the
sum of the given series:
(a) 1+3 +5+ +29 +31
Prog. import java.util.";
publicclass Sum
publicstatic void main(String args[)
Scanner in =new Scanner(System.in);
int n,sum;
System.out.print("Enter the number of terms of the series: ");
n = in.nextInt( );
Sum = n*n
System.out.printn("1+3+5+. +29+31 ="+sum);
}
(b) 1+3+5+7+ .+47+ 49
Prog. import java.util.";
publicclass Sum
public static void main(String argsl])
Scanner in = new Scanner(System.in);
int n,sum;
System.out.print("nter the number of terms of the series: "):
n= in.nextint( );
Sum = n*n
System.out.println("1+3+5+. +47+49 ="+sum);
Q.8 Write a program to input the sum. Calculate and display the compound
interest in 3 years, when the rates for the successive years arer1%, r2%
and r3% respectively.
Hint: A=P ++
100/ 100 100/ CI=A-P
System.out.println("Sum="+s);
else if (n ==2)
d=a-b;
System.out.println("Difference="+d);
else if (n ==3)
p=a*b;
System.out.println("Product="+p);
else
System.out.printin("Wrong Choice!");
32
2. if-else-if construct into switch case:
if(var==1)
System.out.println("Distinction");
else if(var==2)
System.out.println("First Division");
else if(var==3)
System.out.println("Second Division");
else
System.out.print1n("invalid");
Ans. switch (var)
case 1:
System.out.println("Distinction");
break;
case 2:
System.out.println("First Division");
break;
case 3:
System.out.println("Second Division");
break;
default:
System.out.println("invalid");
in Java 35
Conditional Statements
else
Svstem out printn(Not an tquable Iriangle");
Q.2 Aspval twodigit number is such that when the sum of its digits js
added to the product of its digits, the result is equal to the original two.
digit nunmber.
Tor cramplc: Consider the number 59.
Sum of digits = 5 +9= 14
Product of digits =5*9 =45
Total of the sum of digits and product of digits = 14 +45 =59
Write aprogram to accept atwo-digit number. Add the sum of its digits
to the product of its digits. If the value is equal to the number input,
display the message 'Special 2 - digit number" otherwise, display the
message "Not a special two-digit number".
Prog. import java.util.*;
public class Sp_Number
publicstatic void main(String args||)
Scanner in = new Scanner(System.in);
int num, num1, digitl, digit2, dsum, dprod, gsumn;
System.out.print("Enter a 2-digit number: ");
num = in.nextInt( );
if (num<10 | | num > 99)
System.out.println("nvalid input!");
System.exit(0);
numl = num;
digitl = num %10;
digit2 = num / 10;
dsum = digitl +digit2;
dprod =digitl *digit2;
gsum = dsum + dprod;
if (gsum == num)
System.out.println("Special 2-digit number");
else
36
O.3 The standard form of quadratic
equation is given by: ax? + bx + c = 0,
where d= b²4ac, is known as discriminant
of the roots of the equation that determines the nature
as:
Condition Nature
if d >= 0 Roots are real
if d<0 Roots are imaginary
Write a program to determine the nature and the roots of
a
equation, taking a, b, c as input. Ifd=b²-4ac is greater thanquadratic
to zero, then display "Roots are real", otherwise display or equal
"Roots are
imaginary". The roots are determined by the formula as:
-b+b² - 4ac -b-b-Aac
r1 = r2 =
2a 2a
Prog. import java.util.*;
publicclass QuadRoots
public static void nmain(String args[])
Scanner in = new Scanner(System.in);
int a, b, c;
double d, rl, r2;
System.out.print("Enter a: ");
a = in.nextInt( );
System.out.print("Enter b: ");
b= in.nextlnt( );
System.out.print("Enter c: ");
c= in.nextInt( );
d= Math.pow(b, 2) -(4*a*c);
if (d >=0)
System.out.println("Roots are real.");
rl =(b + Math.sqrt(d) / (2*a);
r2 = (-b- Math.sqrt(d)) / (2 *a); equation are:");
System.out.println("Roots of the
System.out.println('r1 ="+r1);
System.out.println("r2 ="+r2);
else
System.out.println("Roots are imaginary.");
Conditional Statements
in Java 37
Q.4 An air-conditioned bus charges fare for the passengers based on the
distance travelled as per the tariff are given below:
Distance Travelled Fare
Up to l0 km Fixed charge 80
11 km to 20 km Z6/km
21 km to 30 km Z5/km
31 km and above 74/km
Design a program to input distance travelled by the passenger. Calculate
and display the fare to be paid.
Prog. import java.util.*;
public class BusFare
}
Q.6 A bank announces new rates for Term Deposit Schemes for their
Customers and Senior Citizens as given below:
if (term <= 1)
interest =(sum * 8.0 * term)/100;
else if (term <= 2)
interest= (sum *9.0* term)/100;
else if (term <=3)
interest= (sum *10.0 *term)/100;
else
interest= (sum *11.0 *term) / 100;
amt = sum + interest;
System.outprintln(Sum Deposit"+"\e'+Term"'+"\t'+"Age"+"\e+ "hterest"+"\"+"Amount");
System.out.println(sum +"\t\e"+ term +"\t"+age+"\E"+interest+"\t\e"+amt);
if (wt <= 0)
charge =0;
else if (wt <= 100)
charge = 80;
else if (wt <= 500)
charge = 150;
else if (wt <= 1000)
charge = 210;
else
charge = 250;
if (ch = 'E')
if (wt <=0)
charge 0;
else if (wt <=100)
charge = 100;
else if (wt <= 500)
charge = 200;
else if (wt <= 1000)
charge = 250;
else
charge = 300;
Statements in Java 41