psu cmpsc 101 final Exam
Questions and Answers
branching - -directs a program to execute either one statement group or
another, depending on an expression's value.
Ex; print "Too young to drive" if userAge < 16, else print "OK to drive".
- What is the final value of numItems?
bonusVal = 5;
if (bonusVal<12) numItems= 100
numItems = 200; - -5 < 12 so the first branch executes, namely numItems
= 100.
- If userAge is greater than 62, assign 15 to discount. Else, assign 0 to
discount. - -if (userAge > 62) {
discount = 15;
}
else {
discount = 0;
}
- If numPeople is greater than 10, execute groupSize = 2 * groupSize.
Otherwise, execute groupSize = 3 * groupSize and also numPeople =
numPeople - 1. - -if (numPeople > 10) {
groupSize = 2 * groupSize;
}
else {
groupSize = 3 * groupSize;
numPeople = numPeople - 1;
}
- If numPlayers is greater than 11, execute teamSize = 11. Otherwise,
execute teamSize = numPlayers. Then, no matter the value of numPlayers,
execute teamSize = 2 * teamSize. - -if (numPlayers > 11) {
teamSize = 11;
}
else {
teamSize = numPlayers;
}
teamSize = 2 * teamSize;
- Braces - -surrounds a branch's statements. {},sometimes redundantly
called curly braces represent a grouping, such as a grouping of statements.
, - rational operator / equality operator - -An if-else expression commonly
involves.
- Boolean value - -either true or false. If userAge is 19, then userAge<25
evaluates to true.
- a is equal to b - -a==b
- a != b - -a is not equal to b or differ
- != - -Differ or less than or greater than
- centsLost is a negative number - -< 0 is the common way to detect a
negative number.
- Strings are equal - -if they have they have the same number of characters
and corresponding characters are identical.
- nested if-else - -A branch's statements can include any valid statements,
including another if-else statements, such occurrence known as nested if-
else statements.
- logical operator - -treats operands as being true or false, and evaluates to
true or false.
- a && b - -Logical AND: true when both of its operands are true
- a || b - -Logical OR: true when at least one of its two operands are true
- !false - -TRUE
- ! - -NOT
- !a - -Logical NOT (opposite): true when its single operand is false (and
false when operand is true)
- !((numPeople > 10) && (numCars > 2)) - -The && expression is false. So !
(false) is true.
- % - -modulo (remainder
- modf - -Break into fractional and integral parts.
- Iterate until c equals 'z' - -c != 'z'
, - bool(short for Boolean) - -data type is for variables that store only values
true or false.
- Binary operators - -The relational operators and logical operators (except
for !) are binary operators.
Take two operands (from the left and right) and evaluate to true or false.
- bitewise - -perform AND or OR on corresponding individual bits of the
operand.
- Mixing bitwise and logical operators - -x == 3 | y > 1 && z != 3
Answer= ((x == 3) || (y > 1)) && (z!= 3)
- Short circuit evaluation - -skips evaluating later operands if the result of
the logical operator can already be determined.
- Switch - -statement can more clearly represent multi-branch behavior
involving a variable being compared to constant values.
- case - -whose constant comparison matches the value of the switch
expression, executes that case's statements, and then jumps to the end.
- default case - -in no case matches, statements are executed
- break - -statement for a case will cause the statements within the next
case to be executed.
- Boolean - -refers to a quantity that has only two possible values, true or
false.
- bool - -for representing Boolean quantities
- index - -Each string character has a position number
- at() - -the notation someString. at (0) access the character at a particular
index of a string, in this case index 0.
- common error - -is to access an invalid array index, especially exactly one
larger than the largest index.
- exception - -is a detected runtime error that commonly prints an error
message and terminates the program.
- Good practice - -is to use .at() rather than brackets for accessing a string's
characters, due to .at()'s error checking.
Questions and Answers
branching - -directs a program to execute either one statement group or
another, depending on an expression's value.
Ex; print "Too young to drive" if userAge < 16, else print "OK to drive".
- What is the final value of numItems?
bonusVal = 5;
if (bonusVal<12) numItems= 100
numItems = 200; - -5 < 12 so the first branch executes, namely numItems
= 100.
- If userAge is greater than 62, assign 15 to discount. Else, assign 0 to
discount. - -if (userAge > 62) {
discount = 15;
}
else {
discount = 0;
}
- If numPeople is greater than 10, execute groupSize = 2 * groupSize.
Otherwise, execute groupSize = 3 * groupSize and also numPeople =
numPeople - 1. - -if (numPeople > 10) {
groupSize = 2 * groupSize;
}
else {
groupSize = 3 * groupSize;
numPeople = numPeople - 1;
}
- If numPlayers is greater than 11, execute teamSize = 11. Otherwise,
execute teamSize = numPlayers. Then, no matter the value of numPlayers,
execute teamSize = 2 * teamSize. - -if (numPlayers > 11) {
teamSize = 11;
}
else {
teamSize = numPlayers;
}
teamSize = 2 * teamSize;
- Braces - -surrounds a branch's statements. {},sometimes redundantly
called curly braces represent a grouping, such as a grouping of statements.
, - rational operator / equality operator - -An if-else expression commonly
involves.
- Boolean value - -either true or false. If userAge is 19, then userAge<25
evaluates to true.
- a is equal to b - -a==b
- a != b - -a is not equal to b or differ
- != - -Differ or less than or greater than
- centsLost is a negative number - -< 0 is the common way to detect a
negative number.
- Strings are equal - -if they have they have the same number of characters
and corresponding characters are identical.
- nested if-else - -A branch's statements can include any valid statements,
including another if-else statements, such occurrence known as nested if-
else statements.
- logical operator - -treats operands as being true or false, and evaluates to
true or false.
- a && b - -Logical AND: true when both of its operands are true
- a || b - -Logical OR: true when at least one of its two operands are true
- !false - -TRUE
- ! - -NOT
- !a - -Logical NOT (opposite): true when its single operand is false (and
false when operand is true)
- !((numPeople > 10) && (numCars > 2)) - -The && expression is false. So !
(false) is true.
- % - -modulo (remainder
- modf - -Break into fractional and integral parts.
- Iterate until c equals 'z' - -c != 'z'
, - bool(short for Boolean) - -data type is for variables that store only values
true or false.
- Binary operators - -The relational operators and logical operators (except
for !) are binary operators.
Take two operands (from the left and right) and evaluate to true or false.
- bitewise - -perform AND or OR on corresponding individual bits of the
operand.
- Mixing bitwise and logical operators - -x == 3 | y > 1 && z != 3
Answer= ((x == 3) || (y > 1)) && (z!= 3)
- Short circuit evaluation - -skips evaluating later operands if the result of
the logical operator can already be determined.
- Switch - -statement can more clearly represent multi-branch behavior
involving a variable being compared to constant values.
- case - -whose constant comparison matches the value of the switch
expression, executes that case's statements, and then jumps to the end.
- default case - -in no case matches, statements are executed
- break - -statement for a case will cause the statements within the next
case to be executed.
- Boolean - -refers to a quantity that has only two possible values, true or
false.
- bool - -for representing Boolean quantities
- index - -Each string character has a position number
- at() - -the notation someString. at (0) access the character at a particular
index of a string, in this case index 0.
- common error - -is to access an invalid array index, especially exactly one
larger than the largest index.
- exception - -is a detected runtime error that commonly prints an error
message and terminates the program.
- Good practice - -is to use .at() rather than brackets for accessing a string's
characters, due to .at()'s error checking.