SCJP 1.4 Mock Exam -1
Q1)What will be the output?
public 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]); } } }
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)What will be the output?
public class Test3{ public void method(){ System.out.println("Called"); } public static void main(String[] args){ method(); } }
A1 “Called”
A2 Compiler
A3 Runtime
A4 Nothing
Q4)What will be the output?
public class Test4{ public static void method(){ System.out.println("Called"); } public static void main(String[] args){ Test4 t4 = null; t4.method(); } }
A1 “Called”
A2 Compiler
A3 Runtime Exception
A4 Nothing is printed in screen
Q5)What will be the output?
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(); } }
A1 “Constructor1”
A2 “Constructor2”
A3 “Constructor1″”Constructor2”
A4 Compiler Errror
Q6)What will be the output?
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(); } }
A1 4
A2 4 4
A3 Compiler Error
A4 Compiles and Runs without error
Q7)What will be the output?
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); } }
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 keywaord 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 public void className()
A3 private className()
A4 static className()
Q14)Which of the following modifiers can be used with top class declaration?
A1 static
A2 privatr
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];
Q16)What 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
Q17)What 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
Q19)What 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
Q20)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); } } 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?
A1 String
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;
Q24)String s1 = new String(“hi”);
String s2 = "hi"; 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.
Q25)Integer i = new Integer(0);
Float f = new Float(0); System.out.println(i==f); System.out.println(i.equals(f));
A1 true false
A2 false true
A3 true true
A4 Compiler error
SCJP 1.4 Mock Exam -1 Answers
1) A1 is correct
Local Array variables are initialized to their default values.
2) 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.
3) 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();
4) A1) “Called”
Static 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.
5) A2)”Constructor2″
Constructors cannot have return types. If its declared with return types then it is consider as methods. So, output wull be “Constructor2”.
6) A3)Compiler Error
7) A3)Hai Hi
8) A1)public Test8(){}
A3)protected Test8(int k){}
9) A3)final void method(){}
A4)int method(){}
10) A1) class Test10
A2) public Test10
A3) final Test10
11) b)variable
12) c)final static class()
d)public final strictfp class{}
13) b)public className()
c)private className()
14) c)public
d)final
e)abstract
15) b)int arr[][] = new int [10][10];
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
21) A1)String
A4)String Buffer
22) A4)None of the Above
23) A3)float f = 1.2;
24)A1) false true
25)A4)Compiler error