Question

In: Computer Science

A.) myNums is an array of 50 elements of type int and k is an int...

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

Solutions

Expert Solution

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


Related Solutions

// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
This is C++ programming Given an int variable k, an int array incompletes that has been...
This is C++ programming Given an int variable k, an int array incompletes that has been declared and initialized, an int variable nIncompletes that contains the number of elements in the array, an int variable studentID that has been initialized, and an int variable numberOfIncompletes, Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes. I tried this code...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
The statement: "int ar[7] = {0};" sets all of the array elements of ar to 0...
The statement: "int ar[7] = {0};" sets all of the array elements of ar to 0 (zero) True False ------------------------------------------- #define SIZE 3 - declares a constant value named SIZE, equal to 3, that is available throughout your program and is immutable. True False ---------------------------------------- Select all of the following that apply to an array: 1. Contiguous storage is storage without any gaps. 2. Arrays make programming complex problems more difficult. 3. An arrays elements are stored contiguously in memory....
Ceate a two dimensional array of int type to hold scores (on a scale of 0...
Ceate a two dimensional array of int type to hold scores (on a scale of 0 to 100) for 10 students (row) for 3 courses (columns) and initialize the array with your preferred data. All the students get 10 bonus points for course #2 (index 1) . Use for a loop to add the bonus points. c programming language
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
import java.util.*; class A { int i, j, k; public A(int i, int j, int k)...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k) { this.i=i; this.j=j; this.k=k; } public String toString() { return "A("+i+","+j+","+k+")"; } } class Main { public static void main(String[] args) { ArrayList<A> aL=new ArrayList<A>(); Random rand= new Random(1000); //1000 is a seed value for (int p=0; p<10; p++) { int i = rand.nextInt(100); int j = rand.nextInt(200); int k = rand.nextInt(300); aL.add(new A(i, j, k)); } System.out.println("----- Original arraylist------"); for (A a: aL)...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1,...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1, 2, 3, 4}; get_value(data, 2) = 100; } Write ????? that returns the value in the array at index: Question Blank type your answer...
You will need to use only two arrays:  One array of type int to store...
You will need to use only two arrays:  One array of type int to store all the ID's  One two‐dimensional array of type double to store all the scores for all quizzes Note: The two dimensional array can be represented also with the rows being the students and the columns being the quizzes. How to proceed: 1. Declare the number of quizzes as a constant, outside the main method. (Recall that identifiers for constants are all in CAPITAL_LETTERS.)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT