1. Java-overview 2. Features of Java 3. History of Java 4. HW SW Req for Java SE 7 5. Java Environment Setup 6. Basic Requirement 7. Comments 8. My First Program in Java 9. Line Break 10. Escape Sequences 11. Literals in Java 12. Identifiers in Java 13. Variables in Java 14. Data-Types in Java 15. Declare Variables 16. Reserved Key Words 17. printf() 18. Chained & Embedded state 19. print sum of 2 nos 20. Scanner class 21. print sum & average 22. Area & Perimeter of Rectangle 23. Area Circumference of Circle 24. Prg.for Simple Calculator 25. Operatos in Java 26. Ex. Of Operators 27. Swap two numbers 28. Hierarchy of Operators 29. if( ) statement 30. check +ve,-ve or zero 31. if..else statement 32. check Odd or Even 33. print larger of 2 nos 34. print largest of 3 nos 35. check divisibility 36. check print range 37. for. . . loop statement 38. print series of nos 39. check divisibility 40. print sum of nos 41. print Pyramid 42. prg.for Factorial 43. fill screen 44. prg.for largest/smallest no 45. print reverse no 46. add each digit 47. prg.for sum of series 48. prg.for fibonacci series 49. check Prime nos 50. prg. Prime no from 1 to 100 51. prg.Specified Prime nos 52. while( ) statement 53. do. . While statement 54. break & continue statement 55. switch case statement 56. Array in Java 57. print reverse order using array 58. Ascending/Descending order 59. search no in Array 60. Multidimensional Array 61. String in Java 62. String with spaces 63. print string in reverse 64. prg. to count A,E,I,O,U vowel 65. User defined methods 66. Methods program example 67. General Purpose Programs 68. Loan & EMI calculation prg. 69. Table print prg. 70. Leap year program 71. lower to UPPER case prg. 72. Age Distribution prg. 73. Bank note calculation prg. 74. Simple Interest prg. 75. Compound Interest prg. 76. Simple Depreciation prg. 77. Reducing bal.Depreciation prg. 78. Marksheet prg. 79. Income Tax prg. 80. Time calculator prg. 81. Distance converter prg. 82. Volume Air Calculation prg. 83. Time to fill Water Tank prg. 84. Salary Calculation prg. 85. Total Sale Calculation prg. 86. Male/Female percentage prg. 87. Library Rent prg. 88. Office Expance prg. 89. Total Salary Calculation prg. 90. Profit or Loss prg. 91. Total Profit/Loss prg. 92. Palindrome program 93. Java Interview Part1 94. Java Interview Part2 95. Java Interview Part3 96. Java Interview Part4 97. Java Interview Part5 98. Java Interview Part6 99. Java Interview Part7 100. Java Interview Part8 101. Java Interview Part9 102. Java Interview Part10 103. Java Interview Part11
Pr.Pg Next Pg

JAVA Interview Questions and Answers Part 3


21. What is an Iterator?

  • Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.


22. State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers?

  • public :Public class is visible in other packages, field is visible everywhere (class must be public too)

  • private :Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.

  • protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.

  • default :What you get by default ie, without any access modifier (ie, public private or protected).It means that it is visible to all within a particular package.


23. What is an abstract class?

  • Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such.

  • A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.


24. What is static in java?

  • Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a static

  • Method with a nonstatic method. In other words, you can't change a static method into an instance method in a subclass.


25. Are the imports checked for validity at compile time? e.g. will the code containing an import such as java.lang.ABCD compile?

  • Yes the imports are checked for the semantic validity at compile time. The code containing above line of import will not compile. It will throw an error saying,can not resolve symbol

    1. symbol : class ABCD

    2. location: package io

    3. import;


26. Does importing a package imports them subpackages as well? e.g. Does importing com.MyTest.* also import com.MyTest.UnitTests.*?

  • No you will have to import the subpackages explicitly. Importing com.MyTest.* will import classes in the package MyTest only. It will not import any class in any of it's subpackage.


27. What is the difference between declaring a variable and defining a variable?

  • In declaration we just mention the type of the variable and it's name. We do not initialize it.

  • But defining means declaration+ initialization.

    1. e.g String s; is just a declaration while String s = new String ("abcd");

    2. Or String s = "abcd"; are both definitions.

28. What is the default value of an object reference declared as an instance variable?

  • null unless we define it explicitly.


29. Can a top level class be private or protected?

  • No. A top level class can not be private or protected. It can have either "public" or no modifier. If it does not have a modifier it is supposed to have a default access.

  • If a top level class is declared as private the compiler will complain that the "modifier private is not allowed here". This means that a top level class can not be private. Same is the case with protected.


30. What type of parameter passing does Java support?

  • In Java the arguments are always passed by value.

Pr.Pg border                                              Next Pg