Java Bank

1. What is the base class of all classes ?
 Answer: java.lang.Object

2. What type of language is Java?
Answer: Pure Object Oriented

3. Which package in Java is automatically imported ( imported implicitly ) ?
Answer: java.lang

4. Which one of the following is a valid boolean declaration ?
Answer: boolean b = false;

5. What is the numerical range of a char?
Answer: 0 to 65535

6. What does the following code do ?
  • public static void main(String[] args)
  • {
  •    int a[] = { 1, 3, 5, 7, 9 };
  •    for(int b:a)
  •        System.out.println(b);
  • }
  • Answer: Print the values of a
  • Explanation: ':' is 'for each' operator and is special in Java.

  • 7. Predict the output
  • public static void main(String[] args)
  • {
  •      int a;      System.out.println(a); 
  • }
  • Answer: None of the above (neither 0 nor garbage) 
  • Explanation: This produces 'variable not initialized' error   

  • 8. Predict the output
  • public static void main(String[] args)
  • {
  •     String str = "Sona";
  •     System.out.println(str[1]);
  • Answer: Error
  • Explanation: The [] operator is available only for array data types. String is a class.

  • 9. How will you find the number of elements in an array ( of any data type ) in Java ?
  • Answer: Using the length member of the array object

  • 10. What will you use to find the length of a String in Java.
  • Answer: length() method.

  • 11. How to you compare two String objects in Java ?
  • Answer: s1.equals(s2)
  • Explanation: The equals method available in the String class makes the comparison between the String objects and returns a boolean. 

  • 12. How do you force garbage collection in Java ?
  • Answer: System.gc();
  • Explanation: Though Java supports automatic garbage collection, manually garbage collection is also possible by calling gc() method of System class.

  • 13. How can you achieve multiple inheritance in Java ?
  • Through interface it can be achieved
  • Explanation: Not only through interface, but also through multi-level inheritance, multiple inheritance is indirectly achievable in Java.

  • 14. What is the base class of the class Object ?
  • Answer: null
  • Explanation: All the classes are derived from the Object class, whereas the Object class has no super class. In other words, Object class is not derived from any other class.

  • 15. How do you redirect the output to a file in Java ?
  • Answers: Use PrintStream object everywhere instead of System.out,
  •                  Pass the PrintStream object to setOut method of System class.
  • Explanation: Both the answers are actually correct. But, the most general thing is the latter, because, if we want the output in console, we can comment just one statement.







  • Explanation: The finally block is executed even when an exception occurs (after executing catch block).
  • Answer: Only method overloading is supported

 

Answer: 7

Answer: for – each



Answer: Keyword
Explanation: 
'if' is a keyword, which is used for writing conditional statements.
For example:
'if' is a keyword.
'if(condition)
{
  true_statements;
}' is a conditional statement (which uses 'if' keyword).

Answer: Oak


Answer: Both try and catch


Answer: sort() method available in the Arrays class

Answer: final class can not be inherited

Answer: Interface

 








  • Explanation: super() calls the base class constructor.

No comments:

Post a Comment