Skip to content
ExamSpine

Lesson 1.6 · Write it

Swap lines

A whole programPython or Java5 tests, 3 of them hidden

On a phone? Put the lines in order instead

Write a program that reads two lines typed by the user, one after the other, and prints them in the opposite order: the second line first, then the first line.

  1. The first line typed is printed second, and the second line typed is printed first.
  2. Each line is printed exactly as it was typed.
  3. Print nothing else. Spaces at the end of a line are ignored.

Your program is run several times, with different lines typed each time. Only what it prints is checked.

In Python, read each line with input() and save it in a variable. In Java, the page starts you with a class, its main method and a Scanner called scanner: read each line with scanner.nextLine() and save it in a String variable.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • two words, printed in the opposite order (rule 1)

    You type

    1. typed: Hello
    2. typed: World

    It prints

    World
    Hello
  • another pair, swapped (rule 1)

    You type

    1. typed: First
    2. typed: Second

    It prints

    Second
    First
solution.pySolution.java