Tracing print Statements in Java
System.out.print("\t****\n********");
4 blank spaces appear in the first line of output due to the escape sequence \t that causes a tab of 4 characters. Since the escape sequence \n produces a new line, the 8 asterisks are on a new line of output.
System.out.print("HELLO WORLD");
HELLO WORLD
System.out.print("HeLlo WoRlD");
HeLlo WoRlD
System.out.print("Hello World");
Hello World
System.out.print("\u001B[31mh\u001B[32me\u001B[33ml\u001B[34ml\u001B[35mo \u001B[36mw\u001B[37morld\u001B[0m");
Research the ANSI color codes 31-37 and 0 (reset) to learn the associated colors.
System.out.print("That\'s great!");
That's great! The \' escape sequence produces a single quote (or apostrophe).
System.out.print("h\tw");
The \t escape sequence produces a tab. In this case, there are 3 blank spaces between the letters. If the system's tab size is 4 then that tab size of 4 minus 1, the length of "h", is 3. Students should mostly know that there is a gap of whitespace between the outputted letters.
System.out.print("h\t\tw");
The \t escape sequence produces a tab. In this case, there are 7 blank spaces between the letters. If the system's tab size is 4 then that tab size of 4 minus 1, the length of "h", is 3 plus another full tab of 4 is 7 total. Students should mostly know that there is a large gap of whitespace between the outputted letters.
System.out.print("hello\fworld");
The escape sequence \f produces a form feed which is old-fashioned typewriter talk for moving the cursor down a line but not to the left edge of that new line. So the word world is horizontally to the right of hello.
System.out.print("hello\nworld");
The escape sequence \n causes a new line to be produces between the two words so the output appears on two separate lines.
System.out.print("hello\n\tworld");
The escape sequence \n causes world to be on a new line of output and the escape sequence \t causes it to be tabbed in an amount depending on the operating system (such as 4 or 8 spaces.)
System.out.print("What/'s up?");
What/'s up? The forward slash should be a backslash since \ is the escape operator in Java and needs to precede special characters like apostrophes. So the output displays as typed in the print statement resulting in a probable "logic" error rather than a "syntax" error.
System.out.print("\\hello\\world\\");
\hello\world\ Each escape sequence \\ produces a single backslash in the output.
System.out.print(hello world);
error Double quotes are required around "hello world"
System.out.print('hello world');
error Double quotes are required with Java print statements rather than single quotes (like the Python programming language allows.)
System.out.print"(hello world)";
error The double quotes must be nested inside the set of parentheses.
System.out.print("hello world)";
error The double quotes must be nested inside the set of parentheses.
System.out.print("hello\\\world");
error The first two backslashes would work together as an escape sequence to produce a single backslash in the output. But since the 3rd backslash is not followed by a valid escape character (w does not form any valid escape sequences in Java).
System.out.print("hello world');
error The first, opening double quote symbol must be paired with a closing double quote, not a single quote symbol.
System.out.prnt("hello world");
error print is misspelled as prnt
System.out.print("h"); System.out.print("\ne"); System.out.print("\nl"); System.out.print("\nl"); System.out.print("\no");
h e l l o
System.out.print("h\n"); System.out.print("e\n"); System.out.print("l\n"); System.out.print("l\n"); System.out.print("o");
h e l l o
System.out.println("h"); System.out.println("e"); System.out.println("l"); System.out.println("l"); System.out.print("o");
h e l l o
System.out.print("\to\b\bl\b\bl\b\be\b\bh");
hello
System.out.print("h"); System.out.print("e"); System.out.print("l"); System.out.print("l"); System.out.print("o");
hello
System.out.print("\u001B[34mhello\u001B[0m");
hello 34 is the ANSI color code for blue and 0 is the reset color code.
// sleep requires 'throws Exception' with main // research the sleep method if necessary // describe how the output is displayed System.out.print("h"); Thread.sleep(1000); System.out.print("e"); Thread.sleep(1000); System.out.print("l"); Thread.sleep(1000); System.out.print("l"); Thread.sleep(1000); System.out.print("o");
hello Displays 1 character at a time with 1 second delays in between.
// Research the sleep method. // Fully describe how the output is displayed. System.out.print("h"); Thread.sleep(1000); System.out.print(" o"); Thread.sleep(1000); System.out.print("\b\b\b\be"); Thread.sleep(1000); System.out.print(" l"); Thread.sleep(1000); System.out.print("\b\bl");
hello The characters display from outside in with 1 second delays in this order: h, o, e, l, & l
System.out.print("hello \"world");
hello "world The \" escape sequence produces a single double quote symbol awkwardly between the two words. Note that there is 1 embedded blank space in between the words and before the double quote symbol.
System.out.print("hello \"world\""");
hello "world" Each \" escape sequence produces a single double quote symbol. So the word world appears in the output with an actual set of double quotes around it.
System.out.print("hello World");
hello World
System.out.print("hello world");
hello world
System.out.print("hello\"world");
hello"world The \" escape sequence produces a single double quote symbol awkwardly between the two words and there is no embedded blank space in this case.
System.out.print("hello-world");
hello-world
System.out.print("hello\\\norld");
hello\ orld The first two backslashes would work together as an escape sequence to produce a single backslash in the output. But since the 3rd backslash is followed by a valid escape character n, the escape sequence \n produces a new line and drops the remaining letters orld to the next line. It is not a "syntax" error but a probable "logic" error with meaningless or misspelled output.
System.out.print("hello\\world");
hello\world The escape sequence \\ produces a single backslash in the output.
System.out.print("helloworld");
helloworld
System.out.print("hllo worldd");
hllo worldd There is no "syntax" error but it could be considered a "logic" error since two English words are misspelled.
System.out.print("hello\b\b\b\bw");
hwllo Each escape sequence \b produces a single backspace. So the e of hello is overwritten by the w since 4 backspaces are included.
System.out.print("hello\rw");
wello The escape sequence \r produces a carriage return which is old-fashioned typewriter talk for moving the cursor to the left edge of the SAME line effectively allowing the h in hello to be printed over by the letter w.
System.out.print("hello\rworld");
world The escape sequence \r produces a carriage return which is old-fashioned typewriter talk for moving the cursor to the left edge of the SAME line effectively allowing hello to be printed over by the word world.