Java Interview Questions - TutorialsPoint

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the default value of byte datatype in Java?

0Fl

What kind of variables a class can consist of?

A class consist of Local variable, Instance variables, and class variables

Define class

A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.

Define Packages in Java

A package can be defined as a grouping of related types (classes, interfaces, enumerations, and annotations) providing access protection and name space management.

What is an Exception?

A problem that arises during the execution of a program. Exceptions are caught by handlers positioned along the threads method invocation stack

what is the difference between Swing and AWT components

AWT components: heavy weight Swing components: light weight

When throw keyword is used?

An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.

Explain Runtime Exceptions?

An exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.

what is an Interface?

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

What are the two ways in which Threat can be created?

Can be created by : -implementing Runnable interface -extending the Thread class

which class should you use to obtain design information about an object?

Class class is used to obtain information about an object's design and java.lang.Class class instance represent classes, interfaces in a running Java application.

What is a static variable?

Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor, or block.

Which are the two subclasses under Exception class?

IOException RuntimeException

When super keyword is used?

If the method overrides one of its superclass's methods, overridden method can be invoked through the use of the keyword super. it can be also used to refer to a hidden field.

When Abstract methods are used?

If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.

What is a Instance Variable?

Instance variables are within a class but outside any method. These variables are instantiated when the class is loaded.

What do you mean by Constructor?

It gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.

Give some features of Interface

It includes: -interface cannot be instantiated -an interface does not contain any constructors -all of the methods in an interface are abstract

Difference between throw and throws?

It includes: -throw is used to trigger an exception where as throws is used to declare an exception

Why is Java is considered dynamic?

It is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time

what are use cases?

It is part of the analysis of a program and describes a situation that a program might encounter and what behavior the program should exhibit in that circumstance

What is finalize() method?

It is possible to define a method that will be called just before an objects final destruction by the garbage collector. This method is called finalize(), and it can be used to ensure that an object terminates cleanly.

Define Inheritance?

It is the process where one object acquires the properties of another. With the use of inheritance this information is made manageable in a hierarchical order.

What is Encapsulation?

It is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. Therefore encapsulation is also referred to as data hiding.

What is comparable interface?

It is used to sort collections and arrays of objects using the collections.sort() and java.utils. The objects of the class implementing the Comparable interface can be ordered.

Explain the usage of this() with constructors?

It is used with variables or methods and used to call constructor of same class.

What is Abstraction?

It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the system.

What is Java Architectural Neutral?

It's compiler generates an architecture-neutral object file format, which makes the compiled code to b executable on many processors, with the presence of java runtime system.

What is JAR file?

Java Archive files it aggregates many files into one hold java classes in a library built on zip file format and have .jar file extension

Define JRE

Java Runtime Environment is an implementation of the Java Virtual Machine which executes Java programs. It provides the minimum requirements for executing a Java application

What do you know about Java?

Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX

What do you mean by Access Modifier?

Java provides access modifiers to set access levels for classes, variables, methods, and constructors. A member has package or default accessibility when no accessibility modifier is specified.

What do you mean by synchronized Non Access Modifier?

Java provides these modifiers for providing functionalities other than Access Modifiers, synchronized used to indicate that a method can be accessed by only one thread at a time.

What are the supported platforms by Java Programming Language

Java runs on a variety of platforms, such as Windows, Max OS, and the various versions of UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubuntu, CentOS, etc.

How java enabled High Performance?

Java uses Just-In-Time compiler to enable high performance. Just-In-Time compiler is a program that turns Java byte code, which is a program that contains instructions that must be interpreted into instructions that can be sent directly to the processor.

List two Java IDE's

Netbeans, Eclipse

What do you mean by Object?

Object is a runtime entity and it's state is stored in fields and behavior is shown via methods. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication

java.util.regex consists of which classes?

Pattern Matcher PatternSyntaxException

According to Java Operator precedence, which operator is considered to be with highest precedence?

Postfix operators i.e. () [] . is at the highest precedence

What is Singleton class?

Singleton class control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes.

List any five features of Java

Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded

What is the difference between StringBuffer and StringBuilder class?

StringBuilder is faster than StringBuffer but StringBuffer is safer for threads

What is Polymorphism?

The ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to child class object.

How finally used under Exception Handling?

The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

What is the primary benefit of Encapsulation?

The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this, Encapsulation gives maintainability, flexibility, and extensibility to our code.

What is a Class Variable?

These are variables declared within a class, outside any method, with the static keyword.

What is Abstract class?

These classes cannot be instantiated and are either partially implemented or not at all implemented. This class contains one or more abstract methods which are simply method declarations without a body.

Why is StringBuffer called mutable?

To allow Strings to be modified. If it is necessary to make a lot of modifications to Strings of characters, then StringBuffer should be used.

What do you mean by Checked Exceptions?

Typically a user error or a problem that cannot be foreseen by the programmer. For example: if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation

Why packages are used?

Used in java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations, and annotations, etc, easier

explain garbage collection in java?

Used to free the memory. By cleaning those objects that is no longer reference by any of the program.

When a byte datatype is used?

Used to save space in large arrays, mainly in place of integers, since byte is four times smaller than an int.

What is a Local Variable?

Variables defined inside methods, constructors, or blocks are called local variables. The variable will be declared and initialized within the method and it will be destroyed when the method has completed.

What is protected access modifier?

Variables, methods, and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

What is Java Virtual Machine and how it is considered in context of Java's platform independent feature?

When Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. this byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run.

Explain TreeSet?

a Set implemented when we want elements in a sorted order.

What are the ways in which a thread can enter the waiting state?

a thread can enter the waiting state by invoking its sleep() method, by blocking on IO, by unsuccessfully attempting to acquire an object's lock, or by invoking an object's wait() method. it can also enter the waiting state by invoking its (deprecated) suspend() method

what is a transient variable?

a variable that may not be serialized during serialization and which is initialized by its default value during de-serialization

what invokes a thread's run() method?

after a thread is started, via its start() method of the thread class, the JVM invokes the threads run() method when the thread is initially executed

Define immutable object?

can't be changed once it is created.

what are Wrapper classes?

classes that allow primitive types to be accessed as objects ex: Integer, Character, Double, Boolean

Explain set interface()

collection of element which cannot contain duplicate elements. The Set Interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

what is the difference between constructors and other methods?

constructors must have the same name as the class and cannot return a value they are only called once while regular methods can be called many times

What do you mean by multithreaded program?

contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.

What is the purpose of default constructor?

created only if no constructor is in the class

List the three steps for creating an Object for a class?

declared instantiated initialized

an applet extend which class?

extends java.applet.Applet

what is final class?

final classes are created so the methods implemented by that class cannot be overridden. it can't be inherited

What is the default value of float and double datatype in Java?

float: 0.0f double: 0.0d

what is the difference between a window and a frame?

frame class extends window to define a main application window that can have a menu bar

Define composition

holding the reference of the other class within some other class

What is function overloading?

if a class has multiple functions by the same name but different parameters

When throws keyword is used?

if a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature

What is function overriding?

if a subclass provides a specific implementation of a method that is already provide by it's parent class

how to add menu shortcut to menu item

if there is a button instance called b1, you may add menu short cut by calling b1.setMnemonic('F'), so the user may be able to use Alt+F to click the button

List some Java keywords(unlike C, C++ keywords)

import, super, finally, etc

what is an applet?

is a Java program that runs in a web browser. can be a fully functional Java application because it has the entire Java API at its disposal

When parseInt() method can be used?

is used to get the primitive data type of a certain String

Define JIT compiler?

it improves runtime performance of computer programs based on bytecode

Which package is used for pattern matching with regular expressions?

java.util.regex

which package has light weight components?

javax.Swing package

Difference between Overloading and Overriding?

method overloading increases the readability of the program. method overriding provides the specific implementation of the method that is already provided by its super class parameter must be different in case of overloading, parameter must be same in case of overriding

Can a constructor be made final?

no, not possible

what is the difference between object oriented programming language and object based programming language?

