What is printed by the following program?
`
public class Deer {
enum Food {APPLES, BERRIES, GRASS}
protected class Diet {
private Food getFavorite() {
return Food.BERRIES;
}
}
public static void main(String[] seasons) {
switch(new Diet().getFavorite()) {
case APPLES: System.out.print("a");
case BERRIES: System.out.print("b");
default: System.out.print("c");
}
}
}
The answer image is from OCP Test bank, in which I prepare my Oracle certification exam
My problem is I can't agree with the answer, that the Oracle company considers correct
I think most correct and accurate option is D.
My argument: main method is not guilty for improper declaration of Diet class. Error is occur when we type this class in switch statement incorrectly, in moment of declaration of class
Even full answer in the image explained that
I created this question because every score on a paid exam is at stake
I thought professional commerce company treats the drafting of questions carefully
How an exam participant can deal in this situation?
I please you tell me is it common have such ambiguous answers in IT certification exams?
Thanks in advance!
The answer E is the correct one. No matter what you think the answer ought to be, when I compile the code using javac (Java 8 and Java 16), the only compilation error is in the main method.
$ javac Deer.java
Deer.java:9: error: non-static variable this cannot be referenced
from a static context
switch(new Diet().getFavorite()) {
^
1 error
$
The compilation error message is a bit odd ... but it is clearly in the main method. You can repeat this experiment for yourself.
You state that you think that the declaration of Diet is improper, but I don't see anything improper about it. It looks like a normal inner class declaration.
The problem in the code is NOT with the declaration of Diet, but in the way that the main method is attempting to create the Diet instance. The (syntactically) correct way would be to write something like this (in main):
switch(new Deer().new Diet().getFavorite()) {
This says:
DeerDiet in the Deer instancegetFavorite() method on the Diet instance.So ... clearly ... main is doing it wrong, and that is where the error is. And the compiler agrees.
And the Java Language Specification (JLS 15.9.2) confirms this:
If C is an inner member class, then:
If the class instance creation expression is unqualified, then:
- If the class instance creation expression occurs in a static context, then a compile-time error occurs.
(You won't find anything in the JLS to support your assertion that the declaration of Diet is wrong ... because it isn't wrong. But feel free to research this, if you want to.)
Meta advice: Before going into a rant about the (supposed) problems with IT certification exams and the companies that administer them ... it is advisable to make sure that you have got your facts right.
The Java language has all sorts of obscure corners, and even after 20+ years of using it, I still keep find things about the language that I didn't realize (or have forgotten). It is a bad idea to assume that that you are right ... in the face of possible evidence to the contrary.
There's nothing wrong with the declaration of the Diet class. It's the usage of the Diet class, not its declaration or definition, that causes the issue, and that usage is in main(). As the question points out, doing new Deer().new Diet().getFavorite() would be fine - so the Diet class compiled correctly.
I think what you're getting wrong here is the word "declaration". This is not ambiguous - it has a very specific and technical meaning in compiled languages like C and Java.
.h and .c files).The declaration and definition of Diet are fine, only the instantiation is wrong. Thus, answer (D) is incorrect.
Also, as pointed out in the other answer, if you try compiling this code with javac, the only compilation error is in the main method - again, because it's the usage of the Diet class that causes the problem, not the definition.