Ibm Java Inter
Ibm Java Inter
Ibm Java Inter
class Counter {
private int count = 0;
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Count: " +
counter.getCount());
}
}
Example:
// No-argument constructor
public ConstructorExample() {
this.value = 0;
}
// Parameterized constructor
public ConstructorExample(int value) {
this.value = value;
}
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:
5. Output of this:
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.
// Regular method
void sleep() {
System.out.println("Sleeping...");
}
}
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;
}
// Method to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
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.
// 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.
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{
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;
import java.util.HashMap;
import java.util.Map;