TutorialsPoint.com Java Interview Questions Flashcards
Five features of Java?
- Object Oriented
- Platform independent
- Robust
- Interpreted
- Multi-threaded
Why architectural neutral?
Compiler generates byte code which then can be interpreted by Java runtime
What do you know about Java?
- High level programming language originally developed by Sun Microsystems.
- Runs on many platforms such as Windows, Mac OS, Unix various OS
Supported platforms?
- Windows
- Mac OS
- Unix/Linux various versions
How Java enabled high performance?
Uses just-in-time compiler which is a program that turns Java byte code which is interpreted into instructions that can be sent directly to the processor.
Why Java is considered dynamic?
Designed to adapt to an evolving environment
What is Java Virtual Machine (JVM)?
- Java is compiled into platform independent byte code.
- The byte code is distributed over the web and interpreted by the Java Virtual Machine on the platform it is being run.
List two or three Java IDEs?
- Eclipse
- Netbeans
- IntelliJ
List some Java keywords unlike C, C++?
- Import
- Super
- Finally
Describe Object?
- A runtime entity in which it’s state is stored in fields and behavior is shown via methods.
- Methods operate on object’s internal state and serve as the primary mechanism for object-to-object communication.
Define class?
- A blue print from which individual objects are created.
- Contains fields and methods to describe the behavior of an object.
What kind of variables a class can consist of?
- Local variable
- Instance variables
- Class variables
What is a local variable?
- Variables defined inside methods, constructors, or blocks.
- Declared and initialized within method and destroyed when method completes.
What is an instance variable?
- Variable within a class but outside any method.
- Instantiated when class is loaded.
What is a class variable?
Declared within a class, outside any method, with the static keyword.
What is Singleton class?
Ensures that only one instance of a class is created.
Define Constructor?
- A special method which is invoked when a new object is created.
- Every class has a constructor.
- Java compiler builds default constructor when none explicitly found.
List three steps for creating an Object for a class?
- Declared
- Instantiated
- Initialized
Default value of byte data type?
0
Default value of float and double data type?
- 0.0f
- 0.0d
When byte data type is used?
Save space in large arrays, in place of integers, since 4 times smaller than int.
What is a static variable?
Class variable which is declared with static keyword in class, but outside a method, constructor, or block
Define Access Modifier?
Used to set access levels for classes, variables, methods, and constructors. Defaults to package access level.
What is protected access modifier?
Variables, methods, and constructors declared protected in a superclass can be accessed only by:
- subclasses in other package
- any class within the package of the protected members’ class.
What is synchronized Non Access Modifier?
Used to indicate that method can be accessed by only one thread at a time.
Variables used in switch statement can be used with which datatypes?
- byte
- short
- int
- char
When parseInt() method can be used?
- Used to get primitive data type of a certain String.
- For translating base 2, 8, 10, and 16 numbers stored as strings into integers.
Why is String class considered immutable?
Once created a String object cannot be changed. This enables safe sharing between many threads which is essential for multithreaded programming.
Which operators have highest precedence?
- ()
- []
Why is StringBuffer mutable?
When necessary to make changes, StringBuffer to be used instead of String.
What is difference between String, StringBuffer, and StringBuilder classes?
- String cannot be updated and thus to concat strings requires create of new String object. It is thread safe.
- StringBuilder is faster, but not thread safe. Recommended for single thread programs.
- StringBuffer is thread safe
Which package is used for pattern matching with regular expressions?
java.util.regex
java.util.regex consists of which classes?
- Pattern
- Matcher
- PatternSyntaxException
What is finalize() method?
- Method which is called just before object’s final destruction by the garbage collector.
- Can be used to ensure object terminates cleanly.
What is an Exception?
- A problem that arises during the execution of a program.
- Exceptions are caught by handlers positioned along the thread’s method invocation stack.
Explain Checked Exceptions?
- Exceptions that are checked at compile time to ensure code handles them properly.
- If code within a method throws a checked exception, then the method must either handle the exception or specify the exception using throws keyword.
- Examples:
- FileNotFoundException
- IOException
Explain Runtime Exception?
Exceptions that aren’t checked at compile time and which should be preventable by proper coding.
Examples:
- ArrayIndexOutOfBoundsException
- ClassCastException
What are the two subclasses under Exception class?
- IOException (checked exception)
- RuntimeException (runtime exception)
When throws keyword is used?
If method does not handle a checked exception, the method must declare it using the throws keyword.
When throw keyword is used?
An exception can be thrown, either newly instantiated one or an exception that was just caught, by using throw keyword.
How is finally used under Exception Handling?
Used to create a block of code that follows a try block. It executes whether or not an exception has occurred.
What things to be kept in mind while creating own exceptions?
- All exceptions must be child of throwable
- Depending on exception type:
- Must extend Exception class to write checked exception that is to be enforced by the Handle or Declare Rule
- Must extend RuntimeException when writing runtime exception
Define inheritance?
Process where one class is built upon another class inheriting (re-using) methods and adding of other methods.
When super keyword used?
- If method overrides its super class’s method, overridden method can be invoked using super keyword. Applies to constructors too.
- Also used to refer to hidden field (when super class and sub-class have same field name which is a bad practice).
What is Polymorphism?
Ability of an object to take on many forms.
Common uses:
- When parent class reference is used to refer to child class object.
- Deer d = new Deer();
- Animal a = d;
- Vegetarian v = d;
- Object o = d;
- When class has multiple inheritance.
- public class Deer extends Animal implements Vegetarian{}
What is abstraction?
- Ability to make a class abstract in OOP.
- Abstract classes usually have one or more abstract methods in which method is declared but not implemented.
What is Abstract class?
- Class cannot be Instantiated and is either partially or not at all implemented.
- The class contains one or more abstract methods which are simply method declarations without body.
When Abstract methods are used?
When wanting a class to contain a particular method but you want actual implementation to be determined by child classes, you declare parent class as abstract.
What is Encapsulation?
Technique of making fields private and providing access to the fields via public methods. AKA data hiding.
What are primary benefits of Encapsulation?
- Ability to modify implemented code without breaking code of others using our code.
- Gives maintainability, flexibility, and extensibility to our code.
What is an interface?
- Collection of abstract methods.
- A class implements an interface, thereby inheriting the abstract methods of the interface.
Give some features of Interface?
- Cannot be Instantiated
- Does not contain any constructors
- All methods are abstract (not implemented)
- All fields automatically constants (public static final)
- Class can implement multiple interfaces
Define Packages in Java?
A group of related types (classes, interfaces, enumerations, and annotations) providing access protection and name space management.
Why packages are used?
- Prevent naming conflicts
- To control access
- Make searching/locating and usage of classes, interfaces, enumerations, and annotations, etc. easier.
Define Multithreaded program?
- Contains two or more parts that can run concurrently.
- Each part is called a thread and each thread defines a separate path of execution.
Explain two ways in which a Thread can be created?
- Implementing Runnable interface
- Extending Thread class
What is an applet?
- Java program that runs in a Web browser.
- An applet can be fully functional because it has entire Java API at its disposal.
An applet extends which class?
java.applet.Applet
Explain garbage collection in Java?
Used to free the memory by cleaning objects no longer referenced.
Define immutable object?
Can’t be changed once created.
Example: String
Explain the usage of this() with constructors?
Used with variables or methods and used to call constructor of same class.
Explain Set Interface?
- Collection of elements which cannot contain duplicate elements.
- Contains only methods inherited from Collection and adds restriction that duplicate elements are prohibited.
Explain TreeSet?
It is Set implemented when we want elements in sorted order.
What is Comparable Interface?
- Used to sort collections and arrays of objects using collections.sort() and Java.utils.
- Objects of the class implementing the Comparable interface can be ordered.
Difference between throw and throws?
- Throw is used to trigger an exception.
- Throws is used in declaration of exception.
Explain the following:
public static void main (String args[])
- public: is the access specifier
- static: allows main() to be called without instantiating a particular instance of a class
- void: affirms the compiler that no value is returned
- main(): method is called at the beginning of Java program
- String args[]: args parameter is an instance array of class String
Define JRE (Java Runtime Environment)?
Implementation of the Java Virtual Machine which invokes Java programs.
What is a JAR file?
- Java Archive file which aggregates many files into one.
- Holds Java classes in a library.
- Built in ZIP file format and has .jar file extension.
What is WAR file?
- Web Archive file used to store XML, Java classes, and JavaServer pages.
- Used to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, static Web pages, etc.
Define JIT compiler?
- Just-in-time compiler that improves runtime performance of computer programs based on bytecode.
- It compiles the bytecode into platform specific executable code that is immediately executed.
Difference between object oriented programming language and object based programming language?
Object based follows all features of OOP except inheritance.
Example: JavaScript is object based.
What is purpose of default constructor?
Java compiler creates default constructor only if there is no constructor in the class.
Can a constructor be made final?
No
You use the final keyword in a method declaration to indicate that the method cannot be overridden
What is static block?
Used to initialize the static data member. It is used before main method at time of classloading.
A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. Here is an example:
static { // whatever code is needed for initialization goes here }
Define composition?
Holding the reference of another class within some class.
Example:
class Fruit { … }
class Apple { private Fruit fruit = new Fruit(); //… }
What is function/method overloading?
When a class has multiple methods by same name but different parameters.
What is function/method overriding?
When a subclass provides a specific implementation of a method that is already provided by its parent class.
What is final class and final method?
- Class declared as final cannot have subclasses.
- Methods declared as final cannot be overridden.
What is NullPointerException?
NullPointerException is thrown when calling the instance method of a null object, accessing or modifying the field of a null object.
To prevent:
public void doSomething(Integer num)
{ if (num != null) { // do something to num } }