Genocide in Rwanda: 100 Days of Terror in the Land of A Thousand Hills
Better than a thousand days of diligent study is one day ... ICSE/JavaQuestionBank 10.0.pdf"Better...
Transcript of Better than a thousand days of diligent study is one day ... ICSE/JavaQuestionBank 10.0.pdf"Better...
"Better than a thousand days of diligent study is one day with a
Great Teacher"
Java Question Bank (Version 10.0) www.techsparx.net
www.techsparx.webs.com Saravanan.G
1 TechSparx Computer Training Center - 9880 205065
Contents
Most Simplest ............................................................................................................................................... 2
Decision Making Statements ........................................................................................................................ 4
Switch Statement .......................................................................................................................................... 7
Menu Driven Program................................................................................................................................... 7
Bill Programs ................................................................................................................................................. 8
Looping constructs (for, while, do while) ..................................................................................................... 9
Numbers – for loop ..................................................................................................................................... 10
Numbers – while loop ................................................................................................................................. 11
Series – nth term .......................................................................................................................................... 15
Series – n terms........................................................................................................................................... 16
Patterns ....................................................................................................................................................... 17
One Dimensional Array ............................................................................................................................... 19
Searching and Sorting ................................................................................................................................. 20
Strings – Part 1 ............................................................................................................................................ 21
Strings – Part 2 ............................................................................................................................................ 22
Strings – Part 3 ............................................................................................................................................ 23
Functions ..................................................................................................................................................... 27
Classes, Objects, Functions and Constructors ............................................................................................ 28
2 TechSparx Computer Training Center - 9880 205065
Most Simplest
1. Write a program (WAP) to print “Hello World”.
2. Write a program to print your name.
3. Write a program to add two numbers and display the given numbers and the sum.
4. Write a program to find the difference of two numbers and display the given numbers and the
difference.
5. Write a program to find the product of two numbers and display the given numbers and the
product.
6. Write a program to find the quotient of two numbers and display the given numbers and the
quotient.
7. Write a program to find the remainder of two numbers and display the given numbers and the
remainder.
8. Write a program to find the sum, difference, product, quotient and remainder of two numbers.
9. Write a program to find area of a square.
10. Write a program to find area of a rectangle.
11. Write a program to find the area of a circle.
12. Write a program to find the area of a triangle whose base and height are given.
13. Write a program to find area of a triangle whose 3 sides are given.
14. Write a program to find the volume and surface area of a sphere.
15. Write a program to convert Fahrenheit to Celsius
16. Write a program to convert Celsius to Fahrenheit.
17. Write a program to calculate the simple interest.
18. Write a program to calculate the compound interest.
19. Write a program to calculate the nth repunit.
20. Write a program to calculate the nth mersenne number.
21. Write a program swap two variables.
22. Write a program to find the roots of a quadratic equation.
3 TechSparx Computer Training Center - 9880 205065
Important formulas:
Name Mathematical Formula Java Expression
1. Area of a Square double area = side * side;
2. Area of a Rectangle double area = length * breadth;
3. Area of a circle double area = Math.PI * radius * radius;
4. Area of a triangle
double area = (1.0/2.0) * breadth * height;
5. Area of a triangle
√ ( )( )( )
where
double s = (a * b * c) / 2.0;
double area = Math.sqrt( s * (s-a) * (s-b) * (s-c) );
6. Volume of a sphere
double volume = (4.0 / 3.0) * 3.14 * r * r * r;
or
double volume = (4.0 / 3.0) * Math.PI * r * r * r;
or
double volume = (4.0 / 3.0) * Math.PI * Math.pow(r, 3);
7. Surface area of a
sphere double surfaceArea = 4.0 * Math.PI * r * r;
8. Roots of quadratic
equation √
double x = (-b + Math.sqrt( (b * b) – (4 * a * c) ) / ( 2 * a) ;
double x = (-b - Math.sqrt( (b * b) – (4 * a * c) ) / ( 2 * a) ;
9. Simple Interest
double si = (p * t * r) / 100.0;
10. Compound Interest {(
)
} ci = p * ( Math.pow( (1 + (r/100.0)), t ) - 1 )
11. Repunit
int repunit = ((int)Math.pow(10, n) – 1) / 9
12. Mersenne Number int mersenne = (int)Math.pow(2, n) – 1
13. Celsius to
Fahrenheit (
) double f = ( c * (9.0/5.0) ) + 32.0;
14. Fahrenheit to
Celsius ( ) (
) double c = ( f – 32.0 ) * (5.0/9.0);
4 TechSparx Computer Training Center - 9880 205065
Decision Making Statements
Using if
1. Write a program to find the larger of two given numbers.
2. Write a program to find the smaller of two given numbers.
3. Write a program to find if the given number is an even or odd number.
4. Write a program to check if the given number is Divisible by 7 (seven).
5. Write a program to check if the given number is positive or negative.
Using if … else
6. Write a program to find the larger of two given numbers.
7. Write a program to find the smaller of two given numbers.
8. Write a program to find if the given number is an even or odd number.
9. Write a program to check if the given number is Divisible by 7 (seven).
10. Write a program to check if the given number is positive or negative.
Using if … else if
11. Write a program to find the largest of three given numbers.
12. Write a program to find the smallest of three given numbers.
13. Write a program to find the real roots of a quadratic equation. (ax2 + bx + c = 0)
14. Write a program to print the grade based on the marks obtained according to the following
table
Marks Grade
>=80 A
60 to 80 B
50 to 60 C
40 to 50 D
<40 E
5 TechSparx Computer Training Center - 9880 205065
15. Write a program to print the day of week. (using if … else if ladder)
Example: day = 1 dayOfWeek = “Monday”
day = 2 dayOfWeek = “Tuesday”
16. Write a program to print the month of the year. (using if … else if ladder)
Example: month = 1 --> monthOfYear = "January"
month = 2 --> monthOfYear = "February"
17. Write a program to check if the given character is a digit or upper case letter or a lower case
letter or a special character.
18. Write a program to check if the given character is a digit or upper case letter or a lower case
letter or a special character. (using Character class functions)
19. Write a program to check if the given character is a vowel.
20. Write a program to check if the given number is a buzz number.
Buzz number: A number which is either divisible by 7 or ends with 7.
For Example: 67, 49, 37, 21 etc
21. Write a program to check if the given number is a Perfect square.
For Example: 25, 36, 49 etc
6 TechSparx Computer Training Center - 9880 205065
7 TechSparx Computer Training Center - 9880 205065
Switch Statement
1. Write a program to input number of week’s day (1-7) and translate to its equivalent name of the day
of the week.
Example: 1 to Monday, 2 to Tuesday… 7 to Sunday
2. Write a program to input number of month of the year (1 - 12) and translate to its equivalent name
of the month.
Example: 1 to January, 2 to February … 12 to December
3. Write a program to check if the given character is a vowel.
Menu Driven Program
1. Using a switch statement, write a MENU driven program to find the sum, difference, product
quotient and remainder. OR Write a program to simulate a SIMPLE CALCULATOR.
2. Using a switch statement, write a MENU driven program to convert a given temperature from
Fahrenheit to Celsius and vice versa. For an incorrect choice, an appropriate error message should
be displayed.
3. Using a switch statement, write a MENU driven program to calculate the AREA of square, rectangle,
triangle and circle.
8 TechSparx Computer Training Center - 9880 205065
Bill Programs
1. Design a class to enter the monthly salary of a person, calculate and print the annual tax
payable by that person.
Annual Salary (Rs.) Tax Payable
Below 50,000 5% of annual salary
50,001 to 1,25,000 10% of annual salary
Above 1,25,001 20% of annual salary
2. Define a class Electricity
a. Accept Name, Consumer Number and Units consumed.
b. Compute the electricity charges accordingly
upto 100 units : 80 paise per unit
for next 100 units : Rs. 1 per unit
for more than 200 units : Rs. 1.25 per unit
c. Display the name, consumer number and total bill to be paid.
3. BSNL charges for using telephone from their consumers according to the calls made (per month)
as per the following tariff:
Number of calls Charge
Up to 50 calls Rs. 1.50 per call
For next 100 calls Rs. 1 per call
For next 200 calls 90 paise per call
More than 350 calls 80 paise per call
However, monthly rental charge is Rs. 180/- per month for all the consumers for using
Telephones. Write a program in Java to calculate monthly telephone bill indicating number of
calls, monthly rental and total amount to be paid. Take number of calls as an input.
9 TechSparx Computer Training Center - 9880 205065
Looping constructs (for, while, do while)
Printing Programs
1. Write a program to print 5 stars. --trace
2. Write a program to print first 5 natural numbers --trace
3. Write a program to print first n natural numbers
4. Write a program to print numbers from 6 to 10 --trace
5. Write a program to print numbers from m to n
6. Write a program to print numbers from 5 to 1 --trace
7. Write a program to print all even numbers from 1 to 10. --trace
8. Write a program to print all even numbers from 1 to n.
9. Write a program to print all odd numbers from 1 to 10. --trace
10. Write a program to print all odd numbers from 1 to n.
11. Write a program to print all even numbers from 1 to 10. (use if inside while) --trace
12. Write a program to print all odd numbers from 1 to 10. (use if inside while) --trace
13. Write a program to print all numbers from 1 to 20, which are divisible by 3. (use if inside while)
--trace
Sum and Product Programs
14. Write a program to find the sum of first 5 natural numbers --trace
15. Write a program to find the sum of first n natural numbers
16. Write a program to find the sum of all even numbers from 1 to n. --trace
17. Write a program to find the sum of all odd numbers from 1 to n. --trace
18. Write a program to find the sum of all odd numbers from m to n.
19. Write a program to find the sum of all numbers from 1 to 20, which are divisible by 3.
20. Write a program to find the product of first five natural numbers. --trace
21. Write a program to find the Factorial of n. --trace
22. Write a program to find the Double Factorial of n. --trace
23. Write a program to find the Triple Factorial of n.
10 TechSparx Computer Training Center - 9880 205065
Numbers – for loop
1. Write a program to calculate and display first n repunits.
2. Write a program to calculate and display first n Mersenne number (binary repunits).
3. Write a program to find the Factorial of n.
4. Write a program to find the Double Factorial of n.
5. Write a program to find the Triple Factorial of n.
6. Write a program to display the factors/divisors of a given number.
7. Write a program to find the number of factors/divisors of a given number.
8. Write a program to calculate the sum of factors/divisors of a given number. --trace
9. Write a program to check if two given numbers are amicable.
10. Write a program to find if the given number is one of the following:
a. Perfect Number
b. Deficient Number
c. Abundant Number
11. Write a program to check if the given number is a Prime Number or composite number.
Nested for loop
12. Write a program to display all the prime numbers from 1 to n.
13. Write a program to display first 20 prime numbers.
14. Write a program to find the Primorial of the given number.
11 TechSparx Computer Training Center - 9880 205065
Numbers – while loop
1. Write a program to print each digit of a given number in different lines.
2. Write a program to find the number of digits in the given number.
3. Write a program to find the sum of all the digits of a given number.
4. Write a program to find the sum of the squares of each digit of a given number.
5. Write a program to check if the given number is an Armstrong/Narcissistic Number.
6. Write a program to convert a BINARY number to DECIMAL number.
7. Write a program to REVERSE a given number.
8. Write a program to check if the given number is a PALINDROME.
9. Write a program to check if the given number is an AUTOMORPHIC Number.
10. Write a program to calculate the LCM and HCF/GCD of two given numbers.
11. Write a program to convert a Decimal Number to Binary.
12. Write a program to convert a Decimal Number to Octal.
13. Write a program to convert a Decimal Number to Hexadecimal. –Needs String
14. Write a program to print first 10 buzz numbers.
Nested while loop
1. Write a program to check if the given number is a SPECIAL number.
2. Write a program to check if the given number is a MAGIC Number.
3. Write a program to check if the given number is a HAPPY Number.
4. Write a program to count the frequency of given digit in given number.
5. Write a program to check if the given number is a UNIQUE Number.
12 TechSparx Computer Training Center - 9880 205065
Definitions of different kinds of numbers:
1. Armstrong or Narcissistic Number: When the sum of the cubes of each digit in the number is
equal to the number itself then the number is said to be an Armstrong/Narcissistic number.
Example: 407 = 43 + 03 + 73
153 = 13 + 53 + 33
2. Repunits: The term repunits comes from the words “repeated” and “unit”, so repunits are
positive integers in which every digit is one.
Formulae: Rn = (10n – 1)/9
Example: 1, 11, 111, 1111, 11111 and so on…
3. Mersenne Numbers: A Mersenne number is a number of the form Mn = 2n – 1, where n is an
integer. The Mersenne numbers consist of all 1s in base-2, and are therefore binary repunits.
Formulae: (21 - 1), (22 - 1), (23 - 1), (24 - 1) … (2n - 1)
Example: The first few Mersenne numbers are 1, 3, 7, 15, 31, 63, 127, 255,
corresponding to 12, 112, 1112, 11112, 111112 ... in binary.
4. Prime Number: A number is said to be a prime number only if it is divisible by 1 and itself.
Example: 2, 3, 5, 7, 11, 13, 17, 19 …
5. Composite Number: A number which is not a prime number is called as Composite Number.
Example: 4, 6, 8, 9, 10, 12, 14, 15, 16…
6. Multi-Factorial:
a. n- Factorial -> n! = n * (n-1) * (n-2) * (n-3) * (n-4) * …… * 1
b. n- Double Factorial -> n!! = n * (n-2) * (n-4) * (n-3) * (n-6) * ……* (1 or 2)
c. n- Triple Factorial -> n!!! = n * (n-3) * (n-6) * (n-9) * (n-12) * ……* (1 or 2 or 3)
7. Primorial(P#): is defined to be the product of prime numbers less than or equal to p.
Example: 3# = 2 * 3 = 6
5# = 2 * 3 * 5 = 30
13# = 2 * 3 * 5 * 7 * 11 * 13 = 30030
8. Perfect Square: A number is said to be a perfect square if and only if the square root of that
number is not a fractional number.
Example: 4, 9, 16, 25, 36, 49, 64, 81 …
9. Perfect Number: A number is said to be a perfect number if and only if the sum of all its divisors
(excluding the number itself) is equal to the number.
Example: 6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14 where 1, 2, 4, 7, 14 are divisors.
13 TechSparx Computer Training Center - 9880 205065
10. Deficient, Perfect or Abundant Number: Suppose you take a positive integer n and add its
positive divisors (inclusive of the given number).
For Example:
If n = 18, then SUM = 1 + 2 + 3 + 6 + 9 + 18 = 39.
In general, when we do this with n, the number n would be one of the following three kinds.
If the SUM is n is said to be Examples
less than 2n Deficient Number 1, 2, 3, 4, 5, 8, 9
Equal to 2n Perfect Number 6, 282, 496
Greater than 2n Abundant Number 12, 18, 20, 24, 30
11. Automorphic Number: A number is said to be Automorphic if it is present at the extreme right
of its square.
Eg: 52 = 25, 62 = 36, 252 = 625 etc.
12. Amicable Numbers: Two numbers are said to be amicable if and only if the sum of the proper
factors of num1 is equal to num2 and the sum of proper factors of num2 is equal to num1
Example: The number pair of 220 and 284 is considered amicable, because the sum of the proper divisors of 220 is 284, while the sum of the proper divisors of 284 is 220.
Number Divisors/Factors Sum of Divisors/Factors
220 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110 284
284 1, 2, 4, 71, 142 220
13. Special Number: Sum of the factorials of each digit of a given number is equal to the given
number then the given number is said to be a special number.
For Example:
145
1! = 1 = 1
4! = 4*3*2*1 = + 24
5! = 5*4*3*2*1 = +120
______
145
_______
14. Buzz Number: A number which is either divisible by 7 or ends with 7.
For Example: 67, 49, 37, 21 etc
15. Magic Number: A number whose sum of the digits until it sums up to a single digit and if that
single digit is equal to 1 then it is called a Magic Number.
For Example:
55
5 + 5 = 10, further find the sum of 10 also since it is not a single digit number 1 + 0 = 1
14 TechSparx Computer Training Center - 9880 205065
16. Happy Number: A number whose sum of the squares of each digit until it sums up to a single
digit and if that single digit is equal to 1 then it is called as a Happy Number.
For Example:
28
22 + 82 = 4 + 64 = 68. (But 68 is not a single digit so again find the sum of squares of 68)
62 + 82 = 36 + 64 = 100. (But 100 is not a single digit so again find the sum of squares of 100)
12 + 02 + 02 = 1. (Since the result is 1, the given number 28 is a HAPPY Number)
17. Unique Number: A number in which no two digits are same i.e., no digit should get repeated.
Example:
1358 is a unique number.
1353 is not a unique number because the digit 3 is repeated
15 TechSparx Computer Training Center - 9880 205065
Series – nth term
1. Program to find the sum and print the Series 1 2 3 4 5 . . . . n
2. Program to find the sum and print the Series 2 4 6 8 . . . . n
3. Program to find the sum and print the Series 1 2 4 8 16 32 . . . . 2n
4. Program to find the sum and print the Series 0 1 3 7 15 31 . . . . 2n - 1
5. Program to find the sum and print the Series 1 4 9 16 25 . . . n2
6. Program to find the sum and print the Series 1 10 100 1000 10000 . . . . 10n
7. Program to find the sum 12 + 22 + 32 + 42 +.... n2
8. Program to find the sum of (1*2) + (2*3) + (3*4) + (4*5) + . . . . + (n * (n+1))
9. Program to find the sum of the series x1 + x3 + x5 + x7 + ... xn
10. Program to find the sum of the series x1 - x3 + x5 - x7 + ... xn
11. Program to find the sum of series 1! + 3! + 5! + .... n!
12. Program to find the sum of series 1! - 3! + 5! - .... n!
13. Program to find the sum of series
14. Program to find the sum of series
15. Program to find the sum of series
1 + (1+2) + (1+2+3) + (1+2+3+4) + . . . + (1+2+3+4+...+n)
16. Program to find the sum of series 1 - (1*2) + (1*2*3) - (1*2*3*4) + . . . - (1*2*3*4*...*n)
17. Program to find the sum of the following series
16 TechSparx Computer Training Center - 9880 205065
Series – n terms
18. Program to find the sum and print the Series 1 2 3 4 5 . . . . n terms
19. Program to find the sum and print the Series 2 4 6 8 . . . . n terms
20. Program to find the sum and print the Series 1 2 4 8 16 32 . . . . n terms
21. Program to find the sum and print the Series 0 1 3 7 15 31 . . . . n terms
22. Program to find the sum and print the Series 2 3 5 7 11 13 17 . . . . n terms
23. Program to find the sum and print the Series 1 4 9 16 25 . . . . n terms
24. Program to find the sum and print the Series 100 1000 10000 . . . . n terms
25. Program to find the sum 12 + 22 + 32 + 42 +.... n terms
26. Program to find the sum of (1*2) + (2*3) + (3*4) + (4*5) + . . . . n terms
27. Program to print the Fibonacci series. 0, 1, 1, 2, 3, 5 ... n terms
28. Program to print the Tribonacci series. 0, 1, 1, 2, 4, 7 ... n terms
29. Program to find the sum of the series x + x3 + x5 + x7 + ... n terms
30. Program to find the sum of the series x - x3 + x5 - x7 + ... n terms
31. Program to find the sum of series 1! + 3! + 5! + .... n terms
32. Program to find the sum of series 1! - 3! + 5! - .... n terms
33. Program to find the sum of series (x1)/(1!) - (x3)/(3!) + (x5)/(5!) - .... n terms
34. Program to find the sum of series 1 - (x2)/(2!) + (x4)/(4!) - .... n terms
35. Program to find the sum of series 1 + (1+2) + (1+2+3) + (1+2+3+4) + . . . + (1+2+3+4+...+n)
36. Program to find the sum of series 1 - (1*2) + (1*2*3) - (1*2*3*4) + . . . - (1*2*3*4*...*n)
37. Program to find the sum of the following series
((1+2)/(1*2)) - ((1+2+3)/(1*2*3)) + . . . - ((1+2+3+4+ ... +n)/(1*2*3*4* ... *n))
17 TechSparx Computer Training Center - 9880 205065
Patterns
1. Square
First Second Third Fourth Fifth Sixth Seventh Eight
* * * *
* * * *
* * * *
* * * *
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
4 4 4 4
3 3 3 3
2 2 2 2
1 1 1 1
4 3 2 1
4 3 2 1
4 3 2 1
4 3 2 1
1 2 3 4
5 6 7 8
8 7 6 5
4 3 2 1
2. Right Angled Triangle
First Second Third Fourth Fifth Sixth Seventh Eight
*
* *
* * *
* * * *
1
1 1
1 1 1
1 1 1 1
1
1 2
1 2 3
1 2 3 4
1
2 2
3 3 3
4 4 4 4
4
3 3
2 2 2
1 1 1 1
4
4 3
4 3 2
4 3 2 1
1
2 3
4 5 6
7 8 9 10
9
8 7
6 5 4
3 2 1 0
3. Reversed Right Angled Triangle
Eleventh Twelfth Thirteenth Fourteenth Fifteenth Sixteenth Seventeenth
* * * *
* * *
* *
*
2 2 2 2
2 2 2
2 2
2
1 2 3 4
1 2 3
1 2
1
4 4 4 4
3 3 3
2 2
1
1 1 1 1
2 2 2
3 3
4
4 3 2 1
4 3 2
4 3
4
0 1 2 3
4 5 6
7 8
9
4. Combined Right Angled Triangles
Twenty First
Pattern
Twenty Second
Pattern
Twenty Third
Pattern
Twenty Fourth
Pattern
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
1
2 3
4 5 6
7 8 9 10
9 8 7
6 5
4
18 TechSparx Computer Training Center - 9880 205065
5. Equilateral Triangle and Rhombus with spaces in between stars
Thirty First Pattern Thirty Second Pattern Thirty Third Pattern
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
6. Equilateral Triangle and Rhombus without spaces in between stars
Forty First Pattern Forty Second Pattern Forty Third Pattern
*
***
*****
*******
*********
*********
*******
*****
***
*
*
***
*****
*******
*********
*******
*****
***
*
7. Equilateral Triangle and Rhombus without spaces in between Numbers Fifty First Pattern Fifty Second Pattern Fifty Third Pattern
1
121
12321
1234321
123454321
123454321
1234321
12321
121
1
1
121
12321
1234321
123454321
1234321
12321
121
1
8. Equilateral Triangle and Rhombus without spaces in between Alphabets
Sixty First Pattern Sixty Second Pattern Sixty Third Pattern
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
19 TechSparx Computer Training Center - 9880 205065
One Dimensional Array
1. Write a program to read 10 numbers from user and store it in an array and display them.
2. Write a program to input an array of 5 integers and find the sum of all the array elements.
3. Write a program to input an array of 5 integers and find the sum of all the elements which are at
even index.
4. Write a program to input an array of 5 integers and find the sum of all the elements which are at
odd index.
5. Write a program to input an array of 5 integers and find the sum of all the even elements.
6. Write a program to input an array of 5 integers and find the sum of all the odd elements.
7. Write a program that quadruples every element of an array of size n and stores it back in the
same array.
8. Write a program to input the marks of 10 subjects and find the highest and lowest marks.
9. Write a program that reverses an array and stores it in a new array.
10. Write a program that reverses an array and stores it in the same array.
11. Write a program to generate first 10 Fibonacci numbers and store it in an array and then display
the array.
12. Write a program to accept two arrays and merge them one after the other as shown in the
example.
First array = {10, 20, 30, 40, 50}
Second Array = {100, 200, 300}
Third Array = {10, 20, 30, 40, 50, 100, 200, 300}
13. Write a program to accept two arrays and generate a third array as shown in example.
Example:
First array = {10, 20, 30, 40, 50}
Second Array = {100, 200, 300, 400, 500}
Third Array = {10, 100, 20, 200, 30, 300, 40, 400, 50, 500}
14. Write a program to delete duplicate elements from an array of size 10. And display the resultant
array.
15. Create two arrays of type char and initialize them with 10 characters each. And then check if
characters in corresponding positions of the two character arrays are same. Display the
difference in the ASCII values of the first unequal pair of characters.
20 TechSparx Computer Training Center - 9880 205065
Searching and Sorting 1. State conditions under which BINARY SEARCH is applicable.
2. Write a program to perform a Linear Search
3. Write a program to perform a Binary Search
4. Write a program to perform a Bubble Sort
5. Write a program to perform a Selection Sort
6. Perform bubble sort on the following array elements and arrange them in ascending order (Do
NOT write the program, you need to write the trace)
26 21 20 23 29 17
7. Perform selection sort on the above array and give the array status after every iteration. (Do
NOT write the program, you need to write the trace)
8. Write a program to search for 66 and 71 in the following array using BINARY SEARCH technique.
Also write down the intermediate status of the array.
3 4 7 11 18 29 45 71 87 89 93 96 99 400
9. Write a program to initialize an array of 10 names and initialize another array with their
respective telephone numbers. Search for a name input by the user, in the list. If found display
“Search Successful” and print the name and telephone number, else display “Search
unsuccessful”.
10. Write a program to read 10 names from the user and then arrange it in ascending order using
bubble sort technique and display the names before and after sorting.
11. Write a program to read 10 names from the user and then arrange it in ascending order using
selection sort technique and display the names before and after sorting.
12. Write a program to input 10 states and its capital. Display the states and capital in the sorted
order of states.
21 TechSparx Computer Training Center - 9880 205065
Strings – Part 1
1. Write a program to display the length of the string.
2. Write a program to display the character along with its index in different lines.
3. Write a program to display characters at even index along with its index in different lines.
4. Write a program to display characters at odd index along with its index in different lines.
5. Write a program to reverse a given string.
6. Write a program to check if given string is a palindrome.
7. Write a program to count the number of vowels in the given string.
8. Write a program to find the frequency of the given character in a given string.
9. Write a program to find the frequency of each character in a given string.
10. Write a program to input a string and convert upper case to lower case and vice versa.
11. Write a program that translates a word into Pig-Latin form.
For example: If the given word is “trouble”, then the Pig-Latin form is “oubletray”. Basically all the letters from the first
character till you find a vowel is moved to the end of the word and “ay” is appended after that (“ouble-tr-ay”)
12. Write a program to accept two strings and check
a. If first string is equal to second string or
b. If first string is alphabetically greater than second string or
c. If first string is alphabetically smaller than second string
13. Write a program to check if given string starts and ends with “MA”
14. Write a program to check if the given number is an Automorphic number using strings.
15. Write a program to print the following patterns
B L U E J
B L U E J
B L U E J
B L U E J
B L U E J
B L U E J
B L U E
B L U
B L
B
B B B B B
L L L L
U U U
E E
J
22 TechSparx Computer Training Center - 9880 205065
Strings – Part 2
1. Write a program to count the number of words and print each word in different line.
2. Write a program to find the frequency of the given word in the given string.
3. Write a program to input a string and display the longest word along with the number of characters.
4. Write a program to input a string, count the number of words, blank spaces, vowels, uppercase,
lower case, digits and special characters. Also display one word per line.
5. Write a program a program to input a string "India is my country” and print out the following
sentences
a. aidnI si ym yrtnuoc
b. country my is India
6. Write a program to input a name and print its short form
Example: Input – Mohandas Karamchand Gandhi
Output - M.K.Gandhi
7. Consider the following statement "May 1 is celebrated as the workers day". Change 1 to 14, May to
November and workers to children and print the original string and changed string.
8. Write a program to accept a string and accept a character. Display all the words containing the
accepted character.
Example:
Input String: “AMIT AND SUMIT WERE VERY CLOSE WITH DEEPAK”
Input Character: ‘A’
Output: AMIT, AND, DEEPAK
9. Write a program to accept a string and display a new string after encoding = -3, which means each
character moves three characters behind.
Example:
Input String: "JAVA" and encoding = 2, means each character moves two characters ahead. Thus, new string is: “LCXC”
Input String: "ABACUS" and encoding = -3, means each character moves two characters behind. Thus, new string is:
“XYXZRP”
23 TechSparx Computer Training Center - 9880 205065
Strings – Part 3
1. Write a program to read the NAME and SALARY of 50 employees and perform the following
activities using one-dimensional array
a. Display the name and salary of the employee who is getting the HIGHEST salary.
b. Display the name and salary of the employee who is getting the LOWEST salary.
c. Display the AVERAGE salary.
2. Write a program to read the name and marks of n students and find the highest and lowest scorer.
3. Write a program to read the name and marks of n students and bubble sort them in ascending order
of their marks.
4. Write a program to read the name and marks of n students and bubble sort them in ascending order
of their names.
5. Write a program to sort the characters of a string in the ascending order and display a string in
which all the characters of the string are in ascending order.
6. Write a program to accept two words and check whether they are Anagrams.
An Anagram is a word that is made with the combination of the letters present in the original word. Example: A word is FOWL and the other word is WOLF which is formed with the combinations of the letters present in the
word. Thus FOWL and WOLF are ANAGRAMS.
7. Write a program to sort the words of a string in ascending order. 8. Program to accept a string and sort the words in the string based on the number of vowels in each
word and display them in the ascending order.
9. Write a program that alters a string in such a way that the alphabet next to a vowel gets replaced by
an equivalent opposite case alphabet.
24 TechSparx Computer Training Center - 9880 205065
Theory Java offers 3 classes to work with character data. 1. Character class whose instances or objects can hold single character data. This class offers many
methods to manipulate or inspect single-character data. 2. String class whose instances or objects can hold unchanging string (immutable string) i.e., once
initialized its contents cannot be modified. 3. StringBuffer class whose instances or objects can hold (mutable) strings that can be changed or
modified. Creating Strings
1. String name = “I am a student”; 2. String myName = new String(“I do not know my name”);
Accessor Methods: Methods used to obtain information about an object are known as assessor methods.
Character Function Prototype Description
1 static Boolean isUppercase(char ch) Returns true if ch is an Upper Case character
2 static Boolean isLowercase(char ch) Returns true if ch is a Lower Case character
3 static char toUpperCase(char ch) Converts ch to upper case and returns that character
4 static char toLowerCase(char ch) Converts ch to lower case and returns that character
25 TechSparx Computer Training Center - 9880 205065
String
Return Type Function Signature and Description
1 int length()
--Returns the length of the string
2 char charAt(int index)
--Returns the character at the specified index
3 int indexOf(char ch)
--Returns the index of the first occurrence of the character ch
4 int lastIndexOf(char ch)
--Returns the index of the last occurrence of the character ch
5 int compareTo(String str)
--Compares two strings lexicographically.
6 String concat(String str)
--Concatenates the specified string to the end of this string --Concatenation operator(i.e., +), achieves same as concat method (str1 + str2)
8 boolean startsWith(String str)
--Tests if the this string starts with str
9 boolean endsWith(String str)
--Tests if the this string ends with str
10 boolean equals(String str)
--Compares the this string to the specified object str
11 boolean equalsIgnoreCase(String str)
--Compares the this string to the specified object str ignoring the case
12 String toLowerCase()
--Converts all of the characters in the this String to lower case
13 String toUpperCase()
--Converts all of the characters in the this String to upper case
14 String toString()
--Returns the String itself
15 String replace(char oldChar, char newChar)
--Returns a new string resulting from replacing all occurrences of oldChar in the this string with newChar
16 String substring(int begIndex, int endIndex)
--Returns a substring starting from begIndex till endIndex. Inclusive of begIndex but exclusive of endIndex.
17 String trim()
--Removes white space from both ends of the this String
18 String valueOf(all types)
--Returns the string representation of the passed argument. Eg: 12 represented as “12”
19 char[] toCharArray()
--Converts this string into an array of characters and returns the array.
26 TechSparx Computer Training Center - 9880 205065
StringBuffer
Return Type Function Signature and Description
1 int length()
-- Returns the length (character count) of this string buffer.
2 int capacity()
-- Returns the current capacity of the String buffer i.e., returns the maximum number of characters that can be stored in this string buffer
3 StringBuffer append(String str)
-- Appends the string str to this string buffer.
4 StringBuffer insert(int offSet, String str)
-- Inserts the string str into this string buffer at offSet index
5 void setCharAt(int index, char ch)
-- The character at the specified index of this string buffer is set to ch.
6 StringBuffer delete(int begIndex, int endIndex)
-- Removes the characters from begIndex(inclusive) to endIndex(exclusive) in this StringBuffer.
7 void setLength(int newLength)
-- Sets the length of this String buffer.
8 StringBuffer reverse()
-- The character sequence contained in this string buffer is replaced by the reverse of the sequence.
9 StringBuffer replace(int begIndex, int endIndex, String str)
-- Replaces the characters in a this StringBuffer with characters in the string str.
10 char charAt(int index)
-- Returns the character at index of this string buffer.
11 int indexOf(String str)
-- Returns the index within this string of the first occurrence of the substring str.
12 String toString()
-- Converts to a string representing the data in this string buffer.
13 String substring(int begIndex)
-- Returns a new String that contains a subsequence of characters currently contained in this StringBuffer. The substring begins at the begIndex and extends to the end of the StringBuffer.
14 String substring(int begIndex, int endIndex)
-- Returns a new String that contains a subsequence of characters currently contained in this StringBuffer.
27 TechSparx Computer Training Center - 9880 205065
Functions
Category 2: Accepts but does NOT Return
1. Write a program having the following functions
a. Function which accepts two integers and finds the greatest of two numbers and displays it.
b. Function which accepts three integers and finds the greatest of 3 numbers and displays it.
c. Main function which calls the above two functions one after the other.
Category 3: Accepts and Returns
2. Write a program to accept two words and check whether they are Anagrams. An Anagram is a
word that is made with the combination of the letters present in the original word.
Example: A word is FOWL and the other word is WOLF which is formed with the combinations of the letters present in
the word. Thus FOWL and WOLF are ANAGRAMS.
3. Write a program to implement checkEven() method that receives a number and then checks
whether the given number is even or not. If it is, the method returns Boolean true otherwise
Boolean false.
Overloaded Functions:
4. Write a program having the following overloaded functions
a. Function to find the area of square – public static void findArea(int side)
b. Function to find the area of rectangle – public static void findArea(int length, int breadth)
c. Function to find the area of circle – public static void findArea(double radius)
28 TechSparx Computer Training Center - 9880 205065
Classes, Objects, Functions and Constructors
1. Define a class Student with the following members:
a. Member variables: name, age, totalMarks
b. Member Function:
i. main() – method which instantiates two objects and displays them.
2. Define a class Student with the following members:
a. Member variables: name, age, totalMarks
b. Member Function:
i. main() – method which instantiates two object and calls the other two functions
ii. input() – method which reads the values from the user and initializes the object
variables
iii. display() – method which displays the details of the student.
3. Define a class Cuboid with following members:
a. Member variables : length, breadth, height, volume, surfaceArea and diagonal
b. Member Methods:
i. initialize() – method which initializes the member variables. Take the values from the user
ii. compute() – method which computes volume, surfaceArea and diagonal of cuboid
display() – method which displays all the member variables
main() – method which creates and object of Cuboid class and calls all the other methods
Formulae:
volume = l * b * h
surfaceArea = 2 * (l*b + b*h + l*h)
diagonalOfCuboid = Math.sqrt(l*l + b*b + h*h)
4. Define a class ‘Compound’ that offers the functionality to calculate simple interest and
compound interest using two functions.
i. simpleInterest = (principal * rate * time) / 100
ii. compoundInterest = principal * ( (1 + (rate/100))time - 1)
5. Write a program that implements a rectangle class. The rectangle class has fields: length,
breadth, area and perimeter. It has methods: to obtain values of length and breadth, to
calculate area and to calculate perimeter.
6. Write a program to implement a class Numbers having fields storing 3 integers and methods to
find maximum, minimum and average of 3 numbers.
29 TechSparx Computer Training Center - 9880 205065
7. Define a class Area that has the following members:
Members Member Name Description
private variables side1, side2, radius Sides of rectangle and radius of circle
Two parameterized constructors
To assign values to sides and radius
public method rectArea() Calculates area of rectangle
public method circleArea() Calculates area of circle
public method main() Invokes all member methods and prints the results
8. Design a class with the following specifications:
Class Name: Employee
Data Members/Instance Variables: String name, float salary, DA, HRA, PF, gross, net
Member Functions:
void inputData(): to accept name and salary of an employee
void calculate(): to find the following-
DA = 20% of salary
HRA = 15% of salary
PF = 12% of salary
gross = salary + DA + HRA
net = gross – PF
void outputData(): to display all the information
9. Design a class to enter the monthly salary of a person, calculate and print the annual tax payable
by that person.
Annual Salary (Rs.) Tax Payable
Below 50,000 5% of annual salary
50,001 to 1,25,000 10% of annual salary
Above 1,25,001 20% of annual salary
10. Define a class Electricity described as below:
Data Members: Name, Consumer Number, Units consumed. Member methods:
i. To accept the name, consumer number, units consumed ii. To compute the electricity charges accordingly:
upto 100 units : 80 paise per unit for next 100 units : Rs. 1 per unit for more than 200 units : Rs. 1.25 per unit
iii. To display the details including name and consumer number
30 TechSparx Computer Training Center - 9880 205065
11. BSNL charges for using telephone from their consumers according to the calls made (per month) as per the following tariff:
Number of calls Charge
Up to 50 calls Rs. 1.50 per call
For next 100 calls Rs. 1 per call
For next 200 calls 90 paise per call
More than 350 calls 80 paise per call
However, monthly rental charge is Rs. 180/- per month for all the consumers for using Telephones. Write a program in Java to calculate monthly telephone bill indicating number of calls, monthly rental and total amount to be paid. Take number of calls as an input.
31 TechSparx Computer Training Center - 9880 205065
I am Saravanan.G, TechSparx is my start-up.
I have completed Bachelor of Engineering in Computer Science and working
as a Senior Software Engineer in a MNC
My passion is teaching!
My motto is to provide Corporate Level Training to
School and College Students
If you wanna learn the
ART of programming in C, C++, JAVA, J2EE easily!!!
Then feel free to call me @ 9880 205065 or mail me
I conduct classes in the following locations
Venkatesh Tutorials Wilson Garden, Bangalore – 560027
TechSparx Computer Training Center
#631, 1st floor, 1st Main Rd, AECS Layout - C Block,
Kundanhalli, Bangalore - 560037
For more information visit
www.techsparx.net
www.techsparx.webs.com