Test Your Java 4


Test Your Java 7 and Test Your Java 8 are advanced questions. Answers are given for each question at the end of each test. Answering these questions increases your knowledge in Java and helps you a lot when you appear for interviews. Solving these basic questions is your first step for Certification Exams preparation.

Test Your Java 4

  1. StringBuffer sb1 = new StringBuffer("Hellow");
    System.out.println(sb1.length());
    What is the output of the above println statement ?
    a) 6 b) 7 c) 20 d) 22 e) displays error message f) none
  2. String str = "ONE ONE";
    System.out.println(str.indexOf('O'));
    What output does this fragment code displays?
    a) 0 b) 1 c) 5 d) 6 e) 0 and 5 f) displays error message g) none
  3. .
         public class SBValue   
         {
           public static void mian(String args[])    
           {
    	 StringBuffer sb = new StringBuffer("SCJP Exam");
    	 sb.setLength(0);
    	 System.out.println(sb);
           }     
         }  

    what is the output of the above program?
    a) SCJP Exam b) 0 c) 1 d) no output e) does not compile f) throws exception g) none

  4. Mr.Rao made a StringBuffer with an ensured capacity of 50 characters. But he made use of only 40 characters in making a string. The remaining 10 characters in the StringBuffer are set to
    a) null b) 0 c) garbage values d) none
  5. String str = "Hello";
    str.insert(2, 'k');
    System.out.println(str);
    The output of the above fragment of code is
    a) Hello b) Helklo c) Hekllo d) displays error message e) none
  6. Method ensureCapacity() ensures the StringBuffer with a minimum capacity to start with. Method ____________ increases or decreases the length of a StringBuffer.
    a) setBuffer b) setLength c) changeBuffer d) changeBufferOf e) setSize f) none
  7. StringTokenizer class belongs to the package
    a) java.lang b) java.util c) java.io d) java.string e) none
  8. final StringBuffer sb = new StringBuffer("I am Okay");
    sb.append("and You are Okay");
    System.out.println(sb);
    The above program is a special case of Stringbuffer with a final keyword. The program
    a) prints I am Okay and You are Okay b) does not compile as the nature of the StringBuffer (not immutable) is restricted with final keyword
    c) program does not compile as final variables can not be reassigned
    d ) program compiles but not appended and the output is I am Okay e) none
  9. finalize() method is linked with try-catch block of exception handling.
    a) True b) False
  10. After executing the exception-handler, program control proceeds to the
    1. first statement after the catch block
    2. try block from where the exception is thrown.
    Suggest the correct of the above two statements.
    a) 1 b) 2 c) 1 or 2 depending on the code d) none
  11. As soon as the control passes the try block, the try block(references of the try block do not exist in the remaining part of the program) is liable for garbage collection and need not wait for the completion of catch block execution to catch and process the exception thrown by the try block.
    a) True b) False
  12. Like a try block can throw any number of exceptions. Catch block can catch any number of exceptions and this can be achieved by writing the catch method with a comma-separated list of exceptions as arguments (perhaps, you must have not tried).
    a) True b) False
  13. It is a syntax error to catch the same type of exception in two different catch blocks associated with a particular try block?
    a) True b) False
  14. .
         class Finally         
         {
           public static void main(String args[])     
           {
             int  i = 5, j = 0, k;
             try   {  k  =  i/j;  }
             finally   {  System.out.println("from finally block");   }
           }      
         }

    Every try block should associate with atleast one catch block else compiler complains and notice that in the above program catch block is missing. The above program compiles, run and displays the output from finally block. a) True b) False

  15. What possibilities of the following can stop the finally block from getting executed successfully?
    a) supply of power to the CPU is interrupted b) An exception raised(if there is one) in the finally block is not caught c) Use of System.exit() in the program d) Death of the thread e) a, b, c, d f) none
  16. .
             public class Numbers
             {
    	   public static void main(String args[])    
               {
    	     try  { int a = 10, b = 0,  c = a/b;   }
      	     catch(ArithmeticException  e)    {  System.out.println(e);  }
    	     finally  
                 {
    	       try 
                   {  
                     int numarray[] = { 1, 2, 3, 4, 5 };   
                     numarray[10] = 25;   
                   }
    	       catch(ArrayIndexOutOfBoundsException  e)    
                   { System.out.println(e);   }
                 }
           	   }
              }      
    

    As a general rule, finally block will be executed after every try-catch block whether he exception is caught or not. But in the above program, the finally block also contains a try-catch block throwing an exception.
    State True or False The above program gets compiled successfully.
    a) True b) False

  17. float f = 2.5F;
    int i = 0;
    System.out.println(f/i);
    The println statement compiles, runs and displays the result
    a) True b) False
  18. The Java object System.err belongs to the class
    a) PrintStream b) OutputStream c) a and b d) none
  19. FileInputStream is a low-level input stream and FilterInputStream is a high-level input stream.
    a) True b) False
  20. RandomAccessFile is a subclass of File.
    a) True b) False
  21. What is the immediate superclass of DataInputStream.
    a) InputStream b) FilterInputStream c) Object d ) FileInputStream e) none
  22. Files can not be opened by instantiating the objects of stream class RandomAccessFile
    a) True b) False
  23. InputStream is the subclass of FileInputStream.
    a) True b) False
  24. FilterInputStream filters malicious characters/data from an input.
    a) True b) False
  25. To convert individual bytes while reading, into aggregate bytes of primitive data types like int, double etc, we use
    a) ByteArrayInputStream b) FileInputStream c) BufferedInputStream
    d) DataInputStream e) none

ANSWERS

1. a 2. a 3. d 4. b 5. d
6. b 7. b 8. a 9. b 10. a
11. a 12. b 13. a 14. a 15. e
16. a 17. a 18. a 19. a 20. b
21. b 22. b 23. b 24. b 25. a

Your Rating

Correct Answers
1 to 5 : Poor
6 t0 10: Below average
11 t0 15: Average
16 to 20: Good
Above 20: Extraordinary

Pass your comments and suggestions for the improvement of this "Test Your Java 4".

2 thoughts on “Test Your Java 4”

  1. Good evening sir. I would like to thank you for sharing greate knowledge with us. However you might want to improve this site. If i click an option for a question, if it is correct then it has to show there it self, otherwise show wrong. Please try to provide this facility.

    Regards,
    M.A.Munaf

Leave a Comment

Your email address will not be published.