TechSparx Java Tuitionstechsparx.webs.com/10th ICSE/JavaQuestionBank 5.3.pdf4 TechSparx Java...

25
TechSparxJavaTuitionsTechSparxJava TuitionsTechSparxJavaTuitionsTechSp arxJavaTuitionsTechSparxJavaTuitions TechSparxJavaTuitionsTechSparxJava TuitionsTechSparxJavaTuitionsTechSp arxJavaTuitionsTechSparxJavaTuitions TechSparxJavaTuitionsTechSparxJava TuitionsTechSparxJavaTuitionsTechSp arxJavaTuitionsTechSparxJavaTuitions TechSparxJavaTuitionsTechSparxJava TuitionsTechSparxJavaTuitionsTechSp arxJavaTuitionsTechSparxJavaTuitions TechSparxJavaTuitionsTechSparxJava TuitionsTechSparxJavaTuitionsTechSp arxJavaTuitionsTechSparxJavaTuitions TechSparxJavaTuitionsTechSparxJava TuitionsTechSparxJavaTuitionsTechSp TechSparx Java Tuitions “Better than a thousand days of diligent study is one day with a Great Teacher” Java Question Bank (version 5.3) http://techsparx.webs.com/ Saravanan.G

Transcript of TechSparx Java Tuitionstechsparx.webs.com/10th ICSE/JavaQuestionBank 5.3.pdf4 TechSparx Java...

Java Question Bank

TechSparxJavaTuitionsTechSparxJava

TuitionsTechSparxJavaTuitionsTechSp

arxJavaTuitionsTechSparxJavaTuitions

TechSparxJavaTuitionsTechSparxJava

TuitionsTechSparxJavaTuitionsTechSp

arxJavaTuitionsTechSparxJavaTuitions

TechSparxJavaTuitionsTechSparxJava

TuitionsTechSparxJavaTuitionsTechSp

arxJavaTuitionsTechSparxJavaTuitions

TechSparxJavaTuitionsTechSparxJava

TuitionsTechSparxJavaTuitionsTechSp

arxJavaTuitionsTechSparxJavaTuitions

TechSparxJavaTuitionsTechSparxJava

TuitionsTechSparxJavaTuitionsTechSp

arxJavaTuitionsTechSparxJavaTuitions

TechSparxJavaTuitionsTechSparxJava

TuitionsTechSparxJavaTuitionsTechSp

TechSparx Java Tuitions

“Better than a thousand days of diligent study is one day with a

Great Teacher”

Java Question Bank (version 5.3)

http://techsparx.webs.com/ Saravanan.G

2 TechSparx Java Tuitions - 9880 205065

Contents

Modularization ........................................................................................................................................ 3

Most Simplest ......................................................................................................................................... 4

Decision Making Statements ................................................................................................................... 5

Switch Statement .................................................................................................................................... 6

Looping Constructs .................................................................................................................................. 7

Numbers ................................................................................................................................................. 8

Series .................................................................................................................................................... 11

Patterns ................................................................................................................................................ 12

Strings ................................................................................................................................................... 14

One Dimensional Array .......................................................................................................................... 18

Searching and Sorting ............................................................................................................................ 19

Strings and Arrays.................................................................................................................................. 20

Classes, Objects, Functions and Constructors......................................................................................... 21

Two Dimensional Array.......................................................................................................................... 23

Output Programs ................................................................................................................................... 24

3 TechSparx Java Tuitions - 9880 205065

Modularization

Module 1 Java basics

Module 2 Most Simplest

Module 3 Decision Making Statements

Module 4 Switch Statement

Module 5 Looping Constructs

Numbers

Module 6 Series

Module 7 Patterns

Module 8 Strings

Module 9 One Dimensional Array

Module 10 Searching and Sorting

Module 11 Strings and Arrays

Module 12 Classes, Objects, Functions and

Constructors

Module 13 Exception Handling

Module 14 Encapsulation(Access Specifier)

Module 15 Scanner class

Module 16 Two Dimensional Array

Module 17 Output Programs

Module 18

Theoretical

-BufferedReader

-Input and Output Stream

-Wrapper class

-Math class

4 TechSparx Java Tuitions - 9880 205065

Most Simplest

1. Write a program (WAP) to print “Hello World”.

2. WAP to print your name.

3. WAP to add two numbers and display the given numbers and the sum.

4. WAP to find the difference of two numbers and display the given numbers and the difference.

5. WAP to find the product of two numbers and display the given numbers and the product.

6. WAP to find the quotient of two numbers and display the given numbers and the quotient.

7. WAP to find the remainder of two numbers and display the given numbers and the remainder.

8. WAP to find the sum, difference, product, quotient and remainder of two numbers.

9. WAP to find area of a square.

10. WAP to find area of a rectangle.

11. WAP to find area of a triangle whose sides are given.

12. WAP to find the area of a circle.

13. WAP to convert Fahrenheit to Celsius

14. WAP to convert Celsius to Fahrenheit.

15. WAP to calculate the simple interest.

16. WAP to calculate the compound interest.

Important formulas:

1. Fahrenheit = (Celsius * 1.8) + 32

2. Area of square = side * side

3. Area of Rectangle = length * breadth

4. Area of a circle = π * r * r

5. Area of a triangle = √(s(s-a)(s-b)(s-c)) where s = (a + b + c)/3

6. Roots of quadratic equation

X = (-b ± √(b2 – 4ac)) / (2a)

7. Simple Interest = (principal * rate * time) / 100

8. Compound Interest = principal * ( (1 + (rate/100))time - 1)

5 TechSparx Java Tuitions - 9880 205065

Decision Making Statements

1. Write a program to find the largest of two given numbers.

2. Write a program to find the smallest of two given numbers.

3. Write a program to find if the given number is an ODD number.

4. Write a program to find if the given number is an ODD or EVEN number.

5. Write a program to find if the given number is DIVISIBLE by 7(seven).

6. Write a program to find the largest of three given numbers.

7. Write a program to find the smallest of three given numbers.

8. Write a program to find the real roots of a quadratic equation. (ax2 + bx + c = 0)

9. Write a program to print the grade based on the marks obtained according to the following

table

Marks Grade

>=80 A

60 to 79 B

50 to 59 C

40 to 49 D

<40 E

10. Write a program to print the day of week. (using else if ladder)

Example: day = 1 dayOfWeek = “Monday”

day = 2 dayOfWeek = “Tuesday”

11. Write a program to print the month of the year. (using else if ladder)

Example: month = 1 --> monthOfYear = "January"

month = 2 --> monthOfYear = "February"

12. 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.

13. Write a program to check if the given character is a vowel.

14. 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

6 TechSparx Java Tuitions - 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 Sunday, 2 to Monday… 7 to Saturday)

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.

4. 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.

5. 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.

6. Using a switch statement, write a MENU driven program to calculate the AREA of square, rectangle,

triangle and circle.

7 TechSparx Java Tuitions - 9880 205065

Looping Constructs

1. Write a program to print 5 stars.

2. Write a program to print n natural numbers.

3. Write a program to print all even numbers from 50 to 500 (inclusive of 50 and 500)

8 TechSparx Java Tuitions - 9880 205065

Numbers

For Loop

1. Write a program to

a. Calculate the nth repunit

b. Calculate and display first n repunits.

2. WAP

a. Calculate nth mersenne number.

b. Calculate and display first n Mersenne number (binary repunits).

3. Write a program to display the divisors/factors of a given number.

4. Write a program to calculate the sum of factors of a given number.

5. Write a program to check if two given numbers are amicable.

6. Write a program to find the

a. Factorial of n.

b. Double Factorial of n.

c. Triple Factorial of n.

7. Write a program to check if the given number is a Perfect square.

8. Write a program to find if the given number is one of the following:

a. Perfect Number

b. Deficient Number

c. Abundant Number

9. Write a program to check if the given number is a Prime Number.

10. Write a program to display all the prime numbers from 1 to n.

11. Write a program to display first 20 prime numbers.

12. Write a program to find the Primorial of the given number.

While Loop

13. Write a program to check if the given number is a palindrome.

14. Write a program to check if the given number is an Armstrong/Narcissistic Number.

15. Write a program to convert a binary number to decimal number.

16. Write a program to convert a Decimal Number to Binary.

17. Write a program that accepts a number and checks whether it is an AUTOMORPHIC Number.

18. Write a program to calculate the LCM and HCF/GCD of two given numbers.

19. Write a program to check if the given number is a SPECIAL number.

20. Write a program to print first 10 buzz numbers.

21. Write a program to check if the given number is a MAGIC Number

9 TechSparx Java Tuitions - 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: 1, 2, 3, 5, 7, 11, 13, 17, 19 …

5. Multi-Factorial:

a. n- Factorial -> n! = n * (n-1) * (n-2) * (n-3) * (n-4) * ……* 2 * 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)

6. 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

7. 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 …

8. 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.

10 TechSparx Java Tuitions - 9880 205065

9. Deficient, Perfect or Abundant Number: Suppose you take a positive integer n and add its

positive divisors, 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

10. Automorphic Number: is one which is present at the extreme right of its square.

Eg: 52 = 25, 62 = 36, 252 = 625 etc.

11. Amicable Numbers: Two numbers are said to be amicable if and only if the sum of the factors of

num1 is equal to num2 and the sum of 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

12. 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

13. Buzz Number: A number which is either divisible by 7 or ends with 7.

