Core Java Flashcards

(139 cards)

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

What is the difference between JDK and JRE?

A

JDK (Java Development Kit) is for developing Java programs and includes tools like the compiler (javac), debugger, and libraries. JRE (Java Runtime Environment) is for running Java programs and includes the JVM and standard libraries. JDK contains JRE.

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

What does JDK include?

A

JDK includes JRE + development tools such as compiler (javac), debugger, documentation tools, and utilities needed to build Java applications.

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

What does JRE include?

A

JRE includes JVM + core class libraries + runtime files required to execute Java programs.

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

Can you run Java code with only JRE installed?

A

Yes, you can run compiled Java programs, but you cannot compile new Java code because the compiler is part of JDK, not JRE.

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

Can you compile Java code with only JRE installed?

A

No. Compilation requires javac, which exists only in JDK.

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

What is the role of JVM inside JRE?

A

JVM executes Java bytecode, manages memory, performs garbage collection, and ensures platform independence.

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

JDK vs JRE vs JVM in one sentence?

A

JDK develops programs, JRE runs programs, JVM executes bytecode.

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

Why is Java called platform independent?

A

Because Java code compiles into bytecode that runs on any JVM regardless of operating system.

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

Which component actually executes Java bytecode?

A

The JVM executes bytecode instructions.

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

What happens if JVM is missing from a system?

A

Java programs cannot run because the execution engine is absent.

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

What is the Java Virtual Machine (JVM)?

A

The JVM (Java Virtual Machine) is a runtime engine that executes Java bytecode and provides platform-independent execution by translating bytecode into machine-specific instructions.

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

Is JVM platform dependent or platform independent?

A

JVM implementation is platform dependent (different JVM for each OS), but Java programs are platform independent because the same bytecode runs on any JVM.

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

What are the main responsibilities of JVM?

A

JVM loads class files, verifies bytecode, executes code, manages memory, and performs garbage collection.

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

What does JVM actually execute?

A

JVM executes compiled Java bytecode, not source code.

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

What is bytecode in Java?

A

Bytecode is the intermediate compiled form of Java source code generated by javac that runs on the JVM.

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

Why does Java achieve ‘Write Once, Run Anywhere’?

A

Because Java code compiles into bytecode that runs on any JVM regardless of underlying hardware or operating system.

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

What happens during class loading in JVM?

A

JVM loads .class files into memory, verifies their correctness, links them, and initializes static data before execution.

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

What is bytecode verification in JVM?

A

It is a security step where JVM checks code for illegal instructions, invalid memory access, and type safety violations before execution.

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

Does JVM manage memory automatically?

A

Yes. JVM automatically manages memory allocation and garbage collection for objects on the heap.

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

What is the difference between JVM and JRE?

A

JVM executes bytecode, while JRE includes JVM plus libraries required to run Java applications.

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

What are the main memory areas of the JVM?

A

The main JVM memory areas are Heap, Stack, Method Area, Program Counter Register, and Native Method Stack.

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

What is the Heap in JVM?

A

Heap is the runtime memory area where objects and instance variables are allocated. It is shared across all threads and managed by garbage collection.

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

What is the Stack in JVM?

A

Stack stores method frames including local variables, parameters, and partial results. Each thread has its own private stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What happens if Stack memory is full?
A StackOverflowError occurs, typically due to deep or infinite recursion.
26
What happens if Heap memory is full?
An OutOfMemoryError occurs when JVM cannot allocate new objects and garbage collection cannot free space.
27
What is the Method Area in JVM?
Method Area stores class metadata, runtime constant pool, method code, and static variables. It is shared among threads.
28
What is the Program Counter Register in JVM?
It stores the address of the current JVM instruction being executed for each thread.
29
Why does each thread have its own PC register?
Because each thread executes independently and must track its own instruction position.
30
What is the Native Method Stack?
It stores native method calls written in languages like C or C++ that are invoked via JNI.
31
Is Heap shared or thread-specific?
Heap is shared among all threads.
32
Is Stack shared or thread-specific?
Stack is thread-specific; each thread has its own stack.
33
What is a stack frame?
A stack frame is a block inside the stack that contains method-specific data like local variables, parameters, and return address.
34
Does JVM memory include ClassLoader?
No. ClassLoader is a JVM subsystem responsible for loading class files, not a memory area.
35
What is stored in the runtime constant pool?
It stores literals, symbolic references, and constants used by class methods at runtime.
36
Which JVM memory areas are thread-specific?
Stack, Program Counter Register, and Native Method Stack are thread-specific.
37
Which JVM memory areas are shared across threads?
Heap and Method Area are shared among threads.
38
What is a JIT compiler in Java?
The JIT (Just-In-Time) compiler is a JVM component that improves performance by compiling bytecode into native machine code at runtime.
39
When does JIT compilation happen?
JIT compiles code during program execution (runtime), not before execution like traditional compilers.
40
Why is JIT compiler faster than interpretation?
Because once bytecode is compiled into native machine code, it runs directly on hardware instead of being interpreted line by line.
41
Is JIT enabled by default in JVM?
Yes. Modern JVMs enable JIT automatically for performance optimization.
42
What problem does JIT solve?
It removes interpretation overhead and speeds up frequently executed code paths.
43
Does JVM interpret or compile code?
It does both: JVM initially interprets bytecode and then JIT compiles frequently used methods into native code.
44
What is a "hot spot" in JVM?
A hot spot is a frequently executed method or code block that JVM identifies and compiles using JIT for faster execution.
45
What is the difference between JIT and javac?
javac compiles source code into bytecode before runtime, while JIT compiles bytecode into native machine code during runtime.
46
Does JIT compile all code immediately?
No. JVM first interprets code and compiles only frequently executed sections to optimize performance.
47
What is the main tradeoff of JIT compilation?
It improves execution speed but adds startup overhead because compilation happens during runtime.
48
What JVM component detects frequently executed code?
The HotSpot runtime profiler identifies frequently executed code sections for JIT compilation.
49
Can JIT optimize code beyond normal compilation?
Yes. JIT can perform runtime optimizations like method inlining, loop unrolling, and dead code elimination based on actual execution behavior.
50
How is the Java platform different from other programming platforms?
Java is platform independent because its compiler converts source code into bytecode that runs on any JVM, while most languages compile directly into platform-specific machine code.
51
What makes Java platform independent?
Java compiles code into bytecode, which can run on any system that has a compatible JVM.
52
What does "Write Once, Run Anywhere" mean?
It means Java programs compiled into bytecode can run on any platform that has a JVM without recompilation.
53
Why do languages like C or C++ need recompilation for each platform?
Because they compile directly into native machine code specific to the target operating system and processor architecture.
54
What role does JVM play in cross-platform execution?
JVM translates platform-neutral bytecode into platform-specific machine instructions at runtime.
55
Is Java truly platform independent?
Java code is platform independent, but JVM implementations are platform dependent because each OS requires its own JVM.
56
What is the key difference between Java compilation and traditional compilation?
Traditional languages compile source code directly into machine code, while Java compiles into intermediate bytecode executed by JVM.
57
Why is Java considered both compiled and interpreted?
Because javac compiles source into bytecode, and JVM interprets or JIT-compiles that bytecode during execution.
58
Can the same Java .class file run on Windows, Linux, and macOS?
Yes, as long as each system has a compatible JVM installed.
59
What is the main advantage of Java’s platform model for enterprise systems?
It simplifies deployment and portability since the same compiled code runs across environments without modification.
60
Why is Java called a "Write Once, Run Anywhere" language?
Because Java source code compiles into platform-independent bytecode that runs on any system with a compatible JVM without needing recompilation.
61
What enables Java’s cross-platform portability?
The Java compiler converts code into bytecode, which can be executed by JVMs available for different operating systems.
62
Can Java programs compiled on Windows run on Linux or macOS?
Yes. The same .class or .jar files can run on any OS if a compatible JVM is installed.
63
What is the role of bytecode in Java portability?
Bytecode acts as an intermediate representation that is independent of hardware and operating systems.
64
Which tools are responsible for compiling and running Java programs?
javac compiles Java source code into bytecode, and java (JVM launcher) runs the compiled bytecode.
65
Is Java truly platform independent?
Java applications are platform independent, but JVM implementations are platform specific for each operating system.
66
Why don’t most programming languages support "Write Once, Run Anywhere"?
Because they compile directly into machine code tied to a specific platform rather than an intermediate portable format.
67
What would happen if JVM did not exist?
Java bytecode could not run, and Java would lose its platform independence.
68
What is required on a system to run Java programs compiled elsewhere?
A compatible Java Runtime Environment (JRE) containing a JVM.
69
Does Java portability mean identical performance on all systems?
No. Behavior is consistent, but performance can vary depending on JVM implementation and hardware.
70
What is a ClassLoader in Java?
A ClassLoader is a JVM subsystem responsible for dynamically loading .class files into memory when they are needed.
71
What is the main job of a ClassLoader?
It loads class bytecode from sources like filesystem, network, or JAR files and defines them in the JVM for execution.
72
What are the three main types of ClassLoaders in Java?
Bootstrap ClassLoader, Extension (Platform) ClassLoader, and Application ClassLoader.
73
What does the Bootstrap ClassLoader do?
It loads core Java classes such as java.lang and foundational libraries from the JDK runtime.
74
Is Bootstrap ClassLoader written in Java?
No. It is implemented in native code and is part of the JVM itself.
75
What does the Extension (Platform) ClassLoader do?
It loads classes from platform extension directories such as jre/lib/ext or platform modules.
76
What does the Application ClassLoader do?
It loads classes from the application classpath specified by CLASSPATH or command-line settings.
77
What is the delegation model in ClassLoader?
ClassLoaders follow parent-first delegation, meaning they ask the parent loader to load a class before trying themselves.
78
Why does Java use ClassLoader delegation?
It prevents duplicate class definitions and improves security by ensuring core classes are loaded only by trusted loaders.
79
What happens if a class is not found by any ClassLoader?
A ClassNotFoundException or NoClassDefFoundError is thrown depending on when the failure occurs.
80
What is the difference between ClassNotFoundException and NoClassDefFoundError?
ClassNotFoundException occurs when loading fails at runtime explicitly, while NoClassDefFoundError happens when a class was present at compile time but missing at runtime.
81
Can you create a custom ClassLoader?
Yes. Developers can extend ClassLoader to load classes dynamically from custom sources such as encrypted files or remote locations.
82
Why are ClassLoaders important for application servers?
They allow isolation between applications so different apps can load different versions of the same class.
83
What security role do ClassLoaders play?
They enforce namespace separation and prevent untrusted code from replacing core Java classes.
84
Is "main" a keyword in Java?
No. "main" is not a Java keyword; it is just the conventional method name recognized by JVM as the program entry point.
85
Why is the method name "main" special in Java?
Because JVM looks specifically for a method with signature public static void main(String[] args) to start program execution.
86
Can a class have multiple methods named main?
Yes. A class can overload main methods, but JVM will only execute the one with the exact entry-point signature.
87
What is the required signature of the main method?
public static void main(String[] args)
88
Why must main be static?
Because JVM calls it without creating an object of the class.
89
Why must main be public?
So JVM can access it from outside the class.
90
Can main return a value?
No. It must return void because JVM does not expect a return value.
91
Can main method be overloaded?
Yes, but only the exact signature main(String[] args) is used as entry point.
92
What happens if main method is missing?
The program compiles but fails at runtime with an error saying main method not found.
93
Can main be declared final or synchronized?
Yes. JVM does not restrict modifiers other than requiring public static void.
94
Does JVM require parameter name args specifically?
No. The parameter name can be anything; only the type String[] matters.
95
What is actually passed into main(String[] args)?
Command-line arguments entered when running the program.
96
Can we declare main as "public void static" instead of "public static void"?
No. In Java syntax, modifiers must come before the return type, so "void static" is invalid. The correct order is modifiers → return type → method name.
97
What is the correct order of elements in a Java method declaration?
Modifiers → Return type → Method name → Parameters → Exceptions → Body.
98
Can we write "static public void main" instead of "public static void main"?
Yes. Modifier order can be changed, so "static public void main" is valid Java syntax.
99
Why does Java allow flexible modifier order?
Because Java syntax defines modifiers as an unordered set before the return type.
100
What happens if return type is written before modifiers?
The code will not compile because Java syntax requires modifiers first.
101
Which parts of main method signature are mandatory for JVM entry point?
public, static, void, method name "main", and parameter type String[].
102
Is modifier order important for compilation?
No. Any order of modifiers is valid as long as they appear before the return type.
103
Why must return type come after modifiers?
Because Java grammar defines the return type as part of the method declaration that follows modifier declarations.
104
Is "void public static main" valid syntax?
No. Return type cannot appear before modifiers.
105
What compile error occurs if method syntax order is wrong?
The compiler throws a syntax error such as "illegal start of type" or "invalid method declaration".
106
What is the default value of a local variable in Java?
Local variables have no default value; they must be explicitly initialized before use.
107
Are local variables automatically initialized in Java?
No. Unlike fields, local variables are not initialized by the JVM and using them before assignment causes a compile-time error.
108
What happens if you use an uninitialized local variable?
The code fails to compile with an error like "variable might not have been initialized."
109
Why doesn’t Java assign default values to local variables?
To prevent logic errors and force developers to explicitly assign meaningful values before use.
110
What is the difference between default values of fields and local variables?
Fields (instance/static variables) get default values, but local variables do not and must be initialized manually.
111
What are default values of instance variables in Java?
Numeric types → 0, boolean → false, object references → null.
112
Do object-type local variables default to null?
No. Local variables of any type, including objects, have no default value and must be initialized.
113
Why is Java strict about local variable initialization?
It enforces safer code by preventing accidental use of undefined values.
114
Is this valid: int x; System.out.println(x);?
No. It fails compilation because x is not initialized.
115
Which variables does JVM initialize automatically?
Only instance and static variables, not local variables.
116
What is the value of String[] args if no command-line arguments are passed?
It is an empty array, not null.
117
Is args ever null in main(String[] args)?
No. JVM always passes a non-null array; if no arguments are provided, it passes an empty array.
118
What is the length of args when no arguments are supplied?
args.length is 0.
119
Why doesn’t JVM pass null for args?
To avoid NullPointerException and allow safe iteration without null checks.
120
What would happen if JVM passed null instead of an empty array?
Programs would need null checks before using args, increasing risk of runtime errors.
121
How can you check whether arguments were provided?
Check if args.length > 0.
122
What is printed by this code with no arguments: System.out.println(args.length)?
0
123
Is this safe: for(String s : args)?
Yes, because args is never null.
124
What is stored inside args array?
Command-line arguments passed when running the program.
125
Example command-line argument usage?
java MyClass hello world → args[0]="hello", args[1]="world"
126
What is the difference between byte and char in Java?
byte is an 8-bit signed numeric type used for raw binary data, while char is a 16-bit unsigned type used to represent Unicode characters.
127
What is the size of byte in Java?
byte is 8 bits (1 byte).
128
What is the range of byte in Java?
-128 to 127.
129
What is the size of char in Java?
char is 16 bits (2 bytes).
130
What is the range of char in Java?
0 to 65,535 because it is an unsigned type.
131
Why is char unsigned while byte is signed?
char represents Unicode characters which require only positive values, while byte is designed for numeric arithmetic and binary data.
132
Can char store numbers?
Yes. char internally stores numeric Unicode values that correspond to characters.
133
Example of char declaration?
char ch = 'x';
134
Is char considered a numeric type in Java?
Yes. char is an integral numeric type because it maps characters to integer Unicode values.
135
Which type is better for raw binary data storage?
byte, because it is specifically designed for small numeric values and binary data.
136
Can byte store character data directly?
Not reliably, because byte is only 8 bits and cannot represent all Unicode characters.
137
Why is char 16 bits in Java?
Because Java originally used UTF-16 encoding to support international character sets.
138
What happens if you assign a char to an int?
It is implicitly converted to its numeric Unicode value.
139
Which type uses less memory: byte or char?
byte uses less memory (1 byte) than char (2 bytes).