Cs1428 revel quizzes/checkpoints

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

Write the include directive needed to allow use of the various I/O operators such as cout and cin.

#include <iostream>

Consider Program 2-19 below. Rearrange the lines of code to fix the scope error, yielding a correct C++ program that prints the number 100. #include using namespace std; int main() { cout << value; int value = 100; return 0; }

#include <iostream> using namespace std; int main () { int value = 100; cout << value; return 0; }

Write the necessary preprocessor directive to enable the use of the stream manipulators like setw and setprecision.

#include<iomanip>

Write an expression that computes the average of the values 12 and 40.

(12+40)/2

Given the boolean variable isFullTimeStudent and the integer variable age, write an expression that evaluates to true if age is less than 19 or isFullTimeStudent is true.

(age<19 || isFullTimeStudent==true) ? true:false

Given two integer variables distance and speed, write an expression that divides distance by speed using floating point arithmetic, i.e. a fractional result should be produced.

(double)distance/speed

Write an expression that computes the average of the variables exam1 and exam2 (both declared and assigned values).

(exam1+exam2)/2

Assume that s is a string. Write an expression whose value is true if and only if the value of s would come between "mortgage" and "mortuary" in the dictionary.

(s > "mortgage" && s< "mortuary")

Given the variables temperature and humidity, write an expression that evaluates to true if and only if the temperature is greater than 90 and the humidity is less than 10.

(temperature>90 && humidity<10) ? true : false

The dimensions (width and length) of room1 have been read into two variables: width1 and length1. The dimensions of room2 have been read into two other variables: width2 and length2. Write a single expression whose value is the total area of the two rooms.

(width1*length1)+(width2*length2)

Assume that x is a char variable that has been declared and already given a value. Write an expression whose value is true if and only if x is a uppercase letter.

(x >='A' && x<='Z')

Assume that x is a char variable that has been declared and already given a value. Write an expression whose value is TRUE if and only if x is a decimal digit (0-9).

(x>='0' && x<='9')

Write a statement that uses the decrement operator, in prefix mode, to decrease the value of the variable time.

--time;

Assume hour is an int variable. Write an if statement that displays the message "Time for lunch" only if hour is equal to 12.

.....

Assume powerSavingMode is a bool variable. Write an ifstatement that displays the message "Conserving Power" only if powerSavingModeis equal to true.

.....

Write an if/else statement that assigns 0.20 to commissionRate if sales is greater than or equal to 50000.00. Otherwise, it should assign 0.10 to commissionRate.

.....

Write an if/else statement that assigns 1 to x if y is equal to 100. Otherwise it should assign 0 to x.

.....

Which of the following lines does NOT consist of (a) valid, though boastful, comment(s)? /* This is a //*// First Rate Program //*// //**// This is a //**// First Rate Program //**// //* This is a //*// First Rate Program //*// // /* This is a */ First Rate Program

/* This is a //*// First Rate Program //*//

Rearrange the following code so that it forms a correct program that prints out the letter Q: int main() } // A SCRAMBLED program return 0; #include cout << "Q"; { using namespace std;

//A SCRAMBLED program #include <iostream> using namespace std;int main() { cout << "Q"; return 0; }

Which of the following is NOT a legal identifier? _42 outrageouslyAndShockinglyLongRunon lovePotionNumber9 _ 7thheaven

7thheaven

When a program runs on a computer, the part of the computer that carries out the instructions is called the.....

CPU

Write a statement that closes a file stream object namedinputFile.

Write a statement that closes a file stream object namedinputFile.

Which of the following IS a legal identifier? "Hello World" ____________ 5_And_10 LovePotion#9 Five_&_Ten

____________

A bit is

a binary digit, like 0 or 1.

The word in the brackets of an include directive specifies

a file containing code that is copied into the program at that point.

A byte in memory is identified by a unique number called its _______. In modern computer systems, a byte consists of ______ bits.

address, 8

Flash drives, CDs, external disks are all examples of ______ storage devices. secondary auxiliary external nonvolatile

all of the above****

Write a statement to set the value of ans equal to the value of num plus 5. (These variables have already been declared and num has already been initialized.)

ans=num+5;

Write a statement to set the value of area to the value of length times the value of width. (The variables have already been declared and length and width have already been initialized.)

area=length*width;

Which comment below is the most helpful? a)int volume; // declare volume b)int volume; // size of trunk in cubic feet c)int volume; // declare an int d) int volume; // declare volume to be an int variable

b)int volume; // size of trunk in cubic feet

Write a statement that defines a bool variable named finished.

bool finished;

Declare a variable hasPassedTest, and initialize it to TRUE.

bool hasPassedTest = true;

Declare a variable isACustomer, suitable for representing a TRUE or FALSE value.

