Lecture1b- Introduction to Java Flashcards

1
Q

What are the three levels of programming languages?

A

1️⃣ Machine Language – Binary instructions the CPU understands.
2️⃣ Assembly Language – Unique to a specific computer
3️⃣ High-Level Language – Uses human-readable syntax, translated into machine code.

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

What are key features of Java?

A

✅ High-level – Easy to read and write.
✅ Object-oriented – Everything is an object.
✅ Platform-independent – Runs on multiple systems.
✅ Multithreaded – Can handle multiple tasks at once.
✅ Secure – Designed for safety.
✅ Distributed – Used in network-based applications.

Java is NOT JavaScript!

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

What are the main components of the Java ecosystem?

A

✔ Java SE (Standard Edition) – Core Java features.
✔ Java EE (Enterprise Edition) – Used for large-scale applications.
✔ Java ME (Micro Edition) – Used in embedded systems.

🔹 Java Runtime Environment (JRE) – Runs Java programs.
🔹 Java Development Kit (JDK) – Includes JRE + compilers/debuggers.
🔹 Java Virtual Machine (JVM) – Translates bytecode to machine code.

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

What is object-oriented programming?

A
  • a style of programming focused on the design of creation of virtual objects
  • can be thought of as solving a problem by modeling it as software objects
  • objects are the things being modeled
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does Java execute a program?

A

1️⃣ Write code → Save as .java file.
2️⃣ Compile → Use javac filename.java (produces .class file).
3️⃣ Run → Execute with java filename (no .class extension needed).

Java compiles to bytecode, which the JVM interprets.

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

What is the difference between syntax and semantics?

A

✔ Syntax – Rules for writing valid Java code (e.g., ; at the end of statements).
✔ Semantics – The meaning of the code (e.g., 5 + 5 makes sense, but “Java is blue” does not).

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

What are the three types of errors in Java?

A

1️⃣ Syntax Errors – Violates Java’s rules (e.g., missing ;).
2️⃣ Runtime Errors – Occurs during execution (e.g., division by zero).
3️⃣ Logic Errors – Wrong program behavior (e.g., using + instead of -).

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

What are Java’s naming conventions?

A

✔ Classes: TitleCase → MyClass
✔ Methods & Variables: camelCase → myVariable
✔ Constants: ALL_CAPS → MAX_VALUE

Java is case-sensitive: total, Total, and TOTAL are different!

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

What are the main components of a Java program?

A

✔ Classes – Define objects and methods.
✔ Methods – Contain code logic (main() method is required).
✔ Statements – Executable commands.

Example Java Program:

```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
~~~

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

What does public static void main(String[] args) mean?

A

✔ public – Accessible anywhere.
✔ static – No need to create an object.
✔ void – No return value.
✔ main – Java’s entry point.
✔ (String[] args) – Takes command-line arguments.

Must be written exactly like this or Java won’t run the program!

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

What are the different types of comments in Java?

A

✔ Single-line: // This is a comment
✔ Multi-line: /* This is a multi-line comment */
✔ Javadoc: /** Used for documentation */

Comments improve code readability but don’t affect execution.

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

Why is white space important in Java?

A

✔ Improves readability.
✔ Java ignores extra spaces, tabs, and line breaks.
✔ Use consistent indentation (2-4 spaces per level).

Example:

```java
if (x > 0) {
System.out.println(“Positive number”);
}
~~~

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

How does Java handle the + operator?

A

✔ Numbers: Performs addition → 5 + 5 = 10
✔ Strings: Performs concatenation → “Hello “ + “World” → “Hello World”

Order matters! Example:

```java
System.out.println(“24 and 45 concatenated: “ + 24 + 45); // “24 and 45”
System.out.println(“24 and 45 added: “ + (24 + 45)); // “24 and 45 added: 69”
~~~

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

What are escape sequences in Java?

A

Special characters written with \.

Sequence Meaning
\b Backspace
\t Tab
\n Newline
\r Carriage return
"\tDouble quote
'\tSingle quote
\\tBackslash

Example:

```java
System.out.println(“I said "Hello" to you.”); // Output: I said “Hello” to you.
~~~

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

What does “Write Once, Run Anywhere” (WORA) mean in Java?

A

Java programs do not depend on a specific operating system or hardware.

Compiled Java code (bytecode) runs on any JVM, making Java platform-independent.
Unlike C/C++, Java does not need separate compilation for different OS types.

Java’s portability is a major advantage!

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

How do you compile and run a Java program using the command line?

A

✔ Compiling:

```sh
javac MyProgram.java
~~~

✔ Running the compiled class file:

```sh
java MyProgram
~~~

Do NOT include .class when running the program!

17
Q

What are the two main types of data in Java?

A

✔ Primitive Data Types:
Directly store values (int, double, char, boolean, etc.).
✔ Reference Data Types:
Store references to objects (String, Scanner, Arrays, etc.).

Example:

```java
int a = 10; // Primitive type (stores 10 directly)
String s = “Hello”; // Reference type (stores memory location of “Hello”)
~~~

18
Q

What is Java bytecode?

A

The intermediate form called bytecode that is compiled into class files.

✔ Bytecode is not machine code but runs on the JVM (Java Virtual Machine).
✔ This makes Java platform-independent, as any device with a JVM can run Java code.

Unlike compiled C/C++ programs, Java bytecode is interpreted at runtime by the JVM.

19
Q

What is printf() in Java, and how is it used?

A

It formats output precisely, unlike println().

✔ Example – Formatting Decimal Places:

```java
double price = 12.997;
System.out.printf(“Total price: $%.2f\n”, price);
~~~

Output:

Total price: $12.99

✔ Common Format Specifiers:

Format Code Meaning Example Output
%d Integer 5
%f Floating-point 3.1416
%s String “Java”
%c Character ‘A’

Use printf() when precision matters (e.g., displaying money values).

20
Q

what are the valid ways to declare main string?

A

write “public static “ or “static public” – both valid
* We can declare “String[]” in any valid form:
main(String[] args)
main(String… args)
main(String []args)
main(String args[])

21
Q

What is the order of the compilation process?

A
  1. Java Source Code (Program.java)
  2. Java Compiler (Program.class)
  3. Translation into bytecode
  4. Send to Java Virtual Machine (JVM)
  5. Run Program in OS + Hardware
22
Q

What is the order of the programming flowchart?

A
  1. edit and save program
  2. compile program
  3. execute program and evaluate results
  4. if there are any errors in any step of the process, go back to step 1
23
Q

what is the main method to memorize?

A

public static void main(String[] args)

static public also acceptable