Obj 1: Handling date, time, text, numeric and boolean values Flashcards

1
Q

Java primitive type

A

Java has eight built- in data types.
A primitive is not an object in Java,
nor does it represent an object. A primitive is just a single value in memory, such as a number
or character.

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

What are the primitive types

A

boolean, byte, short, int, long, float, double, char

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

default values of:
- boolean
- byte
- short
- int
- long
- float
- double
- char

A
  • false
  • 0
  • 0
  • 0
  • 0L
  • 0.0f
  • 0.0
  • \u0000 (nil)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Does it compile
float x = 1.0 ;

A

No, a float requires the letter f or F following the number so Java knows it is a float. Without an f or F, Java interprets a decimal value as a double

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

Does it compile
long x = 123000;

A

No, a long requires the letter l or L following the number so Java knows it is a long.
Without an l or L, Java interprets a number without a decimal point as an int in most
scenarios.

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

short s = -123;
char c = (char) s;
Does it compile ?

A

Yes, short and char values can be cast to one another because the underlying data size is the same

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

A number in the code is called …

A

literal

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

long max = 3123456789;
Does it compile ?

A

No, Java complains the number is out of range.
The solution is to add the character L to the number:
long max = 3123456789L; // Now Java knows it is a long

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

Does Java allow other decimal number system.

A

Yes, Java allows you to
specify digits in several other formats:
* Octal (digits 0–7), which uses the number 0 as a prefix— for example, 017.
* Hexadecimal (digits 0–9 and letters A–F/a–f), which uses 0x or 0X as a prefix— for
example, 0xFF, 0xff, 0XFf. Hexadecimal is case insensitive, so all of these examples
mean the same value.
* Binary (digits 0–1), which uses the number 0 followed by b or B as a prefix— for
example, 0b10, 0B10.

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

double annoyingButLegal = 1_00_0.0_0;
does it compile ?

A

Yes, You can add underscores anywhere except at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point. You can even place multiple underscore characters next to each other.

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

Each primitive type has a wrapper class, which is an object type that corresponds to the primitive.
- boolean
- byte
- short
- int
- long
- float
- double
- char

A
  • Boolean
  • Byte
  • Short
  • Integer
  • Long
  • Float
  • Double
  • Character
    Only Boolean and Character do not extend the class Number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

int x = 123;
Integer y = Integer.valueOf(x);

String str = “123”;
Integer wrapper = Integer.valueOf(str);
int primitive = Integer.parseInt(str);

does code compile ?

A

Yes
There is also a valueOf() variant that converts a String into the wrapper class

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

String str = “””
Hello \s
World!”””;

String str = “””
Hello \
World!”””;

String str = “””
Hello \n
World!”””;

String str = “””
Hello \s World!”””;

String str = “””
Hello \nWorld!”””;

A

Hello
World! (2 lines)

Hello World! (1 line)

Hello

World! (3 lines)

Hello World! (1 line)

Hello
World! (2 lines)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
 int value = 3;                   // Stored as 0011
 int complement = ~value;         // Stored as 1100
 System.out.println(value);       // 3
 System.out.println(complement);  // - 4

how to find the bitwise complement of a number ?

A

