Hi Omar Talabay,
First of all, a warm welcome to CodeRanch!
Omar Talabay wrote:The answer at the end of the book is option E.
However, both code snippests start with line number 1. In addition, no imports and package names were included. Therefore, the right answer is G, the code does not compile.
You are
incorrect! The correct answer in the book is spot-on! The code will print
"u u ucrcr" on execution.
Although it is a very good practice to add a
package statement to your classes, it is not required. If you don't have a
package statement, the class will be in the unnamed package also known as the default package. And adding
import statement to a source code file is also not a requirement. You only need to add an
import statement if you want to use the type by its simple name. If you for example want to use
List instead of
java.util.List, you need to add this
import statement to your source code file
But the code snippets in this question only contain classes from the
java.lang package and this package is automatically imported by default. So you do not need to
import java.lang.*; to use them without fully qualified names.
Hope it helps!
Kind regards,
Roel