Tuesday, January 21, 2025

12 Java Code Challenges for Beginners

If you’re starting a career as a Front-End Developer, Full-Stack Developer, or Computer Scientist, then you’ve probably started learning Java. Maybe you’ve started with an online course, which is a great way to build a solid programming foundation. Once you’re familiar with the basics, try putting your Java skills to the test with some practical exercises to build on your knowledge.

One of the best features of Java is its flexibility. You may find that there are multiple ways to solve the same challenge. In fact, if you’re learning Java with a friend, try these challenges together and learn from each other by comparing your results.

If you get stuck, try thinking through the problem using pseudocode or a general description of the programming steps you’d use to solve the problem. Pseudocode is helpful because it lets you work through programming challenges without having to worry about the specific syntax of a programming language (you can worry about that later).

Below are 12 Java code challenges for beginners. The first six challenges are with strings, while the last six challenges involve numerical inputs. Let’s get started!

Learn something new for free

12 Java code challenges to practice your new skills

1. Word reversal

For this challenge, the input is a string of words, and the output should be the words in reverse but with the letters in the original order. For example, the string “Dog bites man” should output as “man bites Dog.”

After you’ve solved this challenge, try adding sentence capitalization and punctuation to your code. So, the string “Codecademy is the best!” should output as “Best the is Codecademy!”

2. Find the word

Starting with an input string of words, find the second-to-last word of the string. For example, an input of “I love Codecademy” should return “love.”

To make your program more challenging, allow for a second numerical input, n, that results in returning the nth word of a string. So, for the string “I can program in Java” and n = 3, the output should be the third word, “program.”

For a given input string, return a Boolean TRUE if the string starts with a given input word. So, for an input string of “hello world” and input word “hello,” the program should return TRUE.

For a more advanced word searcher, create a program that returns the number of times a word appears in an input string. For example, given an input word “new” and an input string “I’m the new newt,” the program should return a value of 2.

4. Anagrams

Two words are anagrams if they contain the same letters but in a different order. Here are a few examples of anagram pairs:

  • “listen” and “silent”
  • “binary” and “brainy”
  • “Paris” and “pairs”

For a given input of two strings, return a Boolean TRUE if the two strings are anagrams.

As an added challenge, for a given array of strings, return separate lists that group anagrams together. For example, the input {“tar,” “rat,” “art,” “meats,” “steam”}, the output should look something like {[“tar,” “rat,” “art”], [“meats,” “steam”]}.

5. Pangrams

A pangram is a sentence that contains all 26 letters of the English alphabet. One of the most well-known examples of a pangram is, “The quick brown fox jumps over the lazy dog.” Create a pangram checker that returns a Boolean TRUE if an input string is a pangram and FALSE if it isn’t.

For an added pangram challenge, create a perfect pangram checker. A perfect pangram is a sentence that uses each letter of the alphabet only once, such as, “Mr. Jock, TV quiz Ph.D., bags few lynx.”

6. Count vowels and consonants

With this challenge, you’ll create a program that will count the number of vowels and the number of consonants in a string.

Using the same example string “I love Codecademy,” should output as “vowels: 7, consonants: 8.” This is, of course, assuming you didn’t include “y” as a vowel, in which case your output should read: “vowels: 8, consonants: 7.”

7. Number reversal

This one is a technical interview favorite. For a given input number, return the number in reverse. So, an input of 3956 should return 6593.

If you’re ready for a bigger challenge, reverse a decimal number. The decimal point should stay in the same place. So, the number 193.56 should output 653.91.

8. Armstrong numbers

An Armstrong number is a whole number that’s equal to the sum of its digits raised to the power of the total number of digits. For example, 153 is an Armstrong number because there are three digits, and 153 = 13 + 53 + 33. The four-digit number 8208 is also an Armstrong number, as 8208 = 84 + 24 + 04 + 84.

Create an Armstrong number checker that returns a Boolean TRUE if the input number is an Armstrong number. Hint: to extract each digit from a given number, try using the remainder/modulo operator.

If you’re looking for something a little more challenging, create an Armstrong number calculator that returns all Armstrong numbers between 0 and the input number.

9. Product maximizer

For a given input array of numbers, find the two that result in the largest product. The output should include the two numbers in the array along with their product.

As an extra challenge, use an input of two arrays of numbers and find two numbers — one from each input array — that results in the largest product.

10. Prime number checker

A prime number is any whole number greater than 1 whose only factors are 1 and itself. For example, 7 is a prime number because it’s only divisible by 1 and 7.

Create a function that returns TRUE if an input number is prime. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, and 19.

For a slightly harder challenge, create a prime number calculator that outputs all prime numbers between 2 and the input number.

11. Prime factorization

The prime factors of a number are all of the integers below that number that are divisible into the number as well as 1. For example, the prime factors of 12 are 1,2,3,4,6, and 12.

Create a prime factorization calculator that returns the prime factors of any number between 2 and 100. If you’re looking for a more advanced version of this challenge, use exercise 9 to create a prime factorization calculator for any number. Hint: think about using square roots to cut your work in half.

12. Summation

Write a Java program that returns the sum of two numbers. The numbers to be summed may be integers, rational numbers, real numbers, or complex numbers.

For example, the input 289 + 398 should output 687.

Advancing your career with Java

Along with being a fun, low-stress way to test your knowledge of a programming language, code challenges play an important role in helping you prepare for the interview process. If you’re pursuing a career where knowledge of Java is expected, then you’ll be asked to complete a Java-based coding test, as well as other questions related to Java. And, the best way to prepare for that test is to practice code challenges like these.  

To find more opportunities to practice, take a look at our other Java courses, including our popular Java for Programmers course.

This blog was originally published in October 2021 and has been updated to include additional Java challenges for beginners.

Whether you’re looking to break into a new career, build your technical skills, or just code for fun, we’re here to help every step of the way. Check out our blog post about how to choose the best Codecademy plan for you to learn about our structured courses, professional certifications, interview prep resources, career services, and more.

Related Articles

Latest Articles