Udemy Interview Questions Flashcards
What is JVM? Why is Java called the ‘Platform Independent Programming Language’?
JVM, or the Java Virtual Machine, is an interpreter which accepts ‘Bytecode’ and executes it.
Java has been termed as a ‘Platform Independent Language’ as it primarily works on the notion of ‘compile once, run everywhere’
What is the Difference between JDK and JRE?
The “JRE” is the Java Runtime Environment. I.e., the JRE is an implementation of the Java Virtual Machine which actually executes Java programs.
Typically, each JDK contains one (or more) JRE’s along with the various development tools like the Java source compilers, bundling and deployment tools, debuggers, development libraries, etc.
What does the ‘static’ keyword mean?
We are sure you must be well-acquainted with the Java Basics. Now that we are settled with the initial concepts, let’s look into the Language specific offerings.
Static variable is associated with a class and not objects of that class. For example:
public class ExplainStatic { public static String name = "Look I am a static variable"; } We have another class where-in we intend to access this static variable just defined.
public class Application { public static void main(String[] args) { System.out.println(ExplainStatic.name) } } We don’t create object of the class ExplainStatic to access the static variable. We directly use the class name itself: ExplainStatic.name
What are the Data Types supported by Java? What is Autoboxing and Unboxing?
This is one of the most common and fundamental Java interview questions. This is something you should have right at your finger-tips when asked. The eight Primitive Data types supported by Java are:
Byte : 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive)
Short : 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
Int : 32-bit signed two’s complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive)
Long : 64-bit signed two’s complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive)
Float
Double
Autoboxing: The Java compiler brings about an automatic transformation of primitive type (int, float, double etc.) into their object equivalents or wrapper type (Integer, Float, Double,etc) for the ease of compilation.
Unboxing: The automatic transformation of wrapper types into their primitive equivalent is known as Unboxing.
What is the difference between STRINGBUFFER and STRING?
String object is immutable. i.e , the value stored in the String object cannot be changed. Consider the following code snippet:
String myString = “Hello”;
myString = myString + ” Guest”;
When you print the contents of myString the output will be “Hello Guest”. Although we made use of the same object (myString), internally a new object was created in the process. That’s a performance issue.
StringBuffer/StringBuilder objects are mutable: StringBuffer/StringBuilder objects are mutable; we can make changes to the value stored in the object. What this effectively means is that string operations such as append would be more efficient if performed using StringBuffer/StringBuilder objects than String objects.
String str = “Be Happy With Your Salary.’’
str += “Because Increments are a myth”;
StringBuffer strbuf = new StringBuffer();
strbuf.append(str);
System.out.println(strbuf);
The Output of the code snippet would be: Be Happy With Your Salary. Because Increments are a myth.
What is Function Over-Riding and Over-Loading in Java?
This is a very important concept in OOP (Object Oriented Programming) and is a must-know for every Java Programmer.
Over-Riding: An override is a type of function which occurs in a class which inherits from another class. An override function “replaces” a function inherited from the base class, but does so in such a way that it is called even when an instance of its class is pretending to be a different type through polymorphism. That probably was a little over the top. The code snippet below should explain things better.
public class Car { public static void main (String [] args) { Car a = new Car(); Car b = new Ferrari(); //Car ref, but a Ferrari object a.start(); // Runs the Car version of start() b.start(); // Runs the Ferrari version of start() } } class Car { public void start() { System.out.println("This is a Generic start to any Car"); } } class Ferrari extends Car { public void start() { System.out.println("Lets start the Ferrari and go out for a cool Party."); } } Over-Loading: Overloading is the action of defining multiple methods with the same name, but with different parameters. It is unrelated to either overriding or polymorphism. Functions in Java could be overloaded by two mechanisms ideally:
Varying the number of arguments.
Varying the Data Type.
class CalculateArea{
void Area(int length){System.out.println(length2);}
void Area(int length , int width){System.out.println(lengthwidth);}
public static void main(String args[]){ CalculateArea obj=new CalculateArea(); obj.Area(10); // Area of a Square obj.Area(20,20); // Area of a Rectangle } }
What is Constructors, Constructor Overloading?
Constructors form the basics of OOPs, for starters.
Constructor: The sole purpose of having Constructors is to create an instance of a class. They are invoked while creating an object of a class. Here are a few salient features of Java Constructors:
Constructors can be public, private, or protected. If a constructor with arguments has been defined in a class, you can no longer use a default no-argument constructor – you have to write one. They are called only once when the class is being instantiated. They must have the same name as the class itself. They do not return a value and you do not have to specify the keyword void. If you do not create a constructor for the class, Java helps you by using a so called default no-argument constructor. public class Boss{ String name;
Boss(String input) { //This is the constructor name = "Our Boss is also known as : " + input; }
public static void main(String args[]) { Boss p1 = new Boss("Super-Man"); } } Constructor overloading: passing different number and type of variables as arguments all of which are private variables of the class. Example snippet could be as follows:
public class Boss{ String name; Boss(String input) { //This is the constructor name = "Our Boss is also known as : " + input; } Boss() { name = "Our Boss is a nice man. We don’t call him names.”; } public static void main(String args[]) { Boss p1 = new Boss("Super-Man"); Boss p2 = new Boss(); } }
What is the difference between Throw and Throws in Java Exception Handling (remember this question?)
Throws: A throws clause lists the types of exceptions that a method might throw, thereby warning the invoking method – ‘Dude. You need to handle this list of exceptions I might throw.’ Except those of type Error or RuntimeException, all other Exceptions or any of their subclasses, must be declared in the throws clause, if the method in question doesn’t implement a try…catch block. It is therefore the onus of the next-on-top method to take care of the mess.
public void myMethod() throws PRException {..} This means the super function calling the function should be equipped to handle this exception. public void Callee() { try{ myMethod(); }catch(PRException ex) { ...handle Exception.... } } Using the Throw: If the user wants to throw an explicit Exception, often customized, we use the Throw. The Throw clause can be used in any part of code where you feel a specific exception needs to be thrown to the calling method.
try{ if(age>100){throw new AgeBarException(); //Customized ExceptioN }else{ ....} } }catch(AgeBarException ex){ ...handle Exception..... }
What is the difference between ArrayList and LinkedList ?
Please pay special attention as this is probably one of the most widely asked interview questions.
We aren’t going to state the properties of each in this question. What we are looking for are the differences. The prime areas where the two stand apart are as follows :
Arraylist: Random access, Only objects can be added.
Linklist:
Sequential access.The control traverses from the first node to reach the indexed node.
The LinkedList is implemented using nodes linked to each other. Each node contains a previous node link, next node link, and value, which contains the actual data