Random Questions


1) Can we assign a primitive to an Object in java?.

     Yes, from Java 5 the actual type is determined at compile time by the compiler.

      Example :

Object obj1 = 10; // type of java.lang.Integer
Object obj2 = false; // type of java.lang.Boolean
Object obj3 = 'C';   // type of java.lang.Character
2) Will widening primitive conversions result in an exception or loss of data?.

     No.A widening primitive conversion does not lose information about the overall magnitude of a numeric value.Despite the fact that loss of precision may occur, a widening primitive conversion never results in a run-time exception(spec)
class Test {
    public static void main(String[] args) {
        int big = 1234567890;
        float approx = big;
        System.out.println(big - (int)approx);
    }
}

output: -46

3) Does a Narrowing primitive conversion result in a runtime exception?

    Despite the fact that overflow, underflow, or other loss of information may occur, a narrowing primitive conversion never results in a run-time exception.
Conversion from float to int,double to byte, short, char, int, long, or float.

4) Give an example which undergoes both a widening  and narrowing conversion?

    byte to char. The byte first undergoes a widening conversion to int and then results in a narrowing conversion from int to char.

5) What is meant by widening reference conversion?

     A widening reference conversion exists from any reference type S to any reference type T, provided S is a subtype of T. example

       Object obj = new String(); 
       List list = new ArrayList();
       AbstractList list2 = new LinkedList();

6) What is meant by narrowing reference conversion?

     Narrowing reference conversions require a test at run-time to find out whether the actual reference value is a legitimate value of the new type. If not, then a ClassCastException is thrown.As array is a special object which implements Clonable and Serializable a narrowing conversion to any of the interface can be done.
       Cloneable c = new Long[10]; 
       Serializable s = new Long[10]; 
       Object o = new Long[10];
       Number[] n = new Long[10];
7) What are boxing conversions in Java?

    Boxing conversion converts expressions of primitive type to corresponding expressions of reference type.
you can convert primitive to its Wrapper type along with null(null to null boxing conversion is also possible).A boxing conversion may result in an OutOfMemoryError.

8) What are unboxing conversions in Java?

     Unboxing conversion converts expressions of reference type to corresponding expressions of primitive type.If reference is null, unboxing conversion throws a NullPointerException.

9) If we assign a null to a string what value will be reference point to?.

    A value of  "null".

10) What happens if we add an integer to an array of strings?
       Object x[]   =  new String[3];
          x[0]  = new Integer(0);

       An ArrayStoreException comes at line 2.

11)  If a class A extends B and B extends A then will it result in a Runtime Exception or a Compile time error.

       A compilation error.

12) What is "The Single Responsibility Principle" in Object Orientation?

       "THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A
CLASS TO CHANGE."If a class assumes more than one responsibility, then there will be more than one reason for it to change. If a class has more then one responsibility, then the responsibilities become coupled.
Changes to one responsibility may impair or inhibit the class’ ability to meet the others.Simply saying coupling and dependency increases between different objects.

13) What is meant by SOLID in Object Orientation Design?

      This represents an acronym for 5 design principles of Object Oriented programming and design.They are
   
InitialStands for
(acronym)
Concept
SSRP
Single responsibility principle
an object should have only a single responsibility.
OOCP
Open/closed principle
“software entities … should be open for extension, but closed for modification”.
LLSP
Liskov substitution principle
“objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program”. See also design by contract.
IISP
Interface segregation principle
“many client specific interfaces are better than one general purpose interface.
DDIP
Dependency inversion principle
one should “Depend upon Abstractions. Do not depend upon concretions.”
Dependency injection is one method of following this principle.
Refer : SOLID wiki

14) What is meant by Cohesion?

      Cohesion refers to the degree to which the elements of a module belong together.A class has high cohesion when it is designed around a set of related functions, and low cohesion when designed around unrelated functions. We want high cohesion.This is closely related to SRP principle in SOLID.A class should assume a single responsibility and all the code that supports the responsibility should be within the same module(class).If unrelated responsibilities creep into a module then cohesion decreases and the code becomes a maintenance nightmare.[wiki]

15) What is meant by Coupling?

      Coupling or Dependency is the degree to which each program module relies on each one of the other modules.Normally we require loose coupling so that changes in one module does not cause a ripple effect of changes in other modules.
Ex: MVC architecture separates the View,controlling and Business(model) logic and maintains a loose coupling between them
       Webservices provides loose coupling by using Message coupling.Here the data is sent in the form of messages and there is no platform or technology dependency.

16) What might be the disadvantage of loose coupling?
   
       Loose coupling is always desired but if your application is performance centered and if you don't care about maintenance then you can sacrifice maintainability for performance.Because for low coupling we need to follow a message coupling design which has the overhead of Message creation, translation, transmission and interpretation.

