Java Interview Questions

Q.01. What is the difference between a function and method?.

Q.02 How to allocate memory in java programming?.

Q.03 Which algorithm is used by garbage collector?

Q.04 How do you call garbage collector?

Q.05 What is Unicode System?

Q.06 What are control statements?

Q.07 Which loop is more efficient "do…while" or "while" loop?

Q.08 Can you Call the main() method of a class from another class?

Q.09 What is object reference?

Q.10 Explain about 'String Constant Pool'?

Q.11 What is the difference between StringBuffer and StringBuilder classes?

Q.12 What is the difference between Class and Object?

Q.13 What is Hash Code Number in Java?

Q.14 What is the difference between default constructor and parameterized constructor?

Q.15 What is the difference between Constructor and Methods?

Q.16 What is Instance Method?

Q.17 What is the difference between instance variable and class variables (static variables)?

Q.18 What is Static Block?

Q.19 What is Inheritance and what are the advantages of using it?

Q.20 What is the reason that Multiple Inheritance is not accepted in Java?

Q.21 What is Coercion in Java?

Q.22 Can Private Method be overriden in Java?

Q. 23 Difference between Privitive Data Type & Reference Data Type?

Q.24 What is Generalization and Specialization in Java?

Q.25 What is the Object Class in Java?

Q.26 What is an Abstract Class?

Q.27 What is an Interface in Java?

Q.28 How do you call 'garbage collector' in Java?

Q.29 What is JAR Files?

Q.30 What is Throwable?

Q.31 What is the difference between a 'throw' and 'throws'?


Q.01. What is the difference between a function and method?.
Answer: A method is a function. Java do not have functions instead Java uses method. Function is written inside out of the class in C++ but in Java it should be written only inside the class.

Q.02 How to allocate memory in java programming?.
Answer: Class loader subsystem of JVM will allocate necessary memory. JVM also deallocates memory when it is not used.

Q.03 Which algorithm is used by garbage collector?
Answer: The most commonly used is mark and sweep.

Q.04 How do you call garbage collector?
Answer: Garbage collector is called by using "gc ()"

Q.05 What is Unicode System?
Answer: Unicode System is an encoding standard that gives an unique number for every character no matter what the program, platform and language is. Unicode uses 2 bytes to represent a single character.

Q.06 What are control statements?
Answer: Control statements are executed randomly and repeatedly hence providing better control to the programmer on the flow of execution.

Q.07 Which loop is more efficient "do…while" or "while" loop?
Answer: In while loop condition is tested first and statement is executed second but in contrary, in do…while loop, statements are executed first and condition is tested later. Thus programmers don't have full control from the starting. Hence while loop is better efficient.

Q.08 Can you Call the main() method of a class from another class?
Answer: Certainly yes. We can call the main() method of a class from another class using Classname.main(). At the time of calling the main() method, we should pass a string type array to it.

Q.09 What is object reference?
Answer: Object reference is a unique hexadecimal number representing the memory address of the object.

Q.10 Explain about 'String Constant Pool'?
Answer: It is a separate block of memory where string objects are stored by JVM. If string object is created directly (String n1="David";), using assignment operator, it is stored in String Constant Pool.

Q.11 What is the difference between StringBuffer and StringBuilder classes?
Answer: StringBuffer class is synchronized and StringBuilder is NOT. When the programmer wants to use several threads, he should use StringBuffer as it gives reliable outputs. If only one thread is used, StringBuilder is preferred, it can improve execution time.

Q.12 What is the difference between Class and Object?
Answer: A class is a model or blue print for creating objects and does not exists physically. An object is any thing that exists physically and both the class and objects contain variables and methods.

Q.13 What is Hash Code Number in Java?
Answer: Hash Code number is a unique number generated for an object in JVM. Hash Code number is also referred to as 'Reference number' which is created based on the location of the object in memory. And it is unique for all the objects apart from String objects.

Q.14 What is the difference between default constructor and parameterized constructor?
Answer: Default constructor is useful to initialize all objects with same data and does not have any parameters declared and when the data is not passed while creating an object, default constructor is called.
Parameterized constructor is used to initialize each object with different data and constructor will have one or more parameters and when data is passed while creating an object, parameterized constructor is called.

Q.15 What is the difference between Constructor and Methods?
Answer: A constructor is used to initialize the instance variable of a class. Constructor must have same name as class name. Constructor is called and executed concurrently at the time of object creation.
A method is used to perform an action or processing data. Method name and constructor's name should be different as method's name begins with lowercase while constructor's name begins with the uppercase. A method is called after creating an object and can be called several times on the object. And a method is executed only when we call it.

Q.16 What is Instance Method?
Answer: Instance method acts on the instance variables of the class. If an object is used to call method such as obj.sum(), obj has direct connection with the class and its instance variables.

