Why is Java called a platform-independent language?
[span_1](start_span)Java is called platform-independent because its source code is compiled into an intermediate form called bytecode, which can run on any system regardless of the underlying operating system or hardware, provided the system has a Java Virtual Machine (JVM) installed.[span_1](end_span)
What are the primitive data types in Java?
Java has 8 primitive data types:
What is bytecode?
Bytecode is the highly optimized set of instructions generated by the Java compiler (.class file). It is not machine code for a specific CPU, but rather machine code for the Java Virtual Machine (JVM).
Describe the features of Java. (7 Marks)
Explain switch statement with Java code segment. (3 Marks)
The switch statement allows a variable to be tested for equality against a list of values (cases).
Example Code:
int day = 2;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Invalid");
}
Write a Java program to find the factorial of a number. (6 Marks)
public class Factorial {
public static void main(String[] args) {
int num = 5, fact = 1;
for(int i = 1; i <= num; i++) { fact *= i; }
System.out.println("Factorial: " + fact);
}
}
Why is Java called robust and secure? Explain. (4 Marks)
State the differences between abstract class and interface.
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | Can have both abstract and concrete methods. | Generally has only abstract methods (until Java 8). |
| Variables | Can have final, non-final, static, and non-static variables. | Only static and final variables. |
Define the following terms: (Select 2)
What is dynamic array? How do you create one-dimensional array in Java? (4 Marks)
A dynamic array is an array that can grow or shrink in size (like ArrayList). In standard Java, arrays are fixed-size, but dynamic behavior is achieved through the Collections Framework.
Creating 1D Array: int[] arr = new int[5];
Explain the following terms: (6 Marks)
What is the difference between method overloading and method overriding?
Overloading: Same method name but different parameters within the same class (Compile-time polymorphism).
Overriding: Same method name and parameters in a subclass as in the parent class (Runtime polymorphism).
What is wrapper class? Give example.
A wrapper class converts primitive data types into objects. Example: Integer is the wrapper for int.
Why is package used?
Packages are used to prevent naming conflicts, control access, and organize classes/interfaces into related groups.
(i) What are auto-boxing and unboxing? (4 Marks)
Autoboxing: Automatic conversion of primitive types to their corresponding wrapper classes (e.g., int to Integer).
Unboxing: Automatic conversion of wrapper classes to primitive types (e.g., Integer to int).
(ii) Write a Java program to implement the concept of interface. (6 Marks)
interface Animal { void eat(); }
class Dog implements Animal {
public void eat() { System.out.println("Dog eats"); }
}
public class Test {
public static void main(String[] args) {
Dog d = new Dog(); d.eat();
}
}
What are the different types of inheritance supported by Java? Explain with example. (5 Marks)
How does Java implement the concept of multiple inheritance? (5 Marks)
Java does not support multiple inheritance with classes to avoid ambiguity. Instead, it is implemented using Interfaces, where a class can implement multiple interfaces simultaneously.
Difference between Thread class and Runnable interface.
Thread class: You extend the class. You cannot extend any other class.
Runnable interface: You implement the interface. You can still extend another class, making it more flexible.
What are the uses of JDBC?
JDBC (Java Database Connectivity) is used to connect Java applications to databases, execute SQL queries, and retrieve/update data.
What is an exception? Types of exception? Java program to illustrate exception handling.
An exception is an event that disrupts the normal flow of a program.
Types: Checked (e.g., IOException) and Unchecked (e.g., ArithmeticException).
Example:
try { int data = 10/0; }
catch(ArithmeticException e) { System.out.println("Cannot divide by zero"); }
(i) What is multi-threading? Benefits? (4 Marks)
The ability of a CPU to execute multiple threads concurrently.
Benefits: Improved performance, better resource utilization, and responsiveness.
(ii) Write a simple Java program to connect to a database using JDBC. (6 Marks)
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user", "pass");
(i) What do you mean by applet? Explain applet life cycle in detail. (6 Marks)
An applet is a small Java program that runs within a web browser.
Life Cycle: 1. init() -> 2. start() -> 3. paint() -> 4. stop() -> 5. destroy().
(ii) Differences between AWT and Swing. (4 Marks)
(i) What do you mean by event handling? Four listeners. (4 Marks)
Event handling is the mechanism that controls the event and decides what should happen if an event occurs.
Listeners: ActionListener, MouseListener, KeyListener, WindowListener.
(ii) Design a simple calculator using Java swing. (6 Marks)
Requires JFrame, JTextField, and JButton components with ActionListener.