AP Computer Science A - Primitive Types (Unit 1) Test Review

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Modulus operator

%, works on integers/integer expressions ONLY (not on doubles), and it gives the remainder when the first number is divided by the second

Modulus assignment operator

%=

-only || and ||| are the correct answers -when we evaluate ||, we first do 5 % 3 because it's in parantheses and we get that is 2. Then we divide 2 by 2 to get 1, so this has to be one of the correct answers -when we evaluate |||, we first do 2 / 5, which evaluates to 0, since an int / int must be equal to 0, and when we do 2 / 5, we actually get 0.4, but Java truncates this to 0. Now when we add 0 with 1, we get that the answer is 1, which makes this option a correct answer as well -the only incorrect answer is |, and this is because when we evaluate 2 / 5, we get 0, and when we do 0 % 3, we get 0 again

***Disclaimer***: I did not make this question, and it is a practice question on AP Classroom Which of the following arithmetic expressions evaluates to 1?: |. 2 / 5 % 3 ||. 2 / (5 % 3) |||. 2 / 5 + 1

Compound assignment operators

+=, -=, *=, /=, %=

Char

-16 bit -it stores a single symbol using single quotes ' ' -it can store any symbol from the unicode character set -English uses characters from a subset of unicode called ASC || -it can print special characters using the \u escape sequence, followed by the code

Increment/Decrement operators

-An increment or decrements operator provides a more convenient and compact way to increase or decrease the value of a variable by one -For instance, the statement x = x + 1; can be simplified to x++; -For example, the statement x = x - 1; can be simplified to x--;

System.out.println(); and System.out.print();

-a method inside the main method -inside the parentheses, you put in a parameter/argument to get some output after the method acts on it

Modifier

-a word which we use before a variable declaration to modify its properties -the modifier usually comes before the data type when declaring variables -example: final -example of the application of a modifier: final double pi; pi = 3.14

Boolean

-can only either a true value or a false value -it is represented in memory as a single bit -it is a primitive data type -for example, we can set a value for this datatype like this: boolean y = true -in the example above, boolean is the data type, y is the name of the variable, and true is the value of the boolean variable

Integer.MIN_VALUE;

-tell us the minimum value in the domain of the integer data type -we should get a value that is equal to around -2 billion

Integer.MAX_VALUE;

-tells us the maximum value in the domain of the integer data type -we should get a value that is equal to around +2 billion

Execute

-the fourth and final step of Java running your code -basically Java will follow all the commands and directions that you gave it in the code, and it will run your code

Compiling

-the second step in Java running your code -basically, the computer checks for errors, and if there are no errors, then it converts your program to 0's and 1's

Concatenation

-values can be combined by using the + operator -for example, String name = "Naruto" System.out.print("My name is " + name) -in this example, we ____________ the string, "My name is " with the variable name -we never put variables in quote when ________________

Overflow

-when you add a number to the max value of some primitive data type's domain, and what happens is, the number wraps around and you are outputted the minimum value in the domain instead -for example, adding 10 the maximum value in the domain of an int, which is 2 billion, would cause to be outputted the minimum value in the domain, which is around -2 billion (notice how the numbers wrapped around)

-without having to use the math class -for example: int number = 4; int newNum = 4 * number; System.out.print(newNum);

Basic expressions can be utilized without having to use what? What is an example of a basic expression?

-it goes through your datatypes and checks the domain of that variable/all the possible values you can store for that variable and also the functions that we can do with the data in the variable -Java knows what the domain and functions are depending on the datatype

Before allocating memory for a certain variable, what does Java do?

an int cannot hold a decimal value, but doubles can hold either decimals or whole numbers

Can an int hold a decimal value? What about a double?

yes

Can the value of a variable change throughout the code?

-yes we can lose precision while dividing if we cast to an int instead of a double -for example: double num1 = 12.0; int num2 = 5; double num3 = ((int)num1 / num2); System.out.print(num3); -the code above will output 2, since we cast the double, 12.0, to an int first, which truncates its decimal and gives us 12 -next, we divide 12 by 5, so basically we are dividing int / int, and whenever we do this, the output must be an int -hence, Java does 12 / 5 to 2.4, but then truncates the 0.4 off since we divided an int by an int, and that must always equal an int -notice how we lost that 0.4 (which was our precision) -now when stores 2 to a double, it will add on a .0, so it actually sets the value of num3 as 2.0

