+1 and +2 Computer Science

Previous Question paper answered , and Questions from Textbook

+1 and +2 Computer Application

Previous Question paper answered , and Questions from Textbook

Diploma in Computer Application (DCA)

Previous Question paper answered , and Questions from Textbook.

Master of Computer Applications (MCA)

Previous Question paper answered , and Questions from Textbook

True IQ Computer Online Academy

Contact : +91 9036433020 , mail2trueiq@gmail.com

Pageviews

Showing posts with label Structures and pointers. Show all posts
Showing posts with label Structures and pointers. Show all posts

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



                            

+2].1.Structures and pointers - Solved Questions from textbook


                          PLUS TWO  COMPUTER SCIENCE

                                            Chapter 1. Structures and Pointers

                           (+2. Computer Science Questions and answers from text book) 


1. what is a structure?

        It is a user defined data type which represents logically related data items.It can represent a  group of different  types of data  under a common name

       Syntax: define the structure is

        struct structure_tag
  
         {
   
            data_type var1;
             data_type var2;
           ......................;
            data_type var N;
         };

2. Structure combines different type of data under a single unit.State whether this statement is true  or false?

 Ans. True

3. Which of the following is true for accessing an element of a structure.

  a) struct.element

    b) structure_tag.element

     c) struct_variable.element

      d) structure_tag.structure_variable

   Ans. c

4. What is a nested structure?Give a example.

        A Structure placed inside another structure is called a nested structure.

5.  A script is  for array ----------is associated with structure.

   Ans. Base address of the array

6. What is a pointer?

          The data stored in computer occupies a memory cell.Each cell has a unique address.A pointer points to the address of memory location.It holds the address of the memory location.

The syntax is 
 Data_Type *Pointer_Variable;

7. What is the criterion for  determining the datatype of a pointer?

           Pointer is a derived data type and hence a variable of pointer type is to be declared  prior to its use in the program.

8. If mks is a int variable  write the c++ statement to store its address in a pointer.

    int mks;
  int *ptr1;
ptr1=&mks;

9. If  ptr is an integer pointer  write c++ statement to allocate memory of an integer number an initialize  it with  12.
10. Consider the statements

        int *p;
       a=5;
       p=&a;
      cout<<*p+a;
      what is the value of output

   Ans.  10.

11. Dynamic memory allocation operator in c++ ------------

         Ans. new and delete

12.  What happens when the statement is executed
        int *p= new int(5)

13.  What is orphaned memory block?

           If the memory allocated using no operator is not free using delete,that memory is said to be orphaned memory block a block of memory that is left unused but not released for further allocation.

 14.  If p is an integer pointer which of the following is invalid
       
       a) cout<<*p;
        b) p=p*5;
         c) p>0;
          d)p++;
           e)p=1500;
            f)cout<<*p*2;

   Ans. Invalid: c,e

15. What is dynamic Array?
 
            It is an array created during at run time using new operator is called dynamic array.

16. Address of the  first location of an  array is known as -----------------

       Ans, Base address 

17. If arr is an integer array. Which of the following is invalid
    a) arr++;
     b) cout<< arr;
      c) cout<<*(arr+1);
       d) cin>>arr;
        e) arr=1500;
         f) cout<<*arr*2;

Ans. 
18. Write a declaration statement in C++ to refer to the names of 10 books using pointers.

  struct  Book;
{
  int book_id;
char book_name[10];
float book_price;
char author_name[10];
}
19.  Write a  statement  to declare a pointer  and initialize with your names.

    Ans. char name;
             char *cp;
             cp= &name;