0% found this document useful (0 votes)
38 views8 pages

COM121 Java Practice

The document contains a comprehensive list of Java programming practice questions, covering various topics such as arithmetic operations, string manipulation, array handling, and control structures. Each question includes test data and expected output to guide learners in implementing solutions. The exercises are designed to enhance programming skills and understanding of Java concepts.

Uploaded by

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

COM121 Java Practice

The document contains a comprehensive list of Java programming practice questions, covering various topics such as arithmetic operations, string manipulation, array handling, and control structures. Each question includes test data and expected output to guide learners in implementing solutions. The exercises are designed to enhance programming skills and understanding of Java concepts.

Uploaded by

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

JAVA PROGRAMMING PRACTICE QUESTIONS

1. Write a Java program to print the sum of two numbers.


Test Data:
74 + 36
Expected Output :
110
2. Write a Java program to print the result of the following operations.
Test Data:
a. -5 + 8 * 6
b. (55+9) % 9
c. 20 + -3*5 / 8
d. 5 + 15 / 3 * 2 - 8 % 3
Expected Output :
43
1
19
13
3. Write a Java program that takes two numbers as input and display the product of two numbers.
Test Data:
Input first number: 25
Input second number: 5
Expected Output :
25 x 5 = 125
4. Write a Java program to print the sum (addition), multiply, subtract, divide and remainder of two
numbers.
Test Data:
Input first number: 125
Input second number: 24
Expected Output :
125 + 24 = 149
125 - 24 = 101
125 x 24 = 3000
125 / 24 = 5
125 mod 24 = 5
5. Write a Java program that takes a number as input and prints its multiplication table up to 10.
8x1=8
8 x 2 = 16
8 x 3 = 24
...
8 x 10 = 80
6. Write a Java program to print the area and perimeter of a circle.
Test Data:
Radius = 7.5
Expected Output
Perimeter is = 47.12388980384689
Area is = 176.71458676442586
7. Write a Java program to print the area and perimeter of a rectangle.
Test Data:
Width = 5.5 Height = 8.5

Expected Output
Area is 5.6 * 8.5 = 47.60
Perimeter is 2 * (5.6 + 8.5) = 28.20
8. Write a Java program to add two binary numbers.
Input Data:
Input first binary number: 10
Input second binary number: 11
Expected Output

Sum of two binary numbers: 101


9. Write a Java program to convert a decimal number to binary number.
Input Data:
Input a Decimal Number : 5
Expected Output

Binary number is: 101


10. Write a Java program to convert a decimal number to hexadecimal number.
Input Data:
Input a decimal number: 15
Expected Output

Hexadecimal number is : F
11. Write a Java program to compare two numbers.
Input Data:
Input first integer: 25
Input second integer: 39
Expected Output

25 != 39
25 < 39
25 <= 39
12. Write a Java program to compute the area of a hexagon.
Area of a hexagon = (6 * s^2)/(4*tan(π/6))
where s is the length of a side
Input Data:
Input the length of a side of the hexagon: 6
Expected Output:
The area of the hexagon is: 93.53074360871938
13. Write a Java program to reverse a string.
Input Data:
Input a string: The quick brown fox
Expected Output:
Reverse string: xof nworb kciuq ehT
14. Write a Java program to count the letters, spaces, numbers and other characters of an input string.
Expected Output

The string is : Aa kiu, I swd skieo 236587. GH kiu: sieo?? 25.33


letter: 23
space: 9
number: 10
other: 6
15. Write a Java program to print the ascii value of a given character.
Expected Output

The ASCII value of Z is :90


16. Write a Java program to print the following string in a specific format (see the output).
Sample Output

Twinkle, twinkle, little star,


How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are
17. Write a Java program to print the odd numbers from 1 to 99. Prints one number per line.
Sample Output:

1
3
5
……
18. Write a Java program to accept a number and check the number is even or not. Prints 1 if the
number is even or 0 if the number is odd.
Sample Output:

Input a number: 20
1
19. Write a Java program to print numbers between 1 to 100 which are divisible by 3, 5 and by both.
Sample Output:

Divided by 3:
3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57
, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99,

Divided by 5:
5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90,
95,

Divided by 3 & 5:
15, 30, 45, 60, 75, 90,
20. Write a Java program to convert a string to an integer in Java.
Sample Output:

Input a number(string): 25
The integer value is: 25
21. Write a Java program to calculate the sum of two integers and return true if the sum is equal to a
third integer.
Sample Output:

Input the first number : 5


