0% found this document useful (0 votes)
16 views2 pages

Java Question Bank: OOP, Classes, Threads

Uploaded by

singhmadhvi2222
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Java Question Bank: OOP, Classes, Threads

Uploaded by

singhmadhvi2222
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA QUESTION BANK FOR 3 UNITS

UNIT I – Fundamentals of Object-Oriented Programming

4-Mark Questions:
1. Define Object-Oriented Programming. Mention any four benefits of OOP.
2. What are Java tokens? Explain different types with examples.
3. Explain any four data types in Java with examples.
4. What is typecasting? Give examples.
5. Differentiate between variables and operators in Java.
6. Write a short note on Java streams (Input and Output).
7. List and explain the features of Java.
8. What is a variable? Explain its scope and rules for naming.

8-Mark Questions:
1. Explain the basic concepts of Object-Oriented Programming in detail.
2. Discuss the history and features of Java.
3. Write a Java program demonstrating the use of different types of operators.
4. Explain various types of Java operators with suitable examples.
5. Describe variable declaration, initialization, and scope with examples.
6. Explain the concept of Java tokens and precedence of operators with examples.

UNIT II – Creating a Class and Subclass

4-Mark Questions:
1. Define a class and an object with suitable Java examples.
2. Explain the different types of access specifiers in Java.
3. What is constructor overloading? Give an example.
4. Explain method overloading with an example.
5. Write a Java program to demonstrate inheritance.
6. Explain decision-making statements with syntax and examples.
7. What is the difference between single and multiple inheritance?
8. Define an interface. How is it implemented in Java?

8-Mark Questions:
1. Write a Java program to create a class and access its members.
2. Explain the difference between overloading and overriding with examples.
3. Discuss arrays in Java. Write a program to demonstrate one-dimensional and two-
dimensional arrays.
4. Explain the use of if, switch, and while statements with examples.
5. Explain the concept of inheritance. Write a program for single inheritance.
6. Discuss the implementation and use of interfaces in Java.

UNIT III – Packages, Threads, Exceptions & Applets

4-Mark Questions:
1. What are packages in Java? How are they created?
2. Explain the use and syntax of the import statement.
3. What is a thread? List its life cycle.
4. Write a short note on synchronization.
5. What are exceptions in Java? Mention their types.
6. Write a short note on Java Applet life cycle.
7. Differentiate between application and applet.
8. Explain exception handling using try-catch.

8-Mark Questions:
1. Explain the steps to create and use a package in Java.
2. Write a program to create a thread using Runnable interface.
3. Explain the life cycle of a thread in Java with diagram.
4. Discuss exception handling in Java. Write a program to handle multiple exceptions.
5. Explain the structure of a Java applet and demonstrate its life cycle.
6. Write a Java applet program to display a simple message.

Common questions

Powered by AI

Java's variable scope rules significantly affect program design and error prevention by determining the visibility and lifetime of variables. Scope is generally categorized as class-level (or static), instance-level, method-level, and block-level . Class-level variables are shared across all instances and remain alive for the entire application lifecycle, aiding in memory consistency . Instance-level variables persist as long as the object exists, facilitating encapsulation of state . Method-level variables, limited to the method scope, ensure that data is used only where necessary, and they cease to exist after the method execution completes . Block-level variables further constrain visibility to only within loops or conditionals, helping in preventing accidental overwrites or unauthorized access to variables. By adhering to these rules, Java programmers can minimize errors, maintain clean code architecture, and enhance debugging efficiency.

Using packages in Java significantly impacts software modularity and component organization by grouping related classes and interfaces into namespaces, which helps prevent naming conflicts and encourages logical organization of code . Packages provide a hierarchical structure that simplifies code management, especially in large software projects where component segmentation is necessary for maintainability . They also enhance encapsulation by controlling access to classes and members, as classes within the same package can interact more freely compared to those in different packages. Additionally, packages support better scalability and reusable code as they allow for the distribution of components across multiple projects with minimal conflicts . Overall, the structured approach facilitated by packages fosters clean architecture and maintainable code bases.

Java applets offer several advantages for web-based applications, including platform independence, as they can run on any machine with a compatible Java Virtual Machine (JVM). They provide rich graphical capabilities and are automatically downloaded and executed by the browser, facilitating dynamic content delivery . However, applets have limitations, such as security restrictions imposed by browsers to prevent malicious activities, which can hinder functionality . Their reliance on the JVM means performance can vary depending on the client's environment, and they also require the user to have specific plugins installed due to declining browser support . Given the rise of modern web technologies like HTML5, the application of Java applets has diminished as developers prefer more flexible and universally supported solutions.

