SE250:HTTB:Pointers:Questions

From Marks Wiki
Revision as of 05:09, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (6 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

<html>

<image src="http://www.rajithaonline.com/SE250/httb/pointers/final/questions_head.png" width="232" height="92" alt="Intro Header Logo" />

</html>

Previous Page Contents Next Page



Questions (Quiz)

1)

int* myInt; and int *myInt; will behave differently.

<html>

<button onclick='javascript:alert("Wrong")'>Yes</button> <button onclick='javascript:alert("Wrong")'>It depends how they are used</button> <button onclick='javascript:alert("Correct, both reserve a pointer sized block of memory that will reference an int sized block of memory.")'>It makes no difference</button>

</html>

2)

int num1 = 3; 
int* p1 = &num1;
int* p2 = &p1;

What value will p2 be pointing to?

<html>

<button onclick='javascript:alert("Wrong")'>3</button> <button onclick='javascript:alert("Wrong")'>num1</button> <button onclick='javascript:alert("Wrong")'>The memory address of num1</button> <button onclick='javascript:alert("Corret, the number that p2 is pointing to will be the memory address of p1, as we have used &, which means the memory address of")'>The memory address of p1</button>

</html>


3)

long num = 24;
long* p1 = num;
long** p2 = p1;

What will

printf("%d", sizeof(p2)) 

print? <html>

<button onclick='javascript:alert("Wrong")'>0</button> <button onclick='javascript:alert("Wrong")'>1</button> <button onclick='javascript:alert("Correct, all pointers in a 32 bit machine are of size 4 bytes regardless of what they point to. This is because they are holding a memory address and not any assigned value.")'>4</button> <button onclick='javascript:alert("Wrong")'>8</button>

</html>

4) What is the difference between case 1 and case 2? And what is the output in each case?

int x, y;
x = 1;
y = 5;
printf( "%f\n", (float)(x/y) );           /* case 1 */
printf( "%f\n", ((float)x)/((float)y) );  /* case 2 */

<html>

<button onclick='javascript:alert("In case 1, we are first doing an integer division and follow it by a cast to a float type. The integer division results in a value of 0 and hence the output will be 0.000000. As for case 2, we first cast both the integers to type float, then do a floating point division. Which results in 0.200000.")'>Answer</button>

</html>

5) What is the difference between:

char course[] = "SE250";

and

char *course = "SE250";

Answer: When you create a string using "char course[] = "SE250";", C takes care of how many bytes to allocate for the array (in this case 6 because we need space for the null terminator). This is technically an array of char's which can be modified using the syntax of a normal C array. In the second case however, we create a "pointer to char" and make this point to the string "SE250" which is stored in the read-only area of memory. One difference is that the first string can be modified and the second cannot. Another would be the location that these are stored. Also the array of chars cannot be made to point to another string, you can only modify that array and it's size cannot be changed.

6) For the following code, which of the answers is the WRONG way to access an element?

int array[5] = {1, 2, 3, 4, 5};

<html>

<button onclick='javascript:alert("Wrong")'>array[4]</button> <button onclick='javascript:alert("Wrong")'>4[array]</button> <button onclick='javascript:alert("Wrong")'>*array</button> <button onclick='javascript:alert("Corret, the index of an array is between 0 and one minus the size of the array inclusive. Also note that 4[array] is also a valid access because of the way pointer arithmetic works!")'>array[5]</button>

</html>


Previous Page Contents Next Page