Question

What output will be generated by running the program below? Make sure to understand step by...

What output will be generated by running the program below? Make sure to understand step by step what’s happening in computer’s memory and illustrate these steps. You don’t necessarily need to draw formal diagrams. Any notation or diagrams that can be used to show memory allocation and value changes should suffice.

class B {

private int value;

public B () {

} value = 3;

public int getD() {

} return 2 * value;

public void setV(int v) {

value = v + value;

}

public static void main(String[] args) {

int value = 3;

B mymy = new B();

mymy.setV(value);

System.out.println(value + mymy.getD());

} }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

B mymy = new B(); creates a new object of class B whose value is 3.

Now, mymy.setV(value); results in mymy's value = mymy.value + value = 3 + 3 = 6

Hence, mymy's value = 6

Now,

mymy.getD() returns 2 * mymy.value = 2 * 6 = 12

Hence, System.out.println(value + mymy.getD()); prints 3 + 12 = 15

Add a comment
Know the answer?
Add Answer to:
What output will be generated by running the program below? Make sure to understand step by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java 1. Can you explain it how it will be printed step by step? Thanks!! What...

    Java 1. Can you explain it how it will be printed step by step? Thanks!! What is printed by the following? for (int i=1; i<4; i++) { for (char c=’a’; c<=’c’; c++) { if (i%2==0) { i++; System.out.println(i + " " + c); } else { c++; System.out.println(c + " " + i); } } } 2. Is there a compiler error in this code? If so, what is it? If not, what’s printed? Is there a compiler error in...

  • 12. Predict the output generated at the marked println lines in the following program. The program...

    12. Predict the output generated at the marked println lines in the following program. The program makes use of the class Employee that is also given. Please enter your answers in the space provided below the code. public class Employee { private String name; private double salary; public Employee(String name, double salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public double getSalary() { return salary; } public void raiseSalary(double percent) { double...

  • What is the output of this program? class A { public int i; private int j;...

    What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class Inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } Java language!! // include explanation!

  • What is the output for the following program codes? a) [ 0.5 Mark ] class A...

    What is the output for the following program codes? a) [ 0.5 Mark ] class A { int i; } class B extends A { int j; void display() { super.i = j + 1; System.out.println(j + " " + i); }} class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); }} OUTPUT: b) [ 0.5 Mark ] class Parent { public void getBike(){ System.out.println("Suzuki Bike"); }} class Child extends Parent {...

  • Predict the output generated at the marked println lines in the following program. The pro- gram...

    Predict the output generated at the marked println lines in the following program. The pro- gram makes use of the class Prob1. Please enter each answer in the space provided. import java.util.Stack; public class Prob1 { public static int mystery(int m, if(n == 0) return 0; if(n % 2 == 0) return mystery(m+m, n/2); return mystery(m+m, n/2) + m; } int n) { args) { 29)); //------------------(a) public static void main(String[] System.out.println(mystery(2, String str = "Harry Potter"; str.toLowerCase(); str.substring(0, 7);...

  • What is the output of running class C? The three Java classes are in separate Java...

    What is the output of running class C? The three Java classes are in separate Java files in the same directory (or in the same package). class A { public AO { System.out.println("The default constructor of A"); } // end A constructor } // end class A class B extends A { public BCString s) { System.out.println(s); } // end B constructor } // end class B public class C { public static void main(String[] args) { B b =...

  • can someone explain to me what does the dot mean ? and what would be the...

    can someone explain to me what does the dot mean ? and what would be the output and explain it? import java.io.*; public class Green { private int a; private int b; public Green(int aa,int bb) { a=aa; b=bb; } public void equals(Green c) { this.a=c.a;this.b=c.b;} public void fn(Green c) { this.a=3*c.b-c.a; this.b=2*c.a-this.b;} public void gg() { this.b=this.b-1; this.a=this.b-2; } public static void main(String args[]) { Green x=new Green(2,2); Green y=new Green(2,1); Green z=new Green(1,4); int xx=1,yy=2,zz=3; x.fn(y); z.gg(); System.out.println("...

  • what is the output of running class Launcher? class parent { public Parent{ System.out.println( "The default...

    what is the output of running class Launcher? class parent { public Parent{ System.out.println( "The default constructor of Parent is invoked"); } } class Child extends Parent { public Child(); System.out.println( "The default constructor of Child is invoked"); } } public class Launcher { public static void main(String[] args) { Child c = new Child(); } a. no output since the main method does not have System.out.println() b."The default constructor of Child is invoked" c. "The default constructor of Parent...

  • What is the output of following program: public class Test{ public static void main(String[] args) {...

    What is the output of following program: public class Test{ public static void main(String[] args) { A a = new A(): a method B(): } } class A{ public A(){ System out println("A's constructor is executed"): } public void method(){ System out printin ("methodA is executed"): } public void methodAB(){ System out printin ("As methodAB is executed"): } } class B extends A { private int num = 0: public B (){ super(): System out printin ("B's constructor is executed"):...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT