L02: Java Fundamentals and Console Input Flashcards

1
Q

What does each of these comments represent: //, /*….. /, /**……/?

A

Single line comment, multi-line comment, Javadoc comment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the 3 restrictions to identifiers?

A

Can’t begin with a digit, can’t be a reserved word, limit length?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the allowed identifiers?

A

Letters, numbers, underscore (_), $.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the guidelines for class names, constants, and variables/methods?

A

Class Names; MyClass, Car
Constants: MAXIMUM, NUM_OF_WHEELS
Variables, methods: aVariable, fordCar, car_mercedes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Is “Class” a valid identifier?

A

Yes. The reserved word is “class” not “Class”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

is “1AbC” a valid identifier?

A

No. Starts with a digit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the 4 primitive types to represent int in order of size?

A

byte, short, int, long.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the 2 ways to represent floating point numbers in order of size?

A

float, double.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a char and what is one unique property related to casting?

A

Stores a single character, and has a unique unicode value. This is why char can be cast to int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What modifier does a constant variable use? What happens if this variable is modified?

A

final. Throws a compile-error since final variable cannot be modified.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are variables?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How are Java Class libraries organized?

A

They are organized into packages. (ex.java.text).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you import a class (list 2 ways)?

A

import java.text.DecimalFormat (import the class only)

import java.text.* (imports the entire class)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the 2 necessary steps to use a scanner?

A

import java.util.Scanner; // import class
Scanner input = new Scanner(); // Create object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When you are done reading inputs, what should be done?

A

Close the scanner using scanner.close()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

System.out.println(“x+y=” + x + y);
System.out.println(“x+y=” + (x + y));
If x = 1 and, y = 2, what are the outputs?

A

x+y=12
x+y=3

17
Q

System.out.println(“this is a
long string”);
Is this a valid way to print a String?

A

No, you must concatenate.

18
Q

\b, \t, \n, ", ', \. What are these called and what do they do?

A

Escape sequences to insert special characters into a String. backspace, tab, newline, double quotes, single quotes, backslash.

19
Q

What do these operations evaluate to? 10/8, 8/12, 10.0/8, 8/12.0

A

1, 0, 1.25, 0.6667

20
Q

What are unary operators?

A

Operators that act on a single variable (ex. rate++)

21
Q

What are binary operators?

A

Operators that act on two variables. (ex. rate + bonus)

22
Q

How is this expression evaluated: answer = sum / 4 + MAX * lowest;

A

/ * + then finally =

23
Q

Explain the difference between post-increment and pre-increment.

A

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.

24
Q

int intVariable = 2.99;
double doubleVariable = 2;
Are these expressions invalid or valid?

A

int is invalid, double is valid

25
Q

What is arithmetic promotion?

A

If operands are of different types, they are promoted. (short, byte, char) -> int

26
Q

What is the value and type of 2 / 4 * 1.0?

A

double 0.0. 2/4 is evaluated first as 0 then multiplied by double so arithmetic promotion.

27
Q

What is the value and type of 1.0 * 2 / 4?

A

1.0 * 2 evaluated first as 2.0 then / 4 evaluates to 0.5.

28
Q

What is assignment conversion?

A

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.

29
Q

What is casting?

A

Explicitly forcing a type conversion.

30
Q

double d;
d = 2/4;
d = (double) 2/4;
d = (double)(2/4);

A

d = 0, d = 0.5, d = 0.0

31
Q

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

a. “ Here”
b. “our N”
c. “ our Name Her’
d. 14
e. 3
f. -1