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
- 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 - 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 - .
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 - 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 - 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 - 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 - StringTokenizer class belongs to the package
a) java.lang b) java.util c) java.io d) java.string e) none - 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 - finalize() method is linked with try-catch block of exception handling.
a) True b) False - 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 - 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 - 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 - 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 - .
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
- 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 - .
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 - 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 - The Java object System.err belongs to the class
a) PrintStream b) OutputStream c) a and b d) none - FileInputStream is a low-level input stream and FilterInputStream is a high-level input stream.
a) True b) False - RandomAccessFile is a subclass of File.
a) True b) False - What is the immediate superclass of DataInputStream.
a) InputStream b) FilterInputStream c) Object d ) FileInputStream e) none - Files can not be opened by instantiating the objects of stream class RandomAccessFile
a) True b) False - InputStream is the subclass of FileInputStream.
a) True b) False - FilterInputStream filters malicious characters/data from an input.
a) True b) False - 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 |
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".
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
This I have to ask my PHP programmer. It takes some time.