Chapter 2 Flashcards

1
Q

What is the difference between a string literal and a literal

A

A string literal is presented by putting double quotes around the text. A liteal is an explicit data value used in a program.

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

What is the string concatenation?

A

The string concatenation operator (+) concatenates / puts together different string together.
For example, “Peanut butter” + “and jelly”.

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

Why is the following statement wrong?System.out.println(“We present the following facts
for your extracurricular edification:”);

A

The System.out.println cannot be divided into two different lines. If we want to distribute the code onto two different lines, we will have to write two times the method.

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

When concatenating in a println method, what’s the difference between writing: + 34 + 35 and + (34 + 35) ?

A

Without parenthesis 34 and 35 get only concatenated, for them to be added and then displayed we will have to use the parenthesis.

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

What are escape sequences?

A

Escape sequences are a particular form of writing that allows particular characters written in a string not to be confused with synctactical parts of the programming language.

If you were to write …(“John:”I’m coming””);
Java would not understand which quotas are the extremes of a string and which are intended as a character.

So we write:
\" for commas.
\n new line
\t tab
\' quote
\\ backslash
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a variable?

A

A variable is a name for a location in memory that holds a value.

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

How do you declare a variable?

A

You define the data type (like int, double, char, etc), you write the desired name of the variable and you CAN (not necessary) write the variable’s value.

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

How do you declare a constant? What is its use in Java?

A

A constant is defined in the same way as a variable, but at the beginning there must be written “final”. The variable should follow the convention and be written with capital letters only.
Constants are used to distinguish specific variables from others, to avoid writing various variables with the same value and to avoid their values be changed by error.

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

What are the eight primitive data types in Java?

A

Integers: byte, short, int, long
Floating point numbers: float, double
Characters: char
Boolean values: boolean

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

What is the max value of each of these data types?

byte, short, int, long

A
byte = 127
short = 32 767
int = 2 147 483 647
long = > 9 * 10^18
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does a char variable store?

A

A char variable stores a single character. Those are delimited by single quotes: ‘a’, ‘X’, ‘7’, ‘,’, ‘\n’.

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

What is the difference between a char variable and a string?

A

A char variable contains only one character, while a string contains a series of characters.

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

What is a character set?

How many bits used Unicode?

A

It is an ordered list of characters, with each character corresponding to a unique number.
Unicode uses sixteen bits to store characters, allowing to 65 536 differenct characters.

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

What is the ASCII character set?

A

It is an older and smaller character set, but it’s still quite popular.

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

How do you assign characters?

A

char c = ‘\u005B’; -> you assign the character with that unicode value.
char d = 91; -> you assign the character with that unicode value, but written in decimal form.

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

What is a boolean variable?

A

A boolean variable contains either the true or the false condition.

17
Q

What is an expression?

A

An expression is a combination of one or more operators and operands that produces a result.

18
Q
What's the difference between the following writings?
14.0 / 3.0 or 14.0 / 3
and
14 / 3
?
A

With any floating number (or variable), all members of the division and of the following arithmetic operations will be transformed into a floating number, leading to result written in a floating way.
In the other case, two integers dividing will produce an integer result, discarding any obtained remainder.

19
Q

In a long arithmetic expression, how does Java “think”? What is the order of any operation inside the arithmetic expression?

A

Java reads left to right. When encountering an operator between two objects, it will first check if any other more important operators adjacent to the objects are present, if not it will execute the operation, even if other more important operations are to be found inside the whole arithmetic expression. Brackets can help to secure and to understand better the order of operations that Java will have to execute.

20
Q

In what order are the operations in the following expression executed?
a + b * c - d / e

A

a + b * c - d / e

2 1 4 3

21
Q

What are the ++ and – operators? And what is the difference between a++ and ++a

A

The ++ and – operators are respectively called increment and decrement. Connected to a variable they increment or decrement its value of 1.
Writing a++ means that Java will consider the value a first and will then add 1. Writing ++a means Java will take 1 first and then add it to a. This difference is useful when writing particular arithmetic expressions, as it can change the result.

22
Q

What does num += count mean? And other situations with num ?= count ?

A

num += count -> num = num + count.
num -= count -> num = num -count
num *= count -> num = num * count
and so on

23
Q

What is the difference between widening and narrowing data conversions? What are the three possibile data conversions?

A

When converting data from one type to another, some conversions can be subject to data loss: these are called narrowing conversions. Conversions that are not subject to data loss are called widening.

  • Assignment conversion:
    only widening. It happens when the assignment of a variable is equal to the variable of a lower data type (in the sense that the new variable has more “space” to store the value of the assigned variable, hence not losing any data)
  • Promotion:
    only widening. It happens when two variables of two different data type are put together in an arithmetic operation. All variables are taken to the same level, without loss of data.
  • Casting:
    Casting is a special case in which narrowing conversions are also possible. Normally, Java would signal an error everytime a narrowing operation will be displayed. If you put the desired data type inside brackets, Java will ignore the problem and narrow the data in the best possible way, although data loss is to be expected.
    Ex:
    int total = 50;
    float result = (float) total / 6.
24
Q
What does the following mean?
Scanner scan = new Scanner(System.in)
A
Scanner is an object created by the class Scanner.
scan is the variable created from the scanning.
new is the method that creates the new object Scanner.
Scanner(System.in) indicates from where the scan should be taken, in this case the keyboard.
25
Q

What does the “next” method of the Scanner class do?

A

When accompanied with nextInt or nextDouble it reads the next input on the keyboard and saves it as variable int or double (like in this example)

26
Q

What are the four types of variables in Java?

A
  1. Instance Variables (Non-Static Fields):
    In each object, any variable can have such variables, as they’re unique to each instance of a class.
  2. Class variables (Static Fields):
    These type of variables indicate the identifier that there’s only one instance of this variable in existance. So the number of times in which an object would have been instantieted will be irrelevant.
  3. Local variables:
    Local variables are temporarily created and are used by the methods during its execution.
  4. Parameters:
    A parameter acts as a variable inside the method to transmit a variable from outside the method.