Challenges you have to complete in Java.
Lessons
How to set Up environment in Java
How to install Java Software
First basics in Java.
Variables and Strings in Java
Taking User input In Java
Operators in Java
Loops in Java
Arrays in Java
Switch Cases in Java
1. Write a
Java program to print 'Hello' on screen and then print your name on a separate
line.
Expected
Output :
Hello
Alexandra
Abramov
2. Write a
Java program to print the sum of two numbers.
Test Data:
74 + 36
Expected
Output :
110
3. Write a
Java program to divide two numbers and print on the screen.
Test Data :
50/3
Expected
Output :
16
4. 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
5. 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
6. 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
7. Write a
Java program that takes a number as input and prints its multiplication table
upto 10.
Test Data:
Input a
number: 8
Expected
Output :
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
...
8 x 10 = 80
8. Write a
Java program to display the following pattern.
Sample
Pattern :
J
a v v
a
J a
a v
v a a
J J
aaaaa V V aaaaa
JJ
a a V
a a
9. Write a Java
program to compute the specified expressions and print the output.
Test Data:
((25.5 * 3.5
- 3.5 * 3.5) / (40.5 - 4.5))
Expected
Output
2.138888888888889
10. Write a
Java program to compute a specified formula.
Specified
Formula :
4.0 * (1 -
(1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))
Expected
Output
2.9760461760461765
11. 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
12. Write a
Java program that takes three numbers as input to calculate and print the
average of the numbers.
13. 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
Data Types in Java
1. Write a
Java program to convert temperature from Fahrenheit to Celsius degree.
Test Data
Input a
degree in Fahrenheit: 212
Expected
Output:
212.0 degree
Fahrenheit is equal to 100.0 in Celsius
2. Write a
Java program that reads a number in inches, converts it to meters.
Note: One
inch is 0.0254 meter.
Test Data
Input a
value for inch: 1000
Expected
Output :
1000.0 inch
is 25.4 meters
3. Write a
Java program that reads an integer between 0 and 1000 and adds all the digits
in the integer.
Test Data
Input an
integer between 0 and 1000: 565
Expected
Output :
The sum of
all digits in 565 is 16
4. Write a
Java program to convert minutes into a number of years and days.
Test Data
Input the
number of minutes: 3456789
Expected
Output :
3456789
minutes is approximately 6 years and 210 days
5. Write a
Java program that prints the current time in GMT.
Test Data
Input the
time zone offset to GMT: 256
Expected
Output:
Current time
is 23:40:24
6. Write a
Java program to compute body mass index (BMI).
Test Data
Input weight
in pounds: 452
Input height
in inches: 72
Expected
Output:
Body Mass
Index is 61.30159143458721
7. Write a
Java program to takes the user for a distance (in meters) and the time was
taken (as three numbers: hours, minutes, seconds), and display the speed, in
meters per second, kilometers per hour and miles per hour (hint: 1 mile = 1609
meters).
Test Data
Input
distance in meters: 2500
Input hour:
5
Input
minutes: 56
Input
seconds: 23
Expected
Output :
Your speed
in meters/second is 0.11691531
Your speed
in km/h is 0.42089513
Your speed
in miles/h is 0.26158804
8. Write a
Java program that reads a number and display the square, cube, and fourth
power.
Expected
Output:
Square: .2f
Cube: .2f
Fourth
power: 50625.00
9. Write a
Java program that accepts two integers from the user and then prints the sum,
the difference, the product, the average, the distance (the difference between
integer), the maximum (the larger of the two integers), the minimum (smaller of
the two integers).
Test Data
Input 1st
integer: 25
Input 2nd
integer: 5
Expected
Output :
Sum of two
integers: 30
Difference
of two integers: 20
Product of
two integers: 125
Average of
two integers: 15.00
Distance of
two integers: 20
Max integer:
25
Min integer:
5
10. Write a
Java program to break an integer into a sequence of individual digits.
Test Data
Input six
non-negative digits: 123456
Expected
Output :
1 2 3 4 5 6
11. Write a
Java program to test whether a given double/float value is a finite
floating-point value or not.
12. Write a
Java program to compare two given signed and unsigned numbers.
13. Write a
Java program to compute the floor division and the floor modulus of the given
dividend and divisor.
Control Flow
1. Write a
Java program to get a number from the user and print whether it is positive or
negative.
Test Data
Input
number: 35
Expected
Output :
Number is
positive
2. Write a
Java program to solve quadratic equations (use if, else if and else).
Test Data
Input a: 1
Input b: 5
Input c: 1
Expected
Output :
The roots
are -0.20871215252208009 and -4.7912878474779195
3. Take
three numbers from the user and print the greatest number.
Test Data
Input the
1st number: 25
Input the
2nd number: 78
Input the
3rd number: 87
Expected
Output :
The
greatest: 87
4. Write a
Java program that reads a floating-point number and prints "zero" if
the number is zero. Otherwise, print "positive" or
"negative". Add "small" if the absolute value of the number
is less than 1, or "large" if it exceeds 1,000,000.
Test Data
Input a
number: 25
Expected
Output :
Input value:
25
Positive
number
5. Write a
Java program that keeps a number from the user and generates an integer between
1 and 7 and displays the name of the weekday.
Test Data
Input
number: 3
Expected
Output :
Wednesday
6. Write a
Java program that reads in two floating-point numbers and tests whether they are
the same up to three decimal places.
Test Data
Input
floating-point number: 25.586
Input
floating-point another number: 25.589
Expected
Output :
They are
different
7. Write a
Java program to find the number of days in a month.
Test Data
Input a
month number: 2
Input a
year: 2016
Expected
Output :
February
2016 has 29 days
8. Write a
Java program that takes the user to provide a single character from the
alphabet. Print Vowel or Consonant, depending on the user input. If the user input
is not a letter (between a and z or A and Z), or is a string of length > 1,
print an error message.
Test Data
Input an
alphabet: p
Expected
Output :
Input letter
is Consonant
9. 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
10. Write a
program in Java to display the first 10 natural numbers.
Expected
Output :
The first 10
natural numbers are:
1
2
3
4
5
6
7
8
9
10
11. Write a
program in Java to display n terms of natural numbers and their sum.
Test Data
Input the
number: 2
Expected
Output :
Input
number:
2
The first n
natural numbers are :
2
1
2
The Sum of
Natural Number upto n terms :
23
.
12. Write a
program in Java to input 5 numbers from keyboard and find their sum and
average.
Test Data
Input the 5
numbers : 1 2 3 4 5
Expected
Output :
Input the 5
numbers :
1
2
3
4
5
The sum of 5
no is : 15
The Average
is : 3.0
13. Write a
program in Java to display the cube of the number upto given an integer.
Test Data
Input number
of terms : 4
Expected
Output :
Number is :
1 and cube of 1 is : 1
Number is :
2 and cube of 2 is : 8
Number is :
3 and cube of 3 is : 27
Number is :
4 and cube of 4 is : 64
14. Write a
program in Java to display the multiplication table of a given integer.
Test Data
Input the
number (Table to be calculated) : Input number of terms : 5
Expected
Output :
5 X 0 =
0
5 X 1 =
5
5 X 2 =
10
5 X 3 =
15
5 X 4 =
20
5 X 5 = 25
15. Write a
program in Java to display the n terms of odd natural number and their
sum.
Test Data
Input number
of terms is: 5
Expected
Output :
The odd numbers
are :
1
3
5
7
9
The Sum of
odd Natural Number upto 5 terms is: 25
Arrays
. Write a
Java program to get the character at the given index within the String.
Sample
Output:
Original
String = Java Exercises!
The
character at position 0 is J
The
character at position 10 is i
2. Write a
Java program to get the character (Unicode code point) at the given index
within the String.
Sample Output:
Original
String : w3resource.com
Character(unicode
point) = 51
Character(unicode
point) = 101
3. Write a
Java program to get the character (Unicode code point) before the specified
index within the String.
Sample
Output:
Original
String : w3resource.com
Character(unicode
point) = 119
Character(unicode
point) = 99
4. Write a
Java program to count a number of Unicode code points in the specified text
range of a String.
Sample
Output:
Original
String : w3rsource.com
Codepoint
count = 9
5. Write a
Java program to compare two strings lexicographically. Two strings are
lexicographically equal if they are the same length and contain the same
characters in the same positions.
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"
6. Write a
Java program to compare two strings lexicographically, ignoring case
differences.
Sample
Output:
String 1:
This is exercise 1
String 2:
This is Exercise 1
"This
is exercise 1" is equal to "This is Exercise 1"
7. Write a
Java program to concatenate a given string to the end of another string.
Sample
Output:
String 1:
PHP Exercises and
String 2:
Python Exercises
The
concatenated string: PHP Exercises and Python Exercises
8. 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
9. Write a
Java program to compare a given string to the specified character
sequence.
Sample
Output:
Comparing
example.com and example.com: true
Comparing
Example.com and example.com: false
10. Write a
Java program to compare a given string to the specified string buffer.
Sample
Output:
Comparing
example.com and example.com: true
Comparing
Example.com and example.com: false
11. Write a
Java program to create a new String object with the contents of a character
array.
Sample
Output:
The book
contains 234 pages.
12. Write a
Java program to check whether a given string ends with the contents of another
string.
Sample
Output:
"Python
Exercises" ends with "se"? false
"Python
Exercise" ends with "se"? true
13. Write a
Java program to check whether two String objects contain the same data.
Sample
Output:
"Stephen
Edwin King" equals "Walter Winchell"? false
"Stephen
Edwin King" equals "Mike Royko"? false
14. Write a
Java program to compare a given string to another string, ignoring case
considerations.
Sample
Output:
"Stephen
Edwin King" equals "Walter Winchell"? false
"Stephen
Edwin King" equals "stephen edwin king"? true
15. Write a Java
program to print current date and time in the specified format.
Sample
Output:
Current Date
and Time :
June 19,
2017
3:13 pm
N.B. : The
current date and time will change according to your system date and time.
16. Write a
Java program to get the contents of a given string as a byte array.
Sample
Output:
The new
String equals This is a sample String.
17. Write a
Java program to get the contents of a given string as a character array.
Sample
Output:
The char
array equals "[C@2a139a55"
18. Write a
Java program to create a unique identifier of a given string.
Sample
Output:
The hash for
Python Exercises. is 863132599
19. Write a
Java program to get the index of all the characters of the alphabet.
Sample
Output:
a b c d
e f
g h i j
=========================
36 10 7 40 2
16 42 1 6 20
k l
m n o p
q r
s t
===========================
8 35 22 14
12 23 4 11 24 31
u v
w x y
z
================
5 27 13 18
38 37
Sample
string of all alphabet: "The quick brown fox jumps over the lazy
dog."
20. Write a
Java program to get the canonical representation of the string object.
Sample
Output:
str1 ==
str2? false
str1 == str3? true
0 Comments