1 What does the following code fragment do vector myvector m

1- What does the following code fragment do?

vector<double> my_vector;

my_vector.push_back(3.456);

my_vector.push_back(5);

double result = my_vector[1] + my_vector[0];

cout << \"Result is \" << result;

2. What is the difference between a shallow copy and a deep copy?

3. Why is it not a problem to have a shallow copy of a primitive-type data

field?

4-

Answer the following questions about lists.

a. Each node in a single-linked list has a pointer to _________________________.

b. In a double-linked list each node has a pointer to________________, and __________.

c. To remove an item from a single-linked list you need a pointer to________________.

d. To remove an item from a double-linked list you need a pointer to ________________.

Solution

1. It adds theelements at the end. Computes the sum. Displays the result.

2.Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.

Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.

3.Whenever we use default implementation of clone method we get shallow copy of object means it create new instance and copy all the field of object to that new instance and return it as object type we need to explicitly cast it back to our original object. This is shallow copy of the object. clone() method of the object class support shallow copy of the object. If the object contains primitive as well as non primitive or reference type variable In  shallow copy, the cloned object also refers to the same object to which the original object refers as only the object references gets copied and not the referred objects themselves. That\'s why the name shallow copy or shallow cloning in Java. If only primitive type fields or Immutable objects are there then there is no difference between shallow and deep copy in Java.

4.

a. next node

b.previous node and next node

c.the node of the previous node

d.previous or next node.

1- What does the following code fragment do? vector<double> my_vector; my_vector.push_back(3.456); my_vector.push_back(5); double result = my_vector[1] +

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site