Practice Questions (Code Writing)
Lists & Sets
Table of contents
Easy Level Code Writing Questions
- Calculate the number of days a person lived given their birth and death dates:
// Assume the person was born on January 1, 2000, and died on December 31, 2020.
Answer
The person lived for 7661 days.
-
Write a line of code to declare a variable
temperature
and assign it a value of36.6
.Answer
double temperature = 36.6;
-
Write a line of code to declare a
String
variablegreeting
and assign it the value"Hello, World!"
.Answer
String greeting = “Hello, World!”;
- Given the following line of code:
int score = 10;
Write the line of code to assign the value
50
toscore
.Answer
score = 50;
- Complete the code snippet to print the following result:
+----------+ | Code | +----------+ public class NameInBox { public static void main(String[] args) { // TODO print Code in a box made from // characters - | + } }
Answer
public class NameInBox { public static void main(String[] args) { System.out.println("+----------+"); System.out.println("| Code |"); System.out.println("+----------+"); } }
-
Write the code to construct a
Circle
with its center at(150, 75)
and a radius of25
. Assign it to a variable namedcircle
. Use this construction parameters: (int x, int y, int radius)Answer
Circle circle = new Circle(150, 75, 25);
-
Write the code to construct a triangle with vertices at
(0, 0)
,(10, 0)
, and(5, 8.66)
. Assign it to a variable namedtriangle
. Use this construction parameters: (int x1, int y1, int x2, int y2, int x3, int y3)Answer
Triangle triangle = new Triangle(0, 0, 10, 0, 5, 8.66);
-
Write a program that prints your name in large letters, such as
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- Write a program that prints your name in Morse code, like this:
...- .. -.- .- ...
Use a separate call to
System.out.print
for each letter. - Write a program that prints a face similar to (but different from) the following:
///// +"""""+ (| o o |) | ^ | | '-' | +-----+
-
Write a program that prints a house that looks exactly like the following:
+ + + + + +-----+ | .-. | | | | | +-+-+-+
- Write a program that prints an animal speaking a greeting, similar to (but different from) the following:
/\_/\ ----- ( ' ' ) / Hello \ ( - ) < Junior | | | | \ Coder!/ (__|__) -----
-
Write a program that prints three items, such as the names of your three best friends or favorite movies, on three separate lines.
-
Write a program that prints India flag, using special symbols you find on your keyboard.
- Type in and run the following program. Then modify it to show the message “Hello, your name!”.
import javax.swing.JOptionPane; public class DialogViewer { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Hello, World!"); } }
- Write a program that prints a two-column list of your friends’ birthdays. In the first column, print the names of your best friends; in the second, print their birthdays.
Medium Level Code Writing Questions
-
Write a program that initializes a string with “Mississippi”. Then replace all “i” with “ii” and print the length of the resulting string. In that string, replace all “ss” with “s” and print the length of the resulting string.
-
Look into the API documentation of the
String
class and locate thetrim
method. Write a program demonstrating what it does. Then show how you can use thereplace
method to remove all spaces from a string. -
(TEST) Write an
AreaTester
program that constructs aRectangle
object and then computes and prints itsarea
. Use thegetWidth
andgetHeight
methods. Also print the expected answer. -
(TEST) Write a
PerimeterTester
program that constructs aRectangle
object and then computes and prints itsperimeter
. Use thegetWidth
andgetHeight
methods. Also print the expected answer. -
(TRICKY) Write a program that constructs a rectangle with area 42 and a rectangle with perim- eter 42. Print the widths and heights of both rectangles.
-
(TEST) Look into the API documentation of the
Rectangle
class and locate the method voidadd(int newx, int newy)
Read through the method documentation. Then determine the result of the following statements:
Rectangle box = new Rectangle(5, 10, 20, 30);
box.add(0, 0);
Write a program
AddTester
that prints the expected and actual location, width, and height of box after the call to add. -
(TEST) Write a program
ReplaceTester
that encodes a string by replacing all letters"i"
with"!"
and all letters"s"
with"$"
. Use thereplace
method. Demonstrate that you can correctly encode the string “Mississippi”. Print both the actual and expected result. -
Write a program
HollePrinter
that switches the letters"e"
and"o"
in a string. Use the replace method repeatedly. Demonstrate that the string"Hello, World!"
turns into"Holle, Werld!"
-
(TEST) The
StringBuilder
class has a method for reversing a string. In aReverseTester
class, construct aStringBuilder
from a given string (such as “desserts”), call thereverse
method followed by thetoString
method, and print the result. Also print the expected value. -
In the Java library, a color is specified by its red, green, and blue components between 0 and 255. Write a program
BrighterDemo
that constructs aColor
object with red, green, and blue values of 50, 100, and 150. Then apply thebrighter
method of the Color class and print the red, green, and blue values of the resulting color. (You won’t actually see the color.) -
Repeat above exercise, but apply the
darker
method of theColor
class twice to the objectColor.RED
. Call your classDarkerDemo
. -
The
Random
class implements a random number generator, which produces sequences of numbers that appear to be random. To generate random integers, you construct an object of theRandom
class, and then apply thenextInt
method. For example, the callgenerator.nextInt(6)
gives you a random number between 0 and 5. Write a programDieSimulator
that uses theRandom
class to simulate the cast of a die, printing a random number between 1 and 6 every time that the program is run. -
Write a program
RandomPrice
that prints a random price between $10.00 and $19.95 every time the program is run. -
(TEST) Look at the API of the
Point
class and find out how to construct aPoint
object. In aPointTester
program, construct two points with coordinates (3, 4) and (–3, –4). Find the distance between them, using thedistance
method. Print the distance, as well as the expected value. (Draw a sketch on graph paper to find the value you will expect.) -
Using the
Day
class, write aDayTester
program that constructs a Day object representing today, adds ten days to it, and then computes the difference between that day and today. Print the difference and the expected value. -
Using the
Picture
class, write aHalfSizePicture
program that loads a picture and shows it at half the original size, centered in the window. -
Using the
Picture
class, write aDoubleSizePicture
program that loads a picture, doubles its size, and shows the center of the picture in the window.
Hard Level Code Writing Questions
-
Write a program called
FourRectanglePrinter
that constructs aRectangle
object, prints its location by callingSystem.out.println(box)
, and then translates and prints it three more times, so that, if the rectangles were drawn, they would form one large rectangle, as shown below. Your program will not produce a drawing. It will simply print the locations of the four rectangles.
-
Write a
GrowSquarePrinter
program that constructs aRectangle
object square representing a square with top-left corner (100, 100) and side length 50, prints its location by callingSystem.out.println(square)
, applies the translate and grow methods, and callsSystem.out.println(square)
again. The calls totranslate
andgrow
should modify the square so that it has twice the size and the same top-left corner as the original. If the squares were drawn, they would look like the figure below. Your program will not produce a drawing. It will simply print the locations of square before and after calling the mutator methods. Look up the description of thegrow
method in the API documentation.
-
Write a
CenteredSquaresPrinter
program that constructs aRectangle
object square representing a square with top-left corner (100, 100) and side length 200, prints its location by callingSystem.out.println(square)
, applies the grow and translate methods, and callsSystem.out.println(square)
again. The calls togrow
andtranslate
should modify the square so that it has half the width and is centered in the original square. If the squares were drawn, they would look like the figure below. Your program will not produce a drawing. It will simply print the locations of square before and after calling the mutator methods. Look up the description of the grow method in the API documentation.
- The
BigInteger
class represents integer numbers with an arbitrary number of digits. (As you will see later, theint
type cannot express very large integers.) You can construct aBigInteger
object by providing a string of its digits, such asBigInteger a = new BigInteger("12345678987654321");
Write a program that prints the square, fourth power, and eighth power of a, using one of the methods of the BigInteger class.
-
Write a program
LotteryPrinter
that picks a combination in a lottery. In this lottery, players can choose 6 numbers (possibly repeated) between 1 and 49. Construct an object of theRandom
class and invoke an appropriate method to generate each number. (In a real lot tery, repetitions aren’t allowed, but we haven’t yet discussed the programming con structs that would be required to deal with that problem.) program should print out a sentence such as “Play this combination—it’ll make you rich!”, followed by a lottery combination. - Using the
Day
class, write a program that generates aDay
object representing February 28 of this year, and three more such objects that represent February 28 of the next three years. Advance each object by one day, and print each object. Also print the expected values:2019-02-29 Expected: 2019-02-29 2020-03-01 Expected: 2020-03-01 . . .
- The
GregorianCalendar
class describes a point in time, as measured by the Gregorian calendar, the standard calendar that is commonly used throughout the world today. You construct a GregorianCalendar object from a year, month, and day of the month, like this:GregorianCalendar cal = new GregorianCalendar(); // Today’s date GregorianCalendar eckertsBirthday = new GregorianCalendar(1919, Calendar.APRIL, 9);
Use the values
Calendar.JANUARY . . . Calendar.DECEMBER
to specify the month. The add method can be used to add a number of days to aGregorianCalendar
object:
cal.add(Calendar.DAY_OF_MONTH, 10); // Now cal is ten days from today
This is a mutator method — it changes the cal object.
Theget
method can be used to query a givenGregorianCalendar
object:int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); int month = cal.get(Calendar.MONTH); int year = cal.get(Calendar.YEAR); int weekday = cal.get(Calendar.DAY_OF_WEEK); // 1 is Sunday, 2 is Monday, . . . , 7 is Saturday
Your task is to write a program that prints:
- The date and weekday that is 100 days from today.
- The weekday of your birthday.
- The date that is 10,000 days from your birthday.
Use the birthday of a computer scientist if you don’t want to reveal your own.
Hint: TheGregorianCalendar
class is complex, and it is a really good idea to write a few test programs to explore the API before tackling the whole problem.
Start with a program that constructs today’s date, adds ten days, and prints out the day of the month and the weekday. - The
LocalDate
class describes a calendar date that does not depend on a location or time zone. You construct a date like this:LocalDate today = LocalDate.now(); // Today’s date LocalDate eckertsBirthday = LocalDate.of(1919, 4, 9);
The
plusDays
method can be used to add a number of days to aLocalDate
object:LocalDate later = today.plusDays(10); // Ten days from today
This method does not mutate the
today
object, but it returns a new object that is a given number of days away fromtoday
. To get the year of a day, callint year = today.getYear();
To get the weekday of a LocalDate, call
String weekday = today.getDayOfWeek().toString();
Your task is to write a program that prints
- The weekday of “Pi day”, that is, March 14, of the current year.
- The date and weekday of “Programmer’s day” in the current year; that is, the 256th day of the year. (The number 256, or 28, is useful for some programming tasks.)
- The date and weekday of the date that is 10,000 days earlier than today.
- (TEST) Write a program
LineDistanceTester
that constructs a line joining the points (100, 100) and (200, 200), then constructs points (100, 200), (150, 150), and (250, 50). Print the distance from the line to each of the three points, using theptSegDist
method of theLine2D
class. Also print the expected values. (Draw a sketch on graph paper to find what values you expect.)