Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers

Kerala State Board New Syllabus Plus Two Computer Science Chapter Wise Previous Questions and Answers Chapter 1 Structures and Pointers.

Kerala Plus Two Computer Science Chapter Wise Previous Questions and Answers Chapter 1 Structures and Pointers

Question 1.
Represent a structure named student with types and give the advantages of using structure. [March -2016] (3)
Answer:

struct student
{
int regno;
char name [25];
struct
{
short dd;
short mm;
short yy;
} dob;
char sex;
};

the structure is a group of different types of logically related data referenced by a single name.

Question 2.
Structure within a structure is termed as__ [March -2016]
Answer:
Nested structure

Question 3.
Orphaned memory blocks are undesirable. How can they be avoided [March -2016]
OR
Discuss problems created by memory leaks. (2)
Answer:
Proper use of delete operator is heeded
0R
1) It causes orphaned memory blocks
2) wastage of memory
3) insufficient memory
4) due to this sometimes system may be hanged

Question 4.
Explain the use of for loop with an appropriate example. [March -2016] (3)
Answer:
For loop is used to execute a statement more than once.
Refer 5 Mark Question 2

Question 5.
a) How will you free the allocated memory? [MAY-2016] (1)
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. (2)
Answer:
a) delete operator is used to free the allocated memory.

b)

struct time
{
int hours;
int minutes;
int seconds;
};
time current, next;

Question 6.
A program is implemented to find the area of a circle and area of a rectangle with two functions having same name but with different signature. [MAY-2016]
a) Name the concept (1)
b) Explain this concept by writing the above program. (2)
Answer:
a) Polymorphism
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 1
Here the function names are same but the return type and number of parameters are different hence distinguish the functions.

Question 7.
a) Write a C++ program to store and print information (name, roll and marks) of a student using structure. [MAY-2016] (3)
OR
b) Write a program in C++ to input the total marks obtained by a group of students in a class and dis-play them in descending order using pointer. (3)
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 2
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 3
OR
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 4

Question 8.
Compare the aspects of arrays and structures. [March – 2017] (3)
Answer:
Array-: An array is a collection of elements with same data fypeOr with the same name we can store many elements, the first or second or third etc can be distinguished by using the index(subscript). The first element’s index is 0, the second elements index is 1, and so on.

To store 100 numbers the array declaration is as follows

int n[100]; By this we store 100 numbers. The index of the first element is O and the index of last element is 99.

Structure -: But a Structure is a group of different types of Ipgically related data referenced by a single name.
Eg. The collection of details of a student that may contain various data with different data types.

Eg.

struct student
{
int adm_no;
char name[40];
float weight;
};

OR

Structure Array
Differences
1. It is a user-defined data type
Predefined data type
2. It is a collection of different types of logically related data under one name. Collection of data elements of same data type having a common name.
3. Elements referenced using dot operator(.) Elements reference using its subscripts (position value)
4. When an element of a structure becomes another structure nested structure and complex structures are formed. When an element of another becomes another array, multidimensional arrays are formed.
5. Structure contains array as its elements Array of structure can be formed.

Question 9.
Run time allocation of memory is triggered by the operator. [March – 2017] (1)
Answer:
new

Question 10.
Represent the names of 12 months as an array of strings. [March – 2017]
OR
A structure can contain another structure. Discuss. (2)
Answer:
char month(1 2)[]={ “Jan”,”Feb”,”Mar”, “Apr”,May”,“Jun”,”July”,”Aug”, “Sep”,Oct”,“Nov”,”Dec”};
OR
Yes It is possible. This is called nested structure.
Eg.

struct date
{
short day,month,year’
};
struct student
{
inl reg_no;
char name[40];
date dob;
};

Plus Two Computer Science Chapter Wise Practice Questions Chapter 1 Structures and Pointers

Question 1.
Define a structure to represent the details of telephone subscribers which include name of the subscriber and telephone number. Write a menu driven program to store the details of some subscribers with options for searching the name for a given number, and the number for a given name.
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 5
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 6
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 7
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 8

Question 2.
Define a structure to represent the details of customers in a bank. The details include account number, name, date of opening the account and balance amount. Write a menu driven program to input the details of a customer and provide options to deposit, withdraw and view the details. During deposit and withdrawal, proper update is to be made in the balance amount. A minimum balance of Rs. 1000/- is a must in the account.
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 9
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 10
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 11

Question 3.
Write a program to input the TE scores obtained by a group of students in Computer Science and display them in the descending order using pointers.
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 12
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 13

Question 4.
Write a program to input a string and check whether it is palindrome or not using character pointer.
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 14

Question 5.
Write a program to input the names of students in a class using pointers and create a roll list in which the names are listed in alphabetical order with roll number starting from 1.
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 15
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 16

Question 6.
Define a structure student with the details register number, name and CE marks of six subjects. Using a structure pointer, input the details of a student and display register number, name and total CE score.
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 17
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 18
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 19

Plus Two Computer Science Assess Question and Answers

Question 1.
Compare array arid structure in C++.
Answer:

Structure Array
Differences
1. It is a user defined data type
2. It is a collection of different types of logically related data under one name.
3. Elements referenced using dot operator(.)
4.When an element of a structure becomes another structure nested structure and complex structures are formed.
5. Structure contains array as its elements
Predefined data type
Collection of data elements of same data type having a common name.
Elements reference using its subscripts (position value).
When an element of another becomes another array, multidimensional arrays are formed.
Array of structure can be formed.

