0% found this document useful (0 votes)
53 views7 pages

CHAPTER 11 Completed

The document contains one mark multiple choice questions about pointers in C++. It begins with 10 questions about pointers, their usage and declaration. It then provides the answers to those 10 questions. The document continues with another set of 15 multiple choice questions about pointers, followed by the answers to those questions. Finally, it provides model answers to 7 three mark questions about pointers, their usage, dynamic memory allocation, self-referential structures, and arrays of pointers.

Uploaded by

prasanthboddu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views7 pages

CHAPTER 11 Completed

The document contains one mark multiple choice questions about pointers in C++. It begins with 10 questions about pointers, their usage and declaration. It then provides the answers to those 10 questions. The document continues with another set of 15 multiple choice questions about pointers, followed by the answers to those questions. Finally, it provides model answers to 7 three mark questions about pointers, their usage, dynamic memory allocation, self-referential structures, and arrays of pointers.

Uploaded by

prasanthboddu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

NANDI PU COLLEGE

Ch -11 POINTERS

One mark MCQ’s questions:-


1. Pointer is used to store
(a) Memory address (b) Value
(c) Data (d) Information

2. Which of the following is the correct way to declare a pointer?


(a) int *ptr; b) int ptr;
(c) int &ptr; d) All of the above

3. Which of the following operator is used to declare pointer?


(a) * (b) &
(c) = (d) ~

4. Which of the following operator is used to initialize pointer?


(a) * (b) &
(c) = (d) ~

5. Memory allocated during runtime is_________memory allocation


(a) Compile time (b) Dynamic
(c) Static (d) None of the above
6. new operator is used to create memory during
(a) Compile time (b)Runtime
(c) Static time (d) None of the above

7. ____operator is used delete memory created during runtime


(a) new (b) delete
(c) NOT (d) AND

8. is the collection of addresses?


(a) Array of objects (b) Array of pointers
(c) Array of elements (d) Array of variables.
9. is the built in pointer that hold the address of the object defined for a
class

(a) this pointer (b) new


(c) delete (d) AND

10.Pointers pointing to object of a class called


(a) Array of pointers (b) Class pointers
(c) Object pointer (d) This pointer

1
NANDI PU COLLEGE

11.is the structure that include an element that points to another structure
of same type
(a) Memory heap (b) Self-referential structure
(c) Structure (d) Memory pool

12.Dynamically allocated memory not deleted remains occupied called


(a) Orphaned memory (b) Memory pool
(c) Stream (d) Structure
13. Which of the following is the correct way to declare a pointer
a) int *ptr b) int ptr c) int &ptr d) all of the above
14. Which of the following is not possible in the pointers?
a) adding an integer value of pointer b) addition of two pointers
c) subtracting an integer value from pointer
d) incrementing and decrementing of pointers
15. _________ operator is used to allocate memory to objects during run Time
a) new b) & c) delete d) *
16. How do you declare pointer?
a) data type pointer variables b) data type & pointer variable
c) pointer variable = & variableanem d) data type *variable name
17. Static memory allocation is done at ______
a) compile time b) dynamic time c) runtime d) execution time
18. _________ operator returns the value located at the memory address
specified by its operand
a) * b) new c) & d) →
19. Which of the following is the pointer operator?
a) * b) & c) → d) all of the above
20. Which of the following is pointer address operator
a) * b) & c) → d) all of the above
21. Array of pointers means that it is a collection of _______
a) pointers b) address c) array d) objects
22. Following operation that cannot be performed over pointers
a) add an interger value to a pointer
b) subtract an integer value of a pointer
c) compare two pointers
d) division of two pointers
23. ________ is used to free dynamic memory
a) delete b) destructor
c) drop d) constructor
24. Every object in C++ has access to its own address through _________
a) pointer b) this pointer
c) array of pointer d) pointer
25. C++ allows us to use the name of the array a, without any subscript as another name for
_________
a) a[0] b) a [ ] c) & a [0] d) & a [ ]

2
NANDI PU COLLEGE

1 2 3 4 5 6 7 8 9 10

a a a c b a b b a c

11 12 13 14 15 16 17 18 19 20

b a a b a d a c a b

21 22 23 24 25

a d a b b

Key Answers:-

Three marks questions:-


Q.1 What are the advantages of pointer?
A: - The advantages of pointer are:-
 It is possible to write efficient program.
 Memory is utilized properly.
 Dynamic allocation and de-allocation of memory takes place.
 Easy to deal with hardware components.
 Establishes communication between program and data.
 Pointers are used for file handling.
 Pointers are used for creating the complex data structures such as stacks, queues, linked list,
trees and graph.

Q.2 How dynamic memory allocation is different from static memory allocation?
A:
Static Memory Allocation Dynamic Memory Allocation
 The memory allocation occurs at  The memory allocation occurs at
compilation time. execution time or run time.
 Variables remain allocated until the  Variables remain allocated until the
program is active. program unit is active.
 Implemented using stacks and heaps.  Implemented using data segments.
 Wastage of memory space occurs.  No wastage of memory space. Thus, it
Thus it is not an efficient technique. is an efficient technique.
 The amount of memory to be allocated  The amount of memory to be allocated
is pre-known. is not known.

Q.3 Illustrate the use of “self referential structure” with the help of an example?
A: The self referential structures are structures that include an element that is a pointer to another
structure of the same type.
Eg:- struct student

3
NANDI PU COLLEGE

{
int regno;
char name[25];
float fees;
struct student *next;
};
Also assume that, the list consists of three nodes n1,n2 and n3.
struct student n1,n2,n3;
In the above example, student is a “self referential structure”.

Q.4 What is new operator in c++? Give example.


A: “New operator” allocates memory for a variable dynamically.
Syntax:- datatype *pointer_variable;
pointer_variable = new datatype;
Eg:- int *iptr;
iptr = new int;
In the above example, iptr is a pointer variable. 2 bytes of memory is allocated, since the datatype
is integer and then makes the pointer variable to point to this new memory.

Q.5 what is delete operator in c++? Give example.


A: “delete operator” is used to de-allocate the memory dynamically.
Syntax:- delete pointer_variable;
Eg:- delete iptr;
In the above example, the memory which was allocated for iptr (2 bytes) is de-allocated by the
delete operator.

Q.6 Show the general form of new and delete operator in C++?
A: The general form of new operator in C++ is:-
datatype *pointe-rvariable;
pointer_variable = new datatype;

The general form of delete operator in C++ is:-


datatype pointer_variable;

Q.7 What is array of pointers? Give Example


A: Like other datatypes, pointers are also defined in groups called as “Array of pointers”.
Syntax:- datatype *array_name[size];
Eg: - int *arraypointer[5];
It creates five pointer variables to store the address of five integers.
i.e., arraypointer[0] = 10;
arraypointer[1] = 20;
arraypointer[2] = 30;
arraypointer[3] = 40;
arraypointer[4] = 50;
The values stored in memory locations are displayed using pointers as follows:-
for(i=0; i<5; i++)
cout<< *arraypointer[i] <<setw(4)<<endl;

4
NANDI PU COLLEGE

A program to illustrate the use of array of pointers:-

#include<iostream.h>
#include<conio.h>
void main( )
{
int i ,n , *ptr , *a[10];
cout<<” Enter the size of an array “<<endl;
cin>>n;
cout<<” enter array elements “<<endl;
for(i=0; i<n; i++)
cin>>a[i];
cout<<” The array elements are “<<endl;
for(i=0; i<n; i++)
cout<<*ptr[i]<<setw(4)<<endl;
}
Q.8 What is the relationship between array and pointers? Give example.
A: An array stores the similar type of elements in contiguous memory locations for efficient access.
To access all such values using pointers, an initial address of the array must be assigned to the pointer.
Later such elements can be accessed by incrementing the counter.

Eg:- int a[5], i, *arrayptr;


for(i=0; i<5; i++)
cin>>*arrayptr[i];
arrayptr = a;
This statement assigns an initial address of an array to a pointer.

The data elements are accessed as follows:-


cout<<*arrayptr; //displays 1st element from the array
arrayptr++;
cout<<*arrayptr; //display 2nd element from the array
arrayptr++;
cout<<*arrayptr; //display 3rd element from the array and so on.

Q.9 What is the relationship between strings and pointers? Give example.
A: A pointer variable must be declared and starting address of the string must be assigned to a
pointer. It points to first character of an array. Later by incrementing the counter rest of the
characters can be accessed.

Eg:- char s[20] = { “computer science” }; //initialization of a string


char *strptr; //declaration of a pointer to a string
strptr = s;
Above statement assigns initial address of the string “computer science” to a pointer strptr.
*strptr points to contents of the initial address of a string ie, first character of the string ( s[0]=’c’).
cout<<*strptr; //displays 1st character i.e., s[0]=c.
Later by incrementing the pointer, other sequential characters can be accessed as follows:-
5
NANDI PU COLLEGE

strptr++; //points to next position


*strptr; //displays 2nd character i.e, s[1] = o.
strptr++; //points to next position
*strptr; //displays 3rd character i.e s[2] = m.
Similarly, rest of the characters are displayed by incrementing the pointer.

C O M P U T E R S C I E N C E \0

s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9] s[10] s[11] s[12] s[13] s[14] s[15] s[16]

fig. Memory representation of a string.

Q. 10 what is the relationship between structures and pointers? Give example.


A: In c++, pointers are used to access the structure efficiently. The structure is a logically
related data elements of same or different type. The memory is allocated contiguously for all the
members for efficient access. Thus it is efficient compared to arrays. Like other pointers the structure
pointer is declared by the prefix “ * ” in front of a pointer name.

Syntax: - struct_name *struct_pointer;

where,
struct_name is the name of defined structure and
struct_pointer is the pointer to a structure.

Eg:- struct date


{
int dd, mm, yy;
};
date *dateptr;

where,
dateptr is a pointer to a structure date.

Q.11 What is the relationship between object and pointer? Give example.
A: - A pointer pointing to an objects are referred as “object pointers “.
Just like other pointers, object pointers are declared by placing “ * “ in front of
object pointer name.
Syntax:- class_name *object_pointer;

where,
class_name is the name of defined class and
object_pointer is the pointer to an object of class type.

Eg:- #include<iostream.h>
6
NANDI PU COLLEGE

#include<conio.h>
class student
{
private:
int regno;
char name[20];
float fees;
public:
void input( );
void output( );
};
void student ::input ( )
{
cout<<” Enter student register number, name and fees “<<endl;
cin>>regno>> name>>fees;
}
void student :: output( )
{
cout<< “ Student register number = “<<regno<<endl;
cout<<”Student name =”<<name<<endl;
cout<<” Student fees = “<<fees<<endl;
}
void main( )
{
student s, *sp;
sp = &s;
sp ->input( );
sp ->output( );
getch( );
}

In the above example,


‘s’ is a object to the defined class student and
‘* sp’ is a pointer to the object ‘s’.
Arrow operator is used to access the members of the class.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy