CMPSC 201 Exam 1

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

Define a macro MAX_STUDENTS to be the value 100.

#define MAX_STUDENTS 100

How many bytes are needed to store: "n" ?

2

combined assignment operators are also known as

compound operators arithmetic assignment operators

A file not being found is a _____ error

runtime

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

x % 10

Write an expression that evaluates to true if the integer variable x contains an even value , and false if it contains an odd value .

x % 2 == 0

equivalent of x += 5; y -= 2; z *= 10; a /= b; c %= 3;

x = x + 5; y = y - 2; z = z * 10; a = a / b; c = c % 3;

RAM, random-access memory, is called that because

you can pick any two random locations and it will take the same time to access the data

Given a string variable address, write a string expression consisting of the string "http://" concatenated with the variable's string value. So, if the value of the variable were "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com".

"http://"+address

Define a macro RIGHT to represent the true value.

#define RIGHT-true OR #define RIGHT 1; OR #define RIGHT 1 OR #define RIGHT 3 //any integer number above 0

Write a complete program that -declares an integer variable, -reads a value from the keyboard into that variable, and -writes to standard output the square of the variable's value.

#include <iostream> using namespace std; int main() { int intvar; cin>>intvar; intvar=intvar*intvar; cout<<intvar<<endl; return 0; }

Write a character literal representing a comma.

' , '

Write a character literal representing the digit 1.

'1'

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

Three classes of school children are selling tickets to the school play. The number of tickets sold by these classes, and the number of children in each of the classes have been read into these variables:tickets1, tickets2, tickets3 and class1, class2, class3. Write an expression for the average number of tickets sold per school child.

(double)(ticket1+ticket2+ticket3)/(class1+class2+class3)

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 upper-case letter.

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

How many bytes are needed to store: "" ?

1

How many bytes are needed to store: '\n' ?

1

Four Types of Control Structures

1. Sequence 2. Branched 3. Loops 4. Functions

How many bytes are needed to store: "\n", ?

2

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

255

How many bytes are needed to store: "\\n" ?

3

What's the difference in UNICODE value between '3' and '0'?

3

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

3.14159

44 % 8

4

What's the difference in UNICODE value between 'e' and 'a'?

4

'ASCII Values to use for COMPARING CHARACTERS: 'A' to 'Z' 'a' to 'z' blank period '0' to '9'

65-90 97-122 32 46 48-57

A byte consists of ___ bits.

8

stream extraction operator

>>; takes information from a stream and puts it into a variable

The data type int represents what kind of data?

A whole number such as 57.

relational expressions are also known as

Boolean expression

T/F: The control expression of a switch statement may be of type double.

False

T/F: char is a valid identifier in C++

False, its a keyword

operands

The data that operators work with.

T/F: 5 * 2 > 7 && false == false.

True

T/F: The result of an integer division in C++ is a truncated result.

True

The word in the brackets of an include directive specifies

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

A while loop is always ______

a pre-test

initialization

a value is assigned to a variable as part of the variable's definition

A byte in memory is identified by a unique number called its

address

An operating system

allocates resources like memory to programs that are running

Declare a variable hasPassedTest, and initialize it to true.

bool hasPassedTest=1;

flag

boolean or int variable that signals when a condition exists

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

bridgePlayers += 4;

Write the declaration of a char array named smallWord suitable for storing 4-letter words such as "love", "hope" and "care".

char smallWord[5];

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

Statement that reads a word from standard input into firstWord. Assume that firstWord has already been declared as an char array large enough to hold a 50-letter word.

cin.get(firstWord, 51);

Assume that name has been declared suitably for storing names (like "Misha", "Emily" and "Sofia"). Write some code that reads a value into name then prints the message "Greetings, NAME" where NAME is replaced the value that was read into name.

cin.get(name,'Mark'); cout<<"Greeting,"<<name<<endl;

Assume that a,b and c are char variables have been declared. Write some code that reads in the first character of the next line into a, the first character of the line after that into b and the first character of the line after that into c. Assume that the lines of input are under 100 characters long.

cin.ignore(100,'\n')>>a; cin.ignore(100,'\n')>>b; cin.ignore(100,'\n')>>c;

Assume that c is a char variable has been declared. Write some code that reads in the first character of the next line into c. Assume that the lines of input are under 100 characters long.

cin.ignore(20,'\n'); cin.get(c);

Assume that name and age have been declared suitably for storing names (like "Abdullah", "Alexandra" and "Zoe") and ages respectively. Write some code that reads in a name and an age and then prints the message "The age of NAME is AGE." where NAME and AGE are replaced by the values read in for the variables name and age. For example, if your code read in "Rohit" and 70 then it would print out "The age of Rohit is 70.".

cin>>name>>age; cout<<"The age of "<<name<<" is "<<age<<".";

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

const int MonthsInYear=12;

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 ; ***MUST have space in between is: and the "

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.

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

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

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

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

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

cout<<message<<endl;

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<<setw(11)<<m<<endl;

Declare k, d, and s so that they can store an integer, a real number, and a small word (under 10 characters). Use these variables to first read in an integer, a real number, and a small word and print them out in reverse order (i.e., the word, the real, and then the integer) all on the same line, separated by EXACTLY one space from each other. Then, on a second line, print them out in the original order (the integer, the real, and the word), separated again by EXACTLY one space from each other.

double k, d; string s; cin>>k>>d>>s; cout<<s<<" "<<d<<" "<<k<<"\n"; cout<<k<<" "<<d<<" "<<s<<"\n"; notes: use STRING , all variables can be in one statement.

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

double temp; temp=bestValue; bestValue=secondBestValue; secondBestValue=temp;

relational expressions have a ____ precedence than the assignment operator. (Ex: trueValue = x < y;)

higher

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.

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

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

keyword

A _____ operator such as || may be used to combine relational expressions

logical

the precedence of the combined assignment operators is ____ than that of regular math operators.

lower

Every C++ program must contain a

main function

type casting allows you to perform _______

manual data type conversion

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

num_items++;

Write an expression that concatenates the string variable suffix onto the end of the string variable prefix .

prefix + suffix

What will be the output of the following code fragment? int x; x = 1; cout << "remainder"; switch ((x%7)) { case 0: cout<<"0"; case 1: cout<<"1"; case 2: cout<<"2"; case 3: cout<<"3"; }

remainder123

What happens when a programmer tries to compile and run the code fragment below? int x = 0; int y; y = 2/x;

runtime error is generated

integer data types from smallest to largest

short int unsigned short int int unsigned int long int unsigned long int long long int unsigned long long int

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

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

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

string line; getline(cin, line);

auto key word to declare variables

tells the compiler to determine the variable's data type from the initialization value. used to simplify syntax of complex declarations

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;

type coercion

the automatic conversion of data from its existing type into the type required for an operation. promoted: value converted to a higher data type demote: converted to a lower data type


Ensembles d'études connexes

accounting chapter 12 terms/concept questions

View Set

Chapter 1 History Test: A New World

View Set

CH 43 Assisting with Eye and Ear Care

View Set

Do I Know This Already Chapter 4

View Set

Cognitive Psych EXP3604, Ch. 1-4

View Set

Matematik procent del 1 Hvad er 25% af 100

View Set

Chapter 9 Section Quiz: Social Stratification in the United States

View Set

Chapter 15 Italian Renaissance/World History and Geography

View Set