Skip to main content

Job Interview Question Software Engineering

Job Interview Question Software Engineering

What is constructor?
A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values.
Rules  for  creating constructor?
  1. Constructor name must be same as its class name
  2. Constructor must have no explicit return type
What  is the types of constructor?
  1. Default constructor (no-arg constructor)
  2. Parameterized constructor
Default Constructor?
A constructor that have no parameter is known as default constructor.

Does constructor return any value?Why?
Constructor must not have a return type. By definition, if a method has a return type, it's not a constructor.
Is constructor inherited?
java constructors are not inherited

Can you make a constructor final?
can't constructors be final, static, or abstract in Java

Java Copy Constructor?
To create an object identical to the original one. To prevent the original being altered.
Following is an example Java program that shows a simple use of copy constructor.
// filename: Main.java

class Complex {

    private double re, im;
     
    // A normal parametrized constructor
    public Complex(double re, double im) {
        this.re = re;
        this.im = im;
    }
     
    // copy constructor
    Complex(Complex c) {
        System.out.println("Copy constructor called");
        re = c.re;
        im = c.im;
    }
      
}
Difference  b/w  Constructor and  Method?
Constructor is used to create an instance of class. while method is used to perform a specific operation
·         Constructors :
·         Constructors doesn't returns any value.
·         References and pointers cannot be used on constructors and destructors because their addresses cannot be taken.
·         Constructors cannot be declared with the keyword virtual.
·         If the constructors is defined as private, we can not create instance of that class.

Method:
·          In methods we can define variables.These variables scope is within methods only.
·         If you declare a public variable , it will be accessed in all mehtods.
·         Methods may/maynot contains return type.
·         Methods could be inhereted in derived class(Note That mehtod should be Public)

What is static variable?
If you declare any variable as static, it is known static variable.
1.    Static variables are initialized when class is loaded.
2.    Static variables in a class are initialized before any object of that class can be created.
3.    Static variables in a class are initialized before any static method of the class runs.
static final variables are constants.

What is static method?
If you apply static keyword with any method, it is known as static method.
  • A static method belongs to the class rather than object of a class.
  • A static method can be invoked without the need for creating an instance of a class.
  • static method can access static data member and can change the value of it.
What are the restrictions for static method?
There are two main restrictions for the static method. They are:

1.    The static method can not use non static data member or call non-static method directly.
2.    this and super cannot be used in static context.
What is static block?
  • Is used to initialize the static data member.
  • It is executed before main method at the time of classloading.
  • example:
  • class A2{  
  •   static{System.out.println("static block is invoked");}  
  •   public static void main(String args[]){  
  •    System.out.println("Hello main");  
  •   }  
  • }  
  • Output:static block is invoked
       Hello main
Why  main method is static?
Because object is not required to call static method if it were non-static method, jvm create object first then call main() method that will lead the problem of extra memory allocation.
Can we execute a program without main() method?
Ans) Yes, one of the way is static block but in previous version of JDK not in JDK 1.7.
1.    class A3{  
2.      static{  
3.      System.out.println("static block is invoked");  
4.      System.exit(0);  
5.      }  
6.    }  
Types of Inheritance  in  Java?
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later

What is Diamond Problem?
http://www.programmerinterview.com/images/Diamond_inheritance.png
The problem with having an inheritance hierarchy like the one shown in the diagram above is that when we instantiate an object of class D, any calls to method definitions in class A will be ambiguous – because it’s not sure whether to call the version of the method derived from class B or class C.

Why  multiple inheritance is not supported in Java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.
Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.
1.    class A{  
2.    void msg(){System.out.println("Hello");}  
3.    }  
4.    class B{  
5.    void msg(){System.out.println("Welcome");}  
6.    }  
7.    class C extends A,B{//suppose if it were  
8.       
9.     Public Static void main(String args[]){  
10.    C obj=new C();  
11.    obj.msg();//Now which msg() method would be invoked?  
12. }  
13. }  //RESULT : Compile Time Error
What is this in Java?
http://www.javatpoint.com/this-keyword
Here is given the 6 usage of java this keyword.
  1. this keyword can be used to refer current class instance variable.
  2. this() can be used to invoke current class constructor.
  3. this keyword can be used to invoke current class method (implicitly)
  4. this can be passed as an argument in the method call.
  5. this can be passed as argument in the constructor call.
  6. this keyword can also be used to return the current class instance.


What is super in Java?
The super keyword in java is a reference variable that is used to refer immediate parent class object.
  1. super is used to refer immediate parent class instance variable.
  2. super() is used to invoke immediate parent class constructor.
  3. super is used to invoke immediate parent class method.
Can you use this() and super() both in a constructor?

Which class is the superclass for every class.
Object

What is object cloning?
The object cloning is a way to create exact copy of an object. For this purpose, clone() method of Object class is used to clone an object.

Method Overloading
There are two ways to overload the method in java

1.    By changing number of arguments
2.    By changing the data type
Why method overloading is not possible by changing the return type in java?

In java, method overloading is not possible by changing the return type of the method because there may occur ambiguity. Let's see how ambiguity may occur:
because there was problem:
1.    class Calculation3{  
2.      int sum(int a,int b){System.out.println(a+b);}  
3.      double sum(int a,int b){System.out.println(a+b);}  
4.      
5.      public static void main(String args[]){  
6.      Calculation3 obj=new Calculation3();  
7.      int result=obj.sum(20,20); //Compile Time Error  
8.      
9.      }  
10. }  
Can we overload main() method?
Yes, by method overloading. You can have any number of main methods in a class by method overloading. Let's see the simple example:
1.    class Overloading1{  
2.      public static void main(int a){  
3.      System.out.println(a);  
4.      }  
5.        
6.      public static void main(String args[]){  
7.      System.out.println("main() method invoked");  
8.      main(10);  
9.      }  
10. }  

Can we override static method?

No, you cannot override static method in Java because method overriding is based upon dynamic binding at runtime and static methods are bonded using static binding at compile time. 
Can we overload methods that differ only by static keyword?
We cannot overload two methods in Java if they differ only by static keyword (number of parameters and types of parameters is same). See following Java program for example. This behaviour is same in C++ (See point 2 ofthis).
// filename Test.java
public class Test {
    public static void foo() {
        System.out.println("Test.foo() called ");
    }
    public void foo() { // Compiler Error: cannot redefine foo()
        System.out.println("Test.foo(int) called ");
    }
    public static void main(String args[]) {
        Test.foo();
    }
}
Can we override the overloaded method?

Yes, sure.
  • Method overriding is used to provide specific implementation of a method that is already provided by its super class.
  • Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
  1. method must have same name as in the parent class
  2. method must have same parameter as in the parent class.
  3. must be IS-A relationship (inheritance).
Can you have virtual functions in Java?
Yes, In Java, all non-static methods are by default "virtual functions." Only methods marked with the keyword final, which cannot be overridden, along with private methods, which are not inherited, are non-virtual.

00P v/s OBP ( Object Based Programming)

Javahttp://images.intellitxt.com/ast/adTypes/icon1.png is an object oriented , means it uses all the features of oops like u can use inheritence , u can create an class and objects .
Javascipt is an Object based means eventhough u can't use all the features of oops like inheitence etc. but ur making use of object concept. 

What are different aggregation types in UML?


What is Aggregation and Composition..?
Association
Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation.
http://javapapers.com/wp-content/uploads/2010/06/association.jpg
Example: A Student and a Faculty are having an association.
Aggregation
Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object. Aggregation is also called a “Has-a” relationship.
http://javapapers.com/wp-content/uploads/2010/06/aggregation.jpg
Composition
Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.
http://javapapers.com/wp-content/uploads/2010/06/composition.jpg
Example: A class contains students. A student cannot exist without a class. There exists composition between class and students.

What is the difference bw Method Overriding and Overloading.?

