0% found this document useful (0 votes)
12 views6 pages

Java Exam Paper Answer Sheet

The document is a Java exam paper consisting of three groups of questions: very short answer type, short answer type, and long answer type. It covers various Java concepts such as memory management, object-oriented principles, exception handling, and GUI frameworks. The paper includes definitions, examples, and comparisons of key Java features and constructs.

Uploaded by

tuhinakhanam557
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)
12 views6 pages

Java Exam Paper Answer Sheet

The document is a Java exam paper consisting of three groups of questions: very short answer type, short answer type, and long answer type. It covers various Java concepts such as memory management, object-oriented principles, exception handling, and GUI frameworks. The paper includes definitions, examples, and comparisons of key Java features and constructs.

Uploaded by

tuhinakhanam557
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 Exam Paper - Answer Sheet

Group-A (Very Short Answer Type Questions)

Answer any ten of the following:

1. Heap memory

2. Java Virtual Machine (JVM)

3. C) public

4. True

5. B) FORTRAN - Object-Oriented Language

6. True

7. A) int number[]

8. B) Independent

9. setColor()

10. B) Garbage collection is present

11. True

12. D) System dependent

Group-B (Short Answer Type Questions)

Answer any three of the following:

1. Qualified association represents a relationship where a qualifier is used to distinguish between

objects of the associated class.

Example:

class Library {

Map<String, Book> books; // String (ISBN) acts as a qualifier


}

2. An object is an instance of a class containing state (attributes) and behavior (methods).

Java is object-oriented because it is based on principles like encapsulation, inheritance, and

polymorphism.

Differences:

- Procedural: Focus on functions; no data encapsulation.

- Object-Oriented: Focus on objects; encapsulates data and functions together.

3. The static keyword makes a variable or method belong to the class rather than any instance of

the class.

Example:

class Example {

static int count = 0; // Static variable

static void display() { // Static method

[Link]("Count: " + count);

4. Dynamic method dispatch allows a method to be called based on the runtime type of the object.

Example:

class Parent {

void show() { [Link]("Parent class"); }

class Child extends Parent {

void show() { [Link]("Child class"); }

}
public class Main {

public static void main(String[] args) {

Parent obj = new Child(); // Runtime polymorphism

[Link](); // Output: Child class

5. AWT (Abstract Window Toolkit) is a Java framework for building GUI applications.

Event Listener is an interface that handles events, such as button clicks or mouse movements, in

GUI applications.

Group-C (Long Answer Type Questions)

Answer any three of the following:

1. Differences between the following:

i) throw and throws clause

- throw: Used to explicitly throw an exception.

- throws: Declares exceptions a method might throw.

ii) final and finally

- final: Used to declare constants, prevent method overriding, or inheritance.

- finally: Executes cleanup code in a try-catch block.

iii) Abstract classes and Interfaces

- Abstract class: Can have concrete methods; supports inheritance.

- Interface: Pure abstraction; supports multiple inheritance.


2. Write short notes on the following:

i) Link and Association

- Link: A single connection between objects.

- Association: A broader relationship between classes.

ii) Thread Life-Cycle

- States: New, Runnable, Running, Blocked, Terminated.

iii) Encapsulation

- Encapsulation: Bundling data and methods into a single unit (class).

3. Write short notes on the following:

i) Dynamic method dispatch

- Allows method resolution during runtime based on the object type.

ii) Dynamic binding

- Links method calls to method definitions at runtime.

iii) Encapsulation

- Restricts access to class members using access modifiers like private.

4. Create a package and write a Java file with four methods for four basic arithmetic operations.

[Link]:

package operations;

