Programming: Chapters 1, 2, and 3

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

Write a complete program that •declares an integer variable, •reads a value from the keyboard into that variable, and •writes to standard output the variable's value, twice the value, and the square of the value, separated by spaces. Besides the numbers, nothing else should be written to standard output except for spaces separating the values.

#include <cmath> #include <iostream> using namespace std; int main() { int variable; cin >> variable; cout << variable << " " << (2 * variable) << " " << pow(variable,2) << endl;

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

#include <iostream>

Joe's Pizza Palace needs a program to calculate the number of slices a pizza of any size can be divided into. Write a complete program that will perform the following steps: 1. Ask the user for the diameter of the pizza in inches. 2. Calculate the number of slices that may be taken from a pizza of that size. 3. Display a message telling the number of slices. 4. To calculate the number of slices that may be taken from the pizza, you must know the following facts: 5. Each slice should have an area of 14.125 inches. 6. To calculate the number of slices, simply divide the area of the pizza by 14.125. 7. The area of the pizza is calculated with this formula: Area = "pi r squared" where pi is approximately 3.14159 and r is the radius (half the diameter).

#include <iostream> #include <iomanip> using namespace std; int main() { const double PI = 3.14159; // constant for PI const double sliceArea = 14.125; // constant area of each slice double diameter; double radius; double pizzaArea; int sliceCount; //Display cout << Enter pizza diameter: "; cin >> diameter; //Calculations radius = diameter / 2.0; // radius is half diameter pizzaArea = PI*radius*radius; sliceCount = static_cast<int>(pizzaArea) / sliceArea; //Display cout << "Count of slices " << sliceCount << endl; return 0; }

Write a character literal representing the digit 1.

'1'

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

'A'

Write an expression that computes the average of the values 12 and 40.

(12 + 40) / 2.0

Write an expression that computes the average of the variables exam1 and exam2 (both declared and assigned values).

(exam1 + exam2) / 2.0

The dimensions (width and length) of room1 have been read into two variables: width1 and length1. The dimensions of room2 have been read into two other variables: width2 and length2. Write a single expression whose value is the total area of the two rooms.

(width1 * length1) + (width2 * length2)

A comment starts with what characters?

//

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;

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

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

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

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

0

Convert the binary value 1101 to decimal.

13

What is the outcome of the following expression? ( 19 - 3 ) + ( 2 + 2 ) / 4

17

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

2

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

3.14159

What is the value of number after the following statements execute? int number = 10; number += 5; number -= 2; number *= 3;

39

What is the outcome of the following expression? ( ( 19 - 3 ) + ( 2 + 1 ) ) / 4

4

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

4

What is the outcome of the following expression? 6 + 16 % 3 - 2

5

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

6

Which line in the following program will cause a compiler error? 1 #include <iostream> 2 using namespace std; 3 4 int main( ) 5 { 6 const int MY_VAL = 77; 7 MY_VAL = 99; 8 cout << MY_VAL << endl; 9 return 0; 10 }

7

Which line in the following program will cause a compiler error? 1 #include <iostream> 2 using namespace std; 3 4 int main( ) 5 { 6 const int MY_VAL; 7 MY_VAL = 77; 8 cout << MY_VAL << endl; 9 return 0; 10 }

7

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

When a program uses the setw manipulator, the iosetwidth header file must be included in a preprocessor directive. True False

False

Before a variable is used it must be a. defined b. declared c. included d. evaluated e. assigned

a. defined

Which of the following lines contains a valid, though not necessarily accurate, comment? a. int twoPi = 3.14159; /* holds the value of two times pi */ b. int twoPi = 2*3.14159; /* holds the value of two times pi //* c. int twoPi = 2*3.14159; / / *holds the value of 6 //* d. double twoPi = 2*3.14159; /* // holds the value of two time pi */ [comment] //

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

When a program is not running, it is stored a. on a disk b. in level-2 cache (L2) c. in main memory d. on the ethernet or wifi

a. on a disk

This stream manipulator forces cout to show the decimal point even for integer values. a. showpoint b. right c. fixed d. exp e. left

a. showpoint

In the following C++ statement, what will be executed first according to the order of precedence? result = 6 - 3 * 2 + 7 - 10 / 2 ; a. 10 / 2 b. 3 * 2 c. 2 + 7 d. 6 - 3 e. 7 - 10

b. 3 * 2

