Java SE 11: Variables & Operators Flashcards
Variables can be initialized with.
A. Only A value
B. A value or without.
C. Without a value
B. A value or without.
All variables must have a variable _____
Type
You can set the value of a variable without having to declare the type a second time.
True or False
True
String zip;
zip = “83705”;
Variables have default value of __________
uninitialized
When declaring a string variable the string type must be declared with __________ letter
Uppercase
What are the naming guidelines for variables.
Begin each variable with a ______________ letter. Subsequent words should be _______________.
What are the naming guidelines for variables.
Begin each variable with a lowercase letter. Subsequent words should be capitalized.
Ex. myVariable
Names are not case-sensitive.
True or False
False
Name are case-sensitive
Names cannot include a white space
True or False
True
Names should be mnemonic and indicate to the casual observer the intent of the variable.
True or False
True
The Undserscore Character _ is a Legal within a vairable name
True or False
False
As of Java SE 9 the underscore character (“_”) is not a legal character and won’t compile in Java SE9 or later
What type of variable is followed by name of variable set to a string in quotes
String
String name = “Steve”;
What variable type has positive or negative real values containing decibel
double
double price = 9.99
Which variable type holds true or false values?
boolean
boolean outOfStock = false;
What is the default value of a boolean value?
false
String address = “123 Oak St”
Is this variable:
A. Declared
B. Initialized
C. Declared and Initialized
C. Declared and initialized
What type of variable is a positive or negative whole number?
Integer
int quantity = 2;
How do you declare multiple variables of the same type on one line?
Use comma to separate each variable declaration.
String zip = “83705”, customer = “customer”;
What word means to combine multiple strings by joining them
Concatenation
What symbol is used for Concatenation?
+
IndexOf() and Substring() methods provided by the ______ class in Java
String
What does the IndexOf method do.
Gives the location of a character in a string.
Example:
String ipAddress = “122.72.1.45”;
int index1 = ipAddress.indexOf(‘.’);
System.out.println( index1);
System.out.println( index1 + 1);
: 3
: 6
What does the substring method do.
You provide it two arguments that are locations in a string and it prints out what’s in-between two points in a string.
String ipAddress = “122.72.1.45”;
int index1 = ipAddress.indexOf(‘.’);
int index2 = ipAddress.indexOf(index1 + 1)
String secondbyte = ipAddress.substring(index +1, index2)
System.out.println(secondbyte);
: 72
How do you represents commas in integer (int) numbers
Underscore _
int lowest = -2_147_483_648
Can double variable types use scientific notation?
Yes or No
Yes
double scientificValue = 2.345123E22;
Can double types using decimal ( . ) notation
Yes or No
Yes
double decimalValue = 3743743948.2134846
What are the standard mathematical operators in Java
Plus +
Minus -
Divide /
Multiply *
List operators by precedence:
- Multiplication & Division
- Addition
- Parentheses
- Increment and Decrement operators (++ or –)
- Parentheses - IF there are nested parentheses start from middle and work your way out
- ++ or –
- Multiplication and division operators, evaluated from left to right
- Addition and subtraction operators, evaluated from left tor right
You can set __________ by using parentheses ( )
Precedence
Which of the following variable declarations and/or initializations are correct?
- string name;
- boolean isComplete = false;
- int count =5; quantity =2;
- boolean isComplete = “true”;
boolean isComplete = false;
Given the following variable declarations, what variable type should be used for the product of quantity * price?
int quantity = 2;
double price = 9.99;
- String
- int
- boolean
- double
double
Which of the following is an uncommon method of declaring and/or initializing a String?
- String zip = “12345”, country = “USA”;
- String customer;
- String address = “123 Oak St”;
- String hello = new String(“Hello, World”);
String hello = new String(“Hello, World”);
Select the operator/character used for concatenating Strings?
- &
- .
- ~
- $
- +
- *
+
What is the output of the following code?
String phone = “555-548-1254”;
System.out.println(phone.substring(3, 6));
- 548
- 5-5
- -54
- 48-
-54
Which of the following will result in a compile failure?
- double price = 75;
- int quantity = 5.5;
- double price = 25.99;
- int quantity = 10;
int quantity = 5.5;
What is the answer of the following division using Java ints?
int a = 7;
int b = 3;
System.out.println(a / b);
- 4
- 2
- 2.3333…
- 3
2
Put the following operators in order of evaluation precedence in a mathematical expression.
Multiplication and division operators
Addition and subtraction operators
Operators within a pair of parentheses
Increment and decrement operators (++ or –)
- Operators within a pair of parentheses
- Increment and decrement operators (++ or –)
- Multiplication and division operators
- Addition and subtraction operators
What is the value of the variable c
after evaluating the expression?
int c = 50 – 8 * 2 / 4 – 8 + 3
- 16
- 41
- -94
- 61
41
Which of the following statements correctly assigns the value “Bob wrote 3 Java programs.” to the msg variable?
▢ msg = name + “ wrote “ + 3 + “ Java programs.”;
▢ msg = name + “ wrote “ + 2+1 + “ Java programs.”;
▢ msg = name + “ wrote “ + num “ Java programs.”;
▢ msg = “Bob wrote “+ (2+1) + “ Java programs.”;
√ msg = name + “ wrote “ + 3 + “ Java programs.”;
▢ msg = name + “ wrote “ + 2+1 + “ Java programs.”;
▢ msg = name + “ wrote “ + num “ Java programs.”;
√ msg = “Bob wrote “+ (2+1) + “ Java programs.”;