Core Java Interview Questions -6

Core Java Interview Questions and Answers -6

1)Can you make a constructor final?
 

No, constructor can't be final.

2)What is static variable?
static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
static variable gets memory only once in class area at the time of class loading.

3)Which class is the superclass for every class.
Object class.

4)What is the purpose of default constructor?
The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.

5)why main method is static?

because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation..


6)Is constructor inherited?

No, constructor is not inherited.

7)Can we execute a program without main() method?
Ans)Yes,one of the way is, by static block.


8)What if the static modifier is removed from the signature of the main method?

Program compiles. But at runtime throws an error "NoSuchMethodError".

9)What is this in java?

It is a keyword that that refers to the current object.


10)Does constructor return any value?

yes, that is current instance (You cannot use return type yet it returns a value).


11)What is composition?

Holding the reference of the other class within some other class is known as composition.

12)What is static block?
Is used to initialize the static data member.
It is excuted before main method at the time of classloading.

13)Can you use this() and super() both in a constructor?
No. Because super() or this() must be the first statement.

14)Can we override the overloaded method?

Yes.

15)What is difference between aggregation and composition?

Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike has an indicator (aggregation) but bike has an engine (compostion).

16)Why method overloading is not possible by changing the return type in java?

Becauseof ambiguity.

17)What is static method?
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.

18)Why Java does not support pointers?
Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.

19)What is method overriding:

If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method..


20)What is covariant return type?
Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type.


21)What is super in java?

It is a keyword that refers to the immediate parent class object..


22)What is Inheritance?

Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding..


23)Why we cannot override static method?

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

24)What is object cloning?

The object cloning is used to create the exact copy of an object. .


25) Can we overload main() method?
Yes, ofcourse! You can have many main() methods in a class by overloading the main method.

26)Difference between method Overloading and Overriding.
Method overloading increases the readability of the program,Method overriding provides the specific implementation of the method that is already provided by its super class.
method overlaoding is occurs within the class,Method overriding occurs in two classes that have IS-A relationship.
In this case, parameter must be different,In this case, parameter must be same.

27)Can we override static method?

No, you can't override the static method because they are the part of class not object.

28)What is method overloading?

If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program..


29)Can you have virtual functions in Java?

Yes, all functions in Java are virtual by default.

30) Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.

31)What is final method?

Final methods can't be overriden.


32) What is Runtime Polymorphism?
Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time.

In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.


33) What is the difference between abstraction and encapsulation?

Abstraction hides the implementation details whereas encapsulation hides the data.

Abstraction lets you focus on what the object does instead of how it does it.

34) Can we intialize blank final variable?
Yes, only in constructor if it is non-static. If it is static blank final variable, it can be initialized only in the static block.


35) What is abstraction?

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Abstraction lets you focus on what the object does instead of how it does it.

36)What is final class?

Final class can't be inherited.


37) Can there be any abstract method without abstract class?

No, if there is any abstract method in a class, that class must be abstract.

38)What is final variable?

If you make any variable as final, you cannot change the value of final variable(It will be constant).


39)Can you declare the main method as final?

Yes, such as, public static final void main(String[] args){}.

40) Can you use abstract and final both with a method?

No, because abstract method needs to be overridden whereas you can't override final method.

41) Is it possible to instantiate the abstract class?

No, abstract class can never be instantiated.

42) What is the difference between static binding and dynamic binding?

In case of static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at runtime.


43) What is marker interface?

An interface that have no data member and method is known as a marker interface.For example Serializable,Cloneable etc.

44) What is abstract class?

A class that is declared as abstract is known as abstract class.It needs to be extended and its method implemented.It cannot be instantiated.


45) Can you declare an interface method static?

No, because methods of an interface is abstract bydefault, and static and abstract keywords can't be used together.

46) What is blank final variable?

A final variable, not initalized at the time of declaration, is known as blank final variable.


47)Can you achieve Runtime Polymorphism by data members?
No.

48) What is interface?

Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achive fully abstraction and multiple inheritance.


49)Can an Interface be final?

No, because its implementation is provided by another class.

50)When can an object reference be cast to an interface reference?
An object reference can be cast to an interface reference when the object implements the referenced interface.

51)Do I need to import java.lang package any time? Why ?

No. It is by default loaded internally by the JVM.

52)Can we define private and protected modifiers for variables in interfaces?

No, they are implicitely public.

53)Can I import same package/class twice? Will the JVM load the package twice at runtime?

One can import the same package or same class multiple times. Neither compiler nor JVM complains about it.But the JVM will internally load the class only once no matter how many times you import the same class.

54)What is package?

A package is a group of similar type of classes interfaces and subpackages. It provides access protection and removes naming collision.

