4.13. 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 have an extra block that isn’t needed in the correct solution. Try to solve these on your phone or other mobile device!

        4-11-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
        4-11-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");
        4-11-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
        4-11-4: The main method in the following class should print the first 2 characters of the message followed by the last 2 characters of the message. 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 message = "I hope this works"; --- String part1 = message.substring(0,2); String part2 = message.substring(message.length() - 2); --- System.out.println(part1 + part2); --- } // end main method --- } // end class --- System.out.println(part1 * part2); #distractor
        4-11-5: 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
        4-11-6: 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
        4-11-7: The main method in the following class should print the initials in lowercase letters. 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 first = "Gerald"; String middle = "Foster"; String last= "Jones"; --- String initials = first.substring(0,1) + middle.substring(0,1) + last.substring(0,1); --- String lowerInitials = initials.toLowerCase(); --- System.out.println(lowerInitials); --- } --- } --- System.out.println(initials); #distractor
        4-11-8: The main method in the following class should print the message in all uppercase letters. 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 message = "Don't Pokemon and drive!"; --- String upperMessage = message.toUpperCase(); --- System.out.println(upperMessage); --- } --- } --- System.print(upperMessage); #distractor
        4-11-9: The main method in the following class should print the first 3 letters of message in uppercase letters. 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 message = "Have a nice day!"; --- String part = message.substring(0,3); --- String upper = part.toUpperCase(); --- System.out.println(upper); --- } --- } --- String part = message.substring(0,4); #distractor
        4-11-10: The main method in the following class should print the part of the message starting with the word "nice".  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 message = "Have a nice day!"; --- int pos = message.indexOf("nice"); --- System.out.println(message.substring(pos)); --- } --- } --- int pos = message.indexof("nice"); #distractor
Next Section - 5. Conditionals