Core Java Interview Questions & answer- 6

What is the range of the char type?
The range of the char type is 0 to 2^16 - 1.

What is the range of the short type?
The range of the short type is -(2^15) to 2^15 - 1.

Why isn't there operator overloading?
Because C++ has proven by example that operator overloading makes code almost impossible to maintain.

What does it mean that a method or field is "static"?
Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class.
Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That's how library methods like System.out.println() work. out is a static field in the java.lang.System class.

Is null a keyword?
The null value is not a keyword.

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.

Is the ternary operator written x : y ? z or x ? y : z ?
It is written x ? y : z.

How is rounding performed under integer division?
The fractional part of the result is truncated. This is known as rounding toward zero.

If a class is declared without any access modifiers, where may the class be accessed?
A class that is declared without any access modifiers is said to have package access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package.

Does a class inherit the constructors of its superclass?
A class does not inherit constructors from any of its superclasses.

Name the eight primitive Java types.
The eight primitive types are byte, char, short, int, long, float, double, and boolean.

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.

What is the difference between a while statement and a do statement?
A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.

What modifiers can be used with a local inner class?
A local inner class may be final or abstract.

When does the compiler supply a default constructor for a class?
The compiler supplies a default constructor for a class if no other constructors are provided.

If a method is declared as protected, where may the method be accessed?
A protected method may only be accessed by classes or interfaces of the same package or by subclasses of the class in which it is declared.

What are the legal operands of the instanceof operator?
The left operand is an object reference or null value and the right operand is a class, interface, or array type.

Are true and false keywords?
The values true and false are not keywords.

What happens when you add a double value to a String?
The result is a String object.

What is the diffrence between inner class and nested class?
When a class is defined within a scope od another class, then it becomes inner class.
If the access modifier of the inner class is static, then it becomes nested class.

Can an abstract class be final?
An abstract class may not be declared as final

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

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.

To what value is a variable of the boolean type automatically initialized?
The default value of the boolean type is false

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.

What restrictions are placed on method overriding?
Overridden methods must have the same name, argument list, and return type.
The overriding method may not limit the access of the method it overrides.
The overriding method may not throw any exceptions that may not be thrown by the overridden method.

No comments:

Post a Comment