Languages-C & C Plus Plus [3i Infotech Placement]: Sample Questions 303 - 304 of 354

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

Question 303

Describe in Detail Essay▾

What is handle class?

Edit

Explanation

Handle class maintains a pointer to an object programmatically accessible through the public interface of the handle class. Explanation:

  • Abstract classes provide no benefits of the virtual functions unless objects of the class are accessed through pointers and references.
  • User code thus become dependent on implementation details as an abstract type cannot be allocated statistically or on the stack without its knowing its size.
  • Pointers or references put the burden of memory management on the user or the abstract class object has to be of fixed size.
  • Classes must require varying amounts of storage.
  • Single object is thus split into two parts: a handle providing the user interface and a representation for most of the object՚s state.
  • The connection between the handle and the representation is a pointer in the handle- thus handles have more data than simple representation pointer.
  • Layout of the handle remains stable, even when the representation changes solving the problems.

Question 304

Describe in Detail Essay▾

How would you dynamically allocate a one-dimensional and two-dimensional arrays of integer?

Edit

Explanation

  • We can create both statically and dynamically allocated array which can be both one-dimensional or multiple dimensional.
  • Statically allocated have to specify size before compilation.
  • However sometimes programmer doesn՚t know the size of the array ahead of time.
  • Dynamically allocated array solve this problem by allocating space on heap at runtime.
  • Provides library function to request heap memory at runtime.

One Dimensional (Dynamically Allocated)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char argv[])
  4. {
  5. int piBuffer =NULL;
  6. int nBlock =0;
  7. int iLoop =0;
  8. printf("Enter the number of blocks =");
  9. scanf("%d",&nBlock);
  10. piBuffer =(int ⚹)malloc(nBlock sizeof(int));
  11. if(piBuffer == NULL)
  12. {
  13. return 1;
  14. }
  15. for (iLoop =0 ; iLoop <nBlock ; iLoop++)
  16. {
  17. piBuffer[iLoop] =iLoop;
  18. }
  19. for (iLoop =0 ; iLoop <nBlock ; iLoop++)
  20. {
  21. printf("pcBuffer[%d] =%d", iLoop,piBuffer[iLoop]);
  22. }
  23. free(piBuffer);
  24. return 0;
  25. }
OUTPUT

Enter the number of block = 7

pcBuffer [0] = 0

pcBuffer [1] = 1

pcBuffer [2] = 2

pcBuffer [3] = 3

pcBuffer [4] = 4

pcBuffer [5] = 5

peBuffer [6] = 6

Two Dimensional Dynamically Allocated

  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char argv[])
  4. {
  5. int ⚹⚹piBuffer =NULL;
  6. int nRow =0;
  7. int nColumn =0;
  8. int iRow =0;
  9. int iCol =0;
  10. printf("Enter the number of Row =");
  11. scanf("%d",&nRow);
  12. printf("Enter the number of Column =");
  13. scanf("%d",&nColumn);
  14. piBuffer =(int ⚹⚹)malloc(nRow sizeof(int⚹));
  15. if(piBuffer == NULL)
  16. {
  17. return 1;
  18. }
  19. for (iRow =0 ; iRow <nRow ; iRow++)
  20. {
  21. piBuffer[iRow] =(int ⚹)malloc(nColumn sizeof(int));
  22. if(piBuffer[iRow] == NULL)
  23. {
  24. return 1;
  25. }
  26. }
  27. for (iRow =0 ; iRow <nRow ; iRow++)
  28. {
  29. for (iCol =0 ; iCol <nColumn ; iCol++)
  30. {
  31. piBuffer[iRow][iCol] =3;
  32. }
  33. }
  34. for (iRow =0 ; iRow <nRow ; iRow++)
  35. {
  36. for (iCol =0 ; iCol <nColumn ; iCol++)
  37. {
  38. printf("piBuffer[%d][%d] =%d",iRow, iCol,piBuffer[iRow][iCol]);
  39. }
  40. }
  41. for (iRow =0 ; iRow <nRow ; iRow++)
  42. {
  43. free(piBuffer[iRow]);
  44. }
  45. free(piBuffer);
  46. return 0;
  47. }
OUTPUT

Enter the number of Row = 2

Enter the number of column = 2

piBuffer [0] [0] = 3

piBuffer [0] [1] = 3

piBuffer [1] [0] = 3

piBuffer [1] [1] = 3