0% found this document useful (0 votes)
10 views7 pages

OOP_Lab_part_II_Practice_questions_methods,_arrays,_characters

The document outlines various Java programming exercises divided into sections on methods, arrays, and string manipulation. It includes tasks such as defining methods for mathematical operations, checking for prime numbers, manipulating arrays, and working with strings to find palindromes and anagrams. Each section contains specific programming challenges aimed at enhancing Java programming skills.

Uploaded by

gueshberhe97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

OOP_Lab_part_II_Practice_questions_methods,_arrays,_characters

The document outlines various Java programming exercises divided into sections on methods, arrays, and string manipulation. It includes tasks such as defining methods for mathematical operations, checking for prime numbers, manipulating arrays, and working with strings to find palindromes and anagrams. Each section contains specific programming challenges aimed at enhancing Java programming skills.

Uploaded by

gueshberhe97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Contents

Java Methods- part I................................................................................................................................1


Java Methods – part II.............................................................................................................................2
Java Arrays – Part I..................................................................................................................................3
Java Arrays – part II.................................................................................................................................4
Java Characters and Strings – part I.........................................................................................................5
Java characters and strings – part II.........................................................................................................6

Java Methods- part I


1.
Define two methods to print the maximum and the minimum number respectively among three
numbers entered by user.

2.
Define a program to find out whether a given number is even or odd.

3.
A person is elligible to vote if his/her age is greater than or equal to 18. Define a method to find
out if he/she is elligible to vote.

4.
Write a program to print the sum of two numbers entered by user by defining your own method.

5.
Define a method that returns the product of two numbers entered by user.

6.
Write a program to print the circumference and area of a circle of radius entered by user by
defining your own method.

7.
Define a method to find out if number is prime or not.

8.
Write a program which will ask the user to enter his/her marks (out of 100). Define a method that
will display grades according to the marks entered as below:
Marks Grade
91-100 AA
81-90 AB
71-80 BB
61-70 BC
51-60 CD
41-50 DD
<=40 Fail

9.
Write a program to print the factorial of a number by defining a method named 'Factorial'.
Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-
4! = 1*2*3*4 = 24
3! = 3*2*1 = 6
2! = 2*1 = 2
Also,
1! = 1
0! = 0

Java Methods – part II


1.
Print the multiplication table of 15 using recursion.

2.
Define a method to print the prime factors of a number.

3.
Using recursion, define a method to know nth term of a Fibonacci series.
Nth term of Fibonacci series is
F(n) = F(n-1)+(n-2)
F(0) = 0
F(1) = 1
F(2) = F(1)+(0) = 1+0 = 1
F(3) = F(2)+(1) = 1+1 = 2
F(4) = F(3)+(2) = 2+1 = 3

4.
Define a method named 'perfect' that determines if parameter number is a perfect number. Use
this function in a program that determines and prints all the perfect numbers between 1 and 1000.
[An integer number is said to be "perfect number" if its factors, including 1(but not the number
itself), sum to the number. E.g., 6 is a perfect number because 6=1+2+3].

5.
Define a method to calculate power of a number raised to other i.e. ab using recursion where the
numbers 'a' and 'b' are to be entered by the user

6.
Write a program that takes as input your gross salary and your total saving and uses another
function named taxCalculator() to calculate your tax. The taxCalculator() function takes as
parameters the gross salary as well as the total savings amount. The tax is calculated as follows:
(a) The savings is deducted from the gross income to calculate the taxable income. Maximum
deduction of savings can be Rs. 100,000, even though the amount can be more than this.
(b) For up to 100,000 as taxable income the tax is 0 (Slab 0); beyond 100,000 to 200,000 tax is
10% of the difference above 100,000 (Slab 1); beyond 200,000 up to 500,000 the net tax is the
tax calculated from Slab 0 and Slab 1 and then 20% of the taxable income exceeding 200,000
(Slab 2); if its more than 500,000, then the tax is tax from Slab 0, Slab 1, Slab 2 and 30% of the
amount exceeding 500,000.

Java Arrays – Part I


1.
Take 10 integer inputs from user and store them in an array and print them on screen.

2.
Take 10 integer inputs from user and store them in an array. Again ask user to give a number.
Now, tell user whether that number is present in array or not.

3.
Take 20 integer inputs from user and print the following:
number of positive numbers
number of negative numbers
number of odd numbers
number of even numbers
number of 0s.

4.
Take 10 integer inputs from user and store them in an array. Now, copy all the elements in an
another array but in reverse order.

5.
Write a program to find the sum and product of all elements of an array.

6.
Initialize and print all elements of a 2D array.

7.
Find largest and smallest elements of an array.

8.
Write a program to check if elements of an array are same or not it read from front or back. E.g.-

9.
Take an array of 10 elements. Split it into middle and store the elements in two dfferent arrays.
E.g.-
INITIAL array :
58 24 13 15 63 9 8 81 1 78

