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

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

Moderator: admin

Post Reply
nickeasyuptech
Posts: 29
Joined: Sat Jun 08, 2013 11:33 pm
Contact:

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

Post by nickeasyuptech »

For this question -

What can be the return type of method getSwitch so that this program compiles and runs without any problems?

Code: Select all

public class TestClass{
   public static XXX getSwitch(int x){
      return x - 20/x + x*x;
   }
   public static void main(String args[]){
       switch( getSwitch(10) ){
          case 1 :
          case 2 :
          case 3 :
          default : break;
       }
   }
}
"The return type cannot be byte, short, or char because the expression x - 20/x + x*x; returns an int."

How do we know that it returns an int? (Thanks in advance for your help!)

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

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

Post by admin »

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

nickeasyuptech
Posts: 29
Joined: Sat Jun 08, 2013 11:33 pm
Contact:

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

Post by nickeasyuptech »

Thank you for the quick response!

javabean68
Posts: 31
Joined: Wed Mar 16, 2016 8:38 am
Contact:

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

Post by javabean68 »

Hi,

the expression

Code: Select all

x - 20/x + x*x
can actually return a double, 20/x isn't always int...

Thank you for your help,
Bye
Fabio

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

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

Post by admin »

Not always, but in the code given in this question, 20/x will be an int.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 55 guests