Java OCA OCP Practice Question 1909

Question

Which of the following methods does not return any value?

Select 1 option

A. public doStuff () throws FileNotFoundException, 
                            IllegalArgumentException{
         //valid code not shown
   }/*from  w  ww.  j  a va2 s . c om*/
B. public null doStuff () throws FileNotFoundException,
                            IllegalArgumentException{
         //valid code not shown
   }
C. public doStuff ()  {
         //valid code not shown
   }
D. public void doStuff () throws FileNotFoundException,
                            IllegalArgumentException{
         //valid code not shown
   }
E. private doStuff ()  {
         //valid code not shown
   }


Correct Option is  : D

Note

For A.

It is missing the return type.

Every method must have a return type specified in its declaration.

It could be a valid constructor though if the class is named doStuff because the constructors don't return anything, not even void.

For B.

null can be a return value not a return type because null is not a type.

For C.

This is not a valid method because there is no return type declared.

Although it can be a valid constructor if the name of the class is doStuff.

For D.

A method that does not return anything should declare its return type as void.

Note that this is different from constructors.

A constructor also doesn't return anything but for a constructor, you don't specify any return type at all.

That is how a constructor is differentiated from a regular method.

For E.

This is not a valid method because there is no return type declared.

Although it can be a valid constructor if the name of the class is doStuff().




PreviousNext

Related