+ All Categories
Home > Documents > Java Question Bank Advanced

Java Question Bank Advanced

Date post: 02-Jun-2018
Category:
Upload: navigamer
View: 235 times
Download: 0 times
Share this document with a friend

of 18

Transcript
  • 8/10/2019 Java Question Bank Advanced

    1/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.netwww.techsparx.webs.com

    Saravanan.G

    http://www.techsparx.net/http://www.techsparx.net/http://www.techsparx.webs.com/http://www.techsparx.webs.com/http://www.techsparx.webs.com/http://www.techsparx.net/
  • 8/10/2019 Java Question Bank Advanced

    2/18

    1 TechSparx Computer Training Center - 9880 92 1111

    Modules

    Advanced

    Topic Number Programs Total Number of Programs

    Arrays 15 + 12 = 27

    84Strings 17 + 12 + 11 = 40

    Functions and Constructors 6

    Classes and Objects 11

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    3/18

    2 TechSparx Computer Training Center - 9880 92 1111

    Contents

    Modules ........................................................................................................................................................ 1

    Arrays ............................................................................................................................................................ 3

    Part 1One Dimensional Array ................................................................................................................ 3

    Part 2Searching and Sorting .................................................................................................................. 4

    Strings ........................................................................................................................................................... 5

    Part 1Operate on each character .......................................................................................................... 5

    Part 2Operate on each word ................................................................................................................. 6

    Part 3Operations on an Array of strings ............................................................................................... 7

    Functions and Constructors ........................................................................................................................ 11

    Classes and Objects ..................................................................................................................................... 13

    Output Programs ........................................................................................................................................ 16

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    4/18

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

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    5/18

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

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    6/18

    5 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 equalto second string or

    b.

    If first string is alphabetically greaterthan second string or

    c.

    If first string is alphabetically smallerthan 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

    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

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    7/18

    6 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: InputMohandas 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

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    8/18

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

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    9/18

    8 TechSparx Computer Training Center - 9880 92 1111

    TheoryJava 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

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    10/18

    9 TechSparx Computer Training Center - 9880 92 1111

    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 thisstring--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 thisString to lower case

    13 String toUpperCase()

    --Converts all of the characters in the thisString 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 oldCharin 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 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.

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    11/18

    10 TechSparx Computer Training Center - 9880 92 1111

    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 thisstring buffer

    3 StringBuffer append(String str)

    -- Appends the string str to thisstring buffer.

    4 StringBuffer insert(int offSet, String str)

    -- Inserts the string str into thisstring buffer at offSet index

    5 void setCharAt(int index, char ch)

    -- The character at the specified index of thisstring buffer is set to ch.6 StringBuffer delete(int begIndex, int endIndex)

    -- Removes the characters from begIndex(inclusive) to endIndex(exclusive) in

    thisStringBuffer.

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

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    12/18

    11 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 otherwiseBoolean 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 squarepublic static void findArea(int side)

    b.

    Function to find the area of rectanglepublic static void findArea(int length, int breadth)

    c.

    Function to find the area of circlepublic 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) : to compare the numeric value of two characters and

    print the character with the higher numeric value.

    c.

    void compare(String, String) : to compare the length of the two strings and print the

    longer of the two.

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    13/18

    12 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 = 5Output:

    @@@@@

    @@@@@

    iii)

    Output:

    *

    **

    ***

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    14/18

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

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    15/18

    14 TechSparx Computer Training Center - 9880 92 1111

    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 = grossPF

    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

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    16/18

    15 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

    ***** ***** ***** ***** *****

    http://www.techsparx.webs.com/http://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    17/18

    16 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

    http://www.techsparx.webs.com/http://techsparx.webs.com/studymaterials.htmhttp://techsparx.webs.com/studymaterials.htmhttp://techsparx.webs.com/studymaterials.htmhttp://www.techsparx.webs.com/
  • 8/10/2019 Java Question Bank Advanced

    18/18

    17 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

    [email protected]

    I conduct classes in the following locations

    Venkatesh TutorialsWilson Garden, Bangalore 560027

    TechSparx Computer Training Center#631, 1stfloor, 1st Main Rd, AECS Layout - C Block,

    Kundanhalli, Bangalore - 560037

    For more information visitwww.techsparx.net

    www.techsparx.webs.com

    http://www.techsparx.webs.com/mailto:[email protected]://www.techsparx.net/http://www.techsparx.net/mailto:[email protected]://www.techsparx.webs.com/

Recommended