55)What is static import ?

By static import, we can access the static members of a class directly, there is no to qualify it with the class name.


56) What is Exception Handling?
Exception Handling is a mechanism to handle runtime errors.It is mainly used to handle checked exceptions.

57)What is difference between Checked Exception and Unchecked Exception?

1)Checked Exception:
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time.

2)Unchecked Exception:
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.


58)What is the base class for Error and Exception?

Throwable.

59)Is it necessary that each try block must be followed by a catch block?
It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

60)What is finally block?

finally block is a block that is always executed.

61)Can finally block be used without catch?
Yes, by try block. finally must be followed by either try or catch.


62)Is there any case when finally will not be executed?

finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).

63)What is the meaning of immutable in terms of String?

The simple meaning of immutable is unmodifiable or unchangeable. Once string object has been created, its value can't be changed..


64)What is shutdown hook?

The shutdown hook is basically a thread i.e. invoked implicitely before JVM shuts down. So we can use it perform clean up resource.

65)Can subclass overriding method declare an exception if parent class method doesn't throw an exception ?

Yes but only unchecked exception not checked. more details...


66)What is exception propagation ?
Forwarding the exception object to the invoking method is known as exception propagation.


67)Can an exception be rethrown?

Yes.

68)Why java uses the concept of string literal?
To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).

69)How can we create immutable class in java ?

We can create immutable class as the String class by defining final class


70)How many ways we can create the string object?
There are two ways to create the string object, by string literal and by new keyword.


71)Can a class have an interface?

Yes, it is known as nested interface.


72)How many objects will be created in the following code?

String s1="Welcome";
String s2="Welcome";
String s3="Welcome";
Only one object.

73)What is the basic difference between string and stringbuffer object?

String is an immutable object. StringBuffer is a mutable object.


74)What is nested class?

A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class. .


75) Can we access the non-final local variable, inside the local inner class?

No, local variable must be constant if you want to access it in local inner class.


76) What is multithreading?
Multithreading is a process of executing multiple threads simultaneously.Its main advantage is:
 •Threads share the same address space.
 •Thread is lightweight.
 •Cost of communication between process is low.

77)Can we make the user thread as daemon thread if thread is started?

No, if you do so, it will throw IllegalThreadStateException

78)Can an Interface have a class?

Yes, they are static implicitely.


79) What is the purpose of Synchnorized block?

•Synchronized block is used to lock an object for any shared resource.
•Scope of synchronized block is smaller than the method.

80) What is static synchronization?

If you make any static method as synchronized, the lock will be on the class not on object.


81) Is it possible to start a thread twice?

No, there is no possibility to start a thread twice. If we does, it throws an exception.


82)How many objects will be created in the following code?
String s=new String("Welcome");
Two objects, one in string constant pool and other in non-pool(heap).


83)Why string objects are immutable in java?
Because java uses the concept of string literal. Suppose there are 5 reference variables,all referes to one object "sachin".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.

84) Is there any difference between nested classes and inner classes?
Yes ofcourse! inner classes are non-static nested classes i.e. inner classes are the part of nested classes.


85)What is deadlock?

Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource which is held by the other waiting thread.


86) What is synchronization?
Synchronization is the capabilility of control the access of multiple threads to any shared resource.It is used:
1.To prevent thread interference.
2.To prevent consistency problem.

87) What about the daemon threads?

The daemon threads are basically the low priority threads that provides the background support to the user threads. It provides services to the user threads. .


88) What does join() method?

The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.


89)What is the difference between preemptive scheduling and time slicing?

Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

90)What is the difference between StringBuffer and StringBuilder ?
StringBuffer is synchronized whereas StringBuilder is not synchronized.

91) Can we call the run() method instead of start()?

yes
, but it will not work as a thread rather it will work as a normal object so there will not be context-switching between the threads.

92)What is the purpose of toString() method in java ?
The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

93)What is nested interface ?
Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.

94) What is thread?
A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.

95)When should we interrupt a thread?
We should interrupt a thread if we want to break out the sleep or wait state of a thread.

96)Can Java object be locked down for exclusive use by a given thread?
Yes. You can lock an object by putting it in a "synchronized" block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.

97)What is the difference between notify() and notifyAll()?
notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.

98) What is Garbage Collection?
Garbage collection is a process of reclaiming the runtime unused objects.It is performed for memory management.

99) What is gc()?

gc() is a daemon thread.gc() method is defined in System class that is used to send request to JVM to perform garbage collection.

100) What is the purpose of finalize() method?

finalize() method is invoked just before the object is garbage collected.It is used to perform cleanup processing.

-----------------
Also Read:

0 comments:

Post a Comment