Module 6 - Questions & Answers

docx

School

University of North Alabama *

*We aren’t endorsed by this school

Course

135

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

12

Uploaded by DukeStarChimpanzee47

Report
CS 135 Module 6 Variables (integer) 1. Assign numOfCustomers with 100. 2. Assign numOfStudents with 800. 3. What is in numBooks after both statements execute? 4. dogCount = 10. What is in animalsTotal after executing the statement ? 5. Assignment Operators with same variable on both sides. num = num + 4 num is 5. What is the num value after the statement is executed above? 6. numApples = 5. What is numFruit after the statements below are executed ? 7. Write a statement that decreases the value of the variable count by 1.
Identifiers 1. Valid or Not? Rules for identifiers be a sequence of letters (a-z, A-Z), underscores (_), and digits (0-9) start with a letter Identifiers are case sensitive , meaning upper- and lower-case letters differ. So num and Num are different. A reserved word (or keyword ) is a word that is part of the language, like integer, Get, or Put. A programmer cannot use a reserved word as an identifier. a) numCars b) num_Cars1 c) num___Cars d) _numCars e) 1stPlace f) Integer g) hello world
Style guidelines for identifiers Camel case: Lower camel case - multiple words, capitalizing each word except the first. Examples: numTrucks or employeeLoanAmt. Underscore separated: Words are lowercase and separated by an underscore. Examples: num_trucks or employee_loan_amt. Which convention is better? Neither. The key is to be consistent so code is easier to read and maintain. Good practice create meaningful identifier names that indicates an item's purpose. minimizes use of abbreviations except for well-known ones like num in numStudents. Find a balance. Abbreviations - harder to read and can lead to confusion. Avoid long variable names ( averageAgeOfUclaGraduateStudent ) – meaningful but too long and hard to read.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Choose the "best" identifier for a variable. The number of employees in a company. 1. num 2. number 3. numOfEmps 4. numberOfEmployeesInACompany The size of a class 1. s 2. size 3. classSize 4. sizeCl Arithmetic expressions (general) An expression is a combination of items, like variables, literals, operators, and parentheses, that evaluates to a value. 5 * (x + 4) is an expression. A literal is a specific value in code, like 5. An operator is a symbol that performs a built-in calculation (+, -, /, *). Valid or NOT? a) x + 1 b) 5 * (x - y) c) x d) 5 e) 5x f) 5 + (xy) g) y = x + 1
Evaluation of expressions a) y + 2 * z b) z / 2-x c) x * y * z d) x + 1 * y/2 e) x / 2 + y / 2
What is totCount after executing the following? numItems = 5 totCount = 1 + (2 * numItems) * 4 No commas allowed 1,555,055 is written as _______________________. The assignment statement: userAgeDays = userAgeYears * 365 assigns userAgeDays with the product of the user's age and 365, which does not take into account leap years. If the input is 10, what will userAgeYears be assigned? If the user inputs 10, what is userAgeDays assigned? Floating-point numbers (float) A floating-point literal is a number with a fractional part, even if that fraction is 0, as in 1.0, 0.0, or 99.573. Good practice is to always have a digit before the decimal point, as in 0.5, since .5 might mistakenly be viewed as 5.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Which statement best assigns the float variable? 56%, 56.0, 56 Which statement best assigns the variable? Both variables are of type float. a) width = measuredWidth -5 b) width = measuredWidth -5.0 Which is the best assignment for weather of type float? a) weather = .25 b) weather = 0.25 Choose the best variable type to represent each item. (float or integer) a) The number of cars in a parking lot. b) The number of customers at a bank. c) The net salary. d) A person’s height in inches. e) The number of days in a month. f) The average number of items in a room. Floating-point divide by zero Dividing a nonzero floating-point number by zero results in infinity or -infinity. If the dividend and divisor in floating-point division are both 0, the division results in a "not a number". (undefined value) Division: Answer, Positive Infinity or Negative Infinity? a) 13.0 / 3.0 b) / 3.0 c) 15.0 / 0.0 d) 0.0 / 0.0
Using Math Functions Some programs require math operations beyond +, -, *, /, like computing a square root. Functions can be used for this. Function – a list of statements. - executed by invoking the function's name ( function call ). Example : SquareRoot( 36.0) ? What is y? What is y?
What is the final mass? initMass = 5.0 growthRate = 1.0 (100%) yearsGrow = 3 RandomNumber() function Takes two arguments a lowValue and a highValue RandomNumber(1, 100) Integer division If both operands of / are integers, the operator performs integer division: No fractional part is generated. If at least one operand of / is a floating-point type, then floating-point division occurs. a) 10 / 4 = b) 3 / 4 = c) integer w = 10 and float x = 4.0, then w / x = Divide by 0 The second operand of / or % must never be 0, because division by 0 is mathematically undefined. A divide-by-zero error occurs at runtime if a divisor is 0, causing a program to terminate.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Type casting Explicitly convert an item's type. Ex: If myIntVar is 7 , then (myIntVar * 1.0) converts integer 7 to float 7.0. Modulo operator (%) Evaluates to the remainder of the division of two integer operands. a) 5 % 2 = b) 41 % 5 = c) 100 % 2 =
Choose a data type that best matches the value being held or represented. Select the most Appropriate Data Type Integer/Float/Boolean/Float array/String array. a) A program needs to keep track of the number of TVs available for sale. b) A program needs to store a list of people waiting to buy concert tickets. c) A program needs to store the amount of rain fall in inches for the past day. d) A program needs to store the forecasted high temperatures for the next 10 days. e) A program needs to keep track of whether a person's airline ticket is confirmed or not. _____________________________________________________________ Constants A constant is a named value item that holds a value that cannot change. A good practice , is to name constants using upper case letters with words separated by underscores, to make constants clearly visible. a) Variable or Constant? The value of PI. b) User input for the number of items bought in the store. c) Tax rate for the city. d) Net salary calculation after loan deductions for employees. ___________________________________________________
Writing variables and assignments. Write code that assigns finalValue with firstValue * secondValue.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help