Knowee
Questions
Features
Study Tools

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the Java program will be "Complex number is (5.0 + 10.0i)".

Here's the step by step explanation:

  1. The Complex class is defined with two private final variables re and im which represent the real and imaginary parts of a complex number respectively.

  2. The Complex class has a constructor that takes two arguments and assigns them to re and im.

  3. The toString() method in the Complex class is overridden to return a string representation of the complex number in the format "(re + imi)".

  4. In the main method of the Main class, a new Complex object c is created with re as 5 and im as 10.

  5. The System.out.println() statement prints "Complex number is " followed by the string representation of c which is "(5.0 + 10.0i)".

So, the correct option is "Complex number is (5.0 + 10.0i)".

This problem has been solved

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

1/3

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.