bool isACustomer;

Given an integer variable bridgePlayers, write an statement that increases the value of that variable by 4.

bridgePlayers+=4;

The text of a comment

can be anything the programmer wants to write.

Declare a character variable named c.

char c;

Given an int variable datum that has already been declared, write a statement that reads an integer value from standard input into this variable.

cin >> datum;

Write a statement that reads a floating point (real) value from standard input into temperature. Assume that temperaturehas already been declared as a double variable.

cin >> temperature;

A variable c of type char has been declared. Write the necessary code to read in the next character from standard input and store it in c, regardless of whether is a whitespace character.

cin.get(c);

Assume the following variable definition appears in a program: char ch; Write a statement that uses the cin.get function to read a character and store the character in the ch variable.

cin.get(ch);

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".

cin>>callsReceived >> operatorsOnCall; if (operatorsOnCall>0) cout << callsReceived/operatorsOnCall; else cout <<"INVALID";

Declare an int constant MonthsInDecade whose value is the value of the constant MonthsInYear (already declared) multiplied by 10.

const int MonthsInDecade = MonthsInYear*10;

Write a statement that defines a named constant named SPEED. The constant's data type should be int, and its value should be 75.

const int SPEED = 75;

Assume an int variable named count has been defined. Write a statement that assigns the value 100 to the count variable.

count = 100;

Write a cout statement that displays One Two Three.

cout << "One Two Three";

Assume that word is a string variable. Write a statement to display the message "Today's Word-Of-The-Day is: " followed by the value of word on standard output.

cout << "Today's Word-Of-The-Day is: "+word;

Given a floating-point variable fraction, write a statement that writes the value of fraction to standard output. Do not write anything else to standard output -- just the value of fraction.

cout << fraction;

Assume that message is a string variable. Write a statement to display its value on standard output.

cout << message;

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.

cout << num << " " << cost << endl;

Write a statement that prints Hello World to the screen.

cout<<"Hello World";

Assume that x is a double variable that has been given a value.  Write a statement that prints it out with exactly three digits to the right of the decimal point no matter what how big or miniscule its value is.

cout<<fixed<<setprecision(3)<<x;

Assume that m is an int variable that has been given a value. Write a statement that prints it out in a print field of 10 positions.

cout<<m<<setw(10);

Assume the following variable definition appears in a program: double number = 12.3456; Write a cout statement that uses the setprecision manipulator and the fixed manipulator to display the number variable rounded to 2 digits after the decimal point. (Assume that the program includes the necessary header file for the manipulators.)

cout<<setprecision(2)<<fixed<<number;

Assume the following variable definition appears in a program: double number = 12.3456; Write a cout statement that uses the setprecision manipulator to display the number variable with 4 significant digits. (Assume that the program includes the necessary header file for the setprecision manipulator.)

cout<<setprecision(4)<<number;

Checkpoint 3.25 Assume the following variable definition appears in a program: double number = 12.0; Write a cout statement that uses the setprecision manipulator and the showpoint manipulator to display the number variable with 8 significant digits, padded with trailing zeroes. (Assume that the program includes the necessary header file for the manipulators.)

cout<<setprecision(8)<<showpoint<<number;

Assume the following variable definition appears in a program: int number = 25; Write a cout statement that uses the setw manipulator to display the number variable with a field width of 4. (Assume that the program includes the necessary header file for the setw manipulator.)

cout<<setw(4)<<number;

Checkpoint 3.6 Assume that the following variables have been defined in a program: int x = 10; int y = 20; int z = 30; Write a cout statement that displays the sum of the variables x, y, and z.

cout<<x+y+z;

Before a variable is used it must be

declared

Declare a double variable named distance.

double distance;

Write a statement that declares a double variable named dosage.

double dosage;

Write a statement that defines a double variable named result, initialized with the value 6.759.

double result = 6.759;

Write a statement that defines a double variable named temperature.

double temperature;

Declare a variable temperature and initialize it to 98.6.

double temperature=98.6;

Given an integer variable drivingAge that has already been declared, write a statement that assigns the value 17 to drivingAge.

drivingAge = 17;

Given two integer variables matricAge and gradAge, write a statement that gives gradAge a value that is 4 more than the value of matricAge.

gradAge = matricAge+4;

Given an int variable grossPay, write an expression that evaluates to TRUE if and only if the value of grossPay is less than 10,000.

grossPay<10000

Given two integer variables num and highest, write a statement that gives highest the same value that num has.

highest = num;

Write an if statement that prints the message "speed is below the limit" if the expression (speed > 75) is not true. Use the ! operator in your Boolean expression.

if (!(speed>75)) cout<<"speed is below the limit";

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.

