Our website is made possible by displaying online advertisements to our visitors.Please consider supporting us by disabling your ad blocker.
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Translate it in your own Language

Print this Job Post

Print Friendly and PDF
Showing posts with label Java Interviews Questions. Show all posts
Showing posts with label Java Interviews Questions. Show all posts

Tuesday, May 27, 2014

Question-165:What is JDBC?
Answer:
JDBC is a Java API that is used to connect and execute query to the database. JDBC API uses jdbc drivers to connects to the database.


Question-166: What is JDBC Driver?
Answer:
JDBC Driver is a software component that enables java application to interact with the database.There are 4 types of JDBC drivers:
JDBC-ODBC bridge driver
Native-API driver (partially java driver) Network Protocol driver (fully java driver) Thin driver (fully java driver).


Question-167: What are the steps to connect to database in java?
Answer:

  • Register the driver class
  • Creating connection
  • Creating statement
  • Executing queries
  • Closing connection.



Question-165: What is the difference between Statement and PreparedStatement interface?
Answer:
In case of Statement, query is complied each time whereas in case of PreparedStatement, query is complied only once. So performance of PreparedStatement is better than Statement.


Question-169: How can we execute stored procedures and functions?
Answer:
By using Callable statement interface, we can execute procedures and functions.


Question-170: How can we store and retrieve images from the database?
Answer:
By using PreparedStaement interface, we can store and retrieve images.


Question-161: What is Locale?
Answer:
A Locale object represents a specific geographical, political, or cultural region.


Question-162: How will you load a specific locale?
Answer:
By ResourceBundle.getBundle(?) method.

Java Bean Interview Questions


Question-163: What is a JavaBean?
Answer:
are reusable software components written in the Java programming language, designed to be manipulated visually by a software develpoment environment, like JBuilder or VisualAge for Java.

RMI Interview Questions


Question-164: Can RMI and Corba based applications interact?
 Answer:
Yes they can. RMI is available with IIOP as the transport protocol instead of JRMP.

JDBC Interview Questions

Question-148:What are wrapper classes?
 Answer:
Wrapper classes are classes that allow primitive types to be accessed as objects.


Question-149: What is a native method?
Answer:
A native method is a method that is implemented in a language other than Java.


Question-150: What is the purpose of the System class?
Answer:
The purpose of the System class is to provide access to system resources.


Question-151: What comes to mind when someone mentions a shallow copy in Java?
Answer:
Object cloning.


Question-152: What is singleton class?
Answer:
Singleton class means that any given time only one instance of the class is present, in one JVM. AWT and SWING Interview Questions


Question-153: Which containers use a border layout as their default layout?
Answer:
The Window, Frame and Dialog classes use a border layout as their default layout.


Question-154: Which containers use a FlowLayout as their default layout?
Answer:
The Panel and Applet classes use the FlowLayout as their default layout.


Question-155: What are peerless components?
Answer:
The peerless components are called light weight components.


Question-156: Is the difference between a Scrollbar and a ScrollPane?
Answer:
A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.


Question-157: What is a lightweight component?
 Answer:
Lightweight components are the one which doesn?t go with the native call to obtain the graphical units. They share their parent component graphical units to render them. For example, Swing components.


Question-158: What is a heavyweight component?
Answer:
For every paint call, there will be a native call to get the graphical units.For Example, AWT.


Question-159: What is an applet?
Answer:
An applet is a small java program that runs inside the browser and generates dynamic contents.


Question-160: Can you write a Java class that could be used both as an applet as well as an application?
Answer:
Yes. Add a main() method to the applet.

Internationalization Interview Questions


Question-131:What is difference between ArrayList and Vector?
Answer:

ArrayList:


  1. ArrayList is not synchronized.
  2. ArrayList is not a legacy class
  3. ArrayList increases its size by 50% of the array size.

Vector:

  1. Vector is synchronized.
  2. Vector is a legacy class. 
  3. Vector increases its size by doubling the array size. 

Question-132: What is difference between ArrayList and LinkedList?
Answer:

ArrayList:


  1. ArrayList uses a dynamic array
  2. ArrayList is not efficient for manipulation because a lot of shifting is required.

