Core Java Most Asked Real Time Interview Questions
Questions
- Can an interface have a constructor? Why or why not?
- What is the significance of the default keyword in Java 8 interfaces?
- Can a class implement multiple interfaces in Java? If yes, explain how.
- Can an interface extend another interface? If so, what is the use case?
- What happens if a class implements two interfaces with conflicting default methods?
- Can you explain the difference between public, private, protected, and package-private access modifiers?
- What is the purpose of the protected access modifier? Can you access a protected method from another package?
- What is the default access level for a class, method, or variable if no access modifier is specified?
- Can you access a private variable from a subclass?
- What is a functional interface? Can you give an example?
- Explain the concept of Lambda Expressions in Java 8. How do they improve code readability?
- What is the Stream API in Java 8, and how is it different from a collection?
- What is the difference between map() and flatMap() in Java 8 Streams?
- How do you use the Optional class in Java 8 to avoid NullPointerException?
- What are the major improvements introduced in java.time package in Java 8?
- Can you create an instance of a class with only static methods? How would you call the methods?
- What is the difference between a static method and a non-static method?
- Can you access a non-static variable from a static method? Why or why not?
- What is a static block in Java, and when is it executed?
- How would you call a static method in another class?
- Can a static variable be overridden by a subclass? Why or why not?
Questions & Answers
- Can an interface have a constructor? Why or why not?
- Answer: No, an interface cannot have a constructor because it is not designed to be instantiated. Interfaces are meant to define a contract that implementing classes adhere to. Constructors are associated with creating object instances, which is not applicable for interfaces.
- What is the significance of the default keyword in Java 8 interfaces?
- Answer: The default keyword allows interfaces to have method implementations. This was introduced in Java 8 to provide backward compatibility for interfaces when new methods are added. Classes implementing the interface can either use the default implementation or override it.
- Can a class implement multiple interfaces in Java? If yes, explain how.
- Answer: Yes, a class can implement multiple interfaces in Java. It does so by using the implements keyword followed by a comma-separated list of interface names. Example:
class MyClass implements Interface1, Interface2 {
// Implement methods of both interfaces
}
- Can an interface extend another interface? If so, what is the use case?
- Answer: Yes, an interface can extend another interface using the extends keyword. This allows the child interface to inherit the methods of the parent interface, enabling modular design and reusability. Example:
interface ParentInterface {
void method1();
}
interface ChildInterface extends ParentInterface {
void method2();
}
- What happens if a class implements two interfaces with conflicting default methods?
- Answer: If a class implements two interfaces with conflicting default methods, it must explicitly override the conflicting method to resolve the ambiguity. Example:
class MyClass implements Interface1, Interface2 {
@Override
public void defaultMethod() {
// Custom implementation to resolve conflict
}
}
- Can you explain the difference between public, private, protected, and package-private access modifiers?
- Answer:
- public: Accessible from anywhere.
- private: Accessible only within the same class.
- protected: Accessible within the same package and by subclasses in other packages.
- (Default) Package-private: Accessible only within the same package.
- What is the purpose of the protected access modifier? Can you access a protected method from another package?
- Answer: protected allows visibility to subclasses and within the same package. A protected method can be accessed from another package only through inheritance. Example:
class Parent {
protected void display() {
System.out.println(“Protected method”);
}
}
class Child extends Parent {
public void access() {
display(); // Accessible via inheritance
}
}
- What is the default access level for a class, method, or variable if no access modifier is specified?
- Answer: The default access level is package-private, meaning it is accessible only within the same package.
- Can you access a private variable from a subclass?
- Answer: No, private variables are not accessible from a subclass. However, they can be accessed indirectly through public or protected getter and setter methods.
- What is a functional interface? Can you give an example?
- Answer: A functional interface is an interface with exactly one abstract method. It can be used as a target for lambda expressions. Example:
@FunctionalInterface
interface MyFunctionalInterface {
void perform();
}
- Explain the concept of Lambda Expressions in Java 8. How do they improve code readability?
- Answer: Lambda expressions provide a concise way to represent anonymous functions. They improve code readability by reducing boilerplate code for anonymous classes, especially in functional programming scenarios. Example:
(int a, int b) -> a + b;
- What is the Stream API in Java 8, and how is it different from a collection?
- Answer: The Stream API is a sequence of elements supporting functional-style operations like filtering, mapping, and reducing. Unlike collections, streams do not store elements; they process data lazily and can be parallelized for better performance.
Key Difference:
- Collections: Store and manage data in memory.
- Streams: Focus on computation and transformation of data in a pipeline.
- What is the difference between map() and flatMap() in Java 8 Streams?
- map(): Transforms each element in the stream and wraps the result in a stream of individual objects.
- Example: Convert a list of strings to their lengths.
- flatMap(): Transforms each element into a stream and flattens the resulting streams into a single stream.
- Example: Flatten a list of lists into a single list.
- Key Difference: flatMap() removes nested structures, whereas map() retains them.
- How do you use the Optional class in Java 8 to avoid NullPointerException?
- Answer: The Optional class wraps a value that may or may not be null. It provides methods like isPresent(), ifPresent(), and orElse() to handle values safely.
- Example:
Optional<String> optional = Optional.ofNullable(value);
optional.ifPresent(System.out::println); // Prints value if not null
String result = optional.orElse(“Default Value”); // Provides default if null
- What are the major improvements introduced in java.time package in Java 8?
- Answer: The java.time package provides a modern API for date and time operations. Key improvements include:
- Immutable and thread-safe classes (LocalDate, LocalTime, ZonedDateTime).
- Better handling of time zones.
- Parsing and formatting dates using DateTimeFormatter.
- More intuitive APIs for operations like adding/subtracting dates.
- Can you create an instance of a class with only static methods? How would you call the methods?
- Answer: While you can technically create an instance of such a class, it is unnecessary. Static methods are called using the class name directly.
- Example:
MyClass.staticMethod();
- What is the difference between a static method and a non-static method?
- Answer:
- Static Method:
- Belongs to the class, not an instance.
- Can be called using the class name.
- Cannot access non-static members directly.
- Non-Static Method:
- Belongs to an instance.
- Can access both static and non-static members.
- Static Method:
- Can you access a non-static variable from a static method? Why or why not?
- Answer: No, you cannot directly access a non-static variable from a static method because static methods are associated with the class, not an instance. Non-static variables require an object instance to access.
- What is a static block in Java, and when is it executed?
- Answer: A static block is a block of code that is executed when the class is loaded into memory. It is typically used to initialize static variables.
- Example:
static {
System.out.println(“Static block executed”);
}
- How would you call a static method in another class?
- Answer: You call a static method in another class using the class name followed by the method name.
- Example:
AnotherClass.staticMethod();
- Can a static variable be overridden by a subclass? Why or why not?
- Answer: No, static variables cannot be overridden because they are associated with the class rather than any instance. However, they can be hidden by declaring a static variable with the same name in the subclass.
- Example of hiding:
class Parent {
static int value = 10;
}
class Child extends Parent {
static int value = 20;
}
System.out.println(Parent.value); // 10
System.out.println(Child.value); // 20
Related Posts
- NIQ Company Hiring for Data Processing Analyst – Freshers can apply Don’t miss this Opportunity
- Walter P Moore Company Hiring for Application Developer – Freshers can apply Don’t miss this Opportunity
- Sigmoid Company Hiring for Data Engineer – Freshers can apply Don’t miss this Opportunity
- Walmart Company Hiring for Data Analyst – Freshers can apply Don’t miss this Opportunity
- Work at shell Company Hiring for Process Data Associate Engineer – Freshers can apply