0% found this document useful (0 votes)
10 views

Javainterview 2

The document discusses Java concepts like classes, objects, access modifiers, abstraction, static methods, interfaces, inheritance relationships, and multithreading. Classes are blueprints for creating objects that have identity, state, and behavior. Access modifiers control visibility, and abstraction is achieved through interfaces and abstract methods.

Uploaded by

Vinay Dave
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Javainterview 2

The document discusses Java concepts like classes, objects, access modifiers, abstraction, static methods, interfaces, inheritance relationships, and multithreading. Classes are blueprints for creating objects that have identity, state, and behavior. Access modifiers control visibility, and abstraction is achieved through interfaces and abstract methods.

Uploaded by

Vinay Dave
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java interview ques ons.

What is java and why it is used?


- Java is a widely-used programming language for coding web applica ons. It has been a popular choice
among developers for over two decades, with millions of Java applica ons in use today. Java is a mul -
pla orm, object-oriented, and network-centric language that can be used as a pla orm in itself.

What is class?
- A class in java is a collec on of data members and member func ons. A class is a user-de ned type that
describes what a certain type of object will look like. A Class is like an object constructor, or a "blueprint"
for crea ng objects.

What is object?
- A Java object is a member of a Java class. It is also called an instance of a class. Java objects have three
primary characteris cs: iden ty, state, and behavior. These characteris cs are the building blocks of any
class object and set the scene for how they are used.

What are the access modi ers in java and why they are use?
- Access modi ers are keywords that can be used to control the visibility of elds, methods, and
constructors in a class. The four access modi ers in Java are public, protected, default, and private.
Four Types of Access Modi ers
Private: We can access the private modi er only within the same class and not from outside the class.
Default: We can access the default modi er only within the same package and not from outside the
package. And also, if we do not specify any access modi er it will automa cally consider it as default.
Protected: We can access the protected modi er within the same package and also from outside the
package with the help of the child class. If we do not make the child class, we cannot access it from
outside the package. So inheritance is a must for accessing it from outside the package.
Public: We can access the public modi er from anywhere. We can access public modi ers from within
the class as well as from outside the class and also within the package and outside the package.

What is abstrac on method ?


- A method declared using the abstract keyword within an abstract class and does not have a de ni on
(implementa on) is called an abstract method. When we need just the method declara on in a super
class, it can be achieved by declaring the methods as abstracts.
tf
ti
ti
fi
ti
ti
ti
ti
fi
fi
ti
fi
fi
fi
fi
fi
fi
ti
ti
ti
ti
ti
fi
tf
fi
ti
fi
fi
ti
ti
What is instance varible ?
- An instance variable is a variable which is declared in a class but outside of constructors, methods, or
blocks. Instance variables are created when an object is instan ated, and are accessible to all the
constructors, methods, or blocks in the class. Access modi ers can be given to the instance variable.

What is sta c method ?


- A sta c method is a method de ned as a member of an object but is accessible directly from an API
object's constructor, rather than from an object instance created via the constructor.

Why sta c method is used ?


- A sta c method has two main purposes:
For u lity or helper methods that don't require any object state. Since there is no need to access
instance variables, having sta c methods eliminates the need for the caller to instan ate the object just
to call the method.
For the state that is shared by all instances of the class, like a counter. All instance must share the same
state. Methods that merely use that state should be sta c as well.

What is interface ?
- The interface in Java is a mechanism to achieve abstrac on. There can be only abstract methods in the
Java interface, not method body. It is used to achieve abstrac on, mul ple inheritance and to achieve
loose Coupling in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have a
method body.
Java Interface also represents the IS-A rela onship.

Explain is-a and has-a rela on?


- Is-A rela onship depends on inheritance.It is used for code reusability in Java.When there is an extends
or implement keyword in the class declara on in Java, then the speci c class is said to be following the
Is-A rela onship.

What is mul treading in java?


- Mul threading in Java is a process of execu ng two or more threads simultaneously to maximum
u liza on of CPU. Mul threaded applica ons execute two or more threads run concurrently. Hence, it is
also known as Concurrency in Java.
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
fi
ti
ti
ti
ti
ti
ti
fi
ti
ti
fi
ti
ti
Why is Java a pla orm independent language?
- Java language was developed in such a way that it does not depend on any hardware or so ware due
to the fact that the compiler compiles the code and then converts it to pla orm-independent byte code
which can be run on mul ple systems.
The only condi on to run that byte code is for the machine to have a run me environment (JRE) installed
in it.

Why is Java not a pure object oriented language?


- Java supports primi ve data types - byte, boolean, char, short, int, oat, long, and double and hence it
is not a pure object oriented language.

Di erence between Heap and Stack Memory in java.


- Stack memory is the por on of memory that was assigned to every individual program. And it was
xed. On the other hand, Heap memory is the por on that was not allocated to the java program but it
will be available for use by the java program when it is required, mostly during the run me of the
program.

Can java be said to be the complete object-oriented programming language?


- We can say that - Java is not a pure object-oriented programming language, because it has direct
access to primi ve data types. And these primi ve data types don't directly belong to the Integer
classes.

What do you mean by data encapsula on?


- Data Encapsula on is an Object-Oriented Programming concept of hiding the data a ributes and their
behaviours in a single unit.
It helps developers to follow modularity while developing so ware by ensuring that each object is
independent of other objects by having its own methods, a ributes, and func onali es.
It is used for the security of the private proper es of an object and hence serves the purpose of data
hiding.

The di erence between equals() method and equality operator (==) in Java?
- equals() method is used for checking the equality of contents between two objects as per the speci ed
business logic.
fi
ff
ff
ti
ti
ti
tf
ti
ti
ti
ti
ti
ti
ti
tt
ft
fl
ti
tf
ti
ti
tt
ti
ft
fi
== operator is used for comparing addresses (or references), i.e checks if both the objects are poin ng to
the same memory loca on.

Can the main method be Overloaded?


- Yes, It is possible to overload the main method. We can create as many overloaded main methods we
want. However, JVM has a prede ned calling method that JVM will only call the main method with the
de ni on of -
public sta c void main(string[] args)

Overloading and Overriding in java?


- Overloading is same class same method name di erent paramters.
Overriding is di erent Class same method name and same parameters.

Explain the use of nal keyword in variable, method and class.


- In Java, the nal keyword is used as de ning something as constant / nal and represents the non-
access modi er.
nal variable:
When a variable is declared as nal in Java, the value can’t be modi ed once it has been assigned.
If any value has not been assigned to that variable, then it can be assigned only by the constructor of the
class.
nal method:
A method declared as nal cannot be overridden by its children's classes.
A constructor cannot be marked as nal because whenever a class is inherited, the constructors are not
inherited.
nal class:
No classes can be inherited from the class declared as nal. But that nal class can extend other classes
for its usage.

Explain nal, nally and nalize keywords.


- Final: If any restric on is required for classes, variables, or methods, the nal keyword comes in handy.
Inheritance of a nal class and overriding of a nal method is restricted by the use of the nal keyword.
The variable value becomes xed a er incorpora ng the nal keyword.
fi
fi
fi
fi
ti
fi
ti
fi
fi
fi
ff
fi
fi
ti
fi
ti
fi
fi
fi
fi
ft
fi
fi
fi
ti
ff
fi
fi
fi
fi
fi
fi
fi
ti
Finally: It is the block used in execp on handling. all the codes wri en inside nally block gets executed
irrespec ve of handling of excep ons.
Finalize: Prior to the garbage collec on of an object, the nalize method is called so that the clean-up
ac vity is implemented.

When can you use super keyword?


- Following are the cases when this keyword can be used:
Accessing data members of parent class when the member names of the class and its child subclasses
are same.
To call the default and parameterized constructor of the parent class inside the child class.
Accessing the parent class methods when the child classes have overridden them.

Can the sta c methods be overloaded?


- Yes! There can be two or more sta c methods in a class with the same name but di ering input
parameters.

Why is the main method sta c in Java?


- The main method is always sta c because sta c members are those methods that belong to the
classes, not to an individual object. So if the main method will not be sta c then for every object, It is
available. And that is not acceptable by JVM. JVM calls the main method based on the class name itself.
Not by crea ng the object.
Because there must be only 1 main method in the java program as the execu on starts from the main
method. So for this reason the main method is sta c.

Can the sta c methods be overridden?


- No! Declara on of sta c methods having the same signature can be done in the subclass but run me
polymorphism can not take place in such cases.

What is the main objec ve of garbage collec on?


- The main objec ve of this process is to free up the memory space occupied by the unnecessary and
unreachable objects during the Java program execu on by dele ng those unreachable objects.

What is a ClassLoader?
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
fi
ti
tt
ti
ti
fi
ff
ti
- Java Classloader is the program that belongs to JRE (Java Run me Environment). The task of
ClassLoader is to load the required classes and interfaces to the JVM when required.

What part of memory - Stack or Heap - is cleaned in garbage collec on process?


- Heap.

What is a singleton class in Java?


- Singleton classes are those classes, whose objects are created only once. And with only that object the
class members can be accessed.

How would you di eren ate between a String, StringBu er, and a StringBuilder?
- Storage area: In string, the String pool serves as the storage area. For StringBuilder and StringBu er,
heap memory is the storage area.
Mutability: A String is immutable, whereas both the StringBuilder and StringBu er are mutable.
E ciency: It is quite slow to work with a String. However, StringBuilder is the fastest in performing
opera ons. The speed of a StringBu er is more than a String and less than a StringBuilder. (For example
appending a character is fastest in StringBuilder and very slow in String because a new memory is
required for the new String with appended character.)
Thread-safe: In the case of a threaded environment, StringBuilder and StringBu er are used whereas a
String is not used. However, StringBuilder is suitable for an environment with a single thread, and a
StringBu er is suitable for mul ple threads.

What is the di erence between the ‘throw’ and ‘throws’ keyword in java?
- The ‘throw’ keyword is used to manually throw the excep on to the calling method.
And the ‘throws’ keyword is used in the func on de ni on to inform the calling method that this
method throws the excep on. So if you are calling, then you have to handle the excep on.

Is it mandatory for a catch block to be followed a er a try block?


- No, it is not necessary for a catch block to be present a er a try block. - A try block should be followed
either by a catch block or by a nally block. If the excep ons likelihood is more, then they should be
declared using the throws clause of the method.
ffi
ti
ff
ff
ff
ti
ti
ti
fi
ff
ti
ft
fi
ti
ti
ft
ff
ti
ti
ti
ff
ff
ti
ff
Oop's concepts

Object
Class
Inheritance
Polymorphism
Abstrac on
Encapsula on

Coupling
Cohesion
Associa on
Aggrega on
Composi on
ti
ti
ti
ti
ti

You might also like