Linked List: 

  1.  LinkedList uses doubly linked list. 
  2.  LinkedList is efficient for manipulation.


Question-133: What is difference between HashMap and Hashtable? 
Answer:

HashMap:


  1. HashMap is not synchronized. 
  2. HashMap can contain one null key and multiple null values.

Hashtable:

  1. Hashtable is synchronized. 
  2. Hashtable cannot contain any null key nor value. 


Question-135: What is hash-collision in Hashtable and how it is handled in Java?
Answer:
Two different keys with the same hash value. Two different entries will be kept in a single hash bucket to avoid the collision.


Question-136: What is difference between HashSet and HashMap?
Answer:
HashSet contains only values whereas HashMap contains entry(key,value).


Question:137: What is difference between HashMap and TreeMap?
Answer:

HashMap:


  1. HashMap is can contain one null key. 
  2. HashMap maintains no order.

TreeMap:


  1. TreeMapconnot contain any null key.
  2. TreeMap maintains ascending order.



Question-138:What is difference between HashSet and TreeSet?
Answer:
HashSet maintains no order whereas TreeSet maintains ascending order.


Question-142: What is difference between List and Set?
Answer:
List can contain duplicate elements whereas Set contains only unique elements.


Question-143: What is difference between Iterator and ListIterator?
Answer:
Iterator traverses the elements in forward direction only whereas ListIterator traverses the elements in forward and backward direction.


Question-144: Can you make List,Set and Map elements synchronized?
Answer:
Yes, Collections class provides methods to make List,Set or Map elements as synchronized: public static List synchronizedList(List l){}
public static Set synchronizedSet(Set s){}
public static SortedSetsynchronizedSortedSet(SortedSet s){} 
public static Map synchronizedMap(Map m){}
public static SortedMapsynchronizedSortedMap(SortedMap m){}


Question-145: What is difference between Iterator and Enumeration?
Answer:

Iterator:


  1. Iterator can traverse legacy and non-legacy elements.
  2. Iterator is fail-fast.
  3. Iterator is slower than Enumeration.

Enumeration:


  1. Enumeration can traverse only legacy elements.
  2. Enumeration is not fail-fast.
  3. Enumeration is faster than Iterator. 



Question-146: What is difference between Comparable and Comparator?
Answer:

Comparable:


  1. Comparable provides only one sort of sequence. 
  2. It provides one method named compareTo().
  3. It is found in java.lang package. 
  4. If we implement Comparable interface, actual class is modified. 

Comparator:


  1. Comparator provides multiple sort of sequences. 
  2. It provides one method named compare().
  3. it is found in java.util package.  
  4. Actual class is not modified. 



Question-147: What is the Dictionary class?
Answer:
The Dictionary class provides the capability to store key-value pairs.

Miscellaneous Interview Questions


Question-125: What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?
Answer:
The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.


Question-126: Whatan I/O filter?
Answer:
An I/O filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another.


Question-127: What is serialization?
Answer:
Serialization is a process of writing the state of an object into a byte stream.It is mainly used to travel object’s state on the network.


Question-128: What is Deserialization?
Answer:
Deserialization is the process of reconstructing the object from the serialized state.It is the reverse operation of serialization.


Question-129: What is transient keyword?
 Answer:
If you define any data member as transient,it will not be serialized.


Question-130:What is Externalizable?
Answer:
Externalizable interface is used to write the state of an object into a byte stream in compressed format.It is not a marker interface.


Question-131: What is the difference between Serializalble and Externalizable interface?
Answer:
Serializable is a marker interface but Externalizable is not a marker interface.When you use Serializableinterface, your class is serialized automatically by default. But you can override writeObject() and readObject() two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class’s serialization process.


Question-132: How do I convert a numeric IP address like 192.18.97.39 into a hostname like java.sun.com?
Answer:
By InetAddress.getByName(“192.18.97.39″).getHostName() where 192.18.97.39 is the IP address.


Question-133: What is reflection?
Answer:
Reflection is the process of examining or modifying the runtime behaviour of a class at runtime.It is used in:
IDE (Integreted Development Environment) e.g. Eclipse,MyEclipse,NetBeans. Debugger Test Tools etc.


