Java Programming

 Java Programming

Module 1: Introduction (8 Questions)

  1. What are the main features of the Java programming language?

  2. Explain the structure of a Java program with an example.

  3. Describe the role of the Java Virtual Machine (JVM) and Java compiler.

  4. Explain the concept of portability and platform independence in Java.

  5. Discuss Java data types, variables, and operators with examples.

  6. What are control statements in Java? Write examples for each category.

  7. Explain the concept of classes and objects in Java.

  8. Write a Java program to demonstrate constructor overloading and method overloading.



Module 2: Inheritance (9 Questions)

  1. Define inheritance. Explain its advantages with examples.

  2. What is method overriding and how is it used in Java?

  3. Explain runtime polymorphism with a real-world example.

  4. Differentiate between abstract classes and interfaces.

  5. Explain multilevel and hierarchical inheritance in Java.

  6. What is the purpose of the super and final keywords?

  7. Describe how packages are created and imported in Java.

  8. Explain the use of access modifiers (public, private, protected, default).

  9. Write a program to demonstrate interface implementation and multiple inheritance in Java.



Module 3: Exception Handling and Multithreading (9 Questions)

  1. What are exceptions in Java? Why are they used?

  2. Explain the difference between checked and unchecked exceptions.

  3. Write a Java program to demonstrate multiple catch blocks.

  4. Explain the use of throw, throws, and finally keywords.

  5. What is exception chaining and how is it implemented?

  6. Explain the concept of custom exception creation in Java.

  7. Define multithreading. How is it implemented using Thread class and Runnable interface?

  8. Explain thread lifecycle and thread synchronization in Java.

  9. Write a Java program that demonstrates inter-thread communication.



Module 4: JDBC (8 Questions)

  1. What is JDBC? Explain its architecture.

  2. Describe the steps to connect a Java application with a database using JDBC.

  3. Explain the role of JDBC drivers and their types.

  4. Write a Java program to insert and retrieve records from a database table.

  5. What is the purpose of Statement, PreparedStatement, and CallableStatement interfaces?

  6. Explain JDBC-ODBC bridge and its limitations.

  7. Discuss SQL exceptions and error handling in JDBC.

  8. How can you perform database transactions (commit and rollback) in JDBC?



Module 5: AWT, Swing & Applets (15 Questions)

  1. Differentiate between AWT and Swing.

  2. What are containers and components in AWT? Give examples.

  3. Explain layout managers: FlowLayout, BorderLayout, GridLayout, and CardLayout.

  4. Write a simple Java AWT program to design a calculator interface.

  5. Describe the event delegation model in Java.

  6. What are different types of event classes and listeners in Java?

  7. Write a program to demonstrate handling button click events in AWT.

  8. What is the purpose of ActionListener, KeyListener, and MouseListener?

  9. Explain the Swing hierarchy and advantages over AWT.

  10. Write a simple Swing application using JFrame and JButton.

  11. Explain how menus and toolbars are created in Swing.

  12. What is the applet lifecycle? Explain with example code.

  13. Describe the use of HTML tags for embedding applets.

  14. Write a program for an applet that displays a moving banner.

  15. Discuss applet security restrictions and Java sandboxing.

Module 1: Introduction

Question 1: What are the main features of Java programming language?

Java is popular due to its major features:

  • Object-Oriented: Focuses on real-world entities such as classes and objects, supporting features like inheritance, polymorphism, encapsulation, and abstraction. This makes code easier to organize and maintain.

  • Platform Independent: Java code is compiled into bytecode which can run on any system with a Java Virtual Machine (JVM). This means "Write Once, Run Anywhere," making distribution across platforms straightforward.

  • Robust: Java performs strong type checking at compile time and runtime. Features like exception handling, garbage collection, and absence of pointers make Java applications less prone to crashes and leaks.

  • Secure: Java offers runtime security checks, a secure class loader, and automatic memory management, helping prevent unauthorized code execution.

  • Simple: Its syntax is simpler than C++ as it removes complex features like explicit pointers and operator overloading.

  • Multithreaded: Java supports multithreading, allowing multiple tasks to run concurrently for efficient program design.

  • Distributed: Java can create distributed applications using technologies like RMI and EJB, connecting several computers on a network.

Question 2: Explain the structure of a Java program with an example.

A typical Java program has:

  • A package declaration (optional)

  • One or more import statements (optional)

  • A class definition containing variables and methods

  • Main method as entry point

Example:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Explanation:

  • public class HelloWorld creates a class called HelloWorld.

  • Inside the class, public static void main(String[] args) serves as the entry point for program execution.

  • System.out.println("Hello, World!"); prints a message to the user.

Question 3: Describe the role of the Java Virtual Machine (JVM) and Java compiler.

  • Java Compiler (javac): Converts Java source code (.java files) into bytecode (.class files). The compiled bytecode is an intermediate representation, not tied to any particular computer architecture.

  • Java Virtual Machine (JVM): The JVM interprets the bytecode and translates it into machine-specific code at runtime. It is responsible for program execution, memory management, garbage collection, and security. The combination enables platform independence, as the same bytecode can run on any machine with the JVM installed.

Question 4: Explain the concept of portability and platform independence in Java.

Java enables portability and platform independence by compiling source code into bytecode, which is not specific to any hardware or operating system:

  • The bytecode is interpreted or compiled at runtime by the JVM.

  • As a result, you can run the same compiled .class file on Windows, Linux, or Mac, provided each has a compatible JVM.

  • This paradigm is known as "Write Once, Run Anywhere" (WORA).

Question 5: Discuss Java data types, variables, and operators with examples.

  • Data Types:

    • Primitive: int, float, double, char, boolean, byte, short, long

    • Example: int age = 25; double salary = 10125.50; char grade = 'A';

  • Variables: Names attached to data values; must have a declared type.

    • Example: String name = "Alice";

  • Operators: Used to perform operations:

    • Arithmetic: +, -, *, /, % (e.g., int sum = age + 5;)

    • Relational: ==, !=, >, <, >=, <= (e.g., boolean result = age > 18;)

    • Logical: &&, ||, ! (e.g., if (isAdult && hasID) {...}).

Question 6: What are control statements in Java? Write examples for each category.

Control statements determine the flow of execution:

  • Conditional:

    • if, if-else, switch

    • Example:

if (score > 50) System.out.println("Passed");
else System.out.println("Failed");


  • Looping:

    • for, while, do-while

    • Example:

for (int i = 0; i < 3; i++) System.out.println(i);

  • Branching:

    • break, continue, return

    • Example:

for (int i = 0; i < 5; i++) {
    if (i == 2) continue;
    if (i == 4) break;
    System.out.println(i);
}


Question 7: Explain the concept of classes and objects in Java.

  • Class: A blueprint describing the properties (fields) and behaviors (methods) common to all objects of that type.

  • Object: A real instance of a class; it contains actual values for the fields and can use the class’s methods.

Example:

java

class Laptop {
    String brand;
    void display() { System.out.println("Brand: " + brand); }
}

Laptop myLaptop = new Laptop();
myLaptop.brand = "Dell";
myLaptop.display();


Here, Laptop is a class, and myLaptop is an object with its own brand value and ability to call display().

Question 8: Write a Java program to demonstrate constructor overloading and method overloading.

Example:

java

class Box {
    int width, height;

    // Constructor overloading
    Box() {
        width = 10;
        height = 20;
    }
    Box(int w, int h) {
        width = w;
        height = h;
    }

    // Method overloading
    void display() {
        System.out.println("Width: " + width + ", Height: " + height);
    }
    void display(String label) {
        System.out.println(label + " - Width: " + width + ", Height: " + height);
    }
}

public class OverloadingExample {
    public static void main(String[] args) {
        Box b1 = new Box();
        Box b2 = new Box(30, 40);

        b1.display();
        b2.display("Custom Box");
    }
}


This demonstrates two constructors for different ways to create a Box, and two display() methods, each handling different input. Constructor and method overloading allow multiple forms for object initialization and behavior.

Module 2: Inheritance

  1. Define inheritance. Explain its advantages with examples.
    Inheritance in Java is the mechanism by which one class (child or subclass) acquires the properties and behaviors (fields and methods) of another class (parent or superclass). It promotes code reusability by allowing the subclass to use and extend the functionality of the superclass without rewriting code. For example, if Vehicle is a parent class with a method move(), subclasses Car and Bike inherit move() and can invoke it directly without redefining. Advantages include code reuse, reduced redundancy, improved code maintenance, extensibility, and enabling polymorphism through method overriding.

  2. What is method overriding and how is it used in Java?
    Method overriding occurs when a subclass provides a specific implementation of a method already defined in its superclass. It allows runtime polymorphism where the method called is determined by the object's actual type. This is used to change or extend behavior of inherited methods. The overriding method must have the same name, return type, and parameters as the superclass method.

  3. Explain runtime polymorphism with a real-world example.
    Runtime polymorphism means that a call to an overridden method is resolved at runtime depending on the object's type, not the reference type. For example, if Animal is a superclass with a method sound(), and subclasses Dog and Cat override sound(), then:

Animal a = new Dog();
a.sound(); // calls Dog's sound

a = new Cat();
a.sound(); // calls Cat's sound

The actual method executed depends on the object's type at runtime.

  1. Differentiate between abstract classes and interfaces.

  • Abstract classes may have both abstract methods (without body) and concrete methods (with implementation). They support constructors and state (fields).

  • Interfaces declare abstract methods by default (Java 8 onwards can have default and static methods) but primarily define a contract to be implemented.

  • A class can extend only one abstract class but can implement multiple interfaces.

  1. Explain multilevel and hierarchical inheritance in Java.

  • Multilevel inheritance is a chain where a subclass inherits from a superclass, and another subclass inherits from this subclass, forming a multi-level hierarchy.

  • Hierarchical inheritance occurs when multiple subclasses inherit from a single superclass.

  1. What is the purpose of the super and final keywords?

  • super refers to the immediate parent class and is used to access parent class methods or constructors.

  • final is used to declare constants, prevent method overriding, or prevent a class from being subclassed.

  1. Describe how packages are created and imported in Java.
    Packages group related classes. They are created by declaring package packageName; at the top of the Java file. Classes from packages are imported using import packageName.ClassName; or wildcard import packageName.*;.

  2. Explain the use of access modifiers (public, private, protected, default).

  • public: accessible from anywhere.

  • private: accessible only within the defining class.

  • protected: accessible within package and subclasses.

  • Default (no modifier): accessible only within the package.

  1. Write a program to demonstrate interface implementation and multiple inheritance in Java.

interface Printable {
    void print();
}
interface Showable {
    void show();
}
class Demo implements Printable, Showable {
    public void print() {
        System.out.println("Print method");
    }
    public void show() {
        System.out.println("Show method");
    }
}
public class InterfaceDemo {
    public static void main(String[] args) {
        Demo obj = new Demo();
        obj.print();
        obj.show();
    }
}

This program shows multiple inheritance by implementing two interfaces in a single class.



Comments

Popular posts from this blog

Business Intelligence

Computer Graphics

Soft Skills