Pageviews

Tuesday 28 July 2020

+2] - 1.Structures And Pointers Previous Questions Chapter wise


               PLUS TWO  COMPUTER SCIENCE


   Second Year Computer Science Previous Questions Chapter wise ..

                                            Chapter 1. Structures and Pointers

 1. Represent a structure named student with attributes of different types and give advantages of using structure. 

  Ans. struct student
          {
            int age;
            char name[10];
             float fee;
          }
             In structure we can represent  a group of different type of data under a name.Variable declaration  statement causes  memory allocation as per the size of the day.

2. Structure within a structure is termed as ______. 

   Ans. Nested structure

3. Orphaned memory blocks are undesirable. How can they be avoided? 

     Ans. If the memory  allocated using new  operator  is not freed using delete, that memory  is said be an orphaned  memory block-a block  of memory  that is left   unused   but  not released  for further   allocation.Remedy for takes place only in case of dynamic memory allocation.But in case of static memory allocation the operating system takes the responsibility of allocation and de-allocation without use's instruction.

 4. Discuss problems created by memory leak. 

    Ans. Memory leak occurs when programmers create a memory in heap and forget to delete it.It is a situation where once memory is  dynamically allocated to a variable and if later the memory is not deallocated after its purpose satisfied, the memory gets lost.

 5. (a) How will you free the allocated memory? 
  
      Ans. It is necessary to free the memory which is allocated dynamically at run time to avoid     memory leaks. The free() function is used to free the memory which is allocated previously using malloc() or calloc()

     (b) Define a structure called time to group the hours, minutes and seconds. Also write a statement             that declares two variables current_time and next_time which are of type struct time. 

     Ans. 

struct time
{
   int Hour;
   int Minute;
    int Second;
};

    struct time
    {
        int current_time;
        int next_time;
};

6. Write a program to store and print information (name, roll and marks) of a student using structure. 

        #include <iostream>
using namespace std;

struct student
{
    char name[50];
    int roll;
    float marks;
};

int main() 
{
    student s;
    cout << "Enter information," << endl;
    cout << "Enter name: ";
    cin >> s.name;
    cout << "Enter roll number: ";
    cin >> s.roll;
    cout << "Enter marks: ";
    cin >> s.marks;

    cout << "\nDisplaying Information," << endl;
    cout << "Name: " << s.name << endl;
    cout << "Roll: " << s.roll << endl;
    cout << "Marks: " << s.marks << endl;
    return 0;
}
 7.  Write a program in C++ to input the total marks obtained by a group of students in a class and             display them in descending order using pointers. 

8. Compare the aspects of arrays and structures. 

         array:
             
                  An array is collection of items stored at continuous memory locations.

                 Array uses subscripts or “[ ]” (square bracket) for element access.

                Array is pointer as it points to the first element of the collection.

               Array size is fixed and is basically the number of elements multiplied by the size of an           element.
             Array declaration is done simply using [] and not any keyword.

        structure:
          
                A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.
             
Structure uses “.” (Dot operator) for element access.Structure is not a pointer.

Structure size is not fixed as each element of Structure can be of different type and size.

Structure declaration is done with the help of “struct” keyword.

 9. Run time allocation of memory is triggered by the operator ____

    Ans. new

 10. Represent the names of 12 months as an array of strings. 

       Ans. struct dob
                {
                 int d,m,y;
                };
                   Struct student
                  {
                    char name[10];
                     int age;
                     dob date;
                  };

11. A structure can contain another structure. Discuss.

         Ans.  An element of a structure may itself be another structure is known as nested structure.The concept of nesting enables the building of powerful data structure.

 12. If ‘ptr’ is a pointer to the variable ‘num’, which of the following statements is correct? 

(i) ‘ptr’ & ‘num’ may be of different data types. 

(ii) If ‘ptr’ points to ‘num’, then ‘num’ also points to ‘ptr’. 

(iii) The statement num=&ptr; is valid. 

(iv) *ptr will give the value of the variable ‘num’.

        Ans.  ii,iv

13. Identify the correct errors in the following code

      struct
      {
        int reg_no;
         char name[100];
         float mark= 100;
        }

      Ans.  Name  of the structure  is not given. Integer value can't assigned to a float data type.Syntax is not correct.Correct code will be the following,

struct
{
 int reg_no;
char name[100];
float mark=100.0;
};

14. What is the difference between static and dynamic memory allocation?

        The difference between static and dynamic memory allocation is that in static memory allocation once the memory is allocated, the memory size is fixed while in dynamic memory allocation, once the memory is allocated, the memory size can be changed.In dynamic memory allocation done by the new and delete operators.

15. Read the following code segment

    int a[]={5,10,15,20,25};

    int *p=a;
predict the ouput of the following statements
1. cout<<*p;
2. cout<<*p+1;
3.cout<<*(p+1);

Ans. 1. 5
         2. 6
         3.  10