Wednesday, 18 November 2026

1Z0-831 exam review

1Z0-831 exam review – OCP Java SE 25 Developer

I recently took and passed the Oracle Java SE 25 Developer Professional (1Z0-831) exam. Having previously passed the Java SE 7, 8, 11 and 17 certifications, I thought I'd share my experience once again, including how this exam compares with previous versions, the resources I used, and some lessons learnt along the way.

Java has changed considerably since I first started taking these exams, and so has the way we develop software. With AI now capable of generating entire classes, tests and even applications, you might wonder whether a programming certification is still worth pursuing. I'll come back to that later.

📋 Notes on the exam

Unlike the old upgrade exams, which focused on changes introduced since a previous Java version, the Java SE 25 certification covers a broad range of the language.

That means revisiting familiar topics such as inheritance, collections, generics and exception handling, alongside newer features including stream gatherers, scoped values, flexible constructor bodies and the latest additions to the Java APIs.

The challenge isn't necessarily understanding what a feature does. It's understanding precisely how it behaves, particularly when combined with other language features.

You might be perfectly comfortable using streams in your day-to-day work, for example, but that doesn't necessarily mean you'll immediately spot the implications of a parallel stream, an overloaded method reference or a particular collector.

As with previous certifications, the exam tests aspects of Java that you might rarely encounter in normal development. Some questions feel rather artificial, but they do force you to understand the language beyond its most common use cases.

📚 Revision

I approached this exam using a combination of study guides, practice exams, ChatGPT and runnable Java examples.

My initial focus was refreshing my knowledge of the language, particularly areas I don't regularly use. I then concentrated on Java 25-specific material before moving towards mock exams and reviewing the results.

I used the following resources.

OCP Oracle Certified Professional Java SE 21 Developer Study Guide – 3/5 ⭐⭐⭐

OCP Java SE 21 Developer Study Guide

By Scott Selikoff and Jeanne Boyarsky.

I've used several editions of these authors' books over the years and have always appreciated their conversational writing style. They make complicated subjects approachable, with plenty of examples and questions to reinforce the material.

However, I felt this edition hadn't moved on significantly from the Java SE 17 book.

That's not necessarily a criticism of the authors, as much of the language remains the same. Nevertheless, I felt the certification questions had become more demanding in their treatment of edge cases and combinations of language features.

The book remains a good foundation, particularly if you need to refresh the core APIs, streams, concurrency, modules and other established topics. However, I wouldn't rely on it alone when preparing for Java SE 25.

It's worth mentioning that I haven't taken the Java SE 21 exam, so my comparison is with the Java 17 material and my experience of the Java 25 exam.


OCP Java 25 Exam Fundamentals – 4/5 ⭐⭐⭐⭐

OCP Java 25 Exam Fundamentals

By Hanumant Deshmukh.

This was a much more targeted resource for the exam I was preparing for.

Whereas the Java 21 book provided a broad foundation, this book helped identify newer features, relevant APIs and details that might otherwise have escaped my attention.

The suggested study plan was around two hours per night for six months. Personally, I worked through all the chapters apart from the introductory Kickstarter chapter in approximately three weeks.

I didn't complete the end-of-chapter exercises, choosing instead to spend that time on practice exams and investigating questions I got wrong. That was largely a time-management decision rather than a reflection on the exercises themselves.

The advantage of this approach was that I could cover the material relatively quickly and identify weaknesses through testing. The disadvantage is that reading alone can create a false sense of understanding. It's easy to recognise a concept on the page without being able to apply it to an unfamiliar question.

This book complemented the Java 21 material well and felt more closely aligned with the certification.


Enthuware Java SE 25 Practice Exams – 5/5 ⭐⭐⭐⭐⭐

Enthuware OCP Java SE 25 Practice Exams

Enthuware OCP Java Programmer SE 25 Practice Exams

These were easily my most valuable practice resource.

The questions were very relevant to the exam, both in terms of subject matter and difficulty. They also exposed areas that weren't covered in sufficient depth by the Java 21 study guide.

The practice exams provided a useful indication of my progress as I worked through them. However, the greatest benefit wasn't the score at the end of each test. It was the time spent reviewing the answers afterwards.

For every incorrect answer, I wanted to understand why my reasoning was wrong. Sometimes I'd misunderstood a rule. Other times I'd overlooked something subtle, such as the declared type of a reference, a checked exception, method overload resolution or the ordering guarantees of a stream.

I also reviewed questions I'd answered correctly but wasn't entirely confident about.

The downside is that this process can be time-consuming. A practice exam might take a couple of hours, but reviewing it properly could take considerably longer.

Nevertheless, I found the Enthuware questions very similar to the real exam in length, difficulty and subject matter. I'd strongly recommend them.

Other practice exams

I also used some Udemy practice tests.

These were useful for additional exposure to questions, although the quality varied. Some contained poorly formatted code, questionable answers or explanations that didn't adequately justify the result.

Others were considerably more challenging and provided useful practice with complicated language behaviour.

The important lesson here is not to assume every practice question is correct simply because it appears in a paid course.

If an explanation doesn't make sense, verify it against the Java documentation or run the code yourself.

🧠 Using ChatGPT to review questions

This was probably the biggest change to my revision process compared with previous certifications.

After completing a practice exam, I'd work through the questions with ChatGPT, explaining my reasoning and asking it to challenge my understanding.

Rather than simply asking for the correct answer, I found it more useful to investigate questions such as:

  • Why does this overload get selected rather than another?
  • Would the result change if the reference type were different?
  • Does this compile, and if not, which rule prevents it?
  • Is the behaviour guaranteed, or merely one possible outcome?
  • What would happen if the stream were parallel instead of sequential?

This was particularly useful for concurrency, streams, generics and some of the more obscure Java APIs.

Occasionally, ChatGPT would get an answer wrong or overlook a detail itself. That was actually a useful reminder that AI-generated explanations need to be checked, just like AI-generated code.

When in doubt, I'd write a small example and run it in my IDE.

This combination of discussion, experimentation and referring back to the documentation was where I felt my understanding improved most.

⏱️ Taking the exam

The Java SE 25 exam allows two hours for 50 questions, with a pass mark of 68%. That means you can answer 16 questions incorrectly and still pass.

Compared with the 90 minutes available for the Java SE 17 exam, you might expect the additional half-hour to make things more comfortable. I certainly did, but two hours can still be tight.

Some questions contain 30–50 lines of Java code, followed by six or more possible answers, each potentially containing another 15–20 lines. Others involve complicated loops where you need to track the value of several variables through each iteration.

There are also questions with as many as ten answer options, which can look daunting at first.

My advice is to be strict with your time.

If you encounter an enormous question, give it a quick scan. Sometimes the code looks intimidating but the actual question is relatively straightforward. If it's clearly going to require several minutes of analysis, mark it and move on.

Remember, you can afford 16 incorrect answers. Don't sacrifice several straightforward questions trying to solve one particularly difficult example.

I approached the exam by collecting the quicker wins first, then returning to the longer, more complicated questions once I'd worked through the paper.

After tackling those, I revisited questions I'd answered but flagged because I wasn't entirely confident. That's when my time ran out.

So although two hours sounds generous, it certainly doesn't feel that way if you spend too long on the wrong questions.

💡 A few practical tips

Don't assume long code means a difficult question. Some examples contain a lot of boilerplate, such as record or POJO declarations, which you can often skim. Of course, if the question is specifically testing record construction or class design, you'll need to pay closer attention.

Look at the possible answers early. If compilation failure isn't an option, that can be a useful clue about what the question is testing, although it doesn't guarantee the code compiles. If compilation failure is an option, watch carefully for invalid method calls, incompatible types and other compilation issues.

Follow the execution path. Start with the main() method and work through the code. If you finish without having examined a section that appears relevant, ask yourself why. Have you overlooked a method call or misunderstood the control flow?

