Predict the output of following Java Programfinal class Complex { private final double re; private final double im; public Complex(double re, double im) { this.re = re; this.im = im; } public String toString() { return "(" + re + " + " + im + "i)"; }} class Main { public static void main(String args[]) { Complex c = new Complex(5, 10); System.out.println("Complex number is " + c); } }OptionsCompiler ErrorComplex number is (5.0 + 10.0i)Complex number is SOME_GARBAGEComplex number is Complex@8e2fb5
Question
Predict the output of following Java Programfinal class Complex { private final double re; private final double im; public Complex(double re, double im) { this.re = re; this.im = im; } public String toString() { return "(" + re + " + " + im + "i)"; }} class Main { public static void main(String args[]) { Complex c = new Complex(5, 10); System.out.println("Complex number is " + c); } }OptionsCompiler ErrorComplex number is (5.0 + 10.0i)Complex number is SOME_GARBAGEComplex number is Complex@8e2fb5
Solution
The output of the Java program will be "Complex number is (5.0 + 10.0i)".
Here's the step by step explanation:
-
The
Complexclass is defined with two private final variablesreandimwhich represent the real and imaginary parts of a complex number respectively. -
The
Complexclass has a constructor that takes two arguments and assigns them toreandim. -
The
toString()method in theComplexclass is overridden to return a string representation of the complex number in the format "(re + imi)". -
In the
mainmethod of theMainclass, a newComplexobjectcis created withreas 5 andimas 10. -
The
System.out.println()statement prints "Complex number is " followed by the string representation ofcwhich is "(5.0 + 10.0i)".
So, the correct option is "Complex number is (5.0 + 10.0i)".
Similar Questions
lect the correct answerPredict the output of following Java Programfinal class Complex { private final double re; private final double im; public Complex(double re, double im) { this.re = re; this.im = im; } public String toString() { return "(" + re + " + " + im + "i)"; }} class Main { public static void main(String args[]) { Complex c = new Complex(5, 10); System.out.println("Complex number is " + c); } }OptionsComplex number is (5.0 + 10.0i)Complex number is SOME_GARBAGECompiler ErrorComplex number is Complex@8e2fb5
elect the correct answerWhat is the output of the following program? public class Test{ private String function(float i, int f) { return ("ctc"); } private String function(double i, double f) { return ("CTC"); } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.function(1., 20)); }}OptionsRuntime errorCompilation errorCTCCodeTantraCode
Select the correct answerWhat will be the output of the following Java program?interface calculate { void cal(int item);}class display implements calculate { int x; public void cal(int item) { x = item * item; }}class interfaces { public static void main(String args[]) { display arr = new display(); arr.x = 0; arr.cal(2); System.out.print(arr.x); }}Options204Compiler Error
Predict the output of the following program:abstract class Demo{ public int a; Demo() { a = 10; } abstract public void set(); abstract final public void get();}class Test extends Demo{ public void set(int a) { this.a = a; } final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.set(20); obj.get(); }}Select one:a.a = 10b.a = 20c.Compilation error
Predict the output of the below code :import java.util.regex.*; public class TestRegEx{ public static void main(String args[]) { Pattern p = Pattern.compile(".ech"); Matcher m = p.matcher("tech"); boolean b = m.matches(); System.out.println(b); }}Select one:a.Runtime Errorb.truec.falsed.compile time error
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.