Input the second number: 10
Input the third number : 15
The result is: true
22. Write a Java program that accepts three integers from the user and return true if two or more of
them (integers ) have the same rightmost digit. The integers are non-negative.
Sample Output:

Input the first number : 5


Input the second number: 10
Input the third number : 15
The result is: true
23. Write a Java program to convert seconds to hour, minute and seconds.
Sample Output:

Input seconds: 86399


23:59:59
24. Write a Java program to accepts an integer and count the factors of the number.
Sample Output:

Input an integer: 25
3
25. Write a Java program to capitalize the first letter of each word in a sentence.
Sample Output:

Input a Sentence: the quick brown fox jumps over the lazy dog.
The Quick Brown Fox Jumps Over The Lazy Dog.
26. Write a Java program to convert a given string into lowercase.
Sample Output:

Input a String: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
the quick brown fox jumps over the lazy dog.
27. Write a Java program to insert a word in the middle of the another string.
Insert "Tutorial" in the middle of "Python 3.0", so result will be Python Tutorial 3.0
Sample Output:

Python Tutorial 3.0


28. Write a Java program to create the concatenation of the two strings except removing the first
character of each string. The length of the strings must be 1 and above.
Test Data: Str1 = Python
Str2 = Tutorial
Sample Output:

Ythonutorial
29. Write a Java program to test if the first and the last element of two array of integers are same. The
length of the array must be greater than or equal to 2.
Test Data: array1 = 50, -20, 0, 30, 40, 60, 12
array2 = 45, 20, 10, 20, 30, 50, 11
Sample Output:

False
30. Write a Java program to get the larger value between first and last element of an array (length 3)
of integers.
Sample Output:

Original Array: [20, 30, 40]


Larger value between first and last element: 40
31. Write a Java program to check if a string starts with a specified word.
Sample Data: string1 = "Hello how are you?"
Sample Output:
32. Write a Java program to find the kth smallest and largest element in a given array. Elements in
the array can be in any order.
Expected Output:
Original Array:
[1, 4, 17, 7, 25, 3, 100]
K'th smallest element of the said array:
3
K'th largest element of the said array:
25
33. Write a Java program to move every zero to the right side of a given array of integers.
Original array: [0, 3, 4, 0, 1, 2, 5, 0]
Result: [3, 4, 1, 2, 5, 0, 0, 0]
34. Write a Java program to accept two string and test if the second string contains the first one.
Input first string: Once in a blue moon
Input second string: See eye to eye
If the second string contains the first one? False
35. Write a Java program to get the number of element in a given array of integers that are smaller
than the integer of another given array of integers.
Expected Output:
0
3
7
36. Write a Java program to create a basic string compression method using the counts of repeated
characters
Input string: aaaabbbbcccccddddeeee

Expected Output:

Enter a string (you can include space as well)


aaaabbbbcccccddddeeee
The compressed string along with the counts of repeated characters is:
a4b4c5d4e4
37. Write a Java program that takes a year from user and print whether that year is a leap year or not.
Test Data
Input the year: 2016
Expected Output :
2016 is a leap year
38. Write a program in Java to display the pattern like right angle triangle with a number.
Test Data
Input number of rows : 10
Expected Output :

1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910
39. Write a program in Java to make such a pattern like right angle triangle with number increased by
1.The pattern like :
1
23
456
7 8 9 10
40. Write a program in Java to make such a pattern like a pyramid with a number which will repeat
the number in the same row.
1
22
333
4444
41. Write a program in Java to display the pattern like a diamond.
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
42. Write a Java program to display Pascal's triangle.
Test Data
Input number of rows: 5
Expected Output :

Input number of rows: 5


1
11
121
1331
14641
43. Write a java program to generate a following *'s triangle.
Test Data
Input the number: 6
Expected Output :

******
*****
****
***
**
*
44. Write a Java program to display the number rhombus structure
Test Data
Input the number: 7
Expected Output :

1
212
32123
4321234
543212345
65432123456
7654321234567
65432123456
543212345
4321234
32123
212
1
45. Write a Java program to copy an array by iterating the array.
46. Write a Java program to reverse an array of integer values.
47. Write a Java program to find the second largest element in an array.
48. Write a Java program to find the number of even and odd integers in a given array of integers.
49. Write a java program to compare two strings lexicographically
Sample Output:

String 1: This is Exercise 1


String 2: This is Exercise 2
"This is Exercise 1" is less than "This is Exercise 2"
50. Write a Java program to test if a given string contains the specified sequence of char values.
Sample Output:

Original String: PHP Exercises and Python Exercises


Specified sequence of char values: and
true

You might also like