1) What are the advantages of Java Package?
Advantages of Java package are:
- Provides access protection
- Removes naming collision
- Easily maintained
2) What are the types of Access Modifiers in Java?
Two types of Access Modifiers in Java are:
1) access modifiers
- private
- default
- protected
- public
2) non-access modifiers
- static
- abstract
- synchronized
- native
- volatile
- transient etc
3) What are the types of constructors in Java?
Two types of constructors in java are :
- Default constructor
- Parameterized constructor
4) What is the difference between constructor and method in Java?
Differnce between constructor and method :
Constructor |
Method |
It is used to initialize the state of an object |
It is used to expose behaviour of an object |
Invoked implicitly |
Invoked explicitly |
Must not have return type |
Must have return type |
It's name must be same as the class name. |
It's name may or may not be same as class name. |
5) What are the different oops concept in Java?
The different oops concepts are :
- Polymorphism
- Inheritance
- Encapsulation
- Abstraction
- Composition
- Association
- Aggregation
6) What are the restrictions of static method in Java?
Two main restrictions of static method are:
- It can't use non static data member
- super and this can't be used in static context
7) What will be the output of following piece of code?
class TutorialExam
{
int b=98;
public static void main(String args[])
{
System.out.println(b);
}
}
OUTPUT:
8) Can we override java main method?
We can't override main method in java because main method is static.
9) Write a program in java for Single inheritance ?
Example of Single inheritance:
class Mom{
void eat()
{
System.out.println("eating...");
}
}
class Baby extends Mom
{
void run()
{
System.out.println("Running...");
}
}
class TutorialExam{
public static void main(String args[]){
Baby b=new Baby();
b.run();
b.eat();
}
}
10) What are the types of inheritance in Java?
There are five types of inheritance in java:
- Single inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Multiple inheritance
- Hybrid inheritance
11) Write a program in java of super keyword where super() is provided by the compiler implicitly?
Example:
class Cars
{
Cars()
{
System.out.println("Choose a car : BMW, AUDI, JAGUAR");
}
}
class BMW extends Cars
{
BMW()
{
System.out.println("Selected car :BMW");
}
}
class TestCars
{
public static void main(String args[])
{
BMW d=new BMW();
}
}
12) Write a program in java of Runtime polymorphism?
Example of Runtime Polymorphism:
class Car
{
void run()
{
System.out.println("Running");
}
}
class Audi extends Car
{
void run()
{
System.out.println("Running with 150km/h");
}
public static void main(String args[])
{
Car c = new Audi();
c.run();
}
}
13) What are the types of binding in Java?
Two types of binding in java are:
- Static binding(early binding)
- Dynamic binding(late binding)
14) What are the ways to achieve Abstraction in Java?
Two ways to achieve abstraction in java:
- By using Abstract class
- By using Interface
15) What are the commonly used methods of Object class in Java?
Commonly used methods of Object class in java are:
- public int hashCode()
- public String toString()
- public final void notify()
- public final void notifyAll()
- public final Class getClass()
- public boolean equals(Object obj)
- protected void finalize()throws Throwable
- public final void wait()throws InterruptedException
- protected Object clone() throws CloneNotSupportedException
- public final void wait(long timeout)throws InterruptedException
- public final void wait(long timeout, int nanos)throws InterruptedException