IBM JAVA INTER Update
IBM JAVA INTER Update
int a = 1;
System.out.println((a++) * (++a));
7. What is Polymorphism?
13. Write a program to find the number of occurrences of each character in a given
string?
In Java, synchronization is used to control the access of multiple threads to shared resources.
The synchronized keyword is used to synchronize methods or blocks.
class Counter {
private int count = 0;
t1.start();
t2.start();
t1.join();
t2.join();
Example:
// No-argument constructor
public ConstructorExample() {
this.value = 0;
}
// Parameterized constructor
public ConstructorExample(int value) {
this.value = value;
}
• Pass by Value: The method receives a copy of the argument passed. Changes to the
parameter do not affect the argument.
• Pass by Reference: The method receives a reference to the argument passed,
meaning changes to the parameter affect the argument.
In Java, when you pass an object, you pass the reference to that object by value, meaning the
reference itself is copied. Hence, changes to the object affect the original object.
Example:
class MyObject {
int value;
}
In Java, when you pass an array to a method, it is passed by value, but this can be a bit
misleading due to how references work. Specifically, the reference to the array is passed by
value, meaning the method gets a copy of the reference. This means changes to the array
elements inside the method will affect the original array, but changing the reference itself will
not affect the original reference.
Output:
Original array:
1 2 3 4 5
Modified array:
2 4 6 8 10
Inside changeArrayReference method:
10 20 30 40 50
Array after attempting to change reference:
2 4 6 8 10
Explanation:
int a = 1;
System.out.println((a++) * (++a)); // Output will be 3
1. a++: (Postincrement) This returns the current value of a (which is 1), and then a is
incremented to 2.
2. ++a: (Preincrement) This increments a to 3 and then returns the new value of a
(which is 3).
Abstract functions (or abstract methods) in Java are methods that are declared without an
implementation. These methods are defined in abstract classes, which cannot be instantiated
on their own. Instead, subclasses of the abstract class must provide implementations for all
abstract methods.
The primary use of abstract functions is to enforce a contract for subclasses. They ensure that
any subclass that extends the abstract class provides implementations for the specified
abstract methods, thus maintaining a consistent interface.
Let's consider an example involving an abstract class Animal and its subclasses Dog and
Cat.
// Regular method
void sleep() {
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
// Providing implementation for abstract method
void makeSound() {
System.out.println("Bark");
}
}
7. What is Polymorphism?
Benefits of Polymorphism:
Method Overloading: Define multiple methods with the same name but different
parameters.
class MathOperation {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
1. Compile-time Polymorphism:
o The add method is overloaded with different parameter lists.
o The compiler determines which add method to call based on the method
signature and the arguments provided.
2. Runtime Polymorphism:
o The sound method in the Animal class is overridden by the Dog and Cat
classes.
o The actual method to be called is determined at runtime based on the object's
runtime type (Dog or Cat).
In Java, an interface is a reference type, similar to a class, that can contain only constants,
method signatures, default methods, static methods, and nested types. Interfaces cannot
contain instance fields or constructors. Interfaces provide a way to achieve abstraction and
multiple inheritance in Java.
• Abstract Methods: All methods in an interface are abstract (do not have a body) by
default unless they are static or default methods.
• Default Methods: Interfaces can have default methods, which have a default
implementation.
• Static Methods: Interfaces can have static methods, which can be called
independently of any instance.
• Constants: All variables in an interface are implicitly public, static, and
final.
• Multiple Inheritance: A class can implement multiple interfaces, overcoming the
limitation of single inheritance in Java.
How Are Interfaces Useful in Java?
// Define an interface
interface Animal {
void sound(); // Abstract method
default void sleep() { // Default method
System.out.println("Sleeping...");
}
}
1. Interface Definition:
o The Animal interface defines an abstract method sound() and a default
method sleep().
2. Implementation of Interface:
o The Dog and Cat classes implement the Animal interface by providing their
own implementations of the sound() method.
3. Usage:
o In the main method, instances of Dog and Cat are created and assigned to
variables of type Animal.
o The sound() method is called on these instances, demonstrating
polymorphic behavior.
o The sleep() method (a default method in the interface) is also called on
these instances.
Lambda expressions in Java are a way to represent anonymous functions (function literals)
concisely. They were introduced in Java 8 and provide a means to implement the abstract
method of a functional interface directly inline, without needing to define a separate class or
even an anonymous class.
Functional Interface
An interface which has only one abstract method is called functional interface. Java provides
an anotation @FunctionalInterface, which is used to declare an interface as functional
interface.
interface Drawable{
}
public class LambdaExpressionExample2 {
int width=10;
//with lambda
Drawable d2=()->{
System.out.println("Drawing "+width);
};
d2.draw();
int n = s.length();
for(int i=0;i<n/2;i++){
if(s.charAt(i) != s.charAt(n-1-i)){
return false;
return true;
13. Write a program to find the number of occurrences of each character in a given
string?
import java.util.HashMap;
import java.util.Map;
Node(int data) {
this.data = data;
this.next = null;
}
}
System.out.println("Original List:");
list.display();
int indexToRemove = 2;
list.removeAtIndex(indexToRemove);