Q.17 What is the difference between instance variable and class variables (static variables)?
Answer: Each object can have separate copy of instance variables but each object can not have separate copy of class variables. Single copy is shared amongst the objects. Instance variable are stored in Heap memory while class variables are stored within method.

Q.18 What is Static Block?
Answer: Static Block is declared with the java keyword 'static'. JVM executes static block first and then others. Here is the static block look like:

 public class TestStaticBlock
 {
  Static {
   System.out.println("I am Static Block");
   }
 public static void main(String arg[]) {
  System.out.println("I am main method");
 }

Here is the OUTPUT:
I am Static Block:
I am main method:

Q.19 What is Inheritance and what are the advantages of using it?
Answer: Members of super class is inherited to derived class is called Inheritance. Programmers can reuse super class code without rewriting it for sub class. It helps programmers do work quicker and easy hence saves time and increases productivity.

Q.20 What is the reason that Multiple Inheritance is not accepted in Java?
Answer: Multiple Inheritance leads a confusion, and Single Inheritance can provide Multiple Inheritance job by using single inheritance multiple time. Interface also provides the same job as Multiple Inheritance in Java.

Q.21 What is Coercion in Java?
Answer: Coercion is a way of coercing up two piece of data into one identical type. It's an automatic conversion between two or more data types done by the compiler. Here is an example.

Cal  Data Type  Results
X=2.0/3  D/I (Double/Int) D

Q.22 Can Private Method be overriden in Java?
Answer: Private Methods can not be overriden because they can not be used in sub classes. methods are not accessible outside the class. It is not available for sub class too. Private method can be used in Method Overloading.

Q. 23 Difference between Privitive Data Type & Reference Data Type?
Answer: Primitive data type hold single value whereas Reference data type, also called Advance data type, can hold two or more values. Primitive data type can be converted into another primitive data type by using casting. But Primitive data type cannot be converted into referenced data type by using casting. We need to use class wrapper for this function.

Q.24 What is Generalization and Specialization in Java?
Answer: Generalization needs upward casting. Generalization is a concept where a sub class is promoted to a super class. Specialization follows downward casting and It is quite the opposite of generalization as it demote/downgraded to a sub classes.

Q.25 What is the Object Class in Java?
Answer: We can find Object class in java.lang package in java which refers to the super class of all the classes available in java. So, each class has impact of that object class. Object class outlines the methods for comparing objects, and converting objects to a string etc.

Q.26 What is an Abstract Class?
Answer: An Abstract class is a class that contains abstract methods as well as concrete methods and instance variables. An abstract class is declared with 'abstract' java keyword. So is with abstract methods. Abstract methods can only have method header but NOT the body. Sub classes will have method body and will call the abstract methods. Objects cannot be created for abstract class. Only sub classes can have objects. But a reference can be created from abstract class which is used to refer to objects of its subclasses.

Q.27 What is an Interface in Java?
Answer: An Interface is a specification of method prototypes and these methods are public and abstract by default. They are public due to the fact that they needed to be available publicly to the third vendor parties to provide implementations. This is why they are abstract as their implementation is left for the third parties. We can not create objects from Interface so we needed implementation classes to implement methods of the interfaces. Java keyword 'Implement' is used to use interface from implementation classes

Q.28 How do you call 'garbage collector' in Java?
Answer: Garbage Collector is one of the best tool in java used to delete unused and unnecessary variables and unreferenced objects from memory. We can use gc() method to perform this task. For example:
System.gc();

Q.29 What is JAR Files?
Answer: A JAR (java archive) file contains compressed version of .class files, directories, images. '.jar' file is compressed in .zip or tar file. Number of .class files and other related files are grouped and put together in the .jar file. We can create a jar file by using the following command.

=>To create a JAR file:
C:\>jar cf jarfilename inputfiles
C:\>jar cf pack.nettools.jar pack.nettools
=>To view a JAR file:
jar tf jarfilename
i.e. jar tf pack.nettools.jar
=>To extract a JAR file:
jar xf pack.nettools.jar

Q.30 What is Throwable?
Answer: Throwable is a class in java that comes under java.lang package. Throwable class is a super class which handles all the exception errors. Under Throwable, we have two sub classes: Error class and Exception class. Error class handles all the errors whereas Exception class handles Runtime and other errors.

Q.31 What is the difference between a 'throw' and 'throws'?
Answer: Actually they both are quite oppositely behaves as throw clause is used to throw an exception to handle the exception whereas 'throws' clause is used when programmers don't want to handle the exception and throw it out of the method.

No comments:

OTN Discussion Forums: Message List - Database - General

Ask Tom HOT ARTICLES

 
© Copyright 2008 - 2011. All rights reserved to dba-sansar.blogspot.com.
All other blogs, posts and entries are the property of their respective authors.
Please email us for your comments and suggessions Click Here