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?