Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Analyze the following code of the two classes called, Test and A: // -----------

ID: 3726120 • Letter: A

Question

Analyze the following code of the two classes called, Test and A:

// --------------------------------

public class Test {

public static void main(String[] args) {

    A a = new A();

    a.print();

}

}

//---------------------------------

public class A {

public String s;

public A(String s) {

    this.s = s;

}

public void print() {

    System.out.println(s);

}

}

The program Test will print the value of string s

The program Test would compile and run if you change A a = new A() to A a = new A("5")

The program Test compiles and runs fine and prints nothing.

The program will compile only if you make the String s private

A)

The program Test will print the value of string s

B)

The program Test would compile and run if you change A a = new A() to A a = new A("5")

C)

The program Test compiles and runs fine and prints nothing.

D)

The program will compile only if you make the String s private

Explanation / Answer

Dear Student,

Option B is correct.

In the class A the constructor is taking one argument. but the argument has not passed in the test class object.

============================================================

here is the working code..

// --------------------------------

public class Test
{

public static void main(String[] args)
{

    A a = new A("5");

    a.print();

}

}

//---------------------------------

class A
{

public String s;

   public A(String s)
   {

    this.s = s;

}

public void print()
{

    System.out.println(s);

}

}

==============================================================

Kindly Check and Verify Thanks..!!! Please rate..!!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote