You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+290Lines changed: 290 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3320,18 +3320,308 @@ The Object class is the parent class of all the classes in java by default.
3320
3320
</div>
3321
3321
3322
3322
#### Q. What is copyonwritearraylist in java?
3323
+
*`CopyOnWriteArrayList` class implements `List` and `RandomAccess` interfaces and thus provide all functionalities available in `ArrayList` class.
3324
+
* Using `CopyOnWriteArrayList` is costly for update operations, because each mutation creates a cloned copy of underlying array and add/update element to it.
3325
+
* It is `thread-safe` version of ArrayList. Each thread accessing the list sees its own version of snapshot of backing array created while initializing the iterator for this list.
3326
+
* Because it gets `snapshot` of underlying array while creating iterator, it does not throw `ConcurrentModificationException`.
3327
+
* Mutation operations on iterators (remove, set, and add) are not supported. These methods throw `UnsupportedOperationException`.
3328
+
* CopyOnWriteArrayList is a concurrent `replacement for a synchronized List` and offers better concurrency when iterations outnumber mutations.
3329
+
* It `allows duplicate elements and heterogeneous Objects` (use generics to get compile time errors).
3330
+
* Because it creates a new copy of array everytime iterator is created, `performance is slower than ArrayList`.
3331
+
* We can prefer to use CopyOnWriteArrayList over normal ArrayList in following cases:
3332
+
- When list is to be used in concurrent environemnt.
3333
+
- Iterations outnumber the mutation operations.
3334
+
- Iterators must have snapshot version of list at the time when they were created.
3335
+
- We don’t want to synchronize the thread access programatically.
ait.remove(); //mutable operation 'remove' allowed without any exception
3366
+
}
3367
+
```
3368
+
3323
3369
#### Q. How do you test static method?
3324
3370
#### Q. How to do you test a method for an exception using JUnit?
3325
3371
#### Q. Which unit testing libraries you have used for testing Java programs?
3326
3372
#### Q. What is the difference between @Before and @BeforeClass annotation?
3327
3373
#### Q. Can you explain Liskov Substitution principle?
3374
+
Liskov Substitution principle (LSP) states that **sub/child/derived-classes should be substitutable for their base/parent-classes.**
3375
+
3376
+
Given a class B is subclass of class A , we should be able to pass an object of class B to any method that expects(takes as an argument/parameter) an object of class A and the method should not give any weird output in that case.
3377
+
3378
+
`ClientTestProgram` class has a method `playVideoInAllMediaPlayers()` which accepts list of all `MediaPlayer` objects and plays video for each , but method fails at `WinampMediaPlayer` ? Let's check whether it satisfies **LSP**.
Now, in `ClientTestProgram` , instead of creating list of type `MediaPlayer`, we will create list of `VideoMediaPlayer` type that should give us compile time error at statement `allPlayers.add(new WinampMediaPlayer()); ` as `WinampMediaPlayer` isnt subclass of `VideoMediaPlayer`.But in case of `DivMediaPlayer` and `VlcMediaPlayer` they are substitutable for their parent class as seen in `playVideoInAllMediaPlayers()` method
3492
+
that satisefies *Liskov's substitution principle*.
3493
+
3328
3494
#### Q. Give me an example of design pattern which is based upon open closed principle?
3329
3495
#### Q. What is Law of Demeter violation? Why it matters?
3330
3496
#### Q. What is differences between External Iteration and Internal Iteration?
3497
+
3331
3498
#### Q. What are the different access specifiers available in java?
3499
+
* access specifiers/modifiers helps to restrict the scope of a class, constructor, variable, method, or data member.
3500
+
* There are four types of access modifiers available in java:
3501
+
1.`default` – No keyword required, when a class, constructor,variable, method, or data member declared without any access specifier then it is having default access scope i.e. accessible only within the same package.
3502
+
2.`private` - when declared as a private , access scope is limited within the enclosing class.
3503
+
3.`protected` - when declared as protocted, access scope is limited to enclosing classes, subclasses from same package as well as other packages.
3504
+
4.`public` - when declared as public, accessible everywhere in the program.
publicint getId(){ returnthis.id; } /* public scope */
3519
+
...
3520
+
.../* inner classes */
3521
+
classA{} /* default scope */
3522
+
protectedclassB{} /* protected scope */
3523
+
privateclassC{} /* private scope */
3524
+
publicclassD{} /* public scope */
3525
+
...
3526
+
```
3527
+
3332
3528
#### Q. What is runtime polymorphism in java?
3529
+
**Runtime polymorphism** or **Dynamic Method Dispatch** is a process in which a call to an overridden method is resolved at runtime rather than compile-time.
3530
+
3531
+
An overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred.
3532
+
```java
3533
+
classBank{
3534
+
publicfloat roi=0.0f;
3535
+
floatgetRateOfInterest(){returnthis.roi;}
3536
+
}
3537
+
classSBIextendsBank{
3538
+
float roi=8.4f;
3539
+
floatgetRateOfInterest(){returnthis.roi;}
3540
+
}
3541
+
classICICIextendsBank{
3542
+
float roi=7.3f;
3543
+
floatgetRateOfInterest(){returnthis.roi;}
3544
+
}
3545
+
classAXISextendsBank{
3546
+
float roi=9.7f;
3547
+
floatgetRateOfInterest(){returnthis.roi;}
3548
+
}
3549
+
3550
+
Bank b;
3551
+
b=newSBI();
3552
+
System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());
3553
+
3554
+
b=newICICI();
3555
+
System.out.println("ICICI Rate of Interest: "+b.getRateOfInterest());
3556
+
3557
+
b=newAXIS();
3558
+
System.out.println("AXIS Rate of Interest: "+b.getRateOfInterest());
3559
+
3560
+
System.out.println("Bank Rate of Interest: "+b.roi);
3561
+
3562
+
/**output:
3563
+
SBI Rate of Interest: 8.4
3564
+
ICICI Rate of Interest: 7.3
3565
+
AXIS Rate of Interest: 9.7
3566
+
Bank Rate of Interest: 0.0
3567
+
3568
+
//you might think it should be 9.7 , as recent object being refered to is of AXIS but method is overridden, not the data members, so runtime polymorphism can't be achieved by data members/instance variables.
3569
+
**/
3570
+
```
3333
3571
#### Q. What is a private constructor?
3572
+
* A constructor with private access specifier/modifier is private constructor.
3573
+
* It is only accessible inside the class by its data members(instance fields,methods,inner classes) and in static block.
3574
+
* Private Constructor be used in **Internal Constructor chaining and Singleton class design pattern**
3575
+
```java
3576
+
publicclassMyClass {
3577
+
3578
+
static{
3579
+
System.out.println("outer static block..");
3580
+
newMyClass();
3581
+
}
3582
+
3583
+
privateMyInner in;
3584
+
3585
+
{
3586
+
System.out.println("outer instance block..");
3587
+
//new MyClass(); //private constructor accessbile but bad practive will cause infinite loop
0 commit comments