if (age < 18) minors++; else if (age >=18 && age <=64) adults++; else seniors++;

Write an if/else statement that compares the variable age with 65, adds 1 to the variable seniorCitizens if age is greater than or equal to 65, and adds 1 to the variable nonSeniors otherwise.

if (age>=65) seniorCitizens++; else nonSeniors++;

Assume that the variables gpa, deansList and studentName, have been declared and initialized. Write a statement that both adds 1 to deansList and prints studentName to standard out if gpa exceeds 3.5.

if (gpa > 3.5) { deansList++; cout << studentName; }

Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. Given a variable modelYear, write a statement that prints the message "RECALL" to standard output if the value of modelYear falls within that range.

if (modelYear >=2001 && modelYear<=2006) cout << "RECALL";

Write a conditional that decreases the variable shelfLife by 4 if the variable outsideTemperature is greater than 90

if (outsideTemperature > 90) shelfLife -= 4;

Write an if statement that prints the message "System is normal" if the variable pressure is less than 100 and the variable temperature is less than 200.

if (pressure<100&&temperature<200)cout<<"System is normal";

Write an if statement that prints the message "The number is valid" if the variable quantity is within the range 0 through 500.(both inclusive)

if (quantity >=0&&quantity<=500) cout << "The number is valid";

Write an if/else statement that compares the value of the variables soldYesterday and soldToday, and based upon that comparison assigns salesTrend the value -1 or 1. -1 represents the case where soldYesterday is greater than soldToday; 1 represents the case where soldYesterday is not greater than soldToday.

if (soldYesterday>soldToday) salesTrend=-1; else salesTrend=1;

Write an if/else statement that assigns TRUE to the variable fever if the variable temperature is greater than 98.6; otherwise it assigns FALSE to fever.

if (temperature>98.6) fever=true; else fever=false;

Write an if statement that prints the message "Application accepted" if the variable workExperience is greater than or equal to 2 or the variable hasCollegeDegree is true.

if (workExperience>=2||hasCollegeDegree)cout<<"Application accepted";

Write an if statement that prints the message "The number is not valid" if the variable distance is outside the range 100 through 2000.(both inclusive)

if(distance < 100 || distance > 2000) cout << "The number is not valid";

Write a conditional that assigns 10,000 to the variable bonus if the value of the variable goodsSold is greater than 500,000.

if(goodsSold>500000)bonus=10000;

Assume input is a char variable. Write an if statement that prints the message "Digit detected" if the value of input is within the range '0' through '9'.

if(input >=48 && input<=57) cout << "Digit detected";

Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. Given an int variable modelYear write a statement that prints the message "NO RECALL" to standard output if the value of modelYear DOES NOT fall within that range.

if(modelYear > 2000 && modelYear < 2007){}else{ cout << "NO RECALL"; }

Write an if statement that prints the message "We have a match" if the string object name is equal to the string literal "Swordfish".

if(name.compare("Swordfish") ==0) cout << "We have a match";

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.

if(nbooksPurchased > 4){ if(isPremiumCustomer){ freeBooks = 1; if(nbooksPurchased > 7){ freeBooks = 2; } }else{ freeBooks = 0; if(nbooksPurchased > 6){ freeBooks = 1; } if(nbooksPurchased > 11){ freeBooks = 2; } } }else{freeBooks = 0;}

Write a statement that defines an ifstream object named inputFile and opens a file named "Friends.txt".

ifstream inputFile("Friends.txt");

Write a statement that defines an ifstream object named inputFile.

ifstream inputFile;

Write a statement that reads a value from an ifstream objectnamed inputFile, and stores the value in a string variable movieTitle.

inputFile >> movieTitle;

Write a statement that closes a file stream object namedinputFile.

inputFile.close();

Declare an integer variable cardsInHand and initialize it to 13.

int cardsInHand = 13;

Declare an int constant, MonthsInYear, whose value is 12.

int const MonthsInYear = 12;

Write a declaration of a variable named count that can be used to hold numbers like 90000 and -1 and -406.

int count;

Write a statement that declares an int variable named count.

int count;

Declare an integer variable named degreesCelsius.

int degreesCelsius;

Write a statement that defines an int variable named distance, initialized with the value 100.

int distance = 100;

Write a statement that defines an int variable named distance.

int distance;

Declare a variable populationChange, suitable for holding numbers like -593142 and 8930522.

int populationChange;

Declare two integer variables named profitStartOfQuarter and cashFlowEndOfYear.

int profitStartOfQuarter; int cashFlowEndOfYear;

Write a statement that defines an int variable named temperature.

int temperature;

