week01 Intro Flashcards

1
Q

Explain about class in Java

A

class is a blueprint that defines a type, behaviour, and attribute

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

What is behaviour of class in Java?

A

what an object of a class can do

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

What is attribute of class in Java?

A

variables for an object

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

What does instantiate mean in Java?

A

to create an instance(object) of a class

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

Explain how Java works

A

.java file are compiled to .class file containing Java Bytecode. Java Bytecode is executed just-in-time by Java Virtual Machine(JVM) , then JVM interprets the Bytecode.

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

What letter case is used for class names in Java?

A

TitleCase

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

What letter case is used for variables and methods in Java?

A

camelCase

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

Why is Java strongly-typed?

A

because every ‘thing’ has a type

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

Expalin why Java is statically-typed

A

Variables in Java need a declared type, and a variable can store a single data type.

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

What is an identifier in other word?

A

a variable

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

What does initialization mean in Java?

A

to assign a value to a variable

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

What are the special points of primitive in Java?

A
  1. no special object created on the heap. the value is stored where it is used
  2. primitive can only store data within a specified range
  3. immutable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What size is byte?

A

8 bits(integers in the range[-128, 127])

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

List 8 primitives

A
  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. char
  8. boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the order of widening conversion in Java?

A

byte -> short -> int -> long -> float -> double

char -> int -> long -> float -> double

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

We may lose precision when converting from what to what? (widening)

A

when converting from a long to a float

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

What is the order of narrowing conversion in Java?

A

double -> float -> long -> int -> short -> byte/char

char -> byte/short

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

When we do narrowing conversion, range is preserved. true or false

A

false. range is not preserved. when widening, range is preserved.

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

When we do narrowing conversion, we may lose what? Answer all.

A

precision and magnitude

when widening, we may lose precision only.

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

How many operators in Java?

A

38

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

A Java variable whose data type is a primitive stores …

A

a value

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

A Java variable whose data type is a class stores …

A

an address

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

“Primitive can store null”
true or false?

A

false. we can’t assign null to primitive types.

we can pass null as a parameter to a method that expects an object reference.

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

new operator is used for what?

A

to instantiate a class(to create an object)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Why is String a special class?
because we can instantiate with a literal 例: String greeting = "Hi";
26
What does cross-platform distribution mean?
we can execute Java bytecode on any system as long as if you have JVM on your system
27
List 5 details for JVM
1. cross-platform distribution 2. manages and optimizes memory 3. executes the program's bytecodes via interpretation and just-in-time compilation 4. Bytecode that is executed frequently is optimized and compiled directly into machine code 5. performs garbage collection
28
What is identity in Java?
Identity is about the unique address of an object in memory. If two references point to the same address, they are identical(==)
29
What is equality in Java?
Equality is about the state or content of an object. Two objects are equal if they have the same content(.equals() method)
30
Is `anotherNumber == number` true or false? `Integer number = new Integer(2522);` `Integer anotherNumber = number;`
True. anotherNumber and number point to the same object(address).
31
Is `number.equals(thirdNumber)` true or false? Integer number = new Integer(2522); Integer thirdNumber = new Integer(2522);
True. number and thirdNumber have the same content even though they are different objects.
32
What is the main difference between `==` and `.equals()` in Java?
`==` checks if two references point to the same object. `.equals()` checks if two objects have the same content.
33
What does `pass by value` mean in Java method call?
1. if the argument is a primitive, a copy of the value is passed to the method. 2. if the argument is an object, a copy of the reference to the object is passed. As a result, the original value or the object that the reference points to is not affected by what happens within the method.
34
What is String interning in Java?
String interning is Java's process of reusing instances of strings that are identical, storing them in the String Constant Pool.
35
What do references to interned string point to?(How do variables connect to a string that’s been interned in Java’s memory?)
In Java, when you create strings that are exactly the same, Java doesn't make new ones each time. Instead, all those strings are just pointers to one single string saved in a special place called the String Constant Pool.
36
How can you force string interning?
Use the `.intern()`method on a string to store it in the String Constant Pool.
37
Why is String interning important when comparing strings?
Because of interning, `==` compares memory addresses and may falsely imply string inequality. Always use `.equals()` for content comparison.
38
What are Java collections?
Java collections are data structures that hold objects. They use generic types, meaning you must use object types(no primitive types).
39
What does a Python List becomes in Java?
ArrayList<>
40
What does a Python Dictionary becomes in Java?
HashMap
41
What does a Python Set become in Java?
HashSet<>
42
Can Java collections store primitive types directly?
No. Java collections cannot store primitive types like `int`, `double`. Use wrapper classes like `Integer`, `Double`.
43
What are wrapper classes in Java?
Wrapper classes turn primitive data types like `int`, `char` into object like `Integer`, `Character`
44
How do you create a wrapper object from a primitive?
Use the wrapper class constructor: `Integer wrappedInteger = new Integer(64738);` `Character wrappedChar = new Character('c');`
45
How do you get the primitive value from a wrapper object?
Call methods like `.intValue()` or `.charValue()`: `int value = wrappedInteger.intValue();` `char character = wrappedChar.charValue();`
46
What is autoboxing?
Autoboxing is when Java automatically converts a primitive to a wrapper class when needed: `ArrayList list = new ArrayList<>();` `list.add(5); // 5 is autoboxed to Integer`
47
What is unboxing?
Unboxing is when Java automatically converts a wrapper class back to a primitive: `int num = list.get(0); // Integer is unboxed to int`
48
How is getting input in Java different from Python?
Java uses the `Scanner` class for input, not global functions like Python. Java requires explicit import and object creation to read different data types.
49
How do you prepare to use Scanner in Java?
First, import it with `import java.util.Scanner;`. Then create a Scanner object like `Scanner scan = new Scanner(System. in);`
50
How does Scanner read different types of input in Java?
Use `scan.nextInt()` for integers, `scan.nextDouble()` for doubles, `scan.next()` for a single word, and `scan.nextLine()` for a line of text.
51
What is a common issue with Scanner in Java?
`nextInt()` and `nextDouble()` do not consume the newline character. `nextLine()`afterwards may read an empty line.
52
How do you handle the newline character after `nextInt()` in Java?
Before reading the next line of text, use `scan.nextLine()` to clear the newline from the buffer.
53
How do you compile Java code in the command line?
Use `javac ClassName.java`. It compiles `ClassName.java` to `ClassName.class`.
54
How do you execute a compiled Java program in the command line?
Use `java ClassName`. Do not add `.class` after the class name.
55
What is the signature of the main method in Java?
`public static void main(String[] args)`. It accepts an array of `String`.
56
How do you pass command line arguments to a Java program?
Write them after the class name. Example: `java Program arg1 arg2 arg3`
57
What does a Java source file contain?
A `.java` file contains a single class definition written by the developer.
58
What does a Java class file contain?
A `.class` file contains the Java bytecode which is shareable, portable, and generated by the compiler from the `.java` file.
59
What does the Java compiler do?
It converts the `.java` file into a `.class` file that the Java interpreter can execute.
60
How does the Java Virtual Machine(JVM) execute Java bytecode?
During execution, the JVM interprets the `.class` file line by line and converts it to instructions that the CPU can understand.
61
What does the `System` class in Java provide?
Access to system resources including standard input(System.in), standard output(Sytem.out), error output streams(System.err), abd methods for loading files and libraries.
62
What is `println` in Java and which class does it belong to?
`println` is a method provided by the PrintStream class used to print a line of text to the current output stream.
63
What letter case do we use for constants?
All capital with "_" between words like UPPER_CASE.
64
What is the `switch` statement in Java.
The `switch` statement allows you to choose from a fixed number of options based on the value of a variable, which is more efficient than multiple `if-else` statements for this purpose.
65
What is the difference the `while` loop and `do-while` loop in Java?
The `while` loop checks its condition at the beginning before executing the loop's body, whereas the `do-while` loop executes the body first and then checks the condition.
66
True or false? "The `do-while` loop in Java will always executes its statements at least once."
True. `do-while` loop is designed to execute the loop block once before checking the guard condition for the first time