Java
Java
• A mutable string is a string object whose value can be changed after it is created. In Java, the
StringBuilder and StringBuffer classes represent mutable strings. This means
you can modify the contents of a StringBuilder or StringBuffer object without
creating a new object.
(b) What is the use of abstract class in Java?.
• The this keyword in Java refers to the current object of the class. It is used to:
◦ Refer to the current object's instance variables.
◦ Invoke another constructor of the same class.
◦ Pass the current object as an argument to a method.
(e) What is a Copy Constructor?
• A copy constructor is a special constructor that creates a new object of the same class as the
current object, initializing the new object with the values of the current object. It is used to
create a deep copy of an object.
(f) What Java is called platform independent?
• Java is called platform-independent because Java bytecode, which is the intermediate code
generated by the Java compiler, can run on any platform that has a Java Virtual Machine
(JVM) installed. The JVM is responsible for translating the bytecode into machine code
speci c to the underlying operating system.
(g) How vector differs from a list?
• Vector:
◦ Thread-safe (synchronized).
◦ Slower than ArrayList due to synchronization overhead.
◦ Uses an array internally to store elements.
◦ Legacy class, consider using ArrayList for most use cases.
• List:
◦
Not inherently thread-safe.
◦
More ef cient than Vector for most operations.
◦
Provides more exibility in terms of implementation.
◦
Can be used with different implementations like ArrayList, LinkedList,
etc.
(h) What is the use of nally() method in Java?
fi
fi
fi
fl
fi
fi
fi
• The finally block is a block of code that is guaranteed to execute, regardless of whether
an exception is thrown or not. It is often used to release resources such as le handles or
database connections.1
2. (a) What is inheritance in Java? Explain different types of inheritance with example.
class Animal {
2. void eat() {
3. System.out.println("Animal eats");
4. } }
5.
6. class Dog
7. }
8.
class Grandparent { }
The Child class inherits from the Parent class, and the Parent class inherits
from the Grandparent class.
13. Hierarchical Inheritance: Multiple classes inherit from a single base class.
Java
class Animal { }
2. (b) Explain with the help of an example, how Java gets bene ted by using Interface.
• Interfaces de ne a contract or blueprint for classes that implement them. They specify
methods that the implementing classes must provide.
• Bene ts:
interface Shape {
• double calculateArea();
• }
•
• class Circle implements Shape {
• // ...
• }
•
• class Rectangle implements Shape {
• // ...
• }
fi
fi
fi
fi
fl
•
Here, both Circle and Rectangle implement the Shape interface, even though they
represent different geometric shapes.
3. (a) How does String class differ from StringBuffer class?
3. (b) What are Implicate and Explicit Casting? Explain with the help of example.
• Implicit Casting (Widening): This happens automatically when converting a value from a
smaller data type to a larger data type. No explicit cast operator is needed.
Java
• Explicit Casting (Narrowing): This is required when converting a value from a larger data
type to a smaller one. It is done using the cast operator (target_type).
Java
double d = 3.14;
• Error: Represents serious system-level errors that usually cannot be recovered from.
Examples include OutOfMemoryError, StackOverflowError,
VirtualMachineError.
fi
fi
fi
fi
fi
fi
fi
• Exception: Represents more general errors that can occur during program execution. These
can be handled using try-catch blocks. Examples include IOException,
ArithmeticException, SQLException.
4. (b) Illustrate ‘ArrayIndexOutOfBounds’ exception with an example.
Java
4. (c) De ne an exception called “NotEqualException” that is thrown when a oat value is not
equal to 3.14. Write a program that uses that user-de ned exception.
Java
5. (a) What is the difference between Method Overloading and Method Overriding?
• Method Overloading: Occurs within the same class when you have multiple methods with
the same name but different parameters (different number of parameters or different data
types of parameters).
fi
fi
fi
fl
• Method Overriding: Occurs when a subclass provides a speci c implementation for a
method that is already de ned in its superclass. The method in the subclass4 must have the
same name, return type, and parameters as the method in the superclass.5
5. (b) Explain Dynamic Method Dispatch with suitable example.
• Dynamic Method Dispatch is the process of determining which method to call at runtime.
When a method is called on an object, the JVM determines the actual method to execute
based on the object's actual class at runtime. This allows for polymorphism.
Java
class Animal {
void makeSound() {
System.out.println("Generic animal sound");
}
}
5. (c) Write a program in Java to access static variable and static method to explain ‘static’
keyword properly.
Java
class MyClass {
static int count;
static void displayCount() {
System.out.println("Count: " + count);
}
MyClass() {
count++;
}
fi
fi
fi
}
◦ A class implements the Runnable interface and overrides its run() method.
fi
fi
fi
◦ An object of this class is then passed to a Thread object to create and start the
thread.
• Java
• Differences:
◦ Inheritance: Extending Thread limits the class to inherit from only one class
(since Java doesn't support multiple inheritance for classes). Implementing
Runnable allows a class to inherit from another class while still having
multithreading capabilities.
◦ Flexibility: The Runnable interface provides more exibility as it decouples the
thread execution logic from the class hierarchy.
6. (c) What is thread synchronization? How it is achieved in Java?
◦ wait() and notify() methods: These methods are used to pause and resume
threads that are waiting for a condition to be met. The wait() method causes a
thread to wait until another thread noti es it using the notify() method.
fi
fl
7. (a) Explain the life-cycle of an Applet.
1. Initialization:
◦ The browser downloads the applet's bytecode from the server.
◦ The JVM creates an instance of the applet class.
◦ The applet's constructor is called to initialize the applet object.
◦ The init() method is called to perform any necessary initializations.
2. Starting:
◦ The start() method is called when the applet is rst loaded and whenever the
applet is returned to the foreground.
◦ The applet begins its execution.
3. Painting:
◦ The paint() method is called to draw the applet's graphical components on the
screen.
◦ This method is typically called whenever the applet needs to be redrawn.
4. Stopping:
◦ The stop() method is called when the browser leaves the applet's page or
minimizes the browser window.
◦ The applet should release any resources it is using (e.g., stop any animations).
5. Destroying:
◦
The destroy() method is called when the applet is being unloaded from
memory.
◦ The applet should perform any necessary cleanup tasks (e.g., close les, release
network connections).
7. (b) List various attributes of Applet tag used in HTML.
• code: Speci es the name of the Java class that implements the applet.
• width: Sets the width of the applet in pixels.
• height: Sets the height of the applet in pixels.
• align: Speci es the alignment of the applet within the page.
• alt: Provides alternative text for browsers that do not support Java.
• archive: Speci es the location of the JAR le containing the applet's class les.
7. (c) Write a Java program that uses the drawPolygon () method of Graphics class to draw a
triangle with endpoints (28, 35); (78, 82) and (43, 43)
Java
import java.awt.*;
import java.applet.*;
◦
Private: Members declared as private are accessible only within the same class.
They are not accessible from outside the class, including subclasses or other classes
in the same package.
◦ Protected: Members declared as protected are accessible within the same class,
subclasses, and classes in the same package. They are not accessible to classes in
other packages.
8. (b) How an array is declared in Java? Write a program in Java to sort an array in
ascending order and display it.
Declaration:
Java
data_type[] array_name;
// Example: int[] numbers;
Program to sort an array in ascending order using bubble sort:
Java
// Bubble Sort
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = 0; j < numbers.length - i - 1; j++)
{
if (numbers[j] > numbers[j + 1]) {
// Swap elements
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
fi
fi
fi
fi
fi
// Display sorted array
System.out.print("Sorted array: ");
for (int num : numbers) {
System.out.print(num + " ");
}
}
}