public class ArithmeticOperations {

public int add(int a, int b) { return a + b; }

public int subtract(int a, int b) { return a - b; }


public int multiply(int a, int b) { return a * b; }

public double divide(int a, int b) { return a / (double)b; }

[Link]:

import [Link];

public class Main {

public static void main(String[] args) {

ArithmeticOperations ao = new ArithmeticOperations();

[Link]("Addition: " + [Link](10, 5));

[Link]("Subtraction: " + [Link](10, 5));

[Link]("Multiplication: " + [Link](10, 5));

[Link]("Division: " + [Link](10, 5));

5. What are exceptions? Explain user-defined and system-defined exceptions.

- System-defined exceptions: Predefined in Java (e.g., NullPointerException, ArithmeticException).

- User-defined exceptions: Custom exceptions created by the programmer.

Example:

class MyException extends Exception {

public MyException(String message) {

super(message);

public class Main {

public static void main(String[] args) {


try {

throw new MyException("Custom Exception");

} catch (MyException e) {

[Link]([Link]());

a) this and super keywords

- this: Refers to the current object instance.

- super: Refers to the parent class.

b) Difference between = operator and .equals() method

- =: Compares references of two objects.

- .equals(): Compares content of two objects.

Common questions

Powered by AI

Dynamic method dispatch is a mechanism in Java that facilitates polymorphism by allowing method calls to be resolved at runtime based on the actual object's type rather than the reference type. This enables developers to write flexible and extensible code where a single method call can have different behaviors based on the object instance. It impacts code design by supporting the use of interfaces and abstract classes, encouraging a design that adheres to the open/closed principle, and enabling runtime decision-making for method invocation, leading to highly adaptable systems .

Encapsulation enhances the security and modularity of Java programs by restricting direct access to class fields and allowing controlled manipulation through methods. By defining class fields as private and accessing them via public getter and setter methods, Java ensures that data is modified in a controlled manner, reducing errors and maintaining integrity. Furthermore, encapsulation enables modularity by allowing classes to change internally without impacting other parts of the program, thus promoting a clean separation of concerns and reinforcing object-oriented design principles .

Dynamic binding in Java refers to the mechanism by which method calls are linked to method implementations at runtime rather than at compile-time, facilitating runtime polymorphism. This allows a single interface to be used for different underlying forms (data types) and offerings. Dynamic binding provides the flexibility to change method implementations without altering calling code, thus promoting loose coupling. The advantage offered includes flexibility in method extension/modification, minimizing dependencies, increasing code maintainability, and supporting polymorphic behavior where a superclass reference can invoke subclass-specific methods .

User-defined exceptions allow Java developers to create specific exceptions tailored for the application's domain, offering clarity and control in error management. These exceptions extend the base Exception class to encapsulate context-specific error information. Integrating user-defined exceptions enhances error handling by enabling developers to catch and manage application-specific scenarios, triggering meaningful responses rather than generic exception handling. This leads to robust systems that efficiently communicate and resolve issues, prevent application crashes, and promote increased code readability and maintainability. These integrations provide the means to implement custom logic in response to particular business rule violations or failure conditions .

Java's Abstract Window Toolkit (AWT) implements the Event Listener Model by defining event listener interfaces that can be implemented to handle particular event types. For instance, clicking a button generates an ActionEvent handled by ActionListener interface. An event source, such as a button, fires an event, and the registered listeners execute corresponding methods. This model decouples event-handling logic from the event source, enabling scalable and organized GUI design. It allows developers to register multiple listeners for a single event type or coordinate complex interactions among multiple GUI components efficiently .

The 'static' keyword in Java attaches variables and methods to the class instead of instances of the class. This means that there is only one copy of static variables or methods shared across all instances. Static variables are used for constants or shared properties, while static methods can operate without relying on instance-specific data. Such methods cannot access non-static instance variables directly, as they belong to the class level context. This usage promotes resource efficiency and consistent state across multiple objects .

In Java, the 'throw' statement is used within a method to explicitly trigger an exception, often implemented in custom error-handling scenarios. Conversely, the 'throws' clause is used in method signatures to declare exceptions that a method might propagate to the caller, aiding the calling code to handle anticipated exceptions. 'Throw' is typically used for active exception raising, whereas 'throws' prepares the method signature for predictable control flow scenarios where exceptions are likely based on logic or input parameters .

A thread in Java can exist in several lifecycle stages: New, Runnable, Running, Blocked/Waiting, and Terminated. In the New state, a thread is created but not yet started. The Runnable state implies readiness for execution by the JVM. In the Running state, the thread is executing its task. The Blocked/Waiting state occurs when a thread is waiting for resources or signals from other threads. Finally, the Terminated state indicates that a thread has completed its execution. Understanding these stages is crucial for concurrent programming as it influences synchronization, resource allocation, and application responsiveness. Efficient thread management ensures that applications can handle parallel tasks gracefully without resource contention .

Abstract classes in Java can contain both abstract methods (which must be implemented by subclasses) and concrete methods (with defined functionality). They support inheritance, allowing subclasses to inherit behaviors. Interfaces, in contrast, are pure abstractions where all methods were traditionally abstract (before Java 8's default methods), designed to be implemented across any classes. Interfaces provide a contract for methods without code sharing. They support multiple inheritances, allowing a class to implement multiple interfaces, offering flexibility in compatibility and function extension .

The Java Virtual Machine (JVM) manages memory through several key areas: Stack, Heap, and Method Area. The heap is used for dynamic memory allocation for objects, which can grow as needed, whereas the stack manages local variables and method call flows. JVM's garbage collection automatically frees up memory by removing objects that are no longer in use, aiding in resource efficiency and reducing memory leaks. Proper memory management through JVM optimizes application performance by handling object lifecycle and method execution efficiently, although poor management might lead to 'OutOfMemoryError' due to excessive or inefficient use of resources .

You might also like