Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1) Consider the following class (given as a class diagram) Person - int age String name + Person(String, int) + Person() + void speak() Among

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

1) Consider the following class (given as a class diagram) Person - int age String name + Person(String, int) + Person() + void speak() Among the following statements, indicate which are valid and which are invalid. A valid statement is a statement that compiles and executes in any class, which has access to the Person class. If a statement is invalid, explain why. Assume that the variables p and q are of type Person and have been correctly initialized before being used. a) Person p = new Person("Maryjane", 30); b) p.speak(); c) p.sleep(); d) Person p = new Person; e) Person.speak(); f) Person p = new Speak(); g) p.age = 25; h) System.out.println(p.name"); i) System.out.println(p.name); j) p = 9; k) new Person("Huy",26).speak(); 2) For each snippet of code below, draw an object diagram showing the memory organization once the snippet of code has been executed. The variables p, q and r are all of type Person (as defined in question 1)). Indicate on the object diagram any alias and any object that has been garbage- collected (see the given example). Example: Person p = new Person(); Person q = new Person(); Person r = new Person(); p = 9; r=q; q = null; The execution of the above code is illustrated by the following object diagram: Person p Garbage collected Person q null Person Garbage collected p = 9; r = 9; q = null; The execution of the above code is illustrated by the following object diagram: Person o Garbage collected Person q null Person Garbage collected r r is an alias of p a) Person p = new Person(); Person q = new Person(); Person r = new Person(); p = 9; q=r; r = p; b) Person p = new Person(); Person q = p; p = new Person(); Person r = p; p = new Person(); q = p; p = new Person(); r= p; p = null; c) Person p = new Person(); Person q = new Person(); Person r = new Person(); p = 9; q = null; r = 9; q = p; 3) Consider the following piece of code: Person p = null; p.speak(); a) Does it compile? Explain. b) Does it execute? Explain. 4) You are given three variables p, q, and r of type Person. The three variables have been correctly initialized. Write a piece of code that writes the initial value of p in q, the initial value of q in r, and the initial value of rin p. Try to make your code as compact as possible

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago