What is the Correct Order Of Execution of a Java Code

Viewed 4219

We got this question in our test regarding java code execution :-

Select the correct order of java code execution

a) Class Loader

b) Interpretation

c) Compilation

d) Byte Code Verification

e) Java Source Code

f) Execution

Options :-

  1. e-a-c-b-d-f
  2. e-a-b-c-d-f
  3. e-b-a-c-b-f
  4. e-c-a-d-b-f

I marked option 4 as my choice. I don't know the exact reason, but that one sounded best to me.

In result, it said my answer was wrong. T_T

Can someone tell what is the correct answer and why.

(Alleged Right answer by University is option 3) (And after giving link to this post, the university corrected the solution to option 4)

(After reading comments and solutions by everyone, one gets more insight on execution process, rather than just solving a stupid faulty MCQ...... GREAT COMMUNITY).

1 Answers

Correct order is e-c-a-d-b-f, so correct answer is 4.

The other three answers are provably wrong, because all three have a) before c), but you simply cannot have a) Class Loader before the c) Compilation step that generates the byte code that the Class Loader is supposed to be loading.

┌──────────────────────────────────┐
│ e) Java Source Code (.java file) │     Obvious starting point
└──────────────────────────────────┘
                 ↓
 ┌────────────────────────────────┐
 │ c) Compilation (javac command) │
 └────────────────────────────────┘      Then we compile the source code
                 ↓                       to byte code.
     ┌────────────────────────┐
     │ Bytecode (.class file) │
     └────────────────────────┘
                 ↓
       ┌─────────────────┐               The classloader is responsible for
┌──────┤ a) Class Loader ├─ JVM ┐        locating the byte code and making it
│      └─────────────────┘      │        available to the JVM as a byte array
│                ↓              │
│ ┌───────────────────────────┐ │        In the linking phase of preparing a
│ │ d) Byte Code Verification │ │        class for use, the byte code is
│ └───────────────────────────┘ │        verified by the JVM.
│                ↓              │
│     ┌───────────────────┐     │
│     │ b) Interpretation │     │        This one is a bit iffy, but you can
│     └───────────────────┘     │        argue that the byte code is interpreted
│                ↓              │        before it is executed, but the JVM
│        ┌──────────────┐       │        could easily JIT-compile to native
│        │ f) Execution │       │        code without ever executing the code
│        └──────────────┘       │        in interpretation mode.
└───────────────────────────────┘

Note that the ClassLoader is also used again later to load resources on demand.

Related