Knowee
Questions
Features
Study Tools

Select the correct answerPredict the output of the following program.? class CT{    int i = 1;    int j = 2;    CT func(CT obj)    {        CT obj3 = new CT();        obj3 = obj;        obj3.i = obj.i++ + ++obj.j;        obj.j = obj.j;        return obj3;    }    public static void main(String[] args)    {        CT obj1 = new CT();        CT obj2 = obj1.func(obj1);        System.out.println("obj1.i = " + obj1.i + "  obj1.j = " + obj1.j);        System.out.println("obj2.i = " + obj2.i + "  obj1.j = " + obj2.j);    }}OptionsCompilation errorobj1.i = 4 obj1.j = 3obj2.i = 4 obj2.j = 3obj1.i = 1 obj1.j = 2obj2.i = 4 obj2.j = 3non of these

Question

Select the correct answerPredict the output of the following program.? class CT{    int i = 1;    int j = 2;    CT func(CT obj)    {        CT obj3 = new CT();        obj3 = obj;        obj3.i = obj.i++ + ++obj.j;        obj.j = obj.j;        return obj3;    }    public static void main(String[] args)    {        CT obj1 = new CT();        CT obj2 = obj1.func(obj1);        System.out.println("obj1.i = " + obj1.i + "  obj1.j = " + obj1.j);        System.out.println("obj2.i = " + obj2.i + "  obj1.j = " + obj2.j);    }}OptionsCompilation errorobj1.i = 4 obj1.j = 3obj2.i = 4 obj2.j = 3obj1.i = 1 obj1.j = 2obj2.i = 4 obj2.j = 3non of these

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

Solution

The correct answer is: obj1.i = 4 obj1.j = 3 obj2.i = 4 obj2.j = 3

Here's the step by step explanation:

  1. An object obj1 of class CT is created. The variables i and j of obj1 are initialized to 1 and 2 respectively.

  2. The function func() is called with obj1 as argument. Inside the function, a new object obj3 of class CT is created.

  3. The statement obj3 = obj; makes obj3 point to the same object as obj1.

  4. The statement obj3.i = obj.i++ + ++obj.j; first increments obj.j (which is also obj1.j and obj3.j) to 3, then adds it to obj.i (which is also obj1.i and obj3.i), and then increments obj.i (which is also obj1.i and obj3.i) to 2. So, obj3.i, obj1.i and obj2.i become 4.

  5. The statement obj.j = obj.j; doesn't change anything.

  6. The function returns obj3, which is pointing to the same object as obj1. So, obj2 also points to the same object as obj1.

  7. The print statements display the values of i and j for obj1 and obj2. Since obj1 and obj2 are pointing to the same object, their i and j values are the same, which are 4 and 3 respectively.

This problem has been solved

Similar Questions

Select the correct answerWhat will be the output of the following Java statement?class output {    public static void main(String args[])     {      double ct1, ct2, ct3;      ct1 = 3.0/0;      ct2 = 0/4.0;      ct3 = 0/0.0;    System.out.println(ct1);      System.out.println(ct2);      System.out.println(ct3);    }   }Options0.0InfinityNaNall of the mentioned

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

Analyze the following code:public class Test {   public static void main(String[] args) {     int[] x = {1, 2, 3, 4};     int[] y = x;    x = new int[2];    for (int i = 0; i < y.length; i++)      System.out.print(y[i] + " ");  }}Group of answer choicesThe program displays 0 0 3 4The program displays 0 0The program displays 0 0 0 0The program displays 1 2

Select the correct answerWhat will be the output of the following Java program? class access { public int x; static int y; void cal(int a, int b) { x += a ; y += b; } } class static_specifier { public static void main(String args[]) { access obj1 = new access(); access obj2 = new access(); obj1.x = 0; obj1.y = 0; obj1.cal(1, 2); obj2.x = 0; obj2.cal(2, 3); System.out.println(obj1.x + " " + obj2.y); } }Options3 22 31 21 5

What will be the output of the following program?class string_class {public static void main(String args[]){String obj = "hello";String obj1 = "world";   String obj2 = obj;obj2 = " world";System.out.println(obj + " " + obj2); }}

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.