CSCI 261 Test 1
an include directive.
#include <iostream> is
extraction
>> is known as the stream ________ operator.
compiler
A ________ is the program that translates a program written in C++ into machine language.
false
A boolean expression may evaluate to more than 2 values.
block
A compound statement that contains variable declarations is called a ________.
personal computer
A computer that is normally used by only one person at a time is called a
Bit
A digit that can hold a zero or a one is known as a ________.
True
A function may return a boolean value.
variable
A location in memory used for storing data and given a name in a computer program is called a _________ because the data in the location can be changed.
do-while
A loop that always executes the loop body at least once is known as a ________ loop.
where a variable is stored.
A memory address is
bug
A mistake in a computer program is called a ________.
program
A set of instructions that the computer will follow is called a ________.
False
All nested if-else statements can be converted into switch statements.
False
An algorithm is always written in C++.
3.452211903e09
Another way to write the value 3452211903 is
cin >> choice; if(age < 22) { switch(choice) { case 'S': cout << "vegetable juice"; break; case 'T': cout << "cranberry juice"; break; case 'B': cout << "soda"; break; default: cout <<"invalid menu selection"; } } else { switch(choice) { case 'S': cout << "cabernet"; break; case 'T': cout << "chardonnay"; break; case 'B': cout << "IPA"; break; default: cout <<"invalid menu selection"; } }
Assume that an int variable age has been declared and already given a value and assume that a char variable choice has been declared as well. Assume further that the user has just been presented with the following menu: S: hangar steak, red potatoes, asparagus T: whole trout, long rice, brussel sprouts B: cheddar cheeseburger, steak fries, cole slaw (Yes, this menu really IS a menu!) Write some code that reads a single character (S or T or B) into choice. Then the code prints out a recommended accompanying drink as follows: If the value of age is 21 or lower, the recommendation is "vegetable juice" for steak, "cranberry juice" for trout, and "soda" for the burger. Otherwise, the recommendations are "cabernet", "chardonnay", and "IPA" for steak, trout, and burger respectively. Regardless of the value of age, your code should print "invalid menu selection" if the character read into choice was not S or T or B.
for (i=1; i<=n; i++) { for (j=1; j<=i; j++) cout << "*"; cout << "\n"; }
Assume that the int variables i, j and n have been declared, and n has been initialized. Write code that causes a "triangle" of asterisks to be output to the screen, i.e., n lines should be printed out, the first consisting of a single asterisk, the second consisting of two asterisks, the third consisting of three, etc. The last line should consist of n asterisks. Thus, for example, if n has value 3, the output of your code should be (scroll down if necessary): * ** ***
declared
Before a variable is used it must be
if ((modelYear >= 1999 && modelYear <= 2002 && modelName =="Extravagant") || (modelYear >= 2004 && modelYear <= 2007 && modelName == "Guzzler")) cout << "RECALL";
Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002 as well all vehicles in its Guzzler line from model years 2004-2007. Given an int variable modelYear and a string modelName write a statement that prints the message "RECALL" to standard output if the values of modelYear and modelName match the recall details.
int firstNumber,secondNumber = -1, duplicates = 0; do { cin >> firstNumber; if ( secondNumber == -1) { secondNumber = firstNumber; }else { if ( secondNumber == firstNumber ) duplicates++; else secondNumber = firstNumber; } } while(firstNumber > 0 ); cout << duplicates;
Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value is considered a CONSECUTIVE DUPLICATE. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive duplicate because it was preceded by a 7. Write some code that uses a loop to read such a sequence of non-negative integers, terminated by a negative number. When the code exits the loop it should print the number of consecutive duplicates encountered. In the above case, that value would be 3.
true
Determine the value of these Boolean expressions? Assume count = 0 and limit = 10. (count == 0) && (limit < 20)
iteration
Executing one or more statements one or more times is known as
for (char outterChar='a';outterChar <=last; outterChar++) { for (char innerChar='a'; innerChar <=last; innerChar++) { cout << outterChar << innerChar << "\n"; } }
Given an char variable last that has been initialized to a lowercase letter, write a loop that displays all possible combinations of two letters in the range 'a' through last. The combinations should be displayed in ascending alphabetical order: For example, if last was initialized to 'c' the output should be aa ab ac ba bb bc ca cb cc
for (k=0; k < 97; k++) cout <<"*";
Given an int variable k that has already been declared, use a for loop to print a single line consisting of 97 asterisks. Use no variables other than k.
drivingAge = drivingAge = 17;
Given an integer variable drivingAge that has already been declared, write a statement that assigns the value 17 to drivingAge.
k = 1; total = 0; do { total = total + (k*k); k++; } while (k <= 50);
Given int variables k and total that have already been declared, use a do...while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
total = 0; k = 1; while (k<=50) { total =total + k * k; k++; }
Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
2.0
Given the following code fragment and the input value of 2.0, what output is generated? float tax; float total;
10
Given the following code fragment, what is the final value of y?
if( x = 1)
Given the following code fragment, which of the following expressions is always TRUE? int x; cin >> x; if( x < 3) if( x==1) if( (x / 3) >1 ) if( x = 1)
5
Given the following code, what is the final value of i?
3
Given the following code, what is the final value of j?
6
Given the following enumerated data type definition, what is the value of SAT? enum myType{SUN,MON,TUE,WED,THUR,FRI,SAT,NumDays};
int temp; temp = firstPlaceWinner; firstPlaceWinner = secondPlaceWinner; secondPlaceWinner = temp;
Given two int variables, firstPlaceWinner and secondPlaceWinner, write some code that swaps their values. Declare any additional variables as necessary, but do not redeclare firstPlaceWinner and secondPlaceWinner.
14
How many times is "Hi" printed to the screen
false
If x has the value of 3, y has the value of -2, and w is 10, is the following condition true or false? if( x < 2 && w < y)
switch(age) { case 0: case 1: cout << "ineligible"; break; case 2: cout << "toddler"; break; case 3: case 4: case 5: cout << "early childhood"; break; case 6: case 7: cout << "young reader"; break; case 8: case 9: case 10: cout << "elementary"; break; case 11: case 12: cout << "middle"; break; case 13: cout << "impossible"; break; case 14: case 15: case 16: cout << "high school"; break; case 17: case 18: cout << "scholar"; break; default: cout << "ineligible"; }
In the Happy Valley School System, children are classified by age as follows: less than 2, ineligible 2, toddler 3-5, early childhood 6-7, young reader 8-10, elementary 11 and 12, middle 13, impossible 14-16, high school 17-18, scholar greater than 18, ineligible Given an int variable age, write a switch statement that prints out the appropriate label from the above list based on age.
cin >> callsReceived; cin >> operatorsOnCall; if ( operatorsOnCall > 0) { cout << callsReceived / operatorsOnCall; } else {cout << "INVALID";}
NOTE: in mathematics, division by zero is undefined. So, in C++, division by zero is always an error. Given a int variable named callsReceived and another int variable named operatorsOnCall write the necessary code to read values into callsReceived and operatorsOnCall and print out the number of calls received per operator (integer division with truncation will do). HOWEVER: if any value read in is not valid input, just print the message "INVALID".
if ( nbooksPurchased > 4){ if (isPremiumCustomer){ freeBooks = 1; if (nbooksPurchased > 7){ if (isPremiumCustomer) freeBooks = 2;} }else{ freeBooks = 0; if ( nbooksPurchased > 6){ freeBooks = 1; } if ( nbooksPurchased > 11){ freeBooks = 2; } } }else {freeBooks = 0;}
Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with every purchase of 8 or more books. It offers regular customers 1 free book with every purchase of 7 or more books, and offers 2 free books with every purchase of 12 or more books. Write a statement that assigns freeBooks the appropriate value based on the values of the bool variable isPremiumCustomer and the int variable nbooksPurchased.
Central Processing Unit.
The anagram CPU stands for
True
The body of a while loop may never execute.
False
The break statement causes all loops to exit.
\t
The character escape sequence to force the cursor to advance forward to the next tab setting is:
\n
The character escape sequence to force the cursor to go to the next line is:
\'
The character escape sequence to represent a single quote is:
False
The opposite of less than is greater than.
cout
The stream that is used for output to the screen is called ________.
cout << num << " " << cost << endl;
Two variables, num and cost have been declared and given values: num is an integer and cost is a double. Write a single statement that outputs num and cost to standard output. Print both values (num first, then cost), separated by a space on a single line that is terminated with a newline character. Do not output any thing else.
False
Variable names may begin with a number.
Hello students
What does the following statement in C++ print to the screen? cout << "Hello students\n";
specifies exact size in bits
What is the advantage of the C++11 integer data types over the old data types? Number of bits allocated changes dynamically as needed. no advantage, just new names specifies exact size in bits higher precision
while loop
What is the best loop type for the following problem? Testing a function to see how it performs with different values of its arguments
infinite loop.
What is the final value of x after the following fragment of code executes?
6
What is the final value of x in following code fragment?
nothing
What is the output of the following code fragment if x is 15?
30
What is the output of the following code fragment?
Nothing, there is a syntax error.
What is the output of the following code fragment?
value
What is the output of the following code? float value; value = 33.5; cout << "value" << endl; 33.5 33 value garbage
2
What is the third line of the output?
13
What is the value of x after the following code executes?
1.75
What is the value of x after the following statement? float x; x = 3.0 / 4.0 + (3 + 2 )/ 5 5.75 5.75 1.75 3.75
3.0
What is the value of x after the following statements? float x; x = 15/4;
3
What is the value of x after the following statements? int x; x = 15/4;
missing a semicolon
What is wrong with the following statement? cout << "Hello to everyone\n" cout should be count. missing a semicolon missing a " missing a (
semicolon
What punctuation signifies the end of a C++ statement?
(x>=15 || y < 3)
Which of the following are equivalent to (!(x<15 && y>=3))? (x>15 && y<=3) (x>=15 && y < 3) (x>=15 || y < 3) (x>15 || y < 3) (x>=15 || y < 3) and (x>15 || y < 3)
double x; x = 5.0; double x = 5.0;
Which of the following code segment declare a double variable x and assign value 5.0 to it correctly? Please pay attention to syntax error! double x = 5.0 double x, x = 5; double x; x = 5.0; double x = 5.0;
marketing the final program
Which of the following is NOT a phase of the program-design process? Correct marketing the final program problem-solving implementation marketing the final program
three_com
Which of the following is a valid identifier? three_com 3com 3_com three-com
cin >> x;
Which of the following is an input statement? #include <iostream> cin >> x; cout << x; int x;
#include <iostream>
Which of the following is correct? #include <iostream>; #include (iostream) #include <iostream> include <iostream>
!
Which of the following symbols has the highest precedence? && || + (as binary operator for addition) !
-1
Which of the following value can be assigned to x if we have the following declaration statement? int x; -3.45 3.14 1.1 -1
int x = 5; while(x >0);{ x--; }
Which of the following will have infinite loop? int x = 5; while(x >0){ x--; } int x = 0; while(x < 5){ x++; } int x = 5; while(x >0){ x--; }; int x = 5; while(x >0);{ x--; }
cout << "Hello World!\n";
Which of the following will move the cursor to new line after print out the message? cout << "Hello World!\n"; cout >> "Hello World!"; cout << "Hello World!/n"; cout << "Hello World!";
Bjarne Stroustrup
Who developed C++?
int age, weight;
Write a declaration for two integer variables, age and weight.
for (num =5; num <= 500; num += 5) {sum += num;}
Write a for loop that computes the following sum: 5+10+15+20+...+485+490+495+500. The sum should be placed in a variable sum that has already been declared and initialized to 0. In addition, there is another variable, num that has also been declared. You must not use any other variables.
for ( int i=1; i < 175; i++) { if ((i%5)==0) {cout << i << " "; } }
Write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, separated by spaces.
for (int i=1; i < 200; i++) { if ((i%2)==0 && (i%3)==0) { cout << i << " "; } }
Write a for loop that prints, in ascending order, all the positive integers less than 200 that are divisible by both 2 and 3, separated by spaces.
int num=0; int sumeven=0; int sumodd=0; int evencount=0; int oddcount=0; do { cin >> num; if (num % 2 == 0 && num > 0) { evencount++; sumeven += num; } else if (num > 0) { oddcount++; sumodd += num; } } while (num > 0); cout<<sumeven; cout<<" "<<sumodd;
Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read and the sum of all the odd integers read(The two sums are separated by a space). Declare any variables that are needed.
int x; do{ cin >> x; if(x>100) cout << x << " "; }while ( x>0) ;
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, and that terminates when it reads an integer that is not positive. The values should be separated by single blank spaces. Declare any variables that are needed.
string duck; string goose; string x; int count = 0; do { cin >> x; if (x < "goose") { count++; } } while (x < "goose"); cout << count;
Write a loop that reads strings from standard input where the string is either "duck" or "goose". The loop terminates when "goose" is read in. After the loop, your code should print out the number of "duck" strings that were read.
cout << "first is" << first << " " << "second = " << second << endl;
Write a single statement that will print the message "first is " followed by the value of first, and then a space, followed by "second = ", followed by the value of second. Print everything on one line and go to a new line after printing. Assume that first has already been declared as a double and that second has been declared as an int. Assume also that the variables have already been given values.
if (score1 > score2) { cout << "player1 wins"<<endl; ++player1Wins; ++player2Losses; } else if (score2 > score1) { cout << "player2 wins"<<endl; ++player2Wins; ++player1Losses; } else { cout << "tie" << endl; ++tieCount; }
Write a statement that compares the values of score1 and score2 and takes the following actions. When score1 exceeds score2, the message "player1 wins" is printed to standard out. When score2 exceeds score1, the message "player2 wins" is printed to standard out. In each case, the variables player1Wins,, player1Losses, player2Wins, and player2Losses,, are incremented when appropriate. Finally, in the event of a tie, the message "tie" is printed and the variable tieCount is incremented.
int age = 15, weight = 90;
Write a statement that declares and initializes two integer variables. Call the first one age and initialize it to 15, and call the second one weight and initialize it to 90.
total += amount;
Write a statement that increments the value of the int variable total by the value of the int variable amount. That is, add the value of amount to total and assign the result to total.
if (x == y) { break; }
Write a statement that terminates the current loop when the value of the int variables x. and y.are equal
price = cost * 3;
Write a statement to set the value of price equal to three times the value of cost. (The variables have already been declared and cost has already been initialized.)
switch(response) { case 'y': cout << "Your request is being processed"; break; case 'n': cout << "Thank you anyway for your consideration"; break; case 'h': cout << "Sorry, no help is currently available"; break; default: cout << "Invalid entry; please try again"; }
Write a switch statement that tests the value of the char variable response and performs the following actions: if response is y, the message Your request is being processed is printed if response is n, the message Thank you anyway for your consideration is printed if response is h, the message Sorry, no help is currently available is printed for any other value of response, the message Invalid entry; please try again is printed
if ( age < 18) {minors ++;} if ( age >= 18 && age <= 64) {adults++;} if (age >= 65 ) {seniors ++;}
Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18, adds 1 to the variable adults if age is 18 through 64, and adds 1 to the variable seniors if age is 65 or older.
int age = 18; double weight = 114.5;
Write declaration statements to declare and initialize two variables: one is an integer variable named age, initialized to 18, and the other variable, named weight, is initialized to 114.5.
for loop
You have a variable, n, with a non-negative value, and need to write a loop that will keep print n blank lines. What loop construct should you use?
while loop
You need to write a loop that reads integers and adds them to a sum as long as they are positive. Once 0 or a negative value is read in your loop terminates. Which is the preferred loop construct to use?
do while loop
You need to write a loop that will keep reading and adding integers to a sum, until the sum reaches or exceeds 21. The numbers are all less than 20 and the sum is initially 0. Which is the preferred loop construct to use?
for loop
You need to write a loop that will repeat exactly 125 times. Which is the preferred loop construct to use?
Grace Hopper
discovered the first documented computer bug.
is a variable declaration.
int number;