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
Q

Why is String a special class?

A

because we can instantiate with a literal
例:
String greeting = “Hi”;

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

What does cross-platform distribution mean?

A

we can execute Java bytecode on any system as long as if you have JVM on your system

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

List 5 details for JVM

A
  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
Q

What is identity in Java?

A

Identity is about the unique address of an object in memory. If two references point to the same address, they are identical(==)

29
Q

What is equality in Java?

A

Equality is about the state or content of an object. Two objects are equal if they have the same content(.equals() method)

30
Q

Is anotherNumber == number true or false?

Integer number = new Integer(2522);
Integer anotherNumber = number;

A

True. anotherNumber and number point to the same object(address).

31
Q

Is number.equals(thirdNumber)
true or false?

Integer number = new Integer(2522);
Integer thirdNumber = new Integer(2522);

A

True. number and thirdNumber have the same content even though they are different objects.

32
Q

What is the main difference between == and .equals() in Java?

A

== checks if two references point to the same object.
.equals() checks if two objects have the same content.

33
Q

What does pass by value mean in Java method call?

A
  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
Q

What is String interning in Java?

A

String interning is Java’s process of reusing instances of strings that are identical, storing them in the String Constant Pool.

35
Q

What do references to interned string point to?(How do variables connect to a string that’s been interned in Java’s memory?)

A

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
Q

How can you force string interning?

A

Use the .intern()method on a string to store it in the String Constant Pool.

37
Q

Why is String interning important when comparing strings?

A

Because of interning, == compares memory addresses and may falsely imply string inequality. Always use .equals() for content comparison.

38
Q

What are Java collections?

A

Java collections are data structures that hold objects. They use generic types, meaning you must use object types(no primitive types).

39
Q

What does a Python List becomes in Java?

A

ArrayList<>

40
Q

What does a Python Dictionary becomes in Java?

A

HashMap<K, V>

41
Q

What does a Python Set become in Java?

A

HashSet<>

42
Q

Can Java collections store primitive types directly?

A

No. Java collections cannot store primitive types like int, double. Use wrapper classes like Integer, Double.

43
Q

What are wrapper classes in Java?

A

Wrapper classes turn primitive data types like int, char into object like Integer, Character

44
Q

How do you create a wrapper object from a primitive?

A

Use the wrapper class constructor:

Integer wrappedInteger = new Integer(64738);
Character wrappedChar = new Character('c');

45
Q

How do you get the primitive value from a wrapper object?

A

Call methods like .intValue() or .charValue():

int value = wrappedInteger.intValue();
char character = wrappedChar.charValue();

46
Q

What is autoboxing?

A

Autoboxing is when Java automatically converts a primitive to a wrapper class when needed:

ArrayList<Integer> list = new ArrayList<>();
list.add(5); // 5 is autoboxed to Integer

47
Q

What is unboxing?

A

Unboxing is when Java automatically converts a wrapper class back to a primitive:

int num = list.get(0); // Integer is unboxed to int

48
Q

How is getting input in Java different from Python?

A

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
Q

How do you prepare to use Scanner in Java?

A

First, import it with import java.util.Scanner;. Then create a Scanner object like Scanner scan = new Scanner(System. in);

50
Q

How does Scanner read different types of input in Java?

A

Use scan.nextInt() for integers, scan.nextDouble() for doubles, scan.next() for a single word, and scan.nextLine() for a line of text.

51
Q

What is a common issue with Scanner in Java?

A

nextInt() and nextDouble() do not consume the newline character. nextLine()afterwards may read an empty line.

52
Q

How do you handle the newline character after nextInt() in Java?

A

Before reading the next line of text, use scan.nextLine() to clear the newline from the buffer.

53
Q

How do you compile Java code in the command line?

A

Use javac ClassName.java.
It compiles ClassName.java to ClassName.class.

54
Q

How do you execute a compiled Java program in the command line?

A

Use java ClassName. Do not add .class after the class name.

55
Q

What is the signature of the main method in Java?

A

public static void main(String[] args).
It accepts an array of String.

56
Q

How do you pass command line arguments to a Java program?

A

Write them after the class name. Example:
java Program arg1 arg2 arg3

57
Q

What does a Java source file contain?

A

A .java file contains a single class definition written by the developer.

58
Q

What does a Java class file contain?

A

A .class file contains the Java bytecode which is shareable, portable, and generated by the compiler from the .java file.

59
Q

What does the Java compiler do?

A

It converts the .java file into a .class file that the Java interpreter can execute.

60
Q

How does the Java Virtual Machine(JVM) execute Java bytecode?

A

During execution, the JVM interprets the .class file line by line and converts it to instructions that the CPU can understand.

61
Q

What does the System class in Java provide?

A

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
Q

What is println in Java and which class does it belong to?

A

println is a method provided by the PrintStream class used to print a line of text to the current output stream.

63
Q

What letter case do we use for constants?

A

All capital with “_” between words like UPPER_CASE.

64
Q

What is the switch statement in Java.

A

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
Q

What is the difference the while loop and do-while loop in Java?

A

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
Q

True or false?
“The do-while loop in Java will always executes its statements at least once.”

A

True. do-while loop is designed to execute the loop block once before checking the guard condition for the first time