Choosing an exam: Java Foundations vs OCP 11

Update (11/05/2020): Read The 1Z0-819 Exam page to learn how you can easily our Java 11 Study Guides to prepare for Oracle’s 1Z0-819 Exam, as well as the 1Z0-817 Upgrade Exam.

Three years ago, I wrote about why you should take the OCA 11 rather than the Java Foundations exam. Now that the Java 11 cert is out, the situation changes.

Java Foundations vs OCA 8 (1Z0-815)

Until the OCA 8 is discontinued, I think everyone considering the Java Foundations exam should take the OCA 8. The reasons in my original blog post still apply:

  • The OCA 8 is not that much more difficult than the Java Foundations exam. It’s definitely in reach for an entry level programmer.
  • Taking the OCA 8 lets you use it as a pre-req for a later exam. You can take either the OCP 8 (IZ0-809) or OCP 11 part 2 (IZ0-816). This means you will only need to take one more exam to become OCP certified.

So when should you take the Java Foundations exam:

  • Cost: The Java Foundations exam costs less than half the price of the OCA 8. If you can’t afford the OCA, this is an option.
  • Requirement: If you took a course that requires the Java Foundations or lets you take it for free.

Java Foundations vs OCP 11 part 1 (1Z0-815)

With Java 11, it’s a harder decision deciding which exam to take because:

  1. The OCP 11 part 1 (1Z0-815) is significantly harder than the OCA 8 (1Z0-808). The OCP 11 part 1 is still in reach of an entry level programmer, but it is more work.
  2. Passing the OCP 11 part 1 does not give you a certification. Another (even harder exam) is needed for that.

So who should you take the Java Foundations exam:

  • Time: If you don’t have the time to study for two harder exams, the Java Foundations exam gets you a certification faster.
  • Cost: The Java Foundations exam costs less than half the price of the OCP 11 part 2 and a quarter the cost of the OCP credential.
  • Requirement: If you took a course that requires the Java Foundations or lets you take it for free.

Just be aware that taking the Java Foundations exam does not serve as a prerequisite to later certification exam. If you plan to get OCP certified in the future, be aware that the Java Foundations exam is a one off.

comparing strings with compareTo

Thinking about using our OCA 8 book to study for the Java Foundations Junior Associate exam? It covers most of the topics. See what other topics you need to learn and where to read about that. One of those topics is comparing two Strings using compareTo(). Luckily there are only two things you need to know.

What does compareTo() return.

Scenario Result
The two Strings are equal 0
The first String sorts earlier alphabetically  a negative number
The first String sorts later alphabetically a positive number

Let’s look at some examples:

String first = "abc";
String second = "def";
System.out.println(first.equals(first));  // true
System.out.println(first.compareTo(first)); // 0

System.out.println(first.equals(second));  // false
System.out.println(first.compareTo(second)); // -3
System.out.println(second.compareTo(first)); // 3

Don’t worry. You don’t have to know the answer is -3. You just have to know that it is negative or positive or zero.

How is alphabetically defined?
For the exam, you need to know that numbers sort before letters and uppercase sorts before lowercase. What do you think the output of this code is?

public class PlayTest {

	public static void main(String[] args) {

		String nums = "123";
		String uppercase = "ABC";
		String lowercase = "abc";
		printComparison(nums, nums);
		printComparison(nums, uppercase);
		printComparison(nums, lowercase);
		System.out.println();
		printComparison(uppercase, nums);
		printComparison(uppercase, uppercase);
		printComparison(uppercase, lowercase);
		System.out.println();
		printComparison(lowercase, nums);
		printComparison(lowercase, uppercase);
		printComparison(lowercase, lowercase);
	}

	private static void printComparison(String one, String two) {
		int result = one.compareTo(two);
		if (result == 0) {
			System.out.println("0");
		} else if (result < 0) {
			System.out.println("negative");
		} else {
			System.out.println("positive");
		}
	}

The answer is:

0
negative
negative

positive
0
negative

positive
positive
0

Make sure you can fill this in by yourself. You should also know the space sorts before letters. For example, printComparison(“ABC”, “A C”); prints out a positive number.

Summary

There’s not much you have to memorize. The key facts are:

  • The method name is compareTo()
  • Numbers sort before capital letters which sort before lowercase letters
  • Spaces sort before letters

Practice Questions

Question 1

What does the following output? (Choose all that apply)

String first = "MOO";
String second = "moo";
System.out.println(first.compareTo(second));
System.out.println(first.equals(second));

A: A negative number

B: Zero

C: A positive number

D: true

E: false

F: none of the above

Question 2

What does the following output? (Choose all that apply)

String first = "moo";
String second = " moo";
System.out.println(first.compareTo(second));
System.out.println(first.equals(second));

A: A negative number

B: Zero

C: A positive number

D: true

E: false

F: none of the above

Question 3

What does the following output? (Choose all that apply)

String first = "MOO";
String second = "MOO";
System.out.println(first.compareTo(second));
System.out.println(first.equals(second));

A: A negative number

B: Zero

C: A positive number

D: true

E: false

F: none of the above

Question 4

What does the following output? (Choose all that apply)

String first = "moo";
String second = "MOO";
System.out.println(first.compare(second));
System.out.println(first.equals(second));

A: A negative number

B: Zero

C: A positive number

D: true

E: false

F: none of the above

Question 5

What does the following output? (Choose all that apply)

String first = "MOO";
String second = "1";
System.out.println(first.compareTo(second));
System.out.println(first.equals(second));

A: A negative number

B: Zero

C: A positive number

D: true

E: false

F: none of the above

The answers are posted here.

using iterators in java

Thinking about using our OCA 8 book to study for the Java Foundations Junior Associate exam? It covers most of the topics. See what other topics you need to learn and where to read about that. One of those topics is iterating through a list.

Since Java 5, the most common way to iterate though a list (if you don’t need the loop index) is:

List<String>  list = Arrays.asList("sheep", "deer", "rat");
for (String name : list) {
   System.out.println(name);
}

Before Java 5 came along, there was another way:

List list = Arrays.asList("sheep", "deer", "rat");
 Iterator it = list.iterator();
 while (it.hasNext()) {
   String name = (String) it.next();
   System.out.println(name);
 }

But we are told to use generics in new code which would give us:

List<String> list = Arrays.asList("sheep", "deer", "rat");
 Iterator<String> it = list.iterator();
 while (it.hasNext()) {
   String name = it.next();
   System.out.println(name);
 }

Why would you do this? Shrug. For reading old code I guess. But it is on the test so no time like the present to learn this idiom. See what is wrong here?

// DOES NOT COMPILE
 List<String> list = Arrays.asList("sheep", "deer", "rat");
 Iterator<String> it = list.iterator();
 while (it.next()) {
   String name = it.hasNext();
   System.out.println(name);
 }

This one reverses the order of hasNext/next. Remember that you have to check that the iterator has a next element before getting it. Now what do you think is the problem with this?

// BAD
 List<String> list = Arrays.asList("sheep", "deer", "rat");
 Iterator<String> it = list.iterator();
 System.out.println(it.next());

If the list is empty, this code throws an exception. Probably not what you had in mind. Instead, you should write:


 List<String> list = Arrays.asList("sheep", "deer", "rat");
 Iterator<String> it = list.iterator();
 if (it.hasNext()) System.out.println(it.next());

Summary

There’s not much you have to memorize. The key facts are:

  • Call hasNext() in an if statement or while loop
  • Call next() once you know there is a next element
  • If the Iterator doesn’t use generics, you must cast unless you want Object

Practice Questions

Question 1

Which correctly fill in the blanks?


 List<String> list = Arrays.asList("a", "b", "c");
 Iterator<String> it = list.iterator();
 while (it._____()) {
   String name = it.______();
   System.out.println(name);
 }

A: hasNext, next

B: next, hasNext

C: The code does not compile with either A or B

D: The code throws an exception after being completed with A or B

Question 2

Which correctly fill in the blanks?


 List<String> list = Arrays.asList("a", "b", "c");
 Iterator it = list.iterator();
 while (it._____()) {
   String name = it.______();
   System.out.println(name);
 }

A: hasNext, next

B: next, hasNext

C: The code does not compile with either A or B

D: The code throws an exception after being completed with A or B

Question 3

Which correctly fill in the blanks?


 List list = Arrays.asList("a", "b", "c");
 Iterator<String> it = list.iterator();
 while (it._____()) {
   String name = it.______();
   System.out.println(name);
 }

A: hasNext, next

B: next, hasNext

C: The code does not compile with either A or B

D: The code throws an exception after being completed with A or B

The answers are posted here.