Given these classes:
and this main method:Which two options compile when placed at line n1 of the main method? (Choose two.)
Given these classes:
and this main method:Which two options compile when placed at line n1 of the main method? (Choose two.)
Employee employee can access and modify its own salary as it is defined in the Employee class. The Director extends the Manager, which in turn extends the Employee class, thus inheriting the salary field. Therefore, the Director object can access and modify the salary field. Other options such as manager.budget and director.stockOptions will not compile because the reference type Employee doesn't have budget and stockOptions fields.
BF are correct. 1. The type of the object determines which properties exist within the object in memory. 2. The type of the reference to the object determines which methods and variables are accessible to the Java program. class Employee{ public int salary; } class Manager extends Employee{ public int budget; } public class Director extends Manager{ public int stockOptions; public static void main(String[] args) { Employee employee = new Employee(); Employee manager = new Manager(); Employee director = new Director(); // director.stockOptions = 1_000; // Compilation Error employee.salary = 50_000; // manager.budget = 1_000_000; // Compilation Error // manager.stockOption = 500; // Compilation Error // employee.budget = 200_000; // Compilation Error director.salary = 80_000; } }
The answer is DE, why? Because apparently the question is wrong, the question should be which of those DONT compile, because ABCF work fine, those who answered only BF might have problems later with similar questions..
The answer is BF but this is a tricky question as it involves inheritance and polymorphism. But you can make it less complicated if we eliminate the wrong ones: -> Let's start with the letter B, she is evidently right, because "Employee" is the parent class and the method is hers, so we already have a right alternative. -> We are already sure that the letter B is the first correct one, when looking at the code, we realize that all objects are a variable of type "Employee", that is, they can access anything public of the "Employee" class without problems , and the only answer we have with that is the letter F.
The original question asks for the two **wrong** options. Actually manager can access .budget and director can also access .stockOptions.
It is my fault that I overlooked the type of manager and director. The right answer should be B and F.
all compile except DE, so i guest the question normally is which two options don't compile