At each step of its operation, the input to a Central Processing Unit is a. a program b. an instruction c. main memory d. a control unit

b. an instruction

To have access to mathematical functions such as pow(), sqrt(), and tan() you must include this C++ header file. a. Math b. cmath c. System.Math d. math.h e. GMATH

b. cmath

Application software a. processes applications for jobs, school admission, etc. b. is any software that runs with the support of the operating system c. was invented by Microsoft d. is applied to the computer for the purpose of running the operating system e. none of the above

b. is any software that runs with the support of the operating system

A binary digit a. is either positive or negative. b. is zero or one. c. requires one byte of storage. d. is 2. e. is none of the above.

b. is zero or one

Which statement is not true: a. machine languages can be used to express algorithms b. machine languages can be used to write programs that can run on any machine c. machine language consists of zeros and ones d. machine language is produced by compilers

b. machine languages can be used to write programs that can run on any machine

Which statement is equivalent to the following? number += 1; a. number = 1; b. number = number + 1; c. number + 1; d. None of these

b. number = number + 1;

Declare a variable hasPassedTest, and initialize it to true.

bool hasPassedTest; hasPassedTest = true;

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

bridgePlayers += 4;

Which of these is not a programming language? a. C b. C++ c. HTML d. Java

c. HTML

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

prefix + suffix

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

c. hasFever

Which of the following is NOT a C++ keyword? a. using b. int c. main d. namespace e. return

c. main

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

c. mightBePrime

A compiler a. maintains a collection of programs b. tests a program's logic c. translates source code into executable code d. translates executable code to machine code

c. translates source code into executable code

RAM, random-access memory, is called that because a. it is optimized for random number processing b. accesses are randomized to prevent clogging the control lines c. you can pick two random locations and it will take the same time to access the data d. when power is turned off, its contents are lost and replaced by random bits e. none of the above

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

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; cin >> age; cout << "The age of "; cout << name; cout << " is "; cout << age; cout << ".";

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. For example, if your code read in "Rachel" it would print out "Greetings, Rachel".

cin >> name; cout << "Greetings, " << name << endl;

Write a statement that reads an integer value from standard input into the integer variable number.

cin >> number;

Write code that would reads a number from the keyboard into the integer variable number.Then print out the variable 's value , thrice the value , and the cube of the value , separated by spaces. For example if I enter 2 , the code should display 2 6 8

cin >> number; cout << number << " " << number * 2 << " " << number * number * number;

Write a statement that reads a floating point (real) value from standard input into the double variable temperature.

cin >> temperature;

Write a statement that reads 5 successive integers into these variables that have already been declared: x1 x2x3x4 x5. Then write a statement that prints each out on its own line so that they form a right-justified column with a 5-digit width. If any of the integers are 5-digits in size, then they will start at the very beginning of their lines. For example: |54213 | 8713 | 23 | 147 | 15

cin >> x1 >> x2 >> x3 >> x4 >> x5; cout << setw(5) << x1 << endl; cout << setw(5) << x2 << endl; cout << setw(5) << x3 << endl; cout << setw(5) << x4 << endl; cout << setw(5) << x5 << endl;

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

An error in a program that involves a violation of language rules will be detected at ____ time.

compile

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

const int MonthsInYear = 12;

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

cout << " " << message << endl;

Given an int variable datum that has already been declared, write a statement that reads an integer value from standard input into this variable.

cout << "Enter an integer "; cin >> datum;

Assume that we have a string type variable called name and short type variable called age. Write code that asks the user and accepts the user entered values into the variables name and age. Then print the message "The age of is ." For example, if your code read in "Sara" and 17, then your code would print out "The age of Sara is 17.".

cout << "Enter name"; cin >> name; cout << "Enter age"; cin >> age; cout << "The age of " << name << " is " << age << endl;

Write a statement that reads a floating point (real) value from standard input into temperature. Assume that temperature. has already been declared as an double variable.

cout << "Enter the temperature "; cin >> temperature;

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 price is an integer variable that contains the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents". So, if the value of price was 4321, your code would print "43 dollars and 21 cents".

cout << price / 100 << " dollars and " << price % 100 << " cents.";

Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents". So, if the value of price was 4321, your code would print "43 dollars and 21 cents". If the value was 501 it would print "5 dollars and 1 cents". If the value was 99 your code would print "0 dollars and 99 cents".

