Chapter 1 & 2 Flashcards

(59 cards)

0
Q

Define Methods?

A

Groups of related statements in a class that perform a specific task. They are used to do specific tasks on their own objects and other objects. They are comparable to functions and subroutines in other languages. A well-designed method performs only 1 task.

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

How are attributes in a class defined? Discuss.

A

They are defined by variables. Each object in a class has instance variables (aka object variables) that differ between objects.

Each class attribute has a single corresponding variable.

Class variables are the variables that have the same value for every object in the class.

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

How do objects communicate with each other?

A

Methods. A class or object can call methods in another class or object for many reasons, including to report changes to other objects, to change something about the other object, or to ask the other object to do something.

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

Define expression.

A

A statement that returns a value.

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

Return value?

A

The value produced by a statement

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

What punctuation groups statements in Java? What is a group of statements grouped by this punctuation called?

A

Braces, {}. A block, or “block statement”.

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

Name and define the 3 types of variables in Java.

A

Instance, class, and local. Local variables are used inside method definitions or even smaller blocks of statements within a method. They cease to exist after the method or block is done being executed.

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

Variable names in Java must start with ________.

A

a letter, an underscore _, or a dollar sign $

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

What are the types of data that can be used for variables?

A

Primitive types (like Int, Boolean, floats, and characters), the name of a class or interface, or an array

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

Types, ranges, and defs of integers, NOT ints

A

Byte–8 bits, -128 to 127 (halves of 2^8)
Short–16 bits, -32, 768 to 32,767 (halves of 2^16)
Int–32 bits, (halves of 2^32)
Long–64 bits, halves of 2^64

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

What is a double re:floats?

A

A float with more decimal places

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

Difference between Boolean and boolean?

A

Class versus a variable type

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

Can a variable have a class as its type? How is that denoted?

A

Yes, for instance

Color hair;
String lastName= “Hopper”;

Notice the caps in the variable type

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

What is a constant? What are they useful for?

A

A variable whose value never changes. Useful for defining shared values for the use of all methods of an object.

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

What’s the diff between instance variables and methods versus class variables and methods?

A

Class variables occupy memory until a Java program is finished running; if a class variable references am object, that object will ALSO remain in memory. This often causes programs to run too slowly.

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

Packages?

A

A way to group related classes and interfaces. They allow groups of classes to be easily referenced in other classes, as well as eliminating potential naming conflicts among classes.

By default, Java classes can refer to the ones in the java.lang package using only short names. To use classes from another package, you must use the full package name, or use the “import” command to import the package in your source code file.

Because the Color class is contained in the java.awt package, you would call this java.awt.Color.

If the entire package has been imported, you can refer to the class simply as Color.

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

Interface?

A

Helps avoid problems with single inheritance. Allows a class to have behaviors that its superclass doesn’t.

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

Do the methods included in am interface define the additional behavior that it allows a subclass to have?

A

No

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

How do you assign values to variables?

A

=

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

How do you declare a constant?

A

Use the “final” keyword before the variable declaration, and include an initial value, like

final int YAR = 15

Yar is all caps as a naming convention

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

How do you present more than 1 variable or literal as the argument to println()?

A

The + combines them into one string

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

3 types of comments–describe

A

Single-line–preceded by two slash characters. //. Everything after to slashes to the end of the line is considered a comment.

Multi-line–/* text. */

Javadoc–/** text. **/. Certain utilities like javadoc create a set of web page records that document the functionality of a Java class.

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

How do you indicate that a literal should be a “long” integer?

A

Adding the letter L to the number, e.g.,

yarTotal=yarTotal + 4L

Adds the number 4 in the long format to the current yarTotal format.

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

How do you denote binary, octal, and hex as literals?

A

Octal–add a zero to the beginning of the number

Hex–add a zero and an x to the beginning (0x)

Binary–add 0b to the beginning

24
How do you notate a literal as a negative number?
Put the minus sign in front of the number
25
All float literals are _____
Considered to be double type floats, unless you add an f to the end of the number, e.g., float piValue=3.14f
26
How do you use exponents in floats as literals?
Put the letter e followed by the exponent (which can be a negative exponent) at the end of the number
27
How are underscores used in large integers?
In place of commas
28
How are character literals represented?
A single character surrounded by single quote marks
29
A string in Java is a ______ instead of a primitive data type.
Object
30
Give character escape codes for new line, tab, return, single/ double quotes,
\n, \t, \r, \', \"
31
Do you have to explicitly create a new object when working with a string?
No.
32
Expressions can be method calls? T/f
True
33
Define modulus in Java and give its notation
The remainder of two numbers, uses percent sign. 20%7=6
34
What is it important to be aware of when performing division?
Storing a division operator in an integer removed everything after the decimal. Many arithmetic operations involving integers produce an int regardless of the original operand type. Make sure your operands have the same type that you are trying to end up with
35
Which side of an assignment expression is calculated before the assignment takes place?
The left. E.g., int x=5; x=x+2; X is now 7
36
List the assignment operators
X+=y means x=x+y X-=y means x=x-y X*=y means x=x*y x/=y means x=x/y
37
When do assignment operators not work?
When either side of your statement is part of a complex expression
38
Give increment/decrement expressions. What do they do?
X=x++; x=x--; Add or subtract 1 from the variable
39
What's the difference between these two expressions and why? y=++x; y=x++;
In the first, y is equal to the original value of x plus 1. In the second, y is simply equal to the original value of x. X is also incremented by 1 in the second expression, while y is not.
40
When do the position of increment/decrement operators matter?
When part of a larger expression
41
Why is "assigning x to y before x is incremented" not totally correct for this expression? y=x++
Because Java evaluates everything on the right side of the expression before assigning a value to the left.
42
Give Boolean comparison operators
``` == equals != does not equal < less than > greater than = greater than or equal to ```
43
Give Boolean operators for each of the logical combinations.
AND is && or &. && involves no work on the expression on the right side of the && if the left side is false. OR is | or ||. || involves no work on the right side of the || if the left is true XOR is ^ NOT is !. It simply reverses, e.g., If yar>30 is true, then !(yar>30) returns false.
44
What is a shorthand way to append something to the end of a string?
+= e.g. yar=yar+"Vern"; Would be equivalent to yar+="Vern";
45
What are character escape codes?
Used for character string literals for characters that don't easily map to a specific character (like return, tab, new line, etc.)
46
Can string literals include character escape codes?
Yes, e.g., string mike="I said, \"io soy tu padre!\"";
47
Are strings stored as objects when used?
Not the basic types
48
Increments and decrements are called _____ if listed before a variable name, and ______ if listed after.
Prefix, postfix
49
Parentheses are used for___, periods are used for______, and square brackets are used for ______.
Grouping expressions Access to methods and variables within objects and classes Arrays
50
Instanceof operator?
Returns true or false depending on if the object is an instance of the named class or any of its subclasses
51
The new operator?
Used to create new instances of a class
52
<< >> >>>
Bitwise left and right shift?
53
?;
Ternary operator
54
Define "statement"
A simple command that causes something to happen
55
Define variable
A place where information can be stored while a program is running
56
How do you declare several variables of the same type in the same statement?
Separate by commas, like this Int age, weight, height; Or even assign values, like so int age=35, weight=150, height=57;
57
The concatenation operator can_____
Handle any variable or object value as if it were a string. If any part of a concatenation operation is a String or string literal, all elements of the operation are treated like strings
58
Operator precedence, give full list
. [ ] ( ) ++ -- ! ~ instanceof. new (type)expression. The new oper. */% + - << >> >>> Bitwise < > = Relational comparison == != Equality &. AND ^ XOR | OR && Logical AND || Logical OR ? : Ternary operator = += -= *= /= %= ^= Various assignments &= |= <>= >>>= More assignments