CORE JAVA QUESTIONS AND ANSWERS PDF

adminComment(0)

CORE JAVA INTERVIEW. QUESTIONS AND ANSWERS. Table of Contents. 1) what are static blocks and static initalizers in Java?. Top Java Interview Questions with Answers .. Ans: The basic condition of method overriding is that method name, arguments as well as. Basic Java Interview Questions and Answers for Freshers or Experienced Pdf. This interview section questions contains a brief introduction to the Java.


Core Java Questions And Answers Pdf

Author:CARMELA ALFREDO
Language:English, French, Portuguese
Country:Peru
Genre:Personal Growth
Pages:423
Published (Last):17.04.2016
ISBN:542-7-23222-429-6
ePub File Size:24.45 MB
PDF File Size:14.73 MB
Distribution:Free* [*Registration needed]
Downloads:46749
Uploaded by: MITTIE

Interview Questions in Core Java. mencosulwiemudd.gq is a transient How does Java handle integer overflows and underflows? Following answer may not be correct. Dear readers, these Java Interview Questions have been designed especially during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer −. java interview questions and answers PDF, java interview questions PDF eBook Free download, core java interview questions pdf, + questions, 60+ pages.

Use of interfaces also puts an extra burden on the developers as any time an interface is implemented in a class; developer is forced to implement each and every method of interface. Does Importing a package imports its sub-packages as well in Java? Ans: In java, when a package is imported, its sub-packages aren't imported and developer needs to import them separately if required.

For example, if a developer imports a package university. To load the classes from its sub-package say department , developer has to import it explicitly as follows: Import university.

Can we declare the main method of our class as private? Ans: In java, main method must be public static in order to run any application correctly. If main method is declared as private, developer won't get any compilation error however, it will not get executed and will give a runtime error.

How can we pass argument to a function by reference instead of pass by value? Ans: In java, we can pass argument to a function only by value and not by reference. How an object is serialized in java?

Ans: In java, to convert an object into byte stream by serialization, an interface with the name Serializable is implemented by the class. All objects of a class implementing serializable interface get serialized and their state is saved in byte stream. When we should use serialization? Ans: Serialization is used when data needs to be transmitted over the network.

Using serialization, object's state is saved and converted into byte stream. The byte stream is transferred over the network and the object is re-created at destination. Ans: Try block needs to be followed by either Catch block or Finally block or both.

Any exception thrown from try block needs to be either caught in the catch block or else any specific tasks to be performed before code abortion are put in the Finally block. Is there any way to skip Finally block of exception even if some exception occurs in the exception block?

Ans: If an exception is raised in Try block, control passes to catch block if it exists otherwise to finally block. Finally block is always executed when an exception occurs and the only way to avoid execution of any statements in Finally block is by aborting the code forcibly by writing following line of code at the end of try block: System. When the constructor of a class is invoked? Ans: The constructor of a class is invoked every time an object is created with new keyword.

For example, in the following class two objects are created using new keyword and hence, constructor is invoked two times. Can a class have multiple constructors? Ans: Yes, a class can have multiple constructors with different parameters. Which constructor gets used for object creation depends on the arguments passed while creating the objects.

Can we override static methods of a class? Ans: We cannot override static methods. Static methods belong to a class and not to individual objects and are resolved at the time of compilation not at runtime.

Even if we try to override static method,we will not get an complitaion error,nor the impact of overriding when running the code.

In the below example, what will be the output? Is String a data type in java? Ans: String is not a primitive data type in java. When a string is created in java, it's actually an object of Java. String class that gets created.

After creation of this string object, all built-in methods of String class can be used on the string object. In the below example, how many String Objects are created?

String class are created. Why Strings in Java are called as Immutable?

300 Core Java Interview Questions | Set 1

Ans: In java, string objects are called immutable as once value has been assigned to a string, it can't be changed and if changed, a new object is created. In below example, reference str refers to a string object having value "Value one".

What's the difference between an array and Vector? Ans: An array groups data of same primitive type and is static in nature while vectors are dynamic in nature and can hold data of different data types. What is multi-threading? Ans: Multi threading is a programming concept to run multiple tasks in a concurrent manner within a single program. Threads share same process stack and running in parallel. It helps in performance improvement of any program. Why Runnable Interface is used in Java? Ans: Runnable interface is used in java for implementing multi threaded applications.

Runnable interface is implemented by a class to support multi threading. What are the two ways of implementing multi-threading in Java? Ans: Multi threaded applications can be developed in Java by using any of the following two methodologies: 1. By using Java. Runnable Interface.

Classes implement this interface to enable multi threading. There is a Run method in this interface which is implemented. By writing a class that extend Java. Thread class. When a lot of changes are required in data, which one should be a preference to be used?

String or StringBuffer? Ans: Since StringBuffers are dynamic in nature and we can change the values of StringBuffer objects unlike String which is immutable, it's always a good choice to use StringBuffer when data is being changed too much. If we use String in such a case, for every data change a new String object will be created which will be an extra overhead.

What's the purpose of using Break in each case of Switch Statement? Ans: Break is used after each case except the last one in a switch so that code breaks after the valid case and doesn't flow in the proceeding cases too. If break isn't used after each case, all cases after the valid case also get executed resulting in wrong results.