multiply it by negative one and then subtract one.
```
System.out.println(-1value - 1); // - 4
System.out.println(-1
complement - 1); // 3
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
int pelican = !5;         
 boolean penguin = - true;  
 boolean peacock = !0;    

does it compile ?

A

No, you cannot apply a negation operator (- ) to a boolean expression, nor can you apply a logical complement operator (!) to a numeric expression.

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

Tip

A

Keep an eye out for questions on the exam that use numeric values (such as 0 or 1) with boolean expressions. Unlike in some other programming
languages, in Java, 1 and true are not related in any way, just as 0 and false are not related.

17
Q
 int parkAttendance = 0;
 System.out.println(parkAttendance);    
 System.out.println(++parkAttendance); 
System.out.println(parkAttendance);    
System.out.println(parkAttendance--);  
 System.out.println(parkAttendance);   

guess the output of each System.out.println ?

A
 System.out.println(parkAttendance);    // 0
 System.out.println(++parkAttendance);  // 1
  System.out.println(parkAttendance);    // 1
 System.out.println(parkAttendance--);  // 1
 System.out.println(parkAttendance);    // 0

The first pre- increment operator updates the value for parkAttendance and outputs the new value of 1. The next post- decrement operator also updates the value of parkAttendance but outputs the value before the decrement occurs.

18
Q
 float egg = 2.0 / 9;        
 int tadpole = (int)5 * 2L;  
 short frog = 3 -  2.0;       

does this compile ?

A

No, Casting is optional and unnecessary when converting to a larger or widening data type, but it is required when converting to a smaller or narrowing
data type. Without casting, the compiler will generate an error when trying to put a larger data type inside a smaller one.

Casting primitives is required any time you are going from a larger numerical data type to a smaller numerical data type, or converting from a floating-point number to an integral value.

19
Q
byte hat = 1;
 byte gloves = 7 * 10;
 short scarf = 5;
 short boots = 2 + 1;
  short boots = 2 + hat;  
 byte gloves = 7 * 100; 

Which lines do not compile ?

A

The 5th statement does not compile because hat is a variable, not a value, and both operands are automatically promoted to int. When working with values, the compiler had enough information to determine the writer’s intent. When working with variables, though, there is ambiguity about how to proceed, so the compiler reports an error. The 6th expression does not compile because 700 triggers an overflow for byte, which has a maximum value of 127

20
Q
 long goat = 10;
 int sheep = 5;
 sheep = sheep * goat; 
``` 
 Does it compile ?
 
 ```
long goat = 10;
 int sheep = 5;
 sheep *= goat;

Does this compile ?

A
  1. No, We are trying to assign a long value to an int variable. This last line could be fixed with an explicit cast to (int)
  2. Yes, The compound operator will first cast sheep to a long, apply the multiplication of two long values, and then cast the result to an int. Unlike the previous example, in which the compiler reported an error, the compiler will automatically cast the resulting value to the data type of the value on the left side of the compound operator
21
Q
long wolf = 5;
 long coyote = (wolf=3);
 System.out.println(wolf);   // 3
 System.out.println(coyote); // 3
A

The key here is that (wolf=3) does two things. First, it sets the value of the variable wolf to be 3. Second, it returns a value of the assignment, which is also 3.

22
Q
 boolean healthy = false;
 if(healthy = true)
   System.out.print("Good!");
A

While this may look like a test if healthy is true, it’s actually assigning healthy a value of true. The result of the assignment is the value of the assignment, which is true, resulting in this snippet printing Good!

23
Q
var monday = new File("schedule.txt");
 var tuesday = new File("schedule.txt");
 var wednesday = tuesday;
 System.out.println(monday == tuesday);    
 System.out.println(tuesday == wednesday); 
A
  1. false
  2. true

Even though all of the variables point to the same file information, only two references, tuesday and wednesday, are equal in terms of == since they point to the same object.

24
Q
System.out.print(null == null);  
A

true, In some languages, comparing null with any other value is always false, although this is not the case in Java.

25
Q
 int gibbonNumFeet = 2, wolfNumFeet = 4, ostrichNumFeet = 2;
 System.out.println(gibbonNumFeet < wolfNumFeet);      // true
 System.out.println(gibbonNumFeet <= wolfNumFeet);     // true
 System.out.println(gibbonNumFeet >= ostrichNumFeet);  // true
 System.out.println(gibbonNumFeet > ostrichNumFeet);   // false
A

the last example outputs false, because although gibbonNumFeet and ostrichNumFeet have the same value, gibbonNumFeet is not strictly greater than
ostrichNumFeet