Java's access specifiers—public, private, protected, and default (or package-private)—play a crucial role in class design and security by controlling the visibility and accessibility of classes, methods, and variables. Public access specifier allows the class and its members to be accessed from any other class, promoting usability across different packages . Private limits the accessibility to within the class itself, ensuring that sensitive data is protected from unauthorized access . Protected access allows members to be accessed by subclasses and classes within the same package, thus supporting inheritance while maintaining a level of encapsulation between packages . Default or package-private access restricts visibility to classes within the same package, aiding in component encapsulation within Java projects . By appropriately applying these specifiers, developers can enforce encapsulation and secure data integrity within their applications.

Exception handling in Java enhances robustness and error management by providing a structured way to detect and handle runtime errors, ensuring that programs can continue execution or fail gracefully. Through the use of try-catch blocks, Java allows developers to isolate code that may throw exceptions and define specific catch clauses for handling different types of exceptions . This prevents errors from propagating and crashing the program, enabling the execution of alternative paths or resource cleanup operations as specified in finally blocks . Java's exception hierarchy also allows for custom exceptions, providing detailed information about application-specific errors. By promoting a systematic approach to error handling, Java aids in the development of reliable and maintainable software systems.

Inheritance directly contributes to polymorphism in Java by allowing subclass objects to be treated as instances of their parent class, facilitating dynamic method invocation . Through inheritance, subclasses inherit methods and properties from their parent class, but they can also override or extend these behaviors, providing flexibility in method execution . This ability to override parent class methods allows for polymorphic behavior, meaning that the same method call can result in different outcomes depending on the object that invokes it . The benefits of this relationship in software development include increased flexibility and scalability of code, as it promotes reusable components and the ability to introduce new functionalities without altering existing code structures. Consequently, inheritance and polymorphism together enhance the maintainability and extensibility of applications, allowing developers to build adaptable and robust systems.

The fundamental principles of Object-Oriented Programming (OOP) include encapsulation, abstraction, inheritance, and polymorphism. Encapsulation involves bundling the data and methods that operate on the data within a single unit or class, thus restricting access to some components to protect the integrity of the data . Abstraction allows for the simplification of complex systems by modeling classes based on the essential properties and behaviors an object should have . Inheritance enables new classes to adopt the properties and methods of existing classes, promoting code reusability and hierarchical classification . Polymorphism allows entities to be represented in multiple forms by enabling objects to be treated as instances of their parent class, fostering flexibility and dynamic method invocation . Together, these principles enhance software development by enabling modular design, code reusability, and greater system manageability.

Method overloading in Java is significant because it allows multiple methods to have the same name with different parameter lists within a class, enabling intuitive method usage for different contexts while maintaining readability . This feature encourages code reuse and streamlines maintenance because the same logic can be applied in various contexts by varying input parameters, without rewriting the method or altering its name . Overloading enhances clarity and reduces complexity, as developers can use the same method name for similar operations on different data types or argument numbers. Consequently, it simplifies method invocations and improves code maintainability by providing more straightforward interfaces to related operations.

In Java, an interface is distinct from a class in that it cannot contain any concrete methods (initially, though Java 8 introduced default methods); instead, it defines a contract in the form of method signatures that implementing classes must fulfill . While a class encapsulates data and behavior, an interface only specifies behavior expected from the class, serving as a tool for achieving abstraction . Implementing an interface influences class design by enforcing a strict adherence to a specified set of behaviors, thereby promoting code consistency and interoperability among unrelated classes through shared interfaces . By enabling multiple interfaces to be implemented by a single class, interfaces in Java also allow a form of multiple inheritance, which is not possible with classes alone, thus enhancing flexibility in design and encouraging clean separation of concerns.

The life cycle of a Java thread includes several states: New, Runnable, Running, Blocked/Waiting, and Terminated. Initially, a thread is in the 'New' state when it is created but not yet started . When the start() method is invoked, it enters the 'Runnable' state, waiting for the CPU allocation to execute its run() method . Once a thread is actively executing, it is in the 'Running' state . It may enter 'Blocked/Waiting' states when waiting for a resource or another thread's completion . Finally, a thread reaches the 'Terminated' state when its run() method completes or if it exits due to an unhandled exception . Understanding this life cycle is critical for multi-threaded programming as it affects how threads are scheduled, managed, and coordinated. Proper handling of these states ensures efficient resource utilization, synchronization, and the avoidance of concurrency-related issues like deadlocks.

You might also like