Self Test Exercises - Programming and Problem Solving I | CECS 174, Exams of Computer Science

Material Type: Exam; Class: Programming & Problem Solving I; Subject: Computer Engr & Computer Sci; University: California State University - Long Beach; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 08/18/2009

koofers-user-5xg
koofers-user-5xg 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 2 Self Test Exercises
1. Give the declaration for 2 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.
2. Give the declaration for 2 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.
3. Give a C++ statement that will change the value of the variable sum to the sum of the values
n1 and n2. The variables are all of type int.
4. Give a C++ statement that will increase the value of the variable length by 8.3. The
variable length is of type double.
5. 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.
6. Write a program that contains statements that output the value of 5 or 6 variables that have
been declared, but not initialized. Compile and run the program. What is the output?
Explain.
7. Give good variable names for each of the following:
a) A variable to hold the speed of an automobile.
b) A variable to hold the pay rate for an hourly employee.
c) A variable to hold the highest score in an exam.
8. Give an output statement that will produce the following message on the screen:
The answer to the question of
Life, the Universe, and Everything is 42.
9. 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.
10.What statement 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?
11.Write a complete C++ program that writes the phrase Hello world to the screen. The
program does nothing else.
12.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.
1
pf3
pf4

Partial preview of the text

Download Self Test Exercises - Programming and Problem Solving I | CECS 174 and more Exams Computer Science in PDF only on Docsity!

Chapter 2 Self Test Exercises

  1. Give the declaration for 2 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.
  2. Give the declaration for 2 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.
  3. Give a C++ statement that will change the value of the variable sum to the sum of the values n1 and n2. The variables are all of type int.
  4. Give a C++ statement that will increase the value of the variable length by 8.3. The variable length is of type double.
  5. 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.
  6. Write a program that contains statements that output the value of 5 or 6 variables that have been declared, but not initialized. Compile and run the program. What is the output? Explain.
  7. Give good variable names for each of the following: a) A variable to hold the speed of an automobile. b) A variable to hold the pay rate for an hourly employee. c) A variable to hold the highest score in an exam.
  8. Give an output statement that will produce the following message on the screen: The answer to the question of Life, the Universe, and Everything is 42.
  9. 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. 10.What statement 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? 11.Write a complete C++ program that writes the phrase Hello world to the screen. The program does nothing else. 12.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.

13.Give an output statement that produces the new-line character and a tab character. 14.Write a short program that declares and initializes double variables one, two, three, four, and five to the values 1.000, 1.414, 1.732, 2.000, and 2.236, respectively. Then write output statements to generate the following legend and table. Use the tab escape sequence \t to line up the columns. The output should be: N Square Root 1 1. 2 1. 3 1. 4 2. 5 2. 15.Convert each of the following mathematical formulas to C++ expression: 3 x 3 x + y 7 xy 3 2 x y z   16.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’; 17.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; 18.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. 19.Given the following fragment that purports to convert from degrees Celsius to degrees Fahrenheit, 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.

28.What output would be produced in the previous exercise if the > sign were replaced with <? 29.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); 30.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); 31.What is the most important difference between a while statement and a do-while statement? 32.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; } 33.Write a complete C++ program that outputs the number 1 to 20, one per line. The program does nothing else. 34.The following if-else statement will compile and run without any problems. However, it is not laid out in a good programming style. Rewrite it so that the layout follows the good programming style guidelines. if (x < 0) {x = 7; cout << “x is now positive.”;} else {x = -7; cout << “x is now negative.”;} 35.What output would be produced by the following two lines (when embedded in a complete and correct program)? //cout << “Hellow from”; cout << “Self-Test Exercise”; 36.Write a complete C++ program that 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.