A. seekp().
B. tellp().
C. get().
D. put().
Every file maintains two pointers called get_pointer(in input mode file) and put_pointer(in output mode file) which tells the current position in the file where writing or reading will take place.
A. stream.
B. object.
C. file mode.
D. binary.
The file mode describes how a file is to be used : to read from it, to write to it, to append it and so on.
A. bad( ) and good( ).
B. seekp( ) and tellp( ).
C. get( ) and put( ).
D. get( ) and getline( ).
Whenever an I/O function that takes a file handle as an input parameter raises an error, it will pass this file handle as a third parameter to the error handler function.
A. seekp() and tellp().
B. get() and put( ).
C. get() and getline().
D. bad() and good().
seekp() and tellp() are used to manipulate the put_pointer used for performing write operations on a file.
A. file.
B. stream.
C. buffer.
D. template.
A stream is an abstraction that represents a device on which input and output operations are performed. A stream can basically be represented as a source or destination of characters of indefinite length.
A. fail( ).
B. good( ).
C. eof( ).
D. bad( ).
In computing, end-of-file, commonly abbreviated EOF is a condition in a computer operating system where no more data can be read from a data source. The data source is usually called a file or a stream.
A. get() and getline().
B. seekg() and seekp().
C. tellg() and tellp().
D. eof() and fail().
These functions operate on an offset calculation from the beginning, end, or current position in the stream.
A. text file.
B. binary file.
C. media file.
D. archive file.
A text file is a kind of computer file that is structured as a sequence of lines. A text file exists within a computer file system.
A. ifstream.
B. filebuf.
C. ofstream.
D. fstream.
File processing in C++ is performed using the fstream class. To perform file processing, you can declare an instance of an fstream object. If you do not yet know the name of the file you want to process, you can use the default constructor. Unlike the FILE structure, the fstream class provides two distinct classes for file processing. One is used to write to a file and the other is used to read from a file.
A. ifstream.
B. filebuf.
C. ofstream.
D. fstream.
The ofstream function provides an interface to write data to files as output streams.
A. ifstream.
B. filebuf.
C. ofstream.
D. fstream.
Class hierarchy: streambuf - - > filebuf. filebuf class applies the functionality of the streambuf class to read and write from/to files.
A. 0.
B. 1.
C. 2.
D. 3.
The tellg() gets the position of put pointer and therefore the output will be 1.
A. file.seek(3*sizeof(student), ios::beg);
B. fil.seekg(3*sizeof(student), ios::beg);
C. file.seek(2*sizeof(student), ios::beg);
D. file.seek(3*size(student), ios::beg);
The function seekg() sets current get position in a stream.
A. size of array is not given
B. condition given is wrong
C. A cannot be incremented
D. no error
A is the array name and contains the starting address.
A. 2.125 2.125 4.25.
B. 2.125 2.125 2.125 2.125.
C. 2.125 2.125 4.5156.
D. cannot be predicted as address is unknown.
The reason for this is p gives the value stored in the variable p. *(&p) gives the data value stored in the address &p i.e., the data value of x. Since a points to p, *a gives the value of p. Also, b has the same address as that of a, *b also gives the value of a which in turn produces the addition of *a and *b i.e. 2.125 + 2.125.
A. stacks.
B. heaps.
C. orphaned memory blocks.
D. parental memory blocks.
These orphaned memory blocks when increase in number, bring an adverse effect on the system. This situation is known as memory leak.
A. the class pointer.
B. the data members.
C. the this pointer.
D. an array of pointers.
When a member function is called, it is automatically passed an implicit argument that is a pointer to the object that invoked the function. This pointer is called 'this'.
A. can appear on the left of the = sign.
B. can accept only references are arguments.
C. is always called by value.
D. can also return a pointer.
A function returning a reference can appear on the left-hand side of an assignment statement.
For example, min(x,y) = -2;
A. is an array of addresses.
B. does not have a base type.
C. cannot hold addresses of objects.
D. cannot be used with the for(;;) loop.
Pointers also, may be arrayed like any other data type. To declare an array holding 5 int pointers, the declaration would be as follows:
int *ip[5]; //array of 5 int pointers
After this declaration, contiguous memory would be allocated for 5 pointers that can point to integers.
A. delete statement is not executed.
B. memory is allocated dynamically.
C. there is no memory to allocate.
D. there are too many variables.
Many possible reasons lead to the situation of memory leaks. Most common of these are:
1. forgetting to delete something that has been dynamically allocated (i.e., using new)
2. failing to notice that code may bypass a delete statement under certain circumstances.
3. assigning the result of a new statement to a pointer that was already pointing to an allocated object.
A. creates an array of 10 elements whose address is in as.
B. creates an integer array initialized to 10.
C. creates an integer pointer and assigns 10 to it.
D. assigns 10 to a location whose address is in as.
It allocates memory of one integer to as and intializes it with value 10.
A. 1.
B. 4.
C. 2.
D. 3.
In pointer arithmetic, all pointers increase and decrease by the length of the data type they point to.
A. allocates memory of one integer to ptr and initializes it with value 11.
B. allocates memory of one integer to ptr and initializes it with value 10.
C. allocates memory of 10 contiguous integers and stores beginning address in pointer ptr.
D. allocates memory of 11 contiguous integers and stores beginning address in pointer ptr.
The statement
int *ptr = new int[10];
allocates memory of 10 contiguous integers and stores beginning address in pointer ptr. Whereas, the statement
int *ptr = new int (10);
allocates memory of one integer to ptr and initializes it with value 10.
A. int & ans;
B. int *ans;
C. int ans*;
D. *int ans;
Pointer variables are declared like normal variables except for the addition of the unary * character. The general form of a particular declaration is as follows :
type * var_name;
A. . operator.
B. -> operator.
C. -< operator.
D. <- operator.
To refer to the structure members using -> operator, it is written as
struct pointer -> structure-member.
A. a variable that stores address of another variable.
B. a variable that stores value of another variable.
C. also regarded as the name of an array.
D. another name for a variable.
A reference is a type of pointer and when it is declared it must be initialised with the variable it is a reference to.
A. a variable that can store memory address.
B. an arrow towards a variable.
C. declared using ‘&’.
D. a variable that does not have an address.
A pointer is a variable that holds a memory address, usually the location of another variable in memory.
A. done using the free store of computer’s memory.
B. done through special operator, new.
C. done when memory requirements are known beforehand.
D. used to declare arrays in memory.
When the amount of memory to be allocated is known beforehand and the memory is allocated during compilation itself, it is referred to as static memory allocation.
A. done when program is compiled.
B. done at run time.
C. done using the stack.
D. used to pass arguments.
When the amount of memory to be allocated is not known beforehand rather it is required to allocate (main) memory as and when required during runtime itself, then, the allocation of memory at run time is referred to as dynamic memory allocation.
A. first element of the array.
B. last element of the array.
C. middle element of the array.
D. second last element of the array.
An array is a group of homologous elements in memory, whereas a pointer is a variable that holds a memory address.
A. null pointer.
B. zero pointer.
C. empty pointer.
D. constant pointer.
A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing to any valid reference or memory address.
A. pointer to a constant.
B. constant pointer.
C. fixed pointer.
D. function pointer.
Using a pointer to a constant, the constant value can not be modified, however the pointer can be made to point to another location.
A. plus and minus.
B. static and dynamic.
C. new and old.
D. new and delete.
The two operators that C++ offers for dynamic memory allocation and deallocation are new and delete. The operator new allocates the memory dynamically and returns a pointer storing the memory address of the allocated memory. The operator delete deallocates the memory pointed by the given pointer.
The member functions of a class exist at one place in memory and are used by all the objects. When an object calls a function, the function is automatically passed a pointer to that object. Thus, the function has the address of the object and can use its data. This pointer is called the ‘this’ pointer.
Example –
#include< iostream.h>
class stu{
int rno;
int marks;
public:
stu(int a, int b){
rno=a;
marks=b;
}
void disp(stu s){
cout<< this->rno;
cout<< this->marks;
cout<< "nPassed Object";
cout<< s.rno;
cout<< s.marks;
}
};
void main(){
stu s1(10,100);
stu s2(11,98);
s2.disp(s1);
}
#include< iostream.h>
struct item{
int code;
int price;
};
item* func(item *ptr){
cin>>ptr->code;
cin>>ptr->price;
return ptr;
}
void main(){
item i1,*pi;
pi = func(&i1); // function call
cout<< "from Pointer "<< pi->code<< pi->price;
cout<< "from variable "<< i1.code<< i1.price;
}
#include< iostream.h>
class Emp{
int code;
public :
void getdat(int x){code=x;}
void dispdat(){cout<< "code = "<< code;}
};
Emp* func(int x, Emp &Em){
Emp *p;
Em.getdat(x);
cout<< "Value passed";
p = &Em;
return p;
}
void main(){
Emp e1, e2;
Emp *ep;
e2.getdat(3);
ep = func(5,e1); // function call
e1.dispdat();
e2.dispdat();
ep->dispdat();
}
Once a structure has been defined, variables, including pointers can be declared for it. When memory is allocated dynamically for a structure, it is called a dynamic structure.
Example
struct A{
int a;
char b;
};
A v1,*v2;
v2=new A;
A structure that has a member that refers to the structure itself is known as a self referential structure.
Example -
struct guest{
int tel;
char name[20];
guest *next; // pointer to type guest
};
In both the cases, the original data changes after the function call.
When reference is passed, the prototype is something like –
void func1(int &, int &);
The actual parameters are simply given new names and accessed through them. The function call is the same as the call when function is called by value.
When pointers are passed, the prototype is something like -
void func2(int *, int *);
A constant pointer means that the address in it cannot be changed.
int a=12; // integer variable with value 12
int *pa = &a; // pointer pa stores address of a
++pa; // increment (address in) pa
int *const cpa=&a; // constant pointer named cpa stores address of a
++cpa; //error, constant pointer cannot be changed
A pointer to a constant means that the pointer holds the address of a constant.
const int cnum=23; // constant cnum declared
const int *pcnum = &cnum; // pointer to a constant stores address of cnum
++cnum; // Error, cnum is a constant
An array of pointers is an array, each element of which is a pointer.
An array of integer pointers would be declared as – int *N[5];
Now, each of the array elements, N[0], N[1], N[2], N[3] and N[4] would hold the address of an integer variable. Look at the statements below –
int a = 12;
int b = 23;
int c = 34;
int d = 45;
int e = 56;
N[0] = &a;
N[1] = &b;
N[2] = &c;
N[3] = &d;
N[4] = &e;
The following two statements would give the same output.
cout<< a;
cout<< *N[0];
Lets look at another example using a string.
char N[]=“Discover Wildlife”;
cout<< N;
cout<< *N;
cout<< *(N+1);
Output of these lines: Discover WildlifeDi
(code contd.)
char *p=N;
cout<< p;
cout<< *p;
cout<< *(p+1);
Output of these lines: Discover WildlifeDi
The name of an array is regarded as a pointer. It holds the address of the first element of the array. Consider the statements below -
int A[10] = {1,2,3,4,5,6,7,8,9,10};
A is the array name, it holds the address of the first element A[0].
cout<< A; //displays the address of the first element
cout<< *A; //displays value at that address i.e. 1
cout<< A+1; //displays another address
cout<< *(A+1); //displays value at that address i.e. 2
The ‘*’ is also called the indirection operator and ‘&’ is also called address of operator.
When a pointer is declared, the ‘*’ is used :- int *pa;
When an address is to be stored the ‘&’ is used :- pa = &a;
The ‘delete’ operator frees (de-allocates) the memory pointed to by the pointer.
The memory allocated using new remains allocated until it is freed using the delete operator. If the allocated memory is not de-allocated, then with every run of the program the amount of available memory decreases. The general form of using the delete operator is,
delete pointer_variable_name;
delete pa; // frees memory pointed to by pa
delete [10] A; // frees memory allocated to array A, array size is optional
The ‘new’ operator allocates memory dynamically and returns a pointer that stores the address of the location where memory has been allocated. The general form of allocating is :
pointer_variable_name = new data_type;
Here, data type can be any valid data type, including structures and classes. Bytes are allocated according to the data type and the address is stored in the pointer variable of the same data type.
For example : int *pa;
pa = new int;
*pa=12; // 12 is stored at the address in pa
Here, 2 bytes will be allocated for an integer which will be accessed through the address in pointer pa.
Static Memory Allocation : when memory to be allocated is known beforehand, it is allocated during compilation. Generally in a program, variables are declared along with data type and arrays are declared with size and memory is allocated accordingly.
Dynamic Memory Allocation : when the amount of memory required is not known beforehand, it is allocated while the program is running, and is known as dynamic memory allocation. Two operators are used - new and delete. Memory for allocation at runtime is obtained from the free store or heap.
Four logically separate areas are created after a C++ program is compiled.
- One area holds all the program statements that are to be executed i.e. the compiled program code.
- The next area holds all the global variables being used.
- The third area, also called Stack, stores all local variables, arguments passed to functions and the return address when functions are called.
#include< iostream.h>
class Emp{
int code;
public :
void getdat(int x){code=x;}
void dispdat(){cout<< "code = "<< code;}
};
Emp& func(int x, Emp& Em){
Em.getdat(x);
cout<< "Value passed";
return Em;
}
void main(){
Emp e1, e2;
e2.getdat(3);
func(5,e1) = e2; //function call
e1.dispdat();
e2.dispdat();
}
When memory is allocated dynamically using the ‘new’ operator, it remains occupied even when the program is over. Such occupied memory blocks are known as orphaned memory blocks . Gradually, as the number of such blocks increases, there is less memory available to the program which affects its working. This situation is called Memory Leak.
Some reasons for memory leak are -
·
·
·
Memory leak can be avoided by –
·
#include< iostream.h>
void main()
{
char *tmp; int i,j;
char *list[]={"Asia", "Bahamas","Canada","Denmark","England"};
for( i=0;i< 5;i++)
cout<< list[i]<< endl;
for( i=0,j=4; i < 2;i++,j--)
{
tmp=list[i];
list[i]=list[j];
list[j]=tmp;
}
for( i=0;i < 5;i++)
cout<< list[i]<< endl;
#include< iostream.h>
void main()
{
char A[80];
char *str;
char* funblank(char*); // function prototype
cout<< "Enter string ";
cin.getline(A,80);
str=funblank(A); // function call
cout<< A<< endl;
cout<< str<< endl;
}
char* funblank(char* t)
{
char *tmp;int i=0;
while(*t !='?')
{
if(*t!=' ')
{
tmp[i]=*t;
++i;
}
++t;
}
tmp[i]='?';
return tmp;
A. tree.
B. graph.
C. stack.
D. binary search tree.
In stack, the items are stored sequentially, thus forming a linear data structure.
A. array.
B. linked list.
C. stack.
D. tree.
In tree, the elements are not stored in a sequence therefore it’s a non-linear data structure.
A. selection sort.
B. bubble sort.
C. insertion sort.
D. quick sort.
In insertion sort a single element is get considered at a time, inserting each in its suitable place.
A. selection sort.
B. bubble sort.
C. insertion sort.
D. quick sort.
Bubble sort works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order.
A. INT_MAX.
B. INT_MIN.
C. INT_LIM.
D. INT_VAL.
Insertion sort works if the very first element stores the minimum element of the array. In C++, INT_MIN constant from Limits.h, can be used to store minimum possible integer value.
A. 8.
B. 10.
C. 70.
D. 18.
No. of rows = 4 - (-2) + 1 = 4 + 2 + 1 = 7
No. of columns = 6 - (-3) + 1 = 6 + 3 + 1 = 10
No. of elements = Rows X Columns = 7 X 10 = 70 elements.
A. object pointer.
B. current pointer.
C. function pointer.
D. this pointer.
this pointer refers to the current object we are working on.
A. call by value.
B. call by reference.
C. call by address.
D. call by name.
In call by value method, the called function creates its own copies of original values sent to it. Any changes, that are made, occur on the function’s copy of values and are not reflected back to the calling function.
A. pointer cannot be used in assignment statement.
B. ip is a pointer and cannot be used as integer.
C. pointer should not be initialized.
D. pointer cannot be equal to zero.
A pointer points to the address of a variable. It cannot be used as integers.
A. 10 10 10 10.
B. 10 5 20 20.
C. 5 10 10 10.
D. 10 10 20 20.
j is a reference to i. If, we assign value of p to j, then value of i also changes.
A. data item.
B. data type.
C. raw data.
D. information.
Data can be of different types. It can be numeric, alphabetic, or alphanumeric.
A. compiler error : illegal structure declaration.
B. no error.
C. :: cannot be used with constant.
D. run-time error.
&::i is an illegal declaration used in the code.
A. no error.
B. c cannot be passed as an argument. It should be c[].
C. string.h header file is not defined.
D. no return statement for main ().
“string.h” header file defines the string functions like strlen, strcpy. These functions cannot be used without using string.h header file.
A. stack.
B. queue.
C. linked list.
D. array.
Simple data structures are normally built from primitive data types like integers, real, characters, boolean.
A. application level of data structure.
B. abstract level of data structure.
C. the implementation level of data structure.
D. the only way of designing data structure.
Application (or user) level is a way of modelling real-life data in a specific context, also called the problem domain. The data structure can be designed from user level perspective.
A. int.
B. float.
C. double.
D. union, structures.
Non-primitive data types are those data types types which are composed of primitive data types such as int, float, char.
A. structure.
B. single dimensional array.
C. multi dimensional array.
D. two dimensional array.
The syntax of single dimension array is “datatype arrayname[size]” whereas multi dimension arrays are accessed using more than one index. Example names[][]. It can be 2 dimensional or 3 dimensional.
A. aditi.
B. priyanka.
C. amrita.
D. mudita.
One space is needed for null character, which acts as a terminator symbol.
A. aditi.
B. priyanka.
C. amrita.
D. mudita.
One space is needed for null character, which acts as a termination symbol.
A. a structure.
B. single dimensional array.
C. multi dimensional array.
D. two dimensional array.
The syntax of single dimension array is “datatype arrayname[size]” whereas multi dimension arrays are accessed using more than one index. Example names[][]. It can be 2 dimensional or 3 dimensional.
A. arrays.
B. structures.
C. stacks and queues.
D. arrays and structures.
Simple data structures can be combined in various ways to form more complex structures called compound data structures.
A. 5 elements.
B. 29 elements.
C. 30 elements.
D. 11 elements.
The total number of elements in an array [a][b] is a x b. Here 5 x 6 =30.
A. constant
B. function
C. variable
D. data type
A pointer is a variable that holds a memory address, usually the location of another variable in memory.
A. done when program is compiled.
B. done at run time.
C. done using the stack.
D. used to pass arguments.
In computer science, dynamic memory allocation (also known as heap-based memory allocation) is the allocation of memory storage for use in a computer program during the runtime of that program.
A. done using the free store of computer's memory.
B. done through special operator, new.
C. done when memory requirements are known beforehand.
D. used to declare arrays in memory.
Static memory allocation refers to the process of allocating memory at compile-time before the associated program is executed, unlike dynamic memory allocation or automatic memory allocation where memory is allocated as required at run-time.
A. memory is not released using delete operator.
B. memory is allocated dynamically.
C. there is no memory to allocate.
D. there are too many variables.
A memory leak or leakage in computer science is a particular type of memory consumption by a computer program where the program is unable to release memory it has acquired.
A. int & ans;
B. int *ans;
C. int ans*;
D. *int ans;
The pointer is an integer that is intended to hold a memory address. The pointer may be an array or scalar. A pointer can be an assumed size array – that is, the last dimension may be left unspecified by using a '*' in place of a value – but a pointer cannot be an assumed shape array. No space is allocated for the pointer.
A. class pointer.
B. data members.
C. this pointer.
D. an array of pointers.
The 'this' pointer is a pointer accessible only within the non-static member functions of a class, struct, or union type. It points to the object for which the member function is called.
A. can appear on the left of the = sign.
B. can accept only references are arguments.
C. is always called by value.
D. can also return a pointer.
Functions can be declared to return a reference type. There are two reasons to make such a declaration. The information being returned is a large enough object that returning a reference is more efficient than returning a copy. The type of the function must be an l-value.
A. is an array of addresses.
B. does not have a base type.
C. cannot hold addresses of objects.
D. cannot be used with the for(;;) loop.
An array of pointers is used to store addresses of more than one data elements. Each element of the pointer array points to the address of an element.
A.
define a pointer variable named marks.
B. access the address in marks.
C. access the value stored in marks.
D. store NULL value in the variable marks.
The pointer variable is used to store address of a varible. we can declare a pointer variable using astrick (*) Example: int *marks; Here marks is a pointer variable which can hold address of an integer variable.
char * funstring(char *);
float rate;
float *fp;
fp = &rate;
Every pointer has a base data type. It holds the address of variables of that data type. When 1 is added to a pointer, the size of the data type is added to it. Suppose pointer pa holds the address of integer variable a, as 1001. Adding 1 to pa will give 1003. This is because the size of int is 2.
Suppose pf is a pointer that holds the address of float variable f, as 2300. Adding 3 to pf will add 3 times the size of float i.e. 12, to the address in pf, making it 2312.
The four regions are:
1. Program code
2. Global variables
3. Stack
4. Heap
Free Store is also called Heap. It is unallocated memory that is used during program execution. Memory that is dynamically allocated from this free store is unnamed and is manipulated through the address i.e. pointers.
A reference variable is an alias or another name for an existing variable while a pointer is a variable that stores an address. The reference does not occupy memory space but a pointer does. The declaration too is different –
int a; // variable named a
int &alsoa = a; // reference alsoa created, is an alias for a;
int *pa; // pointer pa created
pa=&a; // address of a stored in pa
Normally when we declare a pointer, we give it a type which is the same as the type of the variable whose address it will store. i.e. int *ptr; will create pointer ptr which will hold addresses of integer variables.
Pointer to void is like a general purpose pointer that can store address of any data type. It is used only in certain situations.
A is the address where the system has placed the array and it will stay here till the end of the program. As it is the base address of the array, it is a constant and cannot be changed. However, we can declare a pointer and assign A to it. Now, the pointer variable can be incremented, if required.
int A[]={1,2,3,4,5};
int *p;
p=A;
A[3] refers to the value in array A with index number 3.
*(A + 3) refers to the value at the address A + 3.
In both the cases here, the same element is accessed.
In statement - int *p1 = new int(12);
Bytes are allocated in memory, to store an integer and initialized to 12, the address of this memory location is stored in pointer p1.
In statement - int *p2 = new int [12];
A. stack.
B. queue.
C. linked list.
D. tree.
Compound data structures are classified into following two types : Linear data structures and non-linear data structures. Linear data structures are single level data structures whereas non-linear data structures are multilevel data structures.
A. stack.
B. array.
C. data Structure.
D. queue.
Data Structure refers to a named collection of variables of different data types which can be possessed as a single unit.
A. application (or user) level of data structure.
B. logical level of data structure.
C. implementation level of data structure.
D. the only way of designing data structure.
There are different perspectives while designing a data structure. The implementation requires writing a set of procedures that create and manipulate instances of that structure.
A. arrays.
B. structures.
C. int, float, double.
D. class.
Primitive data types are fundamental or standard data types, such as integer, real or character data types.
A. primitive data type.
B. non-primitive data type.
C. fundamental data type.
D. standard data type.
Non-primitive data types are defined by the users. That is why, they are called as user defined data types.
A. character string.
B. array.
C. integer.
D. character.
A string of characters is collection of character array. A character array is also known as character string.
A. 0 to 20 subscripts.
B. 0 to 19 subscripts.
C. 1 to 20 subscripts.
D. 1 to 19 subscripts.