click below
click below
Normal Size Small Size show me how
Java IQ
Java Interview Questions
| Question | Answer |
|---|---|
| Static keyword in Java | belongs to class and can be called with class name |
| differences between String and StringBuilder | String is not changeable and StringBuilder is changeable |
| Encapsulation | making variables private and giving them access through getters and setters |
| difference between this and this() | this calls variables and methods of same class this() calls constructor of the same class |
| difference between array and arraylist | array is fixed size, does not have methods, and works with any data type ArrayList is resizable, has methods and works with only reference type of data |
| method overloading | methods having same name with different parameters |
| JDK, JRE, and JVM | JDK-Java Development Kit JRE- Java Runtime Environment JVM - Java Virtual Machine |
| autoboxing and unboxing | autoboxing- converting primitive to wrapper class unboxing - converting wrapper class to primitive |
| method overriding | when child class gives different implementation for part class's method |
| private keyword | access modifier that restricts access to outside classes and can only be declared in the same class |
| inheritance | where child class inherits another class which is called parent class, and it inherits variables and methods from parent to child class |
| main two elements of java | instance variables and methods |
| .toString() and why override it | converts a number to a string, when overriding developers customize the way an object is represented |
| type of variables | Primitive: byte, short, int, double, boolean, char Reference: String, StringBuilder, List |
| static block | initialize static variables once we are creating an object, execute only once |
| interface | an abstract data type that defines a list of abstract public methods |
| difference between method and constructor | method is a collection of statements which return value upon execution constructor is a block of code that initializes a newly created object |
| why constructor is needed | used to initialize instance variables of object while object is being created |
| difference between class and object | class is a template for declaring and creating objects object is an instance of a class |
| constructive overloading | allows a class to have multiple constructors with different parameters |
| polymorphism | where object can take many forms(references) |
| static method | belongs to class and can be called with class name |
| class variable | static keyword |
| what methods can static method call | any other static method within the same or different class |
| why we need "this" keyword | calls variables and methods of same class |
| OOP concepts of Java | Encapsulation: making variables private and giving them access through getters and setters Inheritance: where child class inherits another class which is called parent class, and it inherits variables and methods from parent to child class |
| OOP concepts of Java | Abstraction: hiding method implementation Abstract Class: Partial abstraction Interface: full abstraction Polymorphism: where object can take many forms(references) |
| difference between this and super keyword | this calls variables and methods of the same class super calls super class's variables and methods |
| final keyword | cannot be initialized, overridden, or inherited |
| Static and Dynamic Polymorphism | Dynamic Polymorphism points to different method at compile time and calls the overridden method at run time Static calls the method during compile time |
| difference between protected and default access modifiers | protected restricts visibility to child classes default is private practice access |
| Garbage collector | to clean unused objects inside heap memory |
| access modifiers | public, protected, default, private |
| default keyword | acts as a fallback or "else" condition within the switch construct |
| difference between Abstract class and Interface | Abstract class hides the method body while Interface allows multiple inheritance |
| Concrete class | complete implementation of all its methods, cannot have abstract methods and can be instantiated which means create objects in class using "new" |
| key word we use to inherit interface from class | implements |
| which class does the class inherit by default | Object Class |
| what methods cannot be overridden | final, static, private and constructor methods |
| difference between method overloading and method overriding | method overloading means the method name should be the same, parameters should be different, and the return type can be any method overriding access modifier should be the same, return type should be the same or covariant, signature should match |
| how many classes a class can inherit | only one parent class, but as many children classes |
| difference between LinkedList and ArrayList | Arraylist: can only act as a list, store and manage ordered data LinkedList: can be both a list and a queue |
| differences between List, Set, Map | list: collection that maintains a linear order of elements set: collection that does not allow duplicate elements map: collection that associates keys with values |
| difference between HashMap and TreeMap | HashMap: stores key-value pairs TreeMap: provides advantage of maintaining sorted order |
| Exception types | Unchecked: occurs at runtime, optional to handle Checked: show compilation error at compile time, required to handle |
| how to handle exceptions | try catch block: try the code that might throw an exception and catch the code that is executed when execution happens - declaring in method signature: mostly applied for checked exceptions, it will throw it even if declared in method signature |
| finally keyword | block being executed even if we do or don't have exception in try block, to close connection with external files or database |
| Runtime exception | superclass that exceptions are thrown during the normal operation of JVM |
| Error class | subclass of Throwable that indicates serious problems that a reasonable application should not try to catch |
| static block | whenever static keyword is used and associated to block |
| difference between super and this keyword | super is used to refer only to parent's constructor this is used to call current class constructor |