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. |
|
| |||||||||||
Core Java - Interview Questions and Answers for 'Operator' - 28 question(s) found - Order By Newest | |||||||||||
Very frequently asked. Among first few questions in almost all interviews. Among Top 5 frequently asked questions. Frequently asked in Indian service companies (HCL,TCS,Infosys,Capgemini etc based on multiple feedback ) and Epam Systems | |||||||||||
| |||||||||||
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality, whereas "==" operator evaluate to see if the object handlers on the left and right are pointing to the same object in memory. x.equals(y) means the references x and y are holding objects that are equal. x==y means that the references x and y have same object. Sample code: String x = new String("str"); String y = new String("str"); System.out.println(x == y); // prints false System.out.println(x.equals(y)); // prints true | |||||||||||
Sample Code for equals | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   string comparison   string   object class   ==   equals   object equality  operator   == vs equals   equals vs == Asked in 294 Companies basic   frequent | |||||||||||
Try 6 Question(s) Test | |||||||||||
Frequently asked in Infosys and HCL Technologies ( Based on 2 feedback ) | |||||||||||
| |||||||||||
Ans. Using new operator - new xyzClass() Using factory methods - xyzFactory.getInstance( ) Using newInstance( ) method - (Class.forName(xyzClass))emp.newInstance( ) By cloning an already available object - (xyzClass)obj1.clone( ) | |||||||||||
Sample Code for object initialization using clone Sample Code for object initialization using getInstance | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   object creation   new operator   class.forname   cloning   ebay Asked in 9 Companies expert | |||||||||||
Usually asked in relation to casting and ClassCastException. | |||||||||||
| |||||||||||
Ans. The operator instanceOf is used to verify if the specified object is the instance of specified class or interface. Syntax if(x instanceOf ABC) where x is an object reference and ABC could be a class name or interface name. The above statement will be true if x holds an object that is an instance of ABC or any of the child class of ABC or if x holds an object that implements ABC. instanceOf operator is used to verify in case of downcasting. For ex - DerivedClass extends BaseClass x is the reference of BaseClass but holds DerivedClass object ( Polymorphism ) There is an operation that is defined in Derived Class, let's say derivedClassMethod() We cannot call derivedClassMethod() directly using x as x is reference of BaseClass and not DerivedClass and hence can only access methods that are defined in BaseClass and overridden in derived class. Though we can cast it to DerivedClass as following ((DerivedClass)x).derivedClassMethod(); But it may throw ClassCastException in case x doesn't hold an instance of DerivedClass at that point. So before casting it to DerivedClass we may like to make sure that it is an instance of DerivedClass and hence won't throw ClassCastException. So we make a check for it if(x instanceOf DerivedClass) { ((DerivedClass)x).derivedClassMethod(); } | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  instanceOf  instanceOf operator  Why we need instanceOf operator  use of instanceOf operator Asked in 2 Companies Intermediate   frequent | |||||||||||
| |||||||||||
Ans. public static void main(String ar[]) { int n=5; if((n/2)*2==n) { System.out.println("Even Number "); } else { System.out.println("Odd Number "); } } | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   code   coding   tricky questions   interesting questions   modulus operator Asked in 4 Companies   frequent | |||||||||||
| |||||||||||
Ans. In first case we are trying to initialize Inner class object using the instance of Outer Class whereas in second case we are trying to initialize the Inner class object directly using the Outer class name. In second case , Inner class is "static inner class" as we cannot access "non static inner class" using Classname alone. In first case, the inner class could be either "static inner class" or "non static inner class". | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  inner classes  inner class   static inner class   new operator | |||||||||||
| |||||||||||
Ans. Postfix operators i.e () [] . is at the highest precedence. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   operators   operator precedence Asked in 2 Companies | |||||||||||
| |||||||||||
Ans. new operator is used to statically create an instance of object. newInstance() is used to create an object dynamically ( like if the class name needs to be picked from configuration file ). If you know what class needs to be initialized , new is the optimized way of instantiating Class. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   object instantiation   object creation   class.forname   newinstance   new operator   difference between   advanced intermediate | |||||||||||
| |||||||||||
Ans. No | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  operator overloading Asked in 1 Companies basic | |||||||||||
| |||||||||||
Ans. Class.getInstance doesn't call the constructor whereas if we create an object using new operator , we need to have a matching constructor or copiler should provide a default constructor. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   constructor   object creation   default constructor   getinstance   new operator | |||||||||||
| |||||||||||
Ans. Both bitwise right shift operator ( >> ) and bitwise zero fill right shift operator ( >>> ) are used to shift the bits towards right. The difference is that >> will protect the sign bit whereas the >>> operator will not protect the sign bit. It always fills 0 in the sign bit. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   operators   bitwise operators   binary shift   right shift | |||||||||||
| |||||||||||
Ans. The = operator is right associative. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   operators   = operator | |||||||||||
| |||||||||||
Ans. To avoid ClassCastException. Though the following code will compile fine but will result in ClassCastException during runtime. Fruit fruit = new Apple(); Banana banana = Banana(fruit); // ClassCastException This code will not give compile time error as Banana and Fruit are related as Banana either extends or implement Fruit, So downcasting is acceptable. With this code we assume that the Fruit handler will have the Apple object at that point, violating which the code will throw the exception. This exception can be avoided by following code. Fruit fruit = new Apple(); if(fruit instanceOf Banana){ Banana banana = Banana(fruit); // ClassCastException } | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   instanceof   classcastexception   runtime exceptions  instanceOf operator | |||||||||||
Try 2 Question(s) Test | |||||||||||
| |||||||||||
Ans. Prefix operator performs the increment and then assignment whereas its inverse for postfix operator. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  prefix increment operator   postfix increment operator   operators   java basic   frequent | |||||||||||
| |||||||||||
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?&category=code&searchOption&keyword=963 | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  ternary operator  code  coding | |||||||||||
| |||||||||||
Ans. Associativity determines whether an expression is evaluated left-right or right-left. When an expression has two operators with the same precedence, the expression is evaluated according to its associativity. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  associativity  expression evaluation  operator | |||||||||||
| |||||||||||
Ans. No, Its an Operator. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  new keyword  operator | |||||||||||
Rarely asked as it was introduced with Java 8. | |||||||||||
| |||||||||||
Ans. We can refer to a function using this operator like System.out.println(intList.stream().reduce(Math::max).get()); | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  :: operator   lambda expression  java 8 | |||||||||||
| |||||||||||
Ans. = is the assignment operator that assigns the result of the expression on the right to the variable on the left, whereas == is the operator to check object equality to see if the reference on left and right are pointing to the same object. For primitive types, its used to check if both variables holds the same value. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  =  ==  assignment operator  object equality  difference between Basic | |||||||||||
| |||||||||||
Ans. For objects or references, == operator check if the reference on left and right points to the same object. For primitive types or variables, == operator check if the variable on left and right holds the same value. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  ==  object equality  operator Basic | |||||||||||
| |||||||||||
Ans. Braces, i.e () and [] have the highest precedence | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java operators | |||||||||||
| |||||||||||
Ans. No, Java doesn't have a sizeOf operator. In C / C++ , its required to determine how much memory allocation is required which is not the case with Java. Java handles memory allocation and deallocation intrinsically. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  sizeOf  size of object  sizeOf operator Asked in 1 Companies   rare | |||||||||||
| |||||||||||
Ans. Its used to access the object properties using the object reference or class properties using the Class Name. Moreover its used to access the classes and Interfaces of a package. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  operators | |||||||||||
| |||||||||||
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. && 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 | |||||||||||
| |||||||||||
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. >= => will result in error. => somewhat looks like lambda operator "->" | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  operators  >= operator  greater than and equal to operator  comparison operators | |||||||||||
| |||||||||||
Ans. Ternary operator , also called conditional operator is used to decide which value to assign to a variable based on a Boolean value evaluation. It is used as condition ? value1 : value2 For example int y = (x > 0) ? x:0; // assign x if it's greater than 0, else assign 0 | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  operator  ternary operator Basic | |||||||||||
| |||||||||||
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  operator overloading | |||||||||||
| |||||||||||
| |||||||||||