For Example: 67, 49, 37, 21 etc

14. Magic Number: A number whose sum of the digits until the sum is a single digit is equal to 1.

For Example:

55

5 + 5 = 10, further find the sum of 10 also since it is not a single digit number 1 + 0 = 1

11 TechSparx Java Tuitions - 9880 205065

Series

1. Program to find the sum and print the Series 1 2 3 4 5 . . . . n terms

2. Program to find the sum and print the Series 2 4 6 8 . . . . n terms

3. Program to find the sum and print the Series 1 2 4 8 16 32 . . . . n terms

4. Program to find the sum and print the Series 0 1 3 7 15 31 . . . . n terms

5. Program to find the sum and print the Series 2 3 5 7 11 13 17 . . . . n terms

6. Program to find the sum and print the Series 1 4 9 16 25 . . . . n terms

7. Program to find the sum and print the Series 100 1000 10000 . . . . n terms

8. Program to find the sum 12 + 22 + 32 + 42 +.... n terms

9. Program to find the sum of (1*2) + (2*3) + (3*4) + (4*5) + . . . . n terms

10. Program to print the Fibonacci series. 0, 1, 1, 2, 3, 5 ... n terms

11. Program to print the Tribonacci series. 0, 1, 1, 2, 4, 7 ... n terms

12. Program to find the sum of the series x + x3 + x5 + x7 + ... n terms

13. Program to find the sum of the series x - x3 + x5 - x7 + ... n terms

14. Program to find the sum of series 1! + 3! + 5! + .... n terms

15. Program to find the sum of series 1! - 3! + 5! - .... n terms

16. Program to find the sum of series (x1)/(1!) - (x3)/(3!) + (x5)/(5!) - .... n terms

17. Program to find the sum of series 1 - (x2)/(2!) + (x4)/(4!) - .... n terms

18. Program to find the sum of series 1 + (1+2) + (1+2+3) + (1+2+3+4) + . . . + (1+2+3+4+...+n)

19. Program to find the sum of series 1 - (1*2) + (1*2*3) - (1*2*3*4) + . . . - (1*2*3*4*...*n)

20. 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))

12 TechSparx Java Tuitions - 9880 205065

Patterns

1. Right Angled Triangle

First

Pattern

Second

Pattern

Third

Pattern

Fourth

Pattern

Fifth

Pattern

Sixth

Pattern

*

* *

* * *

* * * *

* * * * *

1

1 1

1 1 1

1 1 1 1

1 1 1 1 1

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

5

4 4

3 3 3

2 2 2 2

1 1 1 1 1

5

5 4

5 4 3

5 4 3 2

5 4 3 2 1

2. Reversed Right Angled Triangle

Eleventh

Pattern

Twelfth

Pattern

Thirteenth

Pattern

Fourteenth

Pattern

Fifteenth

Pattern

Sixteenth

Pattern

* * * * *

* * * *

* * *

* *

*

2 2 2 2 2

2 2 2 2

2 2 2

2 2

2

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

5 5 5 5 5

4 4 4 4

3 3 3

2 2

1

1 1 1 1 1

2 2 2 2

3 3 3

4 4

5

5 4 3 2 1

5 4 3 2

5 4 3

5 4

5

3. 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

13 TechSparx Java Tuitions - 9880 205065

4. Equilateral Triangle and Rhombus with spaces in between stars

Thirty First Pattern Thirty Second Pattern Thirty Third Pattern

*

* *

* * *

* * * *

* * * * *

* * * * *

* * * *

* * *

* *

*

*

* *

* * *

* * * *

* * * * *

* * * *

* * *

* *

*

5. Equilateral Triangle and Rhombus without spaces in between stars

Forty First Pattern Forty Second Pattern Forty Third Pattern

*

***

*****

*******

*********

*********

*******

*****

***

*

*

***

*****

*******

*********

*******

*****

***

*

6. 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

7. 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

14 TechSparx Java Tuitions - 9880 205065

Strings

1. WAP to check if the given string is palindrome. 2. WAP 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

3. WAP to find the frequency of the given character in a given string.

4. WAP to find the frequency of each character in a given string.

5. WAP to input a string and convert upper case to lower case and vice versa.

6. 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”)

7. WAP to count the number of words and print each word in different line. 8. WAP to find the frequency of the given word in the given string. 9. WAP to input a string and display the longest word along with the number of characters.

10. WAP 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.

11. WAP a program to input a string "India is my country” and print out the following sentences

a. country my is India

b. aidnI si ym yrtnuoc 12. WAP to input a name and print its short form

Example: Input – Mohandas Karamchand Gandhi Output - M.K.Gandhi

13. 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.

14. 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 ARATHI AND AMAN” Input Character: ‘A’ Output: AMIT, AND, ARATHI, AND, AMAN

