Unit 1: Primitive Types Flashcards
What are primitives?
Primitives are the simplest elements available in a programming language. Ex. int, double, char, boolean
What is the correct way to initialize a primitive?
a) (Type) (Name) = new (Type)();
b) (Name (Type) = (Value);
c) (Type) (Name) = (Value);
d) (Type) (Name) == (Value);
c) (Type) (Name) = (Value);
ex: int num = 10;
What values can int hold?
The int data type stores whole numbers from -2,147,483,648 to 2,147,483,647.
What values does double hold?
The double data type stores decimal numbers up to 15 decimal digits
What values does char hold?
The char data type stores a single character/letter or ASCII values.
What is casting?
Type casting is when you assign a value of one primitive data type to another type.
Ex.
int total = 100; int numPeople = 40; double average = total / (double) numPeople; //Average will equal 2.5 as a double
What is a compound assignment operator?
A simpler way to assign an arithmetic value to a pre-existing value. (+= , /=, *=, -=)
Ex:
int num = 8; num += 7; //Now num will equal 15
What is the correct way to create a Scanner object?
a) Scanner = new Scanner(System.in);
b) Scanner name = new scanner;
c) Scanner name = new Scanner(System.in);
d) Scanner name = System.in;
c) Scanner objectName = new Scanner(System.in);
What does “final” do when used while initializing a primitive?
The word "final" makes it such that you cannot modify the variable after its initialization. Ex. final String name = "Chad"; //This String will always be Chad.
How do you swap the data stored in two variables of the same type?
Through the use of a temp variable that stores the data of one of the variables so that it is not lost.
For example, if you have variables a and b, and you want to switch their values:
int temp c;
c = a;
a = b;
b = c;
What does modulus (%) do?
It returns the remainder of a division operation.
Ex. 25%7 = 4
Ex. 25%5 =0
What is a reference type?
An example of a reference value would be a string.
Reference variables store the address of the value.
What is the correct way to name a variable?
Variables names must start with a letter, $, or _ and be lowerCamelCase examples include: - numApples - $numApples - _numApples
What does a String hold?
A String holds a list of ASCII characters or numbers. For example any word in the English language.
Ex. String numbers = “123456789101123456789011223344556677”;
String words = “Cat dog mice cows horses !@#$%^&&* more things”;
Is a string a Primitive
It is not because it has chars in it