Complete Exam Answer Guide
Based on: Java The Complete Reference, 9th Edition by Herbert Schildt
Semester II | Year II | All 57 Questions | 8 Marks Each
,Q1. What is bytecode in Java?
[8 Marks]
Definition:
Bytecode is a highly optimized set of intermediate instructions generated by the Java compiler (javac). It is NOT
native machine code (executable code). Instead, it is a platform-independent representation of the program that
can be executed by the Java Virtual Machine (JVM) on any platform.
Key Concept:
When you write a Java program and compile it, the Java compiler does not convert your code into CPU-specific
machine instructions. Instead, it converts your source code (.java file) into bytecode (.class file). This bytecode acts
as an intermediate form.
How Bytecode Works:
1. Write Java source code in a .java file
2. javac compiler compiles it into .class file (bytecode)
3. JVM reads the .class file and executes bytecode
4. JVM translates bytecode to native machine code at runtime
Compilation and Execution:
Source Code (.java) --> javac compiler --> Bytecode (.class) --> JVM --> Output
C:\> javac Example.java // creates Example.class (bytecode)
C:\> java Example // JVM reads bytecode and runs it
Why Bytecode is Important:
• Platform Independence (WORA): The same .class file runs on Windows, Linux, Mac because each
platform has its own JVM that interprets the same bytecode
• Security: JVM acts as a sandbox - it controls execution and prevents harmful access to system resources
• Performance: Bytecode is highly optimized. HotSpot JVM uses Just-In-Time (JIT) compiler to convert
frequently used bytecode into native code at runtime for better performance
• Portability: Programs don't need to be recompiled for each platform
JIT Compiler:
The HotSpot JVM uses a Just-In-Time (JIT) compiler that converts selected portions of bytecode into native
machine code in real time (on-demand), greatly improving execution speed. Not all bytecode is compiled at once -
only the portions that benefit from compilation are compiled.
Bytecode vs Native Code:
Native code is CPU-specific executable code. Bytecode is intermediate and platform-independent. Bytecode
requires JVM to execute; native code runs directly on the processor. Although bytecode execution via JVM is slightly
slower than native code, the JIT compiler significantly reduces this gap.
Q2. Explain features of Java.
[8 Marks]
Java is designed with many key features which are often called 'Java Buzzwords'. These features make Java unique
and powerful for modern programming.
1. Simple
Java was designed to be easy to learn for professional programmers. It eliminates complex features of C/C++ such
as pointers, operator overloading, multiple inheritance, and explicit memory management. If you know C/C++,
moving to Java requires very little effort.
2. Object-Oriented
Java is fully object-oriented. Every program in Java is built using classes and objects. Java supports all OOP
principles: Encapsulation, Inheritance, and Polymorphism. Java's object model is simple, clean, and easy to extend.
,3. Platform Independent (Architecture-Neutral)
Java achieves platform independence through bytecode. The Java compiler produces .class files (bytecode) that
can run on any platform with a JVM. The motto is 'Write Once, Run Anywhere (WORA)'.
javac Hello.java --> Hello.class --> Runs on Windows/Linux/Mac via JVM
4. Secure
Java provides a secure execution environment. Applets are confined within the JVM sandbox and cannot access
the host file system without permission. Java eliminates pointers, which prevents unauthorized memory access.
Class loader, bytecode verifier, and security manager enhance security.
5. Robust
Java emphasizes reliability. It has strict compile-time and runtime type checking. Java provides garbage collection
(automatic memory management), exception handling, and eliminates pointers. These features greatly reduce
runtime errors.
6. Multithreaded
Java has built-in support for multithreaded programming. Multiple threads can execute simultaneously within a
single program. Java provides Thread class, Runnable interface, and synchronization mechanisms to handle
concurrent programming.
7. Distributed
Java is designed for distributed computing over networks. It supports TCP/IP protocols natively. Classes like URL,
URLConnection, Socket, ServerSocket allow network programming. Remote Method Invocation (RMI) allows
calling methods over a network.
8. Dynamic
Java programs carry runtime type information. This allows dynamic linking of code. Classes can be loaded on
demand. Reflection API allows runtime inspection of classes and objects.
9. Interpreted and High Performance
Java bytecode is interpreted by JVM. Although interpretation is slower than native execution, Java's JIT compiler
compiles bytecode to native machine code at runtime, providing near-native performance.
10. Portable
Java data types have a strictly defined range across all platforms (e.g., int is always 32 bits). There is no
'implementation-dependent' behavior. This ensures consistent program behavior on all platforms.
11. High Performance
With the JIT (Just-In-Time) compiler, Java achieves near-native execution speed. The HotSpot JVM identifies 'hot
spots' in the code and compiles them to native code for maximum performance.
Q3. Explain OOP principles in Java.
[8 Marks]
Object-Oriented Programming (OOP) is the foundation of Java. According to Herbert Schildt, Java is built around
four fundamental OOP concepts: Abstraction, Encapsulation, Inheritance, and Polymorphism.
1. Abstraction
Abstraction means hiding complex implementation details and showing only the essential features. Humans
manage complexity through abstraction - for example, we drive a car without knowing the internal workings of the
engine.
In Java, abstraction is achieved using abstract classes and interfaces. You define what a class should do (via
method signatures) without specifying how it does it.
abstract class Shape {
abstract double area(); // abstract method - no body
}
class Circle extends Shape {
, double r;
Circle(double r) { this.r = r; }
double area() { return 3.14 * r * r; } // implementation
}
2. Encapsulation
Encapsulation is the mechanism that binds together code (methods) and data (variables), keeping both safe from
outside interference. It is implemented using access modifiers: private, public, protected.
The public interface of a class represents what external users can access. Private members can only be accessed
by the class itself. This protects the internal state of an object.
class BankAccount {
private double balance; // private - hidden from outside
public void deposit(double amount) { balance += amount; }
public double getBalance() { return balance; }
}
3. Inheritance
Inheritance is the process by which one class acquires the properties (fields and methods) of another class. The
class that inherits is called the subclass (child class), and the class being inherited from is the superclass (parent
class). It supports hierarchical classification.
class Animal {
String name;
void eat() { System.out.println(name + " is eating"); }
}
class Dog extends Animal { // Dog inherits from Animal
void bark() { System.out.println("Woof!"); }
}
class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.name = "Tommy";
d.eat(); // inherited method
d.bark(); // own method
}
}
4. Polymorphism
Polymorphism means 'many forms'. It allows one interface to be used for a general class of actions. The specific
action is determined by the exact nature of the situation. In Java, polymorphism is achieved through method
overloading (compile-time) and method overriding (runtime).
class Animal {
void sound() { System.out.println("Animal makes sound"); }
}
class Cat extends Animal {
void sound() { System.out.println("Meow"); } // overriding
}
class Dog extends Animal {
void sound() { System.out.println("Woof"); } // overriding
}
class Main {
public static void main(String[] args) {
Animal a;
a = new Cat(); a.sound(); // prints Meow
a = new Dog(); a.sound(); // prints Woof
}
}