Java Interview Questions Flashcards

1
Q

Comparable vs. Comparator

A

If sorting of objects needs to be based on natural order then use comparable.

If they need some customized sorting order then use comparator

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

Serializable vs Externalizable

A

Serializable : class is serialized automatically by default

Externalizable : gives complete control over class serialization process

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

What is ‘transient’ keyword?

A

Used to make a data member not serializable

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

How many ways to create object?

A

There are 4

  • new keyword
  • clone
  • deserialization
  • Class.forName(“classname”).newInstance()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

new keyword vs. newInstance()

A

In general, new operator is used to create objects but if we want to decide type of object to be created at runtime the newInstance() method needs to be used

Object type can be passed as a parameter using newInstance() method

public static void fun( string c ){
    Object obj = Class.forName(c).newInstance();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is java ‘volatile’ keyword?

A

Volatile is used to indicate that a variables value will be modified by different threads

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

Protected vs. Private

A

Protected: can be accessed from the same class to which they belong or from the sub classes, and from the classes of the same package, but not from the outside

Private: can be accessed from the same class to which they belong

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

When to use Static Nested vs. Singleton

A

Static: used when you want to collect related pieces of functionality but you don’t need to have any internal state in any object

Singleton: used when you do want an actual object (with its own internal state etc.) and you want to limit your system to exactly one instance of that object

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

How to create an immutable class?

A

Declare class final and making all data members final

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

What is reflection?

A

The ability to inspect and modify the runtime behavior of application

We can inspect the java class interface, enum and get their methods and field details

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

Purpose of finalize() method?

A

Invoked just before garbage collection and is used to perform clean up processing

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

What is Externalizable ?

A

Used to write objects to the byte stream in compressed format.

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

Why are string objects immutable?

A

Because Java uses the concept of string literals.

Suppose you have five variables all referencing one object (string) if one variable changes the value of the object it will affect all the referencing variables.

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

What is composition and what are the benefits?

A

Holding the reference to another class within some other class

Has-A relationship is created.

Benefits:

  • Get around inheritance restrictions
  • code reuse, grants more flexibility and extensibility than inheritance.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Object Serialization?

A

To convert an object’s state to the byte stream, so that the byte stream can’t be reverted back into a copy of the object.

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

Why use Generics?

A
  • Stronger type checking at compile time.
  • Elimination of casts.
  • Enables code reuse through the implementation of generic algorithms.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the data types supported by Java?

A
  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is Type Erasure?

A

Replace all type parameters

Insert type casts if necessary to preserve type safety.

Generate bridge methods to preserve polymorphism in extended generic types.

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

What is Abstraction?

A

Abstraction is the process of hiding the implementation details and showing only functionality to the user

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

Can we override static methods?

A

No, static methods to not be overridden because they are part of the class and not the object.

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

What is inheritance?

A

Inheritance is a mechanism in which one object acquires all the properties and behavior of another object of another class.

Creates IS-A Relationship.

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

Can we have a try without catch block?

A

Yes we can have try-finally

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

How many ways to create string?

A

There are two ways

  • new keyword
  • passing a string literal
24
Q

What is a string literal?

A

Sequence of characters enclosed in double quotation marks

Example: “ Hi”

25
Q

What is a Stringbuffer?

A

The stringbuffer class is used to represent characters that can be modified.

  • SYNCRONIZED
26
Q

What is a StringBuilder?

A

Used to create mutable strings.

-NOT SYNCRONIZED

27
Q

What is a nested class & what are the types ?

A

A class of declared inside another class

There are four types

  • inner class
  • local inner class
  • Anonymous inner class
  • static nested class
28
Q

What is a nested interface?

A

An interface that is declared inside of another interface or class.

29
Q

What does the ‘super’ keyword do?

A

The super keyword has two uses.

Verse: it can be used to access super class methods that have been overwritten

Second: to invoke super classes constructor in the child’s class constructor where super is first argument.

30
Q

What is a Set

A
  • No duplicates
  • Insertion order not preserved
  • At most one null
31
Q

What is a HashSet?

A
  • Duplicates not allowed.
  • Insertion order not preserved.
  • Heterogeneous objects allowed.

No insertion allowed.

Best: SEARCH

32
Q

Stack vs. Heap

A

Whenever a object is created it is always stored in the heap space and a reference is stored in the stack.Stack memory only contains local primitive and reference variables.

33
Q

Name the Cursors

A

Enumeration
- reading

Iteration

  • read
  • remove

ListIterator
-everything

34
Q

LinkedList

A
  • Insertion order preserved
  • duplicates allowed
  • no random access

Best: insertion/deletion in the middle
Worst: retrieval

35
Q

What is an ArrayList?

A
  • Resizable
  • duplicates allowed
  • insertion order preserved
  • Heterogeneous objects allowed
  • random access
36
Q

What is a List?

A
  • Insertion order is preserved
  • duplicates allowed
  • heterogeneous objects allowed
37
Q

What is an Iterator?

A

Universal cursor that supports read/remove

Moves in forward direction

*Object that iterates over objects in a collection

38
Q

What are Generics Bounded type Parameters?

A

They are ways to restrict the object type that is passed using keyword extends or super

39
Q

What are Generics?

A
  • Generics allow object types to be passed as parameters to classes, interfaces, and method declarations.
  • Think template
-objects do not follow subclassing rules 
  Ex:      foo    =subclass of bar
        G != subclass of G
40
Q

What is a Static Block & what is their purpose?

A

Group of statements that gets executed when the class is loaded into memory

  • used to initialize static variables of class
  • used to create static resources
41
Q

What is the difference between & >

A

Bound on both sides
- [ ]

extends Number>
Open on one side
- ( ]

42
Q

What is Autoboxing and Unboxing?

A

Autoboxing:is the automatic conversion made by the Java compiler between the primitive types and their corresponding object wrapper classes.

Unboxing:automatic conversion from object to primitive.

43
Q

What are Bridge Methods?

A

Methods that are created by the compiler as part of the type erasure process.

44
Q

Generic bindings keywords

A

Upper Bounds : extends

Lower Bounds : super

45
Q

What is pass by reference and pass by value ?

A
  • When an object is passed by value this means that a copy of the object is past
  • when an object is passed by reference the reference to an object is past
46
Q

What good are ‘static’ classes?

A

Defining one off,utility or library classes where instantiation would not make sense.

47
Q

What are Wrapper Classes?

A
  • The object representation of eight primitive types in java

- Immutable and final

48
Q

What is the ‘final’ keyword for class/variable/method?

A
Class
- to make sure no other class can extend it

Methods
- so that subclasses can’t override methods in parent classes

Variables
- so that variables can be assigned only once

49
Q

Fail-fast iterator

A

Fail fast iterator, while iterating through the collection will instantly throw ‘concurrent modificationException’ if there is structural modification of the collection

50
Q

Fail-Safe Iterator

A

Fail safe makes copy of internal data structure and iterates over the copied data structure

BAD:

  • overhead of maintaining copy
  • not guaranteed everything copied over correctly
51
Q

Is Java pass by value or reference?

A

Java is pass by value

  • Java does manipulate objects by reference and all variables are references, however java doesn’t pass method arguments by reference, it passes them by value
52
Q

What is an Interface?

A

An interface gives a blueprint for a group of related classes by providing common method signatures(abstract) and variables(by default public,static & final) to implement. This provides default functionality for related classes.

53
Q

What is ‘this’ keyword?

A

Provides reference to the current object and it’s resources (makes sure object variables are used, not local variables)

54
Q

When to use Abstract Class vs. Interface?

A

Interface: if we don’t know anything about implementation we just have requirement specification.

Abstract: if we are talking about implementation but not completely (partial implementation)

55
Q

Purpose of Nested Interface?

A

Used to group related interfaces so that they can be easily maintained.