full stack dev quiz

Full-stack dev quiz question #56

Full-stack dev quiz question #56quiz

Construtors in Java have limitations. Not everything is possible in them. Check fifty sixth question of full-stack dev quiz to check if you know what they are.

This is a code of one class in Java 11. It has a serious issue that prevents it from being usable. Actually, it will not even compile.

public class AppException extends Exception {

public AppException(int errorCode, Throwable cause) {
if (errorCode == 1) {
super(buildMessage(errorCode));
} else {
super(buildMessage(errorCode), cause);
}
}

private static String buildMessage(int errorCode) {
switch (errorCode) {
case 1:
return "User not found";
case 2:
return "Malformed request";
default:
return "Unknown error";
}
}

}

What is the compilation-time problem with that class? Choose the best answer.

  1. Exception class cannot be extended as it is final.
  2. AppException constructor must have the same list of arguments as a construction from Exception class.
  3. Calling the super constructor cannot be precedented by any other code.
  4. A constructor cannot call a static method.
  5. A switch statement cannot return String.

For the correct answer scroll down

.

.

 

 

 

 

 

 

 

 

 

 

Do not miss valuable content. You will receive a monthly summary email. You can unsubscribe anytime.

 

 

 

 

 

 

 

 

 

 

 

 

 

.

.

.

The correct answer is: c. If you would like to read more, check Logic in constructor or static factory method article.