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

Frequently Asked Interview Questions: Java Basics

Best Frequently asked questions related to java For Java Related training can visit www.itedge.in
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Frequently Asked Interview Questions: Java Basics

Best Frequently asked questions related to java For Java Related training can visit www.itedge.in
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Frequently Asked Interview Questions: Java Basics

These 20 most frequently asked Java Interview Questions will help


understand the basic concepts and fundamentals recruiters seek among
aspiring Java Developers during placement interviews. These tricky
Java programming interview programming questions for fresher‟s will
help you during objective assessments and interview sessions.

1. What is JAVA?

Java is a fast, secure and reliable programming language used to


develop applications and framework on a wide range of platforms. It was
developed by Sun Microsystems in 1995. It bears similarities with C++
but offers more advanced and simplified features

2. What are the features in JAVA?

The main features of Java are:

 It is object-oriented and allows Inheritance, Encapsulation,


Polymorphism, and Abstraction.

 Java is platform independent which means all Java programs can


run on multiple platforms without any changes in the program.

 Java has Just In Time (JIT) compiler allowing high performance

 Java is multi-threaded which allows users to create multiple


threads using extension of threads or implementation of Runnable
interface.

3. What is Polymorphism?

Polymorphism is the ability of an object to call an overridden method


through the reference variable of a superclass. In other words,
polymorphism occurs when a parent class reference is used to refer to a
child class object in a program.

4. Is Java 100% Object-oriented?

Java is not fully Object-oriented as it makes use of non-object primitive


data types which includes boolean, int, char, float etc.

5. What is final class in Java?

We can declare a class‟s method as final by using the keyword final.


This indicates that the method in that particular class cannot be
overridden or inherited by subclasses. This is really useful in creation of
immutable classes, for instance String class. To add to that, we can
even declare the entire class as final.

6. What if the main() method is declared as private?

If we declare main() as private, it will not be accessible by other JVM


classes and the program will give a runtime error – “main method not
public”.

7. Describe synchronization with respect to multithreading?

Synchronization, with respect to multithreading, gives us control over


multiple threads accessing any shared resource. We use
synchronization as a mechanism to make sure that only a single thread
gets access to resources at a time, thus reducing errors.

We can do this by:

 Declaring synchronized keyword on a method signature

 Writing a synchronized block

8. Using one line of code, how can it be proved that the array is not
null but empty?

Print args.length

The code “Print args.length” will print 0, which proves it is empty and not
null. Because in case it is null there will be a NullPointerException with
the expression “print args.length”

9. What happens if you do not initialize an instance variable of any


of the primitive types in Java?

If we don‟t initialize instance variables, the system initializes them to


their default values. For instance:

byte, short, int : 0


long : 0L
float : 0.0f
double : 0.0d
char : „\u0000‟
String (or any object) : null
boolean : false

10. What is overriding? How does that differ from overloading?

Overriding in Java allows a subclass to provide the implementation of a


method already provided by its super or parent class. So if a subclass
has its name, parameters or return type similar to its parent class, then
the method in the subclass is overriding the method in the parent class.

Overloading takes place when two or more methods in one class are
given the same method name but contains different parameters.

Here is a method overloading vs overriding table:

Sl. No. Method Overriding Method Overloading

1 It used to provide the Overloading increases


implementation of the method readability of programs.
already provided by its super
class.

2 Takes place when any two It takes place within a class


classes in the program are
going through inheritance.

3 Classes contain the same Classes have different parameters


parameters

4 Same return type Return type can be the same or


different

5 Simulates a runtime Simulates a compile time


polymorphism polymorphism

11. How many objects are created in the following piece of code?

MyClass c1, c2, c3;


c1 = new MyClass ();
c3 = new MyClass ();

We need to initialize the declared references to create objects. In this


example, we have only c1 and c3 as valid classes and since c2 is not
initialized it does not create an object.

12. What are wrapper classes and why are wrapper classes
required?

Java Wrapper class help us to use Java primitive data types as objects.
So, using a Java wrapper allows primitive data type function like an
object. It is required in case want an object reference to a primitive
value, where their basic form as int, char, long, etc. is not sufficient.
13. Does garbage collection guarantee that a program will not run
out of memory?

No, garbage collection does not guarantee that a program will not run
out of memory. It just runs asynchronously when the system is idle and
when a separate thread is activated the garbage collector shifts to a
consistent state before it terminates.

14. Write a program having a single loop that removes duplicates


from an array.

public class DuplicateInArray

public static void main(String[] args)

int[] array = {1,1,2,3,4,5,6,7,8,8};

Set<Integer> set = new HashSet<Integer>();

for(int i = 0; i < array.length ; i++)

if(set.add(array[i]) == false)

System.out.println("Duplicate element found : " + array[i]);

15. Explain the concepts of Unboxing and Autoboxing.

When Java compiler makes automatic conversion between the primitive


types and their corresponding object wrapper classes it is termed
as Autoboxing. This helps in an easy compilation. For instance,
converting char to Character, a short to a Short, and so on.

Unboxing is the conversion of object wrapper types into their primitive


equivalent types. It is the reverse of Autoboxing. Both autoboxing and
unboxing help in writing more readable codes.

List of primitive types and their corresponding wrapper classes used by


the Java compiler.

Primitive Type Wrapper Class

boolean Boolean

byte Byte

char Character

float Float

int Integer

long Long

short Short

double Double

16. Can we have an application that has multiple classes


having main() method?

Yes, we can have multiple class having main method in a single


program. But the String[] argument will be called as the main() method
by the JVM ignoring all other main methods. This is called overloading.
We need to call the overloaded methods explicitly.

17. Can I import the same package/class twice? Will the JVM load
the package twice at runtime?

Yes, we can import same package/class twice, but class is created only
once i.e. memory allocation happens only once. This does not affect the
compilation of the program and it runs fine.

The JVM loads the package only once.

18. How can one have control over the serialization process?

The various ways we can have control over serialization are:


 Serialization is more about having serialized data members, so the
application of transient modifier on data members will help us
serialize them.

 Serialization can also be controlled by having readObject and


writeObject() in the class that implements the java.io.Serialization
interface.

 io.ObjectOutputStream and java.io.ObjectInputStream


provides defaultWriteObject() and defaultReadObject() methods
respectively for object serialization. These need to be called from
inside the methods for serialization.

19. What is the difference between an error and an exception?

Exceptions are non-fatal and recoverable errors in programs thrown by


Common Language Runtime. These are a type of object coming from
the System.Exception class. Whereas, errors are instances in a program
that can‟t be handled and usually marks the end of a program. All errors
are exceptions while the reverse is not true.

Exceptions Errors

Type: java.lang.Exception. Type: java.lang.Error.

Includes checked and unchecked Includes only


exceptions. unchecked exceptions.

Recovered by exceptions handling Errors can‟t be recovered


techniques like try-catch blocks

Caused within applications Usually caused by environment


around the application

20. Differentiate between a while statement and a do-while


statement?

While statement: It verifies conditions whether the next loop iteration


should occur or not before entering into the loop.

Do-while statement: It does not check for conditions before executing


iterations. These statements execute the loop body at least once.

You might also like