Java OCA OCP Practice Question 1862

Question

Consider the following method :

public void myMethod (int m, Object p, double d){
   //... valid code here
}

Assuming that there is no other method with the same name, which of the following options are correct regarding the above method?.

Select 1 option.

  • A. If this method is called with two parameters, the value of d in the method will be 0.0.
  • B. If this method is called with one parameter, the value of p and d in the method will be null and 0.0 respectively.
  • C. If this method is called with one parameter, the call will throw a NullPointerException.
  • D. If this method is called with one parameter, the call will throw a NullPointerException only if the code in the method tries to access p.
  • E. If this method is called with two parameters, the code will not compile.


Correct Option is  : E

Note

To call myMethod (int m, Object p, double d), you must pass exactly three parameters.

If you try to pass less (or more) number of parameters, the code will not compile.

Note that method parameters are not assigned default values.

It is possible to declare a method that can take variable number of parameters.

For example:.

public static void someMethod (Object... params){
        System.out.println (params.length);
}

You can call this method by passing any number of parameters.

In this case, calling someMethod() without any parameter will print 0. i.e. the length of params array will be 0.

params will NOT be null.




PreviousNext

Related