Use the drawing tool. The exam provides a drawing facility. It's a little clunky, but it can help with questions involving loops, variable changes and complicated execution paths. Sometimes drawing the flow, or even quietly reading it through to yourself, helps make sense of what's happening.

Don't let one question affect the next. Personally, I can get into a bit of a funk when I'm struggling with a difficult question. It's important to recognise that and move on. Treat each question as a fresh challenge.

I had one question where returning to it later made all the difference. I'd overlooked a break statement associated with a label, which completely changed the output. With a fresh perspective, the answer became much clearer.

Sometimes stepping away from a question is the best thing you can do.

🎯 Exam difficulty

I found Java SE 25 harder than Java SE 17, although not necessarily because the subject matter itself is more difficult.

Both exams cover a broad range of Java, but the style of the questions has changed.

Some Java 25 questions are considerably longer, and what they're testing isn't always immediately apparent. A single example might combine several niche language rules, requiring you to understand how they interact.

That said, not every question is difficult.

There are plenty of straightforward questions where knowing the relevant API or language rule makes the answer obvious. In some ways, the breadth of the syllabus works in your favour because it ensures a mixture of topics and difficulty levels.

It's also worth checking the current exam objectives rather than assuming everything from previous certifications remains relevant. Topics such as JDBC and some older APIs are no longer part of the syllabus, while newer language and library features have taken their place.

I wouldn't recommend trying to memorise every obscure API method. Instead, focus on understanding the underlying rules and recognising what a question is actually testing.

The hardest part is often identifying the important detail among all the surrounding code.

🤖 Is Java certification still worth it in the age of AI?

I'd say so, perhaps more than ever.

AI tools are increasingly capable of generating code, suggesting refactorings, writing tests and even implementing entire features. It's tempting to conclude that developers no longer need such detailed language knowledge when an AI assistant can produce the implementation for them.

I see it differently.

I've interviewed plenty of developers over the years who have struggled with relatively straightforward coding challenges. Some of the more difficult questions in this certification aren't far removed from those challenges, except that you're expected to solve them in a fraction of the time.

Of course, being good at certification questions doesn't automatically make someone a good software engineer. Real-world development involves architecture, collaboration, maintainability and understanding business requirements.

But I do think strong programming fundamentals are becoming increasingly important as AI takes on more of the implementation work.

The best developers using agentic coding tools won't simply be those who can write effective prompts and accept whatever code comes back.

They'll be the developers who can challenge the AI, pair program with it and improve the quality of its output.

That means recognising potential bugs, questioning assumptions, identifying concurrency issues, enforcing coding standards, choosing sensible architectural patterns and ensuring that the implementation is appropriate for the system.

AI-generated code might compile and pass its tests while still containing subtle defects or introducing unnecessary complexity.

If you don't understand the language well enough to recognise those problems, how can you confidently review or maintain the solution?

In my opinion, the future isn't about developers competing with AI to write code faster. It's about developers becoming more effective by combining their own knowledge and judgement with the capabilities of AI.

A certification doesn't prove that someone can do all of this, but it does encourage a deeper understanding of the language and provides an objective way of demonstrating some of that knowledge.

For me, that's still a worthwhile investment.

🔄 What I'd do differently

Looking back, the most effective part of my preparation was the combination of realistic mock exams and thorough reviews.

If I were preparing again, I'd probably spend less time reading material I was already familiar with and move towards targeted practice sooner.

I'd still recommend a study guide for the foundations, but I'd use the official objectives to identify gaps rather than treating every chapter as equally important.

I'd also continue writing small Java examples to investigate behaviour I didn't fully understand.

It's one thing to read that a particular method returns an int rather than a long, or that a collection doesn't guarantee ordering. It's another to see the implications for yourself.

Ultimately, I found that understanding my mistakes was more valuable than simply completing more questions.

💻 Example code and study notes

I've made my Java SE 25 notes and runnable examples available on GitHub:

Java-SE25-Exam – GitHub repository

The repository includes notes organised around the Java SE 21 study guide, a Java 25 addendum covering newer language features, and runnable examples grouped by topic.

I've also included a more detailed review of the study resources I used, including their advantages and disadvantages.

Hopefully, some of it will be useful to anyone preparing for the certification, or simply looking to refresh their Java knowledge.

Tuesday, 26 September 2023

1Z0-829 exam review

I took and passed the "1z0-829 OCP Java SE 17 Developer" exam last week. I've previously passed the 1z0-817, 1z0-810, 1z0-804 and 1z0-803 exams for the Java SE 11, 8 and 7 (Programmer I and Programmer II) certifications respectably. As a veteran of these exams I thought I'd share my experience.

Notes on the exam and the purchase

Firstly, as someone who has already achieved the 'Developer' certification on a previous version, I have become accustomed to taking 'upgrade' exams wherein the topics are specifically the changes in the language. E.g., Java SE 8 was very functional programming heavy and Java SE 11 had a lot of focus on modularity. 1z0-829 covers the entire spectrum of the language making the topics and possible questions faced much broader.

There can be confusion after purchasing the exam which I want to mention here as there was little help online. After you purchase your exam voucher you will be the administrator to that voucher. As the administrator you will need to assign that exam attempt to yourself via the Oracle Exam Attempt Administrator tool before it shows up on the Oracle University site. Also note that Pearson VUE do not support this exam. You need to schedule within the Oracle University site only.

Is this still worth doing!? I'd say so. Personally I like to take these exams as it keeps my knowledge current and fresh. Plus it looks great on the CV! With releases every 6 months and LTS versions every 3 years Oracle are keeping Java alive and kicking. Many libraries and languages have come around due to Java limitations. Oracle tend to see what is popular and put it in - and now at a more regular basis, e.g.:

  • Google Guava date libraries over java.util.Date and java.lang.Calendar classes due to lack of features - Java Date/Time API in SE 8
  • Scala functional programming - Java added this in SE 8
  • Kotlin concise language features - examples of this are the var keyword added in SE 10, records added SE 11, sealed classes in SE 17

Revision

As mentioned, the exam topics are broad covering many aspects of the language. Newer features of the language are covered so make sure you understand switch expressions, text blocks, pattern matching (with instanceof), and records. Hopefully everything else is just a refresher.

I used the following resources for my studies:

Java SE 17 Developer (1Z0-829) (Video Course) By Simon Roberts: This chap dives into the details using presentation slides, whiteboards and code examples. With 31 lessons adding up to 21 hours it may border on the excessive. There were topics he covered which he mentioned would not be on the exam (and some he didn't but weren't either! This may not be the case for you but I wouldn't spend too much time on Semaphores, Phasers or Exchangers for example). Simon is a former employee of Sun and author of exam questions, so I recommend these videos as accompanied by his whiteboard and 'question times', he does help the viewer understand more complex topics. You can also speed up the videos to reduce that 21 hour watch time!

OCP Oracle Certified Professional Java SE 17 Developer Study Guide: Exam 1Z0-829: After using Scott Selikoff and Jeanne Boyarsky's previous OCP book for SE 11 I knew what I was getting here. The writing style is very conversational, making even the more complicated subjects simple via their more human step by step guidance. Again, accompanying the book are the flashcards and over 1000 exam questions via the Wiley site. These questions are much more difficult than the actual exam, with longer code examples and more choices. My main gripe with this book is the section on Modules. The example of a zoo is quite longwinded and many of the explanations and diagrams seem unnecessary. I understood the concepts but I feel it could have been done more concisely.

There are a few program errors in both which is disappointing but nothing major. If you are unsure of something then best run it in your IDE or read the documentation to be sure.

For me I spent one month preparing for this, refreshing on some of tricky edge cases of the language that never come up in day-to-day life, and understanding the new language enhancements.

Taking the exam

This is a proctored exam taken from home. You need to make sure you personal computer can run the test software. It seems to be a browser-like full screen app that include Zoom-like web cam functionality. You cannot actually install this until the check-in. You can check-in 30 min before the exam and wait in a lobby (oddly shared with other candidates tuning their web cam settings). I was expecting to have to share evidence of my room and ID but neither were requested. The admin gave me a code at the time of my exam to start the test. I did not have any papers to hand and did not notice any drawing or note taking features to the test software. In either case, don't let yourself down by giving any doubt to your honesty and abide by the rules.

Exam difficulty

The pass mark is a little higher for this one compared to previous exams with 68% required from 50 questions. That means you need 34 from 50. The time is only 90 min so do not spend too much time on any question. There should be many questions you'll be able to identify the solution within a matter of seconds so it should level things out. I finished with 20 min spare to go over flagged answers.

Example code

I have example code for the new features (from Java SE 11 to 17) available in this repository.

Wednesday, 11 November 2020

1Z0-817 exam review

I took and passed the "1z0-817 Upgrade OCP Java 6, 7 & 8 to Java SE 11 Developer" exam this week. I previously held the OCP (Oracle Certified Professional) Java 8 (Note: Oracle renamed the OCA (Oracle Certified Associate) and OCP to Programmer I and Programmer II respectively). In this post are some of my thoughts and recommendations.

Revision

The exam topics are purposely vague. This is so Oracle can tweak the exam and test you on broader concepts.

1Z0-817 is specifically an upgrade exam from previous OCP versions to Programmer II in Java 11. Documentation and guides are more abundant for the full Programmer I and II exams (1Z0-815 and 1Z0-816) as there is a higher demand.

These two aspects make studying for 1Z0-817 more challenging. I found and used the following resources:

Oracle University: If you have the funds then the Java University subscription is a great option. Oracle added hundreds of hours of new videos this summer as part of their site and content revamp. You can pick and choose the videos relevant to the course. They also have X2 speed for twice as fast learning! You can also get access to a virtual machine (VM) to work through code examples though I personally found some of the code to be messy and overdeveloped for what I needed - stick to the videos.

java.boot.by: This is an excellent (and free) resource from Mikalai Zaikin who also hosts guides on many many other exams. His well researched descriptions and code examples are a great addition to any study plans. Additionally you can purchase questions with detailed answers from Mikalai for a very low fee.

OCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide: Exam 1Z0-815, Exam 1Z0-816, and Exam 1Z0-817: As you can tell from the lengthy name this book claims to cover all three Java exams and it does it very well. Scott Selikoff and Jeanne Boyarsky's writing style is very conversational, making even the more complicated subjects simple via their more human step by step guidance. Buying the book also gives you access to 500 flashcards (meh) and over 800 exam questions via the Wiley site. These questions are much more difficult than the actual exam, with longer code examples and more choices. I read this book with the Kindle App which has a great note taking feature via highlights which I found invaluable when cramming on exam eve and exam morning.

How long you need to prepare depends on how dedicated you are. The Java Uni videos can be sped up and the book could be blitzed a chapter or two a day. My only recommendation is have more than one source for revision for a deeper understanding on any area and to ensure nothing is missed.

Taking the exam

This was the first exam I've taken at home. You need to make sure you personal computer can run the test software (PearsonVue). On exam day you will need to take photos of your workspace from all angles, plus yourself and a form of identity. Your web cam will be on for the duration of the exam but you soon forget it as you focus on the questions. Make sure no one interupts or enters the room to avoid it being thrown out. They also disallow you from reading the questions outloud. If you want to make notes or write anything down the software includes a whiteboard.

Exam difficulty

The pass mark is 61% so you need at most 49 correct from the 80 questions. With 180 minutes there is no chance you will run out of time. I passed the exam with 89% with well over an hour to spare.

I read through the resources cited above (much of it multiple times) so I was very prepared. Many questions regarding var, the new interface methods (i.e. default, static and private), how to write functional interfaces (Single Abstract Methods - SAM) and lambdas are based on rules. If you know the rules then you can eliminate wrong answers really really fast. Questions regarding the interfaces in java.util.function and the modular service provider pattern will take longer to analyse. You need to know the functional interfaces SAMs - particularly what parameters they take and what they return. You need to know the service provider pattern well so you can identify the module from minor clues. Again, if you do the preparation you'll be fine. Watch out for questions that look out of place such as a question on Threads. These are often subtly hiding a more relevant issue with the code such as a lambda issue or var misuse.


Finally, I took a lot of notes and created a lot of example code. It is available on my GitHub site.

Thursday, 21 November 2019

Compiling JAXB and JAX-WS classes with Maven in Java 11

Due to the deprecation of EE modules from SE, migrating projects to Java 11 can be somewhat trickier when you rely on Maven to compile your JAXB or JAX-WS classes. See https://jaxenter.com/jdk-11-java-ee-modules-140674.html for more information.

Here's how I got round the issues:

Dependencies


These were either the latest at time of writing, or the version that happened to work. Always check Maven Repository before throwing in dependencies - there may be a better version out there.

<javax.activation.version>1.1.1</javax.activation.version>
<jaxb-api.version>2.3.1</jaxb-api.version>
<jaxb-runtime.version>2.3.2</jaxb-runtime.version>
<jaxb2.maven.version>2.5.0</jaxb2.maven.version>
<jaxws.tools.version>2.3.2</jaxws.tools.version>

<dependency>
<groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>${javax.activation.version}</version>
</dependency>
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>${jaxb-api.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>${jaxb-runtime.version}</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-tools</artifactId>
    <version>${jaxws.tools.version}</version>
    <scope>compile</scope>
</dependency>

JAXB


Our legacy application was using the following dependencies from org.jvnet.jaxb2 to generate the classes based on the xsds/xjc:

<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.2</version>

<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>

I won't go into detail as to how we configured those as it's not relevant here. Importantly, even by upgrading the versions, at time of writing, these simply do not work in Java 11! Here's the working solution which uses an xjc file to create the files. If you don't have one, they are easily created. There are many xjc guides out there.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>${jaxb2.maven.version}</version>
    <executions>
        <execution>
            <id>mystuff</id>
            <goals>
                <goal>xjc</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/main/resources/xsd/</source>
                </sources>
                <packageName>my.package.name</packageName>
            </configuration>
        </execution>
    </executions>
</plugin>

JAX-WS


This is a bit different because we need the endpoints as well as the JAXB objects. Essentially we need to call wsimport differently than we would have using the fairly straightforward jaxws-maven-plugin.

<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.4.1</version>

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>${maven.exec.version}</version>
    <executions>
        <execution>
            <id>mkdir</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>mkdir</executable>
                <arguments>
                    <argument>-pv</argument>
                    <argument>target/generated-sources/wsimport</argument>
                </arguments>
            </configuration>
        </execution>
        <execution>
            <id>mystuff</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-classpath</argument>
                    <classpath/>
                    <argument>com.sun.tools.ws.WsImport</argument>
                    <argument>-extension</argument>
                    <argument>-Xnocompile</argument>
                    <argument>-wsdllocation</argument>
                    <argument>/xsd/file.wsdl</argument>
                    <argument>-s</argument>
                    <argument>target/generated-sources/wsimport</argument>
                    <argument>src/main/resources/xsd/file.wsdl</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

This plugin literally creates a directory and then calls wsimport from the com.sun.tools.ws.WsImport library.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>${maven.build.helper.version}</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/wsimport/</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

For the code to be picked up use build-helper-maven-plugin as you normally would.

Friday, 14 September 2018

Factorials and Trailing Zeros


Here’s a seemingly impossible and odd query that I’ve been asked in a technical interview:

Question: “How many trailing zeros are there in 126! (factorial)”

At the time I was left stumped and made sure to find out the solution later - so here it is! It turns out there’s a simple solution that works for all numbers.

Answer: 
Firstly, as a quick reminder, a factorial is the product of an integer and the integers below it. For 126! it is: 126 * 125 * 124 * … 3 * 2 * 1
Which can be shown as: n * (n – 1) * (n – 1) * …

Now the solution. The simplest way has three steps, i.e. for number n:
  1. Divide the number n by 5 and write down the quotient. Ignore remainders (if any).
  2. Then divide the quotient by 5 again and again till you get a quotient less than 5.
  3. Add up all the resultant quotients to get the number of zeros in n!
Therefore, for 126! We get to the answer via:

126 / 5 = 25 (Actually 25.2 but we ignore the fraction. Note that 125 / 5 gives us the whole number of 5 – this will be useful in a moment)
25 / 5 = 5
5 / 5 = 1
25 + 5 + 1 = 31 zeros in 126!

This pattern can be shown as:


[ ] indicates only whole numbers; neglect any fractional part in the division process. This pattern may make more sense when we note that 52 is 25 and 53 is 125. The formula technically goes on till infinity but fractions are considered as 0 so do not affect the final result.

For 126! This resulted in [25] + [5] + [1] = 31.

Why does this work? Well, here’s an explanation: We know that a number gets a zero at the end of it if the number has 10 as a factor, e.g. 10 is a factor of 20, 150, and 99990. Also, 5 * 2 = 10, so we need to account for all the products of 5 and 2. However, every other factor is even, so there are far more factors of 2 than 5 - As such, we have to count the number of factors divisible by 5. To phrase it differently, there are many more numbers that are multiples of 2 (2, 4, 6, 8, 10, 12, 14, ...) than are multiples of 5 (5, 10, 15, ...). If we were to take all the numbers with 5 as a factor, we'll have way more than enough even numbers to pair with them to get factors of 10 (and another trailing zero on the factorial). Finally, we need to consider numbers which are factors of 5 and count them again, e.g. 25 (which is 5 * 5 or 52), 125 (5 * 5 * 5 or 53), 625 (5 * 5 * 5 * 5 or 54) etc.

To conclude, here is 126! in full (feel free to count the trailing zeros):

23721732428800468856771473051394170805702085973808045661837377170052497697783313457227249544076486314839447086187187275319400401837013955325179315652376928996065123321190898603130880000000000000000000000000000000

The approximate value is: 2.37217324288E+211



Sunday, 2 September 2018

Cleaner code - Comments

I truly believe that if the code is written well enough, comments should not be required at all. Not all comments are bad. They can be useful, i.e. they add additional details, information, caveats or provide example inputs (e.g. good/bad inputs for complex regex). Sadly, most of the time comments are put in with little thought, added automatically and are not kept up to date. It has become a bug-bear of mine and this nonsense has to stop! The following are the types of comments that bug me the most!

Bad comments
Comments should be informative, explain the intent or clarify what the code is doing. Sadly this is not always the case. Please be on guard to remove these variances of bad commenting:
Journal comments
We don't need to document when code was created, last updated or by whom. That's what versioning systems are for.
* Changes (from 11-Oct-2001)
* --------------------------
* 11-Oct-2001 : Re-organised the class and moved it to new package
*               com.jrefinery.date (DG);
* 05-Nov-2001 : Added a getDescription() method, and eliminated NotableDate
*               class (DG);
* 12-Nov-2001 : IBD requires setDescription() method, now that NotableDate
*               class is gone (DG);  Changed getPreviousDayOfWeek(),
*               getFollowingDayOfWeek() and getNearestDayOfWeek() to correct
*               bugs (DG);
Attributions and Bylines
/* Added by Rick */
Thanks Rick. Remove anything like this.
Noise / Redundant comments
Sometimes you see comments that are nothing but noise. They restate the obvious and provide no new information.
/**
  * Returns the day of the month.
  *
  * @return the day of the month.
  */
 public int getDayOfMonth() {
   return dayOfMonth;
}
Consider what purpose does the comment serve. If it’s not more informative than the code, remove.
Misleading comments
These are possibly the worst kind as they may state a fact about the code that may have been true once but is now false. Too often the comments get separated from the code they describe and become orphaned blurbs of ever-decreasing accuracy.
JavaDocs in Nonpublic code
Javadocs are useful for public APIs. Generating Javadoc pages for the classes and functions inside a system is not generally useful, and the extra formality of the Javadoc comments amounts to little more than a distraction. Comments like this just clutter up the code, propagate lies, and lend to general confusion and disorganization.