cs31 homework 1

Ace your homework & exams now with Quizwiz!

You are trying to calculate one-third of the value entered by the user. You have written the following code to accomplish this task: int i = 0; cin >> i; // get a value from the user double oneThirdOfI = i / 3; cout << oneThirdOfI; When executed, you enter the value: 2 What will get printed by this program? Why?

0 why? because integer division

You are trying to calculate one-third of the value entered by the user. You have written the following code to accomplish this task: int i = 0; cin >> i; // get a value from the user double oneThirdOfI = i / 3; cout << oneThirdOfI; When executed, you enter the value: 2.0 What will get printed by this program? Why?

0 why? i is declared as a int 2.0 is stored as a integer and the result of the code is integer division therefore, 0

Assuming the variable value has been declared as a double, what will be printed by the following statements? value = 1.00; cout << value; 1.0 Actually, this code will not build or run 1.00000000 1.000 1

1

Variables go through a four stage lifecycle in the order

1. variable declaration 2. variable initialization 3. variable free to use (ready) 4. variable death

You are trying to calculate a value based on i and j and k. You have written the following code to accomplish this task: int i = 0; int j = 0; int k = 0; cin >> i; // get values from the user cin >> j; cin >> k; cout << i + j * k - i / 10; When executed, you enter: 2 5 10 What will get printed by this program? Why?

52 why? we must use operator precedence: substitute the values: 2 + 5 * 10 - 2 / 10; 2 + (5 x 10) - (2/10) 52 - 0 (2/10 becomes 0 because int division)

The original creator of C++ is

Bjarne Stroustup

For the next few questions, please consider the following C++ program. Please make not of the lettered lines which are shown in the comments, as these lines of code are referred to by that letter in some of the questions that follow. #include <iostream> // LINE A using namespace std; // LINE B int main( ) // LINE C { // LINE D int i = 0; // LINE E cin >> i; // LINE F cout << "Your value of i was:"; // LINE G cout << i; // LINE H cout << endl; // LINE I return( 0 ); // LINE J } // LINE K When executed, which of the following statements will be run first? Line J Line G Line F Line I Line H

Line F

For the next few questions, please consider the following C++ program. Please make not of the lettered lines which are shown in the comments, as these lines of code are referred to by that letter in some of the questions that follow. #include <iostream> // LINE A using namespace std; // LINE B int main( ) // LINE C { // LINE D int i = 0; // LINE E cin >> i; // LINE F cout << "Your value of i was:"; // LINE G cout << i; // LINE H cout << endl; // LINE I return( 0 ); // LINE J } // LINE K When executed, which of the following statements will be run last? Line C Line H Line B Line J Line I Line F Line G Line D Line E Line A

Line J

For the next few questions, please consider the following C++ program. Please make not of the lettered lines which are shown in the comments, as these lines of code are referred to by that letter in some of the questions that follow. #include <iostream> // LINE A using namespace std; // LINE B int main( ) // LINE C { // LINE D int i = 0; // LINE E cin >> i; // LINE F cout << "Your value of i was:"; // LINE G cout << i; // LINE H cout << endl; // LINE I return( 0 ); // LINE J } // LINE K If Line E and Line F are switched so that Line F comes before Line E, what will happen?

The code will not build and instead will complain about an undeclared identifier

For the next few questions, please consider the following C++ program. Please make not of the lettered lines which are shown in the comments, as these lines of code are referred to by that letter in some of the questions that follow. #include <iostream> // LINE A using namespace std; // LINE B int main( ) // LINE C { // LINE D int i = 0; // LINE E cin >> i; // LINE F cout << "Your value of i was:"; // LINE G cout << i; // LINE H cout << endl; // LINE I return( 0 ); // LINE J } // LINE K When executed and prompted for a value, suppose the user enters: 3.14159 What will happen? The program will print: 3 The program will print 3.14159 The code will crash at run-time, printing nothing Actually, this code will not build or run

The program will print: 3

A declared const value in C++ is a named value which can never be changed after it has been first initialized. true or false

True

A variable in C++ is a named location in memory that holds a single value at a time and can have its value changed as the program executes. true or false

True

For the next few questions, please consider the following C++ program. Please make not of the lettered lines which are shown in the comments, as these lines of code are referred to by that letter in some of the questions that follow. #include <iostream> // LINE A using namespace std; // LINE B int main( ) // LINE C { // LINE D int i = 0; // LINE E cin >> i; // LINE F cout << "Your value of i was:"; // LINE G cout << i; // LINE H cout << endl; // LINE I return( 0 ); // LINE J } // LINE K The program shown above will build and run without any compiler warnings or errors

True

C++ source code gets converted into a runnable executable format by

a compiler that is part of a development tool like Visual Studio or XCode

When your main( ) returns a value back to the calling program, a return value of zero indicates

a successful run

using ints, doubles or other primitive variables before initialization what will c++ do

c++ will store garbage-y values in your code

Which of the following datatypes would be best for holding your CS 31 Discussion Section? char double int bool

char

All of the following names can be used as the name of a variable EXCEPT HappyDays _Howard double m1234

double

Which of the following datatypes would be best for holding the amount money found in your wallet? string char bool double

double

Which of the following datatypes would be best for holding the text of the US Constitution? char bool double string int

string


Related study sets

ABEKA: World Geography Appendix Quiz B

View Set

Real estate quiz 14- final review

View Set

LAB QUIZ 4: SIMPLE STAIN + SMEAR PREP & NEGATIVE STAINING + GRAM STAINING

View Set

مميزات مثلث متساوي الساقين

View Set

Biochemistry I Chapter 2 Problems

View Set

Chapter 5: The Integumentary System

View Set