QB - 9th - Comp Lesson 5,6,7
QB - 9th - Comp Lesson 5,6,7
Ans.2) Syntax errors occur due to violation of rules and grammar of the language or due to typing
mistakes. These errors are shown during compile time and program does not execute until the errors
are removed. Logical errors occur due to wrong planning in program’s logic by programmer or due to
using wrong method to solve a problem or due to use of wrong operators. These errors are never shown
and program gets executed but shows wrong output.
Ans.2) Input can be given in a Java program in the following three ways. They are…
a) By using function arguments
b) By using InputStreamReader class and BufferedReader class
c) By using Scanner class
Ans.3) import keyword is used to include a package in a program. It activates a package and links it with
a program.
Ans.4) A package is a collection of various pre-defined classes. These classes contain various functions
which perform various tasks. In order to use these functions, we need to include the package in our
program. The keyword “import” helps to include a package in a program. Example of packages: java.util
and java.io
Ans.5) class A
{
public static void main(int a)
{
int b = a * a;
System.out.println(“Number = “ + a + “ and its square = “ + b);
}
}
Ans.6) A Diagnostic message indicates that something may be wrong with the program. It is shown by
the compiler. Diagnostical messages detect only syntax errors means there are pre-defined rules in Java
language or syntax of writing statements. If these rules or syntaxes are not followed then, the compiler
can detect it and report these errors during compile time. That’s why only syntax errors can be reported
through Diagnostical messages.
Ans.8) Run time errors occur due to wrong data given as input during run time or due to incorrect
mathematical operations. These errors are shown during the execution of the program. Example of run
time error is dividing a number by zero.
Ans.9) Syntax error, Logical error and Run time error are the three types of error that may occur during
execution of a program.
Ans.10)
a) Testing is a process in which a program is validated. Testing is complete when all desired
verifications against specifications have been performed. Debugging is a process in which the errors
are removed from the program. Debugging is finished when there are no errors and the program is
ready for execution.
b) Syntax errors occur due to violation of rules and grammar of the language or due to typing mistakes.
These errors are shown during compile time and program does not execute until the errors are
removed. Logical errors occur due to wrong planning in program’s logic by programmer or due to
using wrong method to solve a problem or due to use of wrong operators. These errors are never
shown and program gets executed but shows wrong output.
Ans.2)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double bs, da, hra, pf, gross, net;
System.out.println(“Enter the basic salary”);
bs = sc.nextDouble();
da = 30/100.0 * bs;
hra = 15/100.0 * bs;
pf = 12.5/100 * bs;
gross = bs + da + hra;
net = gross – pf;
System.out.println(“Gross Pay = “ + gross);
System.out.println(“ Net Pay = “ + net);
}
}
Ans.3)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double price, discount, gst, net1, net2;
System.out.println(“Enter the printed price of the digital camera”);
price = sc.nextDouble();
discount = 10/100.0 * price;
net1 = price – discount;
gst = 6/100.0 * net1;
net2 = net1 + gst;
System.out.println(“Net Price = “ + net2);
}
}
Ans.4)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double price, d1, d2, d3;
System.out.println(“Enter the price of the article”);
price = sc.nextDouble();
d1 = 30/100.0 * price;
d2 = 20/100.0 * price;
d3 = 10/100.0 * price;
System.out.println(“Discount of first shopkeeper = “ + d1);
System.out.println(“Discount of second shopkeeper = “ + d2 + “ and “ + d3);
}
}
Ans.5)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double P,T=1,R=5,CI,CII,CIII;
System.out.println("Enter Principal");
P = sc.nextDouble();
CI = (P * Math.pow((1 + R/100),T)) - P;
P = P + CI;
CII = (P * Math.pow((1 + R/100),T)) - P;
P = P + CII;
CIII = (P * Math.pow((1 + R/100),T)) - P;
System.out.println(“First Year Interest = “ + CI);
System.out.println(“Second Year Interest = “ + CII);
System.out.println(“Third Year Interest = “ + CIII);
}
}
Ans.7)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int time, hr, min, sec;
System.out.println("Enter time in seconds");
time = sc.nextInt();
hr = time / 3600;
min = (time % 3600) / 60;
sec = (time % 3600) % 60;
System.out.println(“Total time in seconds = “ + time);
System.out.println(“It is = “ + hr +” Hour “ + min + “ Minutes “ + sec + “ Seconds”);
}
}
Ans.8)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a, b;
System.out.println(“Enter two numbers”);
a = sc.nextInt();
b = sc.nextInt();
a = a +b;
b = a – b;
a = a – b;
System.out.println(“a = “ + a + “ b = “ + b);
}
}
Ans.9)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double P, T=3, R=10, CI, SI, diff;
System.out.println("Enter Principal");
P = sc.nextDouble();
CI = (P * Math.pow((1 + R/100),T)) – P;
SI = P * R * T / 100;
diff = Math.abs(SI – CI);
System.out.println(“Simple Interest = “ + SI);
System.out.println(“Compound Interest = “ + CI);
System.out.println(“Difference = “ + diff);
}
}
Ans.10)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double SP1, SP2, CP1, CP2;
System.out.println("Enter the sale price of first calculator");
SP1 = sc.nextDouble();
System.out.println("Enter the sale price of second calculator");
SP2 = sc.nextDouble();
CP1 = SP1 / (1 + 20/100.0);
CP2 = SP2 / (1 – 20/100.0);
System.out.println(“Cost price of two calculators = “ + (CP1 + CP2));
}
}
Ans.4)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double a, b, c, d;
System.out.println(“Enter Physics, Chemistry and Biology marks”);
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = (a + b + c)/3;
System.out.println(“Average mark = “ + d);
System.out.println(“Round off Physics mark = “ + a);
System.out.println(“Round off Chemistry mark = “ + b);
System.out.println(“Round off Biology mark = “ + c);
}
}
Ans.5)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double area, rad;
System.out.println(“Enter area of the circle”);
area = sc.nextDouble();
rad = Math.sqrt((7*area)/22 );
System.out.println(“Radius of the Circle = “ + rad);
}
}
]
else
{
Statement3
}
(b) if else statement is used to check a condition and to execute some statements when the condition is
true and to execute some other statements when the condition is false.
if(condition)
Statement1
else
Statement2
(c) if else if statement is used when more than two actions are to be taken. It checks one condition and it
is true then one action is taken otherwise another condition is checked and if that is true then second
action is taken otherwise next conditions are checked one by one and depending on the result of those
conditions, any one action is taken.
if(condition1)
Action1
else if(condition2)
Action2
else if(condition3)
Action3
------------
------------
else
Last Action
Ans.4) if else is a bi-directional branching statement whereas switch case is a multiple branching
statement. Operators are used to check condition in if else whereas no operator is used to check
condition in switch case. float and double data type constants/variables can be checked in if else but not
in switch case.
Ans.5) A switch case is a multiple branching statement. It is used instead of writing multiple if else if
statements. It is used in menu driven programming.
Ans.6) A default statement is just like the else of if statement. The default statement executes when
none of the case value is matched with the switch variable. Use of default is optional in switch block.
Ans.7) In switch case, when any case value is matched with the switch variable, then statements in that
case block are executed and all other subsequent case blocks also get executed. This is called fall
through. To prevent fall through to happen, break keyword is used. The break keyword is a jump
statement. It terminates the switch block and takes the control out of it.
Ans.8) In switch case, when any case value is matched with the switch variable, then statements in that
case block are executed and all other subsequent case blocks also get executed. This is called fall
through.
Ans.9) A statement that contains one or more statements within its block is called a compound
statement. In the following example, if statement is a compound statement.
if(condition)
{
Statement1;
Statement2;
}
Ans.10) The if else if statement is used when more than two actions are to be taken. It checks one
condition and it is true then one action is taken otherwise another condition is checked and if that is
true then second action is taken otherwise next conditions are checked one by one and depending on
the result of those conditions, any one action is taken.
if(condition1)
Action1
else if(condition2)
Action2
else if(condition3)
Action3
------------
------------
else
Last Action
Ans.11) System.exit(0) will terminate the execution of a program in the middle of its execution. The
remaining statements in the program after System.exit(0) statement will not be executed.
Ans.12) if else is a bi-directional branching statement whereas switch case is a multiple branching
statement. Operators are used to check condition in if else whereas no operator is used to check
condition in switch case. float and double data type constants/variables can be checked in if else but not
in switch case.
Ans.3) 5.5
Ans.4)
(i) Object Oriented
Robust and Secure
(ii) Wrong Input
(iii) Platform Independent
Ans.3) c = a*a + b * b;
d = (a +b) * (a + b);
int p = c / d;
Ans.4) k = Math.pow(p,2);
r = Math.sqrt(n);
Q.5) Unsolved Java programs.
Ans.1)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a, b, c;
System.out.println(“Enter three angles”);
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if( a+b+c == 180 && a > 0 && b > 0 && c > 0)
{
if(a<90 && b<90 && c<90)
System.out.println(“Acute angled triangle”);
else if(a==90 || b==90 || c==90)
System.out.println(“Right angled triangle”);
else
System.out.println(“Obtuse angled triangle”);
}
else
System.out.println(“A Triangle construction is not possible”);
}
}
Ans.2)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double cp, sp;
System.out.println(“Enter the cost price and sale price”);
cp = sc.nextDouble();
sp = sc.nextDouble();
if( sp > cp)
{
System.out.println(“Profit = “ + (sp – cp));
System.out.println(“Profit % = “ + (sp – cp)/cp * 100);
}
else if(cp > sp)
{
System.out.println(“Loss = “ + (cp – sp));
System.out.println(“Loss % = “ + (cp – sp)/cp * 100);
}
else
System.out.println(“Neither profit nor loss”);
}
}
Ans.3)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a, b, c;
System.out.println(“Enter three numbers”);
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if( a != b && a != c && b != c)
{
System.out.println(“Greatest Number = ” + Math.max(a, Math.max(b,c)));
}
else
System.out.println(“Numbers are equal”);
}
}
Ans.4)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a;
System.out.println(“Enter a number”);
a = sc.nextInt();
if( a % 3 = = 0 && a % 5 == 0))
System.out.println(“The number is divisible by 3 as well as by 5”);
else if(a % 3 == 0)
System.out.println(“The number is divisible by 3 and not by 5”);
else if(a % 5 == 0)
System.out.println(“The number is divisible by 5 and not by 3”);
else
System.out.println(“The number is neither divisible by 3 nor by 5”);
}
}
Ans.6)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int yr;
System.out.println(“Enter a year”);
yr = sc.nextInt();
if( yr % 100 == 0)
if ( yr % 400 == 0)
System.out.println(“It is both century year and leap year”);
else
System.out.println(“It is a century year”);
else if( yr % 4 == 0)
System.out.println(“It is a leap year”);
else
System.out.println(“It is a normal year”);
}
}
Ans.6)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a, b, c, d;
System.out.println(“Enter two unequal numbers”);
a = sc.nextInt();
b = sc.nextInt();
if ( a == b)
System.out.println(“Numbers are equal. Please enter two unequal numbers.”);
else if( a < 0 || b < 0)
System.out.println(“Square root of negative numbers cannot be determined”);
else
{
c = (int)Math.sqrt(a);
d = (int)Math.sqrt(b);
if(a == c *c && b == d *d)
System.out.println(“Both are perfect square numbers”);
else if(a==c*c)
System.out.println(a+“ is a perfect square number”);
else if(b==d*d)
System.out.println(b+“ is a perfect square number”);
else
System.out.println(“None of these are perfect square numbers”);
}
}
}
Ans.7)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a, b, c, d, hi, low, mid;
System.out.println(“Enter three unequal numbers”);
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if ( a != b && b != c && c != a )
{
hi = Math.max(a, Math.max(b, c));
low = Math.min(a, Math.min(b, c));
mid = (a + b + c) – (hi + low);
System.out.println(“Second smallest number = “ + mid);
}
else
{
System.out.println(“Numbers are not unequal”);
}
}
}
Ans.8)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a, b, c, d, hi, low;
System.out.println(“Enter three unequal numbers”);
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if ( a !=b && b !=c && c != a )
{
hi = Math.max(a, Math.max(b, c));
low = Math.min(a, Math.min(b, c));
System.out.println(“greatest = “ + hi + “ smallest = “ + low);
}
else
{
System.out.println(“Numbers are not unequal”);
}
}
}
Ans.9)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double km, amt=0;
System.out.println(“Enter the distance covered in kilometer”);
km = sc.nexDouble();
if( km < = 5)
amt = 100;
else if( km <= 15)
amt = 100 + (km – 5) * 10;
else if( km <= 25)
amt = 100 + 100 + (km – 15) * 8;
else
amt = 100 + 100 + 80 + (km – 25) * 5;
System.out.println(“ Taxi No. 9211”);
System.out.println(“Distance covered = “ + km);
System.out.println(“Amount = “ + amt);
}
}
Ans.10)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double cost, dis = 0, net =0;
String gift;
System.out.println(“Enter the total cost of purchase”);
cost = sc.nextDouble();
if( cost < = 2000)
{
dis = 5/100.0 * cost;
gift = “Calculator”;
}
else if( cost <= 5000)
(
dis = 10/100.0 * cost;
gift = “School Bag”;
}
else if( cost < = 10000)
{
dis = 15/100.0 * cost;
gift = “Wall Clock”;
}
else
{
dis = 20/100.0 * cost;
gift = “Wrist Watch”;
}
net = cost – dis;
System.out.println(“ Net amount to be paid = ” + net);
System.out.println(“Gift = “ +gift);
}
}
Ans.11)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int ag; String nm;
double ti, tax=0;
System.out.println(“Enter name, age and taxable income”);
nm = sc.nextLine();
ag = sc.nextInt();
ti = sc.nexDouble();
if( ti < =250000)
tax = 0;
else if( ti <= 500000)
tax = (ti – 250000) * 0.10;
else if( ti <= 1000000)
tax = (ti – 500000) * 0.20 + 25000;
else
tax = (ti – 1000000) * 0.30 + 125000;
if(ag <= 60)
{
System.out.println(“Name = “ + nm);
System.out.println(“Tax Payable = “ + tax);
}
else
System.out.println(“Wrong category”);
}
}
Ans.12)
import java.util.*;
class A
{
public static void main(String args[])
{
System.out.println("Enter sum to be deposited and number of days to be deposited");
Scanner sc=new Scanner(System.in);
double amt=0;
double sum = sc.nextDouble();
int day = sc.nextInt();
if(day <= 180)
amt = sum * Math.pow(1+5.5/100,0.5);
else if( day <= 364)
amt = sum * Math.pow(1+7.5/100,1);
else if( day == 365)
amt = sum * Math.pow(1+9.0/100,1);
else
amt = sum * Math.pow(1+8.5/100,day/365);
System.out.println("Maturity Amount = "+amt);
}
}
Ans.13)
import java.util.*;
class A
{
public static void main(String args[])
{
System.out.println("Enter the name of policy holder”);
Scanner sc=new Scanner(System.in);
String nm=sc.nextLine();
System.out.println(“Enter the sum assured”);
double sum = sc.nextDouble();
System.out.println(“Enter the first premium amount”);
double prem = sc.nextDouble();
double dis=0, comm=0;
if(sum <= 100000)
{
dis = 0.05 * prem;
comm = 0.02 * sum;
}
else if(sum <= 200000)
{
dis = 0.08 * prem;
comm = 0.03 * sum;
}
else if(sum <= 500000)
{
dis = 0.10 * prem;
comm = 0.05 * sum;
}
else
{
dis = 0.15 * prem;
comm = 0.75 * sum;
}
System.out.println("Name of the policy holder = "+nm);
System.out.println("Sum assured = "+sum);
System.out.println("Premium = "+prem);
System.out.println("Discount on first premium = "+dis);
System.out.println("Commission = "+comm);
}
}
Ans.14)
import java.util.*;
class A
{
public static void main(String args[])
{
System.out.println("Enter the name of the employee”);
Scanner sc=new Scanner(System.in);
String nm=sc.nextLine();
System.out.println(“Enter the basic salary”);
double bs = sc.nextDouble();
double da=0, sa=0, gross=0;
if(bs <= 100000)
{
da = 0.10 * bs;
sa = 0.05 * bs;
}
else if(bs <= 200000)
{
da = 0.12 * bs;
sa = 0.08 * bs;
}
else if(bs <= 300000)
{
da = 0.15 * bs;
sa = 0.10 * bs;
}
else
{
da = 0.20 * bs;
sa = 0.12 * bs;
}
gross = bs + da + sa;
System.out.println("Name \t Basic \t DA \t Spl. Allowance \t Gross Salary);
System.out.println(nm + “\t” + bs + “\t” + da + “\t” + sa + “\t” + gross);
}
}
Ans.15)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double c, f;
System.out.println(“Enter 1 to convert Fahrenheit to Celsius”);
System.out.println(“Enter 2 to convert Celsius to Fahrenheit”);
int ch=sc.nexInt();
switch(ch)
{
case 1:
System.out.println(“Enter Temperature in Fahrenheit”);
f=sc.nextDouble();
c=(f – 32) * 5/9.0;
System.out.println(“Temperature in Celsius = ” + c);
break;
case 2:
System.out.println(“Enter Temperature in Celsius”);
c=sc.nextDouble();
f=c * 9/5.0 + 32;
System.out.println(“Temperature in Fahrenheit = ” + f);
break;
default:
System.out.println(“Wrong choice”);
}
}
}
Ans.16)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double l, b, h, r, v;
System.out.println(“Enter 1 for volume of cuboid”);
System.out.println(“Enter 2 for volume of cylinder”);
System.out.println(“Enter 3 for volume of cone”);
int ch=sc.nexInt();
switch(ch)
{
case 1:
System.out.println(“Enter length, breadth and height of cuboid”);
l=sc.nextDouble();
b=sc.nextDouble();
h=sc.nextDouble();
v=l*b*h;
System.out.println(“Volume of cuboid = ” + v);
break;
case 2:
System.out.println(“Enter radius and height of cylinder”);
r=sc.nextDouble();
h=sc.nextDouble();
v=22/7.0 * Math.pow(r,2) * h;
System.out.println(“Volume of cylinder = ” + v);
break;
case 3:
System.out.println(“Enter radius and height of cone”);
r=sc.nextDouble();
h=sc.nextDouble();
v=1/3.0 * h * 22/7.0 * Math.pow(r,2);
System.out.println(“Volume of cone = ” + v);
break;
default:
System.out.println(“Wrong choice”);
}
}
}
Ans.18)
import Java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double r1, r2, R1, R2;
System.out.println(“Enter two resistances”);
r1=sc.nextDouble();
r2=sc.nextDouble();
R1=r1+r2;
R2=(r1*r2)/(r1+r2);
System.out.println(“The equivalent resistance of series = “ + R1);
System.out.println(“The equivalent resistance of parallel = “ + R2);
}
}
Ans.19)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double P, T, R, SI=0, CI=0;
System.out.println(“Enter Principal, Time and Rate”);
P=sc.nextDouble();
T=sc.nextDouble();
R=sc.nextDouble();
System.out.println(“Enter ‘S’ for Simple Interest and ‘C’ for Compound Interest”);
char ch=sc.nextt().charAt(0);
switch(ch)
{
case ‘S’: System.out.println(“Simple Interest = “ + (P*T*R)/100);
break;
case ‘C’:
System.out.println(“Compound Interest = “ + (P*(Math.pow(1+R/100,T) – P) );
break;
default: System.out.println(“Wrong Choice”);
}
}
}
Ans.20)
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double dis=0, net=0;
System.out.println(“Enter Name of the Customer”);
String nm=sc.nextLine();
System.out.println(“Enter the Amount of Purchase”)
double pa=sc.nextDouble();
System.out.println(“Enter ‘L’ for Laptop and ‘D’ for Desktop Computer”);
char pur_type=sc.nextt().charAt(0);
if(pur_type==’L’)
{
if(pa<=25000)
dis=0;
else if(pa<=50000)
dis=0.05*pa;
else if(pa<=100000)
dis=0.75*pa;
else
dis=0.10*pa;
net=pa – dis;
System.out.println(“Name = “ + nm);
System.out.println(“Net Amount = “ + net);
}
else if(pur_type = =’D’)
{
if(pa<=25000)
dis=0.05*pa;
else if(pa<=50000)
dis=0.75*pa;
else if(pa<=100000)
dis=0.10*pa;
else
dis=0.15*pa;
System.out.println(“Name = “ + nm);
System.out.println(“Net Amount = “ + net);
}
else
System.out.println(“Wrong Choice”);
}
}