week5 quiz solutions

.docx

School

Arizona State University *

*We aren’t endorsed by this school

Course

100

Subject

Computer Science

Date

Apr 3, 2024

Type

docx

Pages

26

Uploaded by LieutenantHornetMaster829

Report
QUIZ 6 Question 1 2 / 2 pts What is the value of x after the following statements execute? int x = 5; int y = 30; do{ x = x * 2; }while (x < y); 5 20 10 40 Question 2 2 / 2 pts Suppose that the input is 5 3 4 –6 8. What is the output of the following C++ code? int sum = 0; int num; int j; for (j = 1; j <= 5; j++) {     cin >> num;     if (num < 0)         continue;     sum = sum + num; } cout << sum<<endl; 20 24 22
26 Question 3 2 / 2 pts Suppose sum, num, and j are int variables, and the inputs are 4 7 12 9 -1 respectively. What is the output of the following code? cin >> sum; cin >> num; for (j = 1; j <= 3; j++) {     cin >> num;     sum = sum + num; } cout << sum << endl; 24 41 25 42 Incorrect Question 4 0 / 2 pts What is the index number of the last component in the array numList above? int numList[50]; for (int i = 0; i < 50; i++)     numList[i] = 2 * i; numList[10] = -20; numList[30] = 8; 49 0 50 30
  The array is size 50. Index of the array starts from 0. So the last index is 49. Question 5 2 / 2 pts What is the output of the following C++ code? int x = 7; bool found = false; do { cout << x << " "; if (x <= 2) found = true; else x = x – 5; } while (x > 0 && !found); cout << endl; 7 7 2 2 7 None of these Question 2 2 / 2 pts Which of the following loops is guaranteed to execute at least once? counter-controlled while loop do...while loop for loop sentinel-controlled while loop
Question 4 2 / 2 pts Complete the following C++ program that prints the content of a string in the reverse order #include<iostream> #include<cstring> using namespace  std; int main() {    char str[] = “CSE 100 Fall 2014”;            /* To Do: Select a for loop that prints the content of the string in the reverse order     Hint: you can use strlen( const char* ) function to get the length of the string     Ex. char str[] = "Cat"; int length; length = strlen(str); length gets 3.   */ return 0; } int i, length; length = strlen(str); for(i=0;i<length;i++){    cout<<str[i]; } cout<<endl; int i, length; length = strlen(str); for(i=length-1;i>=0;i--){    cout<<str[i]; } cout<<endl; int i, length; length = strlen(str); for(i=length-1;i>=0;i++){    cout<<str[i]; } cout<<endl; int i, length;
length = strlen(str); for(i=length;i>0;i--){    cout<<str[i]; } cout<<endl; QUIZ 7 Incorrect Question 2 0 / 2 pts What is the value of numList length (size)? int numList[50]; for (int i = 0; i < 50; i++)     numList[i] = 2 * i; numList[10] = -20; numList[30] = 8; 0 30 49 50 The value of numList length (size) is 50 because the code define int numList[50]; 50 is the size of this array. Question 3 2 / 2 pts Consider the following statements: int hits[] = {5, 7, 9, 11, 13, 15}; What is the number of elements in the array hits. 0 6 5
15 Question 4 2 / 2 pts The size declarator indicates the number of elements, or values, an array can hold. True False Question 5 2 / 2 pts Each element of an array is accessed and indexed by a number known as an subscript. True False Question 2 2 / 2 pts Consider the following statements: int hits[] = {5, 7, 9, 11, 13, 15}; cout << hits[3]; What is the output? 5 9 11 13 Question 4 2 / 2 pts The size declarator must be an integer with a value grater than 1. True
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help