C++ midterm

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which input value causes the loop body to execute a 2nd time, thus outputting "In loop" again? string s = "Go";while ((s != "q") && (s != "Q")){cout << "In loop" << endl;cin >> s;} Either "q" or "Q" "Quit" "Q" only "q" only

"Quit"

How is the expression evaluated? ! x - 3 > 0 (!x) - (3 > 0) !((x - 3) > 0) (!(x - 3) > 0 ((!x) - 3) > 0

((!x)-3)>0

Which approach uses a logical operator to detect if x is in the range 1 to 99. (0 <= x) AND (x <= 100) 0 < x < 100 (0 < x) AND (x > 100) (0 < x) AND (x < 100)

(0 < x) AND (x < 100)

Which expression fails to compute the area of a triangle having base b and height h (area is one-half base time height)? None of These (1 / 2) * b * h (1.0 / 2.0 ) * b * h 0.5 * b * h

(1 / 2) * b * h

Given integers i, j , k, which XXX correctly passes three integer arguments for the following function call? addInts XXX; (i + j + k) (10, j, k +5) (10 15 20) (j, 6 + 7)

(10, j, k +5)

Given function Multiply(u, v) that returns u * v, and Add(u, v) that returns u + v, which expression corresponds to: Add(Multiply(x, Add(y, z)), w) (x * (y + z)) + w (x + (y + z) * w) (x * (y + z) * w) ((x * y) + z) + w

(x * (y + z)) + w

Which input value causes "Goodbye" to be output next? int x;cin >> x;while (x >= 0) {// Do somethingcin >> x;}cout << "Goodbye"; 1 0 No such value -1

-1

Given the function definition, what is returned for the call: DoCalc(2, 3) int DoCalc(int x, int y, int z = -1) {return x * y * z;} 0 Error: The call to DoCalc() has too few arguments -6 -1

-6

The numNegatives variable counts the number of negative values in the vector userVals. What should numNegatives be initialized to? vector<int> userVals(20);unsigned int i;numNegatives = XXX;for (i = 0; i < userValues.size(); ++i) {if (userValues.at(i) < 0) {numNegatives = numNegatives + 1;}} 0 No initialization needed userVals.at(0) 1

0

What is the output? int n;for (n = 0; n < 10; n = n + 3) {cout << n << " ";} 0 3 6 9 0 1 2 3 4 5 6 7 8 9 0 3 6 9 12 1 4 7 10

0 3 6 9

What is the range for the last branch below? if (numItems < 0) { ... } else if (numItems > 100) { ... } else { // last branch ... } 0 - 99 1 - 100 0 - 100 1 - 99

0-100

Which is the correct representation of 3.9e-5? 3.90000 0.00039 3900000 0.000039

0.000039

How many times will the loop iterate, if the input is 101 100 103 99? cin >> x;while (x > 100) {// Do somethingcin >> x;} 1 0 2 3

1

What is the final value of numItems? numItems = 0; bonusVal = 5; if (bonusVal > 10) // Need to update bonusVal numItems = bonusVal; numItems = numItems + 1;

1

What is the output? If num is declared as int and initialized to 10. while (num <= 15) {cout << num << " ";if (num == 12) {break;}++num;}cout << "Done"; 10 11 Done 10 11 12 Done 10 Done 10 11 12 13 14 15 Done

10 11 12 Done

What are the ending values in vector newValues? const int NUM_ELEMENTS = 4;vector<int> origValues(NUM_ELEMENTS);vector<int> newValues(NUM_ELEMENTS);origValues.at(0) = 10;origValues.at(1) = 20;origValues.at(2) = 30;origValues.at(3) = 40;newValues = origValues;newValues.at(2) = 21;newValues.at(3) = 22; 0, 0, 21, 22 Error: Invalid assignment of vectors 10, 20, 21, 22 10, 20, 30, 40

10, 20, 21, 22

What is the ending value of sum, if the input is 2 5 7 3? All variables are ints. cin >> x;sum = 0;for (i = 0; i < x; ++i) {cin >> currValue;sum += currValue;} 14 12 10 8

12

numAtoms is initially 7. What is numAtoms after: numAtoms += 5? 13 10 11 12

12

C was formalized by ANSI in _________. 1988 1987 1985 1988 1990

1988

How many times "CS2433 - Spring-2020" is printed? Infinite Error 1 0

1?

For the given program, how many cout statements will execute? void PrintShippingCharge(double itemWeight) {if ((itemWeight > 0.0) && (itemWeight <= 10.0)) {cout << (itemWeight * 0.75) << endl;}else if ((itemWeight > 10.0) && (itemWeight <= 15.0)) {cout << (itemWeight * 0.85) << endl;}else if ((itemWeight > 15.0) && (itemWeight <= 20.0)) {cout << (itemWeight * 0.95)<< endl;}}int main() {PrintShippingCharge(18);PrintShippingCharge(6);PrintShippingCharge(25);return 0;} 9 3 2 1

2

Given a vector/array with values 5, 10, 15, 20, 25, what are the fewest number of swaps needed to reverse the list? 5 2 3 4

2

What is the final value of numItems?bonusVal = 12;if (bonusVal < 12) {numItems = 100;}else {numItems = 200;} 12 100 200 None of These

200

Given the following code, how many times will the inner loop body execute? char letter1;char letter2; letter1 = 'a';while (letter1 <= 'f') {letter2 = 'c';while (letter2 <= 'f') {// Inner loop body++letter2;}++letter1;} 24 26 22 20

24

What is the output, if the input is 3 2 4 5? All variables are ints. cin >> num;for (i = 0; i < num; ++i) {cin >> curr;cout << curr;} 3245 245 324 24

245

What is the ending value of z. z = pow(2.0, pow(2.0, 3.0)); 512.00 256.0 128.00 8.0

256.0

Determine the final value of numBoxes. numBoxes = 0; numApples = 9; if (numApples < 10) { numBoxes = 2; } if (numApples < 20) { numBoxes = numBoxes + 1; } 2 9 3 0

3

What is the range of the 4th branch range: if (numSales < 10) { ... } else if (numSales < 20) { // 2nd branch ... } else if (numSales < 30) { // 3rd branch ... } else { // 4th branch ... } 20 + 10 + 30 + 31 +

30+

What is the ending value of the element at index 0? int numbers[10];numbers[0] = 35;numbers[1] = 37;numbers[1] = numbers[0] + 4; 35 0 39 37

35

What is the ending value of z. x = 9.0;z = pow(sqrt(x) + sqrt(x), 2.0); 162.0 81.0 36.0 9.0

36.0

What is the final value of bonusVal? bonusVal = 11;if (bonusVal < 12) {bonusVal = bonusVal + 2;bonusVal = 3 * bonusVal;}else {bonusVal = bonusVal + 10;} 21 38 11 39

39

What is the ending value of x? int x;userText = "mississippi";x = userText.find("i", 3); 10 7 4 1

4

Determine the result. 13.0 / 3.0 4 Positive Infinity Error 4.33333

4.333

What is the index of the last element?int numList[50]; 0 50 49 Unknown, because the array has not been initialized.

49

What is 540,000,000 as a floating-point literal using scientific notation with a single digit before and after the decimal point. 5.4e8 None of These 5.4e9 5.4e7

5.4e8

What is the output, if the input in x is 3? while (x > 0) {cout << 2 * x << " ";x--;}cout<<x; 6 4 2 2 6 4 2 0 Error: Infinite Loop 6 4 2

6 4 2 0

What is output? double MyFct(double a, double b) {return (a + b) / 2.0;} int main() {double x = 3.0;double y = 5.0;double z = 8.0;double t;t = MyFct(x, y);t = MyFct(t, z);cout << t << endl;return 0;} 8.0 4.0 16.0 6.0

6.0

Which header fine must be included to use sqrt() standard library function in a C++ prorgram? <stdlib> <stdio> <iostream> <cmath>

<cmath>

Two vectors, itemsNames and itemsPrices, are used to store a list of item names and their corresponding prices. Which is true? Using two vectors saves memory versus using one vector. The item names should appear in both vectors. Both vectors should be declared to have the same number of elements. Both vectors should be of the same data type.

Both vectors should be declared to have the same number of elements.

A programmer defines the following: int CalcRoute(int x = -1, int y, int z) ... What is true for the following call: CalcRoute(2, 3)? Error: If any default parameters are defined, all parameters must have default values Error: Any default parameters must always end the parameter list The call will yield x of 2, y of 3, and z of -1 The call will yield x of -1, y of 2, and z of 3

Error: Any default parameters must always end the parameter list

Which XXX calls GetUserScore() to get the user's name and score and store those values in the userName and userScore variables? void GetUserScore(string& userName, int& userScore) {cout << "Enter your name: " << endl;cin >> userName;cout << "Enter your score: " << endl;cin >> userScore;}int main() {string userName;int userScore;XXX;cout << userName << ", " << userScore << endl;return 0;} GetUserScore(userName, userScore); GetUserScore(string userName, int userScore); GetUserScore(&userName, &userScore); GetUserScore(string& userName, int& userScore);

GetUserScore(userName, userScore);

What does this code do? If x less than y Put x to outputElse Put y to output Outputs the lesser of the two integers; if they are equal, outputs that integer Outputs the lesser of the two integers; if they are equal, outputs nothing Outputs the greater of the two integers; if they are equal, outputs that integer Outputs the greater of the two integers; if they are equal, outputs nothing

Outputs the lesser of the two integers; if they are equal, outputs that integer

Which XXX causes the program to output the message "Hello!"? void PrintMessage() {cout << "Hello!";}int main() {XXX;return 0;} cout << PrintMessage() PrintMessage("Hello!") void PrintMessage() PrintMessage()

PrintMessage()

The following program generates an error. Why? void PrintSum(int num1, int num2) {cout << num1 + num2;}int main() {int y;y = PrintSum(4, 5);return 0;} The void function is missing a "return;" statement. PrintSum() has void return type, so cannot be assigned to a variable main() has a return statement that returns the value 0 The values 4 and 5 cannot be passed directly to PrintSum()

PrintSum() has void return type, so cannot be assigned to a variable

What will be the output of following code: Salary is 40000 Salary is 40000 Salary is wage * 40 * 50 Salary is wage * 40 * 50

Salary is 40000

Which function call passes a string myStr to the given function? void StrMod(char modString[]) {int i; // Loop indexfor (i = 0; i < strlen(modString); ++i) {if (modString[i] == '.') {modString[i] = '!';}}} StrMod(char myStr[]); StrMod(myStr); StrMod(myStr[]); StrMod(char myStr[strlen(myStr)]);

StrMod(myStr);

Given the following function definition, which function call prints? void Test(char a, char b = 'y', int num = 0) {cout << a << ", " << b << ", " << num << endl;} Test('x'); Test ('x' 'y'); Test('x', 'y', 'z'); Test(x, y);

Test('x');

Given only int variables a and b, which is a valid expression? a + ab 2 x 3 a + -2 4a + 3

a+-2

A character variable's value is stored in memory as _____ . an integer value a string a floating-point value a graphical symbol

an integer value

A compiler generates the following error messages:Line 7: Missing semicolonLine 9: numItems not definedLine 10: Expected '(' If the programmer corrects an error on line 7, the programmer should _____. Compile again Check earlier lines too Run the Program Check line 7 again

compile again

Which item converts a high-level language program to low-level machine instructions? Memory Machine instruction Compiler Assembly language

compiler

Given str = "Cat", what is the result of this statement? str.at(1) = "ab"; str is "Cab" Error: Assigning a character with a string is not allowed str is "abt" str is "Catab"

error

What is the ending value of userNum? vector<int> userValues;int userNum;userNum = 0;userValues.push_back(45);userValues.push_back(55);userNum = userValues.pop_back(); 0 45 Error: pop_back does not return a value 55

error

What is the ending value of y? pow(u, v) returns u raised to the power of v. x = 2.0;y = 3.0;y = pow(pow(x, y)) + 1.0; Error: The compiler complains about calling a function within another function 8.0 65.0 9.0

error

C++ Language generally uses Top-Down Programming Approach.

false

Features Like Polymorphism, Inheritance, Data hiding, Templates etc. are available in C Language.

false

Following code will execute without syntax error. cout < "Everyone wins.";

false

Following code will execute without syntax error. Assume variable numDogs has been declared. cout << numDogs.

false

The following assignment is valid. x + 1 = 3;

false

Given x = 1, y = 2, and z = 3, how is the expression evaluated? In the choices, items in parentheses are evaluated first. (x == 5) || (y == 2) && (z == 5) false OR (true AND false) --> false OR false --> false (false OR true) AND false --> true AND false --> false (false OR true) AND false --> true AND false --> true false OR (true AND false) --> false OR true --> true

false OR (true AND false) --> false OR false --> false

Which YYY outputs the string in reverse? Ex: If the input is "Oh my", the output is "ym hO". int i;string str;getline(cin, str);for (YYY) {cout << str.at(i);} i = str.length(); i < 0; --i i = str.length() - 1; i >= 0; --i) i = str.length() + 1; i > 0; --i) i = str.length(); i > 0; --i)

i = str.length() - 1; i >= 0; --i)

Which XXX complete the program to calculate the volume of a cone? XXXint main () {double coneVol;coneVol = ConeVolume(2, 4);cout << coneVol;return 0;}double ConeVolume (double r, double h) {return (0.33) * 3.14 * ComputeSquare(r) * h;}double ComputeSquare (double r) {return r * r;} double ConeVolume();double ComputeSquare(); (nothing) double ConeVolume (double r, double h);double ComputeSquare (double r); double ConeVolume, ComputeSquare;

idk

Which character array declaration is appropriate? char name[5] = "Alex"; char name[5] = "Alexandria"; char name[5] = "Alexa"; char name[5] = "Alexander";

idk

Which is true regarding header files? Header files serve as a brief summary of all functions available Header files should contain function definitions for functions declared in another file Header files must end with .cpp Header files must end with .h

idk

Which is true regarding passing C strings to functions? A C string cannot be passed to a function A C string is automatically passed by pointer Passing a C string to a function creates a copy of that string within the function Functions that modify a string must have a char return type

idk

Which if branch executes when an account lacks funds and has not been used recently? hasFunds and recentlyUsed are booleans and have their intuitive meanings. if (!hasFunds && !recentlyUsed) if (hasFunds && !recentlyUsed) if (hasFunds && recentlyUsed) if (!hasFunds && recentlyUsed)

if (!hasFunds && !recentlyUsed)

Which XXX will output only values up to 3? i is an int. for (i = 1; i < 10; i++) {cout << i << " ";XXX {break;}} if (i != 4) if (i == 3) if (i != 3) if (i == 4)

if (i == 3)

What will the following code output? x = 10; while (x != 3) {cout << x << " ";x = x / 2;} 10 10 5 Infinite Loop 10 5 2

infinite loop

Which input for char c causes "Done" to be output next? Any value other than 'y' 'y' only 'n' only No such value (infinite loop)

infinite loop?

What is <iostream> input output stream in out sequence in out stream input output sequence

input output stream

Indicate valid comment or not?// Print "Hello" Then print "Goodbye" And finally return. //

invalid

What does the following get into str2?cin >> str0;cin >> str1;getline(cin, tmpStr);getline(cin, str2); Given the following input: Every oneis great.That's right. is great. That's right. (blank) one

is great.

Which evaluates to true for the string str = "beta123"? isalpha(str[4]) isspace(str[2]) isdigit(str[0]) islower(str[1])

islower(str[1])

Which best describes the meaning of a 1 (true) being output? Assume v is a large vector of ints. int i;bool s;for (i = 0; i < v.size(); ++i) {if (v.at(i) < 0) {s = true;}else {s = false;}}cout << s; some value other than the last is negative all values are negative last value is negative first value is negative

last value is neg

Function CalcSum() was copied and modified to form the new function CalcProduct(). Which line of the new function contains an error? int CalcSum(int a, int b) {int s;s = a + b;return s;}int CalcProduct(int a, int b) {int p; // Line 1p = a * b; // Line 2return s; // Line 3} Line 2 None of the lines contains an error Line 3 Line 1

line 3

Which assigns the vector's last element with 99? vector<int> myVector(15); myVector.at(0) = 99; myVector.at(15) = 99; myVector.at(16) = 99; myVector.at(14) = 99;

myVector.at(14) = 99;

An identifier can _____ . start with an underscore contain a period be a reserved word contain spaces

not "contain a period"

In what year was the first C book published? 1980 1978 1976 1982

not 1976

Given two same-sized integer vectors prevValues and newValues. Which assigns eq with 1 if the vectors are equal, and 0 otherwise. Choices are in the form XXX / YYY / ZZZ. if (XXX) {YYY;}else {ZZZ;} prevValues == newValues / eq = 0 / eq = 1 prevValues = newValues / eq = 0 / eq = 1 prevValues = newValues / eq = 1 / eq = 0 prevValues == newValues / eq = 1 / eq = 0

prevValues == newValues / eq = 1 / eq = 0

Following is NOT a type of memory. Processor Cache RAM ROM

processor

Which expression's range is restricted to 0 to 7? rand() % 6 rand() % 8 rand() % 8 -1 rand() % 7

rand() % 8

Which XXX calculates the area using the CalcSquare() function? The formula to calculate the area of a circle is pi * . double CalcSquare(double x) {return x*x;}double CalcArea(double r) {const double PI_VAL = 3.14159265;XXX;}int main() {cout << CalcArea(5.0);return 0;} PI_VAL * CalcSquare(r); return PI_VAL * CalcSquare(r) * CalcSquare(r); return PI_VAL * CalcSquare(r); PI_VAL * r * r;

return PI_VAL * CalcSquare(r);

The s in srand() most likely stands for _____ . sequence seed sample serial

seed

The header file <stdio> stands for: sequential input output standard input output sequential in out standard in out

standard input output

Which does NOT yield 3.75? 15 / static_cast<double>(4) static_cast<double>(15) / static_cast<double>(4) static_cast<double>(15) / 4 static_cast<double>(15 / 4)

static_cast<double>(15 / 4)

Which XXX prints the message only if str1 and str2 are equal? if (XXX) {// Print "strings are equal"} strcpy(str1, str2) == 0 str1 == str2 strcat(str1, str2) == 0 strcmp(str1, str2) == 0

strcmp(str1, str2) == 0

Given two vectors, studentNames that maintains a list of students, and studentScores that has a list of the scores for each student, which XXX and YYY prints out only the student names whose score is above 80? Choices are in the form XXX/ YYY. vector<string> studentNames(NUM_STUDENTS);vector<int> studentScores(NUM_STUDENTS);unsigned int i; for (i = 0; i < studentScores.size(); ++i) {if (XXX > 80) {cout << YYY << " ";}} studentNames.at(i) / studentScores.at(i) studentScores.at(i) / studentScores.at(i) studentNames.at(i) / studentNames.at(i) studentScores.at(i) / studentNames.at(i)

studentScores.at(i) / studentNames.at(i)

For the given pseudocode, which XXX and YYY will output the sum of the input integers (stopping when -1 is input)? Choices are in the form XXX / YYY. val = Get next inputXXX While val is not -1YYYval = Get next input Put sum to output sum = 0 / sum = sum + val sum = val / sum = val sum = val / sum = sum + val sum = 0 / sum = val

sum = 0 / sum = sum + val

For the string "on time", what character is at index 3? i t m (Space)

t

C can handle low-level activities & high-level programming applications.

true

C is considered as Function Driven Programming Language.

true

Given numPeople = 10, userKey = 'q'. Indicate whether the expression evaluate to true or false. !(userKey == 'a')

true

In C++ Language the programs are generally divided into Classes & Objects.

true

The function fscanf() is used to read data values from a file.

true

Which assigns the last vector element with 20? vector<int> userNum(N_SIZE); userNum.at(20); userNum.at(N_SIZE - 1) = 20; userNum.at() = 20; userNum.at(N_SIZE) = 20;

userNum.at(N_SIZE - 1) = 20;

Indicate valid comment or not?/* numKids = 2; // Typical number numCars = 5; */ Valid Invalid

valid

In an instruction like: z = x + y, the symbols x, y, and z are examples of _____. output instructions variables input

variables

Select the expression whose parentheses match the evaluation order of the original expression. y + 2 * z y + (2 * z) (y + 2) * z

y + (2 * z)

A program should compute two times x. Which statement has a logic error? y = 2 * x; y = x + x; y = x * x; y = x * 2;

y = x * x;

Program execution begins at main( ) and executes statements surrounded by which symbols? [ ] ( ) " " { }

{}


संबंधित स्टडी सेट्स

Applications and Underwriting chapter 10

View Set

RS MGMT CH 8 Recruitment, Selection, and Retention in Health Information Management

View Set

Integrated Physical Science chapter 9 & 10

View Set

PSY 314 - Chapter 10: Abraham Maslow

View Set

Lacharity Chapters 1-21 (3rd Edition), Lacharity Chapter 21: Psychiatric & Mental Health, Lacharity Chapter 19: Pediatric Problems, Lacharity Chapter 20: Emergencies & Disasters, Lacharity Chapter 18: Problems in Pregnancy and Childbearing, Lacharity...

View Set

chapter 2 - marketing management

View Set