27
Authors Name: Richa Garg College/Institution: Indraprastha College(w) Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals Unit no : V Unit name: Files, Pointers and Derived Types Sub-unit/Topic No. Name: lecture 36-Pointer I Tag: Q1/LOD1/ MCQS/ Computer Science/B.Sc (hons)/First year /CSHT_101/UnitV/Rich a Garg Question Array passed as an argument to a function is interpreted as a. Address of the arr ay b. Values of th e firs t ele ments of the array c. Addr ess of t he fir st ele ment of the ar ray d. Number of ele men t of the a rra y Correct Option c  Justification/ Feedback for the option a No it is not the Address of the array  Justification/ Feedback for the option b It is not a value b ut an address of the first element  Justification/ Feedback for the option c Yes it is correct that Array passed as an argument to a function is interpreted as an address of the first element  Justification/ Feedback for the option d Number of elements can be specified as another argument but not mixed with the address of the first element. Reviewer’s Comment: Author’s Name: Richa Garg College/Institution: Indraprastha College(w) Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming F undamentals Unit no : V Unit name: Files, Pointers and Derived Types

Lec 36

Embed Size (px)

Citation preview

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 1/27

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming FundamentalsUnit no : V Unit name: Files, Pointers and Derived Types

Sub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q1/LOD1/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

Array passed as an argument to a function is interpreted as

a. Address of the array

b. Values of the first elements of the arrayc. Address of the first element of the array

d. Number of element of the array

Correct Option c

 Justification/ Feedback for the option a

No it is not the Address of the array

 Justification/ Feedback for the option b

It is not a value but an address of the first element

 Justification/ Feedback for the option c 

Yes it is correct that Array passed as an argument to a function is

interpreted as an address of the first element

 Justification/ Feedback for the option d 

Number of elements can be specified as another argument but not

mixed with the address of the first element.

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived Types

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 2/27

Sub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q2/LOD1/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

Which of the following is the correct way of declaring a float pointer:

a. float ptr;

 b. float *ptr;

c. *float ptr;

d. float *&ptr;

Correct Option b

 Justification/ Feedback for the option a

This is a variable of type float and not a pointer

 Justification/ Feedback for the option b

This the correct declaration of a pointer to a float data

 Justification/ Feedback for the option c 

This is invalid syntax for a pointer to a pointer to float data

 Justification/ Feedback for the option d 

This syntax is invalid of declaring a pointer to a data of type float

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

Tag:

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 3/27

Q3/LOD1/ MCQS/ Computer Science/B.Sc (hons)/First year/CSHT_101/UnitV/Richa Garg

Question

The reason for using pointer is ...

Choose the correct option from the following sentences

a. Accessing arrays or string elementsb. Dynamic memory allocation

c. Implementing linked list, trees, graphs and many other datastructures

d. All are true

Correct Option d

 Justification/ Feedback for the option aThis is one of the application of using pointers

 Justification/ Feedback for the option b

This is another application of using pointers

 Justification/ Feedback for the option c 

This is also another application of using pointers

 Justification/ Feedback for the option d 

Since all these are the applications of pointer so this is the most

appropriate option.

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subject: Data Structures  

Paper No.: CSHT-101 Paper Name: Programming FundamentalsUnit no : V Unit name: Files, Pointers and Derived Types

Sub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q4/LOD1/ MCQS/ Computer Science/B.Sc (hons)/First year

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 4/27

/CSHT_101/UnitV/Richa Garg

Question

C++ uses ____ as the address operator

a. asterisk (*)b. dot (.)c. ampersand (&)

d. the hash (#)

Correct Option c

 Justification/ Feedback for the option a

This is used as a symbol for pointers

 Justification/ Feedback for the option b

This is used to access a data member of a structure

 Justification/ Feedback for the option c 

This is the valid option as C++ uses & as the address operator.

 Justification/ Feedback for the option d 

This is not a valid operator in C++ in terms of memory.

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q5/LOD1/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 5/27

A pointer refers to _____________

a. location of the object in the program on diskb. location of the object in memory

c. value of CPU registers

d. value of the memory specified

Correct Option b

 Justification/ Feedback for the option a

It is not the location on disk

 Justification/ Feedback for the option b

It is the location in memory

 Justification/ Feedback for the option c 

It does not contain the contents of any registers

 Justification/ Feedback for the option d 

It is location of the memory specified and not the value

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q6/LOD1/ MCQS/ Computer Science/B.Sc (hons)/First year/CSHT_101/UnitV/Richa Garg

Question

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 6/27

Which of the following statements initialises x to be a pointer?

(i) int *x = NULL; (ii) int x[ ] = {1,2,3}; (iii) char *x = ‘itb421’;

a. (i) only

 b. (i) and (ii)c. (i), (ii) and (iii)

d. (i) and (iii)

Correct Option d

 Justification/ Feedback for the option a

Not only (i) but (iii) also initializes x to be pointer

 Justification/ Feedback for the option b

Here x is an array and not pointer.

 Justification/ Feedback for the option c 

Since in (ii) x is an array and not pointer so this option is invalid

 Justification/ Feedback for the option d 

This is the most appropriate option as both (i) and (iii) are valid

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived Types

Sub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q7/LOD1/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 7/27

For code

int i = 7;int *iPtr = &i;

cout << iPtr;

cout << *iPtr;

cout « iPtr; will print ___

a. some integer value which is address of ib. some hexadecimal value which is address of i

c. 7

d. 70000

Correct Option b

 Justification/ Feedback for the option a

Since the address is in hexadecimal value so the integer value cant beprinted

 Justification/ Feedback for the option b

Some hexadecimal value will be printed which is the address of i

 Justification/ Feedback for the option c 

It is the value of i or *iptr and not iptr

 Justification/ Feedback for the option d 

It will print the hexadecimal value that is the address and not in the

decimal value

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming FundamentalsUnit no : V Unit name: Files, Pointers and Derived Types

Sub-unit/Topic No. Name: lecture 36-Pointer I

Tag:

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 8/27

Q8/LOD1/ MCQS/ Computer Science/B.Sc (hons)/First year/CSHT_101/UnitV/Richa Garg

Question

Code:

char ptr1[] = "Hello World";char *ptr2 = malloc( 5 );

ptr2 = ptr1;

What is wrong with the above code (assuming the call to malloc doesnot fail)?

a. There will be a memory overwrite.

 b. It will not compile.

c. There will be a segmentation fault.

d. Not enough space is allocated by the malloc.

Correct Option b

 Justification/ Feedback for the option a

The code will not compile so no such problem

 Justification/ Feedback for the option b

This will not compile because of the invalid conversion from void* to

char* in statement char *ptr2 = malloc( 5 );So, the correct statement is char *ptr2 = (char*) malloc( 5 );

 Justification/ Feedback for the option c 

There will be no segmentation fault in the code. Segmentation fault is

a run time error which is an access to the invalid memory segment.

 Justification/ Feedback for the option d 

The code will not compile so no such problem as there is large chunk

of memory available to be allocated

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subject: Data Structures  

Paper No.: CSHT-101 Paper Name: Programming Fundamentals

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 9/27

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q9/LOD1/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

Size of a pointer is ______ bytes

a. 1

b. 2c. 4

d. 8

Correct Option c

 Justification/ Feedback for the option a

The size of a pointer variable to the data of any type (char, integer,float, double, string) is 4 bytes. So this option is invalid

 Justification/ Feedback for the option b

The size of a pointer variable to the data of any type (char, integer,float, double, string) is 4 bytes. So this option is invalid

 Justification/ Feedback for the option c The size of a pointer variable to the data of any type (char , integer,float, double, string) is 4 bytes. So this option is valid

 Justification/ Feedback for the option d 

The size of a pointer variable to the data of any type (char , integer,

float, double, string) is 4 bytes. So this option is invalid

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming FundamentalsUnit no : V Unit name: Files, Pointers and Derived Types

Sub-unit/Topic No. Name: lecture 36-Pointer I

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 10/27

Tag:Q10/LOD1/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

If a variable int i = 7 is declared and is stored at memory location70001 then the value of &i is ____

a. 7

b. 70001c. 0

d. i

Correct Option b

 Justification/ Feedback for the option aThis is the value of i and not of &i (address of i).

 Justification/ Feedback for the option b

Value 7 is stored at 70001

 Justification/ Feedback for the option c 

It is not in relevance here.

 Justification/ Feedback for the option d 

Character “i” will not be printed but the address of i will be printed

 

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived Types

Sub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q11/LOD2/ MCQS/ Computer Science/B.Sc (hons)/First year

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 11/27

/CSHT_101/UnitV/Richa Garg

Question

What is the type of variable f2 and p2 in the following declaration?

#define floatptr float *#define intptr int *

floatptr f1,f2;intptr p1,p2; 

a. float, int pointer

 b. float pointer, int pointer

c. float, int

d. float pointer, int

Correct Option b

 Justification/ Feedback for the option a

F2 is a float pointer

 Justification/ Feedback for the option b

Both f2 and p2 are float pointer and integer pointer respectively. Sothis option is valid

 Justification/ Feedback for the option c 

Both f2 and p2 are float pointer and integer pointer respectively

 Justification/ Feedback for the option d 

P2 is a integer pointer

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 12/27

Tag:Q12/LOD2/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

QuestionWhich of the following statements can change the value of i to 7000

int i = 7;int *iPtr = &i;

cout << iPtr;cout << *iPtr;

a. *i = 7000;b. 7000 = i;

c. i = &7000;d. *iPtr = 7000;

Correct Option d

 Justification/ Feedback for the option a

i is a data of type int and not a pointer so this statement is wrong

 Justification/ Feedback for the option b

This is an invalid syntax

 Justification/ Feedback for the option c 

Also this is an invalid syntax

 Justification/ Feedback for the option d 

This is the correct way of changing the value of i to 7000 as i is

pointed to by iptr

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming FundamentalsUnit no : V Unit name: Files, Pointers and Derived Types

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 13/27

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 14/27

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subject: Data Structures  

Paper No.: CSHT-101 Paper Name: Programming FundamentalsUnit no : V Unit name: Files, Pointers and Derived Types

Sub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q14/LOD2/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

What will be output of following program?

#include<iostream.h>int main()

{

int a=320;char *ptr;

ptr=(char *)&a;cout<<int(*ptr)<<char(a);

}

a. 2@

 b. 320^

c. 64@d. Compiler error

Correct Option C

 Justification/ Feedback for the option a

Second character of the data is not printed but the first byte of thememory pointed to by ptr is printed

 Justification/ Feedback for the option b

Value stored in the first byte of a and not the complete value isprinted.

 Justification/ Feedback for the option c 

As int is two byte data byte while char is one byte data byte. Char

pointer can keep the address one byte at time.

Binary value of 320 is 00000001 01000000 (In 16 bit)Memory representation of int a=320 is:

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 15/27

So ptr is pointing only first 8 bit which color is green and its decimal

value is 64 and its character value is @

 Justification/ Feedback for the option d 

There is no syntax error in the code so there is no compilation error

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subject: Data Structures  

Paper No.: CSHT-101 Paper Name: Programming FundamentalsUnit no : V Unit name: Files, Pointers and Derived Types

Sub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q15/LOD2/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

How do you declare a pointer to a pointer to an integer

a. int **ptr

 b. int *ptr;

c. *int *ptr;

d. **int ptr

Correct Option a

 Justification/ Feedback for the option aThis the correct declaration of a pointer to a pointer to an integer

 Justification/ Feedback for the option b

This is just a pointer to an integer

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 16/27

 Justification/ Feedback for the option c 

This is invalid syntax for a pointer to a pointer to an integer

 Justification/ Feedback for the option d This is invalid syntax for a pointer to a pointer to an integer

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming FundamentalsUnit no : V Unit name: Files, Pointers and Derived Types

Sub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q16/LOD2/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

Which one of the following is a true statement about pointers?

a. Pointer arithmetic is permitted on pointers of any type.

 b. Standard C++ mandates a minimum of four levels of indirectionaccessible through a pointer.

c. A C++ program knows the types of its pointers and indirectly

referenced data items at runtime.

d. Pointers may be used to simulate call-by-reference.

Correct Option c

 Justification/ Feedback for the option a

Pointer arithmetic is permitted on the data of which the pointer isdefined.

 Justification/ Feedback for the option b

Standard C++ mandates a minimum of 12 not four levels of indirection accessible through a pointer.

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 17/27

 Justification/ Feedback for the option c 

A C++ program knows the types of its pointers and indirectly

referenced data items at runtime.

 Justification/ Feedback for the option d 

Pointers can be used to simulate call-by-reference also

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived Types

Sub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q17/LOD2/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

Which one of the following is a true statement about pointers?

a. They are always 32-bit values.

 b. For efficiency, pointer values are always stored in machineregisters.

c. A pointer to one type may not be cast to a pointer to any othertype.

d. A pointer declared should be of type void initially

Correct Option a

 Justification/ Feedback for the option a

A pointer is 4 bytes in size (4*8=32 bits)

 Justification/ Feedback for the option b

For efficiency, pointer values are always stored in memory.

 Justification/ Feedback for the option c 

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 18/27

A pointer to one type may be cast to a pointer to any other type if 

required.

 Justification/ Feedback for the option d 

A pointer of any type can be created. It is not necessarily be of type

void only.

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q18/LOD2/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

#include <iostream.h>

void main(){

int I=3,*j,**k;

 j=&I;k=&j;

cout<<*j<<**k<<*(*k);}

What is the output of the above program code? 

a. 444

b. 000c. 333

d. 433

Correct Option c

 Justification/ Feedback for the option a

Data is not incremented so 4 can not be printed

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 19/27

 Justification/ Feedback for the option b

Pointer is not null so zero is printed.

 Justification/ Feedback for the option c 

I=3 is printed then pointer to I i.e. I=3 is printed and then pointer topointer I is printed.

 Justification/ Feedback for the option d 

Here the data is not incremented so 4 can not be printed

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subjec: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q19/LOD2/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

Define Null pointer

a. a pointer which is pointing to nothingb. NULL pointer points the base address of segment.

c. both of thesed. Null pointers are same as void pointers.

Correct Option c

 Justification/ Feedback for the option a

It is fact about null pointer

 Justification/ Feedback for the option b

It is fact about null pointer

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 20/27

 Justification/ Feedback for the option c 

So this is the most appropriate option as both a and b are valid

statements.

 Justification/ Feedback for the option d 

Null pointers are not same as void pointers. A null pointer is a valuethat any pointer may take to represent that it is pointing to "nowhere",

while a void pointer is a special type of pointer that can point to

somewhere without a specific type. One refers to the value stored inthe pointer itself and the other to the type of data it points to.

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming FundamentalsUnit no : V Unit name: Files, Pointers and Derived Types

Sub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q20/LOD2/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

What will be output of following program? 

void main(){

int i=100;

cout<<"value of i : “<<i<<”addresss of i : “<<&i;i++;

cout<<" value of i : “<<i<<”addresss of i : “<<&i;

}

a. value of i : 100 addresss of i : Address of i value of i : 101

addresss of i : Addressof i b. value of i : 100 addresss of i : Address of i value of i : 100

addresss of i : Address of i

c. value of i : 101 addresss of i : Address of i value of i : 101addresss of i : Address of i

d. Compiler error

Correct Option a

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 21/27

 Justification/ Feedback for the option a

Within the scope of any variable, value of variable may change but itsaddress will never change in any modification of variable.

 Justification/ Feedback for the option b

The value of i will be incremented and it will not remain same.

 Justification/ Feedback for the option c 

In first statement value of i will be 100 and not 101 as it has not

incremented here.

 Justification/ Feedback for the option d 

The syntax of the code is correct and there will be compilation error

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q21/LOD3/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

How can I dynamically allocate a two-dimensional array?

a. int **array1 = (int **)malloc(nrows * sizeof(int *));

for(i = 0; i < nrows; i++)

array1[i] = (int *)malloc(ncolumns * sizeof(int));

b. int **array2 = (int **)malloc(nrows * sizeof(int *));array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));

for(i = 1; i < nrows; i++)

array2[i] = array2[0] + i * ncolumns;

c. int *array3 = (int *)malloc(nrows * ncolumns * sizeof(int));

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 22/27

d. All of the above

Correct Option d

 Justification/ Feedback for the option a

This is the one of the method to allocate a 2D array. In the above

code array1 is a pointer to pointer to type int. In this case it points tothe first element of an array of pointers to type int. If this approach is

chosen note that while one can use the array notation to access

individual elements of the array, e.g. array1[nrow][ncolumn] = 17;, itdoes not mean that the data in the "two dimensional array" is

contiguous in memory. However, it can be used that way. Forexample, it can be written that

Array1[i][j] = 176;

 Justification/ Feedback for the option bIt is another way to dynamically allocate memory to a 2D array.If a contiguous block of memory is required for the elements in the

array it can be done in this way.

The array's contents can be kept contiguous, at the cost of makinglater reallocation of individual rows more difficult, with a bit of explicit

pointer arithmetic:

In either case (i.e for array1 or array2), the elements of the dynamicarray can be accessed with normal-looking array subscripts: arrayx[i]

[j] (for 0 <= i < nrows and 0 <= j < ncolumns).

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 23/27

 Justification/ Feedback for the option c 

This will not create a layout of the 2D array. It will just allocate the

