Developers & Programmers Interview
Java Question(s) & Actual Evaluation
Please refer to the evaluation for more insight.
-
Classification: Developers & Programmers
-
Topic(s): Java
Can you explain the difference between an interface and an abstract class in Java?
Candidate's Answer:Interfaces are used for pure abstraction in which implementation details are completed hidden from user. Whereas abstract classes are used for partial abstraction
Evaluation:Your answer is generally correct, but it could be improved with a more detailed explanation.
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. It cannot contain any implementation and is used to define a contract for what a class can do. Classes can implement multiple interfaces, allowing for more flexibility in the design of your program.
On the other hand, an abstract class is a class that cannot be instantiated and is typically used as a base class for other classes to inherit from. It can contain both abstract methods (methods without a body) and concrete methods (methods with a body). Abstract classes provide the ability to define common behavior for subclasses while allowing for specific implementations in those subclasses.
The key difference between an interface and an abstract class is that an interface only provides a contract for what a class can do, while an abstract class can provide both a contract and some common implementation.
To improve your answer, you could provide examples to better illustrate the concept. For instance, you could mention a real-world scenario where you would use an interface (e.g., a Shape interface with methods like calculateArea() and calculatePerimeter()) and an abstract class (e.g., a Vehicle abstract class with a method like startEngine()).
Overall, your answer is accurate but could be enhanced with additional details. I would rate it 4/5.
-
Classification: Developers & Programmers
-
Topic(s): Java
How do you handle exceptions in Java? Can you give an example of a checked and an unchecked exception?
Candidate's Answer:Using try and catch block. try{
System.out.println(10/0);
}
catch(ArithmaticExpression e)
{System.out.println(e)}
Your answer is partially correct. In Java, exceptions are handled using try-catch blocks. However, there are a few mistakes in your example.
Firstly, you have misspelled "ArithmeticException" as "ArithmaticExpression". It should be:
```java
try {
System.out.println(10/0);
} catch(ArithmeticException e) {
System.out.println(e);
}
```
Secondly, the exception you are trying to catch (10/0) would actually throw an `ArithmeticException` since you cannot divide by zero in Java. So, this is an example of an unchecked exception.
For a checked exception example, you could use something like reading a file. The `FileReader` constructor method throws a checked exception `FileNotFoundException` which must be caught or declared in a method's `throws` clause.
```java
try {
FileReader file = new FileReader("file.txt");
} catch(FileNotFoundException e) {
System.out.println("File not found!");
}
```
To improve your answer, you could have explained the difference between checked and unchecked exceptions and given examples for both. You could also mention the `finally` block which executes regardless of an exception being thrown or caught.
I would rate your answer 3/5. It is somewhat correct but lacks in completeness and explanation.