What is the result of the following statements?1: public class Test {2: public void print(byte x) {3: System.out.print("byte");4: }5: public void print(int x) {6: System.out.print("int");7: }8: public void print(float x) {9: System.out.print("float");10: }11: public void print(Object x) {12: System.out.print("Object");13: }14: public static void main(String[] args) {15: Test t = new Test();16: short s = 123;17: t.print(s);18: t.print(true);19: t.print(6.789);20: }21: }*A. bytefloatObjectB. intfloatObjectC. byteObjectfloatD. intObjectfloatE. intObjectObjectF. byteObjectObject
Question
What is the result of the following statements?1: public class Test {2: public void print(byte x) {3: System.out.print("byte");4: }5: public void print(int x) {6: System.out.print("int");7: }8: public void print(float x) {9: System.out.print("float");10: }11: public void print(Object x) {12: System.out.print("Object");13: }14: public static void main(String[] args) {15: Test t = new Test();16: short s = 123;17: t.print(s);18: t.print(true);19: t.print(6.789);20: }21: }*A. bytefloatObjectB. intfloatObjectC. byteObjectfloatD. intObjectfloatE. intObjectObjectF. byteObjectObject
Solution
The result of the given Java code would be "intObjectfloat".
Here's the step by step explanation:
-
The
Testclass is created with four overloadedprintmethods, each accepting different types of parameters:byte,int,float, andObject. -
In the
mainmethod, aTestobjecttis created. -
A
shortvariablesis declared with the value123. Whent.print(s)is called, theprint(int x)method is invoked because in Java, ashortcan be automatically promoted tointif no method exists that explicitly takes ashortparameter. So, "int" is printed. -
Next,
t.print(true)is called. Since there's noprintmethod that accepts abooleanparameter, theprint(Object x)method is invoked because in Java, abooleancan be boxed into aBooleanobject. So, "Object" is printed. -
Finally,
t.print(6.789)is called. This is adoubleliteral, but since there's noprintmethod that accepts adoubleparameter, it's automatically downcasted tofloat(losing some precision in the process) and theprint(float x)method is invoked. So, "float" is printed.
So, the final output is "intObjectfloat", which corresponds to option D.
Similar Questions
Which of the following are true about the following code? (Choose all that apply)public class Create { Create() { System.out.print("1 "); } Create(int num) { System.out.print("2 "); } Create(Integer num) { System.out.print("3 "); } Create(Object num) { System.out.print("4 "); } Create(int... nums) { System.out.print("5 "); } public static void main(String[] args) { new Create(100); new Create(1000L); }}*A. The code prints out 2 4.B. The code prints out 3 4.C. The code prints out 4 2.D. The code prints out 4 4.E. The code prints 3 4 if you remove the constructor Create(int num).F. The code prints 4 4 if you remove the constructor Create(int num).G. The code prints 5 4 if you remove the constructor Create(int num).
Given1. class Pro{2. 3. public static void main(String args[]){4. System.out.print(new A(5).y);5. System.out.print(new A().x);6. } 7. } 8. 9. class A{10. A(int i){11. y = i;12. System.out.print("A");13. super();14. }15. int y;16. int x = 10;17. } 18. Which is the output?*A510Compilation fails due to multiple errors.Compilation fails due to an error on line 5.A5 followed by an exception.Compilation fails due to an error on line 13.
What's the output from the following statements?public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); }}
What is the result of the following code? 1: public abstract class Bird { 2: private void fly() { System.out.println("Bird is flying"); } 3: public static void main(String[] args) { 4: Bird bird = new Pelican(); 5: bird.fly(); 6: } 7: } 8: class Pelican extends Bird { 9: protected void fly() { System.out.println("Pelican is flying"); } 10: } *1 pointA. Bird is flyingB. Pelican is flyingC. The code will not compile because of line 4.D. The code will not compile because of line 5.E. The code will not compile because of line 9.
What is the output of the following code? 1: class Arthropod 2: public void printName(double input) { System.out.print("Arthropod"); } 3: } 4: public class Spider extends Arthropod { 5: public void printName(int input) { System.out.print("Spider"); } 6: public static void main(String[] args) { 7: Spider spider = new Spider(); 8: spider.printName(4); 9: spider.printName(9.0); 10: } 11: }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.