HW (1.8-1.12)
1,000 name tags are to be sorted by last name by first placing tags into 26 unsorted substacks (for A's, B's, etc.), then sorting each substack. 1) If last names are equally distributed among the alphabet, what is the largest number of name tags in any one substack? 2) Suppose the time to place an item into one of the 26 sub-stacks is 1 second. How many seconds are required to place all 1000 name tags onto a sub-stack? 3) When sorting each substack, suppose the time to insert a name tag into the appropriate location of a sorted N-item sub-stack is N * 0.1 sec. If the largest substack is 50 tags, what is the longest time to insert a tag? 4) Suppose the time to insert a name tag into an N-item stack is N * 0.1 sec. How many seconds are required to insert a name tag into the appropriate location of a 500-item stack?
1,000 / 26 is 39 (rounded up). 26 is not a large number; all substacks can be on a table and in order so a person can quickly (about 1 sec) toss each name tag onto the right substack. 5 sec 50 sec
Indicate which assignments are valid. 1) x = 1 2) x = y 3) x = y + 2 4) x + 1 = 3 5) x + y = y + x
1-3 Valid 4-5 Invalid
Which are valid identifiers? 1) numCars 2) num_Cars1 3) _numCars 4) ___numCars 5) 3rdPlace 6) thirdPlace_ 7) thirdPlace! 8) short 9) very tall
1-4 Valid 5 Invalid 6 Valid 7-9 Invalid
Given variables x, y, and z. x = 9 y = x + 1 What is y?
10
x = 9 y = x + 1 x = 5 What is y?
10
Indicate the value of x after the assignments execute. 1) x = 5x = x + 7
12
x = 2 y = 3 x = x * y x = x * y
18
dogCount is 5. What is in animalsTotal after executing the statement below? animalsTotal = dogCount - 3;
2
The current value in houseRats is 200. What is in houseRats after executing the statement below? Valid answers: 0, 199, 200, or unknown. numRodents = houseRats;
200
What is in numBooks after both statements execute? numBooks = 5; numBooks = 3;
3
y = 30 x = y + 2 x = x + 1
33
dogCount is 5. What is in dogCount after executing the statement below? animalsTotal = dogCount - 3;
5
numApples is initially 5. What is numApples after: numApples = numApples + 3;
8
x = 9 y = x + 1 What is x?
9
identifier
A name created by a programmer for an item like a variable or function is called an identifier. An identifier must: be a sequence of letters (a-z, A-Z), underscores (_), and digits (0-9) start with a letter or underscore
reserved word
A reserved word is a word that is part of the language, like int, short, or double.
variable declaration
A variable declaration is a statement that declares a new variable, specifying the variable's name and type.
assignment
An assignment assigns a variable with a value, such as x = 5.
assignment statement
An assignment statement assigns the variable on the left-side of the = with the current value of the right-side expression.
expression
An expression may be a number like 80, a variable name like numApples, or a simple calculation like numApples + 1.
integer literal.
An integer like 80 appearing in an expression is known as an integer literal.
variable
In a program, a variable is a named item, such as x or numPeople, used to hold a value.
=
In programming, = is an assignment of a left-side variable with a right-side value. = is NOT equality as in mathematics.
incrementing
Increasing a variable's value by 1, as in x = x + 1, is common, and known as incrementing the variable.
An organizer of a 64-person meeting wants to start by having every person individually greet each other person for 30 seconds. Indicate whether the proposed solution achieves the goal without using excessive time. Before answering, think of a possible solution approach for this seemingly simple problem. 1) Form an inner circle of 32 and an outer circle of 32, with people matched up. Every 30 seconds, have the outer circle shift left one position. 2) Pair everyone randomly. Every 30 seconds, tell everyone to find someone new to greet. Do this 63 times. 3) Have everyone form a line. Then have everyone greet the person behind them. 4) Have everyone form a line. Have the first person greet the other 63 people for 30 seconds each. Then have the second person greet each other person for 30 seconds each (skipping anyone already met). And so on. 5) Form two lines of 32 each, with attendees matched up. Every 30 seconds, have one line shift left one position (with the person on the left end wrapping to right). Once the person that started on the left is back on the left, then have each line split into two matched lines, and repeat until each line has just 1 person.
No No No No Yes
Exactly three sock types A, B, and C exist in a drawer. 1) If sock type A is pulled first, sock type B second, and sock type C third, the fourth sock type must match one of A, B, or C. 2) If socks are pulled one at a time and kept until a match is found, at least four pulls are necessary. 3) If socks are pulled two at a time and put back if not matching, and the process is repeated until the two pulled socks match, the maximum number of pulls is 4.
True False False
int userAge;
declares a new variable named userAge that can hold an integer value.
Write a statement ending with - 1 that decreases the value of variable flyCount by 1.
flyCount = flyCount - 1;
Assign houseSize with 2300
houseSize = 2300;
Declare an integer variable named daysCount, initializing the variable to 365 in the declaration.
int daysCount = 365;
Using two statements on two separate lines, declare integer variables named newSales and totalSales. (Do not initialize the variables.)
int newSales; int totalSales;
Declare an integer variable named numDogs, initializing the variable to 0 in the declaration.
int numDogs = 0;
eclare an integer variable named numPeople. (Do not initialize the variable.)
int numPeople;
Write an assignment statement to assign numCars with 99.
numCars = 99;
Assign numFruit with the current value of numApples.
numFruit = numApples;
Assign numItems with the result of ballCount - 3
numItems = ballCount - 3;
Choose the "best" identifier for a variable with the stated purpose, given the above discussion. 2) The size of an LCD monitor size sizeLcdMonitor s sizeLcdMtr 3) The number of jelly beans in a jar. numberOfJellyBeansInTheJar jellyBeansInJar nmJlyBnsInJr
sizeLcdMonitor jellyBeansInJar
What memory location (address) will a compiler allocate for the variable declaration below? If appropriate, type: Unknown int numHouses = 99;
unknown
Complete this assignment to increment y: y = _____
y + 1
Which code segments have an error? 1) 21 = dogCount; 2) int amountOwed = -999; 3) int numDays; int numYears; numDays = numYears * 365;
Error No error Error