Q1.
Feature Java C++
Memory
Automatic Garbage Collection Manual (requires delete/free)
Management
Platform Platform-independent (via Platform-dependent (compiled to
Dependency JVM) machine code)
Purely Object-Oriented (except Not purely (supports both
Object Orientation
primitives) procedural & OO)
Java Platform Independence:
Java code is compiled into bytecode by the [Link] runs on Java Virtual
Machine (JVM), which is [Link] makes Java write once, run
anywhere.
Java Security Features:
No explicit pointers
Bytecode verification
Security manager and sandboxing
Automatic memory management
[Link] class DivisibilityCheck {
public static void main(String[] args) {
try {
int a = [Link](args[0]);
int b = [Link](args[1]);
if (b == 0) {
[Link]("Division by zero is not allowed.");
} else {
if (a % b == 0) {
[Link](a + " is divisible by " + b);
} else {
[Link](a + " is not divisible by " + b);
} catch (Exception e) {
[Link]("Invalid input. Please enter two integers.");
[Link] Components:Class Loader
Bytecode Verifier
Runtime Data Areas (Heap, Stack, Method Area)
Execution Engine
Java Execution Process:
.java file → compiled by javac → .class (bytecode)
Bytecode is interpreted/executed by the JVM
JDK vs JRE vs JVM:
JDK (Java Development Kit): Contains JRE + development tools (javac, javadoc)
JRE (Java Runtime Environment): Contains JVM + libraries to run Java
JVM: Executes bytecode, platform-specific
Q4.
class Student {
String name;
int id;
float marks;
// Constructor
Student(String name, int id, float marks) {
[Link] = name;
[Link] = id;
[Link] = marks;
// Overloaded display methods
void display() {
[Link]("Name: " + name + ", ID: " + id);
void display(boolean showMarks) {
if (showMarks) {
[Link]("Name: " + name + ", ID: " + id + ", Marks: " + marks);
} else {
display();
Q5.
String:
Immutable (value cannot be changed once created)
Thread-safe because it cannot be modified
StringBuffer:
Mutable and thread-safe (synchronized methods)
StringBuilder:
Mutable but not thread-safe
Faster than StringBuffer in single-threaded environments