Java Questions And Answers II

 

                                                            

1

 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.

 

 

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

 

 Q3) 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 ---ans

 "Called"

A2

 Compiler

A3

 Runtime Exception

A4

 Nothing is printed in screen

 

 

Q4

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" ----------------ans

A3

 "Constructor1""Constructor2"

A4

 Compiler Errror

 

Q5

 Which of the following are valid Constructors?

A1

 public Test8(){} -----------ans

A2

 private void Test8(){}

A3

 protected Test8(int k){} ------ans

A4

 Test8(){}   -----ans

 

Q6

Which of the following are valid method declarations?

A1

 abstract method(){}

A2

 abstrct void method(){}

A3

 final void method(){} ----ans

A4

 int method(){} ----ans

 

Q7

which of the following are valid combinations for class declaration?

A1

abstract final class Test12{}

A2

abstract static class Test12{}---invalid

A3

final static class Test12{}

 

Q8

which of the following are valid constructor signatures?

A1

public void className()

 

A2

public void className()

 

A3

private className()

 

A4

static className()

 

 

9. 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];

 

Q10

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  

Q11

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 

 

Q12)What is need of wrapper class?

1)  class Top {

   static void myTop() {
      System.out.println("Testing myTop method in Top class");
   }
}
public class Down extends Top {

    void myTop() {
          System.out.println("Testing myTop method in Down class");
    }
    public static void main(String [] args) {
        Top t = new Down();
         t.myTop();
    }
}

A) Compile Time error
B) Runtime error
C) Prints Testing myTop method in Top class on the console
D) Prints Testing myTop method in Down class on the screen

2)   public class MyTest {

     final int x;
     public MyTest() {
         System.out.println( x + 10 );
     }
     public static void main( String args[] ) {
            MyTest mt = new MyTest();
     }
}

A) Compile time error
B) Runtime error
C) Prints on the screen 10
D) Throws an exception

3)    class MyTest {

   public void myTest() {
         System.out.println("Printing myTest in MyTest class");
   }

   public static void myStat() {
         System.out.println("Printing myStat in MyTest class");
   }
}

public class Test extends MyTest {

    public void myTest() {
         System.out.println("Printing myTest in Test class");
   }

   public static void myStat() {
         System.out.println("Printing myStat in Test class");
   }

   public static void main ( String args[] ) {

       MyTest mt = new Test();
       mt.myTest();
       mt.myStat();
   }

A)   Printing myTest in MyTest class followed by Printing myStat in MyTest class
B) Printing myTest in Test class followed by Printing myStat in MyTest class
C) Printing myTest in MyTest class followed by Printing myStat in MyTest class
D) Printing myStat in MyTest class followed by Printing myStat in MyTest class

4)  Which of the following are examples of immutable classes , select all correct answer(s)?

A) String
B) StringBuffer
C) Double
D) Integer

5) Select the correct answer for the code fragment given below?

public class TestBuffer {

     public void myBuf( StringBuffer s, StringBuffer s1) {
            s.append(" how are you") ;
            s = s1;
     }

     public static void main ( String args[] ) {
             TestBuffer tb = new TestBuffer();
              StringBuffer s = new StringBuffer("Hello");
              StringBuffer s1 = new StringBuffer("doing");
              tb.myBuf(s, s1);
              System.out.print(s);
      }
}

A) Prints Hello how are you
B) Prints Hello
C) Prints Hello how are you doing
D) Compile time error

6) What is the result when you compile and run the following code?

public class MyTest {

   public void myTest( int[] increment ) {
          increment[1]++;
   }

   public static void main ( String args[] ) {
         int myArray[] = new int[1]; 
         MyTest mt = new MyTest();
         mt.myTest(myArray);
         System.out.println(myArray[1]);
   }
}

A) Compile time error
B) Runtime error
C) ArrayOutOfBoundsException
D) Prints 1 on the screen 

7) The following code has some errors, select all the correct answers from the following?

public class MyTest {

         public void myTest( int i ) {
                 for ( int x = 0; x < i; x++ ) {
                       System.out.println( x ) ;
                 }
          }
          public abstract void Test() {
                  myTest(10);
          }
}

A) At class declaration
B) myTest() method declaration
C) Test() method declaration
D) No errors, compiles successfully

8) At what point the following code shows compile time error?

class A {
         A() {
              System.out.println("Class A constructor");
          }
}

class B extends A {
         B() {
              System.out.println("Class B constructor");
          }
}

public class C extends A {
         C() {
              System.out.println("Class C constructor");
          }

         public static void main ( String args[] ) {
                 A a = new A(); // Line 1
                 A a1 = new B(); // Line 2
                 A a2 = new C(); // Line 3
                 B b = new C(); // Line 4
          }
}

A) A a = new A(); // Line 1
B) A a1 = new B(); // Line 2
C) A a2 = new C(); // Line 3
D) B b = new C(); // Line 4

9. This piece of code:

TextArea ta =new TextArea( 10, 3 );

Produces (select all correct statements):

a) a TextArea with 10 rows and up to 3 columns
b) a TextArea with a variable number of columns not less than 10 and 3 rows
c) a TextArea that may not contain more than 30 characters
d) a TextArea that can be edited

10. Of the five Component methods listed below, only one is also a method of the class MenuItem. Which one?

a) setVisible( boolean b )
b) setEnabled( boolean b )
c) getSize()
d) setForeground( Color c )
e) setBackground( Color c )

11 The layout of a container can be altered using which of the following methods:

  1. setLayout(aLayoutManager);
  2. addLayout(aLayoutManager);
  3. layout(aLayoutManager);
  4. setLayoutManager(aLayoutManager);

12. Given that a Button can generate an ActionEvent which listener would you expect to have to implement, in a class which would handle this event?

  1. FocusListener
  2. ComponentListener
  3. WindowListener
  4. ActionListener
  5. ItemListener

13. Assuming we have a class which implements the ActionListener interface, which method should be used to register this with a Button?

  1. addListener(*);
  2. addActionListener(*);
  3. addButtonListener(*);
  4. setListener(*);

14. What are Wrapper Classes?

15. Which containers use a FlowLayout as their default layout?

16. Which is the super class of all event classes?