C Question 1 1 pt What are the similarities and differences

C++

Question 1: (1 pt)

What are the similarities and differences between pointers and iterators when dealing with linked lists?

Question 2: (1 pt)

What are the benefits behind using smart pointers and can they be used with linked lists? Justify.

Question 3: (1 pt)

How can you implement Queue class using LinkedList class using composition vs using inheritance?

Question 4: (1 pt)

Which problem solving approach is better: recursive solutions or iterative ones? Justify.

Question 5: (1 pt)

If there are multiple catch blocks for one try block and in each block the exception objects correspond to a base class and its corresponding derived class. Which catch block should appear first: the one for the base class or for the derived class? Justify.

C++

Solution

Question 1: (1 pt)

What are the similarities and differences between pointers and iterators when dealing with linked lists?

Ans:-

Iterators are more similar to pointers than they are different. All pointers to objects are iterators, but not vice versa. Basically, the concept of an iterator is a generalization of the concept of a pointer.

Question 2: (1 pt)

What are the benefits of using smart pointers and can they be used with linked lists? Justify.

Ans:-

1) Smart pointers are objects that look and feel like pointers but are smarter.

2) To be smarter than regular pointers, smart pointers need to do things that regular pointers don\'t. What could these things be? Probably the most common bugs in C++ (and C) are related to pointers and memory management: dangling pointers, memory leaks, allocation failures and other joys. Having a smart pointer take care of these things can save a lot of aspirin.

Benefits:-

Smart pointers can be used to make more efficient use of available memory and to shorten allocation and deallocation time.

A common strategy for using memory more efficiently is copied on write (COW). This means that the same object is shared by many COW pointers as long as it is only read and not modified. When some part of the program tries to modify the object (\"write\"), the COW pointer creates a new copy of the object and modifies this copy instead of the original object. The standard string class is commonly implemented using COW semantics (see the <string> header).

Automatic cleanup. Reducing the probability for bugs: you don\'t need to remember to free the pointer, and so there is no chance you will forget about it.

Automatic initialization. Another nice thing is that you don\'t need to initialize the auto_ptr to NULL since the default constructor does that for you. This is one less thing for the programmer to forget.

Dangling pointers. A common pitfall of regular pointers is the dangling pointer: a pointer that points to an object that is already deleted.

Create a new copy of the object pointed by p, and have q point to this copy.

Question 4: (1 pt)

Which problem-solving approach is better: recursive solutions or iterative ones?

Ans:-

Recursion vs. Iteration :-

Roughly speaking, recursion and iteration perform the same kinds of tasks: Solve a complicated task one piece at a time and combine the results. The emphasis of iteration: ! keep repeating until a task is “done” e.g., loop counter reaches the limit, linked list reaches null pointer, instream.EOF() becomes true Emphasis of recursion: Solve a large problem by breaking it up into smaller and smaller pieces until you can solve it; combine the results. e.g., recursive factorial function

Which is Better?

No clear answer, but there are known trade-offs. “Mathematicians” often prefer recursive approach.

Solutions often shorter, closer in spirit to abstract mathematical entity. ! Good recursive solutions may be more difficult to design and test. “Programmers”, esp. w/o college CS training, often prefer iterative solutions. it seems more appealing to many.

Control stays local to loop, less “magical”.

Question 5: (1 pt)

If there are multiple catch blocks for one try block and in each block the exception objects correspond to a base class and its corresponding derived class. Which catch block should appear first: the one for the base class or for the derived class? Justify.

Ans:-

1. A try block can have any number of catch blocks.
2. A catch block that is written for catching the class Exception can catch all other exceptions
Syntax: catch(Exception e){ }

3. If multiple catch blocks are present in a program then the above-mentioned catch block should be placed at the last as per the exception handling best practices.
4. If the try block is not throwing any exception, the catch block will be completely ignored and the program continues.
5. If the try block throws an exception, the appropriate catch block (if one exists) will catch it
–catch(ArithmeticException e) is a catch block that can catch ArithmeticException
–catch(NullPointerException e) is a catch block that can catch NullPointerException
6. All the statements in the catch block will be executed and then the program continues.

If you have any querey feel free to reach out.

C++ Question 1: (1 pt) What are the similarities and differences between pointers and iterators when dealing with linked lists? Question 2: (1 pt) What are the
C++ Question 1: (1 pt) What are the similarities and differences between pointers and iterators when dealing with linked lists? Question 2: (1 pt) What are the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site