Interview Questions and Answers - Order By Rating Q661. What access the variable gets if we don't provide any access specifier ? Core Java
Ans. It gets the default access i.e package protected. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  access specifiers  default Access specifier Basic Q662. How are the concept of Association related to Composition and Inheritance ? Core Java
Ans. Composition and Inheritance are the different types of Associations for Classes.
Composition is a has-a association between classes.
Inheritance is a is-a association between classes. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  composition  object oriented programming (oops)  oops concepts  inheritance  object oriented programming (oops)  oops concepts  association  relationship between objects Q663. What are the core OOPs concepts ? Core Java
Ans. Abstraction, Encapsulation, Polymorphism , Composition and Inheritance Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  core oops concepts Asked in 65 Companies basic   frequent Q664. Write a method / program that will determine if the parenthesis are balanced in a given string. Core Java
Ans. https://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/ Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string  code  coding Asked in 14 Companies Q665. Why is it important to close files and streams ? Core Java
Ans. Optimizing Resource utilization. When you open a file using any programming language , that is actually a request to operating system to access the file. When such a request is made , resources are allocated by the OS. When you gracefully close those files, those resources are set free.
In Java if you don’t close the streams and files, Garbage collection identifies the open files which have lost the reference and hence will close them. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  file handling Q666. What is the difference between && and & in Java ? Core Java
Ans. && is a Logical whereas & is a bitwise operator Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  operators  logical vs bitwise operator Q667. Why don't Java objects have destructors ? Core Java
Ans. Java manages memory using built-in garbage collector Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  destructor  garbage collection Q668. Write a Java program to print 10 random numbers between 1 to 100? Core Java
This question was recently asked at 'karya technology'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coding  code Asked in 1 Companies Q669. How to find the median number in an array of integers ? Core Java
Ans.
Arrays.sort(numArray);
double median;
if (numArray.length % 2 == 0)
median = ((double)numArray[numArray.length/2] (double)numArray[numArray.length/2 - 1])/2;
else
median = (double) numArray[numArray.length-1/2]; Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coding Asked in 2 Companies Q670. Write a Program to find the missing integer in an array of consecutive numbers? Core Java
This question was recently asked at 'Knight Capital'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Q671. Write a Program to validate if a particular character occurs after another in a string Core Java
Ans. public class Class{
public static void main(String[] args){
String str = "xyz123.co@m";
if(str.indexOf('.') < str.indexOf('@')){
System.out.println("Not a Valid Email Address");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string  string.indexOf  coding  code Q672. Write a Program to validate an email address Core Java
Ans. public class Class{
public static void main(String[] args){
String str = "xyz@123.com";
if(!str.contains("@") && !str.contains(".") && (str.indexOf('.') < str.indexOf('@'))){
System.out.println("Not a Valid Email Address");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  validate an email address  coding  code Asked in 3 Companies Q673. Upon compiling the following class, What .class files will get created
public class OuterClass{
class InnerClass{
}
}
Core Java
Ans. OuterClass.class AND OuterClass$InnerClass.class Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  class files  compilation  compile  inner classes   nested classes Q674. isnt the use of HashTable and ConcurrentHashMap the same, i.e providing synchronized map collection ? Core Java
Ans. Yes, they both aim at providing synchronized access to the Map collection. The only difference is in their implementation. ConcurrentHashMap came after HashTable and hence technically more efficient as it doesn't lock the complete map while accessing it. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  HashTable  ConcurrentHashMap  map  Collections Q675. Difference between concurrentHashMap and HashTable ? Core Java
Ans. HashTable locks the complete collection to provide synchronization whereas ConcurrentHashMap only locks segments to achieve synchronization and hence better efficient and faster. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  concurrentHashMap  HashTable  Map  Collections Asked in 2 Companies Q676. Write a Program to print numbers like following
1
22
333
4444
55555 Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
for(int x=1;x<=5;x++){
for(int y=1;y<=x;y++){
System.out.print(x);
}
System.out.println(" ");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Q677. Write a Program to print numbers like following
1
12
123
1234
12345 Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
for(int x=1;x<=5;x++){
for(int y=1;y<=x;y++){
System.out.print(y);
}
System.out.println(" ");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Q678. Write a Program to print pattern like following
*_**_***_****_*****_*************** Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
for(int x=1;x<=5;x++){
for(int y=1;y<=x;y++){
System.out.print("*");
}
System.out.print("_");
}
for(int x=5;x>=1;x--){
for(int y=1;y<=x;y++){
System.out.print("*");
}
System.out.print("");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Q679. Write a Program to print pattern like following
*
**
***
****
*****
*****
****
***
**
* Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
for(int x=1;x<=5;x++){
for(int y=1;y<=x;y++){
System.out.print("*");
}
System.out.println("");
}
for(int x=5;x>=1;x--){
for(int y=1;y<=x;y++){
System.out.print("*");
}
System.out.println("");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Q680. Write a Program to print pattern like following
*
**
***
****
*****
******
*******
********
*********
********** Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
for(int x=1;x<=10;x++){
for(int y=1;y<=x;y++){
System.out.print("*");
}
System.out.println("");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coding  code Q681. Write a Program to print asterix like following using Recursion
**
***
****
*****
******
*******
********
*********
**********
Program should set the start and end index and then display this accordingly. For example the above pattern is for displayNumbersBetween(2,10) and following is for displayNumbersBetween(5,7)
*****
******
******* Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
displayNumbersBetween(5,7);
}
private static void displayNumbersBetween(int start,int end){
if(start > end){
return ;
} else {
for(int x=1;x<=start;x++){
System.out.print("*");
}
System.out.println("");
displayNumbersBetween(start+1,end);
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  recursion  code  coding Q682. Write a Program to display numbers between 2 numbers using recursion Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
displayNumbersBetween(2,10);
}
private static void displayNumbersBetween(int start,int end){
if(start > end){
return ;
} else {
System.out.println(start);
displayNumbersBetween(start+1,end);
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  recursion  code  coding Q683. Write a program using Recursion to print multiplication of numbers between the start and end number
For example - Passing start number as 2 and end number as 10, it should print 2*3*4*5*6*7*8*9*10 = 3628800 Core Java
Ans.
public class BuggyBread {
public static void main(String args[]) {
System.out.println(multiply(2,5));
}
private static int multiply(int start,int end){
if(start > end){
return 1;
} else {
return start * multiply(start+1,end);
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  recursion Q684. Write a program to print sum of numbers between the start and end number
For example - Passing start number as 2 and end number as 10, it should print 2+3+4+5+6+7+8+9+10 = 54 Core Java
Ans.
public class BuggyBread {
public static void main(String args[]) {
System.out.println(sum(2,10));
}
private static int sum(int start,int end){
if(start > end){
return 0;
} else {
return start + sum(start+1,end);
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  recursion Q685. Write a Program to print factorial of a number using recursion Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
System.out.println(factorial(5));
}
private static int factorial(int number){
if(number == 1){
return 1;
} else {
return number * factorial(number - 1);
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  recursion Asked in 25 Companies Q686. Write a Program to display the words of a String in alpahbetical or Dictionary Order Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
String str = "we are what we repeatedly do excellence then is not an act but a haBit";
Set<String> wordSet = new TreeSet(); // Using Linked Hash Set as we would like to retrieve words in the insertion order
for(String word: str.split(" ")){
wordSet.add(word);
}
for(String word: wordSet){
System.out.print(word);
System.out.print(" ");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  String  coding  code Q687. Write a Program to remove duplicate words from the String Core Java
Ans.
public class BuggyBread {
public static void main(String args[]) {
String str = "we are what we repeatedly Do excellence, then, is not an act but a haBit";
Set<String> wordSet = new LinkedHashSet(); // Using Linked Hash Set as we would like to retrieve words in the insertion order
for(String word: str.split(" ")){
wordSet.add(word);
}
for(String word: wordSet){
System.out.print(word);
System.out.print(" ");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  String  coding  code Asked in 1 Companies basic Q688. Write a program to print count / no of common words in 2 separate strings Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
String str1 = "we are what we repeatedly Do excellence, then, is not an act but a haBit";
String str2 = "we are what we repeatedly Do is";
String[] str1Words = str1.split(" ");
String[] str2Words = str2.split(" ");
Set str1WordsSet = new HashSet();
for(String word:str1Words){
str1WordsSet.add(word);
}
int commonWordsCount = 0;
for(String word:str2Words){
if(str1WordsSet.contains(word)){
commonWordsCount++;
}
}
System.out.println(commonWordsCount);
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  String  coding  code Q689. Write a Program to find common words in 2 separate strings Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
String str1 = "we are what we repeatedly Do excellence, then, is not an act but a haBit";
String str2 = "we are what we repeatedly Do is";
String[] str1Words = str1.split(" ");
String[] str2Words = str2.split(" ");
Set str1WordsSet = new HashSet();
for(String word:str1Words){
str1WordsSet.add(word);
}
for(String word:str2Words){
if(str1WordsSet.contains(word)){
System.out.println(word);
}
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  String  coding  code Q690. Write a Program to print words of a String having length greater than 2 and ending with it Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
String str = "we are what we repeatedly Do excellence, then, is not an act but a haBit";
for(String word:str.split(" ")){
if(word.length() > 2 && word.endsWith("it")){
System.out.println(word);
}
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve