Answer
Flashcards · AP® Computer Science A · Unit 1
Unit 1: Using Objects and Methods flashcards
81 cards on topics 1.1 to 1.15. Pick a topic or study the whole unit.
Study mode needs JavaScript. Every card is listed below, so you can still cover the answers and test yourself.
Choose your cards
Session complete
Worth another look:
All 81 cards
For a quick read-through or printing. Cover the right-hand side to test yourself.
1.1 Introduction to Algorithms, Programming, and Compilers
- What is an algorithm, and what does sequencing mean?
- An algorithm is a step-by-step process for completing a task. Sequencing means the steps happen one at a time in a defined order, and changing the order can change the result.
- What do an IDE and a compiler each do?
- An IDE gives you tools to write, compile and run code. The compiler checks the code for some errors, and any error it finds must be fixed before the program can run.
- What is a syntax error, and who finds it?
- A syntax error breaks the rules of the language. The compiler detects it, so the program never runs at all.
- What is a logic error, and how is it found?
- The program compiles and runs but gives wrong or unexpected results. It is found only by testing with chosen data whose correct answer you already know.
- What is a run-time error, and what is an exception?
- A run-time error happens while the program is running and usually stops it. An exception is a run-time error the compiler did not detect, such as 12 / 0 with ints, which throws an ArithmeticException.
- Why is "it compiled, so it is correct" a mistake?
- Compiling only shows the code obeys Java's rules. It says nothing about whether the algorithm does what you intended, so you still have to test with specific data.
1.2 Variables and Data Types
- What are the three primitive types used in this course?
- int for whole numbers, double for real numbers, and boolean for true or false. String is not primitive; it is a reference type.
- Which assignment is legal: double d = 4; or int n = 3.0; ?
- double d = 4; is legal and stores 4.0, because an int fits in a double. int n = 3.0; fails to compile, because a double cannot be stored in an int without a cast.
- What is the difference between a primitive variable and a reference variable?
- A primitive variable holds the value itself. A reference variable, such as a String, holds a reference to an object, which can also be null.
- What are the rules for a valid variable name?
- It may use letters, digits, _ or $; it cannot start with a digit and cannot be a keyword. Names are case-sensitive, so count and Count are different variables.
- What does double x = 9 / 2; store?
- It stores 4.0. The division 9 / 2 is done with ints first and gives 4, and only then is the 4 widened to a double.
1.3 Expressions and Output
- How do System.out.print and System.out.println differ?
- println displays the value and then moves to a new line. print displays the value and stays on the same line, so the next output continues after it.
- What is the value of 7 / 2, and of 7 / 2.0?
- 7 / 2 is 3, because two ints give integer division, which keeps only the integer part. 7 / 2.0 is 3.5, because one double operand makes the whole division a double.
- What does the % operator give, and what is n % 10 used for?
- a % b is the remainder when a is divided by b, so 17 % 5 is 2. n % 10 gives the last digit of n, and n / 10 gives n without its last digit.
- What is the value of 7 / 2 * 2.0, and why?
- 6.0. Operators of equal precedence go left to right, so 7 / 2 is done first as integer division and gives 3, and then 3 * 2.0 is 6.0.
- How are a double quote, a backslash and a new line written inside a string?
- With escape sequences that start with a backslash: a backslash then a quote mark gives a double quote, two backslashes give one backslash, and a backslash then the letter n gives a new line. Each is one character.
- What happens if you divide an int by the int zero?
- It compiles but throws an ArithmeticException at run time. Any output printed before the failing statement still appears.
1.4 Assignment Statements and Input
- What does the assignment operator = do?
- It stores the value of the expression on its right in the variable on its left. Read it as "store", not as "equals".
- In a = 3; a = a * 2 + a; what is a at the end?
- 9. The right side is evaluated in full with the current value of a (3 * 2 + 3 = 9) before the result is stored back in a.
- What is null, and which variables can hold it?
- null is a special literal meaning no object. Reference variables such as a String can hold it; int n = null; is not allowed because int is a primitive.
- Why is int y; int z = y + 1; a compile-time error?
- The variable y is used before it has been given a value. A variable must be initialized by assignment before it appears in an expression.
- What do nextInt and nextLine do on a Scanner, and what is a common trap?
- nextInt reads an int and leaves the rest of the line unread. A nextLine call straight after it on the same line often returns an empty string. Reading text that is not an int with nextInt throws an InputMismatchException.
1.5 Casting and Range of Variables
- What does the cast (int) do to a double?
- It truncates: the digits after the decimal point are dropped, so (int) 3.99 is 3 and (int) -3.99 is -3. It does not round.
- What is the difference between (double) 7 / 2 and (double) (7 / 2)?
- (double) 7 / 2 casts 7 first and gives 3.5. (double) (7 / 2) does the integer division first, giving 3, and then casts it to 3.0.
- How do you round a double to the nearest int?
- Use (int) (x + 0.5) for non-negative x and (int) (x - 0.5) for negative x. For example (int) (87.5 + 0.5) is 88.
- What are Integer.MAX_VALUE and Integer.MIN_VALUE, and what is overflow?
- They are the largest and smallest int values. If a result goes outside that range it wraps around with no exception, so Integer.MAX_VALUE + 1 becomes Integer.MIN_VALUE.
- What is round-off error with doubles?
- A double cannot store every decimal exactly, so values are rounded. For example 0.1 + 0.2 gives 0.30000000000000004. Use ints where exact values matter.
- What is the value of (int) 2.5 * 2 compared with (int) (2.5 * 2)?
- (int) 2.5 * 2 is 4, because the cast applies only to 2.5 and gives 2. (int) (2.5 * 2) is 5, because the brackets mean 5.0 is cast to 5.
1.6 Compound Assignment Operators
- What does x += 3 mean, and name the other compound operators?
- x += 3 means x = x + 3. The operators are +=, -=, *=, /= and %=. The operation is done first and the result is then stored in the variable on the left.
- How is x *= 2 + 3 evaluated?
- As x = x * (2 + 3). The whole right side is evaluated before it is combined with x, so for x = 8 the result is 40.
- What do x++ and x-- do?
- x++ adds 1 to x and x-- subtracts 1 from x. In this course they are used as separate statements after the variable, not inside other expressions.
- Why does the order of x += 5; and x *= 2; matter?
- Starting from 10, x += 5 then x *= 2 gives 30, but x *= 2 then x += 5 gives 25. Each statement uses the value left by the previous one.
- What is the effect of n *= 1.5 when n is an int?
- Java converts the result back to an int, truncating the fraction. For n = 5 the product is 7.5 and n becomes 7.
- Name three jobs compound assignment does.
- Accumulate a running total (sum += price), count (count++), and scale or wrap a value (price *= 2, or position %= 10 to keep a value in 0 to 9).
1.7 Application Program Interface (API) and Libraries
- What are a library and an API specification?
- A library is a collection of classes, grouped into packages. An API specification tells the programmer how to use them: each class's attributes and behaviors.
- Which package is available by default, and which classes need an import?
- java.lang (String, Math, Integer) is available by default. Classes such as Scanner and ArrayList in java.util need an import; without one the compiler reports cannot find symbol.
- What are attributes and behaviors of a class?
- Attributes are the data of the class, stored in variables. Behaviors are what objects of the class can do, defined by methods.
- How do you read a method entry such as static double sqrt(double x)?
- The return type (double) comes before the name, then the name (sqrt), then the parameter list with types in order. The word static means it is called through the class name.
- What does the Java Quick Reference give you on the exam?
- It lists the accessible library methods with their signatures and descriptions. You are expected to read and use it correctly, not to memorise it.
1.8 Documentation with Comments
- What are the three kinds of comment in Java?
- Two slashes for the rest of a line; slash-star to star-slash for a block (it does not nest); slash and two stars for Javadoc before a class or method. Comments never change how a program runs.
- What is a precondition?
- A condition that must be true just before a method runs for it to behave as expected. The caller is responsible for it; the method is not expected to check it.
- What is a postcondition?
- A condition that must be true after the method has executed, such as the value returned or the new state of the object. It is only promised if the precondition held.
- What happens if a precondition is broken?
- The method makes no promise about its result and may fail. For example, a method that divides by a parameter documented as non-zero throws an exception for 0.
- How do you tell a precondition from a postcondition?
- A precondition describes the inputs and state before the call, and the caller is responsible. A postcondition describes the outcome after the call, and the method promises it.
1.9 Method Signatures
- What is a method signature?
- The method name together with the ordered list of its parameter types. The return type and the parameter names are not part of the signature.
- What is the difference between a parameter and an argument?
- A parameter is a variable declared in the method header. An argument is the value passed in when the method is called.
- What does call by value mean for a primitive argument?
- The parameter is initialized with a copy of the argument. Changing the parameter inside the method does not change the caller's variable.
- What is the difference between a void and a non-void method?
- A void method returns no value and is called as a statement. A non-void method returns a value of its return type, which must be stored or used in an expression to be of any use.
- What is method overloading?
- Several methods with the same name but different signatures, such as square(int) and square(double). Two methods cannot differ only by return type.
- What happens when a method is called and when it finishes?
- Sequential execution is interrupted and the method runs. At its last statement or a return, control goes back to the point immediately after the call.
1.10 Calling Class Methods
- What is a class method and how is it called?
- A class method has static in its header and belongs to the class, not to an object. Call it with the class name and the dot operator, such as Converter.toFahrenheit(100).
- When can the class name be left out of a call to a static method?
- When the call is inside the defining class, so method(args) works there. From another class you must write ClassName.method(args).
- How is a nested call such as f(g(x)) evaluated?
- From the inside out: g(x) is evaluated first, and its return value becomes the argument to f.
- Why can main be called before any object exists?
- main is itself a class method. It belongs to the class, so no object is needed to start the program.
- Which argument types fit a double parameter, and an int parameter?
- An int argument can fill a double parameter (it is converted automatically). A double argument cannot fill an int parameter; that is a compile-time error.
1.11 Math Class
- Which Math methods are in the Quick Reference?
- abs(int), abs(double), pow(double, double), sqrt(double) and random(). They are all static, so you call them as Math.sqrt(16) and never create a Math object.
- What are the return types of Math.pow, Math.sqrt and Math.abs(-7)?
- Math.pow and Math.sqrt return a double (Math.pow(2, 5) is 32.0). Math.abs returns an int for an int argument, so Math.abs(-7) is 7.
- What range does Math.random() produce?
- A double greater than or equal to 0.0 and less than 1.0. It can be 0.0 but never 1.0.
- How do you get a random int from low to high, both inclusive?
- (int) (Math.random() * (high - low + 1)) + low. For a die, (int) (Math.random() * 6) + 1 gives 1 to 6.
- Why is (int) Math.random() * n always 0?
- The cast applies to Math.random() alone and truncates it to 0 before the multiplication. Put brackets around the product: (int) (Math.random() * n).
1.12 Objects: Instances of Classes
- What is the difference between a class and an object?
- A class is the blueprint that defines attributes and behaviors. An object is one specific instance of that class with its own attribute values.
- What does a reference variable hold?
- An object reference, which can be thought of as the memory address of the object, or null. It does not contain the object itself.
- What does Counter b = a; do when a refers to a Counter object?
- It copies the reference, not the object. Both a and b now refer to the same object, so a change made through one name is visible through the other.
- Does Counter c; create a Counter?
- No. It only declares a variable that can refer to a Counter. An object is created only by a statement that uses new.
- What is the Object class?
- All classes in Java are subclasses of Object. Writing your own inheritance relationships is outside the course and the exam.
1.13 Object Creation and Storage (Instantiation)
- What is a constructor and how is it called?
- A constructor has the same name as the class and no return type. It is called with new, as in new Box(3, 5), to create an object and set its initial attribute values.
- What are overloaded constructors?
- Constructors with different parameter lists, such as Box(), Box(int) and Box(int, int). The arguments in a call decide which one runs.
- What does new Box(1, 2, 3) do if no such constructor exists?
- It is a compile-time error. The number, order and types of the arguments must match the parameters of some constructor.
- What does Box d = null; mean?
- d is a reference variable that refers to no object. Calling a method on it later throws a NullPointerException.
- After Box c = new Box(3, 5); then c = new Box(9); what has happened?
- The variable c now refers to a different, new object. The first Box is no longer referred to by c, and no existing object was changed.
1.14 Calling Instance Methods
- How do you call an instance method?
- With an object name, the dot operator and the method: box.area(). It needs an object, so calling it with only the class name is a compile-time error.
- What happens when you call a method on a null reference?
- A NullPointerException is thrown at run time. The compiler only checks that the method exists for the variable's class, so it does not warn you.
- Why can the same method give different results for two objects?
- Each object has its own attribute values, and an instance method works with the attributes of the object it is called on.
- What does Rectangle s = r; s.grow(4); do to r?
- r sees the change, because s and r refer to the same object. A void method called through one name can change the object for every name.
- Why is int c = r.grow(2); a compile-time error when grow is void?
- A void method returns no value, so there is nothing to store. A void method must be called as a statement.
1.15 String Manipulation
- What does it mean that a String is immutable?
- Its content cannot be changed after it is created. Methods such as toUpperCase return a new String, so the result must be stored or it is lost.
- What does substring(from, to) return?
- The characters from index from up to but not including index to, so the length is to - from. For "microscope", substring(2, 6) is "cros".
- What is the range of valid String indices?
- 0 to length() - 1. An index outside the range throws a StringIndexOutOfBoundsException. substring(index, index + 1) gives the one-character String at index.
- What does indexOf return?
- The index of the first occurrence of the given String, or -1 if it is not found. For "microscope", indexOf("sco") is 5 and indexOf("z") is -1.
- What is "a" + 1 + 2, and what is 1 + 2 + "a"?
- "a" + 1 + 2 is "a12" and 1 + 2 + "a" is "3a". Evaluation is left to right, and once a String is involved the + is concatenation.
- How do you compare Strings, and what does compareTo return?
- Use equals for content, not ==. compareTo returns a negative value, zero or a positive value for before, equal or after in order, and only the sign is guaranteed.
Study Unit 1 further
- 1.1 Introduction to Algorithms, Programming, and Compilers Study guide: 1.1 Introduction to Algorithms, Programming, and CompilersRevision notes: 1.1 Introduction to Algorithms, Programming, and CompilersPractice questions: 1.1 Introduction to Algorithms, Programming, and Compilers
- 1.2 Variables and Data Types Study guide: 1.2 Variables and Data TypesRevision notes: 1.2 Variables and Data TypesPractice questions: 1.2 Variables and Data Types
- 1.3 Expressions and Output Study guide: 1.3 Expressions and OutputRevision notes: 1.3 Expressions and OutputPractice questions: 1.3 Expressions and Output
- 1.4 Assignment Statements and Input Study guide: 1.4 Assignment Statements and InputRevision notes: 1.4 Assignment Statements and InputPractice questions: 1.4 Assignment Statements and Input
- 1.5 Casting and Range of Variables Study guide: 1.5 Casting and Range of VariablesRevision notes: 1.5 Casting and Range of VariablesPractice questions: 1.5 Casting and Range of Variables
- 1.6 Compound Assignment Operators Study guide: 1.6 Compound Assignment OperatorsRevision notes: 1.6 Compound Assignment OperatorsPractice questions: 1.6 Compound Assignment Operators
- 1.7 Application Program Interface (API) and Libraries Study guide: 1.7 Application Program Interface (API) and LibrariesRevision notes: 1.7 Application Program Interface (API) and LibrariesPractice questions: 1.7 Application Program Interface (API) and Libraries
- 1.8 Documentation with Comments Study guide: 1.8 Documentation with CommentsRevision notes: 1.8 Documentation with CommentsPractice questions: 1.8 Documentation with Comments
- 1.9 Method Signatures Study guide: 1.9 Method SignaturesRevision notes: 1.9 Method SignaturesPractice questions: 1.9 Method Signatures
- 1.10 Calling Class Methods Study guide: 1.10 Calling Class MethodsRevision notes: 1.10 Calling Class MethodsPractice questions: 1.10 Calling Class Methods
- 1.11 Math Class Study guide: 1.11 Math ClassRevision notes: 1.11 Math ClassPractice questions: 1.11 Math Class
- 1.12 Objects: Instances of Classes Study guide: 1.12 Objects: Instances of ClassesRevision notes: 1.12 Objects: Instances of ClassesPractice questions: 1.12 Objects: Instances of Classes
- 1.13 Object Creation and Storage (Instantiation) Study guide: 1.13 Object Creation and Storage (Instantiation)Revision notes: 1.13 Object Creation and Storage (Instantiation)Practice questions: 1.13 Object Creation and Storage (Instantiation)
- 1.14 Calling Instance Methods Study guide: 1.14 Calling Instance MethodsRevision notes: 1.14 Calling Instance MethodsPractice questions: 1.14 Calling Instance Methods
- 1.15 String Manipulation Study guide: 1.15 String ManipulationRevision notes: 1.15 String ManipulationPractice questions: 1.15 String Manipulation
AP® and Advanced Placement® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website. Marlbridge is an independent tuition provider. It is not an authorized AP course provider, an AP school or an exam centre, and these resources are not official College Board materials. Reading them or doing simulations does not meet the laboratory requirement of an AP science course.