Wednesday 21 August 2013

Top Most Java Operators and assignments Interview Questions



  1. given,
class A{}
class B extends A{}
B b = null;
A a = b;
B b1= (B)a;
The above code fragment doesn’t  cause NullPointerException since we are casting amongst the members of inheritance tree. Hence b1 will have the value “null”.
Similaraly,
given,
int[] i = null;
Cloneable c = i;
i = (int [] ) c;

The above code fragment doesn’t result in runtime error(NullPointerException) since  any array extends Cloneable interface.
  1. All arrays implement Cloneable interface and java.io.Serializable interface
  2. given,
long [] l = {1,2,3,4,5};
int [] i  = {10,20,30};
l[0]= i[0];//legal and correct
l = i; //causes compiler error
l = (long[])i;//causes error
  1. given,
final short s1 = 1;
final char c1 = 2;
short s2 = 3;
char c2 = 4;
byte b1,b2,b3,b4;
remember the following,
b1 = s1;//legal because compiler applies implicit casting since it is final variable.
b2 = c1;//legal because compiler applies implicit casting since it is final variable.
b3 = s2;//illegal because compiler doesn’t apply implicit casting.
b4 = c2;//illegal because compiler doesn’t apply implicit casting.

  1. Important formula,
-x = ~x +1;
and
~x = -x-1;
  1. if ,
int i1;
int i2;
int i3;
i1 = <<someNumber>>;
i2 = <<someComplexMethodName>>();
i3 = i1<<i2;
if i2 is greater than 31 then “i2&0x1f” is used in its place.
  1. The assignment operators are,
   =
+=
-=
*=
/=
%=
<<=
>>=
>>>=
&=
^=
|=

Note:
There is no assignment operator like &&=, ||= , ~=.

  1. given,
byte b = 1;
long l = 1000;
b += l;//line 3
the above fragment will not cause compiler error since compiler applies implicit casting before executing the line number 3.the above code fragment will be executed as follows.
·         b += l;
·         b = (byte)(b+l);
·         b = (byte(1+1000);
·         b = someValue;
In general ,
E1 op= E2;
            can be written as E1 = (type)(E1 op E2);
            here type is the datatype of the operand E1 and “op” is the operator applied.

given,
interface inter{}
class B implements inter {}
class sub implements B {}

class Test
{
public static void main(String [] args)
{
            B b = new B();
            I i = b;
            sub s = (sub)b;//line 1
}}

  1. At line one compiler error doesn’t occur because we applying explicit casting on members of the same inheritance tree. Thus at line 1 runtime error and not compiler error is caused. This is be cause B is superclass of sub and an instance of B cannot be converted into a type of sub class.

class Test2
{
            public static void main(String [] args)
            {
                        B[] b = {new B()};
                        sub[] s = new sub[1];
                        Object obj = b;
                        s = (sub[])obj;//line 1
            }
}
  1. At line one a runtime error and not compile time error is caused.
  2. given,
byte a = 1;
byte b = 2;
byte c = (byte)a+b;// Line 1
line 1 causes compiler error since casting is done only to a and not to sum of
a and b.As a result the RHS will be int and LHS will be byte which results in compiler error.

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Labels

Core Java programming core java interview question Core Java Faq's Servlets coding database jsp-servlet spring Java linux unix interview questions java investment bank Web Services Interview investment bank mysql Senior java developer interviews best practices java collection tutorial RMI SQL Eclipse FIX protocol tutorial tibco J2EE groovy java questions SCJP grails java 5 tutorial jdbc beginner error and exception Design Patterns Java Programming Tutorials fundamentals general object oriented programming xml Java Programs Hibernate Examples Flex JAMon Java xml tutorial logging Jsp Struts 2.0 Sybase and SQL Server debugging java interviews performance FIX Protocol interview questions JUnit testing WebSphere date and time tutorial experienced java IO tutorial java concurrency thread Ejb Freshers Papers IT Management Java Exapmle Java Script SQL and database tutorial examples Scwcd ant tutorials concurrency example and tutorial future state homework java changes java threading tricky Agile Business of IT Development JSTL Java JSON tutorial Java multithreading Tutorials PM Scrum data structure and algorithm java puzzles java tips testing tips windows 8 5 way to create Singleton Object Architect Interview Questions and Answers Architecture Architecure Bluetooth server as swing application that searches bluetooth device in 10 meter circle and show all devices. You can send file to any bluetooth device. C Programming CIO Callable Statement in Java Circular dependency of Objects in Java Comparable Example in Collection Custom annotation in Java Developer Interview Divide and rule example in java Drupal Example of Singleton Pattern FIX protocol ForkJoin Example in Java 7 Get data from dynamic table with Java Script Git HTML and JavaScript Health Hello World TCP Client Server Networking Program Hibernate Basics Hibernate Interview Question Answer J2EE Interview Question And Answers J2ME GUI Program JEE Interview QA JMS interview question Java J2EE Hibernate Spring Struts Interview Question Java System Property Java Threads Manager Portlets Provident Fund Read data from any file in same location and give the required result. Reading Properties File in Java Redpoint Rest WebService Client Rest Webservice Test SAL join with ven diagram SCP UNIX COMMAND SSL Singleton Pattern in Java Spring Bean Initialization methods and their order Spring Interview Questions Struts Struts 2.0 Basics Struts 2.0 Design Pattern Submit Html Form With Java Script On The Fly Unix executable For Java Program XOM DOM SAX XP books computers core java; core java; object oriented programming data structure; java investment bank; design pattern dtd duplicate rows in table get browser name with jquery grails podcast inner class java beginners tutorial java cache java networking tutorial java spring java util; java collections; java questions java.java1.5 linked list mailto function with all browser oracle database oracle duplicate rows orm schema social spring mvc questions struts transaction tricks tweet windows xslt