AP Computer Science A Study Guide - Primitive Types

AP Computer Science A Study Guide

  • AP is a registered trademark of the College Board.

Key Exam Details

  • The AP® Computer Science A course is equivalent to a first-semester, college-level course in computer science.
  • The exam is 3 hours long and includes 44 questions.
  • The exam is comprised of 40 multiple-choice questions (50% of the exam) and 4 free-response questions (50% of the exam).
  • The exam covers the following course content categories:
    • Primitive Types: 2.5%–5% of test questions
    • Using Objects: 5%–7.5% of test questions
    • Boolean Expressions and if Statements: 15%–17.5% of test questions
    • Iteration: 17.5%–22.5% of test questions
    • Writing Classes: 5%–7.5% of test questions
    • Array: 10%–15% of test questions
    • ArrayList: 2.5%–7.5% of test questions
    • 2D Array: 7.5%–10% of test questions
    • Inheritance: 5%–10% of test questions
    • Recursion: 5%–7.5% of test questions

Primitive Types

  • Around 2.5–5% of the questions cover Primitive Types.

Printing and Comments

  • System.out.print and System.out.println are used to display output on the console.
  • println moves the cursor to a new line after displaying data, while print does not.
  • A comment is any text in a source code file that is marked to not be executed by the computer.
  • Single-line comments are denoted by //.
  • Multiline comments are demarcated by /* and */.

Data Types

  • Every value in a program has a type.
  • Types are categorized as either primitive or reference types.
  • Only int, double, and boolean are used in AP Computer Science A.
  • Literals are representations in code of exact values.
TypeDescriptionExamples of literals
intInteger numbers3, -14, 21860
doubleFloating point numbers3.14, -1.0, 48.7662
booleanTrue and falsetrue, false

Arithmetic Expressions

  • int and double can be used in arithmetic expressions.
  • Arithmetic operators:
    • + addition
    • - subtraction
    • * multiplication
    • / division
    • % modulus
  • Precedence rules:
    1. *, /, %
    2. +, -
  • Operators within the same group are evaluated from left to right.
  • Parentheses can override precedence rules.
  • When an arithmetic operation involves two int values, the result is an int (truncates non-integer part).
    • Example: 7/47 / 4 evaluates to 1.
  • If an operation involves at least one double value, the result will be a double.

Variable Declaration and Assignment

  • Variables represent data.
  • A variable is a name associated with a piece of computer memory that stores a value.
  • Every variable has a type.
  • Variable declaration statement: type name; (e.g., int age;)
  • Assignment statement: variable = expression; (e.g., age = 18;)
  • Declaration and assignment can be combined: int age = 18;
  • The value of a variable can be changed using another assignment statement.
  • If a variable is intended to be constant, use the final keyword:
    • final int x = 5;
    • x = x - 2; // this line will cause a compiler error

Compound Operators

  • Compound operators update the value of a variable using an arithmetic operation.
Compound operatorExample statementEquivalent to…
+=x += 3x = x + 3
-=x -= 1x = x – 1
*=x *= 2x = x * 2
/=x /= 10x = x / 10
%=x %= 10x = x % 10
  • Increment and decrement operations add or subtract one from a variable.
Increment
x++x += 1x = x + 1
Decrement
x--x -= 1x = x - 1

Casting

  • Values of a certain type can only be stored in a variable of that type.
  • Casting operators (int) and (double) can be used to convert values to another type.
  • Casting a double to an int results in truncation.
    • Example: (int)12.8 evaluates to 12.
  • In some cases, int values will automatically be cast to double values.
  • It is legal to store an int value in a double variable.
  • When calling a method that declares a double parameter, it is legal to pass an integer value in as the actual parameter.

Free Response Tip

  • Be careful about storing accumulated values in an int variable, especially when finding an average.
  • Store the accumulated value in a double variable, or cast to a double before dividing to find the average.
  • Otherwise, the calculated average will be truncated, even if the result is stored in a double variable.

Sample Primitive Types Questions

  • Consider the following code segment:

    int x = 9;
    int y = 2;
    int z = 1;
    System.out.println(x / y * 1.5 - z);
    
    • What is printed when the code segment is executed?
    • The correct answer is B. 5.0. Explanation: 9/2 evaluates to 4 (integer division). 4 multiplied by 1.5 evaluates to 6.0 (a double data type). 6.0 – 1 evaluates to 5.0.
  • Consider the following code segment:

    double a = 3.6;
    int b = (int)a + 2;
    double c = b;
    System.out.print(a + " " + b + " " + c);
    
    • What is the output of the following code segment?
    • The correct answer is A. 3.6 5 5.0. Explanation: The first line assigns 3.6 to a. Typecasting a as an int evaluates to 3. 3 added to 2 evaluates to 5, which is assigned to b. 5 is assigned to c, but is automatically widened into the double data type, 5.0.
  • Consider the following code segment:

    int a = 8;
    System.out.print("*****");
    System.out.println(a);
    System.out.println(a + 2);
    System.out.println("*****");
    
    • What is printed when the code segment is executed?
    • The correct answer is A.
      text *****8 10 *****
      Explanation: System.out.println advances the cursor to the next line after printing. A row of asterisks is printed without a newline, then the value of a is printed with a newline, then the value of the expression a+2 is printed with a newline, and finally a row of asterisks is printed with a newline.

Using Objects

  • About 5–7.5% of questions will cover this topic.
  • All values in Java belong to either a primitive type or a reference type.
  • An object is a compound value that has attributes (data) and methods that can access or manipulate the attributes.
  • A class is a blueprint for objects. A class specifies what attributes and methods an object will have.

Constructing and Storing Objects

  • An object is created from a class by calling the class constructor along with the new keyword.
  • The name of a constructor is the same as the name of the class it belongs to.
  • The signature of a constructor consists of the name of the constructor along with the list of types of parameters that it expects.
  • A class may define multiple constructors as long as their signatures differ (constructor overloading).
  • Example:
    • Rectangle(int width, int height)
    • Rectangle(int x, int y, int width, int height)
    • Valid constructor calls:
      • new Rectangle(5, 6)
      • new Rectangle(-1, 2, 3, 8)
    • Invalid constructor calls:
      • new Rectangle(3.2, 1) // invalid since the first parameter is double
      • new Rectangle(1, 2, 3) // invalid since it has three parameters
  • An object needs to be stored in a variable whose type is compatible with the class the object belongs to.
  • Example:
    • Rectangle myRectangle = new Rectangle(5, 6);
  • A variable that refers to an object is called a reference variable.
  • The special value of null is reserved for reference variables that do not contain a reference to any actual object.

Calling Void Methods

  • Interaction with objects is done primarily by calling their methods.
  • Every method has a signature, consisting of its name and a (possibly empty) list of the types of parameters it defines.
  • Methods can be overloaded, meaning multiple methods with the same name may exist in a class, as long as their signatures are different.
  • When a method is called, execution of the program is interrupted, and control is transferred to the method. When the method is complete, execution continues at the method call.
  • Void methods do not return a value and can only be called as standalone statements.
  • A method is called using the dot operator between the name of the object and the name of the method, followed by a list of parameters in parentheses.
  • Example:
    • myRectangle.grow(4, 4);
  • A method cannot be called on a null value, so if myRectangle was null, the statement would cause a NullPointerException.

Calling Non-Void Methods

  • When a method is not void, it has a return type.
  • The method returns a value, and the method call expression evaluates to this value.
  • Since it has a value, it can be used as part of an expression in place of any value of the specified type.
  • Example:
    • int someValue = 2 * myRectangle.getHeight() + 1;
  • If a method has side effects in addition to returning a value, there may be instances when you do not care about the returned value.
  • If you only care about the side effects of a method, it can be called as if it were a void method, even if it returns a value.
    • myRectangle.getHeight();

Strings

  • A string is a sequence of characters.
  • String literals are enclosed in double quotes, as in `