After spliting :
58 24 13 15 63
9 8 81 1 78

10.
Consider an integer array, the number of elements in which is determined by the user. The
elements are also taken as input from the user. Write a program to find those pair of elements
that has the maximum and minimum difference among all element pairs.

11.
If the input array is [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60], your program should be able to
find that the subarray lies between the indexes 3 and 8.

Java Arrays – part II


1.
Take an array of length n where all the numbers are nonnegative and unique. Find the element in
the array possessing the highest value. Split the element into two parts where first part contains
the next highest value in the array and second part hold the required additive entity to get the
highest value. Print the array where the highest value get splitted into those two parts.
Sample input: 4 8 6 3 2
Sample output: 4 6 2 6 3 2

2.
Write a program to shift every element of an array to circularly right. E.g.-
INPUT : 1 2 3 4 5
OUTPUT : 5 1 2 3 4

3.
Initialize a 2D array of 3*3 matrix. E.g.-
1 2 3
4 5 6
7 8 9

Check if the matrix is symmetric or not.

4.
Sorting refers to arranging data in a particular format. Sort an array of integers in ascending
order. One of the algorithm is selection sort. Use below explanation of selection sort to do this.
INITIAL ARRAY :
2 3 1 45 15
First iteration : Compare every element after first element with first element and if it is larger
then swap. In first iteration, 2 is larger than 1. So, swap it.
1 3 2 45 15
Second iteration : Compare every element after second element with second element and if it is
larger then swap. In second iteration, 3 is larger than 2. So, swap it.
1 2 3 45 15
Third iteration : Nothing will swap as 3 is smaller than every element after it.
1 2 3 45 15
Fourth iteration : Compare every element after fourth element with fourth element and if it is
larger then swap. In fourth iteration, 45 is larger than 15. So, swap it.
1 2 3 15 45

Java Characters and Strings – part I


1.
Write a program to print a string entered by user.

2.
Write a program to input and display the sentence I love candies.

3.
Write a program to find the length of the string "refrigerator".

4.
Write a program to check if the letter 'e' is present in the word 'Umbrella'.

5.
Write a program to check if the word 'orange' is present in the "This is orange juice".

6.
Write a program to find the first and the last occurence of the letter 'o' and character ',' in "Hello,
World".

7.
Write a program that takes your full name as input and displays the abbreviations of the first and
middle names except the last name which is displayed as it is. For example, if your name is
Robert Brett Roser, then the output should be R.B.Roser.

8.
Write a program to find the number of vowels, consonents, digits and white space characters in a
string.

9.
Write a program to delete all consonents from the string "Hello, have a good day".

10.
Input a string of alphabets. Find out the number of occurrence of all alphabets in that string. Find
out the alphabet with maximum occurrence.

Java Characters and strings


1.
Write a program to check if a given string is a Palindrome.
A palindrome reads same from front and back e.g.- aba, ccaacc, mom, etc.

2.
Write a program to find out the largest and smallest word in the string "This is an umbrella".

3.
Write down the names of 10 of your friends in an array and then sort those in alphabetically
ascending order.

4.
Write a program to check if the two strings entered by user are anagrams or not. Two words are
said to be anagrams if the letters of one word can be rearranged to form the other word. For
example, jaxa and ajax are anagrams of each other.

5.
Input a string which contains some palindrome substrings. Find out the position of palindrome
substrings if exist and replace it by *. (For example if input string is “bob has a radar plane” then
it should convert in “*** has a ***** plane”.

6.
Write a program to replace a given substring in a sentence with another string. For example, in
the sentence, ” A batman with bat” if we replace ”bat” with ”snow”, the new sentence should be
printed as ”A snowman with snow”.

7.
Write a program to reverse individual words in a string, where each word may be delimited by a
dot, comma, space or tab, like www.google.com should become www.elgoog.moc.

Java characters and strings – part II


1.
Write a program to check if a given string is a Palindrome.
A palindrome reads same from front and back e.g.- aba, ccaacc, mom, etc.

2.
Write a program to find out the largest and smallest word in the string "This is an umbrella".

3.
Write down the names of 10 of your friends in an array and then sort those in alphabetically
ascending order.

4.
Write a program to check if the two strings entered by user are anagrams or not. Two words are
said to be anagrams if the letters of one word can be rearranged to form the other word. For
example, jaxa and ajax are anagrams of each other.
5.
Input a string which contains some palindrome substrings. Find out the position of palindrome
substrings if exist and replace it by *. (For example if input string is “bob has a radar plane” then
it should convert in “*** has a ***** plane”.

6.
Write a program to replace a given substring in a sentence with another string. For example, in
the sentence, ” A batman with bat” if we replace ”bat” with ”snow”, the new sentence should be
printed as ”A snowman with snow”.

7.
Write a program to reverse individual words in a string, where each word may be delimited by a
dot, comma, space or tab, like www.google.com should become www.elgoog.moc.

You might also like