Java Question Bank Advanced
Java Question Bank Advanced
Modules
Advanced
Topic
Arrays
Strings
Functions and Constructors
Classes and Objects
Number Programs
15 + 12 = 27
17 + 12 + 11 = 40
6
11
84
Contents
Modules ........................................................................................................................................................ 1
Arrays ............................................................................................................................................................ 3
Part 1 One Dimensional Array................................................................................................................ 3
Part 2 Searching and Sorting .................................................................................................................. 4
Strings ........................................................................................................................................................... 5
Part 1 Operate on each character .......................................................................................................... 5
Part 2 Operate on each word ................................................................................................................. 6
Part 3 Operations on an Array of strings ............................................................................................... 7
Functions and Constructors ........................................................................................................................ 11
Classes and Objects ..................................................................................................................................... 13
Output Programs ........................................................................................................................................ 16
Arrays
Part 1 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 n integers and find the sum of all the even elements.
6. Write a program to input an array of n 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 of size n 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.
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
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.
Strings
Part 1 Operate on each character
1. Write a program to create 3 strings in three different ways and display the length of each string.
2. Write a program to statically declare and initialize a string and display each character along with its
index in different lines.
3. Write a program to accept a string from user using BlueJ method and display characters at even
index along with its index in different lines.
4. Write a program to accept a string from user and 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 given 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)
(trace)
3. Write a program to find the frequency of the given word in the given string.
4. Write a program to input a string and display the longest word along with the number of characters.
5. Write a program to input a string and display the smallest word along with the number of
characters.
6. 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.
7. Write a program to initialize a string "India is my country and convert it as aidnI si ym yrtnuoc
8. Write a program to initialize a string "India is my country and convert it as country my is India
9. Write a program to input a name and print its short form
Example:
10. 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.
11. 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
12. Write a program to accept a string and an encoding value. Display a new string after encoding. If
encoding value is equal to -3, each character moves three characters behind.
Some more Examples:
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, the new string is:
XYXZRP
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
static Boolean isUppercase(char ch)
static Boolean isLowercase(char ch)
static char toUpperCase(char ch)
static char toLowerCase(char ch)
1
2
3
4
Description
Returns true if ch is an Upper Case character
Returns true if ch is a Lower Case character
Converts ch to upper case and returns that character
Converts ch to lower case and returns that character
String
1
Return Type
int
char
int
int
Int
String
boolean
boolean
10
boolean
11
boolean
12
String
13
String
14
String
15
String
16
String
17
String
18
static String
19
char[]
StringBuffer
Return Type
1
int length()
-- Returns the length (character count) of this string buffer.
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
StringBuffer append(String str)
-- Appends the string str to this string buffer.
StringBuffer insert(int offSet, String str)
-- Inserts the string str into this string buffer at offSet index
void setCharAt(int index, char ch)
-- The character at the specified index of this string buffer is set to ch.
StringBuffer delete(int begIndex, int endIndex)
-- Removes the characters from begIndex(inclusive) to endIndex(exclusive) in
this StringBuffer.
void setLength(int newLength)
-- Sets the length of this String buffer.
StringBuffer reverse()
-- The character sequence contained in this string buffer is replaced by the
reverse of the sequence.
StringBuffer replace(int begIndex, int endIndex, String str)
-- Replaces the characters in a this StringBuffer with characters in the string str.
char charAt(int index)
-- Returns the character at index of this string buffer.
int indexOf(String str)
-- Returns the index within this string of the first occurrence of the substring str.
String toString()
-- Converts to a string representing the data in this string buffer.
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.
String substring(int begIndex, int endIndex)
-- Returns a new String that contains a subsequence of characters currently
contained in this StringBuffer.
3
4
5
6
7
8
9
10
11
12
13
14
10
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. Write the main function to invoke the checkEven() function.
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.
4.
5.
11
6.
c. void polygon()
Example:
i)
Input value of n = 2, ch = O
Output:
OO
OO
ii)
Input value of x = 2, y = 5
Output:
@@@@@
@@@@@
iii)
Output:
*
**
***
12
2.
3.
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
initialize, find maximum, minimum, average of 3 numbers and display them.
13
7.
Members
private variables
Two parameterized
constructors
public method
public method
public method
8.
Member Name
side1, side2, radius
Description
Sides of rectangle and radius of circle
To assign values to sides and radius
rectArea()
circleArea()
main()
9.
Design a class to enter the monthly salary of a person, write appropriate methods to calculate
and print the annual tax payable by that person.
Annual Salary (Rs.)
Tax Payable
Less than or equal to 50,000
5% of annual salary
50,000 to 1,25,000
10% of annual salary
Above 1,25,000
20% of annual salary
10.
14
11.
void compute() :
void display()
Bike No.
Phone No.
Name
No. of days
Charge
*****
*****
*****
*****
*****
15
Output Programs
You can download the latest Output Programs pdf document here
http://techsparx.webs.com/studymaterials.htm
16
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, Groovy and Grails easily!!!
Then feel free to call me @ 9880 92 1111 or mail me
[email protected]
I conduct classes in the following locations
Venkatesh Tutorials
Wilson Garden, Bangalore 560027
17