Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am teaching an introduction to computer science lab session this semester. We are using Java. I am looking for some hard bonus questions for students to solve. They should be able to solve them in a relatively minimal amount of code (say 50-300 lines).

So Far they know:

  • input using Scanner
  • Basic Math (including %)
  • boolean logic
  • Strings
  • if statements
  • while/for loops

Later in the term they will know how to make methods and classes although this is getting towards the end of the class.

I want to offer some short but challenging bonus questions. Does anyone have any good suggestions?


locked by maple_shaft Oct 13 '13 at 13:19

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

Are these bonus questions for an examination, or for them to take home and code on their own? –  Macneil Oct 27 '10 at 0:38
@Macneil: They are to take home questions (or more precisely do in this lab questions). –  sixtyfootersdude Oct 27 '10 at 10:51

12 Answers 12

up vote 4 down vote accepted

There is a good place to practice java, Codingbat go there and there are pleanty of simple problems in java.Ask students to solve them.It will provide an opportunity to prove their skills in String,Array,loops,etc....You can check their answers online.

+1 for Codingbat –  Chankey Pathak Oct 27 '10 at 12:05

A Pig Latin translator.

This project while simple covers a lot of basic techniques.

  1. I/O - users must enter a string to be translated and have the results visible to them
  2. JAVA API - specifically for manipulating strings
  3. Loops - for moving through the input
  4. String manipulation - rearranging each word into it's Pig Latin equivalent
  5. Dealing with requirements and conditions, The rules for Pig Latin translation

I've been a T/A for my schools intro java course, and this was one of the projects given, generally students really enjoyed working on it, and it gives them a program that is more fun than just arbitrary data and output.


Here are some ideas that I've used in intro courses:

  1. Write a "Star Wars Name" Generator -- This is a fun way to use the substring method

  2. Input a double and a String of digits, e.g. 4.980 and "14285" representing a decimal number and then the "repeating digits" after that number. The program will then use a formula you give them to generate the rational form: 34861/7000

  3. You can make #2 more challenging by having them always output the rational in lowest terms (you can give them the GCD algorithm, or they can simply write a loop and do it exhaustively). Naturally, you can make the problem even more challenging by not telling them the formula from 2.

  4. Write a program to take a credit card balance, an APR, and a number of months, and then print the monthly payment in order to pay the credit card off. (Effectively, write the Excel PMT function.) This might be interesting to students from a personal finance stand point, and also because of the new change to credit card statements to show the 36 month payoff. Again, the formula, or only pieces of it, should be provided.

  5. If you can cover graphics at all (simple drawing of lines), have the students use trig to draw a regular polygon.


How about a program that solves a Sudoko puzzle that is provided as input to the program?

They will get a good primer on arrays, and the programming logic will require some thinking about algorithms. You could even give bonus points for a non-brute force solution.

I would argue that this is a bit to difficult for an introduction. –  Martin Oct 27 '10 at 7:04
9x9 Sudoku can usually be solved reasonably quickly with just simple recursive backtracking, especially in Java or C. If they understand recursion, they'll only need a small nudge. Most of the code will be spent on checking if assignments are valid. There will only be three or four conceptually difficult lines that you can just give them detailed instructions on. If they read puzzles from a file, they should be able to do it in at most 50-75 lines. –  Hoa Long Tam Oct 27 '10 at 8:02
I really like this question however I think that it is probably too difficult. Students are not introduced to recursion until next semester. –  sixtyfootersdude Oct 27 '10 at 10:56
How about a simpler version? You could take a solved soduku puzzle and validate whether it is a valid solution. –  JohnFx Oct 27 '10 at 14:50

Whatever you do, don't skip on the difficulty of the problem. Make sure it is hard. Students that do not have to take time to figure out the problem are never learning anything, they should be immediately stumped and really have to think about the solution.

The problem is, only you know the correct problem to give in terms of size, difficulty and how long it takes to solve.

I can throw a dozen ideas at you, my favorite, Knights Tour

But only you have the right answer!


Verify the check digit in a barcode. Some of the most common barcode standards have check digits which are fairly easy to calculate.

UPC (12 digits) and its superset EAN (13 digits) are among the most common barcode standards you will find. The check digit is calculated like this:

  • the right-most digit is the check digit which we want to verify
  • if you have a 12-digit barcode, add a zero to the beginning - e.g. 008811908027 becomes 0008811908027
  • considering the left-most digit to be in position 0 and ignoring the check digit, sum the digits in odd-numbered positions (0 + 8 + 1 + 9 + 8 + 2 = 28) and multiply the result by 3 (84)
  • sum the digits in even-numbered positions (0 + 0 + 8 + 1 + 0 + 0 = 9)
  • add those two results together (93) then calculate that total modulo 10 (93 % 10 = 3)
  • if the answer is 0 then that is the check digit, otherwise subtract the result from 10 to give the check digit (7)

That should cover most of the areas highlighted in the question

An USB bar code scanner would be great for this. –  user1249 Nov 14 '10 at 10:18
On this same topic: the Luhn algorithm is a good problem for this level. It's very "follow the steps" algorithmic. –  nlawalker Jun 8 '11 at 17:59

Creating a number guessing game (or variation of it) using the random generator might be a fun one.


This might be a bit of a stretch, but would a recursive method work? I think the Fibbonacci numbers function would probably be the easiest.

Recursion is off the table. This is a second semester topic that students usually have a hard time grasping. –  sixtyfootersdude Oct 27 '10 at 10:53
Fibbonacci numbers done recursively is highly inefficient without caching the values. It can be done with a simple for-loop. –  user1249 Nov 14 '10 at 10:20

Let them "transpose" a text file. For example, this file:


would be transposed to:

I don't understand..? –  sixtyfootersdude Oct 27 '10 at 10:54
Uh. The first row ("ab") becomes the first column. The second row ("cde") becomes the second column. In effect, the file is swapped from left-to-right, top-to-bottom to top-to-bottom, left-to-right reading order. –  zvrba Oct 27 '10 at 11:39
OOOOOOOOOOOO. Ok! –  sixtyfootersdude Oct 29 '10 at 16:31

One type of question that can be quite fun is to give them a bit of code that behaves in a standard way and then challenge them to find a way to implement the same functionality in fewer lines of code.

That can be particularly good if they're starting to learn about the standard libraries available to them and you can use it as a way to bring in utility methods from those rather than implementing stuff longhand.


Have them parse a string into a number without using anything like parseInteger(). They get to use strings, arithmetic, mod operator, and a loop.

A lot of beginning students have trouble with that.


Give me some code that will make the JVM to crash

(taken from the excellent book, My Job Went To India from Chad Fowler)

An example would be great! –  sixtyfootersdude Oct 27 '10 at 10:56
@sixtyfootersdude: we are both screwed –  user2567 Oct 27 '10 at 15:11
JNI to the rescue! –  user1249 Nov 14 '10 at 10:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.