You are on page 1of 10

Java language Fundamentals:

1. Select all correct list of keywords or Java reserved words? [a] superclass [b] goto [c] open [d] integer [e] They are all java keywords Answer 1

2. Select all valid identifiers from the following? [a] 1hello [b] _hello [c] *hello [d] #hello Answer 2

3. What is the correct order in list given below? [a] package, import, class [b] class, import, package [c] import, package, class [d] package, class, import Answer 3

4. Select the correct anonymous inner class declaration ?

[a] new Outer.new Inner [b] new Inner() { } [c] new Inner() [d] Outer.new Inner()

Answer 4

5. Select all the correct choices from the following statements? [a] A static modifier may not be applied to an inner class [b] An inner class can have explicit constructors [c] An inner class may implement an interface, or extend other classes [d] Anonymous inner classes must be instantiated at the same point they are defined.

Answer 5

6. What is the result of compiling the following code? public class Test { public static void main ( String[] args) { int value; value = value + 1; System.out.println(" The value is : " + value); } } [a] Compile and runs with no output [b] Compiles and runs printing out "The value is 1" [c] Does not compile [d] Compiles but generates run time error Answer 6

7. What is the result of compiling the following code? When you run like given below? java Test Hello How Are You public class Test { public static void main ( String[] args) { for ( int i = 0; i < args.length; i++) System.out.print(args[i]); } } [a] Compile and runs with no output [b] Compiles and runs printing out HelloHowAreYou [c] Does not compile [d] Compiles but generates run time error Answer 7

8. Which of the following is not a Java keyword or reserved word? [a] goto [b] synchronized [c] extends [d] implements [e] this [f] NULL Answer 8

9. Which of the following are Java keywords ?

[a] class [b] transient [c] native [d] Double [e] switch Answer 9

10. What is the result when you compile and run the following code? public class TestMember { String s ; int intStr ; public static void main(String args[]) { TestMember t = new TestMember(); System.out.print(t.s + " " + t.intStr); } } [a] A null is printed [b] Compile time error [c] Nothing is printed [d] null followed by 0 is printed on the screen 11. What is the result when you compile and run the following code? public class TestLocal { public static void main(String args[]) { String s ; int intStr ; System.out.print(s + " " + intStr); } } [a] A null is printed [b] Compile time error [c] Nothing is printed [d] null followed by 0 is printed on the screen Answer 11

12. What is the result when you compile and run the following code? public class TestLocal { public static void main(String args[]) { String s[] = new String[6]; System.out.print(s[6]); } } [a] A null is printed [b] Compile time error [c] Exception is thrown [d] null followed by 0 is printed on the screen Answer 12

13. Which of the declaration following compile correctly? [a] double d = 10.2f; [b] float f = 10L; [c] byte b = 10.0d; [d] byte b1 = 155; Answer 13

14. Which of the declaration following compile correctly? [a] String s = "Test"; [b] float f = 10L; [c] byte b = true; [d] boolean b1 = false; Answer 14

15. Which of the declaration following compile correctly? [a] double d = (byte)15.0d; [b] long k = 'A'; [c] byte b = 10.0d; [d] byte b1 = 155; Answer 15

16. Which of the following assignment statements are invalid? [a] long l = 698.65; [b] float f = 55.8; [c] double d = 0x45876; [d] All of the above Answer 16

17. What is the numeric range for a Java int data type? [a] 0 to (2^32) [b] -(2^31) to (2^31) [c] -(2^31) to (2^31 - 1) [d] -(2^15) to (2^15 - 1) Answer 17 18. What is the result when you compile and run the following code?

int i = 100; switch (i) { case 100: System.out.println(i); case 200: System.out.println(i); case 300: System.out.println(i); } [a] Nothing is printed [b] Compile time error [c] The values 100,100,100 printed [d] Only 100 is printed Answer 1

19. What is the result when you compile and run the following code? int i = 100; switch (i) { case 100: System.out.println(i); break; case 200: System.out.println(i); break; case 300: System.out.println(i); break; } [a] Nothing is printed [b] Compile time error [c] The values 100,200,300 printed [d] Only 100 is printed Answer 2

20. What is the result when you compile and run the following code? boolean b = true; if (b) { System.out.println("True"); } else { System.out.println("False"); } [a] Nothing is printed [b] True is printed [c] False is printed [d] Compile time error Answer 3

21. What is the result when you compile and run the following code? int a = 0, b = 6, c = 10; if (a > 5) { if ( b < 5 ) { System.out.println("First"); } else { System.out.println("Second"); } } else if (c > 5) { System.out.println("Third"); } else { System.out.println("Fourth"); } [a] Nothing is printed [b] First is printed [c] Second is printed [d] Third is printed [e] Fourth is printed Answer 4

22. How can you change the break statement below so that it breaks out of the inner and middle loops and continues with the next iteration of the outer loop? outer: for ( int x =0; x < 3; x++ ) { middle: for ( int y=0; y < 3; y++ ) { if ( y == 1) break; } } } [a] break inner: [b] break middle: [c] break outer: [d] continue [e] continue middle Answer 5

23. What is the result of attempting to compile and run the following class? public class Test { public static void main(String args[]) { int[] Myarray = new int[3]; for ( int i = 0 ; i < Myarray.length; i++ ) { System.out.println(i); } } }

[a] 0 [b] 1 [c] 2 [d] 3 [e] The program does not compile. Answer 6

24. What is the output of the following program if you compile and run? outer: for (int i = 0; i < 2; i++ ) { for ( int j = 0; j < 3; j++ ) { if ( i == j ) { continue outer; } System.out.println("The value of i is: " + i ); System.out.println("The value of i is: " + j); } } [a] The value of i is: 0 The value of j is: 0 [b] The value of i is: 0 The value of j is: 1 [c] The value of i is: 0 The value of j is: 2 [d] The value of i is: 1 The value of j is: 0 [e] The value of i is: 1 The value of j is: 1 [f] The value of i is: 1 The value of j is: 2 Answer 7

25. What is the result of compiling the following code? class MyExp { void MyMethod() throws IOException, EOFException { //............ }

class MyExp1 extends MyExp { void MyMethod() { //.......... } } public class MyExp2 extends MyExp1 { void MyMethod() throws IOException { //......... } }

[a] Compile time error [b] No compile time error [c] Run-Time error [d] MyMethod() cannot throw an exception in MyExp2 class Answer 8

26. Select all correct answers? public class ExpTest { public static void main ( String[] args ) { try { MyMethod(); } catch ( Exception e) { } } static void MyMethod() { try { System.out.println("a"); } catch ( ArithmeticException ae) { System.out.println("b"); } finally { System.out.println("c"); } System.out.println("d"); } } [a] a [b] b [c] c [d] d Answer 9

27. What is the result of the following code when you compile and run? public class ThrowDemo { static void demoMethod() { try { throw new NullPointerException("demo"); } catch(NullPointerException e) { System.out.println("Caught inside demoMethod."); throw e; // re-throw the exception } } public static void main(String args[]) { try { demoMethod(); } catch(NullPointerException e) { System.out.println("Recaught: " + e); } } }

[a] Compilation error [b] Runtime error [c] Compile successfully, nothing is printed. [d] Caught inside demoMethod. followed by Recaught: java.lang.NullPointerException: demo Answer 10

28. Select the correct answer? public class ThrowsDemo { static void throwMethod() { System.out.println("Inside throwMethod."); throw new IllegalAccessException("demo"); } public static void main(String args[]) { try { throwMethod(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); } } } [a] Compilation error [b] Runtime error [c] Compile successfully, nothing is printed. [d] inside demoMethod. followed by caught: java.lang.IllegalAccessException: demo Answer 11 29. Select all the correct answers from the following ? [a] An array can store many different types of values. [b] An array subscript should normally be of 'double' data type. [c] An array can store only same data type. [d] An array can only stores objects Answer 1 [c] An array can store only same data type.

30. What is the result when you compile and run the following program? public class MyArray { public static void main(String arg[]){ int myArray []= new int [10]; for (int i=0; i<myArray.length; i++) { System.out.println(myArray[i]); } } } [a] Prints all 0's on the screen from 0 to 9 in sequence. [b] Prints on the screen from 1 to 10 in sequence. [c] Prints on the screen 0 only. [d] Compile time error.

Answer 2

You might also like