Java Certification Model Question & Answer - 1
(The Answers are in BOLD)


  1. What of the following will stop the execution of a running Thread ?
    1. When the thread throws an interrupted exception.
    2. When a high priority thread starts running.
    3. While the thread’s wait method is called.
    4. When the main method completes.
    5. In the running thread when a new thread is created

    6.  

       
       
       
       
       

  2. Which of the following will throw a NullPointerException?
  3. String s = null;
    1. if ((s!=null) & (s.length() > 0))
    2. if ((s!=null) && (s.length() == 0))
    3. if ((s==null) | (s.length() > 0))
    4. if ((s==null) || (s.length() > 0))

    5.  

       
       
       
       
       

  4. Which of the following describes a fully encapsulated class?
    1. Methods cannot be private
    2. Variables cannot be private
    3. All instance variables are private.
    4. All instance variables are public
    5. All instance variables are private and public accessor methods are provided to get the values of instance variables

    6.  

       
       
       
       
       

  5. In the following program, if somecondition() returns true, then only line number 3 must throw a custom exception MyException. MyException is not a subclass of runtime exceptions. What are the changes to be made to the given method to handle the eception
    1. Class Exceptionthrown{
    2. public void method() {
    3. --------------
    4. ---------------
    5. If (somecondition()) {
    6. }
    7. }
    8. }

    9.  

       
       
       
       
       

    1. Add throws new MyException(); in line number 4
    2. Add throw MyException(); in line number 6
    3. Add throw new MyException(); in line number 6
    4. Add throw new MyException(); in line number 4
    5. Modify the method declaration such that an object of type Exception is to be thrown

    6.  

       
       
       
       
       

  6. What is the output of the following program
  7. public class Trial{

    int x;

    public static void main(String args[]) {

    x = 8;

    System.out.print("The value of x is " + x);

    } }

    1. The program prints The value of x is 8;
    2. The program prints The value of x is 0;
    3. The program will not compile.

    4.  

       
       
       
       
       

  8. Given the following code

  9.  

     
     
     

    Float F1 = new Float(0.9F);

    Float F2 = new Float(0.9F);

    Double D1 = new Double (0.9);

    Which of the following will return true?

    1. (F1 == F2)
    2. (F1 == D2)
    3. F1.equals(F2)
    4. F1.equals(D2)
    5. F1.equals(new Float(0.9F))

    6.  

       
       
       
       
       

  10. What are the assignments which are valid in the line XXXX

  11.  

     
     
     

    class Super{

    private int i;

    public int ma_cd(float f){

    i = (int) f;

    return i;

    }

    }

    class Sub extends Super{

    int j;

    float g;

    public Sub(){

    Super s = new Super(); XXXX

    }

    }

    1. j = i;
    2. g = f;
    3. j = s.ma_cd(3.2f);
    4. j = s.i;
    5. g = s.f;

    6.  

       
       
       
       
       

  12. Which is the earliest possible instance at which the String object referred by s is eligible for Garbage Collection?
  13. public static void main(String args[ ]){
    1. String s = "abcd";
    2. String s1 = "efghi";
    3. String s2 = s+ s1;
    4. s = null;
    5. s = s1;
    6. s1 = s;

    7.  

       
       
       
       
       

    1. Before Line 4.
    2. Before Line 5 starts
    3. After Line 5.
    4. Before line 6.

    5.  

       
       
       
       
       

  14. A Polygon is drawable. You want to store the coordinates of the Polygon in a vector. The Polygon has color and fill flags. Which of the following types will you choose for declaring the variables of a Polygon class.
    1. Vector
    2. drawable
    3. Polygon
    4. Color
    5. Boolean

    6.  

       
       
       
       
       

  15. An Employee is a Person. The Employee has a identity, name etc. There is a plan to prepare a class of type Employee which is going to used in many unrelated parts of the project. Using the following keywords construct a class that defines an Employee.

  16.  

     
     
     

    abstract, public, Employee, throws, class, Person, void, extends

    public class Employee extends Person

  17. FilterOutputStream has sub classes such as BufferedOutputStream, DataOutputStream and PrintStream. Which of the following is the valid argument type for the constructor of the FilterOutputStream
    1. File
    2. PrintStream
    3. DataOutputStream
    4. BufferedOutputstream
    5. FileOutputStream

    6.  

       
       
       
       
       

  18. What is the equivalent Octal representation of 7 (not more that 4 characters)

  19.  

     
     
     

    07

  20. What is the return type of the methods in Java 2 Listener Interfaces ?
    1. Boolean
    2. boolean
    3. void
    4. String
    5. int

    6.  

       
       
       
       
       

  21. Which of the following is true regarding GridBagLayout
    1. if a component has been given fill both is true, then as the container resizes the component resizes
    2. The number of rows and columns are fixed while loading the components.
    3. The number of rows and columns are fixed while loading the layout itself .
    4. if a component has a non-zero weightY value, then the component grows in height as the container is resized.

    5.  

       
       
       
       
       

  22. Given the following code
  23. switch(x){

    case 1: System.out.println("Test 1");

    case 2:

    case 3: System.out.println("Test 3");

    break;

    default : System.out.println("Test 4");

    break;

    }

    What are the possible values of x if "Test 3" is to be printed ?

    1. 4
    2. 2
    3. 3
    4. 1
    5. 0

    6.  

       
       
       
       
       

  24. What is the valid argument type for the methods in a MouseMotionListener Interface ?

  25.  

     
     
     

    MouseEvent

  26. Which of the following is valid native method declaration in Java ?
    1. public static void native method1(){}
    2. static void native method1(){}
    3. static native void method1();
    4. static void native method1(){}
    5. static native int method1();

    6.  

       
       
       
       
       

  27. Given the following code
  28. class Super{

    public void method1() {}

    }

    class Sub extends Super{

    // xx

    }

    Which of the following methods can be legally added at line marked xx ?

    1. int method1() { }
    2. String method1() throws IOException{ }
    3. void method1(String s) { }
    4. void method1(Float s) { }
    5. public void method2() { }

    6.  

       
       
       
       
       

  29. Given the following code

  30.  

     
     
     

    if (x > 4) {

    System.out.println( "Test 1" );

    }

    else if (x> 9){

    System.out.println("Test 2");

    }

    else

    System.out.println("Test 3");

    What are the possible values of x that will cause "Test 2 " to be printed ?
    1. 0 to 4
    2. Less than 0
    3. 5 to 9
    4. 10 and greater
    5. None

    6.  

       
       
       
       
       

  31. What is the range of a char type?

  32.  

     
     
     

    0 to 216-1

  33. Which of the following are true about >> and >>> operators?
    1. >> performs a unsigned shift and >>> performs a rotate.
    2. >> performs a signed shift and >>> performs an unsigned shift.
    3. >> performs an unsigned shift and >>> performs a signed shift.

    4.  

       
       
       
       
       

  34. What are valid Java keywords
    1. NULL
    2. null
    3. TRUE
    4. implements
    5. interface
    6. instanceof
    7. sizeof

    8.  

       
       
       
       
       

  35. Which of the following collection objects can be used for storing values that may appear more than once and ordered ?
    1. Map
    2. List //dupes + ordered
    3. Set
    4. Collection // dupes + unordered

    5.  

       
       
       
       
       

  36. You want a component to retain its width, but not the height when resized. How will you achieve this ?
    1. Place the component in North or South in a BorderLayout
    2. Place the component in the Center of a BorderLayout
    3. Place the component in East or West in a BorderLayout

    4.  

       
       
       
       
       

  37. You have defined a class and sub classes of the same. But you don’t want the methods of the super class to be overridden . what modifier you will use for declaring the methods of the super class ?

  38.  

     
     
     

    final

  39. What is the body of is a body code of a Thread
    1. public void run()
    2. public void start()
    3. public void runnable()
    4. public static void main(String args[])

    5.  

       
       
       
       
       

  40. What is the output of the following code?
  41. int i = 0;

    while(i-- > 0) {

    System.out.println("The value of i is " + i);

    }

    System.out.println("Finished");

    1. The value of i is 1
    2. The value of i is 0
    3. Finished
    4. Compilation Error
    5. Runtime Error

    6.  

       
       
       
       
       

  42. What is the output of the following program
  43. class Example{

    static int arr[] = new int[10];

    public static void main(String args[]){

    System.out.println(" The value 4th element is " + arr[4]);

    }

    }

    1. The value of 4th element is 4
    2. The value of 4th element is null
    3. The value of 4th element is 0
    4. Null Pointer exception is raised
    5. Runtime error, because the class is not instantiated

    6.  

       
       
       
       
       

  44. Which of the following is the correct class declaration for Car.java. See to that it is a case-sensitive system
    1. public class Car{

    2.  

       
       
       

      int in;

      public Car(int inn){

      in = inn

      }

      }

    3. public class Car extends Truck, Vehicle{

    4.  

       
       
       

      int in;

      public Car(int inn){

      in = inn;

      }

      }

    5. public class Car{

    6.  

       
       
       

      int in;

      public abstract void raceCar() {System.out.println("RaceCar");}

      public Car(int inn){

      in = inn;

      }

      }

    7. public class Car{

    8.  

       
       
       
       
       

    int in;

    public Car(int inn){

    in = inn;

    } }

  45. Which of the following is true about Garbage Collection mechanism
    1. Garbage Collection is carried out at predictable intervals
    2. The programmer can write come complex code to perform garbage collection
    3. The programmer can make objects eligible for garbage collection through the reference variables
    4. Garbage collection cannot be performed on objects which are referred by the running threads.

    5.  

       
       
       
       
       

  46. What is the output of the following program?

  47.  

     
     
     

    class Super{

    String name;

    Super(String s){

    name =s ;

    } }

    class Sub extends Super{

    String name;

    Sub(String s){

    name=s;

    }

    public static void main(String args[]){

    Super sup = new Super("First");

    Sub sub = new Sub("Second");

    System.out.println(sub.name + " " + sup.name);

    } }

    1. First Second
    2. Second First
    3. Compilation error
    4. Runtime error stating same name found in Super as well as in the Sub class

    5.  

       
       
       
       
       

  48. java.awt.AWTEvent is a super class of all delegation model event classes. There is one method called getID() in that class. What does that method returns
    1. text of the event
    2. source of the event
    3. ID of the event

    4.  

       
       
       
       
       

  49. What of the following cannot be added to a container ?
    1. Applet
    2. Panel
    3. Frame
    4. Component
    5. Container
    6. MenuComponent

    7.  

       
       
       
       
       

  50. How to initialise the variables in class Super from a constructor in the sameclass(at line xx). Write a single line code without any spaces

  51.  

     
     
     

    class Super{

    float x;

    float y;

    float z;

    Super(float a, float b){

    x = a;

    y = b;

    }

    Super(float a, float b, float c){

    //xx

    z = c;

    }

    }

    this(a,b);

  52. What is true about inner classes?
    1. Inner classes have to be instantiated only in the enclosing class
    2. Inner classes can access all the final variables of the enclosing class
    3. Inner classes cannot be static
    4. Inner classes cannot be anonymous class
    5. Inner classes can extend another class.

    6.  

       
       
       
       
       

  53. Consider the following hierarchy

  54.  

     
     
     

    Parent

    ------------------------------

    | |

    DerivedOne DerivedTwo

    The following variables are initialised to null.

    Parent P1;

    DerivedOne D1;

    DerivedTwo d2;

    What will happen if the following assignment is made( take the above into account)?

    D1 = (DerivedOne) P1

    1. It is legal at compile time and illegal at run time
    2. It is legal at compile time and legal at run time // read q properly, init to null
    3. It is illegal at compile time.
    4. It legal at compile time and may throw "CastClassException" at run time

    5.  

       
       
       
       
       

  55. Which modifiers are to be used to obtain the lock on the object
    1. public
    2. private
    3. static
    4. synchornized
    5. lock

    6.  

       
       
       
       
       

  56. // Point X
  57. public class Example

    What can be possibly used at Point X

    1. import java.awt.*;
    2. package local.util;
    3. class NoExample{}
    4. protected class SimpleExample
    5. public static final double PI = 3.14;

    6.  

       
       
       
       
       

  58. What is the range of int type

  59.  

     
     
     

    -231 to 231-1

  60. What are the legal identifers in Java?
    1. %employee
    2. $employee
    3. employ-ee
    4. employee1
    5. _employee

    6.  

       
       
       
       
       

  61. Which of the following are true about the listener interfaces in Java ?
    1. awt listener menthods generally takes an argument which is an instance of some subclass of java.awt.AWTEvent
    2. When multiple listeners are added to a single component the order of invocation of the listeners cannot be guaranteed
    3. A single component can have multiple listeners added to it.

    4.  

       
       
       
       
       

  62. Given the following declaration.

  63.  

     
     
     

    String S1 = "Hello";

    Which of the following are true ?

    1. S1 = S1 >> 2;
    2. S1 += "there";
    3. S1 += 3;
    4. char c = S1[2]; // Will give an error, use chatAt, as String is aclass not a Array

    5.  

       
       
       
       
       

  64. Given the following String declarations and methods. What is the String object referred by S1 after execution of these statements ?
  65. String S1 = "Hello";

    String S2 = "There";

    S1.concat(" my friend");

    S1.concat(" our friend");

    S1 += S2;

    HelloThere

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.