Can we use casting to lose precision when dividing? What is an example?

nope

Can you declare multiple variables of DIFFERENT types on the same line?

yessir

Can you declare multiple variables of the SAME data type on the same line?

no

Can you set the values of multiple variables of any data type (whether they have the same data type of not), on the same line?

you can store an int in a double, but you cannot store a double in an int

Can you store an int in a double? Can you store a double in an int?

you can store numbers in strings, but you cannot use them for mathematical calculations (only for concatenation)

Can you store numbers in strings? Then can strings be used for mathematical calculations?

double, since they are 64 bit, while ints are only 32 bit

Do ints or doubles take up more memory?

never

Do we ever put variables in quotes when concatenating?

-it will truncate -for example, when you cast the double, 1.999, to an int by doing (int)1.999, you will get 1, NOT 2

Does Java truncate or round when you cast a double to an int?

no, the spacing is just for readibility -meaning that you can have single spaced or double spaced lines

Does spacing between lines matter when writing code? What is the spacing for?

-primitive data types have variables that hold the actual value that they are set to -reference/class data types have variables that hold a reference to a memory location

How are primitive data types different fro reference data types?

mod by 10

How can we find the one's place digit of an integer

(int)(x - 0.5);, where x is the variable

How can we round a double variable with a negative value to the nearest integer, instead of truncating?

(int)(x + 0.5);, where x is the variable

How can we round a double variable with a positive value to the nearest integer, instead of truncating?

-just mod that integer by 2 -if the output from the mod (the remainder) is equal to 0 then that means the integer is even and if it's anything else, then the integer is odd -this is because 2 divides into all even integers perfectly and it does not divide perfectly into any odd integer

How can you tell is an integer is even or odd, using modular division?

count = count % 3

How could we rewrite the following code?: count %= 3

count = count * 3

How could we rewrite the following code?: count *= 3

count = count - 1

How could we rewrite the following code?: count -= 1

count = count / 5

How could we rewrite the following code?: count /= 5

count +=10

How could we rewrite the following code?: count = count + 10

-they ALWAYS START with a lower case letter -for example: int age; -notice how the (i) is not capitalized -another example: double rainfall; -notice how the (d) is not capitalized

How do names of primitive data types like int, double, etc, always start?

-they ALWAYS START with capitalized letters -for example: String name; -notice how the (S) is capitalized

How do the names of reference data types like strings, always start with?

-by placing what you want to cast the value to, in front of the value you want to cast -what you want to cast to must be in parentheses (for example, if we wanted to cast to an int, we must write it like this: (int)) -for example, (double)2 will output 2.0, since we casted the integer, 2, to a double

How do we cast?

we can define it like this: int fortniteWins and then we can set it to a value like this: int fortniteWins = 20

How do we define a variable and then set its value?

import java.util.Scanner; Scanner scan = new Scanner(System.in); System.out.println(" ") String input = scan.nextLine -"scan" is the name of the scanner, "new Scanner" initiates a new scanner object, and "(System.in)" lets the user input into the console -when we use the print method, we essentially let the user input something into the console -In String input = scan.nextLine();, we are checking for what the user inputted into the console and then storing that in the variable, input

How do we use the scanner class?

System.out.print(); prints code without forcing a new line, but when you use System.out.println();, Java forces a new line after it executes the print statement

How is System.out.print(); different from System.out.println();?

-in the first expression, we first casted x to an int and then we added that int to 0.5, to get a double (since an int + double = double) -in the second expression, we first evaluated (x + 0.5) and then we cast the sum of x and 0.5 to an int (basically, we rounded x properly, assuming that it was a positive integer)

How is the first expression different from the second expression?: (int)x + 0.5 (int)(x + 0.5)

boolean, int, double, string

How would you arrange the following data types in terms of increasing memory required to store their values?: double, int, string, and boolean

-treat the integers as double (by either multiplying one of the integers by a double like 1.0 first, or by casting it to a double) and then store the result in a double (not an int, since ints CANNOT hold decimal values) -for example, we could do two things we if wanted to keep precision when doing 5 / 2: -one way: double num1 = (1.0) * 5 / 2; (basically, Java ALWAYS reads from left to right, so it sees that (according to pemdas), we must multiply first since it came before the division), and so if we simplify that it'd be: 5.0 / 2, which is equal to 2.5 (we can then safely store this in a double since doubles can hold decimals, while ints cannot) -the other way: double num1 = (double)5 / 2; (basically we just cast 5 to a double first, since java reads from left to right, and then we divided 5.0 / 2, which once again, gave us 2.5)

