0% found this document useful (0 votes)
3 views8 pages

Java Theory Interview Questions

Uploaded by

darlingsayed1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Java Theory Interview Questions

Uploaded by

darlingsayed1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

OOPS concepts:

Q. What is mean by OOPs in java ?


(OOP) is a programming model used in Java that organizes software design around
objects and data, rather than logic and functions.
OOP can make software development more modular, reusable, and maintainable, which
can make it easier to upgrade and update the system.

Class & Objects: A class is like a blueprint or template, that's used to create objects,
which are the products developed by inheriting the class's methods and variables
Objects : Objects are the instances of a class that are created to use the attributes and
methods of a class
Data Encapsulation: Binding the data and metods into a single unit is called as
Encapsuation.
Example:
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
}
Public class company
{
public static void main (String[] args)
{
Employee e = new Employee();
e.setName("Robert");
System.out.println("Employee's name: " + e.getName());
}
}

Encapsulation is achieved by making the variables private and providing public getter
and setter methods to access and update the values of the private variables

Inheritance:
Allowing one class to inherit the data and methods from the super class is called
Inheritance.

Polymorphism:
Polymorphism in java refers to any entity whether it is an operator or a constructor or
any method which takes many forms or can be used for multiple tasks

Q. Defien abstraction in java ?


It enables you to hide complex implementation details and display only the most
essential features of an object.
Q.Differances between abstract and Interface?
-> Abstract class can have both abstract and non-abstract methods. But Interface
can have only abstract metods and static variables.
-> Interface supports multiple Inheritance and abstract class doesn’tsupport.
-> By default Interface variables are public, static and final. But in abstract class no
need to declare.
-> Interface keyword is used t declare Interface. Abstract keyword is used to
declare abstract class.

1.What is Polymorphism: Polymorphism in java refers to any entity whether it is an


operator or a constructor or any method which takes many forms or can be used for
multiple tasks

Runtime Polymorphism: In this example, we are creating two classes Bike and
Splendor. Splendor class extends Bike class and overrides its run() method. We are
calling the run method by the reference variable of Parent class. Since it refers to the
subclass object and subclass method overrides the Parent class method, the subclass
method is invoked at runtime.
Since method invocation is determined by the JVM not compiler, it is known as runtime
polymorphism.

class Bike{
void run(){System.out.println("running");}
}
class Splendor extends Bike{
void run(){System.out.println("running safely with 60km");}
public static void main(String args[]){
Bike b = new Splendor();//upcasting
b.run();
}
}

Compile time Polymorphism: Method Overloading is the example for this.

Q.can we Overload static and final methods?


We can load only overload Static methods but we can’t overload Final methods

2.Can we Override static and final methods ?


No We can’t override

3.Can we access super class version of overridden method in


the sub class. If yes, how?
Yes. We can access super class version of overridden method in the sub class
using super keyword.

4. Can we remove throws clause of a method while


overriding it?
Yes, we can remove throws clause of a method while overriding it.
5.Can you list out the differences between method
overloading and method overriding?
Overloading occurs when methods in the same class have the same method name but
different parameters,
whereas overriding occurs when two methods have the same method name and
parameters

What is the difference between ArrayList and LinkedList ?


ArrayList internally uses a dynamic array to store the elements. LinkedList internally
uses a doubly linked list to store the elements.
Manipulating ArrayList takes more time due to the internal implementation. Whenever
we remove an element, internally, the array is traversed and the memory bits are
shifted
Manipulating LinkedList takes less time compared to ArrayList because, in a doubly-
linked list, there is no concept of shifting the memory bits. The list is traversed and the
reference link is changed.
ArrayList class works better when the application demands storing the data and
accessing it.
LinkedList class works better when the application demands manipulation of the stored
data
ArrayList & LinkedList both are Non Synchronized
HashSet & LinkedHashSet both are Non Synchrnized
HashMap and LikedHashMap both are Non Synchrnized

Q.what is the difference between Call by value and call by


referance

class Operation{
int data=50;
void change(int data){
data=data+100;//changes will be in the local variable only
}

public static void main(String args[]){


Operation op=new Operation();
System.out.println("before change "+op.data);
op.change(500);
System.out.println("after change "+op.data);
}
}

Overloaded methods can have same or different return types.


Override methods can have only same return types.

Static or final methods can be Overloaded


Static or final methods can’t be Overrided

Overloading shows static polymorphism.


Overriding shows dynamic polymorphism
Overloading Example:
Arrays.toString(char[], int[] , Object[])
arrayList. Remove(index)
arrayList. Remove(Object)
Click()
Click(WebElement elm)
ContextClick()
COntextClick(WebElement elm)

public class MainClass


{
static String concateString(String s1, String s2)
{
return s1+s2;
}
static String concateString(String s1, String s2, String s3)
{
return s1+s2+s3;
}
static String concateString(String s1, String s2, String s3, String s4)
{
return s1+s2+s3+s4;
}
public static void main(String[] args)
{
concateString("ONE", "TWO");
concateString("ONE", "TWO", "THREE");
concateString("ONE", "TWO", "THREE", "FOUR");
}
}

Overriding example:
class SuperClass
{
void SuperClassMethod()
{
System.out.println("SUPER CLASS METHOD");
}
}
class SubClass extends SuperClass
{
@Override
void SuperClassMethod()
{
System.out.println("SUPER CLASS METHOD IS OVERRIDDEN");
}
}

Exception handling:
Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, null pointer exception,

IOException: when a failure occurs while performing read, write or search operations in
files or directories
A ClassNotFoundException : in Java occurs when the Java Virtual Machine (JVM) cannot
locate a specific class referenced in the code.
Null pointer exception: a NullPointerException occurs when a variable that is being
accessed has not yet been assigned to an object, in other words, the variable is assigned
as null .
NoSuchElementException: This is the most common Selenium exception which is a
subclass of NotFoundException class and thrown by findElement() method of Selenium
WebDriver. This exception occurs when the locators defined in the Selenium program is
unable to locate the element in the DOM.
InvalidSelectorException: This exception in selenium is thrown when invalid or incorrect
selector is used. This case probably occurs while creating XPATH.
TimeoutException: a TimeoutException is an exception when an operation takes longer
than the specified timeout value.

Q. what does public static void main mean in java

main() method is the starting point from where the JVM starts the execution of a Java
program.
Public : Is an AccessModifier Making the main() method public makes it globally
available. It is made public so that JVM can invoke it from outside the class.
Static : Its an keyword. The main() method is static so that JVM can invoke it
without instantiating the class.
Void : main() method doesn’t return any vlue
String[] args : It stores Java command-line arguments and is an array of
type java.lang.String class. Here, the name of the String array is args but it is not fixed
and the user can use any name in place of it.

6. Access Modifiers:
Default access modifiers are accessible only within the same package.
Private : The methods or data members declared as private are accessible
only within the class in which they are declared.
Protected: The methods or data members declared as protected are accessible
within the same package or subclasses in different packages.
Public: Classes, methods, or data members that are declared as public are accessible
from everywhere in the program.

Q.what is mean by Singleton class?


A singleton class in Java is a class that only allows one instance to exist at a time during
the runtime of an application. This is achieved through the singleton pattern, which is a
software design pattern that limits the instantiation of a class to a single instance

public class Singleton {


private static Singleton singleton_ref = null;
//declared constructor as private
private Singleton()
{
System.out.Println("This is Singleton Class");
}
//declared static method that returns the object of singleton class
public static Singleton getInstance() {
if(singleton_ref == null)
singleton_ref = new Singleton();
return singleton_ref;
}

public static void main(String[] args)


{
Singleton a = Singleton.getInstance();
Singleton b = Singleton.getInstance();
}
}
Collections:
List: implemented by  ArrayList, LinkedList and Vector
Set: implemented by  HashSet and LinkedHashSet
Map: implimeted by HashMap & HashTable
extends by SortedMap and its implemented by TreeMap
HashMap uses Buckets to store the objects as LinkedHashMap uses Double-linked Buckets
to store the elements.

Difference between Collection and Collections ?


Collection is an interface that represents a group of objects as a single unit, while
collections is a utility class that provides methods for operating on collections

Strings:
What is the difference between str1 == str2 and
str1.equals(str2)?
->If str1 and str2 are compared using the == operator, then the result will be false,
because both have different addresses in the memory. Both must have the same address
in the memory for the result to be true.
->If you use the equals method, the result is true since it's only comparing the values
given to str1 and str2, even though they are different objects.

Difference between String Literal and String Object?


String literal in Java is a set of characters that is created by enclosing them inside a pair
of double quotes. In contrast, String Object is a Java is a set of characters that is created
using the new() operator.

Q.Difference between String Builder and String Buffer in


java?
StringBuilder class can be used when you want to modify a string without creating a
new object. For example, using the StringBuilder class can boost performance when
concatenating many strings together in a loop.
A string buffer is like a String , but can be modified. At any point in time it contains
some particular sequence of characters, but the length and content of the sequence can
be changed through certain method calls. String buffers are safe for use by multiple
threads.

Difference : StringBuffer is synchronized, meaning its methods are thread-safe and


can be safely used in a multithreaded environment. On the other hand, StringBuilder is
not synchronized, which makes it faster than StringBuffer, but it is not thread-safe and
should not be used in a multithreaded environment.
And Both StringBuffer and Strign Builder are Mutable.

Difference between String buffer and String Builder ?


StringBuffer’s methods are synchronized, making it thread-safe and safe to use in a
multithreaded environment. StringBuilder’s methods are not synchronized, making it
faster than StringBuffer but not thread-safe.

Speed
Because of the additional synchronization mechanisms, StringBuffer is slower than
StringBuilder.

Use cases
If you’re working in a single-threaded environment, StringBuilder is recommended over
StringBuffer. If you need thread-safety, StringBuffer is the better choice.
Exception Hierarchy :

Q. What are wrapper classes in java and why do we use it?


The primitive data types in java are not objects, by default. They need to be converted
into objects using wrapper classes.

Difference between sleep and wait methods


Sleep method is from Thread Class and it is static method
Wait methods is from Object class and it is non static method

You might also like