L02: Java Fundamentals and Console Input Flashcards
What does each of these comments represent: //, /*….. /, /**……/?
Single line comment, multi-line comment, Javadoc comment.
What are the 3 restrictions to identifiers?
Can’t begin with a digit, can’t be a reserved word, limit length?
What are the allowed identifiers?
Letters, numbers, underscore (_), $.
What are the guidelines for class names, constants, and variables/methods?
Class Names; MyClass, Car
Constants: MAXIMUM, NUM_OF_WHEELS
Variables, methods: aVariable, fordCar, car_mercedes
Is “Class” a valid identifier?
Yes. The reserved word is “class” not “Class”
is “1AbC” a valid identifier?
No. Starts with a digit
What are the 4 primitive types to represent int in order of size?
byte, short, int, long.
What are the 2 ways to represent floating point numbers in order of size?
float, double.
What is a char and what is one unique property related to casting?
Stores a single character, and has a unique unicode value. This is why char can be cast to int.
What modifier does a constant variable use? What happens if this variable is modified?
final. Throws a compile-error since final variable cannot be modified.
What are variables?
Store information, creates a location in memory, must be declared before it is used (ex. int total;) If variable has no value, default value is used.
How are Java Class libraries organized?
They are organized into packages. (ex.java.text).
How do you import a class (list 2 ways)?
import java.text.DecimalFormat (import the class only)
import java.text.* (imports the entire class)
What are the 2 necessary steps to use a scanner?
import java.util.Scanner; // import class
Scanner input = new Scanner(); // Create object
When you are done reading inputs, what should be done?
Close the scanner using scanner.close()
System.out.println(“x+y=” + x + y);
System.out.println(“x+y=” + (x + y));
If x = 1 and, y = 2, what are the outputs?
x+y=12
x+y=3
System.out.println(“this is a
long string”);
Is this a valid way to print a String?
No, you must concatenate.
\b, \t, \n, ", ', \. What are these called and what do they do?
Escape sequences to insert special characters into a String. backspace, tab, newline, double quotes, single quotes, backslash.
What do these operations evaluate to? 10/8, 8/12, 10.0/8, 8/12.0
1, 0, 1.25, 0.6667
What are unary operators?
Operators that act on a single variable (ex. rate++)
What are binary operators?
Operators that act on two variables. (ex. rate + bonus)
How is this expression evaluated: answer = sum / 4 + MAX * lowest;
/ * + then finally =
Explain the difference between post-increment and pre-increment.
Post-increment increments by 1 but uses the old value in the expression. Pre-increment increments by 1 but the new value is used in the expression.
int intVariable = 2.99;
double doubleVariable = 2;
Are these expressions invalid or valid?
int is invalid, double is valid
What is arithmetic promotion?
If operands are of different types, they are promoted. (short, byte, char) -> int
What is the value and type of 2 / 4 * 1.0?
double 0.0. 2/4 is evaluated first as 0 then multiplied by double so arithmetic promotion.
What is the value and type of 1.0 * 2 / 4?
1.0 * 2 evaluated first as 2.0 then / 4 evaluates to 0.5.
What is assignment conversion?
If a variable has wider type than expression then the expression is widened automatically. If a variable has a smaller type than the expression then there will be a compilation error.
What is casting?
Explicitly forcing a type conversion.
double d;
d = 2/4;
d = (double) 2/4;
d = (double)(2/4);
d = 0, d = 0.5, d = 0.0
String name = “Your Name Here”
a. name.substring(9) ;
b. name.substring(1,6);
c. name.substring(1,name.length()-1);
d. name.length();
e. name.indexOf(‘r’);
f. name.indexOf(‘n’);
a. “ Here”
b. “our N”
c. “ our Name Her’
d. 14
e. 3
f. -1