number of bytes required by a 2D array.

In this method one needs to perform subscript calculations manually,

accessing the i,jth element with the expressionarray3[i * ncolumns + j] and this array cannot necessarily be passed

to functions which expect multidimensional arrays. (A macro such as

#define Arrayaccess(a, i, j) ((a)[(i) * ncolumns + (j)])could hide the explicit calculation, but invoking it would require

parentheses and commas which wouldn't look exactly like conventionalC++ multidimensional array syntax, and the macro would need access

to at least one of the dimensions.

 Justification/ Feedback for the option d 

Since all the options can be used to dynamically allocate 2D array sothis is the most appropriate option.

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q22/LOD3/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

Which of the following is the example of NULL pointer:

a. char *ptr=’\0’;

 b. char *ptr=(char *)0;

c. int *ptr=(char *)0;d. All of these

Correct Option d

 Justification/ Feedback for the option a

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 24/27

It is an example of null pointer

 Justification/ Feedback for the option b

It is also an example of null pointer

 Justification/ Feedback for the option c 

It is also an example of null pointer

 Justification/ Feedback for the option d 

So this is the most appropriate option as all three are examples of null

pointers

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q23/LOD3/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

What will be the output of the following program?

int a = 7;

int b = 17;int *c = &b;

*c = 7;cout << "a=" << a << " b= " << b << endl;

a. a=7 b=17

 b. a=7 b=7

c. a=17 b=17

d. a=17 b=7

Correct Option b

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 25/27

 Justification/ Feedback for the option a

The address where c points to is modified to 7 and the name of thatlocation is b so it should be 7 and not 17.

 Justification/ Feedback for the option b

This is the correct option.

 Justification/ Feedback for the option c 

A = 7 so 7 should be printed and not 17. So it is invalid

 Justification/ Feedback for the option d 

A = 7 so 7 should be printed and not 17. So it is invalid

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q24/LOD3/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

What will be the output of the following program?

int a = 7;int b = 17;

int *c = &b;

a = *c;

*c = *c + 1;cout << "a=" << a << " b= " << b << endl;

a. a=7 b=18

 b. a=7 b=7

c. a=17 b=18

d. a=18 b=7

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 26/27

Correct Option c

 Justification/ Feedback for the option a

Since a stores the value pointed to by c i.e. 17 and not 7

 Justification/ Feedback for the option b

Since a stores the value pointed to by c i.e. 17 and not 7

 Justification/ Feedback for the option c 

This option correctly gives the output of the code

 Justification/ Feedback for the option d 

B is not 7 but increment of 17 i.e. 18. So it is also invalid

Reviewer’s Comment:

Author’s Name: Richa Garg College/Institution: Indraprastha College(w)

Subject: Data Structures  Paper No.: CSHT-101 Paper Name: Programming Fundamentals

Unit no : V Unit name: Files, Pointers and Derived TypesSub-unit/Topic No. Name: lecture 36-Pointer I

Tag:Q25/LOD3/ MCQS/ Computer Science/B.Sc (hons)/First year

/CSHT_101/UnitV/Richa Garg

Question

Which of the following is true (select more than one) about thefollowing program?

float a = 0.3; float b = 1.17;

float *fptr1 = &a; float *fptr2 = &b; 

fptr1 = fptr2;

 cout << "a=" << *fptr1;

8/8/2019 Lec 36

http://slidepdf.com/reader/full/lec-36 27/27

cout << " b=" << *fptr2 << endl;

 

cout << "a=" << a;cout << " b=" << b << endl;

 

cout << "fptr1=" << (unsigned long) fptr1 << endl;cout << "fptr2=" << (unsigned long) fptr2 << endl;

a. the statements printing a and b will print different valuesb. both the pointers point to different locations in memory

c. addresses printed by the last two lines are the samed. all the above

Correct Option d

 Justification/ Feedback for the option a

This is the correct statement about the code as the output will bea = 1.17 b = 1.17

This is so because both the pointers stores address of variable b

 Justification/ Feedback for the option b

This is also correct statement about the code. As the values stored in a

and b are not same.a = 0.3 b = 1.17

 Justification/ Feedback for the option c 

This is also correct statement about the code

Fptr1= address of b

Fptr2= address of bAs both the pointers points to memory location b

 Justification/ Feedback for the option d 

So this is the most appropriate option about the code

Reviewer’s Comment: