


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: Programming & Problem Solving I; Subject: Computer Engr & Computer Sci; University: California State University - Long Beach; Term: Unknown 1989;
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Chapter 2 Self Test Exercises
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 x y 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.