Session-2 Interview Theory Questions
Session-2 Interview Theory Questions
Session -3_Interview_Theory_Questions
In some questions Examples are needed.Sometimes students can’t
understand without example(Either faculty can provide that or we can
modified our questions)
Week-2 Session-3(Polymorphism)
The enhanced for loop in Java provides a concise way to iterate over
elements in an array or a collection.
It eliminates the need for explicit initialization, termination, and
increment of a loop counter variable.
It is especially useful when you need to iterate over all elements of an
array without requiring the index.
class ForEachExample1{
Modification needed
Inheritance in Java allows a class to inherit attributes and methods from
another class.The subclass inherits the properties and behaviors (methods)
of the superclass, enabling the subclass to extend or modify its functionality
as needed. This promotes modularity, extensibility, and flexibility in
Java programs, as common attributes and behaviors can be defined
in a superclass and shared among multiple subclasses. Inheritance
promotes code reuse, reduces, and helps in organizing and
maintaining large code effectively.
Here's why:
Static Binding: Method overriding in Java is based on dynamic or late
binding, where the method to be invoked is determined at runtime
based on the actual object type. However, static methods are resolved
at compile-time using static binding, where the method to be invoked
is determined by the reference type rather than the actual object type.
Inheritance Hierarchy: When a subclass defines a static method with
the same signature as a static method in the superclass, it is simply
hiding the superclass's static method rather than overriding it.
Key Differences:
Purpose:
Inheritance focuses on code reuse and establishing relationships
between classes by allowing a subclass to inherit properties and
methods from a superclass.
Encapsulation focuses on data hiding and providing a controlled
interface for accessing and manipulating data within a class.
Code Organization:
Inheritance organizes code by promoting reuse and
specialization among related classes.
Encapsulation organizes code by bundling data and methods
together within a class and controlling access to that data.
Access Control:
Inheritance controls access to superclass members through
inheritance hierarchies.
Encapsulation controls access to class members through access
modifiers (e.g., public, private, protected).
Week 3- Session 2
1. What is an interface in Java?
An interface in Java is a mechanism that is used to achieve complete
abstraction. It is basically a kind of class that contains only constants
and abstract methods.
In Java, an interface is a reference type that defines a contract for
classes to follow. It specifies a set of abstract methods (methods
without a body) and constants (variables that are implicitly final and
static) that must be implemented by any class that implements the
interface.
Interfaces serve as blueprints for classes, providing a way to define
common behavior that can be implemented by unrelated classes. They
promote code reuse, abstraction, and polymorphism in Java programs.
Week 3- Session 3
Exception handling
Many questions are repeated 2 times.
Some MCQ needed example code with it.
Can you explain the difference between the 'catch' and 'finally'
blocks in Java exception handling?
The 'catch' block is used to handle exceptions that occur within the
corresponding 'try' block. It contains code that is executed when an
exception of the specified type is thrown.
The 'finally' block is used to execute cleanup code that should run
regardless of whether an exception occurs or not. It is typically used to
release resources acquired in the 'try' block.
How can you create custom exceptions in Java?
Custom exceptions in Java can be created by extending the 'Exception'
class or one of its subclasses.
public class CustomException extends Exception {
public CustomException() {
super();
}
Since Exception is the base class for all exception types, make use of a catch
block that catches the Exception class-
try {
// do something
} catch (Exception exception) {
// handle exception
}
However, it is recommended to use accurate Exception handlers instead of generic
ones. This is because having broad exception handlers could make the code error-
prone by catching exceptions that were not anticipated in the software design and could
result in unexpected behavior.
import javax.xml.bind.JAXBException;
}
}
We know that the FileNotFoundException is the subclass of IOException - By the
rule of multiple catch block, we need to have the most specific exception at the
top and the most generic exception at the bottom. If there are no parent-child
relationships between the exceptions, they can be placed anywhere. In this
example, JAXBException is not related to IOException and
FileNotFoundException exceptions. Hence, it can be placed anywhere in the
multiple catch block structure. However, IOException being the parent class is
mentioned at the beginning and that is followed by FileNotFoundException. This
results in an error - Unreachable catch block for FileNotFoundException. It is
already handled by the catch block for IOException. To fix this issue, depending
on the business requirements, we can either remove the FileNotFoundException
from the throws list and catch block OR we can rearrange the catch blocks