I we want to keep precision when dividing two integers, what do we need to do?

it gets some default value that is determined by its data type

If no value is set, then what value does a variable get? Why is this value determined by?

an int

If you add, subtract, or multiply two or more ints, what will you get?

-you MUST get an int -for example, if you do 5 / 2, in real life, you'll get 2.5, but Java sees that you are dividing two ints, so it actually truncates the 0.5 off of the 2.5 and instead outputs 2 (basically: int / int = int)

If you divide two ints, what will you get?

-the variable's data type is String -the name is quote -the value is "Don't quit"

In the following declaration and assignment of the variable, what is the variable's data type, what is its name, and what is its value?: String quote = "Don't quit";

-no, since in modding you divide to find the remainder and so the output that you get is the remainder of the divison, but when you divide, you simply find how many times one number goes into another -also, modular division only accepts ints, while normal division can accept ints or doubles

Is modding the same as dividing?

True

True or False: We can add, subtract, multiply, or divide 2 or more variables that have stored ints or doubles?

False

True or False: We can add, subtract, multiply, or divide 2 or more variables that have stored strings?

-values of variables that have been locked in place using the final modifier, meaning that they cannot change later on -these types of values cannot be changed by the user -for example, you might want to use the final modifier for a number like pi when creating code that finds the area of a circle

What are final values?

reference data types are data types that have variables that have been set values which cannot fit in the allocated memory spot given to them, so the reference data type directs Java to store the value of its variable in a location with plenty of space

What are reference data types?

-not having a semicolon ; after each line -if DOUBLE quotes are not placed around strings -if you forget to put brackets {} at the very start and the very end of the entire code

What are some common errors in Java?

-used to determine how many are left -used to determine if numbers are divisible by a specific number -used for clocks (since each hour is mod 12) -used in cryptography and encryption -used for patterns -used to tell even vs odd numbers -used for time calculations -used for money

What are some things that mod/modular division is used for?

variables that hold strings as values

What are string variables?

-compile-time error: incorrect syntax like spelling errors, no parentheses, etc. -run time error: a problem during program execution that causes the program to terminate/stop -logical error: the program runs but produces incorrect results (for example, if you used a wrong formula, etc.)

What are the 3 major types of errors when coding in Java?

-int -byte -short -long -double -float -char (a single letter) -boolean -we really only need to worry about double, int, and boolean

What are the 8 primitive data types?

1. it receives your code (the source code file) 2. it compiles (translates it to the computer) 3. now it has the bytecode file (a bunch of 0's and 1's) 4. now it executes (runs) the code

What are the four steps to a program running, in Java?

unary operators

What are the increment operators also known as?

-the name must be unique and cannot be a copy of some method in Java or something -it cannot begin with a number -the names are case sensitive (upper vs lower case) -use camelCase style for longer name, where the first word is all lower case and then all the words afterwards start with a capitalized letter

What are the rules for writing variable names?

-String, Integer, and Custom Classes -don't confused Integer with int, because, one starts with an upper case "I" and the other with a lower case "i"

What are the three reference data types?

-System.out.print(); -System.out.println();

What are the two methods used to print things in Java?

string data type variables

What can strings be stored inside?

