Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
39 views

Java Interview

Uploaded by

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

Java Interview

Uploaded by

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

1.

Explain all keywords Public Static void main(String[] Args)

Public is access modifier, this method is accessible by any class

Static: with the help of static method can be accessed without creating the instance of the class

Void : this methods returns no value

Main: it is the name of the main java method

String args[] : Array of string type this contains the command line arguments that we can pass by
running the program.

2.What is object and class?

An object is instance of the class. Object has state and behaviour.it is a logical entity. eg. dog

A Class is template or blueprint form which objects are created. Eg. Animal

3.What is Static keyword:

Used for memory management (memory is allocation is done only one time at the time loading of
class)

Can be used with class, variable (also known as class variable), methods and block

(single copy of the static variable is created and shared among all the classes.)

Static members belong to class instead of specific instance.

Used to refer common properties of all object which is not unique of each object

Static methods can be overloaded but not overridden

Eg. Company name of the employee

4. What do you mean by constructor?

Special method used to initialise object

Has same name as of class name in which constructor I declared

Constructor must have no explicit return type(cannot be abstract, static, synchronized)

Constructor can be overloaded


Types of constructor

Default :

When constructor do not have any parameter

Used to provide default values to the constructor

Parameterised:

Constructor with a specific number of parameters

Used to provide distinct values to the object or even same value

Constructor overloading do not return any value.

5. this keyword in java

Used to refer current class instance variable

Used to invoke current class method(implicitly)

This() can be used to invoke current class constructor

This() can passed as argument in method call/constructor call.

This() can be used to return current class instance from the method.

6. What is local variable and instance variable?

Local variable:

Is variable that is declared within the method of a class.

Its scope is limited to the method in which it is declared.

Cannot be defined with static keyword.

Instance variable:

Is a variable that is declared inside a class but outside a method.

7. What is inheritance?

Inheritance is a process by which one class acquires the data-members and properties of the other
class. Mainly used for the reusability of the code.
8. What is encapsulation?

A process of wrapping code and data together into a single unit.

Used for data hiding.

Can be achieved by using private keyword.

Such data can be accessed to other class by setting getter and setter method as public

9. What is polymorphism?

Perform single action in multiple ways.

The most common oops concept used in java is having more than one method with same name in
single class.

Types:

Static/compile time: method overloading

Dynamic/runtime: method over-ridding

10. Method Overloading v/s method overriding

Method Overloading method overriding


Method with Same name but different Def: Inheriting the method from super class to
signature. sub class and changing the implementation
according to the sub class requirement
Method with same name and same signature

11. What is access modifier and it’s type?

Used to set the visibility of classes, data member, constructor and methods in other class

Default: Declaration visible only within the package (private package)

Private: Declaration are visible within the class only

Protected: Declaration are visible within the package or all sub-classes

Public: Declaration are visible everywhere

12. What is interface?

It looks like a class but not exactly a class

Can have methods and variables just like a class but methods declared in interface are by default
abstract (only method signature)

The variables declared in the interface are public, static and final.
Can be used to achieved loose coupling

13. What are abstract class?

A class declared using abstract keyword is called abstract class

It can have abstract method as well as concrete method

14.

Abstract class Interface


Extend only one class or one abstract class at the Can extend any number of interface at the time
time

Can have only abstract method


Can have both concrete as well as abstract
method
Abstract keyword is mandatory to declare Abstract key word is optional
method as an abstract

Can have protected and public abstract method Can have only public abstract method

Can have static, final or static final variable with Can have only public static final variable
any access specifier

15. Final keyword:

Final variable are nothing but constants, once initialise value remain cannot be changed

Final method cannot be overridden.

Final class if a class is declared as final than that class couldn’t be sub-classed. No class can extend
the final class.

16. What is Thread?

The flow of execution of java programme is called thread

Every java programme has atleast one thread called the main thread

Can be defined by extending the thread class or by implementing the runnable.


17. String v/s String builder v/s string buffer

String is a sequence of characters, it is an immutable object and cannot be changed once it is created

String can be created in 2 ways

string literals: String str =”welcome to testing world”;

Using new keyword: String str =new String(“testing world welcome you”);

String builder and StringBuffer classes are used for string manipulation.

These are mutable objects which provide methods such as substring, insert, append, delete for
String manipulation.

String builder String Buffer


Operation are not thread save and not Operation are thread save and synchronised
synchronised
Is used in single threaded environment This to be used when multiple threads are
working on the same String
Performance is faster as compared to String Performance is slower as compared to String
Buffer Builder
Syntax: Syntax:
StringBulider sb=new StringBuilder(str); StringBuffer sbf=new StringBuffer(str);

18. Array v/s ArrayList

Array Array-List
Is a fixed length data structure Belongs to variable length Collection class
Can contains both primitive datatype and objects Only supports object entries
Performance is faster Performance is slower
Store specific type of items Store any type of items
Do not support. Supports additional operations like indexOf,
Remove()
Syntax: Syntax:

String[] cars = {"Volvo", "BMW", "Ford", import java.util.ArrayList;


"Mazda"};
ArrayList<String> cars = new
ArrayList<String>(); // Create an
ArrayList object
19. collection is java

Collection framework is the combination of classes and interfaces which is used to store and
manipulate the data in the form of object.

Interfaces : Iterable collection, List, Que, Set

Class under List are list, linked list, vector, stack

Class under set are hastset, linked hashset

20. List v/s set

List Set
List is an ordered collection it maintains the Set is an unordered collection
insertion order
Allows duplicates Do not allow duplicates
List Implementation: arraylist, linkedlist Set implementation are
hashset,linkedhashset,treeset…
List allow any number of null value Can have only one null value
List-iterator used to traverse a list in both Iterator to traverse a set
direction
List interface has one legacy class called vector Set do not have any legacy class

21. ArrayList v/s vector

Array list Vector


Not synchronised synchronized
Not Thread save Thread saved as every methods is synchronise
Not legacy class Is a legacy class
Increases the size by 50% of the array size Increases the size by doubling the array size
Is fast Not fast
Uses the iterator interface to traverse

22. set v/s map

Set contains value whereas Map contains key and value both.

Set contains unique value whereas Map contains unique key with duplicate values

Set hold single number of null values whereas Map can includes a single null key with n numbers of
null values.
23.

HashMap HashTable
Synchronised : no Synchronised : yes
Allow null: one null key with n number of null Not alllowed
values
Maintain insertion order No order maintained
Inherits: AbstractMap class Dictionary class

24.exceptions and its types

Unwanted event that interrupts normal flow of the programme.

Checked exception:

Checked by compiler at the time of compilation.

Eg. IOException, ClassnotFoundException, etc

Unchecked Exception:

Not Checked during compile time, the compiler doesn’t force to handle these exception

Eg ArithmeticException, NullPoniterException, ArrayIndexOutOfBound Exception, etc

25. Exception handling keywords

Try, catch, finally, throws, throw

You might also like