What results from the following code? 1. class MyClass 2. { 3. void myMethod(int i) {System.out.println("int version");} 4. void myMethod(String s) {System.out.println("String version");} 5. public static void main(String args[]) 6. { 7. MyClass obj = new MyClass(); 8. char ch = 'c'; 9. obj.myMethod(ch); 10. } 11. }

  1. The code compiles and produces output: int version.

  2. Line 9 will not compile as there is no version of myMethod which takes a char as argument.

  3. An exception at line 9.

  4. Line 4 will not compile as void methods can't be overridden.


Correct Option: A

AI Explanation

To answer this question, we need to analyze the code snippet provided.

The code defines a class called MyClass with two methods:

  • myMethod(int i), which prints "int version"
  • myMethod(String s), which prints "String version"

In the main method, an object of MyClass is created on line 7. On line 8, a char variable ch is declared and assigned the value 'c'. On line 9, the myMethod method of the MyClass object is invoked with the char variable ch as an argument.

The correct answer is A) The code compiles and produces output: int version.

Explanation: When a method is called with an argument, Java looks for a matching method signature. In this case, there is no method with the exact signature myMethod(char c). However, the argument can be implicitly converted from char to int, which matches the signature of myMethod(int i). Therefore, the myMethod(int i) is invoked, and "int version" is printed as output.

Find more quizzes: