← Unit 2: Programming Basics
ICS3U · Unit 2 test review
Programming Basics: Test Review
Everything for the Unit 2 test on one page: the key facts from all 3 lessons, the mistakes that cost the most marks, and a practice test that points you back to the lessons you need.
Key facts
Your First Java Program
- Every Java program lives in a class, and execution starts at
public static void main(String[] args). - The file name must exactly match the public class name, including capitals:
public class Hellogoes in Hello.java. - Braces
{ }group statements into blocks; a semicolon;ends one statement. Braces take no semicolon. javac Hello.javacompiles to Hello.class;java Helloruns it — no file extension on the run command.printlnprints then moves to a new line;printprints and leaves the cursor in place.- Three error types: syntax (caught at compile time), runtime (crashes mid-run), logic (runs fine, answer is wrong).
Variables, Primitive Types and Casting
- The eight primitives are
byte,short,int,long,float,double,char,boolean.Stringis a class, not a primitive. intholds up to ; going past it wraps silently to the most negative int.- If both operands of
/are integers, Java truncates:5 / 2is2. Make one side a double to get 2.5. %gives the remainder:17 % 5is2, andn % 2 == 0tests for even.- Widening (byte → short → int → long → float → double) is automatic; narrowing needs a cast such as
(int). - A cast truncates toward zero —
(int) 9.99is9— andfinalconstants are written in UPPER_SNAKE_CASE.
Operators, Expressions and Input
- Precedence: brackets, then unary, then
* / %left to right, then+ -left to right, then assignment. %sits at the same level as*and/, so10 % 4 * 2is(10 % 4) * 2 = 4.- If either operand of
+is a String,+joins:"total: " + 1 + 2givestotal: 12, so bracket the arithmetic. x += 3meansx = x + 3;x++uses the old value then increments,++xincrements then uses the new one.Math.roundreturns along; to round to two decimals useMath.round(v * 100) / 100.0with a double divisor.- After
nextInt()ornextDouble(), call a barenextLine()before reading a line of text.
Where marks get lost
- Saving a file under a name that does not match the public class name — Java rejects it before it looks at anything else. (Your First Java Program)
- Typing
java Hello.javainstead ofjava Hellowhen running, or forgetting to compile first so the .class file is stale or missing. (Your First Java Program) - Leaving off a semicolon, or adding one after a closing brace. Statements end in
;; blocks do not. (Your First Java Program) - Writing
System.out.printlnwith a lowercase s or a misspelledprintn— Java is case sensitive and will not guess. (Your First Java Program) - Assuming that because a program printed something, it worked. Only a test with a known correct answer tells you that. (Your First Java Program)
- Expecting
5 / 2to be 2.5. With two ints Java truncates to 2 — and never warns you, because nothing illegal happened. (Variables, Primitive Types and Casting) - Casting too late:
(double) (a / b)does the integer division first, so the decimal is already gone by the time the cast runs. (Variables, Primitive Types and Casting) - Assuming a cast rounds.
(int) 9.99is 9, not 10. UseMath.roundwhen you want rounding. (Variables, Primitive Types and Casting) - Storing a value that overflows an
int— anything past 2 147 483 647 wraps to a negative number silently. Uselong. (Variables, Primitive Types and Casting) - Using
==to compare Strings because String looks like a primitive. It is a class, so.equals()is the correct tool. (Variables, Primitive Types and Casting) - Expecting
"total: " + 1 + 2to print total: 3. Evaluation is left to right, so it prints total: 12 unless you bracket the sum. (Operators, Expressions and Input) - Forgetting the bare
nextLine()afternextInt(), so the next text prompt is skipped and an empty String is stored. (Operators, Expressions and Input) - Writing
Math.round(v * 100) / 100with an int divisor, which truncates and throws away the decimals you just rounded to. (Operators, Expressions and Input) - Assuming
%binds more loosely than*. They share a level and run left to right, so10 % 4 * 2is 4, not10 % 8. (Operators, Expressions and Input) - Forgetting
import java.util.Scanner;above the class, or building a new Scanner every time input is read instead of reusing one. (Operators, Expressions and Input)
Practice test
18 questions from across the unit. Each one shows the lesson it comes from.
- Your First Java Program1Which of these is the correct signature for the method where a Java program starts?
- Your First Java Program2What appears on screen after
System.out.print("Ja"); System.out.print("va");runs? - Your First Java Program3A program compiles, runs to the end, and prints an average of 45 when the correct average is 72. This is best classified as:
- Your First Java Program4Put the steps of getting a Java program running into order.
- 1Run java ClassName to launch the bytecode
- 2A ClassName.class file of bytecode is produced
- 3Fix any syntax errors the compiler reports and recompile
- 4Write the source code and save it as ClassName.java
- 5Run javac ClassName.java to compile it
- Your First Java Program5What file does
javac Hello.javaproduce? - Your First Java Program6Which of these would produce a runtime error rather than a compile error?
- Variables, Primitive Types and Casting7What value does
7 / 2produce when both operands are ints? - Variables, Primitive Types and Casting8What does
(double) 7 / 2evaluate to? - Variables, Primitive Types and Casting9Which statement correctly declares a constant for Ontario's 13% HST rate?
- Variables, Primitive Types and Casting10What does
System.out.println(5 / 2.0);print? - Variables, Primitive Types and Casting11Put these primitive types in order of widening, from narrowest to widest.
- 1float
- 2long
- 3double
- 4byte
- 5short
- 6int
- Variables, Primitive Types and Casting12Rewrite
int avg = total / count;so the average keeps its decimal places in a double named avg. - Operators, Expressions and Input13What does
3 + 4 * 2evaluate to? - Operators, Expressions and Input14What does
10 % 4 * 2evaluate to? - Operators, Expressions and Input15
int x = 5;thenx += 3;thenx *= 2;. What is x? - Operators, Expressions and Input16What does
Math.round(2.5)return? - Operators, Expressions and Input17A program calls
sc.nextInt()and thensc.nextLine()to read a name, but the name comes back empty. Why? - Operators, Expressions and Input18Order these steps as Java evaluates
2 + 3 * 4 - 6 / 2.- 1Evaluate 6 / 2, giving 3
- 2Subtract 3 from 14, giving 11
- 3Add 2 + 12, giving 14
- 4Evaluate 3 * 4, giving 12
Endless drill
Fresh numbers every time, for the calculation questions in this unit.
Endless drillnew numbers every time
Generating a problem…