0% found this document useful (0 votes)
17 views9 pages

Session-2 Interview Theory Questions

Interview question

Uploaded by

rifegi8834
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
17 views9 pages

Session-2 Interview Theory Questions

Interview question

Uploaded by

rifegi8834
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

Session-2 Interview Theory Questions

Add below questions in this session

What is enum? Give an example of it.

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)

Session-4 Interview Theory Questions


Add below questions in this session

 What is abstraction, and why is it important in OOP?


 Abstraction is the concept of hiding the implementation details of
an object and exposing only the essential features. It allows
programmers to focus on what an object does rather than how it
does it, promoting modularity, flexibility, and code
maintainability.
 What is the difference between an abstract class and an
interface?
 An abstract class can have abstract and non-abstract methods,
and it can also have fields. An interface can only have abstract
methods and constants (fields that are implicitly static and final).
A class can implement multiple interfaces but can extend only
one abstract class.
 What are the benefits of using interfaces in Java?
 Interfaces promote loose coupling, as they define contracts that
classes must follow without specifying the implementation
details. They support multiple inheritance, allowing a class to
implement multiple interfaces. Interfaces also facilitate code
reuse and provide a way to achieve polymorphism.

Session 5-Java Operators


No questions available related to operator,please add
below question and Answers

 What is the difference between the logical AND (&&) and


bitwise AND (&) operators?
 The logical AND (&&) operator performs short-circuit evaluation,
meaning if the left-hand side expression evaluates to false, it
doesn't evaluate the right-hand side expression. Whereas, the
bitwise AND (&) operator evaluates both sides regardless of the
outcome.
 What is the difference between == and .equals() in Java?
 == is a reference comparison operator, which checks if two
references point to the same memory location.
 .equals() is a method available in the Object class, which can be
overridden by subclasses to provide their specific
implementation for comparison. By default, it behaves the same
as ==, but many classes override it to compare the contents of
the objects.

variabWhat is the significance of the 'final' keyword in Java when


applied to variables?

 When applied to variables, the 'final' keyword indicates that the


variable's value cannot be changed once assigned. It essentially
makes the variable a constant.
 If the 'final' keyword is applied to a reference variable, it means
that the reference cannot be changed to point to a different
object, though the object itself can be modified.
 Explain the difference between prefix and postfix
increment/decrement operators (++i, i++, --i, i--) in Java.
 Prefix increment/decrement (++i, --i) first performs the operation
and then returns the value.
 Postfix increment/decrement (i++, i--) first returns the current
value and then performs the operation.

Week-2 Session-3(Polymorphism)

What is polymorphism in Java?


Polymorphism is the ability of an object to take on multiple forms. In Java, it
refers to the ability of different classes to be treated as objects of a common
superclass.(already define in quiz)

Actual Answer should be

The word polymorphism means having many forms. Polymorphism is


considered one of the important features of Object-Oriented
Programming. Polymorphism allows us to perform a single action in
different ways. In other words, polymorphism allows you to define one
interface and have multiple implementations. The word “poly” means
many and “morphs” means forms, So it means many forms.
Real-life Illustration of Polymorphism in Java: A person at the same
time can have different characteristics. Like a man at the same time is
a father, a husband, and an employee. So the same person possesses
different behaviors in different situations. This is called polymorphism.
Types of Java Polymorphism
In Java Polymorphism is mainly divided into two types:
 Compile-time Polymorphism
 Runtime Polymorphism

Interview Questions on Arrays

We can add proper examples in existing Q & A

 What is the difference between length and length() in Java?


 'length' is a property of arrays that returns the number of
elements in the array.
 'length()' is a method used with strings and other collections in
Java to return the number of characters or elements.

Explain the concept of the enhanced for loop (for-each loop) in


Java and how it is used with arrays.

 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{

public static void main(String args[]){


int arr[]={12,13,14,44};
for(int i:arr){
System.out.println(i);
} } }
What is the difference between arrays and ArrayLists in Java?
 Arrays have a fixed size, meaning the length of the array is predefined
and cannot be changed once created. ArrayLists, on the other hand,
are dynamic in size and can grow or shrink dynamically as elements
are added or removed.

Week3-Session 1 Interview Questions 1. What is Inheritance in Java?


Inheritance is an Object-oriented feature which allows a class to inherit
behavior and data from other classes. For example, a class Car can extend
the basic feature of Vehicle class by using Inheritance. One of the most
intuitive examples of Inheritance in the real world is Father-Son relationship,
where Son inherits Father's properties and behavior.

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.

2. Why can we not override the static method?


It is because the static method is the part of the class, and it is
bound with the class whereas the instance method is bound with
the object, and static gets memory in class area, and instance gets
memory in a heap.

Answer should add why?


Java, static methods belong to the class itself rather than to any specific
instance of the class. Therefore, they are associated with the class's
definition rather than with any particular object created from that class. Due
to this nature, static methods cannot be overridden in the same way that
instance methods can.

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.

3. What is the difference between Inheritance and Encapsulation?


Inheritance is an object oriented concept which creates a parent-
child relationship. It is one of the ways to reuse the code written for
parent class but it also forms the basis of Polymorphism. On the
other hand, Encapsulation is an object oriented concept which is
used to hide the internal details of a class e.g. Hash Map
encapsulate how to store elements and how to calculate hash
values.

Answer should modified

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.

 What is the difference between abstract classes and interfaces?


 Abstract classes can have both abstract and concrete methods,
while interfaces can only have abstract method signatures.
 A class can extend only one abstract class, but it can implement
multiple interfaces.
 Abstract classes can have constructors, while interfaces cannot.
 Abstract classes can have instance variables, while interfaces
cannot have non-constant instance variables.
 Can an interface extend another interface?
 Yes, an interface in Java can extend one or more other interfaces
using the 'extends' keyword.
 Can a class implement multiple interfaces in Java?
 Yes, a class in Java can implement multiple interfaces by
separating them with commas in the 'implements' clause.

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();
}

public CustomException(String message) {


super(message);
}
What are runtime exceptions in Java?
Runtime exceptions are those exceptions that occur at the run time of the program
execution. These exceptions are not noticed by the compiler at the compile time and
hence the program successfully gets compiled. Therefore, they are also called
unchecked exceptions. All subclasses of the java.lang.RunTimeException class and
java.lang.Error class belongs to runtime exceptions. Examples of runtime exceptions
include NullPointerException, NumberFormatException,
ArrayIndexOutOfBoundException, StackOverflowError, ClassCastException,
ArithmeticException, ConcurrentModificationException
Can you catch and handle Multiple Exceptions in Java?
There are three ways to handle multiple exceptions in Java:

 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.

 From Java 7 onwards, it is now possible to implement multiple catch blocks as


shown below
try { // do something
} catch (FileNotFoundException fileNotFoundException) {
// handle FileNotFoundException
} catch (EOFException eofException) {
// handle EOFException }
What is error?
Errors are those abnormal failures that are not in the scope of recovery for the
application. It is not possible to anticipate errors or recover from them. Errors
could be due to failures in hardware, out-of-memory, JVM crash, etc.

What does JVM do when an exception occurs in a program?


When there is an exception inside a method, the method creates and passes
(throws) the Exception object to the JVM. This exception object has information
regarding the type of exception and the program state when this error happens.
JVM is then responsible for finding if there are any suitable exception handlers to
handle this exception by going backwards in the call stack until it finds the right
handler. If a matching exception handler is not found anywhere in the call stack,
then the JVM terminates the program abruptly and displays the stack trace of the
exception.

What happens when an exception is thrown by the main method?


When there is an exception thrown by the main() method if the exception is not
handled, then the program is terminated by the Java Runtime, and the exception
message along with the stack trace is printed in the system console.

What happens when you run the below program?


import java.io.IOException;
import java.io.FileNotFoundException;

import javax.xml.bind.JAXBException;

public class TestExceptionDemo {

public static void main(String[] args) {


try {
demoException();
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (JAXBException e) {
System.out.println(e.getMessage());
}
}

public static void demoException() throws IOException, FileNotFoundException,


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

You might also like