15. WAP 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”

15 TechSparx Java Tuitions - 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 yourName = new String(“I do not know your name”);

Accessor Methods: Methods used to obtain information about an object are known as assessor methods.

Character class Sl.No. 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

String class Sl.No. Function Prototype 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

7 str1 + str2 Concatenation operator(i.e., +), achieves same as concat method

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 Returns a new string resulting from replacing all

16 TechSparx Java Tuitions - 9880 205065

newChar) 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 static String valueOf(all types) Returns 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 of characters.

StringBuffer class 1 int capacity() Returns the maximum number of characters that can

be entered in the current string object(this) i.e., its capacity

2 int length() int capacity() StringBuffer append(String arg0) StringBuffer insert(int arg0, CharSequence arg1) void setCharAt(int arg0, char arg1) StringBuffer delete(int arg0, int arg1) void setLength(int arg0) StringBuffer reverse() StringBuffer replace(int arg0, int arg1, String arg2) char charAt(int arg0) int indexOf(String arg0) String toString() String substring(int arg0) String substring(int arg0, int arg1)

17 TechSparx Java Tuitions - 9880 205065

18 TechSparx Java Tuitions - 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 find the largest and smallest element of an array.

3. Write a program that quadruples every element of an array of size n.

4. Write a program to delete duplicate elements from an array of size 10. And display the resultant

array.

5. 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 appropriate

message after comparison.

6. Write a program that reverses an array and stores it in a new array.

7. Write a program that reverses an array and stores it in the same array.

19 TechSparx Java Tuitions - 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 algorithm, 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 algorithm, 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 selection sort technique and display the names before and after sorting.

11. Write a program to input 10 states and its capital. Display the states and capital in the sorted

order of states.

20 TechSparx Java Tuitions - 9880 205065

Strings and Arrays

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 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.

3. 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.

4. Write a program to sort the words of a string in ascending order. 5. 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.

6. 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.

21 TechSparx Java Tuitions - 9880 205065

Classes, Objects, Functions and Constructors

1. Write a program using a class to calculate the volume, surface area and the diagonal of cuboid.

2. Design a class with the following specifications:

Class Name: Emp

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

3. 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

4. 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

22 TechSparx Java Tuitions - 9880 205065

5. 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.

6. 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

7. 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)

8. 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.

9. 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.

10. Write a program to implement a class Numbers having fields storing 3 integers and methods to

find maximum, minimum and average of 3 numbers.

23 TechSparx Java Tuitions - 9880 205065

Two Dimensional Array

1. Write a program to read the elements of a 3 x 3 matrix from the user.

2. Write a program to display the elements of a 3 x 3 matrix to the user.

3. Write a program to input two 3 x 3 matrix, find the sum of the matrices and then display the

given matrices as well as the resultant matrix.

4. Write a program to input two 3 x 3 matrix, find the difference of the matrices and then display

the given matrices as well as the resultant matrix.

5. Write a program to input two 3 x 3 matrix, find the product of the matrices and then display the

given matrices as well as the resultant matrix.

6. Write a program to input two 3 x 3 matrix, and perform addition, subtraction and multiplication

of the two matrices based on the user’s choice.

7. Write a program to replace all the even numbers of 4 x 4 matrix with their squares and odd

numbers with their square roots.

8. Write a program to accept the elements of (m x n) matrix and find the largest and smallest

element of the matrix and print their position.

9. Write a program and do the following:

a. Sum of each row.

b. Sum of each column.

c. Sum of principle diagonal elements.

d. Sum of the other diagonal elements.

e. Sum of all the elements

f. Sum of boundary elements

10. Write a program to accept a 4 x 4 matrix. Print all the prime numbers present in it along with its

location.

Eg :

5 4 3 2

6 5 4 3

7 8 9 6

1 2 3 4

Output:

PrimeNumber RowNo ColNo 5 0 0 2 0 3

11. Write a program

a. To find the transpose of a given m x n matrix b. To check if the given matrix is an identity matrix

24 TechSparx Java Tuitions - 9880 205065

Output Programs

25 TechSparx Java Tuitions - 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!

I love teaching programming languages like C, C++, Java, J2EE and Android

My motto is to provide Corporate Level Training to School and Engineering

College Students.

If you want to learn the

ART of programming in C, C++, JAVA, J2EE and Android easily!!!

Then feel free to call me@ 9880 205065 or mail me [email protected]

I conduct classes in the following locations

Venkatesh Tutorials

Wilson Garden, Bangalore - 27

Srushti Degree College

#92, 2nd Floor, Karthik Nagar, (next to Café Coffee Day, before HDFC Bank)

Outer Ring Road, Marthahalli, Bangalore – 37

For more information visit www.techsparx.webs.com