Chapter 2 - Introduction to C++

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

Which of the following statements is correct?

#include <iostream>

Which of the following is a character literal?

'A'

Which of the following is a char literal in C++?

'a'

What does the following statement display? cout << "(Hello\nWorld)" << endl;

(Hello World)

These make up the "core" of the C++ language and have specific purposes.

Key words

What will the following code display? cout << "Monday"; cout << "Tuesday"; cout << "Wednesday";

MondayTuesdayWednesday

What will the following code display? int number = 7; cout << "The number is " << "number" << endl;

The number is number

String literals are always stored in memory with a null terminator at the end.

True

What does the following statement display? cout << R" (a\nb\nc)" << endl;

a\nb\nc

Assume that ans and num are two int variables that have already been declared, and num has been assigned a value. Write a statement that assigns to ans the value of num plus 5.

ans = num + 5;

This key word tells the compiler to determine the variable's data type from the initialization value.

auto

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

const int MonthsInDecade = MonthsInYear * 10;

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

const int MonthsInYear = 12;

Write a statement that prints Hello World to the screen.

cout << "Hello World";

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.

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

Assume that message is a string variable. Write a statement to displays its value.

cout << message;

Two variables, num and cost have been declared and given values: num is an int and cost is a double. Write a single statement that displays num and cost. Display both values (num first, then cost), separated by a space on a single line that is terminated with a newline character. Do not output anything else.

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

Which of the following is not a data type in C++?

decimal

Which of the following data types can represent floating-point numbers?

double long double float

Declare a double variable named distance.

double distance;

Write a statement that declares a double variable named dosage.

double dosage;

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

drivingAge = 17;

Declare an int variable named degreesCelsius.

int degreesCelsius;

Which of the following is a valid variable definition?

int number;

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

int populationChange;

Assume that price and cost are variables that have already been declared, and cost has been assigned a value. Write a statement that assigns to price the value of three times cost.

price = cost * 3;

"42" is an example of a(n) _____ literal.

string

Which of the following expressions is an example of a unary operator?

-5

What will the value of x be after the following statements execute? int x; x = 18 / 4;

4

What will the value of x be after the following statements execute? int x; x = 18.0 / 4;

4

The first step in using the string class is to define a string object.

False

3DMovieName is a legal variable name in C++.

False

A variable called "average" should be declared as an integer data type because it will probably hold data that contains decimal places.

False

This data type allows you to create small integer variables that are suitable for holding true or false values.

bool

Declare a Boolean variable named hasPassedTest, and initialize it to true.

bool hasPassedTest = true;

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

#include <iostream>

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

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

Consider the program 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() { cout << value; int value = 100; return 0; }

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

Which of the following preprocessor directives is needed for the string class?

#include <string>

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

#include <string>

Which of the following is a specific rule that must be followed with the C++ identifiers?

- Uppercase and lowercase characters are distinct. - The first character must be one of the letters a through z, A through Z, or an underscore character (_). - After the first character you may use the letters a through z, A through Z, the digits 0 through 9, or underscores.

Which of the following is an example of a multi-line comment?

/* Knowledge comes, but wisdom lingers. */

The Boolean value true is stored in memory as _____.

1

How much memory does a character normally occupy?

1 byte

Look at the following code. What value will be assigned to the result variable after this code executes? double result; int number1 = 5, number2 = 2; result = number1 / number2;

2

Which of the following is NOT a legal identifier?

7thheaven

Unsigned data types can store positive or negative values.

False

When one of the division operator's operands is an integer, the result of the division will also be an integer.

False

You cannot initialize a named constant that is declared with the const modifier.

False

You may choose your own variable names in C++, including any of the C++ key words.

False

What will the following code display? cout << "Four"; cout << "Five"; cout << "Six";

FourFiveSix

When you assign values to variables as part of the definition, it is called this:

Initialization.

What will the following program display on the screen? // My first C++ program #include <iostream> using namespace std; int main() { cout << "One"; cout << "Two\n"; cout << "Three\n"; return 0; }

OneTwo Three

A left brace in a C++ program should always be followed by a right brace later in the program.

True

A variable cannot be used in any part of the program before the definition.

True

An advantage of using named constants is that they make programs more self-documenting.

True

Character literals are enclosed in single quotation marks.

True

Comments are part of the program, but the compiler ignores them.

True

In C++ terminology, an lvalue is something that identifies a place in memory whose contents may be changed, and an rvalue is any expression that has a value. true

True

Integer variables can hold only whole numbers.

True

One of the problems of portability is the lack of common sizes of data types on all machines.

True

The compiler doesn't actually see the #include directive. Instead it sees the code that was inserted by the preprocessor, just as if the programmer typed it there.

True

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

True

The stream insertion operator is always written as two less-than signs with no space between them.

True

There are no unsigned floating point data types.

True

Unlike a comment started with //, a multi-line comment can span several lines.

True

When a floating-point value is assigned to an integer variable, the fractional part of the value (the part after the decimal point) is discarded.

True

Working with string objects is similar to working with variables of other types.

True

You can use cout to display the value of a string object.

True

You should always choose names for your variables that give an indication of what the variables are used for.

True

short and short int are the same data type.

True

Which of the following IS a legal identifier?

______________

Which of the following is a C++ identifier?

a

The word in the brackets of an include directive specifies

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

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;

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 a floating-point variable named fraction, write a statement that displays the value of fraction. Do not display anything else -- just the value of fraction.

cout << fraction;

Declare a variable temperature and initialize it to 98.6.

float temperature = 98.6;

Which of the following must every C++ program have?

function main

Given two int variables named matricAge and gradAge, write a statement that assigns gradAge a value that is 4 more than the value of matricAge.

gradAge = matricAge + 4;

Which data type should you use to store a person's age?

int

Declare an int variable cardsInHand and initialize it to 13.

int cardsInHand = 13;

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;

Write one statement that defines two int variables named profitStartOfQuarter and cashFlowEndOfYear.

int profitStartOfQuarter, cashFlowEndOfYear;

Which of the following lines contains a valid, though not necessarily accurate comment?

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

How would you consolidate the following declaration statements into one statement? int x = 7; int y = 16; int z = 28;

int x = 7, y = 16, z = 28;

This header file must be included in any program that uses the cout object.

iostream

A preprocessor directive starts with #. Preprocessor directives are carried out _____

just before a program is processed by the compiler.

Which of the following would store integer literal 64 as a long long int?

light years = 64LL;

Declare a long integer variable named grossNationalProduct.

long grossNationalProduct;

Every C++ program must contain a _____ function.

main

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

monthlypay

Assume that tax, gross_pay, and net_pay are variables that have already been declared. Also, assume that tax and gross_pay have been assigned values. Write a statement that subtracts tax from gross_pay and assigns the result to net_pay.

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;

This special operator will report the number of bytes of memory used by any data type or variable.

sizeof

Write an expression whose value is the number of bytes that an int variable occupies on whatever machine the expression is executed on. Note: Simply write the expression. Do not assign the expression to a variable.

sizeof(4);

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 str and initialize it to Hello.

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

The preprocessor inserts the entire contents of the file into the program at the point it encounters this.

the #include directive


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

itf+ Test Prep errors FULL LENGTH TEST SET 13

View Set

Chapter 31: The Infant and Family

View Set

МОВА ДIЛОВИХ ПАПЕРІВ

View Set

Physiology Exam Practice Questions

View Set