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?
A1 abstract method(){}
A2 abstrct void method(){}
A3 final void method(){}
A4 int method(){}


Q10 Which of the following are valid top-level class declarations?
A1 class Test10
A2 public class Test10
A3 final class Test10
A4 abstract final class Test10

Q11 transient keywords can be used with?
A1 method
A2 variable
A3 class
A4 constructor

Q12 which of the following are valid combinations for class declaration?
A1 abstract final class Test12{}
A2 abstract static class Test12{}
A3 final static class Test12{}
A4 public final strictfp class Test12{}
Q13 which of the following are valid constructor signatures?

A1 public void className()
A2 private className()
A3 static className()
A4 public void className()
Q14 Which of the following modifiers can be used with top class declaration?

A1 static
A2 private
A3 public
A4 final
A5 abstract

Q15 Which of the following are valid array declarations?

A1 int arr[] = new int[];
A2 int arr[][] = new int [10][10];
A3 float arr[][] = new float[][10];
A4 float arr[] = new float[10];

Q16What will be the output of the following program?

public class Test1
{
static{
 System.out.println("Static");
}
{ System.out.println("Instance");}
public void Test1(){
 System.out.println("Constructor");
}
public static void main(String[] args)
{ Test1 t = null; }
}
A1 Instance Static
A2 Static Instance
A3 Static
A4 Static Instance Constructor

Q17What will be the output of the following program?

class Sup{
public Sup(String str)
{
System.out.println("Super class"); }}


public class Test2 extends Sup{
public Test2(){

 System.out.println("Sub class"); }
public static void main(String[] args)
{
 Test2 t2 = new Test2(); }}


A1 Super class,SubClass
A2 Super class
A3 Sub class
A4 Compiler Error


Q18 What will be the output of the following program?
public class Test3 {
public static void main(String[] args) {
System.out.println("Main Method1");
}
public static void main(String args){
System.out.println("Main Method2"); } }

A1 Main Method1
A2 Main Method1 Main Method2
A3 Main Method2
A4 Runtime Exception


Q19What will be the output of the following program?

public class Test4 {
public static void main(String args) {
System.out.println("Sample Program"); }
}

A1 Sample Program
A2 Compiler Error
A3 Runtime Exception
A4 None
What will be the output of the following program?

class Sup1{
public Sup1()
{
 System.out.println("Hai"); }
private Sup1(String str)
{
 System.out.println(str); }
}


Q20

public class Test5 extends Sup1{

 private Test5(String str)
{
System.out.println(str);
super();

}
public static void main(String[] args)
{
Test5 t5 = new Test5("HI"); }}


A1 Hai,Hi,Hi
A2 Hai,Hi
A3 Hi,Hi
A4 Compiler Error


Q21 Which of the following are not a wrapper class?
String
A1 ing
A2 Integer
A3 StringBuffer A4 Boolean

Q22 Select the correct syntax for main method :
A1 public void main(String args[])
A2 public static void main(String args)
A3 public static void Main(String args[])
A4 None of the Above

Q23 Which of the following are not a valid declarations?
A1 float f = 1;
A2 float f = 1.2f;
A3 float f = 1.2;
A4 float f = (float)1.2;
String s1 = new String("hi");
String s2 = "hi";


Q24 System.out.println(s1 ==s2);
System.out.println(s1.equals(s2));
A1 false true
A2 true false
A3 true true
A4 None of the above.

Integer i = new Integer(0);
Float f = new Float(0);
System.out.println(i==f);


Q25 System.out.println(i.equals(f));

A1 true false
A2 false true
A3 true true
A4 Compiler error




Answers 

A1 is correct 
Local Array variables are initialized to their default values. 


A3)double []arr = new double[10]; 
A1 is not valid because (new int[]) array size must be specified unless it is annonymous 
array declaration. 
A2 is not valid because array declaration must not specify the size of the array.


A2) Compiler Error 
non-static(instance) methods or variables are cannot be referenced from static 


context.Compiler will give the following error while compiling the above code: 
Test3.java:6: non-static method method() cannot be referenced from a static context 
method(); 

A1) "Called"
 4Static methods canbe called only by referance.Because its not binded with objects.So, in 
this case "null" value doesn't make sense."method" is called without any problem. 

A2)"Constructor2" 

Constructors cannot have return types. If its declared with return types then it is consider 
as methods. So, output wull be "Constructor2". 

A3)Compiler Error 

7 A3)Hai Hi 

A1)public Test8(){} 
A3)protected Test8(int k){} 

A3)final void method(){} 
A4)int method(){} 

A1) class Test10 
10 A2) public Test10 
A3) final Test10 

11 b)variable 

c)final static class(
)
d)public final strictfp class{


b)public className()

13 

c)private className() 

c)public 
14 d)final 
e)abstract 

b)int arr[][] = new int [10][10];

15 

d)float arr[] = new float[10]; 

16 c)Static 

17 d)Compiler Error 

18 a)Main Method1 

19 c)Runtime Exception 

20 d)Compiler Error 

21A1)String 
 A4)String Buffer


22 A4)None of the Above 

23 A3)float f = 1.2; 

24 A1) false true 
25 A4)Compiler error