Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens? Answer The compiler issues an error message. The compiler issues a warning message. An exception will be thrown when that statement is executed. Another variable or array will be unexpectedly modified.
which one?
Suppose v is an array with 100 int elements. If 99 is assigned to v[100], what happens? Answer The compiler issues an error message. The compiler issues a warning message. An exception will be thrown when that statement is executed. Another variable or array will be unexpectedly modified. The value of 99 will be assigned to the cell v[100].
Depends. C will allow you to do that but the result is undefined. Bottom of line is, the memory location assigned to v[100] does not belong to v. It's sequentially after v[99] the last element. You have no insurance whatsoever that a variable assigned to v[100] will stay there. Also, it will overwrite anything that is there, or at least try to. So it will modify something unexpectedly. That being said, the compiler will either issue an error (more generally) or issue a warning (less frequent, but happens). Bottom line is, all the outcomes can happen. They are compiler and language dependent.
The result is not undefined, I am sorry. What I try to say is that you have no guarantee that a variable assigned to v[100] will continue there.
In C, if you assign 99 to the v[100] location you will overwrite memory that does not belong to the array. If you attempt to read that location you will get miscellaneous garbage that be there which will cause unexpected results in your program. If you are using Java you will get an out of bounds exception when you run it.
As bmp pointed out, different language handle it differently. It would be best to provide the language you're programming in when asking a question like this.
Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens? in JAVA Answer The compiler issues an error message. The compiler issues a warning message. An exception will be thrown when that statement is executed. Another variable or array will be unexpectedly modified.
An exception will be thrown when that statement is executed.
java.lang.ArrayIndexOutOfBoundsException: 100
thanks Given that an array named a whose elements are of type int has been declared, assign the value -1 to the last element in a . Answer a[length-1] = -1; a[ a.length - 1] = -1; a[a.length() -1] = -1; a[a.length] = -1;
Well if you declared an array of 10, to access the last element you would need to write to a[9]. So you would need to grab the length with a.length - 1 since a.length would return the overall length of 10. So the second option listed.
An array of 99 integers has been created. What is the largest integer that can be used as an index to the array? Answer 100 99 101 98
Well think about the previous examples... In each case the number of values always ran from 0 - (N-1). So in this case if N = 99, what is the range of your index?
0 - 98 because its says 99 integer i started questioning if 0 was an integer
Well the question specifically asks "the largest" so all you are concerned with is the upper end of your index value.
ok thanks eSpex
Any time. :)
largest integer , largest element , largest index would mean the same right?
Not exactly. When they talk about the 'element' they are usually talking about the place holder in the array, as they are when dealing with an index. Typically when an integer is mentioned they are specifically asking for a number, which in this case is associated with an element or index.
so for this question Consider the declaration: int v[] = new int[1]; What is the index of the last element of this array? Answer 0 1 2 3 0 will be the answer right? since the first and last index is 0 in case of 1 element
Exactly. :)
I think you got this topic licked.
Consider the declaration: double tide[] = {12.2, -7.3, 14.2, 11.3, 8}; How do you access the last element of this array? Answer double 8 8 v[8] tide(4) v[7] tide[4] double[4] tide[5] answer would be tide[4]?
Yep.
index 0-4 thanks
Not a problem. :)
Join our real-time social learning platform and learn together with your friends!