Java Interview Questions Flashcards
(58 cards)
What is the difference between Declaration and Definition in Java?
Declaration: If you just declare a class or method/function or variable without mentioning anything about what that class or method/function or variable looks like is called as declaration in Java.
Definition: If you define how a class or method/function or variable is implemented then it is called definition in Java.
When we create an interface or abstract class, we simply declare a method/function but
not define it.
What is a Class in Java?
A class can be defined as a collection of objects. It is the blueprint or template that describes the state and behavior of an object.
1 public class MyClass{ //Class name (MyClass) declaration 2int a = 9; // Variable declaration 3int b = 99; 4public void myMethod(){ //Method (myMethod) declaration 5int sum=a+b;
What is an Object in Java?
An object is an instance of a class. Objects have state (variables) and behavior
(methods).
Example: A dog is an object of Animal class. The dog has its states such as color,
name, breed, and behaviors such as barking, eating, wagging her tail.
Differences between object and class ?
Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.
A Class member is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class can change. The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed. An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.
//Murodil Answer: Class: - Is a template/blueprint for objects - We can define properties and methods in it - Class is an idea Object; - is always created from a class - Cannot exist without a class - Using objects we can use class’s methods and variables
What’s the difference between primitives and objects?
Primitives:
- Build into Java and foundation of all other types
- Have a single value and no behaviour
- int j=25; j=j+10; print j; j>5 etc; char ch = ‘q’;
Objects:
- Is created from a class
- Can have multiple values/properties and can have behaviour(methods)
- str.trim; str.toUpperCase;
What is the difference between equals() and == ?
== -> is a reference comparison, i.e. both objects point to the same memory
location
.equals() -> evaluates to the comparison of values in the objects
1- Main difference between .equals() method and == operator is that one is method and
other is operator.
2- We can use == operators for reference comparison (address comparison) and
.equals() method for content comparison. In simple words, == checks if both objects
point to the same memory location whereas .equals() evaluates to the comparison of
values in the objects.
What is the difference continue and break statement?
Break leaves a loop, continue jumps to the next iteration.
break to terminate a for, while, or do-while loop
What are the 5 different types of operators?
Arithmetic Operator, +,-,%,/,* Relational Operator, >=, <= Logical Operator, && , || Ternary Operator and. Result = testStatement ? value1 : value2 Assignment Operator int n = 3;
What is constructor and purpose of default constructor?
A constructor that have no parameter is known as default constructor.
Default constructor provides the default values to the object like 0, null etc. depending
on the type.
Explain public static void main(String args[]). (PSVM)
● public : Public is an access modifier, which is used to specify who can access
this method. Public means that this Method will be accessible by any Class.
● static : It is a keyword in java which identifies it is class based i.e it can be
accessed without creating the instance of a Class.
● void : It is the return type of the method. Void defines the method which will not
return any value.
● main: It is the name of the method which is searched by JVM as a starting point
for an application with a particular signature only. It is the method where the main
execution occurs.
● String args[] : It is the parameter passed to the main method.
Difference between static class ,static method and static block ?
Static Class ● A Class can be made static only if it is a nested Class. The nested static class can be accessed without having an object of outer class. Static Block ● Static block is mostly used for changing the default values of static variables.This block gets executed when the class is loaded in the memory. ● A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program. Static Methods ● Static Methods can access class variables without using object of the class. It can access non-static methods and non-static variables by using objects. Static methods can be accessed directly in static and non-static methods.
What is the purpose of toString() method?
It’s used to retrieve the object information in String format. This method called by
Sysout.println internally. By default toString() returns the hashcode from the Object
class
What is OVERLOADING Method?
Definition 1: Best Answer: Must have Same Name , Must have Different parameters
Definition 2: Overloading allows different methods to have same name, but different
signatures where signature can differ by number of input parameters or type of input
parameters or both.
Definition 3: There are three ways to overload a method.
● Parameters with different data types
● Parameters with different sequence of a data types
● Different number of parameters
What is Method Signature?
In the Java programming language, a method signature is the method name and the
number, type and order of its parameters.
Consist of: method name, number of parameters and their types:
Public static void m1(int n, String str) {}
Above m1 method signature:
2 parameters -> 1 int, 1 string
*Return type, or other modifiers are not part of method signature
What is Java Collection?
Collection is a framework in java to store objects and manipulate them.
Two main “root” interfaces of Java Collection classes
The Collection Interface (java.util.Collection)
Map Interface (java.util.Map)
Not: ArrayList is part of Java Collections, that is one of the most popular q’s.
What is Array And ArrayList?
Array:
- Array is a container object that holds a fixed number of values of a single data
type.
ArrayList:
- Dynamic array, internally it is build on Arrays.
- Part of java collections, and one of the most popular collection in use.
Difference Array and Arraylist?
*Array is part of core java programing and has special syntax but arraylist is part of
collection framework and implement list interface
- Arrays are fixed size and size cannot be changed:
int[] arr = new int[5];
The arr size/length cannot be modified, it is fixed. - Arrays can accept both primitives and Objects.
double[] d = {23.54, 23.1};
String[] s = {“One”, “Five”}; - Arrays can be multi dimensional
String[][][] data = new String [5][3][2];
String[][] matrix = new String [5][3]; - Arrays are built into core Java
- ArrayLists are resizable and size of elements can be modified. It can increase and
decrease.
ArrayList nums = new ArrayList<>();
nums.add(200);
nums.add(43); // keep adding. Size will increase
nums.remove(0); // remove to decrease the size - ArrayList can only accept Objects. We will need to use Wrapper classes to add
primitive types.
ArrayList nums = new ArrayList<>();
ArrayList strs = new ArrayList<>(); - ArrayList cannot be multi dimensional.
- ArrayList is a part of Collection framework and it is a type of List interface.
Array data structure is fixed;
Arraylist is dynamic
Array Arraylist
Resizable No Yes
Primitives Yes No
Iterating values for, for each Iterator , for each
Length length variable size method
Performance Fast Slow in comparison
Multidimensional Yes No
Add Elements Assignment operator add method
What is Iterator and difference between for each loop?
- Iterator works with arrayList and not array. It will help us Iterate through the elements.
- Difference is with iterator you can make changes(remove item) to the list while
iterating. within for each loop we can not make changes to our list
Iterator it = list.iterator();
while(it.hasNext()) {
Integer n = it.next();
System.out.println(n);
it.remove();
}
System.out.println(list);
System.out.println(“================”);
for(Integer n : list) {
System.out.println(n);
//list.remove(n);
What is Wrapper Classes ? Why we need wrapper classes?
1.To cover primitive to an object. Use some methods from it.
2.Since most data structures in Java do not support primitive types, We need
Wrapper classes instead of
Integer.parseInt() vs Integer.valueOf();
Wrapper classes: Byte, Short, Integer, Long, Float, Double, Boolean, Character *String is not wrapper class Wrapper classes for primitives How many primitive ? 8
Can one class call another class in main method?
- yes a main method can be called in another class main method
- we can call from different package, as long as we know the method name and we
- have to import it.
- code will compile without main method but will not run
-For ex: calculator is class, add is method
(Calculator.add(1234, 400);
Implicit casting vs explicit casting?
- Implicit Casting: (big => small) int i = 100; double d = i; - Explicit Casting: (small=> big) int n = 12; byte b = (byte)n; - Auto - Boxing Integer num = n; - Un - Boxing int j = Integer.valueOf(num); DON’T WORK: Integer i = new Integer(100); double d = i ; // will not work
What is Thread-safe or Synchronized?
-StringBuffer is thread safe, so it is slower
-StringBuilder is not thread safe, so it is faster
-Both are mutable versions for Strings
-Thread unsafe all four people can go to pizza and pickup the slice at the exact same
time.
-Thread safe first person takes the slice while 2nd is waiting
what is thread:
A sequential or single threaded program has single flow
synchronized:
Means that two threads can not execute the method or access the
variables at the same time and the JVM takes care of enforcing that. it is used to
achieve thread-safety