Você está na página 1de 18

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

Java Question Bank Advanced (Version 14.04)


www.techsparx.net
www.techsparx.webs.com
Saravanan.G

Modules
Advanced
Topic
Arrays
Strings
Functions and Constructors
Classes and Objects

Number Programs
15 + 12 = 27
17 + 12 + 11 = 40
6
11

Total Number of Programs

84

TechSparx Computer Training Center - 9880 92 1111

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

TechSparx Computer Training Center - 9880 92 1111

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.

TechSparx Computer Training Center - 9880 92 1111

Part 2 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

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.

TechSparx Computer Training Center - 9880 92 1111

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)

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
BLUEJ
BLUEJ
BBBBB
BLUEJ
BLUE
L L L L
BLUEJ
BLU
UUU
BLUEJ
BL
EE
BLUEJ
B
J
16. Write a program to accept a word and convert the entire word into lowercase. Display the new
word by replacing only the vowels with the character following it.
Sample Input
:
computer
Sample Output
:
cpmpvtfr
17. Write a program to accept a string. Convert the string to uppercase. Count and output the number
of double letter sequences that exist in the string. Also, display the double letter sequences.
Sample Input: SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE

TechSparx Computer Training Center - 9880 92 1111

Part 2 Operate on each word


1. Write a program to count the number of words.
2. Write a program to print each word in different line and also count the number of words.

(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:

Input Mohandas Karamchand Gandhi


Output - M.K.Gandhi

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

TechSparx Computer Training Center - 9880 92 1111

Part 3 Operations on an Array of strings


1. Write a program to accept the name and marks of n students. Store it in two arrays and then display
them in a tabular format.
2. Write a program to read the name and marks of n students and find the highest and lowest scorer
along with the position.
3. 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.
4. Write a program to read the name and marks of n students and bubble sort them in ascending order
of their marks.
5. Write a program to read the name and marks of n students and bubble sort them in ascending order
of their names.
6. Write a program to sort the characters of a string in the ascending order and display the sorted
string (the string in which all the characters are in ascending order).
7. 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: If one word is FOWL and the other word is WOLF which is formed with the combinations of the letters present in
the first word then FOWL and WOLF are ANAGRAMS.

8. Write a program to sort the words of a string in ascending order.


9. Program to accept a string and sort the words in the string based on the number of vowels in each
word and display them along with number of vowels.
10. 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.
11. Input a sentence and output the words that occur more than once in the sentence along with their
frequency of occurrence.

TechSparx Computer Training Center - 9880 92 1111

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

TechSparx Computer Training Center - 9880 92 1111

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[]

Function Signature and Description


length()
--Returns the length of the string
charAt(int index)
--Returns the character at the specified index
indexOf(char ch)
--Returns the index of the first occurrence of the character ch
lastIndexOf(char ch)
--Returns the index of the last occurrence of the character ch
compareTo(String str)
--Compares two strings lexicographically.
concat(String str)
--Concatenates the specified string to the end of this string
--Concatenation operator(i.e., +), achieves same as concat method (str1 + str2)
startsWith(String str)
--Tests if the this string starts with str
endsWith(String str)
--Tests if the this string ends with str
equals(String str)
--Compares the this string to the specified object str
equalsIgnoreCase(String str)
--Compares the this string to the specified object str ignoring the case
toLowerCase()
--Converts all of the characters in the this String to lower case
toUpperCase()
--Converts all of the characters in the this String to upper case
toString()
--Returns the String itself
replace(char oldChar, char newChar)
--Returns a new string resulting from replacing all occurrences of oldChar in the
this string with newChar
substring(int begIndex, int endIndex)
--Returns a substring starting from begIndex till endIndex. Inclusive of begIndex
but exclusive of endIndex.
trim()
--Removes white space from both ends of the this String
valueOf(all types)
--Returns the string representation of the passed argument. Eg: 12 represented
as 12
toCharArray()
--Converts this string into an array of characters and returns the array.
TechSparx Computer Training Center - 9880 92 1111

StringBuffer
Return Type
1

Function Signature and Description

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

TechSparx Computer Training Center - 9880 92 1111

Functions and Constructors


1.

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.

Write a program having the following functions


a. Overloaded function which accepts two integers and finds the greater of two numbers and
displays it.
b. Overloaded 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.

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)

5.

Design a class to overload a function compare() as follows:


a. void compare(int, int)
:
to compare two integer values and print the greater of
the two integers
b. void compare(char, char)

c. void compare(String, String) :

11

to compare the numeric value of two characters and


print the character with the higher numeric value.
to compare the length of the two strings and print the
longer of the two.

TechSparx Computer Training Center - 9880 92 1111

6.

Design a class to overload a function polygon() as follows:


a. void polygon(int n, char ch) :
with one integer and one character type argument to
draw a filled square of side n using the character stored
in ch.
b. void polygon(int x, int y)

with two integer arguments that draws a filled rectangle


of length x and breadth y, using the symbol @

c. void polygon()

with no argument that draws a filled triangle shown


below.

Example:
i)
Input value of n = 2, ch = O
Output:
OO
OO
ii)

Input value of x = 2, y = 5
Output:
@@@@@
@@@@@

iii)

Output:
*
**
***

12

TechSparx Computer Training Center - 9880 92 1111

Classes and Objects


1.

Define a class Student with the following members:


a. Member variables: name, age, marks
b. Member method:
i. main() method which instantiates two objects and displays them.

2.

Define a class Student with the following members:


a. Member variables: name, age, marks
b. Member Methods:
i. main() method which instantiates two object and calls the other two functions
ii. initialize() 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
iii. display() method which displays all the member variables
iv. main() method which creates an 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
initialize, find maximum, minimum, average of 3 numbers and display them.

13

TechSparx Computer Training Center - 9880 92 1111

7.

Define a class Area that has the following members:

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

Calculates area of rectangle


Calculates area of circle
Invokes all member methods and prints the
results

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 followingDA = 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, 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.

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

14

TechSparx Computer Training Center - 9880 92 1111

11.

Define a class called Mobike with following specifications:


Instance Variables:
int bno
:
to store the bike number
int phno
:
to store the phone number of the customer
String name
:
to store the name of the customer
int days
:
to store the number of days the bike is taken out on rent
int charge
:
to calculate and store the rental charge
Member Methods:
void input()
:

to input and store the details of the customer

void compute() :

to compute the rental charge


The rent for a mobike is charged on the following basis:
For first five days
:
Rs. 500 per day
For next five days
:
Rs. 400 per day
Rest of the days
:
Rs. 200 per day

void display()

to display the details in the following format

Bike No.

Phone No.

Name

No. of days

Charge

*****

*****

*****

*****

*****

15

TechSparx Computer Training Center - 9880 92 1111

Output Programs
You can download the latest Output Programs pdf document here
http://techsparx.webs.com/studymaterials.htm

16

TechSparx Computer Training Center - 9880 92 1111

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, Groovy and Grails easily!!!
Then feel free to call me @ 9880 92 1111 or mail me
tech253545@gmail.com
I conduct classes in the following locations
Venkatesh Tutorials
Wilson Garden, Bangalore 560027

TechSparx Computer Training Center


st

#631, 1 floor, 1st Main Rd, AECS Layout - C Block,


Kundanhalli, Bangalore - 560037

For more information visit


www.techsparx.net
www.techsparx.webs.com

17

TechSparx Computer Training Center - 9880 92 1111

Você também pode gostar