Java Basics practice questions

From OCA Java SE 7 (1Z0-803) · 23 questions on this topic

Java Basics practice questions from OCA Java SE 7 (1Z0-803). This pack has 23 questions tagged Java Basics, drawn from its timed mock exams. 8 of them are worked through in full below — the question, every option, why each is right or wrong, and the explanation.

Worked examples for Java Basics

  1. Question 1

    A program is launched with the command line `java App one two`. What does args[0] contain inside App's main method?

    1. A. App

      Java does not include the class name in args; the program name is not passed as an argument.

    2. B. java

      The launcher command java is not part of the argument array; args holds only what follows the class name.

    3. C. oneCorrect answer

      args contains only the arguments after the class name, so args[0] is the first of those, "one".

    4. D. two

      "two" is the second argument (args[1]); args[0] is the first argument, "one".

    Explanation

    Unlike C, Java does not pass the program name in the argument array — args holds only the arguments after the class name: args[0] is "one", args[1] is "two".

  2. Question 2

    What is the result of compiling the following program? ```java static import java.lang.Math.abs; public class Main { public static void main(String[] args) { System.out.println(abs(-4)); } } ```

    1. A. 4

      The program would print 4 only if it compiled, but "static import" is the wrong keyword order and fails to compile.

    2. B. -4

      abs returns the absolute value (4), not -4, and in any case the syntax error stops the program from compiling.

    3. C. It compiles with a warning about the import order

      The reversed keyword order is a syntax error, not a warning; the file does not compile.

    4. D. Compilation failsCorrect answer

      The keyword order is fixed as import static, never static import; writing static import is a syntax error (JLS 7 §7.5.3).

    Explanation

    The keyword order is fixed: `import static`, never `static import`. Writing `static import` is a syntax error, not a warning. With the correct order this program would print 4.

  3. Question 3

    Which of the following are keywords (or reserved words) in the Java language?

    1. A. gotoCorrect answer

      goto is a reserved word in Java: it has no function but cannot be used as an identifier (JLS 7 §3.9).

    2. B. constCorrect answer

      const is likewise a reserved word with no use in the language, kept reserved so it cannot serve as an identifier.

    3. C. unsigned

      unsigned is a C/C++ type modifier that Java does not recognize; it is a perfectly legal Java identifier, not a keyword.

    4. D. include

      include is a C/C++ preprocessor directive with no meaning in Java, so it is a legal identifier rather than a keyword.

    5. E. volatileCorrect answer

      volatile is a working keyword in Java, used as a field modifier.

    Explanation

    `goto` and `const` are reserved words in Java — they have no function but cannot be used as identifiers. `volatile` is a working keyword (field modifier). `unsigned` and `include` come from C/C++ and mean nothing to Java; both are legal identifiers.

  4. Question 4

    What is the output of the following program? ```java import java.util.*; public class Main { public static void main(String[] args) { List<String> l = new ArrayList<String>(); l.add("w"); System.out.println(l.get(0)); } } ```

    1. A. Compilation fails because List must be imported explicitly

      A wildcard import makes all types of java.util available, so List needs no separate explicit import.

    2. B. wCorrect answer

      List and ArrayList both resolve from java.util.*, and get(0) returns the stored element itself, "w".

    3. C. Compilation fails because two types cannot come from one import

      A type-import-on-demand brings in every type of the package, not just one, so using both List and ArrayList is fine.

    4. D. [w]

      The bracketed form is how the whole list prints; get(0) returns the single element, so the output is just w.

    Explanation

    A wildcard import makes ALL types of that package available — List and ArrayList both resolve from java.util.* (`Compilation fails because List must be imported...` and `Compilation fails because two types cannot come...` wrong). get(0) returns the element itself, "w", not the bracketed list form (`[w]` would be printing l).

  5. Question 5

    What is the result of compiling the following program? ```java public class Main { String greet() { return "hi"; } public static void main(String[] args) { System.out.println(greet()); } } ```

    1. A. Compilation failsCorrect answer

      greet() is an instance method with no instance to be called on from static main, so the compiler reports it cannot be referenced from a static context.

    2. B. hi

      This assumes greet() runs and returns "hi", but the call never compiles because an instance method cannot be invoked from a static context without an object.

    3. C. null

      There is no null value here to print; the program does not compile, so nothing runs.

    4. D. An exception is thrown at runtime

      The problem is detected at compile time, not at runtime, so the code never reaches execution.

    Explanation

    greet() is an instance method, and the static main has no instance to call it on — "non-static method greet() cannot be referenced from a static context". Fix: new Main().greet().

  6. Question 6

    Which of the following method declarations allow a class to be launched with `java <ClassName>`?

    1. A. public static void main(String[] args)Correct answer

      This is the canonical entry-point signature: public, static, returning void, taking a single String array parameter.

    2. B. static public void main(String... data)Correct answer

      Modifier order does not matter and a varargs String... is accepted as a String array, so this is a valid entry point.

    3. C. public void main(String[] args)

      It lacks static, so the JVM cannot invoke it without an instance and will not launch the class.

    4. D. public static int main(String[] args)

      The entry point must return void; returning int disqualifies it as a launchable main method.

    5. E. public static void main(String args)

      The parameter is a single String rather than a String array, so it does not match the required entry-point signature.

    Explanation

    The entry point must be public, static, return void, and take a single String array — a varargs parameter (String...) counts, and modifier order (static public) is irrelevant, so both `public static void main(String[] args)` and `static public void main(String... data)` work. `public void main(String[] args)` lacks static, `public static int main(String[] args)` returns int, and `public static void main(String args)` takes a single String rather than an array.

  7. Question 7

    What is the result of compiling and running the following program? ```java public class Main { int count = 5; public static void main(String[] args) { System.out.println(count); } } ```

    1. A. 5

      Printing 5 assumes the instance field count is readable from main, but a static method has no instance through which to read it, so the program never runs.

    2. B. Compilation failsCorrect answer

      main is static and has no this, so it cannot reference the instance field count directly ("non-static variable count cannot be referenced from a static context"), which is a compile error (JLS 7 §8.4.3.2).

    3. C. 0

      0 is the default for an uninitialized int field, but the field is explicitly set to 5, and more importantly the static-context reference is illegal so nothing prints.

    4. D. An exception is thrown at runtime

      The problem is detected at compile time, not runtime; the program never reaches execution, so no exception is ever thrown.

    Explanation

    `main` is static, and a static method has no `this` — it cannot read the instance field `count` directly ("non-static variable count cannot be referenced from a static context"). It would need an instance: `new Main().count`.

  8. Question 8

    What is the result of compiling the following program? ```java public class Main { public static void main(String[] args) { final int limit = 5; limit = 6; System.out.println(limit); } } ```

    1. A. Compilation failsCorrect answer

      A final variable may be assigned once, so the reassignment limit = 6 is rejected at compile time.

    2. B. 6

      This assumes the reassignment to 6 succeeds, but assigning again to a final variable is a compile error.

    3. C. 5

      The program never runs; the illegal reassignment of the final variable stops compilation before any output.

    4. D. An exception is thrown at runtime

      The violation is caught at compile time, not thrown at runtime.

    Explanation

    A final variable can be assigned exactly once. The reassignment `limit = 6` is rejected at compile time ("cannot assign a value to final variable limit").

Practise all 23 Java Basics questions

OCA Java SE 7 has the full set, inside timed mock exams that mirror real exam conditions — every question with a worked explanation.

Open OCA Java SE 7

Other topics in this pack