Chapter 2 C++

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

A preprocessor directive starts with what character or characters?

#

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

#include <iostream>

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"; return 0; }

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

#include <string>

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 floating point literal corresponding to the value zero.

.0

A comment starts with what characters?

//

Rearrange the following code so that it forms a correct program that prints out the letter Q:

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

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

0

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

0

Write a literal representing the integer value zero.

0

Write a literal representing the true value.

1

Write a literal corresponding to the floating point value one-and-a-half

1.5

What's the difference in UNICODE value between '3' and '0'? (consult a table of UNICODE values):

3

What's the difference in UNICODE value between 'E' and 'A'? (consult a table of UNICODE values):

4

What's the difference in UNICODE value between 'e' and 'a'? (consult a table of UNICODE values):

4

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

6

What's the difference in UNICODE value between '6' and '0'? (consult a table of UNICODE values):

6

Which of the following IS a legal identifier?

____________

The word in the brackets of an include directive specifies

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

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;

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

bool isACustomer;

The text of a comment

can be anything the programmer wants to write.

Declare a character variable named c.

char c;

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

const int MonthsInYear = 12;

Which of the following will not be recognized if iostream is not included in the program?

cout

Suppose your name was Alan Turing. Write a statement that would print your last name, followed by a comma, followed by your first name. Do not print anything else (that includes blanks).

cout << "Turing,Alan";

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 << endl;

Given an integer variable count, write a statement that writes the value of count to standard output.

cout << count;

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 << endl;

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

cout << message << 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.

cout << num << " " << cost << '\n';

Declare a variable miles_run and initialize it to 3.75.

double miles_run = 3.75;

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

double precise = 1.09388641;

Declare a variable temperature and initialize it to 98.6.

double temperature = 98.6;

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

double temperature;

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

drivingAge = 17;

Declare a floating point variable named distance.

float distance;

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

hasFever

Write a declaration for an integer variable area and a variable price that can hold numbers with decimal places.

int area; float price;

Declare an integer variable cardsInHand and initialize it to 13.

int cardsInHand = 13;

Write a declaration for a variable channel that can hold TV channel numbers (1 to 900) and a variable hours_recorded that can hold the number of hours of TV recorded (numbers like 1.0 or 3.5).

int channel; float hours_recorded;

Write a statement that declares an int variable named count.

int count;

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;

Which comment below is the most helpful?

int volume; // size of trunk in cubic feet

Write a declaration of an int variable year and initialize it to 365.

int year = 365;

Preprocessor directives are carried out

just before a program is processed by the compiler.

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

key word

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;

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?

mightBePrime

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 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 a statement to find the remainder rem when num is divided by 5. (The variables have already been declared and num has already been initialized.)

rem = num % 5;

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

string empty;

Declare a string variable named mailingAddress.

string mailingAddress;

Declare a string variable named oneSpace and initialize it to a string consisting of a single space.

string oneSpace; oneSpace = " ";

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;

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

unsigned short age, weight;

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.

unsigned short hits;


Ensembles d'études connexes

AAFP Family Medicine Board Questions

View Set

APUSH Unit 8 - FDR's Foreign Policy & WWII

View Set