object based programming languages follow all the features of OOPs except inheritance. JavaScript is an example of object based programming languages

what is the difference between the paint() and repaint() methods?

paint() supports painting via a Graphics object repaint() used to cause paint() to be invoked by the AWT painting thread

Why Vector class is used?

provides the capability to implement a growable array of objects. it proves to be very useful if you don't know the size of the array in advance, or you just need one that can change sizes over the lifetime of a program

explain the following line used under Java Program: public static void main (String args[])

public - access specifier static - allows main() to be called without instantiating a particular instance of a class void - it affirms the compiler that no value is returned by main() main() - method called at the beginning of a Java program String args[] - parameter is an instance array of class String

what is serialization and deserialization?

serialization: process of writing the state of an object to a byte stream de-serialization: process of restoring these objects

what is the difference between sleep() and wait()?

sleep() : puts the thread asleep for the exact amount of time set wait() : causes a wait up to the time set. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread

Why is String class considered immutable?

so that once it is created it cannot be changed. Since String is immutable it can safely be shared between many threads which is considered very important for multithreaded programming.

what is the difference between static and non-static variables?

static variable: associated with the class as a whole rather than with specific instances of a class non-static variable: take on unique values with each object instance

Variables used in a switch statement can be used with which datatypes?

string enum byte short int char

what is the difference between the reader/writer class hierarchy and the InputStream/OutputStream class hierarchy?

the Reader/Writer class hierarchy is character-oriented the InputStream/OutputStream class hierarchy is byte-oriented

what is synchronization

the capability to control the access of multiple threads to shared resources. synchronized keyword in java provides locking which ensures mutual exclusive access of shared resource and prevent data race

how does multi-threading take place on a computer with a single CPU?

the operating system's task schedule allocates execution time to multiple tasks. by quickly switching between executing tasks, it creates the impression that tasks execute sequentially

explain the use of subclass in a java program?

they inherit all the public and protected methods and the implementation. it also inherits all the default modifier methods and their implementation

what is NullPointerException?

thrown when calling the instance method of a null object, accessing, or modifying the field of a null object

How many bits are used to represent Unicode, ASCII, utf-16, and utf-8 characters?

unicode - 16 bits ascii - 7/8 bits utf 16 - 16 bits or larger utf 8 - 8, 16, & 18 bits

what is the purpose of the file class?

used to create objects that provide access to the files and directories of a local file system

What is static block?

used to initialize the static data member it is executed before main method at the time of classloading

What is a WAR file?

web archive file used to store XML, java classes, and javaserver pages which is caused to distribute a collection of javaserver pages, java servlets, java classes, XML files, and static web pages

What is the difference between yielding and sleeping?

when a task invokes its yield() method, it returns to the ready state. when a task invokes it sleep() method, it returns to the waiting state

when is the ArrayStoreException thrown?

when copying elements between different arrays, if the source or destination arguments are not arrays or their types are not compatible

What things should be kept in mind while creating your own exceptions in Java?

while creating your own expression: -All exceptions must be a child of Throwable -If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class. -You want to write a runtime exception, you need to extend the RuntimeException class

does it matter in what order catch statements for FileNotFoundException and IOException are written?

yes it does. the FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first

can you write a java class that could be used both as an applet as well as an application?

yes, just add a main() method to the applet

is there any limitation of using inheritance

yes, since inheritance inherits everything from the super class and interface, it may make the subclass too clustering and sometimes error-prone when dynamic overloading in some situation

can you call one constructor from another if a class has multiple constructors?

yes, use () syntax


Conjuntos de estudio relacionados

Ch.8 Political Parties Vocabulary for A.P. Government.

View Set

mAtCh tHe eXcErPt to the POEM&&&&AUTHOR !

View Set

4.3 The importance of Non-Nutrient Fiber

View Set

Cardiovascular System Ch 18 and 19 Week 2

View Set

4.7 Magnetism and electromagnetism

View Set

Chapter 16 Fluid and Electrolytes Practice Questions

View Set

Project Cost Management - Ch 7 (True/False)

View Set