Chapter 2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Operands

THe data that operators work with. Ex: unitSold = 12 Then unitSold and 12 are operands.

Boolean expressions

Expressions that have a true or false value. True = 1 False = 0

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 that declares a double variable named dosage.

double dosage;

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

num=4;

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

string empty="";

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

highest=num;

A variable's scope

Every variable has a scope. The scope of a variable is the part of the program where the variable may be used.

Given the variable pricePerCase , write an expression corresponding to the price of a dozen cases.

pricePerCase * 12

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;

Write a character literal representing the (upper case) letter A .

'A'

Write a literal representing the character whose ASCII value is 65.

'A'

Write a hexadecimal integer literal representing the value fifteen.

0xf

Which of the following is NOT a C++ keyword? using int main namespace return

main

Declare a variable hasPassedTest , and initialize it to true .

bool hasPassedTest=true;

Write a literal representing the largest character value . Suppose we are using unsigned one-byte characters .

255

The char data type

Is used to store individual characters(1 letter) and needs to be put in ' '. These are characters are known as character literals. Its actually an integer data type that typically uses 1 byte of memory.

Which of the following names in a program is equivalent to the name int? Int INT All of the above None of the above

None of the above //Correct - Only int is int: case matters.

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

#include <iostream>

Which special of the following special characters does NOT need to be paired with another special character in C++? { ; ( "

;

Returning float points

Even if double is declared you may still lose the decimal. Ex: double Num Num = 5/2 Code prints out 2 NOT 2.5 However Num = 5.0/2

Strings

Are consecutive sequences of characters that occupy consecutive bytes of memory.

Initialization

When a value is assigned to a variable as a part of a variables definition.

The character escape sequence to represent a single quote is:

\'

The character escape sequence to force the cursor to go to the next line is:

\n

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

const int MonthsInYear = 12;

Given the variables costOfBusRental and maxBusRiders , write an expression corresponding to the cost per rider (assuming the bus is full).

costOfBusRental/maxBusRiders

Write a statement that prints the following to standard output : i= Just write one statement that generates the message above: do not generate any extraneous spaces. Do not declare variables , do not write a main() function, do not write a whole program .

cout << "i=";

Given two double variables , bestValue and secondBestValue, write some code that swaps their values . Declare any additional variables as necessary.

double tempValue; tempValue=bestValue; bestValue=secondBestValue; secondBestValue=tempValue;

Declare a variable x , suitable for storing values like 3.14159 and 6.02E23.

float x;

Given the variables fullAdmissionPrice and discountAmount (already declared and assigned values ), write an expression corresponding to the price of a discount admission. (The variable discountAmount holds the actual amount discounted, not a percentage.)

fullAdmissionPrice - discountAmount

Given three already declared int variables , i , j , and temp , write some code that swaps the values in i and j . Use temp to hold the value of i and then assign j 's value to i . The original value of i , which was saved in temp , can now be assigned to j .

temp=i; i=j; j=temp;

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;

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 long integer literal that has the value thirty-seven

37L

How many lines are printed out by this statement: cout << "abc\ndef\tghi\njkl" << endl << endl << "mno\npqr\n";

6

String literals

Are always stored in memory with a null terminator at the end. Which marks the end of a string. Enclosed in " "

Null terminator/character

Its 0. It adds a extra byte to a string to inform it that it is concluded.(so in terms of space its always +1)

The character escape sequence to represent a backslash is:

\\

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

____________ Rules: First character , must be a letter or underscore character. After first, you may use letters, numbers, or underscore. Upper and lower case symbolize 2 diff things.

Character literals

are enclosed in ' '

Which of the following will not be recognized if iostream is not included in the program? main std namespace return cout

cout

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 an integer variable count, write a statement that writes the value of count to standard output .

cout << count;

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<<"\n";

Write a declaration for a variable temperature that can hold the current outdoor temperature, measured to the half degree (like 98.6 or 31.5).

float temperature;

Of the following variable names, which is the best one for keeping track of whether a patient has a fever or not? temperature feverTest hasFever fever

hasFever

Write a declaration for two integer variables , age and weight.

int age, weight;

Declare an integer variable cardsInHand and initialize it to 13.

int cardsInHand=13;

Write a statement that declares an int variable named count.

int count;

Write a declaration of a variable named numberOfWidgets that can be used to hold numbers like 57 and 981 and -4.

int numberOfWidgets;

Write a declaration of a variable named number_of_children that can be used to hold the number of children in a family.

int number_of_children;

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

int populationChange;

Write a statement that declares an int variable presidentialTerm and initializes it to 4.

int presidentialTerm=4;

Declare two integer variables named profitStartOfQuarter and cashFlowEndOfYear.

int profitStartOfQuarter; int cashFlowEndOfYear;

Declare a string variable named mailingAddress.

string mailingAddress;

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

string str="Hello";

Write a literal representing the false value in C++.

false

Write a literal representing the true value .

true

Declare a variable isACustomer, suitable for representing a true or false value .

bool isACustomer;

The text of a comment is checked by the compiler for accuracy. must appear in the first line of the program. is printed out when the program runs. can be anything the programmer wants to write.

can be anything the programmer wants to write.

Declare a character variable named c.

char c;

Write a statement to multiply diameter by 3.14159 and assign the result to circumference. (The variables have already been declared and diameter has already been initialized .)

circumference=diameter*3.14159;

Declare an integer variable named degreesCelsius .

int degreesCelsius;

Which of the following lines contains a valid 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 //* double twoPi = 2*3.14159; /* // holds the value of two time pi */ [comment] //

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

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

int volume; // size of trunk in cubic feet

Write an expression that computes the remainder of the variable principal when divided by the variable divisor . (Assume both are type int .)

principal % divisor

An identifier that cannot be used as a variable name is a

reserved word

Write an expression that computes the sum of the two variables verbalScore and mathScore (already declared and assigned values ).

verbalScore + mathScore

Assume that an int variable x has already been declared ,. Write an expression whose value is the last (rightmost) digit of x .

x % 10

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 //*//

Write a literal corresponding to the value of the first 6 digits of PI ("three point one four one five nine").

3.14159

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

7thheaven

Write an expression whose value is the number of bytes that an unsigned long variable occupies on whatever machine the expression is executed on.

sizeof(long);

Literals

"Constant" values that are assigned to variables. can be string or integer

A preprocessor directive starts with what character or characters?

#

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 <iostream> using namespace std; int main() { int value = 100; cout <<value; return 0; }

Suppose your name was George Gershwin. Write a complete program that would print your last name, followed by a comma, followed by your first name. Do not print anything else (that includes blanks).

#include <iostream> using namespace std; int main() { cout << "Gershwin,George " <<endl; return 0; }

The character escape sequence to force the cursor to advance forward to the next tab setting is:

\t

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 declaration for a variable hits that can hold the number of times a baseball player has hit the ball in a baseball game.

int hits;

The exercise instructions here are LONG -- please read them all carefully. If you see an internal scrollbar to the right of these instructions, be sure to scroll down to read everything. Declare and initialize the following variables : A variable monthOfYear , initialized to the value 11 A variable companyRevenue , initialized to the value 5666777 A variable firstClassTicketPrice , initialized to the value 6000 A variable totalPopulation , initialized to the value 1222333

int monthOfYear=11; long companyRevenue=5666777; int firstClassTicketPrice=6000; long totalPopulation=1222333;

Declare an unsigned integer variable named degreesKelvin .

unsigned int degreesKelvin;

Write a literal representing the integer value zero.

0

QUESTION 1: How many bytes are needed to store: 'n' ? QUESTION 2: How many bytes are needed to store: "n" ? QUESTION 3: How many bytes are needed to store: '\n' ? QUESTION 4: How many bytes are needed to store: "\n", ? QUESTION 5: How many bytes are needed to store: "\\n" ? QUESTION 6: How many bytes are needed to store: "" ?

1. 1 2. 2 3. 1 4. 2 5. 3 6. 1

What's the difference in UNICODE value between 'E' and 'A'? (consult a table of UNICODE values): QUESTION 2: What's the difference in UNICODE value between 'e' and 'a'? (consult a table of UNICODE values): QUESTION 3: What's the difference in UNICODE value between '3' and '0'? (consult a table of UNICODE values): QUESTION 4: What's the difference in UNICODE value between '6' and '0'? (consult a table of UNICODE values):

1. 4 2. 4 3. 3 4. 6

Write a literal representing the long integer value twelve million.

12000000L

Qualifying an integer declaration with "unsigned" essentially increases the maximum value of the variable by a factor of

2

The word in the brackets of an include directive specifies A. a namespace. B. a file containing code that is copied into the program at that point. C. a file containing definitions of input/output code. D. the name of the program.

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

Identifier

A programmer-defined name that rep some element of a program.

Function

Can be though of as a group of one or more programming statements that collectively has a name.

Integer variables

Can only hold whole numbers. -Float point holds decimals

The character escape sequence to represent a double quote is:

\"

Given an integer variable i and a floating-point variable f, write a statement that writes both of their values to standard output in the following format: i=value -of-i f=value -of-f Thus, if i has the value 25 and f has the value 12.34, the output would be: i=25 f=12.34 But if i has the value 187 and f has the value 24.06, the output would be: i=187 f=24.06

cout << "i=" << i << " " << "f=" << f;

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

cout << message;

Declare a numerical variable precise and initialize it to the value 1.09388641.

double precise=1.09388641;

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.

int temp; temp=firstPlaceWinner; firstPlaceWinner=secondPlaceWinner; secondPlaceWinner=temp;

Given two int variables , i and j, which have been declared and initialized , and two other int variables , itemp and jtemp, which have been declared , write some code that swaps the values in i and j by copying their values to itemp and jtemp, respectively, and then copying itemp and jtemp to j and i, respectively.

itemp=i; jtemp=j; i=jtemp; j=itemp;

Preprocessor directives are carried out: just before a program is loaded into the processor. just before a program is executed by the central processing unit (CPU). just before a program is processed by the compiler. just before the program's output is processed.

just before a program is processed by the compiler. its the set up for your source code. They also dont require ; at the end

Declare a long integer variable named grossNationalProduct.

long int grossNationalProduct;

Every C++ program must contain a ____ function.

main

Of the following variable names, which is the best one for keeping track of whether an integer might be prime or not? divisible isPrime mightBePrime number

mightBePrime

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

monthlyPay

Write an expression whose value is the number of bytes that an int variable occupies on whatever machine the expression is executed on.

sizeof(int);

The names defined in iostream are associated with which namespace?

std

Write a floating point literal corresponding to the value zero.

.0

A comment starts with what characters?

//

How many spaces printed out by this statement: cout << "how" << "now" << "brown" << "cow" << "?";assingned

0

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 <iostream> cout << "Q"; { using namespace std;

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

Write a complete program that prints Hello World to the screen.

#include <iostream> using namespace std; main() { cout <<"Hello World"; return 0; }

Write the include directive that allows use of the function headers in the file myFuncs.h.

#include <myFuncs.h>

Write the necessary preprocessor directive to enable the use of the C++ string class .

#include <string>

C++ string class

#include <string> then define the string variable Allows you to store more then one character in a variable unlike char.

Write a character literal representing the digit 1 .

'1'

{} Braces

All the statements that make up a function are enclosed in a set of braces.

Dividing two integers

Will only result in an integer(whole #) and integer division occurs. AKA you lose the decimal place NOT rounding.

Declare a variable temperature and initialize it to 98.6.

float temperature=98.6;

Assume that an int variable x that has already been declared and initialized . Write an expression whose value is 1 more than x .

x+1


Kaugnay na mga set ng pag-aaral

Chapter 2: World Trade - An Overview

View Set

Chapter 9 - Cellular Respiration: Harvesting Chemical Energy

View Set

Psych Nursing Mental Health Assessment of Children and Adolescents PrepU

View Set

Intro Criminal Justice- Chapter 6

View Set

Integumentary System Ch 6 Notes (Part 2 Dermis)

View Set

EMT Chapter 14: Medical Overview

View Set