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..!!!