cout << price/100 << " dollars and " << price%100 << " cents";

Assume that classCount 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(10) << classCount;

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(10) << m;

Assume that x is a double variable that has been initialized. Write a statement that prints it out, guaranteed to have a decimal point, but without forcing scientific (also known as exponential or e-notation).

cout << showpoint << x;

You can use these to override the rules of operator precedence in a mathematical expression. a. // double slashes \\ b. [ square brackets ] c. < angle brackets > d. ( parentheses ) e. { braces }

d. ( parentheses )

Which of the following lines does NOT consist of (a) valid, though boastful, comment(s)? a. // /* This is a */ First Rate Program b. //**// This is a //**// First Rate Program //**// c. //* This is a //*// First Rate Program //*// d. /* This is a //*// First Rate Program //*//

d. /* This is a //*// First Rate Program //*//

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

prefix = prefix + suffix;

An operating system a. is the chief hardware unit in a computer b. is loaded into the computer each time it needs to carry out an operation c. ensures that programs will not run on the computer at the same time d. allocates resources like memory to programs that are running e. all of the above

d. allocates resources like memory to programs that are running

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

d. can be anything the programmer wants to write

What best defines a "programming language"? a. it allows us to control a computer b. it allows us to make a calculation c. it allows us to execute a program d. it allows us to express an algorithm

d. it allows us to express an algorithm

Which of the following is true? a. there is exactly one and only one statement on each line of code b. there can be more than one statement on a line, but a statement must not extend over more than one line c. statements can extend over more than one line but there must not be more than one statement on a line. d. there are no language rules regarding statements and line in general

d. there are no language rules regarding statements and line in general

A bit is a. a metallic rod inserted into a horses mouth to control it while riding b. a small amount of data c. an alternative term for byte d. an electronic device used in computers e. a binary digit, like 0 or 1

e. a binary digit, like 0 or 1

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;

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.

float(tickets1 + tickets2 + tickets3) / (class1 + class2 + class3)

________ reads a line of input, including leading and embedded spaces, and stores it in a string object.

getline

Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the number of single dollars that would have to be paid.

price / 100

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

principal % divisor

Assume that brushCount is an integer variable containing the number of paint brushes in a box and that boxCount is an integer variable containing the number of boxes in a store. Write an expression that calculates the average number of brushes per box.

int avgCount; avgCount = brushCount / boxCount;

Declare an integer variable named degreesCelsius

int degreesCelsius;

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, companyRevenue = 5666777, firstClassTicketPrice = 6000, totalPopulation = 1222333;

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

Division by zero when the program is executing is an example of a ____ error.

runtime

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 using the increment operator to increase the value of num_items (an already declared integer variable) by 1.

num_items = num_items + 1;

Write a statement using a compound assignment operator to multiply num_rabbits by 4, changing the value of num_rabbits (num_rabbits has already been declared and initialized).

num_rabbits *= 4;

Write a statement using the increment operator to increase the value of the integer variable num_steps by 10.

num_steps = num_steps + 10;

Write a statement using a compound assignment operator to cut the value of pay in half (pay is an integer variable that has already been declared and initialized).

pay /= 2;

Assume that children is an integer variable containing the number of children in a community and that families is an integer variable containing the number of families in the same community. Write an expression whose value is the average number of children per family.

static_cast<double>(children) / families

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.

static_cast<double>(distance) / speed

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

string empty;

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

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

string str; str = "Hello";

The rules that govern the correct order and usage of the elements of a language are called the ____ of the language.

syntax

Write a statement using a compound assignment operator to calculate half price of a given ticket price stored in the variable.

ticketPrice /= 2;

Assume the dimensions (width and length) of one room have been read into two float variables (width1 and length1), and the dimensions of a second room have been read into two other variables(width2 and length2). Write a single expression that will calculate the total area of the two rooms and store it in a variable named totalArea.

totalArea = (width1 * length1) + (width2 * length2);

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

unsigned int number_of_children;

Write a statement using a compound assignment operator to add 5 to val (an integer variable that has already been declared and initialized).

val += 5;

A location in memory used for storing data and given a name in the computer program is called a ____ because the data in the location can be changed.

variable

Write the C++ expression for the following algebraic formula: y = x4

y = pow(x, 4); (For Moodle) y = z z z z;


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

Human growth and development Nemcc

View Set

Chapter 7: Electrolyte Imbalances Multiple choice

View Set