Java Flashcards

1
Q

List any five features of Java?

A

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

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

Why is Java Architectural Neutral?

A

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

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

How Java enabled High Performance?

A

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

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

Why Java is considered dynamic?

A

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.

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

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

A

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.

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

What is Singleton class?

A

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

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

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

A

An Object is first declared, then instantiated and then it is initialized.

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

When a byte datatype is used?

A

This data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.

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

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

A

Variables used in a switch statement can only be a string, enum, byte, short, int, or char.

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

When parseInt() method can be used?

A

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

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

Why is String class considered immutable?

A

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

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

Why is StringBuffer called mutable?

A

The String class is considered as immutable, so that once it is created a String object cannot be changed. If there is a necessity to make alot of modifications to Strings of characters then StringBuffer should be used.

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

What is the difference between StringBuffer and StringBuilder class?

A

Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects.

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

What is finalize() method?

A

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

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

Explain Runtime Exceptions?

A

It is 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.

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

Which are the two subclasses under Exception class?

A

The Exception class has two main subclasses : IOException class and RuntimeException Class.

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

When throws keyword is used?

A

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.

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

When throw keyword is used?

A

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

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

How finally used under Exception Handling?

A

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.

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

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

A

While creating your own exception −

- 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Define Inheritance?

A

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

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

When super keyword is used?

A

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.

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

What is Polymorphism?

A

Polymorphism is 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 a child class object.

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

What is Abstraction?

A

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.

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

What is Abstract class?

A

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.

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

When Abstract methods are used?

A

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.

27
Q

What is Encapsulation?

A

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.

28
Q

What is the primary benefit of Encapsulation?

A

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.

29
Q

What is an Interface?

A

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

30
Q

Give some features of Interface?

A
  • Interface cannot be instantiated
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
31
Q

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

A

Thread can be created by: implementing Runnable interface, extending the Thread class.

32
Q

Explain garbage collection in Java?

A

It uses garbage collection to free the memory. By cleaning those objects that is no longer reference by any of the program.

33
Q

Define immutable object?

A

An immutable object can’t be changed once it is created.

34
Q

What is Comparable Interface?

A

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.

35
Q

What is function overloading?

A

If a class has multiple functions by same name but different parameters, it is known as Method Overloading.

36
Q

What is function overriding?

A

If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding.

37
Q

Difference between Overloading and Overriding?

A

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.

38
Q

What is final class?

A

Final classes are created so the methods implemented by that class cannot be overridden. It can’t be inherited.

39
Q

What is NullPointerException?

A

A NullPointerException is thrown when calling the instance method of a null object, accessing or modifying the field of a null object etc.

40
Q

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

A

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.

41
Q

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

A

The operating system’s task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.

42
Q

What is the difference between yielding and sleeping?

A

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

43
Q

Why Vector class is used?

A

The Vector class provides the capability to implement a growable array of objects. Vector 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.

44
Q

What are Wrapper classes?

A

These are classes that allow primitive types to be accessed as objects. Example: Integer, Character, Double, Boolean etc.

45
Q

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.

46
Q

What is Serialization and deserialization?

A

Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.

47
Q

Explain the use of subclass in a Java program?

A

Sub class inherits all the public and protected methods and the implementation. It also inherits all the default modifier methods and their implementation.

48
Q

What’s the difference between constructors and other methods?

A

Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.

49
Q

Is there any limitation of using Inheritance?

A

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

50
Q

When is the ArrayStoreException thrown?

A

When copying elements between different arrays, if the source or destination arguments are not arrays or their types are not compatible, an ArrayStoreException will be thrown.

51
Q

What’s the difference between the methods sleep() and wait()?

A

The code sleep(2000); puts thread aside for exactly two seconds. The code wait(2000), causes a wait of up to two second. 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.

52
Q

When ArithmeticException is thrown?

A

The ArithmeticException is thrown when integer is divided by zero or taking the remainder of a number by zero. It is never thrown in floating-point operations.

53
Q

What is a transient variable?

A

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

54
Q

What is synchronization?

A

Synchronization is 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.

55
Q

Does garbage collection guarantee that a program will not run out of memory?

A

Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to 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.

56
Q

What is the difference between an Interface and an Abstract class?

A

An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation.

57
Q

What is the difference between error and an exception?

A

An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. Exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist.

58
Q

What are the advantages of ArrayList over arrays?

A

ArrayList can grow dynamically and provides more powerful insertion and search mechanisms than arrays.

59
Q

Why deletion in LinkedList is fast than ArrayList?

A

Deletion in linked list is fast because it involves only updating the next pointer in the node before the deleted node and updating the previous pointer in the node after the deleted node.

60
Q

How do you decide when to use ArrayList and LinkedList?

A

If you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then LinkedList should be used. If you need to support random access, without inserting or removing elements from any place other than the end, then ArrayList should be used.

61
Q

Describe life cycle of thread?

A

A thread is an execution in a program. The life cycle of a thread include −

  • Newborn state
  • Runnable state
  • Running state
  • Blocked state
  • Dead state
62
Q

Why Generics are used in Java?

A

Generics provide compile-time type safety that allows programmers to catch invalid types at compile time. Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods or, with a single class declaration, a set of related types.

63
Q

What is daemon thread?

A

Daemon thread is a low priority thread, which runs intermittently in the back ground doing the garbage collection operation for the java runtime system.