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.
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)...
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.
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ).
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ). Given a sorted array of distinct integers and a single integer value, find all pairs of indexes in the list whose product is equal to the value. Return the index pairs on the same row of a two-dimensional array. Note, m will be the number of rows and total number of pairs found, while 2 will be the number of columns along every row...
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT