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


1. What makes a Thread to stop execution

  1. An interrupted exception is thrown
  2. Invoke sleep() method
  3. Higher priority thread takes over
  4. While a thread does a read() using inputstream

  5.  

     
     
     
     
     

2. Which is the correct expression to be used to read line by line and store the result into a String object a) File f = new File("test.txt");
  1. FileInputStream f = new FileInputStream("test.txt");
  2. DataInputStream d = new DataInputStream(new FileInputStream("test.txt"));
  3. BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt")));
  4. BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt", "8859_1)));

  5.  

     
     
     
     
     

3. Which of the following will not throw a NullPointerException? 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. The state of the object can only be modified through accessor method.
  4. All variables are private.
  5. All variables and all methods are private

  6.  

     
     
     
     
     

5. Which state decide a var. ’a’ which is suitable for referring to an array of 50 string objects?
  1. char a[ ] [ ];
  2. String a [ ];
  3. String [ ] a;
  4. Object a [50];
  5. String a[50];

  6.  

     
     
     
     
     

6. In the following program, if somecondition() is 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.
    1. Public void method() {
    2. If (somecondition()) {
    3. }
    4. }

    5.  

       
       
       
       
       

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

  5.  

     
     
     
     
     

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

private int i;

public static void main(String args[]) {

method() ;

}

public static void method() {

int j;

j= i;

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

}

}

  1. The program prints The value of i is 0;
  2. The program gives a compilation error. By changing the private int i to public int i the problem gets resolved
  3. The program gives a compilation error. By changing the private int i to private static int i the problem gets resolved
  4. The program gives a compilation error. By changing the public static of the method to public only and then calling this method from the main using an instance of Example, the problem gets resolved.

  5.  

     
     
     
     
     

8. Which of the following are true?

Long L9 = new Long(9);

Long L10 = new Long(9);

Long L11 = L10;

long A = 9;

long B = 9;

  1. L9 == L10
  2. L9 == L11
  3. L10 == L11
  4. A == L9
  5. A == B

  6.  

     
     
     
     
     

9. How should we make a class to handle the events generated by a single user interface component?
  1. Subclassing a adapter class is inappropriate in this case - False
  2. Implements class which handles all the user interface listener methods - True

  3.  

     
     
     
     
     

10. What are the assignments which are valid in the line XXXX
        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.  

     
     
     
     
     

11. What is the output of the above code?

outer : for(int x = 0 ; x<2 ; x++){

inner: for(int a = 0 ; a < 2 ; a++){

if (a==1) {

break inner;

}

System.out.println(" a is " + a + " x is " + x );

}

}

  1. a is 0 x is 0
  2. a is 0 x is 1
  3. a is 0 x is 2
  4. a is 1 x is 0
  5. a is 1 x is 1
  6. a is 1 x is 2
  7. a is 2 x is 0
  8. a is 2 x is 1
  9. a is 2 x is 2

  10.  

     
     
     
     
     

12. Which is the earliest possible instance that Garbage Collection is activated for String created in line 1. 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 3.
  2. Before Line 4.
  3. Before Line 5.
  4. Before line 6.

  5.  

     
     
     
     
     

13. There is a plan to prepare a class which is going to used in many unrelated parts of the project. The class should be Polygon object which is a Shape. The polygon has a information about the coordinates stored in a vector, color status which states whether true or false. Write a class declaration which describes the above public class Polygon extends Shape{

vector coord;

boolean colorstatus;

}
 
 

14. There is an Employee who is a Person. The employee stores details in a Vector, date employed and number of instances. What are the type of

variables that is to be added to the Employee class

  1. Vector
  2. Date
  3. Object
  4. Person
  5. Employee
  6. Int

  7.  

     
     
     
     
     

15. What is the constructor argument for the FilterInputStream
  1. File
  2. InputStream
  3. DataInputStream
  4. BufferedReader
  5. InputStreamReader

  6.  

     
     
     
     
     

16. What is the hexadecimal representation of 7 (not more that 4 characters)

0x7

17. What is the output of the following program Example if args[0] is printed

java Example cat dog
  1. dog
  2. cat
  3. Example
  4. java
  5. Null PointerException is thrown

  6.  

     
     
     
     
     

18. What is the modifier for all the listener methods
  1. private
  2. default (no access modifier specified)
  3. protected
  4. public
  5. static

  6.  

     
     
     
     
     

19. 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.  

     
     
     
     
     

20. What is the value of x, if "Test 3" to be printed 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;

}

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

  6.  

     
     
     
     
     

21. If the string array name is argc in the arguments in the main method, which is invoked by the interpreter when a program is executed
  1. char string [ ] [ ]
  2. char [ ] a [ ]
  3. char a[ ]
  4. String argc
  5. String argv[ ]
  6. String argc[ ]

  7.  

     
     
     
     
     

22. What is the argument of the KeyListener methods KeyEvent 23. What is the correct declaration of an abstract method
  1. public abstract method(); // no ret type
  2. public void abstract method(): // position ret type
  3. public abstract void method() {} // no body
  4. public final abstract void method(); // final abs cannot come together
  5. public abstract void method () ;

  6.  

     
     
     
     
     

24. How will you override or overload the following method method1 of the Super class in the class Sub? class Super{

protected void method1() {}

}

class Sub extends Super{}

  1. public void method1() { return 0; }
  2. public int method1() { return 0; }
  3. public void method1(StringBuffer s) { }
  4. public void method1() { }
  5. public void method1(String s) { }

  6.  

     
     
     
     
     

25. What is the value of x if Test 3 is to be printed if (x > 4) {
System.out.println( "Test 1" );

} else if (x> 9){

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

} else

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

  1. 0 to 4
  2. Less than 0
  3. 5 to 9
  4. Greater than 10

  5.  

     
     
     
     
     

26. What is the range of a char type? 0 – 216-1 27. What does >> and >>> denotes
  1. >> is right shift >>> is round
  2. >> is signed right shift >>> round
  3. >> is signed shift and >>> is unsigned shift
  4. >> is unsigned >>> is unsigned

  5.  

     
     
     
     
     

28. What are valid keywords
  1. NULL
  2. TRUE
  3. implements
  4. interface
  5. instanceof
  6. sizeof

  7.  

     
     
     
     
     

29. Which of the following should be used to have No Order, Duplication or Perfect retrieval mechanism
  1. Map // no order, no dupe, perfect retrive
  2. List //order, dupe, no ret
  3. Set //no order, no dupe, not perfect retrieve
  4. Collection //no order, dupes, no ret

  5.  

     
     
     
     
     

30. In which LayoutManager, a component to be added to have only the width resized but not the height
  1. FlowLayout
  2. GridLayout
  3. GridBagLayout
  4. East or West of BorderLayout
  5. North or South of BorderLayout

  6.  

     
     
     
     
     

31. What is the access modifier for a class member variable which is to be used in the same package where the class is defined
  1. protected
  2. private
  3. public
  4. no access modifier
  5. static

  6.  

     
     
     
     
     

32. What is the body of a Thread
  1. run
  2. begin
  3. execute
  4. start
  5. resume

  6.  

     
     
     
     
     

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

do {

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

}while(--i > 0)

System.out.println("Finished");

  1. The value of i is 1
  2. The value of i is 0
  3. Finished
  4. Compilation Error ( no ; after while condn);
  5. Runtime Error

  6.  

     
     
     
     
     

34. What is the output of the following program 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.  

     
     
     
     
     

35. 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. import java.io.*;

  8.  

     
     
     
     
     

public class Car extends Object{

int in;

public Car(int inn){

in = inn;

} }

36. Which of the following are true about Threads?

  1. Threads can only be created by extending the java.lang.Thread
  2. Threads of the same program end together
  3. A thread which is suspended, cannot be restarted
  4. Uncoordination of threads, will result in the data be corrupted
  5. Java Interepter will exit only when all non-demon threads are not ended

  6.  

     
     
     
     
     

37. What is true about Garbage Collection mechanism
  1. Garbage Collection releases memory at predictable times and at predictable rates
  2. A correct program is one in which does not depend on the memory release by the garbage collector
  3. A programmer can indicate to the gc mechanism by making reference to a variable as null
  4. Garbage collection ensures that there may not be any leakage of memory.

  5.  

     
     
     
     
     

38. A question on Equality operator Ok 39. What is the output of the following program?

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.  

     
     
     
     
     

40. An IOException needs to be thrown in some method of a program. Select the correct expression for the raising an exception
  1. new Exception();
  2. throw Exception();
  3. throw new(new IOException());
  4. throw new Exception(); // its actually throw new IOException();
  5. throws new Exception();

  6.  

     
     
     
     
     

41. A Question on Method Overriding

OK

42. What is the output of the following program if method1() does not throw any exception?

try{

method1();

System.out.println("First");

}catch (Exception e){

System.out.prinln("Second");

}finally{

System.out.println("Finally");

} System.out.println("Last");

  1. First followed by Finally
  2. First followed by Finally followed by Last
  3. First followed by Last
  4. First

  5.  

     
     
     
     
     

43. What are the modifier which cannot be used for a local automatic variable
  1. default ( no access modifier )
  2. final
  3. static
  4. public

  5.  

     
     
     
     
     

44. What does getID() method of the event return
  1. time of the event
  2. source of the event
  3. nature of the cause of the event
  4. place of the event

  5.  

     
     
     
     
     

45. What cannot be added to a container
  1. Applet
  2. Panel
  3. Component
  4. Container
  5. MenuItem // can be added only to a Menu

  6.  

     
     
     
     
     

46. How to invoke the constructor from a constructor in the same class

class Super{

float x;

float y;

float z;

Super(float a, float b){

x = a;

y = b;

}

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

XXXX

z = c;

}

}

  1. super(a,b);
  2. Super(a,b);
  3. this(a,b);
  4. This(a,b);

  5.  

     
     
     
     
     

47. 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.  

     
     
     
     
     

48. What are the correct declaration for an inner class
  1. private class C
  2. new simpleinterface() { //Anonymous so v can use new
  3. new complexinterface(x) {
  4. new innerclasses() extends someotherclass {
  5. new innerclasses extends someotherclass { //Not as we have given a name and so v cannot use new

  6.  

     
     
     
     
     

49. What are true about Listener interfaces
  1. A component can have only one listener attached to it
  2. If a component has multiple listeners attached, the order in which the listeners are invoked are unknown
  3. If a component has multiple listeners, then all the listeners have to be friends

  4.  

     
     
     
     
     

50. If a class member variable is initialized and the value is not be changed at all, what is the modifier to be used during the declaration of the variable
  1. const
  2. fixed
  3. public
  4. static
  5. final

  6.  

     
     
     
     
     

51. What will happen if the following assignment is made, take the above into account?

class Parent

class Derived1 extends Parent

class Derived2 extends Parent

Parent p;

Derived d1;

p = d1;

  1. Legal at compile time and illegal at run time
  2. Legal at compile time and possibly legal at run time
  3. Legal at compile time and definitely legal at run time
  4. Illegal at compile time

  5.  

     
     
     
     
     

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

  6.  

     
     
     
     
     

53. What can be possibly used at Point X

// Point X

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

  6.  

     
     
     
     
     

54. What is the range of int type -231 to 231-1 55. What is the output of the following program? class Super{

String firstname;

String secondname;

Super(){}

Super(String f, String s){

firstname = f;

secondname = s;

}

public String toString(){

return "It is Monopoly Mr. " + firstname;

}}

class Sub extends Super{

String firstname;

String secondname;

Sub(String f, String s){

firstname = f;

secondname = s;

}

public String toString(){

return "It is Monopoly Mr. " + secondname + " "+ firstname;

}}

class Test{

public static void main(String args[]){

Super first = new Super("Scott", "McNealy");

Super second = new Sub("Bill","Gates");

System.out.println( second + " \n " +first);

}}

  1. It is Monopoly Mr. Bill Gates

  2.  

     
     
     

    It is Monopoly Mr. Scott

  3. It is Monopoly Mr. Gates Bill

  4.  

     
     
     

    It is Monopoly Mr. McNealy

  5. It is Monopoly Mr. Gates Bill

  6.  

     
     
     

    It is Monopoly Mr. Scott

  7. It is Monopoly Mr. Gates

  8.  

     
     
     
     
     

It is Monopoly Mr. Scott McNealy 56. How to instantiate the class Inner at point XXXX

class Outer{

class Inner{

}

public static void main(String args[]){

XXXX

}

}

  1. new Inner();
  2. Inner i = Outer.new Inner();
  3. Outer.Inner i = new Outer().new Inner();
  4. Inner i = Outer(new Inner());

  5.  

     
     
     
     
     

57. What is the output of the following program public class Example{

Stack s1;

Stack s2;

public static void main(String args[]){

new Example();

}

public Example() {

s1 = new Stack();

s2 = new Stack();

method1(s1,s2);

System.out.println(s1 + " " + s2);

}

public void method1(Stack ms1, Stack ms2){

ms2.push(new Long(100));

ms1 = ms2;

}

}

  1. Compilation error since ms2 cannot be assigned to ms1.
  2. s1 [ ] s2 [ ]
  3. s1 [ ] s2 [100]
  4. s1 [100] s2 [100]

  5.  

     
     
     
     
     

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

  6.  

     
     
     
     
     

59. Which of the following are true about the listener mathods in AWT? (C)
  1. In 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 is not specified.
  3. A single component can have multiple listeners added to it.

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.