Questões do exame: Construtor
Autor: Bruno Souza | Publicado: 2012-05-03T14:30:00-07:00

Apenas public, protected, private e default (sem modificador de acesso) são legais quando se declara construtores.Veja as questões abaixo:

1.Can a constructor be declared static?&nbsp;



No.

Answers

Correct




1Yes
2No







2.What is the expected output?&nbsp;






01.public class Profile {

02.&nbsp;

03.private Profile(int w) { // line 1

04.System.out.println(w);

05.}

06.&nbsp;

07.public static Profile() { // line 5

08.System.out.println(10);

09.}

10.&nbsp;

11.public static void main(String args[]) {

12.Profile obj = new Profile(50);

13.&nbsp;

14.}

15.}







1Won't compile because of line (1) – constructor can't be private
210

50

350
4Won't compile because of line (5) – constructor can't be static



Construtores &nbsp;não podem ser static.






3.What is the expected output?&nbsp;








01.public class Profile {

02.&nbsp;

03.private Profile(int w) { // line 1

04.System.out.println(w);

05.}

06.&nbsp;

07.public final Profile() { // line 5

08.System.out.println(10);

09.}

10.&nbsp;

11.public static void main(String args[]) {

12.Profile obj = new Profile(50);

13.&nbsp;

14.}

15.}








1Won't compile because of line (1) – constructor can't be private
2Won't compile because of line (5) – constructor can't be final
350
4
10
50




Construtores não podem ser final 






4.What is the expected output?&nbsp;


01.import java.io.IOException;

02.&nbsp;

03.class AirPlane {

04.public AirPlane() throws IOException {

05.System.out.print("AirPlane");

06.throw new IOException();

07.}

08.}

09.&nbsp;

10.class AirJet extends AirPlane {

11.public AirJet() throws IOException {

12.try {

13.super();

14.} catch (IOException e) {

15.System.out.print("IOException is thrown in AirJet");

16.}

17.}

18.}

19.&nbsp;

20.public class Tester {

21.public static void main(String args[]) {

22.try {

23.new AirJet();

24.} catch (IOException e) {

25.System.out.print("IOException is thrown in Tester");

26.}

27.}

28.}









1"AirPlaneIOException is thrown in AirJet" will be printed
2"AirPlaneIOException is thrown in AirJetIOException is thrown in Tester" will be printed
3"AirPlaneIOException is thrown in Tester" will be printed
4Compile error&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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
&nbsp; 


01.public class Tester {

02.&nbsp;

03.Tester() {&nbsp;&nbsp;&nbsp; }&nbsp; // line 1

04.static void Tester() {&nbsp; this(); } // line 2&nbsp; 

05.&nbsp;

06.public static void main(String[] args) { // line 3

07.Tester(); // line 4

08.}

09.}





1<td class="dr-table-cell rich-table-cell 

textAlignLeft " id="renderQForm:questionTable:0:j_id254" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-color: rgb(204, 204, 204); border-bottom-width: 1px; border-right-color: initial; border-right-style: solid; bottom-style: solid; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px; right-width: 0px; vertical-align: top;">At line 1, constructor Tester must be marked public like its class<td class="dr-table-cell rich-table-

cell textAlignCenter " id="renderQForm:questionTable:0:j_id259" style="background-attachment: initial; background-image: initial; background-origin: initial; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-right-color: initial; border-right-style: solid; border-right-width: 0px; clip: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px; vertical-align: top;">
<td class="dr-table-cell 

rich-table-cell textAlignCenter " id="renderQForm:questionTable:1:j_id251" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-right-color: initial; border-right-style: solid; border-right-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px; vertical-align: top;">2<td class="dr-table-cell 

rich-table-cell textAlignLeft " id="renderQForm:questionTable:1:j_id254" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-right-color: initial; border-right-style: solid; border-right-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px; vertical-align: top;">At line 2, constructor call “this()” can only be called inside constructors
<td class="dr-table-cell 

rich-table-cell textAlignCenter " id="renderQForm:questionTable:2:j_id251" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-right-color: initial; border-right-style: solid; border-right-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px; vertical-align: top;">3<td class="dr-table-cell 

rich-table-cell textAlignLeft " id="renderQForm:questionTable:2:j_id254" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-right-color: initial; border-right-style: solid; border-right-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px; vertical-align: top;">At line 3, compilation error, ambiguity problem, compiler can”t determine wether a constructor
Tester or method Tester is called<td class="dr-table-cell 

rich-table-cell textAlignCenter " id="renderQForm:questionTable:2:j_id257" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-right-color: initial; border-right-style: solid; border-right-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px; vertical-align: top;"><td class="dr-

table-cell rich-table-cell textAlignCenter " id="renderQForm:questionTable:2:j_id259" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-color: rgb (204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-right-color: initial; border-right-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px; style: solid; vertical-align: top;">





Uma chamada de construtor (super() ou this()) deve ser a primeira instrução 
dentro de um construtor.
Nenhuma chamada do construtor são permitidos 
dentro de um método.
Tester static void () é um método, não um construtor .



&nbsp; &nbsp; &nbsp;