Exam Flashcards
(30 cards)
Give three similarities and three differences between an ArrayList and a linkedList
Differences
ArrayList :
Structure = is index based structure in Java
Capacity = is created with initial capacity of 10
When to use = use ArrayList when get operations is more frequent than add and remove operations in Java
LinkedList :
Structure = is a data structure consisting of a group of nodes
Capacity = linkedList’s initial capacity is 0, it’s size grow with addition of each and every element in Java.
When to use = use linkedList when add and remove operations are more frequent than get operations in Java
Similarity :
ArrayList and linkedList both are not synchronized in Java
Array list and linkedList both allows to store null in Java
Array list and linkedList both are implementation of the Java.util.List interface
Collection framework indicating two advantages of it use?
Reduced development effort by using core collection classes rather than implementing our own collection classes.
Code quality is enhanced with the use of well tested collections framework classes .
What is Java collection?
Are used in every programming language contained few classes for collections: vector , stack , array
Collection
Is the root of the collection hierarchy , represents a group of objects known at its elements
Set
Set is a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets.
List
Is a an ordered collection and can contain duplicate elements
Map
Is an object that maps keys to values. A map cannot contain duplicate keys.
Polymorphism and dynamic method binding?
examples
Poly:
Ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes.
Example:
Given a base class shape , polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles.
Dynamic :
The method being called upon an object or the function being called with arguments is looked up by name at runtime.
Here Human is a super class and Boy is a child class since Boy extends Human. Both these classes have a same method void walk().
package beginnersbook.com; class Human{ public void walk() { System.out.println("Human walks"); } } class Boy extends Human{ public void walk(){ System.out.println("Boy walks"); } public static void main( String args[]) { //Reference is of parent class Human myobj = new Boy(); myobj.walk(); } }
Scope
Scope refers to the lifetime and accessibility of a variable. How large the scope is depends on where a variable is declared. For example, if a variable is declared at the top of the class then it will accessible to all of the class methods. If it’s declared in a method then it can only be used in that method.
Interfaces
Objects define their interaction with the outside world through the methods that they expose. Methods form the object’s interface with the outside world; the buttons on the front of your TV set , for example , are the interface between you and the electrical wiring on the other side of its plastic casing. You press the power button to turn the tv on and off.
Implementation
Means that it takes on the designated behaviour that the interface specifies
Abstract and concrete classes differences
Abstract is a concept that you can not always see or touch. Concrete is something that can be seen or known
Algorithms
Algorithms are defined as static methods the collections class.
Example :
Static void copy(List list1, List list2)
Copies the elements of list2 to list1
Relationship between interfaces and implementation
Red
TreeSet and Hashset
Hashset stores the object in random order.
HashSet can store null object while TreeSet does no allow null object.
HashSet is much faster than TreeSet.
HashSet uses equals() method for comparison in java while TreeSet uses compareTo() method for maintaining ordering.
Checked and unchecked Exceptions in java, Two examples of each.
differences : is that the checked exceptions are checked at compile-time while unchecked exceptions are checked at runtime.
checked exceptions means if a method is throwing a checked exception then it should handle the exception using try-catch block.
unchecked exceptions if your program is throwing an unchecked exception and even if you didn’t handle/declare that exception, the program won’t give a compilation error.
how exceptions are handled in java.
A Java program can use the exception-handling mechanism to deal with program-specific errors in a clean manner. A program simply uses the throw statement to signal an exception.
exceptions
occurs during the execution of the program…
abstract vs interface
- one abstract class at a time
- interface can extend any number of interfaces at a time
- abstract class can have protected , public and public abstract methods.
- interface can have only public abstract methods i.i.e by default
- abstract class can have static, final variable with any access specifier
- interface can have only static final variable.
- abstract class can have both abstract and concrete methods
- interface can have only abstract methods.
Serializable why?
to serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object.
Serialization is simply turning an existing object into a byte array.
why?
Communication: if you have two machines that are running the same code,and they need to communicate an easy way is for one machine to build an object with information that it would like to transmit, and then serialize that object to the other machine.
where?
Packages
is a technique for organizing java classes into namespaces similar to the modules of modula. java packages can be stored in files called JAR files.
Class
is nothing but a blueprint for creating different objects which defines its properties and behaviours. A class can contain fields and methods to describe the behavior of an object.
Objects
exhibit the properties and behaviors defined by its class
method
a method is a set of code which is referred to by name and can be called at any point in a program simply by utilizing the method’s name.