Exam Flashcards

(30 cards)

1
Q

Give three similarities and three differences between an ArrayList and a linkedList

A

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

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

Collection framework indicating two advantages of it use?

A

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 .

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

What is Java collection?

A

Are used in every programming language contained few classes for collections: vector , stack , array

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

Collection

A

Is the root of the collection hierarchy , represents a group of objects known at its elements

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

Set

A

Set is a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets.

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

List

A

Is a an ordered collection and can contain duplicate elements

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

Map

A

Is an object that maps keys to values. A map cannot contain duplicate keys.

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

Polymorphism and dynamic method binding?

examples

A

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();
   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Scope

A

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.

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

Interfaces

A

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.

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

Implementation

A

Means that it takes on the designated behaviour that the interface specifies

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

Abstract and concrete classes differences

A

Abstract is a concept that you can not always see or touch. Concrete is something that can be seen or known

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

Algorithms

A

Algorithms are defined as static methods the collections class.

Example :

Static void copy(List list1, List list2)
Copies the elements of list2 to list1

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

Relationship between interfaces and implementation

A

Red

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

TreeSet and Hashset

A

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.

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

Checked and unchecked Exceptions in java, Two examples of each.

A

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.

17
Q

how exceptions are handled in java.

A

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.

18
Q

exceptions

A

occurs during the execution of the program…

19
Q

abstract vs interface

A
  1. one abstract class at a time
  2. interface can extend any number of interfaces at a time
  3. abstract class can have protected , public and public abstract methods.
  4. interface can have only public abstract methods i.i.e by default
  5. abstract class can have static, final variable with any access specifier
  6. interface can have only static final variable.
  7. abstract class can have both abstract and concrete methods
  8. interface can have only abstract methods.
20
Q

Serializable why?

A

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?

21
Q

Packages

A

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.

22
Q

Class

A

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.

23
Q

Objects

A

exhibit the properties and behaviors defined by its class

24
Q

method

A

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.

25
variables | give example
that holds values that are used in a java program. Every variable must be declared to use a data type. Example int myAge = 21; The variable "myAge" is declared to be an int data type and initialized to a value of 21.
26
Constants | give example
a constant is a variable whose value cannot change once it has been assigned. Example Add the final keyword to declare an int variable as a constant final int DAYS_IN_JANUARY =31;
27
Encapsulation
Nb To encapsulate something means to wrap it up so code is encapsulated in a class, so it is hide something as well, because it can be hidden in the class. You get it because there is a published interface the API and true encapsulation that is where you use the key word private, so attribute are kept private and that is encapsulation , so you cannot get at the attribute directly.
28
Inheritance
Nb
29
Abstraction
Nb
30
A map does not implement the collection interface | What does that mean?
It doesn't have a method that you will find in an array list or link list, so it is separate to that, again it is an interface it does have some sort of method