Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| ||||
Interview Questions and Answers - Order By Rating | ||||
| ||||
Ans. You can implement encapsulation in Java by keeping the fields (class variables) private and providing public getter and setter methods to each of them. Java Beans are examples of fully encapsulated classes. Encapsulation in Java: Restricts direct access to data members (fields) of a class. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  oops  oops example  oops concepts  oops features Asked in 2 Companies basic   frequent | ||||
| ||||
Ans. OOPs or Object Oriented Programming is a Programming model which is organized around Objects instead of processes. Instead of a process calling series of processes, this model stresses on communication between objects. Objects that all self sustained, provide security by encapsulating it's members and providing abstracted interfaces over the functions it performs. OOP's facilitate the following features 1. Inheritance for Code Reuse 2. Abstraction for modularity, maintenance and agility 3. Encapsulation for security and protection 4. Polymorphism for flexibility and interfacing | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  oops  oops features Asked in 260 Companies basic   frequent | ||||
Very frequently asked. Usually among very first few questions. | ||||
| ||||
Ans. Encapsulation is a feature of OOP's that binds the data and it's associated methods together as a single unit and facilitate protection and data hiding by providing minimal interface to outside. For example - member variables are declared private and are accessed through public methods. Moreover we have private methods that can only be used internally and hence providing minimal interface to outside class through use of public methods. | ||||
Sample Code for encapsulation | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  encapsulation  object oriented programming (oops)  oops concepts  oops  oops concepts  oops features Asked in 4 Companies Basic   frequent | ||||
| ||||
Ans. int compare(int a, int b) { a>b? return a: return b; } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  code  program  coding Asked in 1 Companies basic | ||||
| ||||
Ans. ArrayLists aren't synchronized and hence doesn't allow synchronized access. As multiple threads can access an arraylist in parallel, it may result in an inconsistent state. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  vector  arraylist  list  collections Basic | ||||
| ||||
Ans. final keyword is a modifier that means differently when applied to variables, methods and classes. 1. Final variable cannot be changed once initialized 2. Final method cannot be overridden 3. Final class cannot be sub classed | ||||
Sample Code for final variable Sample Code for final method Sample Code for final class | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  final  final keyword Asked in 1 Companies Basic   frequent | ||||
| ||||
Ans. Following Classes in Java SE creates immutable objects String class Wrapper Classes like Integer, Float etc. StackTraceElement Most Enum classes File Class Locale Class | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  immutable  immutability objects  immutable  immutability Asked in 1 Companies Basic   frequent | ||||
| ||||
Ans. All are valid declarations. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  static final  constants basic | ||||
| ||||
Ans. Object is an entity in Java , i.e which has a state ( instance variables ) and methods attached to it ( static or non static , through class definition ). References are the identifiers that are used to point to objects. For example - Employee emp = new Employee(); emp = new Employee(); In this code, emp is the reference that gets assigned to the new object created by the new operator. In the second line , we have assigned the same reference to another object. So with these 2 lines of code, we have 2 objects in memory with reference emp now pointing to second object. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  reference  object  reference vs object Basic   frequent | ||||
| ||||
Ans. Enumeration can iterate only legacy collections like Vector , HashTable and Stack whereas Iterator can iterate both legacy and non legacy collections. Enumeration is less safer than Iterator Enumeration is fail safe whereas Iterator is fail fast Iterator allows for removal of element while traversal whereas Enumeration doesn't have remove method. Enumerations were introduced in Java 1 whereas Iterators were introduced with Java 2 Enumerations have methods like hasMoreElements and nextElement whereas Iterators have methods like hasNext, next and remove | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  enumeration vs iterator  collections Basic   frequent | ||||
| ||||
Ans. Such a class still can have member elements which can be inherited and hence facilitate code reuse. Moreover Abstract class can have non final static elements whereas interfaces are only allowed to have static final elements. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  abstract class   interfaces Asked in 2 Companies Basic | ||||
| ||||
Ans. int convert(int binaryInt) { int sumValue=0; int multiple = 1; while(binaryInt > 0) { binaryDigit = binaryInt; binaryInt = binaryInt /10; sumValue = sumValue (binaryDigit * multiple); multiple = multiple * 2; } return sumValue; } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  code  coding Asked in 1 Companies basic | ||||
| ||||
Ans. Java identifies the method to be called at runtime by the object that is being referenced. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  polymorphism  object oriented programming (oops)  oops concepts  overloading  overriding basic | ||||
| ||||
Ans. runtime polymorphism or method overriding doesn't require method name and signature to be different whereas compile time polymorphism or method overloading requires method name to be same but the signature to be different. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  polymorphism  object oriented programming (oops)  oops concepts  overloading  overriding basic | ||||
| ||||
Ans. Abstract classes provide a mechanism of interfacing ( using abstract method ) as well as code reuse through inheritance ( extending abstract class ) Comparing to concrete class they have an advantage of providing interface which a concrete class doesn't provide. Comparing to interfaces they have an advantage of providing code reuse through inheritance which interfaces dont provide. | ||||
Sample Code for abstract class | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  abstract class  importance of abstract class Basic   frequent | ||||
| ||||
Ans. It would result in endless function calls and hence eventually would result in stackoverflow exception. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  recursion  stackoverflowexception Basic   frequent | ||||
| ||||
Ans. It's a programming technique wherein the method can call itself. The method call usually involves a different set of parameters as otherwise it would lead to an infinite loop. There is usually a termination check and statement to finally return the control out of all function calls. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve   Asked in 1 Companies Basic | ||||
| ||||
Ans. JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed.JVMs are available for many hardware and software platforms (i.e. JVM is platform dependent).Runtime Instance Whenever you write java command on the command prompt to run the java class, an instance of JVM is createdThe JVM performs following operation:Loads codeVerifies codeExecutes codeProvides runtime environment | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  memory management  jvm Asked in 3 Companies basic   frequent | ||||
| ||||
Ans. It will print the garbage value as compiler doesn't provide a default value to local variables. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  local variable  default values basic | ||||
| ||||
Ans. http://introcs.cs.princeton.edu/java/11precedence/ | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  operators  operator precedence basic | ||||
| ||||
Ans. import java.util.*; class LinkedListSolution{ protected LinkedList list; public LinkedListSolution(){ list = new LinkedList(); } public Object pop() throws NoSuchElementException{ if(list.isEmpty()) throw new NoSuchElementException(); else return list.removeFirst(); } public void push(Object obj){ list.addFirst(obj); } public Object peek() throws NoSuchElementException{ if(list.isEmpty()) throw new NoSuchElementException(); else return list.getFirst(); } public boolean isEmpty(){ return list.isEmpty(); } public String toString(){ return list.toString(); } } class TestStack{ public static void main(String args[]){ LinkedListSolution s = new LinkedListSolution(); s.push("First"); s.push("Second"); s.push("Third"); System.out.println("Top: " s.peek()); s.push("Fourth"); while(!(s.isEmpty())) System.out.println(s.pop()); } } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  Linkedlist  data structures  algorithm Asked in 2 Companies basic | ||||
| ||||
Ans. 1. Class name should be a noun and should start with capital case character. 2. Interface Name should be an adjective and should start with capital case character. 3. Method and Variable names should follow lower camel case notation. 4. package name should be all lower case. 5. constant variables (static final) should be all capital case. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  naming conventions basic | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
Ans. && - AND || - OR ! - LOGICAL NOT | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  operators Basic | ||||
| ||||
Ans. boolean byte char double float int long short void | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  data types  primitive data types Asked in 1 Companies Basic | ||||
| ||||
Ans. https://javasearch.buggybread.com/InterviewQuestions/questionSearch.php?searchOption=label' | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  Algorithm  Sorting Algorithm Asked in 4 Companies basic   frequent | ||||
| ||||
Ans. Authentication is the process of verifying the identity and credentials of the user to authenticate him into the system. whereas Authorization is the process by which access to a segment , method or resource is determined. Authorization is usually a step next to authentication. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  authentication  authorization  authentication vs authorization Asked in 9 Companies basic   frequent | ||||
| ||||
Ans. It's the process of preserving the state of an object and then restoring it. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve   basic | ||||