Question-134: Can you access the private method from outside the class?
Answer:
Yes, by changing the runtime behaviour of a class if the class is not secured.

Collection Interview Questions


Question-117: What is Garbage Collection?
Answer:
Garbage collection is a process of reclaiming the runtime unused objects.It is performed for memory management.


Question-118: What is gc()?
Answer:
gc() is a daemon thread.gc() method is defined in System class that is used to send request to JVM to perform garbage collection.


Question-119: What is the purpose of finalize() method?
Answer:
finalize() method is invoked just before the object is garbage collected.It is used to perform cleanup processing.


Question-120: Can an unrefrenced objects be refrenced again?
Answer:
Yes.


Question-121: What kind of thread is the Garbage collector thread?
Answer:
Daemon thread.


Question-122: What is difference between final, finally and finalize?
Answer:

  1. final: final is a keyword, final can be variable, method or class.You, can’t change the value of final variable, can’t override final method, can’t inherit final class.
  2. finally: finally block is used in exception handling. finally block is always executed.
  3. finalize():finalize() method is used in garbage collection.finalize() method is invoked just before the object is garbage collected.The finalize() method can be used to perform any cleanup processing.


Question-123:What is the purpose of the Runtime class?
Answer:
The purpose of the Runtime class is to provide access to the Java runtime system.


Question-124: How will you invoke any external process in Java?
Answer:
By Runtime.getRuntime().exec(?) method.

I/O Interview Questions


There is given a list of multi threading interview questions and answers. If you know any multi threading interview question, kindly post it in the comment section.

Question-100: What is multithreading?
Answer:

  • 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.



Question-101: What is thread?
Answer:
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.


Question-102: What is the difference between preemptive scheduling and time slicing?
Answer:
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.


Question-103: What does join() method?
Answer:
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.


Question-104: What is difference between wait() and sleep() method?
Answer:

  1. The wait() method is defined in Object class. The sleep() method is defined in Thread class. 
  2.  wait() method releases the lock. The sleep() method doesn’t releases the lock.



Question-105: Is it possible to start a thread twice?
Answer:
No, there is no possibility to start a thread twice. If we does, it throws an exception.


Question-106: Can we call the run() method instead of start()?
Answer:
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.


Question-107: What about the daemon threads?
 Answer:
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.


Question-108: Can we make the user thread as daemon thread if thread is started?
Answer:
No, if you do so, it will throw IllegalThreadStateException .


Question-109: What is shutdown hook?
Answer:
The shutdown hook is basically a thread i.e. invoked implicitly before JVM shuts down. So we can use it perform clean up resource.


Question-110: When should we interrupt a thread?
Answer:
We should interrupt a thread if we want to break out the sleep or wait state of a thread. 

Core Java : Synchronization Interview Questions

The following interview questions are also the part of multi threading interview questions.


Question-111: What is synchronization?
Answer:
Synchronization is the capabilility of control the access of multiple threads to any shared resource.It is used:
To prevent thread interference. 
To prevent consistency problem.


Question-112: What is the purpose of Synchronized block?
Answer:
Synchronized block is used to lock an object for any shared resource.
Scope of synchronized block is smaller than the method.


Question-113: Can Java object be locked down for exclusive use by a given thread?
Answer:
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.


Question-114: What is static synchronization?
Answer:
If you make any static method as synchronized, the lock will be on the class not on object.


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


Question-116: What is deadlock?
Answer:
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.

Garbage Collection Interview Questions

There is given a list of string handling interview questions with short and pointed answers. If you know any string handling interview question, kindly post it in the comment section.

Question-84:What is the meaning of immutable in terms of String?
Answer:
The simple meaning of immutable is unmodifiable or unchangeable. Once string object has been created, its value can’t be changed.


Question-85: Why string objects are immutable in java?
Answer:
Because java uses the concept of string literal. Suppose there are 5 reference variables,allreferes 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.


Question-86: How many ways we can create the string object?
Answer:
There are two ways to create the string object, by string literal and by new keyword.


Question-87: How many objects will be created in the following code?
String s1=”Welcome”;

String s2=”Welcome”;

String s3=”Welcome”;
Answer:
Only one object


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


Question-89: How many objects will be created in the following code?
String s=new String(“Welcome”);
Answer:
Two objects, one in string constant pool and other in non-pool(heap).


Question-90: What is the basic difference between string and stringbuffer object?
Answer:
String is an immutable object. StringBuffer is a mutable object.


Question-91: What is the difference between StringBuffer and StringBuilder ?
Answer:
StringBuffer is synchronized whereas StringBuilder is not synchronized.


Question-92: How can we create immutable class in java ?
Answer:
We can create immutable class as the String class by defining final class .


Question-93: What is the purpose of toString() method in java ?
Answer:
The to String() 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. Core Java : Nested classes and Interfaces Interview Questions


Question-94: What is nested class?
Answer:
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.


Question-95: Is there any difference between nested classes and inner classes?
Answer:
Yes ofcourse! inner classes are non-static nested classes i.e. inner classes are the part of nested classes.


Question-96: Can we access the non-final local variable, inside the local inner class?
Answer:
No, local variable must be constant if you want to access it in local inner class.


Question-97: What is nested interface ?
Answer:
Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.


Question-98: Can a class have an interface?
Answer:
Yes, it is known as nested interface.


Question-99: Can an Interface have a class?
Answer:
Yes, they are static implicitly.

Core Java : Multi Threading Interview Questions


There is given a list of exception handling interview questions with answers. If you know any exception handling interview question, kindly post it in the comment section.
Question-73: What is Exception Handling?
Answer:
Exception Handling is a mechanism to handle runtime errors.It is mainly used to handle checked exceptions.


Question-74: What is difference between Checked Exception and Unchecked Exception?
Answer:

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.


Question-75: What is the base class for Error and Exception?
Answer:
Throwable.


Question-76: Is it necessary that each try block must be followed by a catch block?
Answer:
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.


Question-77: What is finally block?
Answer:
finally block is a block that is always executed.


Question-78: Can finally block be used without catch?
Answer:
Yes, by try block. finally must be followed by either try or catch.


Question-79: Is there any case when finally will not be executed?
 Answer:
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).


Question-80: What is difference between throw and throws?
Answer:

  1. throw is used to explicitly throw an exception. throws is used to declare an exception.
  2. checked exceptions can not be propagated with throw only. checked exception can be propagated with throws.
  3. throw is followed by an instance. throws is followed by class.
  4. throw is used within the method. throws is used with the method signature. 
  5. You cannot throw multiple exception You can declare multiple exception e.g. public void method()throws IOException,SQLException.



Question-81:Can an exception be rethrown?
Answer:
Yes.


Question-82: Can subclass overriding method declare an exception if parent class method doesn’t throw an exception ?
Answer:
Yes but only unchecked exception not checked.


Question-83: What is exception propagation ?
Answer:
Forwarding the exception object to the invoking method is known as exception propagation.

Question-69: What is package?
Answer:
A package is a group of similar type of classes interfaces and sub packages. It provides access protection and removes naming collision.


Question-70:Do I need to import java.lang package any time? Why ?
Answer:
No. It is by default loaded internally by the JVM.


Question-71: Can I import same package/class twice? Will the JVM load the package twice at runtime?
Answer:
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.


Question-72: What is static import ?
Answer:
By static import, we can access the static members of a class directly, there is no to qualify it with the class name.

Core Java : Exception Handling Interview Questions



Question-56:What is abstraction?
Answer:
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.


Question-57: What is the difference between abstraction and encapsulation?
 Answer:
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.


Question-58: What is abstract class?
Answer:
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.


Question-69: Can there be any abstract method without abstract class?
Answer:
No, if there is any abstract method in a class, that class must be abstract.


Question-60: Can you use abstract and final both with a method?

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


Question-61: Is it possible to instantiate the abstract class?
Answer:
No, abstract class can never be instantiated.


Question-62: What is interface?
Answer:
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.


Question-63: Can you declare an interface method static?
Answer:
No, because methods of an interface is abstract by default, and static and abstract keywords can’t be used together.


Question-64: Can an Interface be final?
Answer:
No, because its implementation is provided by another class.


Question-65: What is marker interface?
Answer:
An interface that have no data member and method is known as a marker interface.For example Serializable,Cloneable etc.


Question-66: What is difference between abstract class and interface?
Answer:


  1. An abstract class can have method body (non-abstract methods). Interface have only abstract methods.
  2. An abstract class can have instance variables. An interface cannot have instance variables. 
  3. An abstract class can have constructor. Interface cannot have constructor.
  4. An abstract class can have static methods. Interface cannot have static methods. 
  5. You can extends one abstract class. You can implement multiple interfaces.



Question-67:Can we define private and protected modifiers for variables in interfaces?
Answer:
No, they are implicitly public.


Question-68: When can an object reference be cast to an interface reference?
Answer:
An object reference can be cast to an interface reference when the object implements the referenced interface.

Core Java – OOPs Concepts : Package Interview Questions


Question-53: What is Runtime Polymorphism?
Answer:


  • 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.


Question-54: Can you achieve Runtime Polymorphism by data members?
Answer:
No.


Question-55: What is the difference between static binding and dynamic binding?
Answer:
In case of static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at runtime.

Core Java – OOPs Concepts : Abstraction Interview Questions

Question-47: What is final variable?
Answer:
If you make any variable as final, you cannot change the value of final variable(It will be constant).


Question-48: What is final method?
Answer:
Final methods can’t be overridden..


Question-49: What is final class?
Answer:
Final class can’t be inherited.


Question-50: What is blank final variable?
Answer:
A final variable, not initalized at the time of declaration, is known as blank final variable.


Question-51: Can we intialize blank final variable?
Answer:
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.


Question-52: Can you declare the main method as final?
Answer:
Yes, such as, public static final void main(String[] args){}.

Monday, May 26, 2014

Question-40: What is method overriding:
Answer:
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.


Question-41: Can we override static method?
Answer:
No, you can’t override the static method because they are the part of class not object.


Question-42: Why we cannot override static method?
Answer:
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.


Question-43: Can we override the overloaded method?
 Answer:
Yes.


Question-44: Difference between method Overloading and Overriding.
Answer:


  1. 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. 
  2. methodoverlaoding is occurs within the class. Method overriding occurs in two classes that have IS-A relationship. 
  3. In this case, parameter must be different. In this case, parameter must be same. 


Question-45:Can you have virtual functions in Java?
Answer:
Yes, all functions in Java are virtual by default.


Question-46: What is covariant return type?
Answer:
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.

Core Java – OOPs Concepts: final keyword Interview Questions

Question-37: What is method overloading?
Answer:
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.


Question-38: Why method overloading is not possible by changing the return type in java?
Answer:
Because of ambiguity


Question-39: Can we overload main() method?
Answer:
Yes, of-course! You can have many main() methods in a class by overloading the main method.

Core Java – OOPs Concepts: Method Overriding Interview Questions




Question-27: What is this in java?
Answer:
It is a keyword that that refers to the current object.


Question-28: What is Inheritance?
Answer:
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.


Question-29: Which class is the superclass for every class.?
Answer:
Object class.


Question-30: Why multiple inheritance is not supported in java?
Answer:
To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.


Quetion-31: What is composition?
Answer:
Holding the reference of the other class within some other class is known as composition.


Question-32: What is difference between aggregation and composition?
Answer:
Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike has an indicator (aggregation) but bike has an engine (compostion).


Question-33: Why Java does not support pointers?
Answer:
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.

Question-34: What is super in java?
Answer:
It is a keyword that refers to the immediate parent class object.


Question-35: Can you use this() and super() both in a constructor?
Answer:
No. Because super() or this() must be the first statement.

Question-36: What is object cloning?
Answer:
The object cloning is used to create the exact copy of an object.

Core Java – OOPs Concepts: Method Overloading Interview Questions


Question-20: What is static variable?
Answer:

  • 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.


Question-21: What is static method?
Answer:

  • 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.


