About Question enthuware.ocajp.i.v7.2.1158 :

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

Moderator: admin

Post Reply
vlezz94
Posts: 12
Joined: Wed Sep 28, 2016 6:31 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by vlezz94 »

Hi!

right now I'm kind of confused with this code. I understand that the static statements/blocks are called first in the order they are created. But I don't see why "1" it's the last one called:

Code: Select all

public class InitTest{
   public InitTest(){
      s1 = sM1("1");
   }
   static String s1 = sM1("a");
   String s3 = sM1("2");{
      s1 = sM1("3");
   }
   static{
      s1 = sM1("b");
   }
   static String s2 = sM1("c");
   String s4 = sM1("4");
    public static void main(String args[]){
        InitTest it = new InitTest();
    }
    private static String sM1(String s){
       System.out.println(s);  return s;
    }
}

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

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by admin »

Lets look at it from a different angle - Which line do you think should be called after s1 = sM1("1");?
-Paul.
If you like our products and services, please help us by posting your review here.

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by AndaRO »

Because the constructor is the last who is executed.

acharnamys
Posts: 2
Joined: Mon Nov 20, 2017 8:00 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by acharnamys »

At the moment with the following question there is no correct answer.

Code: Select all

public class InitTest{
   public InitTest(){
      s1 = sM1("1");
   }
   static String s1 = sM1("a");
   String s3 = sM1("2");{
      s1 = sM1("3");
   }
   static{
      s1 = sM1("b");
   }
   static String s2 = sM1("c");
   String s4 = sM1("4");
    public static void main(String args[]){
        InitTest it = new InitTest();
    }
    private static String sM1(String s){
       System.out.println(s);  return s;
    }
}
Possible answers:
- The program will not compile.
- It will print : a b c 2 3 4 1
- It will print : 2 3 4 1 a b c
- It will print : 1 a 2 3 b c 4
- It will print : 1 a b c 2 3 4

The closest answer is "a b c 2 3 4 1" which is still far from the reality "a c b 2 4 3 1".

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

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by admin »

Are you sure? I just ran the code and it printed a b c 2 3 4 1

Paul.
If you like our products and services, please help us by posting your review here.

acharnamys
Posts: 2
Joined: Mon Nov 20, 2017 8:00 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by acharnamys »

Right, got confused as Intellij formatted the code in different order. Thanks

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 39 guests