Thursday, May 28, 2020

Interview @ 1mg : Round -1


Hi Friends, In this post, I'm sharing Java interview questions asked in 1mg in round - 1.

You can also go through my other java interview posts:


Interview Questions @ 1mg:

Question 1:

What is System.out, System.in and System.err?


Answer:

out, in and err all are fields in System class.

out: The standard output stream.This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.
Basically, it gives PrintStream. And all print() methods belong to class PrintStream.

public static final PrintStream out

in: The standard input Stream. This stream is already open and ready to supply input data. typically this stream corresponds to keyboard input or other input source specified by the host environment or user.

public static final InputStream in

err: The standard error output stream. This stream is already open and ready to accept output data.
By convention, this output stream is used to display error messages.

public static final PrintStream err 




Question 2:

What is the difference between Class.forName() and Class.forName().newInstance() methods?

Answer:

Lets take an example to understand the difference better:

public class Demo{

    public Demo(){

        
    }

    public static void main(String[] args){

        Class clazz = Class.forName("Demo");
        Demo demo = (Demo)clazz.newInstance();
    }

}

Class.forName() returns the Class object associated with the class or interface with the given string name.

Then, calling clazz.newInstance() creates a new instance of the class represented by this Class object.
This class is instantiated as if by a new expression with an empty argument list.
 



Question 3:

Why non-static variables are not allowed in static methods?

Answer:


non-static variables means instance variables and they are only initialized when an instance is created. As static methods can be called without creating an instance , so accessing non-initialized instance variable is wrong as instance doesn't exist.

So, only way to access non-static variable in static method is that , just create instance of class in static method and access that variable.

public class StaticTest{

    private int count = 0;
   
    public static void main(String[] args){

        StaticTest test = new StaticTest();

        test.count++;

    }

}




Question 4:

What is the difference between HTTP HEAD and GET verbs?

Answer:

HTTP HEAD is almost identical to GET, but without the response body. Means in HTTP HEAD, we don't get any response body.

In other words, if GET /users returns a list of users, then HEAD /users will make the same request , but will not return the list of users.

HEAD requests are useful for checking what a GET request will return before actually making a GET request - like before downloading a large file or response body. 




Question 5:

What is the difference between HTTP GET and POST methods?

Answer:

There are multiple differences between GET and POST methods:

  • GET is used to request data from a specified resource. POST is used to send data to server to create a resource.
  • GET requests can be cached. POST requests cannot be cached.
  • GET requests remain in the browser history. POST requests do not remain in the browser history.
  • GET requests can be bookmarked. POST requests cannot be bookmarked.
  • GET requests have length restrictions. POST requests have no restrictions on data length. 




Question 6:

Is there any speed increase while indexing a table?  And will indexing every column defeat the purpose of indexing?

Answer:

Indexing any table, either memory or file system based, will speed up queries that select or sort results based on that column. 
This is because the index works like a tree structure and the search distance depends upon the depth of the tree, which increases a lot slower than the row count of the column.

Indexing every column doesn't defeat the purpose of the index, but it will slow up inserts and updates because those changes will cause an update of every index of that table.
Also, the indexes take up space on the database server.  




Question 7:

What is covariant return  type?

Answer:

In covariant return type, parent's instances can be replaced with child's instances.

e.g.:


class WildAnimal{

    public String willYouBite(){
        return "Yes";
    }

}

class Lion extends WildAnimal{

    public String whoAreYou(){

        return "Lion";
    }

}


class BengalTiger extends WildAnimal{

    public String whoAreYou(){

       return "Tiger";
    }
}


class Zoo{

    public WildAnimal getWildAnimal(){

        return new WildAnimal();
    }

}


class AfricaZoo extends Zoo{

    @Override
    public Lion getWildAnimal(){

        return new Lion();
    }

}

class IndiaZoo extends Zoo{

    @Override
    public BengalTiger getWildAnimal(){

        return new BengalTiger();
    }
}


public class Covariant{

    public static void main(String[] args){

        AfricaZoo africaZoo = new AfricaZoo();
        System.out.println(africaZoo.getWildAnimal().whoAreYou());
    }

}

So, in class AfricaZoo, parent class WildAnimal is replaced by child class Lion while overriding method.




That's all for this post.
Thanks for reading!!

No comments:

Post a Comment

CAP Theorem and external configuration in microservices

 Hi friends, In this post, I will explain about CAP Theorem and setting external configurations in microservices. Question 1: What is CAP Th...