In: Computer Science
A.) myNums is an array of 50 elements of type int and k is an int variable. For which one we get the index of out of bounds?
a. |
for (k = 0; k <= 49; k++) cout << myNums[k] << " "; |
b. |
for (k = 1; k < 50; k++) cout << myNums[k] << " "; |
c. |
for (k = 0; k <= 50; k++) cout << myNums[k] << " "; |
d. |
for (k = 0; k <= 48; k++) cout << myNums[k] << " "; |
B.)
if we have int myNums[4] = {10, 20, 30, 40};. Which one is equivalent to this statement?
a. |
int myNums[] = {10, 20, 30, 40}; |
b. |
int myNums[] = {10 20 30 40}; |
c. |
int myNums[4] = [10, 20, 30, 40]; |
d. |
int myNums[] = (10, 20, 30, 40); |
C.)
The C-string cityName[30] can contain ________.
A) thirty characters
B) thirty one characters
C) twenty nine characters and the null terminator
D) thirty characters and the null terminator
E) None of the above
D.)
What is the output of the following C++ code?
int nums[5] = {0, 5, 10, 15, 20};
int j;
for (j = 1; j <= 4; j++)
cout << nums[j] << " ";
cout << endl;
a. |
0 5 10 15 |
c. |
5 10 15 20 |
b. |
10 15 20 0 |
d. |
Code results in index out-of-bounds |
D.)
What is the output of the following C++ code?
int nums[5] = {2, 4, 6, 8, 10};
int j;
for (j = 3; j >= 0; j--)
cout << nums[j] << " ";
cout << endl;
a. |
4 6 8 10 |
c. |
6 4 2 0 |
b. |
3 2 1 0 |
d. |
8 6 4 2 |
A)
The correct option is
c. for (k = 0; k <= 50; k++)
cout << myNums[k] << " ";
Explanation :
An array of 50 elements can store the values from the
starting
index of 0 to the index,49 which is one less than the size of the
array,50.
In the given options, c statement for loop iterates from k =0 to
k<=50
Since k=50, the code throws index out of bounds exception for
myNums array.
Therefore, the correct option is c.
--------------------------------------------------------------------------------------------------
B)
The correct option is a. int myNums[] = {10, 20, 30, 40};
Explanation :
The given statement, int myNums[4] = {10, 20, 30, 40}
The equivalent statement is int myNums[] = {10, 20, 30, 40};
Option b) is invalid since it contains no comma as a separator
between elements.
The option c) is invalid since array values must be enclosed in
flower brackets "{" and "}"
The option d) is invalid since array values must be enclosed in
flower brackets "{" and "}"
--------------------------------------------------------------------------------------------------
C)The correct option is D) thirty characters and the null terminator
Explanation :
The given C-string cityName[30].
The cityName can hold 30 characters and NULL character('\0') as
terminator
at the end of the string automatically.
--------------------------------------------------------------------------------------------------
D)
The correct option is c.5 10 15 20
Explanation :
Given c++ code statemenets
int nums[5] = {0, 5, 10, 15, 20};
int j;
for (j = 1; j <= 4; j++)
cout << nums[j] << " ";
cout << endl;
The nums array contains 5 elements starting from index,0 to
index =4.
The loop starting from j =1 to 4.Threrefore, the loop
statements
prints 5,10,15 and 20 values .
-------------------------------------------------------------------------------------
E) The correct option is d. 8 6 4 2
Explanation :
Given C++ code statement
int nums[5] = {2, 4, 6, 8, 10};
int j;
for (j = 3; j >= 0; j--)
cout << nums[j] << " ";
cout << endl;
In the above statements, nums array stores 5 integer
values
from index,0 to 4.
The for loop starts from index j =3 , nums[3] =8
Then , j-- becomes 2 . nums[2]= 6
Then , j-- becomes 1 . nums[1]= 4
Then , j-- becomes 0 . nums[2]= 2
Then , j-- becomes -1 .The for loop condition , j>=0 will
fail.The control
comes out of the for loop body.
Note: The last question numbered as D as may typing error. Let it as E