-numbers (1, 2, 3, etc.) -letters (a, b, c, etc.) -symbols (#, $, %, etc.)

What can strings contain, as long they are in double quotes?

we can concatenate the two strings or the string and the variable

What can we do if we want to combine two or more strings or if we want to combine a string with something that is inside a variable?

for doubles

What data type do we use scan.nextDouble(); for?

for ints

What data type do we use scan.nextInt(); for?

for strings

What data type do we use scan.nextLine(); for?

they tell something to happen

What do methods do inside classes?

they manage data

What do programs do?

you get a double

What do you get when you do int + double, double + int, double - int, or int - double?

you still end up getting an int

What do you get when you do int + int or int - int?

you still end up getting a double

What do you get when you double + double or double - double?

a semicolon ;

What do you need to put after each line of code?

it is a javadoc comment

What does */ mean?

-it is an inline comment -for example, it could be something like: double sum; // we defined the double variable sum -in the example above, everything after // was the inline comment

What does // mean?

it is an "at author tag"

What does @author mean?

it helps us in keeping precision when we divide two integers

What does casting help us with?

its data type, its name, and its value

What does each variable have?

it only works on ints and NOT doubles

What does modular division only work on? What does it not work on?

prevents the user (that's inputting into the console), from changing that primitive data type's variable's value

What does putting the word "final" before a primitive data type name (such as int), do?

slots

What does the computer's memory has, that tells us the address of data?

it outputs the remainder of the result of (a) divided by (b)

What does the following output?: (a) modulo (b)

it adds a new line

What does the shortcut, /n, do?

it adds a tab

What does the shortcut, /t, do?

methods

What goes inside the class body?

-Java automatically truncates the double's decimal part and so we actually end up getting an int, and therefore, we lose precision -for example: double num1 = 5.0; int num2 = 2; int num3 = ((int)num1 / num2); -from the code, we can see that we originally cast num1 to an int, so Java truncated the decimal portion and then set num1 to 5 instead of 5.0 -since we were dividing an int by an int, we must have gotten an int, so when Java did 5 / 2, it got 2.5, but then truncated to 0.5, to get 2, in order to keep the answer an int -Java then stored 2 as the in the variable, num3

What happens when we cast a double to an int?

-Java automatically adds a .0 at the end of the int, in order to keep precision when doing calculations or when just storing that value in a variable -for example: int num1 = 5; int num2 = 10; double num3 = ((double)num1 / num2); -from this code, we can see that 0.5 will be stored as the value fro num3 since we originally casted num1, an integer, to a double, which caused precision to be kept throughout the entire calculation

What happens when we cast an int to a double?

the assignment operator, that sets the value of a variable

What is = in Java?

a datatype of variables that can hold either decimal values or integers

What is a double?

rainfall

What is a variable that a double can be used to store?

street address

What is a variable that a string can be used to store?

age

What is a variable that an int can be used to store?

a class is like your house and the method is like the power in your house

What is an analogy that can be used to relate classes to methods?

double num1 = 5.5; int num2 = (int)5.5; -notice how we will end up storing 5 as num2 since when we casted 5.5, a double, to an int, we had to truncate the decimal off

What is an example of a narrowing conversion?

it MUST be equal to an int

What is an int / int, equal to?

a datatype of variables that can only hold integers (positive and negative whole numbers)

What is an int?

the class body

What is below the class header?

public class ______________________;

What is the class header in Java?

-ints are 32 bit (take up less memory) and they CANNOT hold decimal values (only whole numbers) -doubles are 64 bit (take up more memory), their values can either be decimals or whole numbers, and they add .0 to the output when a whole number is inputted

What is the difference between an int and a double?

-128 to 127

What is the domain (range of input values) for a byte?

15 to 17 significant figures

What is the domain (range of input values) for a double?

6 to 9 significant figures

What is the domain (range of input values) for a float?

-9 quintillion to 9 quintillion

What is the domain (range of input values) for a long?

-32,000 to 32,000

What is the domain (range of input values) for a short?

-2 billion to 2 billion

What is the domain (range of input values) for an int?

it receives you source code file (the code you wrote)

What is the first step in Java running a code?

it executes the code (runs the code)

What is the fourth step in Java running code?

public static void main(String[] args);

What is the main method header?

0, since 0 modded by any number is always equal to 0 (this is because there is never any remainder when we divide anything into 0)

What is the output to the following code? Why?: 0 % 6

1, since 6 only goes twice into 12 and the remainder left over is 1

What is the output to the following code? Why?: 13 % 6

-it outputs 2.4 -this is because we first casted the int, 12, to a double, so it ended up become 12.0 -we then divided 12.0 by the int, 5, to get 2.4 (notice how Java didn't truncate the decimal, and this is because we divided a double / int, so we have to get a double as answer, since doubles always override ints)

What is the output to the following code? Why?: int num1 = 12; int num2 = 5; double num3 = ((double)num1 / num2); System.out.print(num3);

it is always equal to 0

What is the output when you divide a smaller number by a large number?

it is always going to be 0, since whenever we mod a smaller integer by a larger integer, the answer is always going to be equal to the smaller integer

What is the output when you mod 0 by any integer that's larger?

you get a run time error, since dividing by 0 is undefined

What is the output when you mod ANY integer by 0?

the answer is always going to be the smaller number (hence, 1 % 2 = 1)

What is the output when you mod a smaller integer like 1 by ANY integer that is larger than it, such as 2?

-in the code, we can see that we are dividing an int by an int, so we should get an int -but since we cast the first int to a double, we are now dividing a double / int, so we should get a double as the answer to the division calculation -this is a problem since we cannot store a double value into a variable of the int data type -hence, Java will end up giving us a run time error

What is the problem with the following code?: int num1 = 7; int num2 = 2; int num3 = ((double)num1 / num2);

it compiles the code (checks your code for any errors and then translates it to a bunch of 0's and 1's)

What is the second step in Java running code?

it gets the bytecode file (the code that only has 0's and 1's)

What is the third step in Java running code?

they must all be equal to a double

What must a double / double, double / int, or int / double, double x int, or double x double, must be equal to?

they must be equal to an int

What must an int / int or int * int, be equal to?

brackets {}

What must be placed at the very beginning and end of the code?

double quotations " "

What must we put all strings in?

declare the variable

What must you ALWAYS do before setting the value of a variable?

+

What operator do we use to do addition?

/

What operator do we use to do division?

*

What operator do we use to do multiplication?

-

What operator do we use to do subtraction?

reference data type

What type of data type are strings?

a reference data type

What type of data type is a string?

Java will concatenate the string stored in the variable with the number

What will happen if you put a plus sign (+), between a string variable that holds a STRING and some number?

Java will mathematically add the int/double with the other number and then once you output this, you see the sum of the int/double and the number

What will happen if you put a plus sign (+), between an int/double and some other number?

you'd get a run time error

What would happen if you previously defined the final value of an int or double (using the final modifier), and tried to add or subtract some number from that value, and then print it?

-this prints out 5.0, because what happens is java recognizes that we are dividing an int by an int (11 / 2), and so it solves that part (which we get as 5.5) but then it has to truncate the 0.5 off of the 5.5 since an int / int must equal an int (and int's cannot hold decimals) -so by truncating the 0.5 off of 5.5, we get that the expression, a / 2 is equal to 5 -now Java recognizes that we need to store this 5 as a double, so it will automatically add a .0 to the end of the 5 (because that's what happens when you store an int as a double) -so in the end, (b) ends up storing 5.0, and so when we print this, we see 5.0 -we could store 5.5 for (b) if we originally ended up casting a to an int

What would the following code print out? Why?: int a = 11; double b = a / 2; System.out.println(b);

-Java follows the order of operations when doing calculations (PEMDAS) -Java reads the calculation from left to right, so if you have something like x + y / 2, Java would do y/2 first since division comes before addition, and then it would add this quotient with x (be careful because doing x + y / 2 isn't the same as doing (x+y)/2 -remember that a double always overrides an int: double / int = double, int / double = double, double * int = double, etc.

When doing number calculations, what are some rules to keep in mind?

-by doing something like this System.out.println(variable1 + " " + variable2); -as you can see, we purposefully put a space between the two double quotes to create a space between the two variables

When we concatenate, how we put spaces between two variables?

in memory

Where is data stored?

Bit

a binary value (either a 0 or a 1)

Scanner

a class that allows us to read text from a given input

Narrowing conversion

a conversion from a larger data type to a smaller data type (basically we convert a data that type that is more precise and takes up more memory to a data type that is less precise and takes up less space)

Widening conversion

a conversion from a smaller data type to a larger data type (basically we convert a data that type that is less precise and takes up less memory to a data type that is more precise and takes up more space)

Run time error

a problem during program execution that causes the program to terminate/stop

Debugging

fixing errors in your programming

Compile time error

incorrect syntax like spelling errors, no parantheses, etc.

Modulo

means remainder

String literals

strings that are found inside the double quotes inside like the parentheses for the printing method

Variables

the names of each slot of the computer's memory that holds data from the program

Logical error

the program runs but produces incorrect results (for example, if you used a wrong formula, etc.)

Class

where the program lives in memory


Kaugnay na mga set ng pag-aaral

Chapter 5: Structure and Function of Macromolecules

View Set

HRTM 364 - EXAM3/FINAL (STUDY ALL)

View Set

Unit 5 Review -Special Right Triangles, similarity, trig

View Set

The American Revolution 1775-1783

View Set

Med Surg I - Midterm 1 - Units 1-3

View Set

History; Labor Practices: The Advent of Unions

View Set

Mental Health & Psych Nursing #1-NurseLabs

View Set