1.8. Unit 1 Practice Coding

1.8.1. Mixed Up Code Practice

Try to solve each of the following. Click the Check Me button to check each solution. You will be told if your solution is too short, has a block in the wrong order, or you are using the wrong block. Some of the problems may have an extra block that isn’t needed in the correct solution. Try to solve these on your phone or other mobile device!

        1-8-1: The following program segment should print Maria's first name on one line and her last name on the next line.  But, the blocks have been mixed up and include an extra block that isn't needed in the solution.  Drag the blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

String firstName = "Maria"; String lastName = "Hernandez"; --- System.out.println(firstName); --- System.out.println(lastName); --- System.out.printlln(firstname); #distractor
        1-8-2: The following program segment should print the words to a famous poem. But the blocks have been mixed up.  Drag the blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

System.out.println("Roses are red"); --- System.out.println("Violets are blue"); --- System.out.println("Sugar is sweet"); --- System.out.println("And so are you");
        1-8-3: The main method in the following class should print Marcus's name on one line and his favorite color on the next line. But, the blocks have been mixed up and include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) { --- String name = "Marcus"; String color = "Blue"; --- System.out.println(name); --- System.out.println(color); --- } // end main method --- } // end of class --- System.out.println(Name); #distractor
        1-8-4: The following program segment should figure out the cost for each shirt if they are buy 2 and get the third free and they are originally $45 each.  But, the blocks have been mixed up.  Drag the blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

double price = 45; --- double totalCost = price * 2; --- double pricePerShirt = totalCost / 3; --- System.out.println(pricePerShirt);
        1-8-5: The following program segment should figure out the cost per person for a dinner including the tip. But the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

double bill = 89.23; --- double tip = bill * 0.20; --- double total = bill + tip; --- int numPeople = 3; double perPersonCost = total / numPeople; --- System.out.println(perPersonCost); --- System.out.println(perpersoncost); #distractor
        1-8-6: The main method in the following class should print the number of seconds in 5 days. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) { --- int sInMin = 60; int mInHour = 60; int hInDay = 24; --- int sInDay = sInMin * mInHour * hInDay; --- int total = sInDay * 5; --- System.out.println(total); --- } // end main method --- } // end class --- public Class Test1 { #distractor
        1-8-7: The main method in the following class should calculate the number of months it would take you to save 500 if you make 50 a week. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) { --- double weeklyRate = 50; double goal = 500; --- double numWeeks = goal / weeklyRate; --- double numMonths = numWeeks / 4; --- System.out.println(numMonths); --- } // end main method } // end class --- public void main(String[] args) { #distractor
        1-8-8: The main method in the following class should print the name on one line followed by the age on the next line. But, the blocks have been mixed up and include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) { --- String name = "Layla"; String age = "16"; --- System.out.println("Your name is " + name); --- System.out.println("Your age is " + age); --- } // end main method --- } // end class --- System.out.println("Your age is " age); #distractor
        1-8-9: The main method in the following class should print the name on one line and the favorite food on the next line. But, the blocks have been mixed up and include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) --- { --- String name = "Julian"; String food = "chicken wings"; --- System.out.println("Your name is " + name); --- System.out.println("You favorite food is " + food); --- } --- } --- System.println("Your name is " + name); #distractor
        1-8-10: The main method in the following class should calculate the cost of a trip that is 200 miles when the price of gas is 2.20 and the miles per gallon is 42. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) --- { --- int miles = 200; double price = 2.20; int mpg = 42; --- double numGalls = miles / mpg; --- double totalCost = numGalls * price; --- System.out.println(totalCost); --- } --- } --- System.println(totalCost); #distractor
        1-8-11: The main method in the following class should calculate how many miles you can go on half a tank of gas if the miles per gallon is 26 and your tank holds 15 gallons. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) --- { --- int mpg = 26; int tankHolds = 15; --- double numGalls = tankHolds / 2; --- double miles = numGalls * mpg; --- System.out.println(miles); --- } --- } --- public static main(String[] args) #distractor
        1-8-12: The main method in the following class should calculate how many chicken wings you can buy with $3.50 if the wings are $.60 each. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) --- { --- double cost = 0.6; double money = 3.5; --- int numWings = (int) (money / cost); --- System.out.println(numWings); --- } --- } --- int cost = 0.6; int money = 3.5; #distractor
        1-8-13: The main method in the following class should calculate how much you will have to pay for an item that is 60% off the original price of $52.99. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) --- { --- double price = 52.99; double discount = 0.6; --- double savings = price * discount; --- double finalPrice = price - savings; --- System.out.println(finalPrice); --- } --- } --- int price = 52.99; int discount = 0.6; #distractor
        1-8-14: The main method in the following class should calculate how much the per item costs is for shorts that are buy 2 and get the third free.  The shorts are $39.99 each. But, the blocks have been mixed up and may include an extra block that isn't needed in the solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution.

