About Question enthuware.ocpjp.v8.2.1590 :

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

Moderator: admin

Post Reply
jckozy86
Posts: 3
Joined: Fri Feb 10, 2017 7:02 pm
Contact:

About Question enthuware.ocpjp.v8.2.1590 :

Post by jckozy86 »

why does the below run with no issues, where in CODE 1 A a = new B(); calls the method from A
and with CODE B Widget w = new GoodWidget(); the method is called from GoodWidget();

Thanks,
Kozy

CODE 1:

Code: Select all

class A
{
   public void getItDone(int counter)
   {
      assert counter >= 0 : "Less than zero";
      for(int i=0; i<counter; i++){ }
   }
}
class B extends A
{
   public void getItDone(int counter)
   {
      assert counter < 100 : "Greater than 100";
      for(int i=counter; i>0; i--){ }
   }
   public static void main(String args[])
   {
      A a = new B();
      a.getItDone(-4);
   }
}
(Assume that assertions are enabled.)


CODE B:

What will the following code print when compiled and run?

Code: Select all

abstract class Widget {

    String data = "data";
    public void doWidgetStuff() {
    }

}

class GoodWidget extends Widget{
    String data = "big data";

    public void doWidgetStuff() {
        System.out.println(data);
    }
}

public class WidgetUser{
    public static void main(String[] args) {
        Widget w = new GoodWidget();
        w.doWidgetStuff();
    }
   
}

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

Re: About Question enthuware.ocpjp.v8.2.1590 :

Post by admin »

Why do you think the method from A is called in the first code?
If you like our products and services, please help us by posting your review here.

jckozy86
Posts: 3
Joined: Fri Feb 10, 2017 7:02 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1590 :

Post by jckozy86 »

I'm sorry,

it was late and was reading the explanation in a sleepy state.

Re-read the explanations and in both cases, since the passed object has the method, that one gets used.

Thanks!

d0wnvk@gmail.com
Posts: 7
Joined: Wed Jun 06, 2018 6:46 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1590 :

Post by d0wnvk@gmail.com »

Hi there))

The whole purpose of assertions: that the program should fail if that assumption fails.
With counter value as -4 the assertion returns true and no program fail occurs (and the program continues to proceed).
Why is the loop is never entered ?
What I'm missing out ?

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

Re: About Question enthuware.ocpjp.v8.2.1590 :

Post by admin »

Inside the getItDone method, counter is -4, so, the assertion counter>100 is satisfied. Therefore, no AssertionError will be thrown here. Further, since counter is -4, for condition counter>0 is not satisfied and so the loop will not be entered.
If you like our products and services, please help us by posting your review here.

d0wnvk@gmail.com
Posts: 7
Joined: Wed Jun 06, 2018 6:46 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1590 :

Post by d0wnvk@gmail.com »

Thanks for reply and pointing me out.
Foreach condition - shame on me((

Oussama
Posts: 3
Joined: Mon Mar 29, 2021 6:33 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1590 :

Post by Oussama »

The problem here is with the for loop :
for(int i=counter; i>0; i--){ }
i = -4 ; so i >0 will return false --------> The loop is never entered and the program terminates.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], pavvel and 45 guests