Java Flashcards

1
Q

Java What is the difference between an interface and an Abstract class?

A

Interface can only declare constants and methods. However, interface can have default behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Java Describe synchronization in respect to multithreading.

A

With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for the one thread to modify a shared variable while another thread is in the process if using or updating same share variable. This usually leads to significant errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Java Thread usage.

A

The thread could be implemented by using runnable interface or by inheriting from the thread class. The former is more advance advantageous, ‘cuase when you are going for multiple inheritance..the only interface can help.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Java What is a constructor.

A

A member method using to create the object. It has the same name as the class. invoked using the new operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an Iterator?

A

Allows to iterate through objects, collections of objects. Use Java.util.Iterator. It is best not to modify anything. Just read it only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a public variable?

A

Public class is visible in

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Java?

A

A high-level programming language developed by Sun Microsystems. Java was originally called OAK, and was designed for handheld devices and set-top boxes. Oak was unsuccessful so in 1995 Sun changed the name to Java and modified the language to take advantage of the burgeoning World Wide Web.`

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the main features of java?

A

The main features of java are ■Compiled and Interpreted ■Object oriented ■Robust and secure ■Type safe ■High Performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the Java API?

A

The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the Java Virtual Machine (JVM)?

A

The Java Virtual Machine is software that can be ported onto various hardware-based platforms.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Java What is variables and then types?

A

Variables is an identifier that denotes a storage location used to store a data values.unlike constants that remain unchanged during the execution of a program, a variable may takes different values at different times during the execution of the program. ■Instance variables ■Class variables ■Local variable ■Parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Java What is dot operator?

A

The dot operator(.) is used to access the instance variables and methods of class objects.It is also used to access classes and sub-packages from a package. Examples : Person1.age ———> Reference to the variable age

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Java Define strings?

A

Strings represent a sequence of characters.The easiest way to represent a sequence of characters in java is by using a character array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Java What is serialization?

A

Serialization is the process of converting a objects into a stream of bytes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Java What are different types of access modifiers?

A

Access specifiers are keywords that determine the type of access to the member a class. ■Public ■Protected ■Private ■Default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Java What is the Collection interface?

A

The Collection interface provides support for the implementation of a mathematical bag - an unordered collection of objects that may contain duplicates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Java What must a class do to implement an interface?

A

The class must provide all of the methods in the interface and identify the interface in its implements clause.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Java What is the Collections API?

A

The Collections API is a set of classes and interfaces that support operations on collections of objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Java What is an array?

A

Array is a group of related data items that share a common name.For instance, we can define an array name salary to represent a set of salaries of a group of employees. Examples : salary[10]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

java What is list iterator?

A

The List and Set collections provide iterators, which are objects that allow going over all the elements of a collection in sequence. The java.util.Iterator interface provides for one-way traversal and java.util.ListIterator is an iterator for lists that allows the programmer to traverse the list in either direction (i.e. forward and or backward) and modify the list during iteration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Java What is the main difference between a String and a StringBuffer class?

A

String is immutable : you can’t modify a string object but can replace it by creating a new instance. Creating a new instance is rather expensive. StringBuffer is mutable : use StringBuffer or StringBuilder when you want to modify the contents. StringBuilder was added in Java 5 and it is identical in all respects to StringBuffer except that it is not synchronized,which makes it slightly faster at the cost of not being thread-safe.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Java When to use serialization?

A

A common use of serialization is to use it to send an object over the network or if the state of an object needs to be persisted to a flat file or a database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Java What is the main difference between shallow cloning and deep cloning of objects?

A

Java supports shallow cloning of objects by default when a class implements the java.lang.Cloneable interface. Deep cloning through serialization is faster to develop and easier to maintain but carries a performance overhead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Java What are wrapper classes?

A

primitive data types may be converted into object types by using the wrapper classes contained in the java.lang package. Exampes : int, float, long, char, double

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Java What is the difference between an instance variable and a static variable?

A

variable per JVM per class loader.When a class is loaded the class variables are initialized. Instance variables are non-static and there is one occurrence of an instance variable in each class instance.Also known as a member variable or a field.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Java Where and how can you use a private constructor?

A

Private constructor is used if you do not want other classes to instantiate the object and to prevent subclassing.The instantiation is done by a public static method (i.e. a static factory method) within the same class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Java What is type casting?

A

Type casting means treating a variable of one type as though it is another type. Examples : int m = 5; byte n =i;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Java What is a user defined exception?

A

User defined exceptions may be implemented by defining a new exception class by extending the Exception class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Java What is an instanceof operator?

A

Instanceof is an object reference operator and returns true if the object on the left-hand side is an instance of the glass given to the right hand side.This operator allows to determine whether the object belongs to a particular class or not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Java What are runtime exceptions?

A

Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Java What is the difference between an interface and an abstract class?

A

An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Java What is a package?

A

A package is a namespace that organizes a set of related classes and interfaces.The classes contained in the packages of other programs can be easily reused.Packages, classes can be unique compared with classes in other packages.That is, two classes in two different packages can have the same name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Java Why do threads block on I/O?

A

Threads block on i/o (that is enters the waiting state) so that other threads may execute while the i/o Operation is performed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Java What is the List interface?

A

The List interface provides support for ordered collections of objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Java What is the Vector class?

A

The Vector class provides the capability to implement a growable array of objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Java What is the base class of all classes?

A

java.lang.Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Java What is the importance of static variable?

A

static variables are class level variables where all objects of the class refer to the same variable. If one object changes the value then the change gets reflected in all the objects.

38
Q

Java What is the difference between a while statement and a do while statement?

A

A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do while statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do whilestatement will always execute the body of a loop at least once.

39
Q

Java Describe life cycle of thread?

A

A thread is similiar to a program that has a single flow of control.A thread is a execution in a program. The life cycle of threads are ■Newborn state ■Runnable state ■Running state ■Blocked state ■Dead state

40
Q

Java What is an Applets?

A

Applets are small java programs that are primarily used in Internet computing. They can be transported over the internet from one computer to another and run using the Applet Viewer or any web browser that supports java.

41
Q

Java What are wrapped classes?

A

Wrapped classes are classes that allow primitive types to be accessed as objects.

42
Q

Java What is the difference between an if statement and a switch statement?

A

The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.s

43
Q

Java What modifiers are allowed for methods in an Interface?

A

Only public and abstract modifiers are allowed for methods in interfaces.

44
Q

Java What is the difference between method overriding and overloading?

A

Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same method name but different arguments.

45
Q

Java What do you mean by polymorphism?

A

Polymorphism means the ability to take more than one form. fro example, an operation may exhibit behaviour in different instances. The behaviour depends upon the types of data used in the operatiom.

46
Q

What is the difference between an abstract class and an interface?

A

Abstract class Interface - Have executable methods and abstract methods.Can only subclass one abstract class Interface - Have no implementation code. All methods are abstract.A class can implement any number of interfaces.

47
Q

Java What is the purpose of finalization?

A

The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.

48
Q

Java What is the difference between a break statement and a continue statement?

A

Break statement results in the termination of the statement to which it applies (switch, for, do, or while). A Continue statement is used to end the current loop iteration and return control to the loop statement.

49
Q

Java When is a method said to be overloaded and when is a method said to be overridden?

A

Overloading deals with multiple methods in the same class with the same name but different method signatures. Overriding deals with two methods, one in the parent class and the other one in the child class and has the same name and signatures.

50
Q

Java How is final different from finally and finalize()?

A

■Final - constant declaration. ■The finally block always executes when the try block exits, except System.exit(0) call. ■finalize() is a method of Object class which will be executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity.

51
Q

Java What is Byte Code?

A

All Java programs are compiled into class files that contain bytecodes. These byte codes can be run in any platform and hence java is said to be platform independent.

52
Q

Java What is the difference between error and an exception?

A

Exception means When a method encounters an abnormal condition (an exception condition) that it can’t handle itself, it may throw an exception. ssError mens system doesn’t handle.For example:Overflow,Out of memory.

53
Q

What if the main method is declared as private?

A

When a method is declared as private, the program compiles properly but it will give runtime error Main method not “public„.

54
Q

Java What type of parameter passing does Java support?

A

In Java the arguments are always passed by value.

55
Q

Java What is singleton?

A

It is one of the design pattern. This falls in the creational pattern of the design pattern. There will be only one instance for that entire JVM. You can achieve this by having the private constructor in the class.

56
Q

Java What is Locale?

A

A Locale object represents a specific geographical, political, or cultural region.

57
Q

Java What is the difference between constructors and normal methods?

A

A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.

58
Q

Java What is the difference between static and non-static variables?

A

A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.

59
Q

Java What is the difference between java and c++?

A

■Java is a true object - oriented language while c++ is basically c with object-oriented extension. ■C++ supports multiple inheritence but Java provides interfaces in case of multiple inheritence. ■Java does not support operator overloading. ■Java does not have template classes as in c++. ■java does not use pointers.

60
Q

Java What a private modifier?

A

Private variables or methods may be used only by an instance of the same class that declares the variables or method. A private feature may only be accessed by the class that owns the feature.

61
Q

Java What is the protected variable or method?

A

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 the a differenct package from the class that owns the protected feature.

62
Q

When to use interface in Java.

A

Interface is best choice for Type declaration or defining contract between multiple parties. If multiple programmer are working in different module of project they still use each others API by defining interface and not waiting for actual implementation to be ready. This brings us lot of flexibility and speed in terms of coding and development. Use of Interface also ensures best practices like “programming for interfaces than implementation” and results in more flexible and maintainable code. Though interface in Java is not the only one who provides higher level abstraction, you can also use abstract class but choosing between Interface in Java and abstract class is a skill

63
Q

Java What is the difference between creating String as new() and literal?

A

When we create string with new() Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap. String s = new String(“Test”); does not put the object in String pool , we need to call String.intern() method which is used to put them into String pool explicitly. its only when you create String object as String literal e.g. String s = “Test” Java automatically put that into String pool.

64
Q

Java How does substring () inside String works?

A

Another good Java interview question, I think answer is not sufficient but here it is “Substring creates new object out of source string by taking a portion of original string”. see my post How SubString works in Java for detailed answer of this Java question.

65
Q

Java Which two method you need to implement for key Object in HashMap ?

A

In order to use any object as Key in HashMap, it must implements equals and hashcode method in Java. Read How HashMap works in Java for detailed explanation on how equals and hashcode method is used to put and get object from HashMap. You can also see my post 5 tips to correctly override equals in Java to learn more about equals.

66
Q

Java Where does these two method comes in picture during get operation?

A

This core Java interview question is follow-up of previous Java question and candidate should know that once you mention hashCode, people are most likely ask How its used in HashMap. See How HashMap works in Java for detailed explanation.

67
Q

Java How do you handle error condition while writing stored procedure or accessing stored procedure from java?

A

This is one of the tough Java interview question and its open for all, my friend didn’t know the answer so he didn’t mind telling me. my take is that stored procedure should return error code if some operation fails but if stored procedure itself fail than catching SQLException is only choice.

68
Q

Java What is difference between Executor.submit() and Executer.execute() method ?

A

This Java interview question is from my list of Top 15 Java multi-threading question answers, Its getting popular day by day because of huge demand of Java developer with good concurrency skill. Answer of this Java interview question is that former returns an object of Future which can be used to find result from worker thread)

69
Q

Java What is the difference between factory and abstract factory pattern?

A

Abstract Factory provides one more level of abstraction. Consider different factories each extended from an Abstract Factory and responsible for creation of different hierarchies of objects based on the type of factory. E.g. AbstractFactory extended by AutomobileFactory, UserFactory, RoleFactory etc. Each individual factory would be responsible for creation of objects in that genre.

70
Q

What is garbage collection? What is the process that is responsible for doing that in java?

A

Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process

71
Q

What kind of thread is the Garbage collector thread?

A

It is a daemon thread.

72
Q

What is a daemon thread?

A

These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly.

73
Q

What is reflection?

A

Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions.

74
Q

What are the approaches that you will follow for making a program very efficient?

A

By avoiding too much of static methods avoiding the excessive and unnecessary use of synchronized methods Selection of related classes based on the application (meaning synchronized classes for multiuser and non-synchronized classes for single user) Usage of appropriate design patterns Using cache methodologies for remote invocations Avoiding creation of variables within a loop and lot more.

75
Q

When you think about optimization, what is the best way to findout the time/memory consuming process?

A

Using profiler

76
Q

What is the difference between JDK and JRE?

A

Java Development Kit (JDK) is the most widely used Java Software Development Kit. Java Runtime Environment (JRE) is an implementation of the Java Virtual Machine which executes Java programs

77
Q

What is the difference between equals() and “==” ?

A

Answer: Equals is intended to check logical equality and == checks if both references point to same object. (Thanks Sandeep) a == b; // Compares references, not values. a.equals(b); // Compares values for equality. Source

78
Q

When will you use Comparator and Comparable interfaces?

A
79
Q

What is the difference between checked and unchecked exceptions?

A

In general, unchecked exceptions represent defects in the program (bugs), which are normally Runtime exceptions. Furthermore, checked exceptions represent invalid conditions in areas outside the immediate control of the program.

80
Q

What is the difference between final, finally and finalize?

A

“final” is the keyword to declare a constant AND prevents a class from producing subclasses. (Thanks Tom Ellis) “finally” is a block of code that always executes when the try block is finished, unless System.exit() was called. finalize() is an method that is invoked before an object is discarded by the garbage collector

81
Q

Can you write critical section code for singleton?

A

This core Java question is followup of previous question and expecting candidate to write Java singleton using double checked locking. Remember to use volatile variable to make Singleton thread-safe.

82
Q

What is MVC Architecture?

A

The main aim of the MVC (Model View Control) architecture is to separate the
business logic and application data from the presentation
data to the user.

Here are the reasons why we should use the MVC design pattern.

  1. They are resuable : When the problems recurs, there is
    no need to invent a new solution, we just have to follow the
    pattern and adapt it as necessary.
  2. They are expressive: By using the MVC design pattern
    our application becomes more expressive.

1). Model: The model object knows about all the data that
need to be displayed. It is model who is aware about all the
operations that can be applied to transform that object. It
only represents the data of an application. The model
represents enterprise data and the business rules that
govern access to and updates of this data. Model is not
aware about the presentation data and how that data will be
displayed to the browser.

2). View : The view represents the presentation of the
application. The view object refers to the model. It uses
the query methods of the model to obtain the contents and
renders it. The view is not dependent on the application
logic. It remains same if there is any modification in the
business logic. In other words, we can say that it is the
responsibility of the of the view’s to maintain the
consistency in its presentation when the model changes.

3). Controller: Whenever the user sends a request for
something then it always go through the controller. The
controller is responsible for intercepting the requests from
view and passes it to the model for the appropriate action.
After the action has been taken on the data, the controller
is responsible for directing the appropriate view to the
user. In GUIs, the views and the controllers often work
very closely together.

83
Q

What is an abstract class?

A

Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such.

A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.

84
Q

What is static in java?

A

Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object.

A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can’t override a static method with a nonstatic method. In other words, you can’t change a static method into an instance method in a subclass.

85
Q

What is final?

A

final class can’t be extended ie., final class may not be subclassed. A final method can’t be overridden when its class is inherited. You can’t change value of a final variable (is a constant).

86
Q

What environment variables do I need to set on my machine in order to be able to run Java programs?

A

CLASSPATH and PATH are the two variables.

87
Q

Can an application have multiple classes having main method?

A

Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.

88
Q

Do I need to import java.lang package any time? Why ?

A

No. It is by default loaded internally by the JVM.

89
Q

What are different types of inner classes?

A
  1. Nested top-level classes
  2. Member classes
  3. Local classes
  4. Anonymous classes
**Nested top-level classes**- If you declare a class within a class and specify the static modifier, the compiler treats the class just like any other top-level class.
 Any class outside the declaring class accesses the nested class with the declaring class name acting similarly to a package. eg, outer.inner. Top-level inner classes implicitly have access only to static variables.There can also be inner interfaces. All of these are of the nested top-level variety.

Member classes - Member inner classes are just like other member methods and member variables and access to the member class is restricted, just like methods and variables. This means a public member class acts similarly to a nested top-level class. The primary difference between member classes and nested top-level classes is that member classes have access to the specific instance of the enclosing class.

**Local classes** - Local classes are like local variables, specific to a block of code. Their visibility is only within the block of their declaration. In order for the class to be useful beyond the declaration block, it would need to implement a
 more publicly available interface.Because local classes are not members, the modifiers public, protected, private, and static are not usable.

Anonymous classes - Anonymous inner classes extend local inner classes one level further. As anonymous classes have no name, you cannot provide a constructor.

90
Q
A