Question 2.
Identify the errors in the following structure definition and write the reason for each:

struct
{
int roll, age;
float fee= 1000:
};

Answer:
Errors
1) No name for the structure.
The correct structure is as follows

struct student
{
short roll.age:;
float fee=1000;
}

Question 3.
Read the following structure definition and answer the following questions:

Struct Book
{
int book_no;
char bk_name [20];
struct
{
short dd;
short mm;
short yy;
} dI_of_purchase;
float price;
};

a) Write a C++ statement to declare a variable to refer to the details of a book. What is the memory requirement of this variable? Justify your answer.
b) Write a C++ statement to initialize this variable with the details of your Computer Science text book.
c) Write C++ statement (s) to display the details of the book.
d) The missing of structure tag in the inner structure does not cause any error. State whether this is true or false. Give reason.
Answer:
a) Book b;
The memory requirements are as follows

variable  Memory Size
book_no  4
bk_name  20
dt_of_purchase  2+2+2=6
price  4

A total of 34 Bytes allocated to the variable b.

b) book b={101 .”Computer Science”,{22,9,2015},127};

Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 20
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 21

d) This is true. There is no need to mention the structure tag because it is declared inside the structure.

Question 4.
“Structure is a user-defined data type”. Justify this statement with the help of an example.
Answer:
Yes it is true. A user can give define according to his needs.
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 22

Question 5.
Read the following statements:
i) While defining a structure in C++, tag may be omitted.
ii) The data contained in a structure variable can be copied into another variable only if both of them are declared using the same structure tag.
iii) Elements of a structure is referenced by stmcture_name. element
iv) A structure can contain another structure.
Now, Choose the correct option from the following:
a) Statements (i) and (ii) are true
b) Statements (ii) and (iv) are true
c) Statements (i), (ii) and (iv) are true
d) Statements (i) and (iii) are true jtyg
Answer:
d) Statements (i) and (iii) are true.

Question 6.
Read the following C++ statements:
int * p, a=5
p=&a;
a) What is the speciality of the variable p?
b) What will be the content of p afterthe execution of the second statement?
c) Howdo the expressions *p+1 and* (p+1) differ?
Answer:
a) p is special variable and it is called a pointer.
b) p contains the address of the variable a

Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 23

Here p+1 returns (address of the variable a) +4(4 is the size of int data type in Geany C++). *(p+1) returns the content of this next address location.

Question 7.
Identify the errors in the following C++ code segment and give the reason for each.

int p, *q a=5;
float b2;
p=&a;
cout<<p<<*p<<*a;
if (p<q)coLit<<p;
cout<<cp *a;

Answer:
Following are the errors
1) Here q is an integer pointer it can not store the address of float variable b.
2) Cannot print *a.
3) The pointers p and q are different data types so cannot use relational operator.

The correct code is as given below.
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 24

Question 8.
While writing a program, the concept of dynamic memory allocation is applied. But the program does not contain a statement with delete operator and it creates a problem. Explain the problem.
Answer:
If the memory allocated using new operator is not de allocated using delete, that memory is left unused and not released forfurtherallocation. Such memory blocks are called orphaned memory. On each execution the amount of orphaned block is increased. This situation is called memory leak.

Question 9.
Read the C++ statements given below and answer the following questions: •
int ar[] = {34,12, 25,56, 38};
int *p = ar;
a) What will be the content of p?
b) What is the output of the expression: *p + *(ar+2)?
c) The statement ar++; is invalid. Why? How does it differ from p++;?
Answer:
a) 34
b) *p=34 and *(ar+2)=25
then *p+*(ar+2)=34+25=59.
c) ar++ is invalid but p++ valid, p is the pointer, pointer arithmetic is allowed.

Question 10.
Explain the working of the following code segment and predict the output:
char *str = “Tobacco Kills”;
for (int i=0; str [i] !=’\0’; i++)
if (i>8)
* (str +i) = toupper (*(str+i);
cout«str;
Answer:
The output is “Tobacco KILLS”. The toupperO function convert the characters into upper case from ninth characteronwards.

Question 11.
Observe the following C++ statements:
int ar [ ] = {14, 29, 32,63, 30};
One of following expressions cannot be used to access the element 32. Which is that?
a) ar [2] b)ar[*ar%3] c)*ar+2 d)*(ar+2)
Answer:
c) *ar+2.
*ar returns 14 and *ar+2 gives 14+2=16.

Question 12.
Explain the operations performed by the operators new and delete with the help of examples.
Answer:
new operator is used to allocate memory dynamically

Syntax:
pointer_variable =newdata_type;
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 25

Question 13.
What is meant by memory leak? What are the reasons for it? How can we avoid such a situation?
Answer:
If the memory allocated using new operator is not de allocated using delete , that memory is left unused and not released for further allocation. Such memory blocks are called orphaned memory. On each execution the amount of orphaned block is increased. This situation is called memory leak. Use delete operator to avoid this.

Question 14.
Compare the following two statements, int a=5;
int*a=newint(5);
Answer:
int a=5; This means ‘a’ is an integer variable that is initialized with the integer value 5.
int *a=new int(5). Here ‘a’ is a pointer variable and it allocates memory dynamically and stores a value 5.

Question 15.
Read the structure definition given below and answer the following questions:

struct sample
{
int num;
char *str;
} *sptr

a) Write C++ statements to dynamically allocate a location for sample type data and store its address in sptr.
b) Write C++ statements to input data into the location pointed to by sptr.
c) Modify this structure into a self-referential structure.
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 1 Structures and Pointers 26