Java Basics practice questions

From OCA Java SE 8 (1Z0-808) · 19 questions on this topic

Java Basics practice questions from OCA Java SE 8 (1Z0-808). This pack has 19 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

    Which of the following is NOT a feature of the Java platform?

    1. A. Platform-independent bytecode

      Platform-independent bytecode is a core selling point of the platform, so it is not the odd one out.

    2. B. Manual deallocation of objects by the programmerCorrect answer

      Java has no free/delete — memory is reclaimed automatically by the garbage collector, so manual deallocation is the one item that is not a Java feature.

    3. C. Encapsulation through access modifiers

      Access modifiers really do provide encapsulation in Java, so this is a genuine feature rather than the exception.

    4. D. A large built-in class library

      The extensive standard library is one of the platform's core strengths, not something Java lacks.

    Explanation

    Java has no free/delete — memory is reclaimed automatically by the garbage collector, and programmers cannot deallocate objects manually. The other three are core selling points of the platform.

  2. Question 2

    A program is launched with the command line: ``` java Main one "two three" ``` What is the value of args.length inside main?

    1. A. 3

      This counts "two three" as two separate arguments, but the surrounding quotes group them into one.

    2. B. 4

      This over-counts, for example by including the class name Main, which is never part of args.

    3. C. 2Correct answer

      Quotes group "two three" into a single argument, so args holds "one" and "two three" — length 2; the class name is not part of args.

    4. D. 1

      This undercounts; there are two arguments, "one" and the quoted "two three".

    Explanation

    Quotes group words into a single argument: args[0] is "one" and args[1] is "two three" — length 2. Without the quotes it would be 3; the class name itself is never part of args.

  3. Question 3

    What is the result of compiling the following program? ```java import java.util.Date; import java.sql.Date; public class Main { public static void main(String[] args) { System.out.println("dates"); } } ```

    1. A. dates

      This would be printed only if the program compiled, but the two single-type imports of Date collide before main ever runs.

    2. B. It compiles; the second import wins

      Single-type imports do not override one another; a duplicate simple name is a compile error rather than a last-one-wins.

    3. C. Compilation failsCorrect answer

      Two single-type imports of the same simple name Date collide, and the compiler reports that Date is already defined.

    4. D. It compiles because neither Date is actually used

      The clash is detected at the import declarations themselves, regardless of whether the Date name is ever used in the body.

    Explanation

    Two SINGLE-TYPE imports of the same simple name collide — the compiler reports that Date is already defined, even if the name is never used (`It compiles because neither Date is actually used` is wrong). Contrast with two WILDCARD imports of packages that both contain Date: that compiles and only errors if you use the ambiguous name.

  4. Question 4

    A source file Zoo.java contains a valid class Zoo with a proper main method. Which command sequence compiles and then runs it?

    1. A. javac Zoo.java, then java ZooCorrect answer

      javac takes the source file name (with .java) and java takes the class name (no extension), which is exactly this sequence.

    2. B. java Zoo.java, then javac Zoo

      This reverses the tools and swaps the arguments: java does not compile, and javac needs the .java source file, not a bare class name.

    3. C. javac Zoo, then java Zoo.class

      javac needs the source file name with .java, not a bare Zoo, and java takes the class name without the .class extension.

    4. D. javac Zoo.class, then java Zoo.java

      javac compiles a .java source, not a .class file, and java runs a class name, not a .java file — both arguments are wrong.

    Explanation

    javac takes the SOURCE file name (with .java); java takes the CLASS name (no extension). Passing Zoo.class to java or bare Zoo to javac are the classic reversed forms.

  5. Question 5

    What is the output of the following program? ```java import java.util.*; import java.sql.Date; public class Main { public static void main(String[] args) { Date d = new Date(0); System.out.println(d.getTime()); } } ```

    1. A. Compilation fails because Date is ambiguous

      An explicit single-type import overrides the wildcard, so Date is unambiguously java.sql.Date; ambiguity would arise only if both Date types came in via wildcards.

    2. B. Compilation fails because java.sql.Date has no constructor taking a long

      java.sql.Date does provide a constructor taking a long, so new Date(0) compiles cleanly.

    3. C. An exception is thrown at runtime

      getTime() on a Date constructed from 0 simply returns 0; nothing throws here.

    4. D. 0Correct answer

      The explicit import makes Date resolve to java.sql.Date; new Date(0) sets the time to 0, and getTime() (inherited from java.util.Date) returns 0.

    Explanation

    An EXPLICIT single-type import always beats a wildcard: Date resolves to java.sql.Date, so there is no ambiguity (`Compilation fails because Date is ambiguous` would apply only with two wildcards). java.sql.Date has a (long) constructor and inherits getTime() from java.util.Date — printing 0.

  6. Question 6

    What is the output of the following program? ```java public class Main { static { System.out.print("1"); } { System.out.print("2"); } Main(String t) { System.out.print(t); } public static void main(String[] args) { System.out.print("3"); new Main("4"); new Main("5"); System.out.println(); } } ```

    1. A. 312425

      This puts the static initializer's 1 after main's 3, but the static initializer runs once at class initialization, before main.

    2. B. 123245

      This runs the instance initializer's 2 before main's 3, but no object is constructed until main calls the constructor.

    3. C. 13245

      This forgets the instance initializer runs for EVERY object, so the 2 is missing before the second construction's 5.

    4. D. 132425Correct answer

      The static initializer runs once before main (1), main prints 3, then each construction runs the instance initializer then the constructor body: 2,4 and 2,5 (JLS 8 §12.4, §12.5).

    Explanation

    The static initializer runs once at class initialization, before main (1). main prints 3. Each construction runs the instance initializer then the constructor body: 2,4 and 2,5. Total: 132425. `13245` forgets the instance initializer runs for EVERY object.

  7. Question 7

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

    1. A. [a]

      This assumes the program runs, but it never compiles because the type List is never imported.

    2. B. Compilation failsCorrect answer

      List has no import of its own, so it is "cannot find symbol" — each type needs its own import or a java.util.* wildcard (JLS 8 §7.5.1).

    3. C. It compiles because List comes along with ArrayList

      Importing ArrayList does not import its interface List; imports never cascade to a type's supertypes.

    4. D. An exception is thrown at runtime

      The failure is a missing import resolved at compile time, not a runtime condition.

    Explanation

    Importing ArrayList does NOT import its interface List (`It compiles because List comes along with ArrayList` is wrong) — each type needs its own import (or a java.util.* wildcard). List is "cannot find symbol" here.

  8. Question 8

    What is the output of the following program? ```java public class Main { static StringBuilder log = new StringBuilder(); static { log.append("S"); } { log.append("I"); } Main() { log.append("C"); } public static void main(String[] args) { log.append("M"); new Main(); System.out.println(log); } } ```

    1. A. SIMC

      This places the instance-initializer marker immediately after the static one, as if instance initialization were part of class loading. Instance initializers run only when an object is created, which here happens after main has already appended its own marker.

    2. B. SMICCorrect answer

      The static initializer runs once at class load, then main begins and appends its marker, and only then does constructing the object run the instance initializer before the constructor body (JLS 8 §12.4, §12.5).

    3. C. MSIC

      This runs main before the static initializer. Static initialization completes when the class is first loaded, which necessarily happens before main can be invoked, so its marker cannot come first.

    4. D. SICM

      This runs the instance initializer and constructor before main appends its own marker. In fact main appends first, and only the subsequent object creation triggers instance initialization and the constructor.

    Explanation

    Static initializers run once when the class is first loaded, before any method executes. main then begins and appends its own marker before creating any object. Only when an instance is actually constructed do the instance initializer block and then the constructor body run, in that order. The result is class-load work first, main-method work next, and per-object initialization last.

Practise all 19 Java Basics questions

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

Open OCA Java SE 8

Other topics in this pack

Java Basics — 1Z0-808 practice questions with explanations · TestHoop