public class Test1 { --- public static void main(String[] args) --- { --- double price = 39.99; --- double priceForTwo = price * 2; --- double itemPrice = priceForTwo / 3; --- System.out.println(itemPrice); --- } --- } --- double priceForTwo = price * 2 #distractor

1.8.2. Code Practice with Variables

The following code should print “Mary’s favorite color is blue”. However, the code has errors. Fix the code so that it compiles and runs correctly.

Line 5 is missing a starting ". Line 6 is missing a ending ;. Line 7 has Name when it should be name. Remember that variable names start with a lowercase letter.

This is the answer to the previous question.

Show Comments

The following code should print “Gabby’s favorite sport is soccer”. However, the code has errors. Fix the code so that it compiles and runs correctly.

Line 5 is missing a =. Line 6 is missing the closing ". Line 7 has Name when it should be name. Remember that a variable name starts with a lowercase letter. Line 8 is missing an ending +.

Show Comments

The following code should print “Your name is Carly and your favorite color is red”. Finish the code so that it prints the output correctly.

Add the required strings using the + operator and be sure to include spaces as needed.

This is the answer to the previous question.

Show Comments

Finish the code below so that it prints “Your name is Justin and your age is 16”.

Use the + operator to append the strings. Be sure to include spaces as needed.

Show Comments

Write the code to print “Julian’s favorite color is green. His favorite food is pizza.”.

Add the strings together using +. Don’t forget to include spaces and periods at the end of the sentences.

This is the answer to the previous question.

Show Comments

Finish the code below to print your favorite animal and food.

Use + to add strings together. Add spaces as needed and periods.

This is the answer to the previous question.

Show Comments

Finish the code below to print your favorite movie and book.

Add the strings together using +. Don’t forget to include spaces and periods at the end of the sentences.

Show Comments

The following code should calculate the cost of a trip that is 300 miles if gas is $2.50 a gallon and your car gets 36 miles per gallon. However, the code has syntax errors, like missing semicolons, wrong case on names, or unmatched " or (. Fix the code so that it compiles and runs correctly.

Line 5 is missing a semicolon. Line 6 has Double instead of double. Remember that the primitive types all start with a lowercase letter. Line 8 has tripmiles instead of tripMiles. Remember that you should uppercase the first letter of each new word to make the variable name easier to read (use camel case).

This is the answer for the previous question.

Show Comments

The following code should calculate the body mass index (BMI) for someone who is 5 feet tall and weighs 110 pounds. However, the code has syntax errors, like missing semicolons, wrong case on names, or unmatched " or (. Fix the code so that it compiles and runs correctly.

Line 5 has Height instead of height. Remember that variable names should start with a lowercase letter. Line 6 is missing an equal sign. Line 7 is missing a * to square the height. Line 8 is missing a semicolon at the end of the statement.

This is the answer for the previous question.

Show Comments

The following code should calculate the number of miles that you can drive when you have $8.00 and the price of gas is 2.35 and the car gets 40 miles per gallon. However, the code has errors. Fix the code so that it compiles and runs correctly.

Line 5 is missing the type double. Line 6 is backwards. It should be double milesPerGallon = 40;. Line 8 is missing a /. Line 10 is missing a ).

This is the answer to the previous question.

Show Comments

The following code should calculate the cost of an item that is on clearance (70% off) when you also have a coupon for an additional 20% off the clearance price. However, the code has errors. Fix the code so that it compiles and runs correctly.

Lines 5, 6, and 7 should all be double versus int so that the decimal portion of the calculation isn’t thrown away.

This is the answer to the previous question.

Show Comments

The following code should calculate the number of whole days in 320893 seconds. However, the code has errors. Fix the code so that it compiles and runs correctly.

Lines 6 and 7 are both missing a /. Line 8 is missing a (. Line 9 is missing a } to close the main method.

This is the answer to the previous question.

Show Comments

Write the code below to calculate and print how many months it will take to save $200 if you earn $20 a week.

Calculate how many weeks it would take to make $200. Next divide the number of weeks by 4 (roughly the number of weeks in a month).

This is the answer to the previous question.

Show Comments

Write the code to calculate the number of miles you can drive if you have a 10 gallon gas tank and are down to a quarter of a tank of gas and your car gets 32 miles per gallon.

First calculate the number of gallons you have left and then multiply that by the miles per gallon to get the number of miles you can still drive.

This is the answer to the previous question.

Show Comments

Write the code to calculate the number of seconds in 3 days. Remember that there are 60 seconds in a minute and 60 minutes in an hour and 24 hours in a day.

First compute the number of seconds in 1 day and then multiple that by 3 days.

This is the answer to the previous question.

Show Comments

Write the code to print the number of chicken wings you can buy if you have $4.50 and they cost $0.75 each. Remember that you can’t buy part of a wing.

Divide the amount of money you have by the cost of each wing and set the result to an integer since you can’t buy a part of a wing.

This is the answer to the previous question.

Show Comments
You have attempted of activities on this page
Next Section - 1.9. Multiple Choice Exercises