What is the output ? public class Airplane

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
zuzu007
Posts: 12
Joined: Mon Sep 09, 2019 2:08 pm
Contact:

What is the output ? public class Airplane

Post by zuzu007 »

Hi! Can you please help me with this question? I answered option D. I thought that the var end can't be changed in the constructor because de var end is final. If it was a method it couldn't be changed but because is a constructor you can? Please let me know if my logic is correct. Thanks!

What is the output of the following application?
public class Airplane {
static int start = 2;
final int end;
public Airplane(int x) {
x = 4;
end = x;
}
public void fly(int distance) {
System.out.print(end-start+" ");
System.out.print(distance);
} public static void main(String... start) {
new Airplane(10).fly(5);
}
}
A. 2 5
B. 8 5
C. 6 5
D. The code does not compile.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: What is the output ? public class Airplane

Post by admin »

yes, a final variable is allowed to be initialized once before use in the constructor. Try it out.
This is explained in OCP Java 11 Fundamentals by Hanumant Deshmukh.
If you like our products and services, please help us by posting your review here.

zuzu007
Posts: 12
Joined: Mon Sep 09, 2019 2:08 pm
Contact:

Re: What is the output ? public class Airplane

Post by zuzu007 »

Thank you!

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests