Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

298
Visualizações
Who is right and who is wrong? Oracle certification OCP. Inconsistency in the choice of answers

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");
      }
   }
}

` enter image description here

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!

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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:

  • create an instance of the outer class Deer
  • create an instance of Diet in the Deer instance
  • call the getFavorite() 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.

over 4 years ago · Santiago Trujillo Relatório

0

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.

  • An object's declaration is when it's declared to exist by code. Often this is coupled with
  • an object's definition describes what it is (almost always combined with the declaration, in Java; in C/C++, the declaration and definition are often split between .h and .c files).
  • An object's instantiation is when the definition is invoked to create an instance of the object/variable in memory. Every instance (there may be multiple) will conform to the same definition.

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda