Skip to main content

Java For Python Programmers Edition 2

Section 9.7 Summary & Reading Questions

  1. In Java, every variable must be declared with its type before use; undeclared variables cause compilation errors.
  2. Java requires explicit import statements for classes from external packages (e.g., java.util.Scanner); otherwise, you get "cannot find symbol" errors.
  3. The new keyword is mandatory when creating new objects; forgetting it leads to errors as Java treats the constructor call incorrectly.
  4. Every Java statement must end with a semicolon (;); missing semicolons cause syntax errors.
  5. Java uses generics for type safety in containers like ArrayList; forgetting to specify the contained type leads to compiler warnings about unchecked operations.
  6. Compiler error messages may sometimes be misleading; understanding common mistakes helps quickly identify the root cause.

Reading Questions Reading Questions

1.

What happens if you use a variable in Java without declaring it first?
  • The compiler gives an error indicating the variable cannot be found.
  • Correct! Java requires all variables to be declared before use.
  • The variable is automatically declared as type Object.
  • No. Java does not implicitly declare variables.
  • The program compiles but throws an error at runtime.
  • No. This is a compile-time error.
  • Java ignores the variable and continues compiling.
  • No. Java will stop compiling with an error.

2.

Why must you include import statements for classes like Scanner?
  • Because these classes belong to external packages and are not automatically available.
  • Correct! Java requires explicit imports for external classes.
  • Because Java does not support standard input without imports.
  • No. Standard input is supported but needs the Scanner class explicitly imported.
  • Because the classes are only available in Python, not Java.
  • No. This is a Java-specific requirement.
  • Because the compiler ignores unknown classes without imports.
  • No. It causes a compile error instead.

3.

What warning occurs when you use an ArrayList without specifying a type?
  • An "unchecked" warning indicating potential type safety issues.
  • Correct! Using raw types disables generic type checks.
  • A syntax error.
  • No. This is a compiler warning, not an error.
  • A runtime exception.
  • No. It only warns about possible runtime errors.
  • A logical error in the program.
  • No. The warning points out type safety concerns.
You have attempted of activities on this page.