3i Infotech Placement: Sample Questions 439 - 440 of 1245

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 439

Edit

Describe in Detail

Essay▾

How can you increase the size of a dynamically allocated array?

Explanation

Dynamically Allocatng Arrays (2D)
  • In C, Malloc allocates new memory space of size “newsize” to the pointer variable “ptr” and returns a pointer to the first byte of the new memory block.
  • The new size may be smaller or large than original size, it returns a NULL pointer and the original block is freed
  • If the function is unsuccessful in allocating additional space
  • Subsequently memcpy (memory copy) function can be used to copy the contents from old location to new location.
  • C ++ memory allocation using “new” is allocated below.

Program in C ++ (Using New to Allocated Memory) :

  1. intmain()
  2. {
  3.     stringname =NULL;
  4.     name =newstring[1];
  5.     name[0] ="Mike";
  6.     cout ≪ name[0] ≪ endl;
  7.     delete [] name;
  8.     name =newstring[3];
  9.     name[0] ="Mike";
  10.     name[1] ="Steve";
  11.     name[2] ="Dave";
  12.     cout ≪ name[0] ≪ endl;
  13.     cout ≪ name[1] ≪ endl;
  14.     cout ≪ name[2] ≪ endl;
  15.     delete [] name;
  16.     while (_kbhit() == 0);
  17.     return0;
  18. }

Question 440

Edit

Describe in Detail

Essay▾

What is iterator class?

Explanation

  • A class used to traverse through the objects maintained by a container class. There are five categories of iterators:
    • input iterators,
    • output iterators,
    • forward iterators,
    • bidirectional iterators,
    • random access.
  • An iterator gives access to the contents of a container object without violating encapsulation.
  • Access is granted on a one-at-a-time basis in order.
  • The order can be storage order (as in lists and queues) or some arbitrary order (as in array indices) or follow some ordering relation (as in an ordered binary tree) .
  • The iterator is a construct, when called, yields either the next element in the container, or some value denoting the fact that there are no more elements to examine.
  • Iterators hide the details of access to and update of the elements of a container class.
  • The simplest and safest iterators are those that permit read-only access to the contents of a container class.
  • The following code fragment shows how an iterator might appear in code: cont_iter:= new cont_iterator () ; x:= cont_iter. next () ; while x/ = none do … s (x) ; … x:= cont_iter. next () ; end;
  • In this example, cont_iter is the name of the iterator.
  • It is created on the first line by instantiation of cont_iterator class, an iterator class defined to iterate over some container class, cont. Succesive elements from the container are carried to x.
  • The loop terminates when x is bound to some empty value (here, none) . In the middle of the loop, there is s (x) an operation on x, the current element from the container.
  • The next element of the container is obtained at the bottom of the loop.

Developed by: