It's not a secret anymore!
The moose likes Mock Exam Errata and the fly likes Compile or not compile? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Compile or not compile?" Watch "Compile or not compile?" New topic

Compile or not compile?

Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
This query is not a question in any of the mock exams but it causes a lot of confusion while answering the mock exam questions.
The answer choices would be:
A. Compiles and runs with no out put
B. Compiles but generates a compile time error
C. Compiles but generates a runtime error
D. Does not compile
How to make a choice if we have never tried writing a code that particular type of code.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
This is something you do need to know for the exam. You need to know what kind of error certain things can cause. Let me go over your choices:
1. Compiles and runs with no output - This would happen when all of you code is correct but you either have some conditional statement or loop where the executing block following the loop will never get executed.

This is the simplest form, but you can see that nothing will get printed out eventhough there are no errors in the code.
2. Compile time error - This is the same as 4. Does not compile. This happens when the compiler knows there is an error. The compiler doesn't do any math or calculations, but it will know if you make an obvious mistake.
You have a class with a mehtod that takes an int as an argument and you overload it with another method of the same name that also takes a single int as an argument. This will be a compiler error because the compiler knows that if you have two methods with the same name both taking a single int as an argument, whenever you try to call that method, the JVM isn't going to know which method to call.
3. Runtime error - This happens with things that compiler wouldn't know but at runtime it doesn't work. The most obvious ones are when you try to divide an int by 0 or when you try to access an element of an array that is outside of the arrays bounds. The compiler isn't going to do the math or try to access the array elements, that happens at runtime, so errors are not caught until later.
There are a lot of rules when it comes to runtime and compile time errors especially when it comes to casting. You do need to know these rules, and my best advise to you is to do as much coding and samples as possible.
I agree. Here's the link:
subject: Compile or not compile?
It's not a secret anymore!