How garbage collection is done in Java? Ans: In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically. For automatic garbage collection java calls either System.

Why Core Java Interview Questions?

How we can execute any code even before main method? Ans: If we want to execute any statements before even creation of objects at load time of class, we can use a static block of code in the class. Any statements inside this static block of code will get executed once at the time of loading the class even before creation of objects in the main method.

Can a class be a super class and a sub-class at the same time? Ans: If there is a hierarchy of inheritance used, a class can be a super class for another class and a sub-class for another one at the same time. In the example below, continent class is sub-class of world class and it's super class of country class. How objects of a class are created if no constructor is defined in the class? Ans: Even if no explicit constructor is defined in a java class, objects get created successfully as a default constructor is implicitly used for object creation.

This constructor has no parameters. In multi-threading how can we ensure that a resource isn't used by multiple threads simultaneously? Ans: In multi-threading, access to the resources which are shared among multiple threads can be controlled by using the concept of synchronization.

Using synchronized keyword, we can ensure that only one thread can use shared resource at a time and others can get control of the resource only once it has become free from the other one using it.

Can we call the constructor of a class more than once for an object? Ans: Constructor is called automatically when we create an object using new keyword. It's called only once for an object at the time of object creation and hence, we can't invoke the constructor again for an object after its creation.

There are two classes named classA and classB. Both classes are in the same package. Can a private member of classA can be accessed by an object of classB? Ans: Private members of a class aren't accessible outside the scope of that class and any other class even in the same package can't access them.

Can we have two methods in a class with the same name? Which method is to get invoked will depend upon the parameters passed.

For example in the class below we have two print methods with same name but different parameters. How can we make copy of a java object?

Core Java Interview Questions and Answers

The static variables are stored in the class area, and we do not need to create the object to access such variables. Therefore, static is used in the case, where we need to define variables or methods which are common to all the objects of the class.

For example, In the class simulating the collection of the students in a college, the name of the college is the common attribute to all the students. Therefore, the college name will be defined as static. There are various advantages of defining packages in Java. Packages avoid the name clashes. The Package provides easier access control. We can also have the hidden classes that are not visible outside and used by the package.

It is easier to locate the related classes. Now, their sum 30 is treated as the string and concatenated with the string Javatpoint. Therefore, the output will be 30Javatpoint.

In the second case, the string Javatpoint is concatenated with 10 to be the string Javatpoint10 which will then be concatenated with 20 to be Javatpoint In the second case, The numbers 10 and 20 will be multiplied first to be because the precedence of the multiplication is higher than addition.

The result will be treated as the string and concatenated with the string Javatpointto produce the output as Javatpoint However, 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.

It is a programming paradigm based on objects having data and methods defined in the class to which it belongs. Object-oriented paradigm aims to incorporate the advantages of modularity and reusability.

Objects are the instances of classes which interacts with one another to design applications and programs. There are the following features of the object-oriented paradigm. Follows the bottom-up approach in program design. Focus on data with methods to operate upon the object's data Includes the concept like Encapsulation and abstraction which hides the complexities from the user and show only functionality.

Top 100 Java Interview Questions with Answers

Implements the real-time approach like inheritance, abstraction, etc. The Object is the real-time entity having some state and behavior. In Java, Object is an instance of the class having the instance variables as the state of the object and the methods as the behavior of the object. The object of a class can be created by using the new keyword.

There are the following basic differences between the object-oriented language and object-based language. Object-oriented languages follow all the concepts of OOPs whereas, the object-based language doesn't follow all the concepts of OOPs like inheritance and polymorphism.

Object-oriented languages do not have the inbuilt objects whereas Object-based languages have the inbuilt objects, for example, JavaScript has window object. Examples of object-oriented programming are Java, C , Smalltalk, etc. All object references are initialized to null in Java. The constructor can be defined as the special type of method that is used to initialize the state of an object.

It is invoked when the class is instantiated, and the memory is allocated for the object. Every time, an object is created using the new keyword, the default constructor of the class is called.

The name of the constructor must be similar to the class name. The constructor must not have an explicit return type. Based on the parameters passed in the constructors, there are two types of constructors in Java.

Default Constructor: default constructor is the one which does not accept any value. The default constructor is mainly used to initialize the instance variable with the default values. It can also be used for performing some useful task on object creation. A default constructor is invoked implicitly by the compiler if there is no constructor defined in the class. Parameterized Constructor: The parameterized constructor is the one which can initialize the instance variables with the given values.

In other words, we can say that the constructors which can accept the arguments are called parameterized constructors. The purpose of the default constructor is to assign the default value to the objects. The java compiler creates a default constructor implicitly if there is no constructor in the class.

Here 0 and null values are provided by default constructor. Ans: yes, The constructor implicitly returns the current instance of the class You can't use an explicit return type with the constructor. No, The constructor is not inherited. No, the constructor can't be final.Using Math.

What is difference between path and classpath variables? I have multiple constructors defined in a class. There exists composition between class and students. The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Yes, we can have try-finally statement and hence avoiding catch block.

What is the difference between an Inner Class and a Sub-Class?