Java Interview Questions and Answers – Part 4

Java Interview Questions and Answers
1. What is the difference between the Boolean & operator and the && operator?
  • If an expression involving the Boolean & operator is evaluated, both operands are evaluated. Then the & operator is applied to the operand.
  • When an expression involving the && operator is evaluated, the first operand is evaluated. If the first operand returns a value of true then the second operand is evaluated. The && operator is then applied to the first and second operands. If the first operand evaluates to false, the evaluation of the second operand is skipped.
2. What is the difference between the >> and >>> operators?
  • The >> operator carries the sign bit when shifting right.
  • The >>> zero-fills bits that have been shifted out.
3. Which Java operator is right associative?

The = operator is right associative.

4. Can a double value be cast to a byte?

Yes, a double value can be cast to a byte.

5. To what value, a String variable be automatically initialized?

The default value of an String type is null.

6. What is a transient variable?

Transient variable is a variable that may not be serialized.

7. To what value, a Boolean type variable be automatically initialized?

The default value of the Boolean type is false.

8. What is the difference between the prefix and postfix forms of the ++  operator?
  • The prefix form performs the increment operation and returns the value of the increment operation.
  • The postfix form returns the current value all of the expression and then performs the increment operation on that value.
9. What happens when you add a double value to a String?

The result is a String object.

10. How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
  • Unicode requires 16 bits and ASCII require 7 bits Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits.
  • UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.
11. How does Java handle integer overflows and underflow?

It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.

12. How many static initializes can we have ?

As many as we want, but the static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope.

13. What is the difference between an if statement and a switch statement?
  • The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed.
  • The switch statement is used to select among multiple alternatives. It uses an int
    expression to determine which alternative should be executed.
14. What is numeric promotion?
  • Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place.
  • In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required.
15. Which characters may be used as the second character of an identifier, but not as the first character of an identifier?

The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.

16. What restrictions are placed on the values of each case of a switch statement?

During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value.

17. What is a native method?

A native method is a method that is implemented in a language other than Java.

Java applications can call code written in C, C++, or assembler. This is sometimes done for performance and sometimes to access the underlying host operating system or GUI API using the JNI. The steps for doing that are:

  • First write the Java code and compile it
  • Then create a C header file
  • Create C stubs file
  • Write the C code
  • Create shared code library (or DLL)
  • Run application

It is possible to execute C or C++ code by a Java application. This process is done by taking certain performance issues and access to the underlying host operating systems into consideration. To execute these programs, Java Native Interface framework is used.

18. What are order of precedence and associativity, and how are they used?

Order of precedence determines the order in which operators are evaluated in expressions. Associatity determines whether an expression is evaluated left-to-right or right-to-left.

19. What is the difference between inner class and nested class?
  • When a class is defined within a scope of another class, then it becomes inner class.
  • If the access modifier of the inner class is static, then it becomes nested class.
20. What is the difference between a public and a non-public class?

A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package.

Profile photo of Siva

About Siva

Senior Hadoop developer with 4 years of experience in designing and architecture solutions for the Big Data domain and has been involved with several complex engagements. Technical strengths include Hadoop, YARN, Mapreduce, Hive, Sqoop, Flume, Pig, HBase, Phoenix, Oozie, Falcon, Kafka, Storm, Spark, MySQL and Java.

Leave a comment

Your email address will not be published. Required fields are marked *