Tuesday, January 25, 2011

JAVA Mock exam test paper with 25 most important and frequently asked questions with answers and explanation

 QUESTIONS

Q1public class Test1{

public static void main(String[] args)
{int arr[] = new int[3]; 
for(int i = 0;i < arr.length;i++){

System.out.println(arr[i]); 
}
 } }

What will be the output?
A1 0 0 0
A2 ArrayIndexoutOfBoundsException
A3 NullPointerException
A4 null null null


Q2 Which of the following are valid array declarations?
A1 int arr[] = new int[];
A2 float arr[10] = new fl
A3 double []arr = new double[10];
A4 None Of the Above.

Q3 public class Test3{
public void method(){
 System.out.println("Called");
}
public static void main(String[] args){
method();
}
}

What will be the output?
A1 "Called"
A2 Compiler
A3 Runtime
A4 Nothing

Q4 public class Test4{


public static void method(){
System.out.println("Called");

}


public static void main(String[] args)
{ Test4 t4 = null;
t4.method();
} }


What will be the output?

A1 "Called"

A2 Compiler

A3 Runtime Exception

A4 Nothing is printed in screen

Q5 public class Test5{
public void Test5()
{

System.out.println("Constructor1");
}
public Test5(){}
System.out.println("Constructor2");
public static void main(String[] args)
{
Test5 t5 = new Test5(); } }


What will be the output?
A1 "Constructor1"
A2 "Constructor2"
A3 "Constructor1""Constructor2"
A4 Compiler Errror

Q6 public class Test6{
public Test6()
{
this(4);
}
public Test6(byte var)
{


}
System.out.println(var);
public static void main(String[] args)
{
Test6 t6 = new Test6();
} }


What will be the output?
A1 4
A2 4 4
A3 Compiler Error
A4 Compiles and Runs without error

Q7 public class Test7{
public Test7(){}
public Test7(Test7 ref){
this (ref,"Hai");
}
public Test7(Test7 ref,String str)
{
 ref.Test7(str);
 System.out.println("Hi");
}
public void Test7(String str)
{ System.out.println(str); }
public static void main(String[] args){
Test7 t = new Test7();
Test7 t7 = new Test7(t);
 }
}

What will be the output?
A1 HI
A2 hai
A3 Hai Hi
A4 Hi Hai

Q8 Which of the following are valid Constructors?
A1 public Test8(){}
A2 private void Test8(){}
A3 protected Test8(int k){}
A4 Test8(){}

Q9 Which of the following are valid method declarations?

Tuesday, December 14, 2010

Embed Image in email to avoid enabling downlod/display image in outlook, Gmail and other

There are scenarios when we need to send automated mail through our code with an image. Most of the time user has to click on the "display image" or "download image" to view the image in an email.

So, the better way to add the image into an email is to embed it in the email body. Beolw is the steps by which we can achieve this.

1. Following two namespace is to be used.
using System.Net.Mime;
using System.Net.Mail;


We will use "LinkedResource" class available in System.Net.Mail for representing embedded external resource.
To create the object of LinkedResource we need the content of the embedded resource as stream or need the path of that resource and then it is added to the HTML part of AlternateView's LinkedResources collection.

To create an embedded image we will need to first create an HTML view of the email using AlternateView class. Within that alternate view we need to create a tag that points to the ContentId (cid) of the LinkedResource of the image view as shown below:

//creating LinkedResource object with passing the path to the image to its constructor.
LinkedResource lrImage = new LinkedResource(Server.MapPath("images") + "\\" + "newsletter.gif");

//Set the content ID for the LinkedResouce object
        lrImage.ContentId = "newsletter";       

        string strmail = "";
        strmail = strmail + "";
        strmail = strmail + "";       
        strmail = strmail + "";
        strmail = strmail + "";
        strmail = strmail + "
";

 //add the image at the desired place using contentId(cid). BY default linkedResource ContentId is used    as cid:contentId. In our case the content id is "newsletter" so the image source would be the way as written below:
        strmail = strmail + "";
        strmail = strmail + "
";
        strmail = strmail + "";

// Now we need to create a Alternate view for the strmail.

 AlternateView av = AlternateView.CreateAlternateViewFromString(strmail, null, MediaTypeNames.Text.Html);
//now add the lrImage LinkedResource object to Alternate view
av.LinkedResources.Add(lrImage);

Now our "strMail" is ready to be send as email body.