Professional Documents
Culture Documents
Table of Contents
1 Overview ................................................................................................................. 1
2 Operators .............................................................................................................. 14
3 Arrays ................................................................................................................... 15
4 Strings .................................................................................................................. 16
5 Encapsulation ........................................................................................................ 16
6 Inheritance ............................................................................................................ 17
7 Polymorphism ........................................................................................................ 17
8 Classes and Objects ............................................................................................... 18
9 Methods ................................................................................................................ 20
10 Abstract Classes .................................................................................................. 24
11 Interfaces ........................................................................................................... 24
12 Packages ............................................................................................................ 27
13 Wrapper Classes ................................................................................................. 27
14 Threads .............................................................................................................. 28
15 Collections .......................................................................................................... 30
16 Exception Handling ............................................................................................. 31
17 I/O Streams ........................................................................................................ 33
1 Overview
1. What is meant by Object Oriented Programming?
OOP is a method of programming in which programs are organized as cooperative collections of
objects. Each object is an instance of a class and each class belongs to a hierarchy.
7. What is a JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
8. Is JVM's platform-independent?
JVM's are not platform-independent. JVM's are platform specific run time implementation provided
by the vendor.
17. What are the good programming practices for better memory
management?
We shouldn't declare unwanted variables and objects.
We should avoid declaring variables or instantiating objects inside loops.
When an object is not required, its reference should be nullified.
We should minimize the usage of String objects and SOP.
2/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
Static binding is a binding in which the class association is made during compile time. This is also
called as Early binding.
24. What is the purpose of garbage collection in Java, and when is it used?
The purpose of garbage collection is to identify and discard objects that are no longer needed by
a program so that their resources can be reclaimed and reused. A Java object is subject to garbage
collection when it becomes unreachable to the program in which it is used.
26. Does garbage collection guarantee that a program will not run out of
memory?
Garbage collection does not guarantee that a program will not run out of memory. Programs can
use up memory resources faster than they are garbage collected. It is also possible for programs
to create objects that are not subject to garbage collection
31. What is the difference between a field variable and a local variable?
A field variable is a variable that is declared as a member of a class. A local variable is a variable
that is declared local to a method.
4/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
The default constructor is a no-argument constructor that initializes the instance variables to their
default values. This will be provided by the compiler at the time of compilation. The default
constructor will be provided only when you don’t have any constructor defined.
35. When does the compiler supply a default constructor for a class?
The compiler supplies a default constructor for a class if no other constructors are provided.
45. State the significance of public, private, protected, default modifiers both
singly and in combination and state the effect of package relationships
on declared items qualified by these modifiers.
5/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
public: Public class is visible in other packages, the field is visible everywhere (class
must be public too)
private: Private variables or methods may be used only by an instance of the same
class that declares the variable or method, a private feature may only be accessed by
the class that owns the feature.
protected: Is available to all classes in the same package and also available to all
subclasses of the class that owns the protected feature. This access is provided even
to subclasses that reside in a different package from the class that owns the protected
feature.
default: What you get by default i.e., without any access modifier (i.e., public private
or protected). It means that it is visible to all within a particular package.
47. What modifiers may be used with an inner class that is a member of an
outer class?
A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.
49. Want to print "Hello" even before the main is executed. How will you
achieve that?
Print the statement inside a static block of code. Static blocks get executed when the class gets
loaded into the memory and even before the creation of an object. Hence it will be executed
before the main method.
50. What if I write static public void instead of the public static void?
The program compiles and runs properly.
6/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
Yes, for statement can loop indefinitely. For example, consider the following: for(;;);
55. What restrictions are placed on the values of each case of a switch
statement?
During compilation, the values of each case of a switch statement must evaluate to a value that
can be promoted to an int value.
60. What happens if you don’t initialize an instance variable of any of the
primitive types in Java?
Java by default initializes it to the default value for that primitive type. Thus an int will be initialized
to 0, a boolean will be initialized to false.
61. What will be the initial value of an object reference which is defined as
an instance variable?
The object references are all initialized to null in Java. However, to do anything useful with these
references, you must set them to a valid object, else you will get NullPointerExceptions everywhere
you try to use such default initialized references.
7/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
Static: - These are the class level variables. They are initialized when the class is
loaded in JVM for the first time and remain there as long as the class remains loaded.
They are not tied to any particular object instance.
65. If I do not provide any arguments on the command line, then the String
array of the main method will be empty or null?
It is empty. But not null.
73. What is the first argument of the String array in the main method?
The String array is empty. It does not have any element. This is unlike C/C++ where
the first element by default is the program name.
9/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
78. What happens to the static fields of a class during serialization? Are
these fields serialized as a part of each serialized object?
Yes, the static fields do get serialized. If the static field is an object, then it must have implemented
a Serializable interface. The static fields are serialized as a part of every object. But the
commonness of the static fields across all the instances is maintained even after serialization.
80. Does Java provide any construct to find out the size of an object?
No there is no sizeof operator in Java. So there is not a direct way to determine the size of an
object directly in Java.
83. Are there any global variables in Java, which can be accessed by another
part of your program?
No, it is not the main method in which you define variables. Global variables are not possible
because the concept of encapsulation is eliminated here.
85. What does the "abstract" keyword mean in front of a method? A class?
Abstract keyword declares either a method or a class. If a method has an abstract keyword in
front of it, it is called an abstract method. An abstract method has no body. It has only arguments
and return type. Abstract methods act as placeholder methods that are implemented in the
subclasses. Abstract classes can't be instantiated. If a class is declared as abstract, no objects of
that class can be created. If a class contains an abstract method, it must be declared as abstract.
89. Can you give a few examples of final classes defined in Java API?
java.lang.String, java.lang.Math is the final classes.
90. Which non-Unicode letter characters may be used as the first character
of an identifier?
The non-Unicode letter characters $ and _ may appear as the first character of an identifier
95. How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-
8 characters?
Unicode requires 16 bits and ASCII requires 7 bits Although the ASCII character set uses only 7
bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18-bit
patterns. UTF-16 uses 16-bit and larger bit patterns.
96. What is the order of precedence and associativity, and how are they
used?
Order of precedence determines the order in which operators are evaluated in expressions.
Associativity determines whether an expression is evaluated left-to-right or right-to-left.
104. If a class is declared without any access modifiers, where may the class
be accessed?
A class that is declared without any access modifiers is said to have package access. This means
that the class can only be accessed by other classes and interfaces that are defined within the
same package.
2 Operators
119. What is the % operator?
It is referred to as the modulo or remainder operator. It returns the remainder of dividing the first
operand by the second operand.
121. What is the difference between the prefix and postfix forms of the ++
operator?
14/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
The prefix form performs the increment operation and returns the value of the increment
operation. The postfix form returns the current value all of the expression and then performs the
increment operation on that value.
122. What is the difference between the Boolean & operator and the &&
operator?
If an expression involving the Boolean & operator is evaluated, both operands are evaluated.
Then the & operator is applied to the operand. When an expression involving the && operator is
evaluated, the first operand is evaluated. If the first operand returns a value of true, then the
second operand is evaluated. The && operator is then applied to the first and second operands.
If the first operand evaluates to false, the evaluation of the second operand is skipped.
123. What is the difference between the >> and >>> operators?
The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been
shifted out.
3 Arrays
127. What will be the default values of all the elements of an array defined as
an instance variable?
If the array is an array of primitive types, then all the elements of the array will be initialized to
the default value corresponding to that primitive type. e.g. All the elements of an array of int will
be initialized to 0, while that of boolean type will be initialized to false. Whereas if the array is
an array of references (of any type), all the elements will be initialized to null.
128. How can one prove that the array is not null but empty?
Print args.length. It will print 0. That means it is empty. But if it would have been null
15/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
4 Strings
130. What is the importance of == and equals() method concerning String
object?
”== “is used to check whether the references are of the same object. equals() is used to check
whether the contents of the objects are the same. But for strings, object reference with the same
content will refer to the same object.
String str1="Hello";
String str2="Hello";
(str1==str2) and str1.equals(str2) both will be true.
If you take the same example with Stringbuffer, the results would be different.
Stringbuffer str1="Hello";
Stringbuffer str2="Hello";
str1.equals(str2)will be true.
str1==str2 will be false.
5 Encapsulation
136. What is meant by Encapsulation?
Encapsulation is the process of compartmentalizing the elements of an abstraction that defines
the structure and behavior. Encapsulation helps to separate the contractual interface of an
abstraction and implementation.
16/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
6 Inheritance
137. What modifiers may be used with top-level class?
public, abstract and final can be used for top-level class.
141. Can you write Java code for declaration of multiple inheritance in Java?
Class C extends A implements B
142. I don't want my class to be inherited by any other class. What should I
do?
You should declare your class as final. But you can't define your class as final if it is an abstract
class. A class declared as final can't be extended by any other class.
143. Are constructors inherited? Can a subclass call the parent's class
constructor? When?
You cannot inherit a constructor. That is, you cannot create an instance of a subclass using a
constructor of one of its super classes. One of the main reasons is because you probably don't
want to override the super classes constructor, which would be possible if they were inherited. By
giving the developer the ability to override a super classes constructor you would erode the
encapsulation abilities of the language.
7 Polymorphism
145. What is meant by Polymorphism?
Polymorphism means taking more than one form. Polymorphism is a characteristic of being able
to assign a different behavior or value in a subclass, to something that was declared in a parent
class.
149. Can a method be overloaded based on different return type but the same
argument type?
No, because the methods can be called without using their return type in which case there is
ambiguity for the compiler.
18/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
160. Why is an Interface be able to extend more than one Interface but a Class
can't extend more than one Class?
Java doesn't allow multiple inheritance, so a Class is restricted to extend only one Class. But an
Interface is a pure abstraction model and doesn't have inheritance hierarchy like classes (do
remember that the base class of all classes is Object). So an Interface is allowed to extend more
than one Interface.
166. Can a source file contain more than one class declaration?
Yes, a single source file can contain any number of Class declarations but only one of the class
can be declared as public.
168. What is the difference between a static and a non-static inner class?
A non-static inner class may have object instances that are associated with instances of the class's
outer class. A static inner class does not have any object instances
169. What is the difference between the inner class and the nested class?
When a class is defined within the scope of another class, then it becomes an inner class. If the
access modifier of the inner class is static, then it becomes a nested class.
9 Methods
173. What is the importance of the main method in Java?
20/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
The main() method is the entry point of any standalone java application. The syntax of the main
method is public static void main(String args[]). The main method is public and static so
that java can access it without initializing the class. The input parameter is an array of String
through which we can pass runtime arguments to the java program. Check this post to learn how
to compile and run a java program.
181. What if the static modifier is removed from the signature of the main
method?
Program compiles. But at runtime throws an error "NoSuchMethodError".
21/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
Virtual methods are used to use the polymorphism feature in C++. Say class A is inherited from
class B. If we declare say function f() as virtual in class B and override the same function in
class A then at runtime appropriate method of the class will be called depending upon the type of
the object.
184. Can you have an inner class inside a method and what variables can you
access?
Yes, we can have an inner class inside a method and final variables can be accessed.
186. What if I do not provide the String array as the argument to the method?
Program compiles but throws a runtime error "NoSuchMethodError".
193. Does the order of public and static declaration matter in main() method?
No. It doesn't matter but void should always come before main().
23/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
because Java runtime figures out that foo() is a static method and calls it on the class loaded
into the memory and doesn’t use the object at all, so no NullPointerException.
10 Abstract Classes
199. What is an Abstract Class?
An abstract class is a class that has no instances. An abstract class is written with the expectation
that its concrete subclasses will add to its structure and behavior, typically by implementing its
abstract operations.
11 Interfaces
205. What is an Interface?
Java does not allow multiple inheritance for classes (i.e. a subclass being the extension of more
than one superclass). To tie elements of different classes together Java uses an interface.
Interfaces are similar to abstract classes but all methods are abstract and all properties are static
final. As an example, we will build a Working interface for the subclasses of Animal. Since this
interface has the method called work(), that method must be defined in any class using the
Working interface.
public interface Working
{
public void work();
}
When you create a class that uses an interface, you reference the interface with the reserved
word implements Interface_list. Interface_list is one or more interfaces as multiple interfaces are
allowed. Any class that implements an interface must include code for all methods in the interface.
This ensures commonality between interfaced objects.
public class WorkingDog extends Dog implements Working
24/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
{
public WorkingDog(String nm)
{
super(nm); // builds ala parent
}
public void work() // this method specific to WorkingDog
{
speak();
System.out.println("I can herd sheep and cows");
}
}
Interfaces can be inherited (i.e. you can have a sub-interface). As with classes, the extends
keyword is used. Multiple inheritance can be used with interfaces.
206. Why is an Interface be able to extend more than one Interface but a Class
can't extend more than one Class?
Java doesn't allow multiple inheritance, so a Class is restricted to extend only one Class. But an
Interface is a pure abstraction model and doesn't have inheritance hierarchy like classes (do
remember that the base class of all classes is Object). So an Interface is allowed to extend more
than one Interface.
25/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
The Enumeration interface allows you to iterate through all the elements of a collection. Iterating
through an Enumeration is similar to iterating through an Iterator. However, there is no removal
support with Enumeration.
211. Why do you create interfaces, and when MUST you use one?
You would create interfaces when you have two or more functionalities talking to each other.
Doing it this way helps you in creating a protocol between the parties involved.
220. What is a cloneable interface and how many methods does it contain?
It is not having any method because it is a TAGGED or MARKER interface.
223. Can we define private and protected modifiers for variables in interfaces?
No.
12 Packages
224. What is a package?
The package is a collection of related classes and interfaces. The package declaration should be
the first statement in a java class.
225. What is the impact using a * during importing (for example import
java.io.*;?
When a * is used in an import statement, it indicates that the classes used in the current source
can be available in the imported package. Using slightly increases the compilation time but has no
impact on the execution time.
228. Does importing a package imports the subpackages as well? e.g. Does
importing com.MyTest.* also import com.MyTest.UnitTests.*?
No, you will have to import the subpackages explicitly. Importing com.MyTest.* will import
classes in the package MyTest only. It will not import any class in any of its subpackage.
13 Wrapper Classes
232. What are the Wrapper classes?
Java provides specialized classes corresponding to each of the primitive data types. These are
called wrapper classes. They are e.g. Integer, Character, Double, etc.
27/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
14 Threads
234. Describe synchronization concerning multithreading.
For multithreading, synchronization is the capability to control the access of multiple threads to
shared resources. Without synchronization, one thread can modify a shared variable while another
thread is in the process of using or updating the same shared variable. This usually leads to
significant errors.
28/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
thread is in the process of using or updating that object's value. This often leads to significant
errors.
245. What is multithreading and what are the methods for inter-thread
communication and what is the class in which these methods are
defined?
Multithreading is the mechanism in which more than one thread runs independently of each other
within the process. wait (), notify () and notifyAll() methods can be used for inter-thread
communication and these methods are in Object class.
wait(): When a thread executes a call to wait() method, it surrenders the object lock
and enters into a waiting state.
notify() or notifyAll(): To remove a thread from the waiting state, some other
thread must make a call to notify() or notifyAll() method on the same object.
246. What is the class and interface in java to create a thread and which is the
most advantageous method?
Thread class and Runnable interface can be used to create threads and using Runnable interface
is the most advantageous method to create threads because we need not extend thread class
here.
29/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
If a thread attempts to execute a synchronized method or synchronized statement and is unable
to acquire an object's lock, it enters the waiting state until the lock becomes available.
15 Collections
251. What is the difference between a Choice and a List?
A Choice is displayed in a compact form that requires you to pull it down to see the list of available
choices. Only one item may be selected from a Choice. A list may be displayed in such a way that
several List items are visible. A list supports the selection of one or more List items.
30/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
16 Exception Handling
260. How to create custom exceptions?
Your class should extend class Exception or some more specific type thereof.
31/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
reaches the caller method. The same procedure is repeated if the caller method is included in a
try catch block. This process continues until a catch block handling the appropriate type of
exception is found. If it does not find such a block, then finally the program terminates.
273. If I write System.exit (0); at the end of the try block, will the finally
block still execute?
No, in this case, the finally block will not execute because when you say System.exit (0);
the control immediately goes out of the program, and thus finally never executes.
275. How does a try statement determine which catch clause should be used
to handle an exception?
When an exception is thrown within the body of a try statement, the catch clauses of the try
statement are examined in the order in which they appear. The first catch clause that is capable
of handling the exceptions executed. The remaining catch clauses are ignored.
17 I/O Streams
278. What is a stream and what are the types of Streams and classes of the
Streams?
A Stream is an abstraction that either produces or consumes information. There are two types of
Streams and they are:
Byte Streams: Provide a convenient means for handling input and output of bytes.
Character Streams: Provide a convenient means for handling input & output of
33/34
ADF Essentials Training by Deepak Bhagat
Core Java Questions and Answers
characters. Byte Streams classes: Are defined by using two abstract classes, namely
InputStream and OutputStream.
Character Streams classes: Are defined by using two abstract classes, namely Reader
and Writer.
281. What is the difference between the Reader/Writer class hierarchy and
the InputStream/OutputStream class hierarchy?
The Reader/Writer class hierarchy is character-oriented, and the
InputStream/OutputStream class hierarchy is byte-oriented.
282. What class allows you to read objects directly from a stream?
The ObjectInputStream class supports the reading of objects from input streams.
34/34
ADF Essentials Training by Deepak Bhagat