17) What are the disadvantages of tight coupling?

        Tightly coupled systems tend to exhibit the following developmental characteristics, which are often seen as disadvantages:
  • A change in one module usually forces a ripple effect of changes in other modules.
  • Assembly of modules might require more effort and/or time due to the increased inter-module dependency.
  • A particular module might be harder to reuse and/or test because dependent modules must be included.
18) What is Dependency Injection?
      
      Dependency injection is a software design pattern that allows a choice of component to be made at run-time rather than compile time.When using dependency injection, a consumer component specifies the service contract by interface, and the injector component selects an implementation on behalf of the dependent component.


19) What are the different types of Dependency Injection?.

     There are usually 3 types of dependency injection types implemented by most frameworks.
  1. Interface injection: in which the exported module provides an interface that its users must implement in order to get the dependencies at runtime.
  2. Setter injection: in which the dependent module exposes a setter method that the framework uses to inject the dependency.
  3. Constructor injection: in which the dependencies are provided through the class constructor.
20) What are the advantages of Dependency Injection?

      There are multiple advantages both at the design level and build level.

  1. Boiler plate can be reduced in the code and  delegated to the provider component.
  2. Loose coupling between different components.
  3. Provide alternate implementations to a service without code recompilation.
  4. Extend the functionality without any modification to code.
  5. Easy to test by injecting mock objects at Unit Test time.    
21) What is boxing and unboxing in Java?

     Boxing is the process of converting a primitive to its reference type and unboxing is converting from reference to primitive.

Example 1:
// boxing
Integer a = 1;
Long b = 2L;

//unboxing
int ia= a;
long ib = b;
Example 2:
Object c = 10;
Object d = 10L;

int ic = c; // compilation error
long id = d; // compilation error

d = c;  // no compilation error

System.out.println(d instanceof Integer); // prints true
Example 3:
Integer e = 10;
Integer f = e;
e = 11;

System.out.println(e);  // e is 10 becuase Integer is immutable
                        // boxing creates a new reference does not   update the same.
ArrayList<Integer> list = new ArrayList<Integer>(); 
list.add(10);                 // boxing
int returnedInt=list.get(0);  // unboxing

22)  What is the encoding in which java programs are written?
A)  Unicode.

23)  Can a digit be the first char/element of a java identifier?
A) No,a Java identifier should start with a letter.

24) What is the use of String.intern?
A) String.intern returns a canonical representation of the string, allowing interned strings to be compared using ==.

25) How integral types are stored in Java?
A) Integral types are byte, short , int and long, whose values are 8-bit,16-bit,32-bit and 64-bit signed two’s compliment integers, respectively, and char,whose values are 16-bit unsigned integer representing Unicode characters.       

26)  What is a signed 2’s compliment representation?

27)  What are floating-point types in Java?
A)  float and double are the floating-point types in Java. They are conceptually associated with the 32 bit single precision and 64 bit double precision IEEE 754 values and operations.

28) How is overflow in integral types handled in Java?
A) The integral operations does not indicate overflow in anyway; they wrap around on overflow.

29) Provide an example showing the overflow in Java?

30) What is the result if we compare 0.0 == -0.0?
A) true.Floating point data contains negative zero, positive zero, -ve infinity and +ve infinity.

31) What is the value of 0.0> -0.0?
A) false, as both are equal.

32) What is the value of 1.0/-0.0?
A) -ve Infinity.

33) What are the different types of reference types in Java?
A) There are 3 kinds of reference types:
     1) The class types
     2) The interface types
     3) The array types.

34) What additional property does all objects contain in Java?
A) Each object has an associated lock that is used by synchronization process.

35) Do arrays support the methods of class Object?
A) Yes

36) What are the different types of variables in Java?
A) There are 7 kinds of variables in Java
1)Class variable.
2)Instance variable.
3)Array components.
4)Method parameters.
5) Constructor parameters.
6)Exception-handler parameters.
7)Local variables.

37) What is a class variable?
A)  A class variable is a field of calls type declared using the keyword static within a class declared, or with or without the keyword static in an interface declaration.

38) What are array components?
A) Array components are unnamed variables created and initialized to default values whenever a new object that is an array is created.

39) What are method parameters?
A) Method parameters name argument values passed to a method. For every parameter declared in a method declaration, a new parameter variable is created each time the method is invoked.  

40) Are local variables automatically initialized?
A) No, they need to be initialized before using it.

41) Why are package names always in lowercase?
A) Packages are always in lowercase to avoid conflict with class and interface names.

No comments:

Post a Comment