Method overloading deals with the notion of having two or more methods in the same class with the same name but different arguments.
Method overriding means having two methods with the same arguments, but different implementations. One of them would exist in the parent class, while another will be in the derived, or child class. The @Override annotation, 
Can you override private or static method in Java?
Can we Override static methods in java?
We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’.
Can we Override private methods in java?
You can't override a private method, but you can introduce one in a derived class without a problem. This compiles fine:
class Base
{
   private void foo()
   {
   }
}

class Child extends Base
{
    private void foo()
    {
    }
}
What is the difference bw Interface, Abstract and concrete Class..?

Interview Question: What is the difference between an abstract class and an interface?
Answer from the perspective of Java 7 (and before)

Abstract classes may have fully implemented and unimplemented methods, but interfaces may not
Abstract classes may contain static methods while interfaces may not
Abstract classes may have instance variables of any kind, whereas interfaces can have only public static final variables 
All methods in an interface are public and abstarct, but abstract classes may have implemented methods of any visibility and abstract methods that are either public or protected 

Answer from the perspective of Java 8 (and after)

Abstract classes may have fully implemented methods;  interfaces may also have implemented methods, but they must bear the keyword “default” 
Abstract classes may have instance variables of any kind, whereas interfaces can have only public final static variables 
All methods in an interface are public, but abstract classes may have implemented methods of any visibility and abstract methods that are either public or protected 

Class with private access modifier..?

The private access modifier is accessible only within class.
Inside Rule ( Inside can be anything)

1.class inside class

class Outer 
private String text1 = "I am Outer Class!";
public void callinner()
 {
        Inner iobj = new Inner();
        iobj.printText();
class Inner 
{
private String text2 = “ I am Inner Class!”;
  void printText() 
System.out.println(text1);
System.out.println(text2);}
 } 
}
class Main()
{
public static void main(String args[])
{
// Object for outer class
  Outer ot = new Outer();
ot.callinner(); 
// object for inner class
Outer.Inner obj = new Outer().new Inner();
Obj.printText();
}
}
Output


I am Outer Class!
I am Inner Class!
I am Outer Class!
I am Inner Class!

2. Interface inside interface
1.    interface interface_name{  
2.     ...  
3.     interface nested_interface_name{  
4.      ...  
5.     }  
6.    }   
3.class inside interface
Can we define a class inside the interface?
Yes, If we define a class inside the interface, java compiler creates a static nested class. Let's see how can we define a class within the interface:
1.    interface M{  
2.      class A{}  
3.    }  
4.interface inside class ( always static)
1.    class class_name{  
2.     ...  
3.     interface nested_interface_name{  
4.      ...  
5.     }  
6.    }  

Comments

Popular posts from this blog

ifference between @RestController and @Controller

Difference between @RestController and @Controller Annotation in Spring MVC and REST Read more:  http://javarevisited.blogspot.com/2017/08/difference-between-restcontroller-and-controller-annotations-spring-mvc-rest.html#ixzz4sbGy2Glh The  @RestController  annotation in Spring MVC is nothing but a combination of  @Controller  and  @ResponseBody  annotation. It was added into Spring 4.0 to make the development of RESTful Web Services in Spring framework easier. If you are familiar with the  REST web services  you know that the fundamental difference between a web application and a REST API is that the response from a web application is generally view (HTML + CSS + JavaScript) while REST API just return data in form of JSON or XML. This difference is also obvious in the  @Controller  and  @RestController  annotation. The job of  @Controller  is to create a Map of model object and find a view but  @RestContr...

Presentation topics

Topics for presentation : Knockout.js, Angular.js, Node,js, Kendo UI, MVC 4, Nuget, Ninject, Unity Framework, Dependency Injection , PMC Package Manager Control, Mordenizer, Moq, Json, Html5, Entity Framework code1st. Spring.net Castle Windsor , Structure Map and Microsoft Unity ,
What happens when you compile/run the following code: class MyClass { public static void main(String[] args) { new MyClass(); } Ans: It executes and create a object for that class without reference. The output of the program is nothing.