Interview_Programms
Interview_Programms
==================================
Note:=>There are two forms of singleton design pattern
Early Instantiation: creation of instance at load time.
ex: private static A obj=new A(); //Early, instance will be created at load time
Static member: It gets memory only once because of static, it contains the instance
of the Singleton class.
Private constructor: It will prevent to instantiate the Singleton class from
outside the class.
Static factory method: This provides the global point of access to the Singleton
object and returns the instance to the caller.
package com.intellect;
private SingleToneClass()
{
// constructor
}
return obj;
OP:
---
this is my function called
HashCode 1 : 705927765
_____________________________
this is my function called
HashCode 2 : 705927765
_____________________________
this is my function called
HashCode 3 : 705927765
_____________________________
package com.intellect;
public final class ImutableString {
OP:
---
366712642
Id : 102Name :Badri lal
Old Object return
705927765
Id : 101Name :vivek ramteke
package com.app;
int id;
String name;
}
// equals overriding
@override
public boolean equals(Object obj)
{
if(obj==this)
return true;
// toString() overriding
public String toString()
{
return "id : "+id +" Name : "+name;
}
public static void main(String[] args) {
Employee obj= new Employee(123,"BL");
Employee obj2= new Employee(123,"BL");
System.out.println("HC : "+obj.hashCode());
System.out.println(obj);
System.out.println("Equals : "+ obj.equals(obj2));
System.out.println("HC : "+obj2.hashCode());
System.out.println(obj2);
System.out.println("Equals : "+obj2.equals(obj));
}
}
ex:2
class InvalidAgeException extends Exception{
InvalidAgeException(String s){
super(s);
}
}
class TestCustomException1{
ex3:
class MyException extends Exception
{
public MyException(String msg)
{
super(msg);
}
}
public class CustomException {
}
else
{
System.out.println("Welcome user : "+pin);
}
}
public static void main(String[] args)
{
try{
pin(1234);
}
catch(MyException ex)
{
System.out.println("Exception catched :-"+ex.getMessage());
}
}
ex4 :
package com.intellect;
class Employee
{
}
class EmpNotFoundException extends Exception
{
EmpNotFoundException(String msg)
{
super(msg);
}
}
class EmpManager
{
public Employee find(String EmpId) throws EmpNotFoundException
{
if(EmpId.equals("12345"))
{
try{
System.err.print(e);
}
OP:
---
Employee object is created with id 12345
or
for(String city : hsmp2.values())
{
System.out.println(city);
}
or
4: forEach(action) method :
-----------------------------
hsmp2.forEach((k,v) -> System.out.println(k +" "+v));
import java.util.ArrayList;
package com.intellect;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
System.out.println("al :"+al);
al.clear();
al.addAll(hashset);
System.out.println("al"+al);
package com.intellect;
import java.util.ArrayList;
import java.util.Collections;
int rollno;
String name;
int age;
@Override
public String toString() {
return "Student [rollno=" + rollno + ", name=" + name + ", age=" + age
+ "]";
}
@Override
public int compareTo(Student o) {
//Acending
return this.age-o.age;
//Descending
//return -this.age-o.age
}
public class ComparableSorting {
Collections.sort(al);
for(Student st:al)
{
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
op:
===
111 Monu 30
101 Badri lal 29
105 Nagendra 27
EXAMPLE 2:
==========
package com.intellect;
import java.util.ArrayList;
import java.util.Collections;
int Salary;
@Override
public int compareTo(Employee o)
{
//Ascending
//return this.Salary-o.Salary;
//Descending
return -this.Salary-o.Salary;
//on name : return -this.getName().compareTo(o.getName());
}
public class ComparableSoring {
Collections.sort(e);
for(Employee e1: e)
{
System.out.println(e1.name+" "+e1.Salary);
}
/*package com.intellect;
import java.util.ArrayList;
import java.util.Collections;
int Salary;
}
public class ComparableSoring {
Collections.sort(e);
for(Employee e1: e)
{
System.out.println(e1.name+" "+e1.Salary);
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
String name;
String Dept;
int salary;
public Employee(int id, String name, String dept, int salary) {
super();
this.id = id;
this.name = name;
Dept = dept;
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", Dept=" + Dept + ",
salary=" + salary + "]";
}
}
@Override
public int compare(Employee e1, Employee e2) {
return e1.salary-e2.salary;
}
}
//Dept comparator class
class DeptComparator implements Comparator<Employee>
{
@Override
public int compare(Employee e1, Employee e2)
{
return e1.Dept.compareTo(e2.Dept);
}
}
public class ComparatorSorting {
//sorting by salary
Collections.sort(obj, new salaryComparator());
//sorting by dept
Collections.sort(obj, new DeptComparator());
COMPARATOR
------------
COMPARATOR
package com.intellect;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
//Employee class with setter/getter/toStirng
class Employee
{
int id;
String name;
String Dept;
int salary;
public Employee(int id, String name, String dept, int salary) {
super();
this.id = id;
this.name = name;
Dept = dept;
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", Dept=" + Dept + ",
salary=" + salary + "]";
}
@Override
public int compare(Employee e1, Employee e2) {
return e1.salary-e2.salary;
}
}
//Dept comparator class
class DeptComparator implements Comparator<Employee>
{
@Override
public int compare(Employee e1, Employee e2) {
// TODO Auto-generated method stub
return e1.Dept.compareTo(e2.Dept);
}
}
public class ComparatorSorting {
}
Q : TO FIND PALINDROME NO BW RANGE. ?
--------------------------------------
package com.intellect;
Description:
Sort or order a HashMap or TreeSet or any map item by value. Write a comparator
which compares by value, not by key. Entry class might hleps you here.
package com.java2novice.algos;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
Output:
Why this kolavari ==== 93
Unix ==== 67
C++ ==== 45
MAC ==== 26
java ==== 20
Java2Novice ==== 2
C : 1
E : 2
W : 1
L : 1
M : 1
SOL:
public class Duplicate_Char {
//set
Set<Character> set =hm.keySet();
for(Character ch : set)
{ //TO PRINT ONLY DUPLICATE USE IF
CONDITION
// if(hm.get(ch)>1)
System.out.println(ch +" : "+hm.get(ch)); <-// this print all char
with accurance
}
}
findDuplicate("WELCOME");
java: 2
php : 1
, : 3
parl: 1
PROG:
package com.app;
import java.util.HashMap;
import java.util.Set;
or
==
or
2: //
charCount = 0;
for(char ch: input.toCharArray())
{
if(ch == 'a')
{
charCount++;
}
}
System.out.println("count of character 'a' on String: 'Today is Monday'
using for each loop " + charCount);
int count = 1;
}
}
System.out.println("Number of words in a string = " + count);
}
}
{
if(words[i]!=null)
{
}
}
}
removeDeplicate(str);
}
------------------
import java.util.Scanner;
}
}
or
public static String reverse(String source)
{
if(source==null || source.isEmpty())
{
return source;
}
String reverse = " ";
for(int i=source.lentgh() -1; i>=0 ; i--)
{
reverse = reverse + reverse.charAt(i);
}
ruturn reverse ;
}
System.out.println("Entet String");
Scanner sc = new Scanner(System.in);
String text =sc.nextLine();
wordReverse(text);
}
}
}
System.out.println("Entet String");
Scanner sc = new Scanner(System.in);
String text =sc.nextLine();
wordReverse(text);
}
or
class CommonElements
{
public static void main(String[] args)
{
Integer[] i1 = {1, 2, 3, 4, 5, 4};
set1.retainAll(set2);
Description:
You have got a range of numbers between 1 to N, where one of the number is
repeated. You need to write a program to find out the duplicate number.
package com.java2novice.algos;
import java.util.ArrayList;
import java.util.List;
int sum = 0;
for(int num:numbers){
sum += num;
}
return sum;
}