ICS4U1 Unit 1 & 2: Basics & Flow Structures
TEST REVIEW
Unit 1 & 2
Test Format:
Marks: 38 marks total
Format: Part A: Multiple Choice (13 questions)
Part B: Short Answer (6 questions)
Part C: Debugging (1 question with multiple parts)
Part D: Programming (2 options, select 1 to answer)
Rubric for Part D:
|
3 points
|
2 points
|
1 point
|
0 points
|
Execution
|
Logic and problem solving are robust. All cases are solvable by the solution presented.
|
Logic and problem solving are somewhat robust. The solution would work for most, but not all cases.
|
Logic and problem solving are not robust. Few cases would work.
|
No solution is given.
|
Clarity
|
Each line of pseudo-code clearly defines a step that the code would perform. Lines of pseudo-code are as specific and comprehensive as regular code would be.
|
Each line of pseudo-code defines a step that the code would perform. Some lines may accomplish more than what a single line of code could. More comprehensiveness is required.
|
A general idea has been communicated, but has not been divided into sufficient steps.
|
No solution is given.
|
Review Questions:
- What is a variable?
- What is the purpose of a variable?
- What are the three rules for naming variables?
- If a variable name broke one of these rules, what would happen?
- What are the two guidelines for naming variables?
- What is a…
- int
- double
- String
- boolean
- char
- What is the console?
- How do you display text in the console from your program?
- How do you collect data from the console to your program?
- What type of data is always returned with the nextLine() function?
- What is “casting” a variable?
- What is “parsing” a variable?
- Explain the difference between widening conversions and narrowing conversions.
- What are the basic math operators?
- What happens when you divide two ints that are not divisible?
- What does the modulus (%) operator do?
- What is an assignment operator?
- Why do we need to import certain libraries, like “import java.util.Scanner”?
- Does the Math class need to be imported into Java?
- What does Math.sqrt() do? What type of data does sqrt() return?
- What is a compiler error? What causes compiler errors?
- What is a “substring” of a String?
- What does it mean to concatenate a String?
- What is needed for a complete header comment?
- What term is used to describe the enclosure of one structure inside of another?
- For which values of x and y will the expression (x > 7) || (y <= 2) evaluate to false?
- What is wrong with the boolean expression ((x < 5) && (x < 100))?
- What is an infinite loop?
- What causes an infinite loop?
- What is the value of y after the following code runs, and the value of x = 7 and
y = 4?
if (x > 7 and y < 5){
y = 3*x+1;
}
else {
y = 3*x;
}
if (y%3 == 1) {
y += 2;
}
|
- Determine if each statement is either true or false.
- The condition of an if-statement is a boolean expression.
- A boolean expression evaluates to either true or false.
- The if-statement executes a set of statements when a condition is false.
- The indentation used in an if-else statement does not affect the execution of the statement.
- The expression num != 8 would evaluate to false if num was an int and
num = 8.
- All if-statements must include an “if”.
- All if-statements may include an “else if”.
- All if-statements must include an “else”.
- State whether each statement is true or false, if x = 10, y = 20, z = 30, and
foo = "groovy".
- ((x*2 == y) && false)
- ((foo.length() < x) || y < x)
- (Math.pow(x,2) > y*2)
- (!(y < z) || foo.charAt(1) == 'r')
- (((y+z)%5) == 0) && ((x+y) == z))
- True or false…
- A set of statements that perform a task over and over again based on a condition is called a loop structure.
- A sentinel value must be -1
- A for loop executes a set of statements a fixed number of times
- The -- operator is used in a for loop to increment the iterator variable down
- If you have a loop inside another loop, how would you find how many times the inner loop would execute?
- What is a sentinel value?
- When is the use of a while loop appropriate?
- When is the use of a for loop appropriate?
- Consider the code:
if (x < 5) { System.out.println("fairly");
}
else if (x =< 7) {
System.out.println("moderately");
}
else if (x > 10) {
System.out.println("marginally");
}
if (x % 2 == 1) {
System.out.println(" odd parents");
}
|
What would be displayed if…
- x = 3
- x = 7
- x = 12
- What is the difference between parameters and arguments?
- Give an example of a practical use of the while loop.
- Give an example of a practical use of the for loop.
- Write the following while loop as a for loop.
int num = 100;
while (num <= 110) {
System.out.println(num);
num += 2;
}
|
- Write the following for loop as a while loop.
for (int i=12; i>2, i-=2) {
System.out.println(counter);
}
|
- What is “scope”?
- What details should a complete method header comment contain?
- What is the “Single Responsibility Principle”?
- What is “divide and conquer”?
- What is “code reuse”?
- What are 5 ways why methods can improve our code?
- Write a method header for a public class method named calculateTip that has double parameters: cost and percentage, and returns a double value.
- The method squareArea takes an int parameter, side.
- Could the double value 6.5 be passed into squareArea as an argument? Why or why not?
- Could the short value 1 be passed into squareArea as an argument? Why or why not?
- Name two reasons why you should write methods, rather than writing all your code in the main() method.
- What does “pass-by-value” mean?
- What does “pass-by-reference” mean?
- What types of data are “pass-by-value”? What types are “pass-by-reference”?
- Write a header comment for a program that calculates the user’s body mass index.
- Given the following code, what would be outputted to the console?
int a = 100;
int b = 8;
int c = 2;
System.out.print(a + Math.pow(b, c) - a / b);
- Given the following code, what would be outputted to the console?
int x = 84;
x /= 12;
x += 1;
x %= 3;
System.out.print(x);
- Given the following code, what would be outputted to the console?
int num = 100;
num -= 12*7;
System.out.print(num);
System.out.print(Math.sqrt(Math.sqrt(num)));
- Given the following code, what would be outputted to the console?
String myStr = "But Professor Utonium accidentally added an extra ingredient to the concoction... Chemical X";
String a = myStr.substring(0, myStr.length()-11).toLowerCase();
String b = myStr.substring(21).toUpperCase());
System.out.print(a + " " + b);
- Given the following code, what would be outputted to the console?
String yourStr = "The doom and gloom up in his room are broken instantly by his magic little fish who grant his every wish";
yourStr = yourStr.replace("o", "0");
System.out.print(yourStr);
System.out.print(yourStr.contains("0"));
System.out.print(yourStr.indexOf("0"));
- Given the following code, what would be outputted to the console?
String ourStr = "Danger or trouble, I’m there on the double, you know that you always can call Kim Possible";
String x = "ouble";
int i = ourStr.indexOf(x);
ourStr = ourStr.substring(0, i) +
ourStr.substring(i, i + x.length()).toUpperCase() + ourStr.substring(x.length());
System.out.print(ourStr);
- Given the following code, what would be outputted to the console?
String theirStr = "A-G-L-E-T";
theirStr = theirStr.replace("-", "") + "Don’t forget it!";
System.out.print(theirStr.contains("ETD"));
- The following code has an error in it:
String punctuation = ".,:;?!";
String exclamation = punctuation.charAt(punctuation.length());
System.out.print(exclamation);
a. Explain what the code is attempting to do.
b. What is the error and why does it occur?
c. Edit the code so that it runs as intended, with no errors.
- The following code has an error in it:
int sideA = 5;
int sideB = 6;
int SideC = Math.sqrt(Math.pow(sideA,2) + Math.pow(sideB,2));
a. Explain what the code is attempting to do.
b. What is the error and why does it occur?
c. Edit the code so that it runs as intended, with no errors.