Which of the following lines contains a valid, though not necessarily accurate, comment? -double twoPi = 2*3.14159; /* // holds the value of two time pi */ [comment] // -int twoPi = 3.14159; /* holds the value of two times pi */ -int twoPi = 2*3.14159; /* holds the value of two times pi //* -int twoPi = 2*3.14159; / / *holds the value of 6 //*

int twoPi = 3.14159; /* holds the value of two times pi */

A binary digit

is zero or one.

Write an expression that evaluates to TRUE if and only if the value of the boolean variable isAMember is FALSE.

isAMember == false

Words that have a special meaning in a programming language are called _____.

key words

Write a statement to assign to kilos the value of pounds divided by 2.2. (The variables have already been declared and pounds has already been initialized.)

kilos=pounds/2.2;

Assume a char variable named letter has been defined. Write a statement that assigns the letter A as a character literal to the letter variable.

letter = 'A';

Every C++ program must contain a _____ function.

main

Write a statement using a compound assignment operator to subtract 10 from minutes_left (an integer variable that has already been declared and initialized).

minutes_left-=10;

Which is the best identifier for a variable to represent the amount of money your boss pays you each month? monthlyPay paymentAmount wages notEnough money

monthlyPay

Write a statement to subtract tax from gross_pay and assign the result to net_pay . (The variables have already been declared and gross_pay and tax have already been initialized.)

net_pay=gross_pay-tax;

Write a statement to set the value of num to 4 (num is a variable that has already been declared).

num = 4;

Write a statement using the increment operator to increase the value of num_items (an already declared integer variable) by 1.

num_items++;

Write a statement using a compound assignment operator to multiply num_rabbits by 4, changing the value of num_rabbits (num_rabbits has already been declared and initialized).

num_rabbits*=4;

Assume a double variable named number has been defined. Write a statement that calculates 100 divided by 5 and assigns the result to the number variable.

number = 100 / 5;

Compilers translate the instructions written by the programmer into _____ code.

object

Write a statement that defines an ofstream object named outputFile

ofstream outputFile;

Monitors, printers, status lights are all examples of _____ devices.

output

Write a statement that writes the string literal "I love cookies!" to an ofstream object named outputFile.

outputFile<<"I love cookies!";

Write a statement that uses the increment operator, in postfix mode, to increase the value of the variable points.

points++;

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.)

price= cost*3;

Write an expression that evaluates to TRUE if and only if the variables profits and losses are exactly equal.

profits==losses

Assume that three double variables named result, number1, and number2 have been defined, and have been assigned values. Write a statement that assigns the value of number1 multiplied by number2 to result.

result = number1 * number2;

Assume the following variable definitions appear in a program: double base = 10.0; double result; Write a statement that uses the pow function to raise the base variable to the power of 5, and assigns the result to the result variable. (Assume that the program includes the necessary header file for the pow function.)

result = pow(base,5);

Checkpoint 3.33 Assume the following variable definition appears in a program: double number = 125; double result; Write a statement that uses the sqrt function to get the square root of the number variable, and assigns the result to the result variable. (Assume that the program includes the necessary header file for the sqrt function.)

result=sqrt(number);

The code that a programmer writes is called _____ code.

source

Write a statement that defines a string object named address.

string address;

Declare a string variable named empty and initialize it to the empty string.

string empty="";

Declare a string named line and write a statement that reads in the next line of console input into this variable.

string line; getline(cin,line);

Declare a string variable named mailingAddress.

string mailingAddress;

Declare a string variable named str and initialize it to Hello.

string str; str = "Hello";

Write a statement to add the values of x and y together, storing the result in sum. (The variables have already been declared and x and y have already been initialized.)

sum=x+y;

HTTP is the protocol that governs communications between web servers and web clients (i.e. browsers). Part of the protocol includes a status code returned by the server to tell the browser the status of its most recent page request. Some of the codes and their meanings are listed below: 200, OK (fulfilled) 403, forbidden 404, not found 500, server error Given an int variable status, write a switch statement that prints out the appropriate label from the above list based on status.

switch(status) { case 200: cout << "OK (fulfilled)"; break; case 403: cout << "forbidden"; break; case 404: cout << "not found"; break; case 500: cout << "server error"; break; }

The rules that govern the correct order and usage of the elements of a language are called the _____ of the language.

syntax

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.

variable

Write an expression that evaluates to TRUE if and only if the value of the integer variable x is equal to zero.

x==0

Write an expression that evaluates to TRUE if the value x is greater than or equal to y.

x>=y

Write an expression that evaluates to TRUE if and only if the integer x is greater than the integer y.

x>y


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

SER216: Testing Lifecycle, Unit Testing, Network Programming

View Set

Mental Health Ch 9 (Therapeutic Communication) - NCLEX style questions

View Set

Final Lecture Exam: micro Dr. Dan

View Set