Question-22: why main method is static?
Answer:
because object is not required to call static method if It were non-static method,jvmcreats object first then call main() method that will lead to the problem of extra memory allocation.


Question-23: What is static block?
Answer:

  • Is used to initialize the static data member.
  • It is excuted before main method at the time of classloading.


Question-24: Can we execute a program without main() method?
Answer:
Yes,one of the way is, by static block.


Question-25: What if the static modifier is removed from the signature of the main method?
Answer:
Program compiles. But at runtime throws an error “NoSuchMethodError”.


Question-26: What is difference between static (class) method and instance method?
Answer:

  1. A method i.e. declared as static is known as static method. A method i.e. not declared as static is known as instance method.
  2. Object is not required to call static method. Object is required to call instance methods. 3)Non-static (instance) members cannot be accessed in static context (static method, static block and static nested class) directly. static and non-static variables both can be accessed in instance methods. 4)For example: public static int cube(int n){ return n*n*n;} For example: public void msg(){…}.

Core Java – OOPs Concepts: Inheritance Interview Questions

Question-15: What is constructor?
Answer:
Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.


Question-16: What is the purpose of default constructor?
Answer:
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.


Questiom-17: Does constructor return any value?
Answer:
yes, that is current instance (You cannot use return type yet it returns a value).

Question-18: Is constructor inherited?
Answer:
No, constructor is not inherited.

Question-19: Can you make a constructor final?
Answer:
No, constructor can’t be final.

Core Java – OOPs Concepts: static keyword Interview Questions


There is given more than 50 OOPs (Object-Oriented Programming and System) interview questions. But they have been categorized in many sections such as constructor interview questions, static interview questions, Inheritance Interview questions, Abstraction interview question, Polymorphism interview questions etc. for better understanding.


Question-13: What is difference between object oriented programming language and object based programming language?
Answer:
Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.


Question-14: What will be the initial value of an object reference which is defined as an instance variable?
Answer:
The object references are all initialized to null in Java.

Core Java – OOPs Concepts: Constructor Interview Questions

Question-1:  What is difference between JDK,JRE and JVM?
Answer:

JVM:

  • JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java bytecode can be executed.
  • JVMs are available for many hardware and software platforms (so JVM is platform dependent).

JRE:

JRE stands for Java Runtime Environment. It is the implementation of JVM and physically exists.

JDK:

JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.


Question-2:  How many types of memory areas are allocated by JVM?
Answer:



  1. Class(Method) Area
  2. Heap
  3. Stack
  4. Program Counter Register
  5. Native Method Stack


Question:3- What is JIT compiler?
Answer:
Just-In-Time(JIT) compiler:It is used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.


Question-4: What is platform?
Answer:
A platform is basically the hardware or software environment in which a program runs. There are two types of platforms software-based and hardware-based. Java provides software-based platform.


Question-5: What is the main difference between Java platform and other platforms?
Answer
The Java platform differs from most other platforms in the sense that it’s a software-based platform that runs on top of other hardware-based platforms.It has two components:

Runtime Environment

API(Application Programming Interface)


Question-6: What gives Java its ‘write once and run anywhere’ nature?
Answer:
The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platorm specific and hence can be fed to any platform.


Question-7: What is classloader?

Answer:
The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.


Question-8: Is Empty .java file name a valid source file name?
Answer:
Yes, save your java file by .java only, compile it by javac .java and run by java yourclassname Let’s take a simple example:

//save by .java only

class A{

public static void main(String args[]){ System.out.println(“Hello java”);

}

}

//compile by javac .java //run by java A compile it by javac .java run it by java A


Question-9: Is delete,next,main,exit or null keyword in java?
Answer:
No.


Question-10: If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?
Answer:
It is empty. But not null.


Question-11: What if I write static public void instead of public static void?
Answer:
Program compiles and runs properly.


Question-12: What is the default value of the local variables?
Answer: 
The local variables are not initialized to any default value, neither primitives nor object references.

 Core Java – OOPs Concepts: Initial OOPs Interview Questions

Copyright @ CrackMNC 2014-2024
Divas Nikhra Theme by Crack MNC