Get ICSE Revision Tour II solutions for class 10. It will help you to make good preparation before attending the ICSE Board exam. We have provided you with the Revision Tour II solutions class 10 to score good marks in your exam.
In ICSE Class 10, Revision Tour II solutions is compulsory to score good marks in computer application. Students Who are planning to score higher marks in class 10 should practice ICSE Revision Tour II solutions class 10.
ICSE Revision Tour II Solutions Class 10 Computer Applications
A. Tick (✓) the correct option.
Question. Which among the following is not a valid error in Java?
a. Syntax errors
b. Logical errors
c. Run-time errors
d. Technical errors
Answer
D
Question. Which among the following Scanner methods allows us to input a number with a decimal point?
a. nextInt()
b. nextFloat()
c. nextDecimal()
d. nextPoint()
Answer
B
Question. The output in BlueJ occurs in which window?
a. Console window
b. Terminal window
c. Both a and b
d. None of these
Answer
B
Question. The input in BlueJ occurs in which window?
a. Console window
b. Terminal window
c. Both a and b
d. None of these
Answer
B
Question. Assigning value to a variable during declaration is called.
a. Declaration
b. Assignment
c. Initialisation
d. None of these
Answer
C
Question. Which among the following is used to represent single-line comment?
a. //
b. /*
c. \\
d. <!—
Answer
A
Question. Which among the following is a logical error?
a. Missing semicolon
b. Mismatched braces in classes and methods.
c. Misspelled keywords and identifiers.
d. Addition is required but subtraction is performed.
Answer
D
Question. Which among the following represents a syntax error?
a. Dividing an integer by zero.
b. Accessing an element that is out of bounds of an array.
c. Trying to store a value which is incompatible to a certain data-type.
d. Missing semicolon
Answer
D
Question. If the data that is to take part in a calculation in a method is fixed, which form of input is necessary?
a. Initialisation
b. Parameterised input
c. Scanner input
d. None of these
Answer
A
Question. In case you need to give a proper message (prompt string) before taking an input from the user, which would be the most preferable method?
a. Parameterised input
b. Initialisation
c. Scanner input
d. None of these
Answer
C
Fill in the blanks.
Question. The ______________break statement is used to avoid fall through.
Question. The ______________next().chart(0) function of the Scanner class is used to accept a character from the user.
Question. A loop within another loop is called nested loops.
Question. The ______________relational/comparison operator is used to compare two quantities.
Question. The else block is preceded by the ______________if block.
Question. The ______________sqrt() function of the Math class is used to return the square root of a number.
Question. The return type of cbrt( ) function is ______________double.
Question. The ______________continue jump statement is used to skip the remaining statements in a loop.
Question. Single line comment and ______________multiline comments are two types of comments in Java.
Question. Any error in the grammar of the language is a ______________syntax error.
State whether the following statements are True (T) or False (F).
Question. Scanner class is present in the java.lang package. F
Question. Math.abs( ) is used to find the absolute value of a number. T
Question. The return type of Math.sqrt( ) function is float. F
Question. The fraction 1/2 will evaluate to 0.5. F
Question. The continue statement in a switch block is used to avoid fall through. F
Question. The default statement is essential in a switch block. F
Question. The for loop is an entry controlled loop. T
Question. The while loop is an exit controlled loop. F
Question. The do-while loop is generally used when the number of iterations is known. F
Question. You can have only the for loop as the nested loops. F
Question. Name the functions of the Scanner that is used to:
(i) Accept a number of long data type
(ii) Accept a group of alphabets.
Answer. (i) nextLong() (ii) nextLine()
Question. What is a bug? What is debugging?
Answer. An error in a program is called a bug and the process of removing it is called debugging.
Question. What are the different types of errors that may occur in a Java program?
Answer. Syntax Error, Logical Error and Run-time Error.
Question. What are syntax errors? Give two examples.
Answer. A syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular program.
Examples:
• Mismatched braces in classes and methods
• Wrongly accessing variable, whose visibility doesn’t exist
• Misspelled keywords and identifiers
Question. What are run-time errors? Give two examples.
Answer. An error that occurs during the execution of a program is called run time error. Example:
• Dividing an integer by zero.
• Accessing an element that is out of bounds of an array.
• Trying to store a value which is incompatible to a certain datatype.
Question. What are comments? Name the different types of comments used in Java.
Answer. A comment is a programmer-readable explanation or annotation in the source code of a computer program. Types: Single-line comment and Multiline comment
Question. What are conditional statements? With which commands do you implement conditional statement in Java?
Answer. Conditional construct are specific statements that allow us to check a condition and execute certain parts of code depending on whether the condition is true or false. Implementation is done using: if, if-else, if-else if-else, switch-case and ternary operators.
Question. When are braces optional or necessary in conditional statements?
Answer. Giving braces is optional for if/else in case there is only one statement to be executed.
Question. What is the difference between the Scanner class functions next() and nextLine()?
Answer. next() can read the input only till the space. It can’t read two words separated by space. Also, next() places the cursor in the same line after reading the input. nextLine() reads input including space between the words (that is, it reads till the end of line \n).
Question. What are relational operators?
Answer. The relational operator allows you to test or define some kind of relation between two entities.
Question. State the difference between = and = =.
Answer. In Java, “=” is the assignment operator and “==” is a comparison operator.
Question. What are logical operators? Explain each of them with the help of an example.
Answer. The logical operators are used to combine simple relational statements into more complex expressions.
Question. State the difference between while and do while loop.
Answer. The while loop is an entry controlled loop, whereas the do-while loop is an exit controlled loop.
Question. State the difference between entry controlled loop and exit controlled loop.
Answer. Difference between entry controlled and exit controlled loop
Entry Control Loop | Exit Control Loop |
The test expression or condition is checked before entering the loop. | The test expression or condition is checked after entering the loop. |
It the test expression evaluates to false, the loop doesn’t get executed. | Even if the test expression evaluates to false, the loop gets executed atleast once. |
Question. Give the general syntax of a while-loop. How do you create infinite loops using a while-loop structure?
Answer. The syntax of a while loop is,
while (condition or test-expression)
{
bodyoftheloop;
}
One of the method of creating an infinite loop is to write ‘true’ in the test-expression of the loop.
while(true)
System.out.println(“Infinite”);
Question. Give the general syntax of a do-while loop. How do you create infinite loops using do-while loop structure?
Answer. The syntax of the dowhile loop is:
do
{
statement;
}while (condition);
Infinite loop using do-while loop:
do
{
System.out.println(“Infinite”);
}while(true);
Question. Compare loops according to its usage.
Answer. Generally the for-loop is used when the number of iterations is known. The while loop is generally used when the number of iterations is not known. The do-while loop is generally used when it is necessary to execute the loop at least once.
Question. What are Nested-loops? Give an example of a nested for-loop to show its functionality.
Answer. A loop within a loop is called a nested loop.
Example of a nested loop is:
public class Loops
{
static void Nested()
{
int x , y;
for(x=1;x<=3;x++)
{
for(y=1; y<=3 ; y++)
{
System.out.println(x+” “+y);
}
}
}
}
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Here x starts from 1 and then enters the inner loop where y too, starts from 1. That is why we get 1 and 1 as the first result. As told earlier the inner loop should finish its iteration first. Therefore y continues iteration till it reaches 3, in the meanwhile x continues to be at 1. That is why the next two outputs are 1 and 2 & 1 and 3. After it comes out of the inner loop the value of x increments by 1. Thus x is 2 and y starts back from 1 again and it reaches 3 while x continues to be 2. Hence the next result is 2 & 1, 2 & 2 and 2 & 3. Then x reaches 3 and y starts from 1, hence we get the result 3 & 1, 3 & 2 and 3 & 3. Since x is exhausted i.e. it has already iterated for 3 times, the control comes out of it and the program ends.
Question. Name two jump statements and their use.
Answer. Jump statements in Java:
break- The break statement causes an immediate exit from the do-while, for, switch or while statement in which it appears.
continue- The continue statement is used with the loop instructions do-while, for, and while. It transfers control to the place in the program where the next iteration of the loop in which it appears, begins.
Question. What is the difference between break and continue statements when they occur in a loop?
Answer. The break statement is used to exit from a loop. The continue statement is used skip the remaining statements in a loop and continue with the next iteration.
Question. State one similarity and one difference between while and for loop.
Answer. Similarity: Both while and for loop is used as entry controlled loop. Difference: The for loop may have initialisation, test expression and updation, written before the loop. The while loop have the initialisation always before the loop and updation in the body of the loop.
Question. Analyze the following program segment and determine how many times the body of loop will be executed (show the working).
Answer. The loop executes as long as the given condition is true.
Question. What is an exception?
Answer. An exception is a run-time error that occurs in a program.
Question. Give the output and determine how many times the loop will execute:
x=1; y=1;
while(x<=y)
{
y = y/x;
System.out.println(y);
}
Answer. x=1; y=1;
x<=y -> the loop executes once.
y=y/x =1/1=1
x<=y -> the loop executes next time
The entire process will continue infinite number of times, with the output as 1 in different lines.
Question. Give the output of the following code fragment:
when
(i) opn = ‘b’
(ii) opn = ‘x’
(iii) opn = ‘a’
switch (opn)
{
case ‘a’:
System.out.println(“Platform Independent”);
break;
case ‘b’:
System.out.println(“Object Oreinted”);
case ‘c’:
System.out.println(“Robust and Secure”);
default:
System.out.println(“Wrong Input”);
}
Answer. i. Object Oreinted
Robust and Secure
Wrong Input
ii. Wrong Input
iii. Platform Independent
Question. Convert the following if else if construct into switch case
if( var==1)
System.out.println(“good”);
else if(var==2)
System.out.println(“better”);
else if(var==3)
System.out.println(“best”);
else
System.out.println(“invalid”);
Answer. switch(var)
{
case 1:
System.out.println(“good”);
break;
case 2:
System.out.println(“better”);
break;
case 3:
System.out.println(“best”);
break;
default:
System.out.println(“invalid”);
}
Question. Rewrite the following using ternary operator:
if (bill >10000 )
discount = bill * 10.0/100;
else
discount = bill * 5.0/100;
Answer. discount=(bill >10000 ) ? bill * 10.0/100: bill * 5.0/100;
Question. Rewrite the following program segment using the if .. else statement.
comm = (sale>15000)?sale*5/100:0;
Answer. if (sale>15000)
comm = sale*5/100
else
comm=0;
Question. Rewrite the following using ternary operator :
if(x%2==0)
System.out.print(“EVEN”);
else
System.out.print(“ODD”);
Answer. System.out.print((x%2==0)?”EVEN”:“ODD”);
Question. Convert the following segment into an equivalent do loop.
int x,c;
for (x=10, c=20; c>=10; c = c – 2)
x++;
Answer. int x=10,c=20;
do
{
x++;
c=c-2;
}while(c>=10);
Question. Give the output of the following program segment and also mention how many times the loop is executed:
int i;
for (i = 5 : i > 10; i ++)
System.out.println(i);
System.out.println(i * 4);
Answer. Output: 20
The loop do not execute, as the condition results to false.
Question. Convert the following while loop to the corresponding for loop:
int m = 5, n = 10;
while (n>=1)
{
System.out.println(m*n);
n–;
}
Answer. int m=5,n;
for(n=10;n>=1;n–)
{
System.out.println(m*n);
}
Question. Convert following do-while loop into for loop.
int i = 1;
int d=5;
do {
d=d*2;
System.out.println(d;)
i++ ; } while (i<=5);
Answer. int I,d=5;
for(i=1;i<=5;i++)
{
d=d*2;
System.out.println(d);
}
Question. What will be the output of the following code?
int m=2;
int n=15;
for(int i = 1; i<5; i++);
m++; – –n;
System.out.println(“m=” +m);
System.out.println(“n=” +n);
Answer. m=3
n=14
Question. Analyze the given program segment and answer the following questions:
for(int i=3;i<=4;++)
{ for(int j=2;j<i;j++)
{
System.out.print(“ ”);
}
System.out.printlin(“WIN”);
}
i. How many times does the inner loop execute ?
ii. Write the output of the program segment.
Answer. i. The inner loop executes 3 times.
ii. Output:
WIN
WIN
Question. Analyse the following program segment and determine how many times the loop will be executed and what will be the output of the program segment.
int k=1, i=2;
while (++i<6)
k*=i;
System.out.println(k);
Answer. The loop executes 3 times.
Output: 60
Question. How many times will the following loop execute? What value will be returned?
int x = 2, y = 50;
do
{
++x;
y- = x++;
}
while(x <= 10);
return y;
Answer. The loop will execute 5 times.
Value returned is 15.
Question. Analyse the following program segment and determine how many times the loop will be executed and what will be the output of the program segment?
int p=200;
while(true)
{
if(p<100)
break;
p=p-20;
}
System.out.println(p);
Answer. The loop executes 7 times.
Output: 80
Question. Name the types of error (syntax, runtime or logical error) in each case given below:
i. Division by a variable that contains a value of zero.
ii. Multiplication operator used when the operation should be division.
iii. Missing semicolon.
Answer.
i. Runtime error
ii. Logical error
iii. Syntax error
Question. Give the output of the following program segment:
double x = 2.9, y = 2.5;
system.out.printIn(math.min(math.floor(x), y));
system.out.printIn(math.min(math.cell(x), y));
Answer. Output:
2.0
3.0
Question. What is the final value of ctr after the iteration process given below, executes?
int ctr = 0;
for (int i = 1 ; i < = 5 ; i++)
for (int j = 1 ; j < = 5 ; j+=2)
++ctr;
Answer. Final value of ctr is 15.
Question. What are the final values stored in variables x and y below?
double a = – 6.35;
double b = 14.74;
double x = Math.abs(Math.ceil(a));
double y = Math.rint(Math.max(a,b));
Answer. x=6.0 and y=15.0
Question. What are the values stored in variables r1 and r2:
(i) double r1 = Math.abs(Math.min(-2.83,-5.83));
(ii) double r2 = Math.sqrt(Math.floor(16.3));
Answer. r1=5.83 and r2=4.0
Question. Rewrite the following program segment using if-else statements instead of the ternary operator.
String grade=(mark>=90) ? “A” : (mark>=80) ? “B” : “C”;
Answer. String grade;
If(marks>=90)
grade=“A”;
else
grade=“B”;
Question. Study the method and answer the given questions.
public void sampleMethod()
{ for(int i=0;i<3;i++)
{ for(int j=0;j<2;j++)
{
int number = (int) (Math.random( ) * 10);
System.out.println(number);
}
}
}
(i) How many times does the loop execute?
(ii) What is the range of possible values stored in the variable number?
Answer. (i) The loops executes for 6 times.
(ii) The possible range of values stored in ‘number’ is 0 to 9.
Question. What are the values of a and b after the following function is executed, if the values passed are 30 and 50:
void paws(int a, int b)
{
a = a + b;
b = a – b;
a = a – b;
}
Answer. a=50 and b=30
Question. Write the output of the following code segment:
char ch; int x = 97;
do
{
ch = (char)x;
System.out.print(ch+“ ”);
if(x%10==0)
break;
++x;
}
while(x<=100);
Answer. Output:
a b c d
Question. Analyze the given program segment and answer the following questions:
(i) Write the output of the program segment.
(ii) How many times does the body of the loop gets executed?
for(int m=5; m<=20; m+=5)
{
if(m%3 == 0)
break;
else
if(m%5 == 0)
System.out.println(m);
continue;
}
Answer. i. Output:
5
10
ii. The loop executes 3 times.
Question. Give the output of the following program segment and also mention the number of times the loop is executed:
Answer. Output:
12
The loop executes 2 times.
Question. Give the output of the following:
a) Math.floor (-4.7) b) Math.ceil(3.4) + Math.pow(2, 3) c) Math.floor(-126.349) d) Math.max(45.6,17.3) e) Math.min(-0.0,0.0) f) Math.pow(4,3) g) Math.sqrt(625) h) Math.cbrt(125) i) Math.max(11,11) j) Math.ceil(-12.56) k) Math.floor(15.36) l) Math.round(146.5) m) Math.max(-17, -19) n) Math.ceil(7.8) o) Math.ceil(4.2) p) Math.abs(-4)
Answer. a) -5.0 b) 12.0 c) -127.0 d) 45.6 e) -0.0 f) 64.0 g) 25.0 h) 5.0 i) 11 j) -12.0 k) 15.0 l) 147 m) -17 n) 8.0 o) 5.0 p) 4
Question. Write equivalent Java expressions for the following:
Answer. a. Math.sqrt(a+b)
b. 1/3.0+Math.pow(a,3)+1/3.0Math.pow(b,3)
c. s=ut+1/2.0aMath.pow(t,2)
d. d=Math.sqrt(ll+bb);
e. (-b+Math.sqrt(bb-4ac))/(2a)
f. (Math.pow(a,3)+Math.pow(b,3))/ (Math.pow(a,3)-Math.pow(b,3))
g. Math.abs((a-b)/(a+b))
h. Math.cbrt((a+b)/(ab))
i. Math.pow(a+b.n)/Math.sqrt(3+b)
j. z=(5xxx+2y)/(x+y);
k. Math.sqrt(2as+uu)
l. Math.sqrt(3x+xx)/(a+b)
m. (aa+bb)/(2ab)
n. T=Math.sqrt(AA+BB+CC); o. aMath.pow(x,5)+b*Math.pow(x,3)+c
Question. Write a program to input the area of a square and find its perimeter.
Answer. {
static void main()
{
Scanner sc=new Scanner(System.in);
double a,s,p;
System.out.println(“Enter the area of a square:”);
a=sc.nextDouble();
s=Math.sqrt(a);
p=4*s;
System.out.println(“Perimeter=”+p);
}
}
Question. Write a program to input the length and breadth of a rectangle and find its diagonal.
Answer. {
static void main()
{
Scanner sc
System.out.println(“Enter the length and breadth of the rectangle:”);
l=sc.nextDouble();
b=sc.nextDouble();
d=Math.sqrt(l*l+b*b);
System.out.println(“Diagonal=”+d);
}
}
Question. Write a program to input 2 integers and check whether both the numbers are multiples of 7 or not.
Answer. {
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b;
System.out.println(“Enter 2 integers:”);
a=sc.nextInt();
b=sc.nextInt();
if(a%7==0 && b%7==0)
System.out.println(“Both are multiples of 7”);
else
System.out.println(“Both are not multiples of 7”);
}
}
Question. Write a program to pass 2 integer numbers as parameters. If either of the two numbers is 0, display invalid entry and the program should end, if it is valid entry, divide the larger number with the smaller number and display the result.
Answer. {
static void divide(int a,int b)
{
if(a==0 || b==0)
System.out.println(“Invalid Entry”);
else
{
float q;
if(a>b)
q=(float)a/b;
else
q=(float)b/a;
System.out.println(“Quotient:”+q);
}
}
}
Question. Write a program to input 3 unique integers and print the smallest among them.
Answer. {
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println(“Enter 3 integers:”);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a<b && a<c)
System.out.println(“Smallest:”+a);
else if(b<a && b<c)
System.out.println(“Smallest:”+b);
else
System.out.println(“Smallest:”+c);
}
}
Question. Write a program to input the three angles of a triangle and check whether it forms a triangle or not, if it forms a triangle, check whether it is an equilateral, isosceles or a scalene triangle.
(Hint: To form a triangle, the sum of the angles should be 180 degrees.
To form an equilateral triangle every angle should be equal.
To form an isosceles triangle any two angles should be equal.
To form a scalene triangle all three angles should be different from each other.)
Answer. {
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println(“Enter 3 angles:”);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a+b+c==180)
{
if(a<90 && b<90 && c<90)
System.out.println(“Acute angled triangle”);
else if(a>90 || b>90 || c>90)
System.out.println(“Obtuse angled triangle”);
else
System.out.println(“Right angled triangle”);
}
else
System.out.println(“Cannot form a triangle”);
}
}
Question. Write a program to input the three sides of a triangle and check whether it forms a triangle or not, if it forms a triangle, check whether it is an equilateral, isosceles or a scalene triangle.
(Hint: To form a triangle, each side should be less the sum of the other two sides.
To form an equilateral triangle every side should be equal.
To form an isosceles triangle any two sides should be equal.
To form a scalene triangle all three sides should be different from each other.)
Answer. {
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println(“Enter 3 sides:”);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a<b+c && b<a+c && c<a+b)
{
if(a==b && b==c)
System.out.println(“Equilateral triangle”);
else if(a==b || b==c || c==a)
System.out.println(“Isosceles triangle”);
else
System.out.println(“Scalene triangle”);
}
else
System.out.println(“Cannot form a triangle”);
}
Question. Write a program to accept three sides of a triangle as parameter and check whether it can form a triangle or not. If it forms a triangle, check whether it is an acute angled, obtuse angled or right-angled triangle.
(Hint: To form a triangle, each side should be less the sum of the other two sides..
To form an acute angled triangle the square of every side should be less than the sum of the squares of the other two sides.
To form an obtuse angled triangle the square of any side should be greater than the sum of the squares of the other two sides.
To form an right angled triangle the square of any side should be equal to the sum of the squares of the other two sides.)
Answer. {
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println(“Enter 3 sides:”);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a<b+c && b<a+c && c<a+b)
{
if(a*a<b*b+c*c && b*b<a*a+c*c && c*c<a*a+b*b)
System.out.println(“Acute angled triangle”);
else if(a*a>b*b+c*c || b*b>a*a+c*c || c*c>a*a+b*b)
System.out.println(“Obtuse angled triangle”);
else
System.out.println(“Right angled triangle”);
}
else
System.out.println(“Cannot form a triangle”);
}
}
Question. Write a program to accept a mark obtained by a student in computer science and print the grades accordingly:
Marks Grade
Above 90 A
70 to 89 B
50 to 69 C
below 50 D
Answer. {
static void main()
{
Scanner sc=new Scanner(System.in);
int c;
System.out.println(“Enter marks in Computer science:”);
c=sc.nextInt();
if(c>=90)
System.out.println(“Grade=A”);
else if(c>=70 && c<90)
System.out.println(“Grade=B”);
else if(c>=50 && c<70)
System.out.println(“Grade=C”);
else
System.out.println(“Grade=D”);
}
}
Question. A cloth showroom has announced the following festival discounts on the purchase of items, based on the total cost of the items purchased:
Write a program to input the total cost and compute and display the amount to be paid by the customer after availing the discount.
Answer. {
static void main()
{
Scanner sc=new Scanner(System.in);
float tc,d,ap;
System.out.println(“Enter the total cost of the items:”);
tc=sc.nextFloat();
if(tc<=2000)
d=5/100f*tc;
else if(tc>=2001 && tc<=5000)
d=25/100f*tc;
else if(tc>=5001 && tc<=10000)
d=35/100f*tc;
else
d=50/100f*tc;
ap=tc-d;
System.out.println(“Amount Payable:”+ap);
}
}
Question. An electronics shop has announced the following seasonal discounts on the purchase of certain items.
Write a program based on the above criteria, to input name, address, amount of purchase and the type of purchase (L for Laptop and D for Desktop) by a customer. Compute and print the net amount to be paid by a customer along with his name and address.
(Hint: discount = (discount rate/100)* amount of purchase
Net amount = amount of purchase – discount)
Answer. {
static void main()
{
Scanner sc=new Scanner(System.in);
String name, add;
float a,d=0,na=0;
char type;
System.out.println(“Enter the name:”);
name=sc.nextLine();
System.out.println(“Enter the address:”);
add=sc.nextLine();
System.out.println(“Enter the amount of purchase:”);
a=sc.nextFloat();
System.out.println(“Enter the type of purchase:”);
type=sc.next().charAt(0);
if(type==’L’)
{
if(a>=0 && a<=25000)
d=0;
else if(a>=25001 && a<=57000)
d=5/100f*a;
velse if(a>=57001 && a<=100000)
d=7.5f/100*a;
else
d=10/100f*a;
}
if(type==’D’)
{
if(a>=0 && a<=25000)
d=5/100f*a;
else if(a>=25001 && a<=57000)
d=7.5f/100*a;
else if(a>=57001 && a<=100000)
d=10f/100*a;
else
d=15/100f*a;
}
na=a-d;
System.out.println(“Name:”+name);
System.out.println(“Address:”+add);
System.out.println(“Ndet amount:”+na);
}
}
Question. Given below is a hypothetical table showing rates of Income Tax for male citizens below the age of 65 years:
Taxable Income (TI) in Income Tax in
Does not exceed 1,60,000 Nil
Is greater than 1,60,000 and less than or
equal to 5,00,000
(TI – 1,60,000) * 10%
Is greater than 5,00,000 and less than or
equal to 8,00,000
[(TI – 5,00,000) * 20%] + 34,000
Is greater than 8,00,000 [(TI – 8,00,000) * 30%] + 94,000
Write a program to input the age, gender (male or female) and Taxable Income of a person. If
the age is more than 65 years or the gender is female, display “wrong category*.
If the age is less than or equal to 65 years and the gender is male, compute and display the
Income Tax payable as per the table given above.
Answer. {
static void main()
{
Scanner sc=new Scanner(System.in);
int age;
String gender;
float ti,it;
System.out.println(“Enter the age:”);
age=sc.nextInt();
System.out.println(“Enter the gender:”);
gender=sc.nextLine();
sc.nextLine();//dummy input
System.out.println(“Enter the taxable income:”);
ti=sc.nextFloat();
if(age>65 && gender.equals(“female”))
System.out.println(“Wrong category”);
else
{
if(ti<=160000)
it=0;
else if(ti>160000 && ti<=500000)
it=10/100f*(ti-160000);
else if(ti>500000 && ti<=800000)
it=20/100f*(ti-500000)+34000;
else
it=30/100f*(ti-800000)+94000;
System.out.println(“Income Taxable:”+it);
}
}
}
Question. Write programs for each of the following to print the series:
a. 2, 4, 6, 8, 10, … , 100
b. 99, 97, 95, 93, 91, …, 1
c. 7, 14, 21, 28, 35, …, 70
d. 80, 72, 64, 56, 48, …, 8
e. 1, 4, 9, 16, 25, 36, …. 100
f. 0, 3, 8, 15, 24, 35, …, 99
g. 1, 2, 4, 7, 11, 16, 22, 29, …, upto n terms. [Take n as input]
h. 2, 4, 8, 14, 22, 32, 44, 59, …, upto n terms. [Take n as input]
i. 1, 2, 5, 10, 17, 26, 37, 50, …, upto n terms. [Take n as input]
j. 1, 1, 2, 3, 5, 8, 13, 21, 34, …, upto n terms. [Take n as input]
k. 1, 2, 5, 12, 29, 70, 169, …, upto n terms. [Take n as input]
Answer. {
static void main()
{
int i;
for(i=2;i<=100;i+=2)
{
System.out.print(i+“ ”);
}
}
}
b) class Sol13
{
static void main()
{
int i;
for(i=99;i>=1;i-=2)
{
System.out.print(i+“ ”);
}
}
}
c)
class Sol13
{
static void main()
{
int i;
for(i=7;i<=70;i+=7)
{
System.out.print(i+“ ”);
}
}
}
d) class Sol13
{
static void main()
{
int i;
for(i=80;i>=8;i-=8)
{
System.out.print(i+“ ”);
}
}
}
e) class Sol13
{
static void main()
{
int i;
for(i=1;i<=10;i++)
{
System.out.print((i*i)+“ ”);
}
}
}
f) class Sol13
{
static void main()
{
int i;
for(i=1;i<=10;i++)
{
System.out.print((i*i-1)+“ ”);
}
}
}
g) import java.util.*;
class Sol13
{
static void main()
{
int i,n,s=1;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the number of terms:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
System.out.print(s+“ ”);
s=s+i;
}
}
}
h) import java.util.*;
class Sol13
{
static void h()
{
int i,n,s=2,c=2;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the number of terms:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
System.out.print(s+“ ”);
s=s+c;
c=c+2;
}
}
}
i) import java.util.*;
class Sol13
{
static void main()
{
int i,n,s=1,c=1;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the number of terms:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
System.out.print(s+“ ”);
s=s+c;
c=c+2;
}
}
}
j) import java.util.*;
class Sol13
{
static void main()
{
int i,n,f=1,s=0,t;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the number of terms:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
t=f+s;
System.out.print(t+“ ”);
f=s;
s=t;
}
}
}
k) import java.util.*;
class Sol13
{
static void main()
{
int i,n,f=1,s=0,t;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the number of terms:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
t=f+2*s;
System.out.print(t+“ ”);
f=s;
s=t;
}
}
}
Question. Write a program to find the sum of all 3-digit even natural numbers.
Answer. {
static void main()
{
int i,s=0;
for(i=100;i<=998;i+=2)
{
s+=i;
}
System.out.print(“Sum=”+s);
}
}
Question. Write a program to find the sum of all 3 digit odd natural numbers, which are multiples of 5.
Answer. {
static void main()
{
int i,s=0;
for(i=101;i<=999;i+=2)
{
If(i%5==0)
s+=i;
}
System.out.print(“Sum=”+s);
}
}
Question. Write a program to input an integer and find its factorial. Factorial of a number is the product of all natural numbers till that number. For example factorial of 5 is 120 since 1×2×3×4×5=120.
Answer. {
static void main()
{
long i,n,f=1;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter an integer:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.print(“Factotrial:”+f);
}
}
Question. Write a program to input an integer and check whether it is a prime number or not.
Answer.{
static void main()
{
long i,n,c=0;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter an integer:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
If(n%i==0)
c++;
}
If(c==2)
System.out.print(“Prime Number”);
else
System.out.print(“Not a Prime Number”);
}
}
Question. Write a program to input 10 integers and find the sum of two-digit as well as three-digit numbers separately.
Answer. {
static void main()
{
int i,n,s2=0,s3=0;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(n>=10 && n<=99)
s2+=n;
if(n>=100 && n<=999)
s3+=n;
}
System.out.println(“Sum of 2 digit numbers:”+s2);
System.out.println(“Sum of 3 digit numbers:”+s3);
}
}
Question. Write a program to input 10 integers and display the largest as well as the smallest integer.
Answer. {
static void main()
{
int i,n,lar=0,sma=0;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(i==1)
lar=sma=n;
else
{
if(n>lar)
lar=n;
if(n<sma)
sma=n;
}
}
System.out.println(“Largest number:”+lar);
System.out.println(“Smallest number:”+sma);
}
}
Question. Write a program to input 10 integers and check whether all the entered numbers are even numbers or not.
Answer. {
static void main()
{
int i,n,f=0;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(n%2!=0)
f=1;
}
if(f==0)
System.out.println(“All are even numbers”);
else
System.out.println(“All are not even numbers”);
}
}
Question. Write a program to input 10 integers and check whether all the entered numbers are same or not.
For Example,
INPUT:
Enter 10 numbers: 10 12 13 234 45 34 67 78 76 12
OUTPUT:
All numbers are not same.
INPUT:
Enter 10 numbers: 12 12 12 12 12 12 12 12 12 12
OUTPUT:
All numbers are same.
Answer. {
static void main()
{
int i,n,f=0,p=0;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(i==1)
p=n;
else
{
if(n!=p)
f=1;
}
}
if(f==0)
System.out.println(“All numbers are same”);
else
System.out.println(“All numbers are not same”);
}
}
Question. Write a program to input 10 integers and check whether the entered numbers are in ascending order or not.
For Example,
INPUT:
Enter 10 numbers: 10 12 13 25 45 55 67 78 106 122
OUTPUT:
The numbers are in ascending order.
INPUT:
Enter 10 numbers: 25 34 56 67 12 32 43 21 23 111
OUTPUT:
The numbers are not in ascending order.
Answer. {
static void main()
{
int i,n,f=0,p=0;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(i==1)
p=n;
else
{
if(n<p)
f=1;
p=n;
}
}
if(f==0)
System.out.println(“The numbers are in ascending order”);
else
System.out.println(“The numbers are not in ascending order”);
}
}
Question. Write a program to print the first 15 numbers of the Pell series. Pell series is such a series which starts from 1 and 2 , and subsequent numbers is the sum of twice the previous number and the number previous to the previous number. Pell series: 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378, 5741, 13860, …
Answer. {
static void main()
{
int i,n,f=1,s=0,t;
for(i=1;i<=15;i++)
{
t=f+2*s;
System.out.print(t+“ ”);
f=s;
s=t;
}
}
}
Question. Write a program to find the sum of 1st 10 numbers of Lucas series i.e. 2,1,3,4,7,11,18,…. Lucas series is such a series which starting from 2 and 1, and subsequent numbers are the sum of the previous two numbers.
Answer. {
static void main()
{
int i,n,f=2,s=1,t,sum=0;
sum=f+s;
for(i=1;i<=8;i++)
{
t=f+s;
sum+=t;
f=s;
s=t;
}
System.out.println(“Sum=”+sum);
}
}
Question. Write a program to input an integer and check whether it is perfect, abundant or deficient number. If the sum of the factors excluding itself is equal to that number it is perfect, if greater than that number it is abundant and if less than that number it is deficient number.
Answer. {
static void main()
{
int i,n,s=0;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter an integer:”);
n=sc.nextInt();
for(i=1;i<n;i++)
{
if(n%i==0)
s+=i;
}
if(s==n)
System.out.println(“Perfect Number”);
else if(s>n)
System.out.println(“Abundant Number”);
else
System.out.println(“Deficient Number”);
}
}
Question. Write a program to input two integers and check whether it forms an amicable pair or not. An amicable pair is such that, the sum of the factors excluding itself of one number is the other number and sum of the factors excluding itself of the other number is this number.
Example, (220, 284). Since sum of factors excluding itself of :
220 = 1+2+4+5+10+11+20+22+ 44+55+110=284
284 = 1+ 2+4+71+142=220.
Answer. {
static void main()
{
int a,b,sa=0,sb=0;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter 2 integers:”);
a=sc.nextInt();
b=sc.nextInt();
for(i=1;i<a;i++)
{
if(a%i==0)
sa+=i;
}
for(i=1;i<b;i++)
{
if(b%i==0)
sb+=i;
}
if(sa==b && sb==a)
System.out.println(“Amicable Pair”);
of the
Example, (220, 284). Since sum of factors excludin
}
}
Question. Using nested loops write programs to generate the following patterns on the screen: