Chapter 2 Flashcards

1
Q

What is a statement?

A

A single command that causes something to happen

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

What punctuation mark terminates every statement in Java?

A

a semicolon (;)

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

A group of statements between an opening brace – { – and a closing brace – } – is called a…

A

block, or block statement

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

What is the difference between an instance variable and a local variable?

A

An instance variable defines an attribute of an object.
A local variable is used inside a method definition, or within even smaller blocks of statements inside the method. They cease to exist afterwards.

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

How would you declare a variable named “loanLength” that will hold an integer representing the length of a loan?

A

int loanLength;

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

How would you declare a variable named “message” that will hold text?

A

String message;

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

What is the most efficient way of declaring three String variables called “street,” “city” and “state”?

A

String street, city, state;

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

If you declare a Boolean variable without specifying its value, what initial value is it given automatically?

A

A value of “false”

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

How do you declare an integer variable called “box” that has an initial value of 200?

A

int box = 200;

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

Explain what would be wrong about declaring a variable like the following:
int 2013spending = 3000;

A

Variables may not begin with numbers.

They may begin with a letter, underscore character or dollar sign.

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

Describe the formatting convention that most Java programmers use when naming a variable.

A

Join several words together that describe the variable’s content.
Use lowercase for the first letter, then capitalize each successive word.

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

Why are data types such as integers and Booleans called “primitive” types?

A

They are built-in parts of the Java language. They are not objects, which makes them easier to create and use.

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

Since byte, short, int and long are all data types for storing integers, when might you set up a variable as a “byte” data type rather than an “int”?

A

A “byte” variable, which is limited to the range of -128 to 127, would use less memory. But since memory is more plentiful in today’s computers, it’s better to use larger data types like int.

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

What is the largest number that an “int” variable can hold?

A

just over 2 billion (2,147,483,647)

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

What are floating-point numbers?

A

Numbers with a decimal point

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

What are the two kinds of floating-point variables?

A

float

double

17
Q

When would you need to use the “double” data type?

A

You would only use this if dealing with decimal numbers where the size of the numbers will be more than 3.4 followed by 38 zeros.

18
Q

What is the “char” data type used for?

A

individual characters, including letters, numbers, punctuation and other symbols.

19
Q

“Void” is a data type. What does it represent?

A

Nothing – it’s used in methods to indicate they do not return a value.

20
Q

In addition to the primitive data types, what can be used to specify the data type of a variable?

A

A class.

If a variable has a class as its type, the variable refers to an object of that class or one of its subclasses.

21
Q

What symbol is used as the assignment operator, to assign a value to a variable?

A

= (equals sign)

22
Q

What is a constant?

A

A variable with a value that never changes

23
Q

What statement could you use to set the value of “pi” to 3.141592 and make it a constant?

A

final float PI = 3.141592;

Note: “final” is the keyword that turns a variable into a constant.

24
Q

What naming convention do most Java programmers use when they want to make it clear that the variable is a constant?

A

They capitalize all letters.

Example: PI

25
Q

How would you add a comment to the end of a line of code without causing a compiler error?

A

Use two slashes (//).

Example:
int creditHours = 3; // This is a comment

26
Q

How do you begin and end a multi-line comment?

A

Single slash (/).

Example:
/ This is a multiline comment
we are using for demonstration purposes. /

27
Q

When specifying a large integer literal such as 3800000, how can you make it more readable?

A

You can insert underscore characters where you would put commas.

Example:
3_800_000

28
Q

Is there a numeric equivalent for the boolean values of true and false?

A

No.

Some other languages equate true with 1 and false with zero. Not Java.

29
Q

How do you specify a new line in a print statement?

A

\n

30
Q

In Java, are string objects real objects?

A

Yes.

The “String” data type is capitalized, indicating it is a class.

31
Q

Since strings in Java are handled as objects, is it necessary to create a new object when working with them?

A

No. You get the power and flexibility of objects, but Java saves you the trouble of having to create them yourself.

32
Q

The mathematical symbols such as +, - and * are known as …

A

Operators

33
Q

What will be the value of x below?

int x = 27 / 10;

A

2, since an integer can only store whole numbers.

34
Q

The comparison operator that compares two values or variables is …

A

==

double equal signs

35
Q

The comparison operator that means “not equal to” is …

A

!=

question mark followed by equal sign

36
Q

What symbol do you use to combine two or more boolean expressions in an “AND” combination?

A

& (both sides will always be evaluated)

&& (if left side is false, right side will not be evaluated)

37
Q

What symbol do you use to combine two or more boolean expressions in an “OR” combination?

A

| || (if left side is false, right side will not be evaluated)

(both sides will always be evaluated)

38
Q

What symbols are used for grouping expressions?

A

( )

39
Q

What symbol is used for concatenating two or more strings?

A

+