LATEST UPDATES

Friday 18 November 2016

What is the difference between continue and break statement?



Break and continue are two important keywords used in Loops. When a break keyword is used in a loop, loop is broken instantly while when continue keyword is used, current iteration is broken and loop continues with next iteration.

In below example, Loop is broken when counter reaches 4.



for (counter=0;counter
System.out.println(counter);
 
if (counter==4) {
 
break;}
 
}



In the below example when counter reaches 4, loop jumps tonext iteration and any statements after the continue keyword are skipped for current iteration.



for (counter=0;counter
System.out.println(counter);
 
if (counter==4) {
 
continue;
 
}
 
System.outprintln("This will not get printed when counter is 4");
 
}
Java

1

for (counter=0;counter

What is a singleton class? Give a practical example of its usage.

A singleton class in java can have only one instance and hence all its methods and variables belong to just one instance. Singleton class concept is useful for the situations when there is a need to limit the number of objects for a class.
The best example of singleton usage scenario is when there is a limit of having only one connection to a database due to some driver limitations or because of any licensing issues.

What’s the purpose of Static methods and static variables?

When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects.

What are the various access specifiers for Java classes?

1. Public : Class,Method,Field is accessible from anywhere.
2. Protected:Method,Field can be accessed from the same class to which they belong or from the sub-classes,and from the class of same package,but not from outside.
3. Default: Method,Field,class can be accessed only from the same package and not from outside of it’s native package.
4. Private: Method,Field can be accessed from the same class to which they belong.

What is the difference between an Inner Class and a Sub-Class?

An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.
A sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class.

What is the difference between throw and throws ?

The throw keyword is used to explicitly raise a exception within the program.

The throws clause is used to indicate those exceptions that are not handled by a method.

Each method must explicitly specify which exceptions does not handle, so the callers of that method can guard against possible exceptions. 

Finally, multiple exceptions are separated by a comma.

What is the difference between HashSet and TreeSet ?


  • The HashSet is Implemented using a hash table.
  • Its elements are not ordered. 
  • The add, remove, and contains methods of a HashSet.
  • Time  complexity O(1). 



  •  TreeSet is implemented using a tree structure. 
  • The elements in a TreeSet are sorted.
  • The add, remove, and contains methods.
  • Time complexity  O(logn).
Designed By Blogger Templates