C++ Programming Chapter 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

If a char is 1 byte (which is common), what is the range of values it can store?

-128 to 127

If an int is 4 bytes (which is common), what is the range of values it can store?

-2,147,483,648 to 2,147,483,647

Which of the following are valid C++ operators?

/ %

What characters are used to denote multi-line comments in C++?

/* ... */

What characters are used to denote a single line comment in C++?

//

What is the result of the following expression? 3 * 5 % 3

0

What is the result of the following expression? 7 % 2

1

What is the value of x after the following bit of code runs? int a=5; int b=3; x=a+b*2;

11

What is the result of the following expression? 7 / 2

3

What is the result of the following expression? 1 + 5 / 2.0

3.5

What is the result of the following expression? 3 + 7 % 2

4

What is the result of the following expression? 1 + 2 * 4 - 3

6

What is the result of the following expression? 3 + 2 * 2

7

What are coding conventions?

A set of guidelines followed by a group of programmers so that all of their code is of a similar style thereby making maintenance easier

Given the ascii value 65 equates to 'A', what does the following code snippet print? char letter=65; letter++; cout << letter;

B

In C++, there are 4 categories of numeric data types. Types in which category are used to store true/false values?

Boolean

What is the result of the following expression? 5.0 * 5 % 2

Compiler error

Which of the following statements are true for comments?

Complex, difficult to understand lines should have a comment clarifying their purpose Blocks of code should be preceded with a short comment describing what they do or why it's being done

Which of the following are true about declaring variables in C/C++?

Declaring a variable allocates memory to store some type of data The data type must be specified The variable name must be specified

Which of the following are true about operators?

If an operator has two integer operands (e.g. 5/2), integer math is done and the result is an integer If an operator has two floating point operands (e.g. 5.0/2.0), floating point math is done and the result is a floating point number. If an operator has one integer and one floating point operand (e.g. 5 / 2.0), floating point math is done and the result is a floating point number.

In C++, there are 4 categories of numeric data types. Types in which category are used to store numbers that will not have decimal values, such as the number of students enrolled in a class?

Integer

Which of the following are true about the char data type?

It's used to store a letter, digit, or other symbol It takes up one byte of storage It actually stores the ASCII value (a number from 0-127) that represents the character being stored

Which of the following statements are true about order of operations?

Multiplication, division, and modulus are done first in the order they appear (from left to right), then addition and subtraction in the order they appear. Parenthesis can be used to change the order operations are done

What are identifiers?

Names of things, such as variables or functions

What does the following code snippet print?\ char letter="Z"; letter-=2; cout << letter;

Nothing, because the line "char letter="Z";" line is invalid.

Who are source code comments for?

Programmers looking at the code in the future (which may include you!)

Which of the following are true about reserved words?

Reserved words are words that have special meaning in the programming language Reserved words cannot be used for anything other than than their defined purpose Many source code editors often display reserved words in a unique color so they can easily be seen/identified

Which of the following statements are true for coding conventions?

Self-documenting code entails using meaningful variable and function names that clearly describe their contents or functions If your code is self-documenting, fewer comments are needed Comments can lie; code never lies Self-documenting code is easier to debug than code that is not self-documenting

Which of the following are true about constants?

They are declared using the const keyword They must be initialized when declared They cannot be changed once initialized Constant names are typed in ALL_CAPS by convention

In C++, what are the rules (enforced by the compiler) regarding identifiers? (i.e. variable/function names)

They must begin with a letter or _ They may contain letters, numbers, and the _ They are case sensitive

Which of the following are legal variable names (pt1)?

ThirdPlanetName PlanetName3 planetname

What is the primary purpose of adding comments to source code?

To make the code easier to understand

Which of the following are true about whitespace?

Whitespace includes blanks, tabs, and newlines Whitespace is used to make code more readable Almost all whitespace (except a single blank used to separate keywords and identifiers) is ignored by the compiler

What is the reserved word for the C++ boolean data type? (can only store true/false, which is really 1 (one) and 0 (zero))

bool

Which variable type would be best to store if a student was in class or not?

bool

Which of the following variable types could hold the value 5?

char int float double

There are 5 different signed integer types in C++. While the exact size of each isn't defined by the language (each compiler defines them), in general each of them can store a number bigger than the previous one. Which of the following lists them correctly from smallest to largest?

char, short, int, long, long long

Which of the following is the best name for a variable that will store how fast a car is currently travelling (and is an example of self-documenting code)?

currentSpeedMPH

Which variable type would be best to store the weight of a cat in pounds?

double

Which of the following would create a variable of type double and initialize it to 6 (and not generate any compiler warnings)?

double i=6; double i=6.0; double i = 6.0;

What does the following code snippet print? char letter='a'; letter = letter+4; cout << letter;

e

What is true of programming conventions?

enforced by company/instructor designed to make all code created/maintained by a group of programmers look similar so it's all easy to maintain typically include rules on indentation, comments, naming conventions, white space, etc.

Which of the following variable types could hold the value 1.23?

float double

There are 3 different floating point data types in C++, each of which can store a number significantly larger then the previous. Which of the following lists them correctly from smallest to largest?

float, double, long double

What does the modulus operator (%) do?

gives you the remainder of integer division

Which of the following are true about type conversions?

implicit conversion are those done automatically (such as when you multiple a float by an int; the system automatically converts the int to a float for the operation) explicit conversions are those the programmer specifically says to do (such as converting a float to an int so integer math would be done)

Which of the following variable types could hold the value 5000?

int double

Which of the following would create three integer variables? (named, albeit poorly, x, y, and z)

int x, y, z; int x; int y; int z;

Which of the following would create a variable and initialize it to 6 (and not generate any compiler warnings)?

int x=6; int x = 6;

Which of the following are legal variable names (pt3)?

planet_number_three Planet3of8

Which of the following are legal variable names (pt2)?

planet_number_three planetNumberThree _PlanetThree

Which of the following demonstrates how to cast myVariable to an int?

static_cast<int>(myVariable)

Which of the following variable types would be best to store the number of students in a class?

unsigned int

What variable declaration is a good example of self-documenting code?

unsigned int totalStudents;

Integer types come in both signed (can store negative and positive values) and unsigned (can only store positive values). How do signed and unsigned types differ?

unsigned types have the keyword "unsigned" before them (e.g. unsigned int)

What are common programming conventions?

use ALL_CAPS for constant names, use camelCase variable/function names

In C and C++, if you don't initialize a variable, what value will it initially hold?

whatever value happened to be sitting in the memory location allocated to the variable


Conjuntos de estudio relacionados

Nutrition/Metabolism Review McGraw Hill Connect

View Set

Making Public Policy and Democracy

View Set

PF Ch. 5 Mult. Choice Semester 1

View Set

California Driver's Handbook (Continued) 2

View Set