Sunday, 9 July 2017

Q:16 How To Count The No Of  Same key And Value From Two HashMap.


import java.util.HashMap;
import java.util.Map.Entry;

public class CompareKeyValue
{
int count=0;
boolean flag=false;
public int  commonKeyValuePairs(HashMap<String,String> hm1,HashMap<String,String> hm2)
{
for (Entry<String, String> entry : hm1.entrySet())
 {

for (Entry<String, String> entry1 : hm2.entrySet())
{
              String key=entry1.getKey();
              String key1=entry.getKey();
              String values=entry.getValue();
              String values1=entry1.getValue();
             
             flag=key.equals(key1)&&(values.equals(values1));
             if(flag==true)
               count++;
   
   }



 }
 
return count;
}
public static void main(String[] args)
{

  HashMap<String, String> hm1=new HashMap<>();
hm1.put("abs","salmasn");
hm1.put("abc", "salmdan");
hm1.put("sam","more");
HashMap<String, String> hm2=new HashMap<>();
hm2.put("abs","salman");
hm2.put("abc", "salman");
hm2.put("sam","more");
CompareKeyValue m=new CompareKeyValue();
int count=m. commonKeyValuePairs(hm1, hm2);
System.out.println("The same no.of key and value btween two hashmap are "+count);

}

}

Wednesday, 12 April 2017

q-15 What is output of the following program?
class ReturnValue
{
    public static void main(String[] args)
    {
        System.out.println(methodReturningValue());
    }

    static int methodReturningValue()
    {
        try
        {
            return 49;
        }
        catch (Exception e)
        {
            return 20;
        }
        finally
        {
              System.out.println("finally block");
         }
    }
}


a)Compilation fails b)runtime exception c)20 finally block    d)49 finally block



q-13 how to throw the exception explicitly?


class Test
{
public static void main(String[] args) {
explicitlyCall();
}
public static  void explicitlyCall()
{
try
{
               ArithmeticException ae=new ArithmeticException("ArithmethicException");
       throw ae;
 }
catch(ArithmeticException ae)
{
ae.printStackTrace();
}

}
}

Tuesday, 10 January 2017

Q-12:-Lambda Expression Using List(java 8)

import java.util.*;
class a
{
public static void main(String[] args)
{
List<String> list=new ArrayList<>();
list.add("salman");
list.add("sam");
list.add("karthik");
list.forEach(Items->System.out.println(Items));

}

Friday, 28 October 2016

wa.p to print the following pattern

6543210
543210
43210
3210
210
10
0

ans:-


  class abc
{
public static void main(String[] args)
{
int k;

for(int i=1;i<=7;i++)
{
    int m=8-i;
k=m;


for(int j=1;j<=7;j++)
{


if(j<=m)
{
    int  max=(j<=7)?k--:k++;
System.out.print(k);//1
}
else
{

System.out.print(" ");
}
}
System.out.println("\n");

}



}
}

Friday, 2 September 2016

Q-12:-can we develop java application without any taking any userdefined class defination?

ANS-
possible  by taking user defined enum
FOR EX:-
public enum myenum
{
  a;
public static void main(String[] args)
{
   System.out.println("welcome");
}
}

Sunday, 28 August 2016

Q-10 find the output of the following program code.


class demo
{
   public static void main(String[] args)
{
  System.out.println(null);
}
}
1) NULL 2) C.T.E 3) NULL POINTER EXCEPTION 4) RUN TIME ERROR

ANS:-C.T.E 

REASON:-REFERENCED TO println METHOD IS AMBIGUOS BOTH METHOD println (char a[]) in printstream & method println(String) in printstream match.