CSCI 121 Chapter 1-3
Which of the following is correct? #include "iostream" #include <iostream> #include "iostream"; #include <iostream>;
#include <iostream>
**Write a statement to declare a double type variable named hours then write another statement to assign a value from keyboard to variable hours.
#include <iostream> using namespace std; int main() { double hours; cin >>hours; cout <<"Hours value is " << hours; cout << endl; return 0; }
**Write a for statement to calculate the sum of 1+2+3+...+n and assign the result to an int type variable named sum . Assume that sum and int type variable n have been declared and n has been initialized. You may need to initialize the sum to be zero first.
#include <iostream> using namespace std; int main() { int n = 10; int sum = 0; int counter = 1; while(counter != n) sum += counter++; cout << "The sum of " << n << " is " << sum; cout << endl; return 0; }
**Write an if-else statement to print out EVEN if x is even; print out ODD if x is odd. Here x is an int type variable.
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number:"; cin >> num; if(num%2 == 0) cout<<"EVEN"; else cout<<"ODD"; return 0; }
**Write a while statement to print digits of an int type variable x reversely. For example, if int x = 3584 , it should print out 4 lines as: 4 8 5 3
#include <iostream> using namespace std; int main() { int x = 3584; while(x != 0) { printf("%d\n",x%10); x = x /10; } return 0; }
Write a program that contains statements that output the value of five or six variables that have been declared, but not initialized. Compile and run the program. What is the output?
#include <iostream> using namespace std; int main () { int first, second, third, fourth, fifth, sixth; cout << first << " " << second << " " << third << " " << fourth << " " << fifth << " " << sixth << endl; return 0; }
Write a complete C++ program that reads in two whole numbers and outputs their sum. Be sure to prompt for input, echo input, and label all output.
#include <iostream> using namespace std; int main () { int n1, int n2, sum; cout <<"input two whole numbers\n"; cin >> n1 >> n2; sum= n1 + n2; cout << "The sum of " << n1 << " and " << n2 << " is " << sum << endl; return 0; }
Write a complete C++ program that reads two whole numbers into two variables of type int and then outputs both the whole number part and the remainder when the first number is divided by the second. This can be done using the operators / and &
#include <iostream> using namespace std; int main () { int number1, int number2; cout << "Enter two whole numbers \n"; cin >> number1 >> number2; cout <<number1 << "divided by" << number2; << "equals" << (number1/number2)<< endl; << "with a remainder of" << (number1%number2) << endl; return 0; }
Write a complete C++ program thats asks the user for a number of gallons and then outputs the equivalent number of liters. There are 3.78533 liters in a gallon. Use a declared constant.
#include <iostream> using namespace std; int main() { const double Liters_per_gallon = 3.78533; double gallons, liters; cout << "Enter the number of gallons: \n"; cin >> gallons; liters= gallons * Liters_per_gallon; cout <<There are " <<liters << " in " << gallons << " gallons. \n" return 0; }
Write a complete C++ program that writes the phrase Hello World to the screen. The program does nothing else
#include <iostream> using namespace std; int main() { cout << "Hello World \n"; return 0; }
Write a complete C++ program that outputs the numbers 1 to 20, one per line. The program does nothing else
#include <iostream> using namespace std; int main() { int n =1 while (n<=20) { cout << n << endl; n++; } return 0; }
**Write a do-while statement to print out I Love Programming repeatedly. Every time after the printing, the program will ask the user if he/she wants to continue. If the user input Y or y , then a new line of "I Love Programming" will be printed out.
#include<iostream> using namespace std; int main() { char choice; do{ cout<<"I Love Programming"<<endl; cout<<"Do you want to continue?(Y/N): "; cin>>choice; }while(choice =='y' || choice == 'Y'); return 0; }
What is the output of the following program lines when embedded in a correct program that declares number to be of type int? number = (1/3) *3; cout << "(1/3) *3 is equal to " << number;
(1/3) * 3 is equal to 0 Since 1 and 3 are of the type int. the / operator performs integer division, which discards the remainder, so the value of 1/3 is 0 not 0.333. This makes the value of the entire expression 0 *3, which of course is 0
Consider a quadratic expression x^2 - x - 2 Describe where this quadratic is positive, involves describing a set of numbers that are either less than the smaller root (-1) or greater than the larger root (+2) Write a C++ boolean expression that is true when this formula has positive values.
(x<-1) || (x>2)
Consider the quadratic expression x^2 - 4x+3 Describing where this quadratic is negative involves describing a set of numbers that are simultaneously greater than the smaller root (+1) and less than the larger root (+3). Write a C++ Boolean expression that is true when the vaue of this quadratic is negative
(x>+1) && (x<3)
What is the output produced by the following (when embedded in a correct program with x declared to be of type int?) x=-42; do { cout << x << endl; x=x-3; } while (x>0);
-42
What is the output of the following program lines when embedded in a correct program that declares month, day, year, and date to be of type string? month = "0.3"; day = "0.4"; year = "0.6" date = month + day + year; cout << date << endl;
030406 The strings are linked together (concatenated) with the + operator
What is the output of program int n =1 do cout << n << " "; while (+nn <=3);
1 2 3
What is the output of the following int n =1; do cout << n << " "; while (n++ <=3);
1 2 3 4
What are the six steps of the software life cycle
1. Analysis and specification of tasks (problem definition) 2. Design of the software (object and algorithm design) 3. Implementation (coding) 4. Testing 5. Maintenance and evolution of system 6. Obsolescence
Five components of a computer
1. input device 2. output device 3. CPU 4. Main memory 5. secondary memory
What is the value of x after the following statements? double x; x = 0; x += 3.0 * 4.0; x -= 2.0;
10
What is the output produced by the following (when embedded in a correct program with x declared to be of type int) x=10; do { cout << x << endl; x= x-3; } while (x > 0);
10, 7, 4, 1
What is the output produced by the following (when embedded in a correct program with x declared to be of type int)? x=10; while (x>0) { cout << x << endl; x= x-3; }
10, 7, 4, 1
What is the output of this loop? int n =1024; int log=0 for (int =1. i< n; i=1*2); log++ cout << n << " " << log << endl;
1024
What is the output of the following input count = 3; while (--count > 0) cout << count << " "
2 1
What is the output of the following (when embedded in a complete program) int count = 3; while (count-- > 0) cout << count << " ";
2 1 0
What is the output of the following for (int count =1; count < 5; count++) cout << (2* count) << " " ;
2 4 6 8
Given the following declaration and output statment. Assume that this has been embedded in a correct program and is run. What is the output? enum direction { N = 5, S = 7, E = 1, W}; //.. cout<< W << " " << E << " " << S << " " << N << endl;
2, 1, 7, 5
What is the output of the following for (double sample = 2; sample > 0; sample = sample - 0.5) cout << sample << " "
2.000000 1.500000 1.000000 0.500000
Convert each of the following mathematical formulas to a C++ expression: 3x 3x+y x+y/7 3x + y/ z +2
3 * x 3 * x +y (x+y)/7 note that x+y/7 is not correct (3 * x +y)/ (z + 2)
What is the value of x after the following statements? int x; x = 15/4; 15 3 4 3.75
3 is the answer since it gets rounded down because data type is an integer.
Which of the following value cannot be assigned to x if we have the following declaration statement? int x; 3.14 -1 0 1
3.14 since int is for whole numbers only. We would use double in this case
What is the output of the following code? float value; value = 33.5; cout << value << endl;
33.5
What is the output of following int n = 5; while (--n > 0) { if ( n == 2) exit (0); cout << n << " "; } cout << "End of Loop. ";
4 3
What is the output of following int n = 5; while (--n > 0) { if ( n == 2) break; cout << n << " "; } cout << "End of Loop. ";
4 3 End of loop
Given the following code fragment and the input value of 4.0, what output is generated? float tax; float total; cout << "enter the cost of the item \n"; cin >> total; if (total >=3.0) { tax=0.10; cout << total+(total*tax) <<endl; } else { cout << total << endl; }
4.4
What output is produced by the following code, when embedded in a complete program? int number = 22; { int number =42; cout <<n number << " "; } cout << number;
42 22
A byte consists of how many bits?
8
What is the operating system's purpose
Allocate the computers resources to different tasks the computer must accomplish
What is the value of x after the following statements? float x; x = 15%4; 3.75 4.0 3.0 60
Answer is 3.0 4 goes into 15 3 times and has a remainder of 3. since the remainder is 3 the answer is 3. % represents remainder
What is a boolean expression?
Any expression that is either true or false. An if-else statement always contains a Boolean_Expression
Who developed C++?
Bjarne Stroustrup
If the following statement were used in a c++ program, what would it cause to be written on screen? cout << "C++ is easy to understand. ";
C++ is easy to understand.
The anagram CPU stands for
Central processing unit
What output will be produced by the following code, when embedded in a complete program? int firstChoice = 2; switch (firstchoice + 1) { case 1: cout << "Roast beef \n"; break; case 2: cout << "Roast worms \n"; break; case 3: cout << "Chocolate ice cream \n"; case 4: cout << "Onion ice cream \n"; break; default: cout << "Bon appetit! \ n";
Chocolate ice cream onion ice cream
The opposite of less than is greater than. True or false
False. It is greater than or equal to
What is the output of the following for (int n = 10; n >0; n = n-2) { cout << "Hello "; cout << n << endl; }
Hello 10 Hello 8 Hello 6 Hello 4 Hello 2
What does the following print to the screen? cout << "Hello Students/n";
Hello Students/n
#include <iostream> is an example of what and what does it do?
It is a directive and it tells the compiler where to find information about certain items that are used in your program
What output will be produced by the following code, when embedded in a complete program? int firstChoice = 3; switch (firstchoice + 1) { case 1: cout << "Roast beef \n"; break; case 2: cout << "Roast worms \n"; break; case 3: cout << "Chocolate ice cream \n"; case 4: cout << "Onion ice cream \n"; break; default: cout << "Bon appetit! \ n";
Onion ice cream
RAM stands for
Random Access Memory
What output will be produced by the following code, when embedded in a complete program? int firstChoice = 1; switch (firstchoice + 1) { case 1: cout << "Roast beef \n"; break; case 2: cout << "Roast worms \n"; break; case 3: cout << "Chocolate ice cream \n"; case 4: cout << "Onion ice cream \n"; break; default: cout << "Bon appetit! \ n";
Roast worms
What output would be produced by the following two lines ( when embedded in a complete and correct program)? //cout << "Hello from"; cout << "Self -test Exercise";
Self test exercise
What are keywords
Special class of identifiers written in a different font such as int or double
What output will be produced by the following code, when embedded in a complete program? int x = Some_constant; cout << Start \n"; if (x <100) cout << "First Output. \n"; else if ( x > 100) cout << "Second output. \n"; else cout << x << endl; cout << "End \n";
Start 100 End
What output will be produced by the following code int x = 2 cout << "start\n"; if (x<=3) if (x!=0) cout << "Hello from the second if. \n"; else cout << "Hello from the else. \n"; cout << "End \n"; cout << "Start again \n"; if (x > 3) if (x !=0) cout << "Hello from the second if \n"; else cout << "Hello from the else \n"; cout << "End again \n";
Start Hello from the second if End Start again End again
What output will be produced by the following code when embedded in a complete program int x = 200; cout << "Start \n"; if (x< 100) cout << "First output \n"; else if (x >10) cout << "Second Output \n"; else cout << "Third output \n"; cout << "End \n";
Start Second Output End
Given the following code fragment, what is the final value of y? int x, y, x=-1 y=0 while (x<=3) { y+= 2; x-=1; }
The answer is 10. The program will keep running until x >=4. The program will run a total of 5 times each time increasing y by 2.
Define: software
The collection of programs used by a computer
What would be the data for a program that assigns letter grades to students in a class.
The grades for each student's tests and assignments
define linking
The process of combining object code for program with the object code for routines
What is the meaning of the following statement? totalPeas = number0fPods * peasPerpod;
The statement says to multiply the two variables and assign the result to the variable
What is the meaning of the following statement? cin >> peasPerPod;
The statement tells the computer to read the next number that is typed in the keyboard and assign it to the variable peasPerPod.
What would be the data for a program to add two numbers
The two numbers being added
Rules for choosing a variable name
The variable must start with either a letter or the underscore symbol and all the rest of characters must be letters, digits, or underscore symbol.
What does n++ and n-- indicate?
These are increment and decrement operators. If n is a variable of type int. then n++ increases the value of n by one and n-- decreases the value of n by one
What is the meaning of \n as used in the following statement (which appears in display 1.8) cout << "Enter the number of peas in a pod: \n";
To start a new line
The following statement is legal: cout >> "Hello, my name is Bill\n"; True or false
True
What does a break statement do
Used to exit a loop or to terminate a case in a switch statement
Difference between a while statement and do statement
With a do while statement, the loop body is always executed at least once. For a while statement there can be conditions under which the loop body is not executed at all.
What is a high level language. Give examples of some.
Written in more english-like format and is translated by a compiler into a machine -language program that the computer can directly understand and execute. Examples are java, C, C++, python
An algorithm is what
a finite set of steps to solve a problem.
define compiler
a program that translates high level language program into a machine-language program that the computer can directly understand and execute
Given the following fragment that purports to convert from degrees Celsius to degrees Fahrennheit, answer the following questions: double c =20; double f; f = (9/5) * c +32.0; a. What value is assigned to f b. Explain what is actually happening and what the programmer likely wanted c. Rewrite the code as the programmer intended
a) 52 b) 9/5 has int value of 1; since numerator and denominator are both int, integer division is done; the fractional part is discarded. c) f = 1.8 * c +32.0;
What i anything, is wrong with the following #include directies a. #include <iostream > b. #include < iostream> c. #include <iostream>
a. The extra space after the iostream file name causes a file-notfound error message b. The extra space before the iostream file name causes a file-notfound error message c. This is correct
For each of the following situtations, tell which type of loop (while, do-while, or for) would work best a. summing a series such as 1/2 + 1/3 + 1/4 +1/5 b. Reading in the list of exam scores for one student c.Reading in the number of days of sick leave taken by employees in a department d. Testing a function to see how it performs for different values of its arguments
a. for b. while c. do while
definition of byte
an eight bit portion of memory
What can char hold?
any single character on a keyboard such as a symbol or letter.
What is the output of the following program lines when embedded in a correct program that declares all variables to be of type char? a = 'b'; b = 'c'; c = a; cout << a << b << c << 'c';
bcbc
What output will be produced by the following code, when embedded in a complete program? int firstChoice = 4; switch (firstchoice + 1) { case 1: cout << "Roast beef \n"; break; case 2: cout << "Roast worms \n"; break; case 3: cout << "Chocolate ice cream \n"; case 4: cout << "Onion ice cream \n"; break; default: cout << "Bon appetit! \ n";
bon appetit
Which of the following lines correctly reads a value from the keyboard and stores it in the variable named myFloat? cin >> myFloat; cin << myFloat; cin >> "myFloat"; cin >> myFloat >> endl;
cin >> myFloat;
Which of the following is an input statement? cin >> x; int x; #include <iostream> cout << x;
cin >> x
count +=2 is equivalent to what?
count = count +2
Give an input statement that will fill the variable the_number (of type int) with a number typed in at the keyboard. Precede the input statement with a prompt statement asking the user to enter a whole number.
cout << "Enter a whole number and press return: "; cin >> the_number;
Give an output statement that will product the following message on the screen The answer to the question of Life, the Universe, and Everything is 42.
cout << "The answer to the question of \n" << "Life, the Universe, and Everything is 42. \n";
**Write one statement that prints out "No pain, no gain." The output need to be in two lines as (quotes and indents should be print out): "No pain, no gain."
cout << "\"No pain,\n \t no gain.\"" << endl;
Give an output statement that produces the new line character and tab character
cout << endl << "\t"
What statements should you include in your program to ensure that when a number of type double is output, it will output in ordinary notation with three digits after the decimal point?
cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision (3);
Which of the following is NOT part of the Software Life Cycle? analysis design data entry implementation testing
data entry
definition of bit
digit that can assume only the values 0 or 1
A loop that always executes the loop body at least once is known as a ________ loop.
do-while
Three properties of Object Oriented Programming (OOP)
encapsulation- implication of the description of objects inheritance-writing re-useable program code polymorphism- single name could have multiple meanings
Given the following declaration and output statement, assume that this has been embedded in a correct program and is run. What is the output enum direction { N, S, E, W}; //.. cout<< W << " " << E << " " << S << " " << N << endl;
enum constants are given default values starting at 0, unless otherwise assigned. The constants increment the previous value by 1. The output is 3 2 1 0
A mistake that is a direct violation of the syntax rules will generate a compiler ________
error message
Write a loop that will write the world Hello to the screen ten times
for (int count = 1; count <==10; count++) cout << "hello \n":
Write a multiway if-else statement that classifies the value of an int variable n into one of the following categories and writes out an appropriate message n < 0 or 0<= n <= 100 or n > 100
if ( n< 0) cout << n << "is less than zero. \n"; else if ((0<= n) && (n <=100)) cout << n << " is between 0 and 100 (inclusive). \n"; else if (n > 100) cout << n << " is larger than 100. \n";
Write an if else statement that outputs the word passed provided the value of the variable exam is greater than or equal to 60 and the value of the variable programs_done is greater than or equal to 10. Otherwise, the if-else statment outputs the word failed. The variables exam and programs_done are both of type int
if ((exam >= 60) && (programs_done >= 10)) { cout <<"Passed"; } else { cout <<"Failed"; }
Write an if-else statement that outputs the word Warning provided that either the value of the variable temperature is greater than or equal to 100 or the value of the variable pressure is greater than or equal to 200 or both. Otherwise the if else statement outputs the word ok. The variables are both type int
if ((temperature >=100) || (pressure >=200)) { cout<<"warning"; } else { cout<<"ok"; }
Suppose savings and expense are variables of type double that have been given values. Write an if-else statement that outputs the word solvent, decreases the value of savings by the value of expenses, and sets the value of expenses to 0, provided that savings is at least as large as expenses. If, however, savings is less than expenses, the if-else statement simply outputs the word bankrupts and does not change the value of any variables
if (savings >= expenses) { savings = savings - expenses; expenses = 0; cout <<"Solvent"; } else { cout << "bankrupts"; }
Write an if-else statement that outputs the word high if the value of the variable score is greater than 100 and low if the value of score is at most 100. The variable score is of type int
if (score > 100) cout <<"High"; else cout<<"Low";
Given the following code fragment, which of the following expressions is always TRUE? int x; cin >> x; if( x < 3) if( x==1) if( (x / 3) >1 ) if( x = 1)
if (x=1) remember = is used to assign a value to a variable == asks the question is value of " " equal to value of " b" if yes return true. if not return false
What is the final value of x after the following fragment of code executes int x=0; do { x++; } while (x>0);
infinite loop
What is the output produced by the following (when embedded in a correct program with x declared to be of type int?) x= 10; while (x>0) { cout << x << endl; x= x+3; }
infinite loop 10, 13, 16...
Give the declaration for two variables called count and distance. count is of type int and is initialized to zero. distance is of type double and is initialized to 1.5
int count = 0; double distance = 1.5;
Give the declaration for two variables called feet and inches. Both variables are of type int and both are to be initialized to zero in the declaration. Use both initialization alternatives.
int feet = 0, int inches = 0; int feet(0), inches(0);
Whats the structure of man function
int main () {
Which of the following code segment declare an integer variable x and assign value 5 to it correctly? This is a multiple answer question! int x = 5; int x; x = 5; int x = 5; int x; x = 5; int x, x = 5; int x = 5
int x = 5; int x; x = 5; int x = 5; int x; x = 5;
What output will be produced by the following code, when embedded in a complete program? int extra= 2; if (extra < 0) cout << "small"; else if (extra = = 0) cout << "medium"; else cout <<"large";
large
Give a C++ statement that will increase the value of the variable length by 8.3. The variable length is of type double
length = length +8.3;
if-else statements that are inside other if-else statements are said to be ________.
nested
A _____________ consists a number of computers connected so that they may share resources such as printer and may share information.
network
Computers that are interconnected are known as a ________.
network
The translated machine-language program that is output by the compiler is called the ___________________
object program
cout << "How many items would you want?\n"; is an output statement. is an input statement. is a variable declaration. is a program.
output statement
Give a C++ statement that will change the value of the variable product to its old value multiplied by the value of the variable n. The variables are all of type int
product = product * n;
The set of instructions that a computer will follow is known as
program
Which of the following is NOT a valid identifier? myInteger return myInt myInteger total3
return
What punctuation signifies the end of a C++ statement?
semicolon
The high level language that is input to a compiler is called the ________________
source program
Give a C++ statement that will change the value of the variable sum to the sum of the values in the variables n1 and n2. The variables are all of type int
sum = n1 + n2;
Name the types of program errors with their definition
syntax error: violation of grammar rules of language such as missing a semicolon runtime error: errors that the system detects only when a program is run. Many have to do with numeric calculations such as trying to divide a number by zero logic error: Mistakes in the underlying algorithm such as using a + sign instead of a multiplication sign
What kinds of errors are discovered by the compiler?
syntax errors
Loops are used when we need our program to make a choice between two or more things. True or false
true
int myValue; is called a ________ declaration.
variable