Computer Applications
Give output of the following method definition and also write the mathematical operation they carry out:
void test4(String x, String y)
{
if(x.compareTo(y) > 0)
System.out.println(x);
else
System.out.println(y);
}
if "AMIT" and "AMAN" are passed to the method.
Java
User Defined Methods
152 Likes
Answer
AMIT
Working
The first differing characters of "AMIT" and "AMAN" are 'I' and 'A', respectively. So output of "AMIT".compareTo("AMAN") will be ASCII Code of 'I' - ASCII Code of 'A' ⇒ 73 - 65 ⇒ 8. The if condition is true so string x which is "AMIT" gets printed as the output.
Answered By
80 Likes
Related Questions
Give output of the following method definition and also write the mathematical operation they carry out:
void test2(int a, int b) { while( a != b) { if ( a > b) a = a — b; else a = b — a; } System.out.println(a); }
if 4 and 17 are passed to the function.
Give output of the following method definition and also write the mathematical operation they carry out:
void test3(char c) { System.out.println( (int) c); }
if 'm' is passed to c.
A method is a program module that is used simultaneously at different instances in a program. It helps a user to reuse the same module multiple times in the program. Whenever you want to use a method in your program, you need to call it through its name. Some of the components/features of a method are as described below:
(a) It defines the scope of usage of a method in the user's program.
(b) It is the value passed to the method at the time of its call.
(c) It is a return type that indicates that no value is returned from the method.
(d) It is an object oriented principle which defines method overloading.
Write the appropriate term used for each component/feature described above.
Define a method. What is meant by method prototype?